/
functions.php
3887 lines (3051 loc) · 115 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* bbPress Topic Functions
*
* @package bbPress
* @subpackage Functions
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/** Insert ********************************************************************/
/**
* A wrapper for wp_insert_post() that also includes the necessary meta values
* for the topic to function properly.
*
* @since 2.0.0 bbPress (r3349)
*
* @param array $topic_data Forum post data
* @param array $topic_meta Forum meta data
*/
function bbp_insert_topic( $topic_data = array(), $topic_meta = array() ) {
// Parse arguments against default values
$topic_data = bbp_parse_args( $topic_data, array(
'post_parent' => 0, // forum ID
'post_status' => bbp_get_public_status_id(),
'post_type' => bbp_get_topic_post_type(),
'post_author' => bbp_get_current_user_id(),
'post_password' => '',
'post_content' => '',
'post_title' => '',
'comment_status' => 'closed',
'menu_order' => 0
), 'insert_topic' );
// Insert topic
$topic_id = wp_insert_post( $topic_data, false );
// Bail if no topic was added
if ( empty( $topic_id ) ) {
return false;
}
// Parse arguments against default values
$topic_meta = bbp_parse_args( $topic_meta, array(
'author_ip' => bbp_current_author_ip(),
'forum_id' => 0,
'topic_id' => $topic_id,
'voice_count' => 1,
'reply_count' => 0,
'reply_count_hidden' => 0,
'last_reply_id' => 0,
'last_active_id' => $topic_id,
'last_active_time' => get_post_field( 'post_date', $topic_id, 'db' )
), 'insert_topic_meta' );
// Insert topic meta
foreach ( $topic_meta as $meta_key => $meta_value ) {
// Prefix if not prefixed
if ( '_bbp_' !== substr( $meta_key, 0, 5 ) ) {
$meta_key = '_bbp_' . $meta_key;
}
// Update the meta
update_post_meta( $topic_id, $meta_key, $meta_value );
}
// Update the topic and hierarchy
bbp_update_topic( $topic_id, $topic_meta['forum_id'], array(), $topic_data['post_author'], false );
/**
* Fires after topic has been inserted via `bbp_insert_topic`.
*
* @since 2.6.0 bbPress (r6036)
*
* @param int $topic_id The topic id.
* @param int $topic_meta['forum_id'] The topic forum meta.
*/
do_action( 'bbp_insert_topic', (int) $topic_id, (int) $topic_meta['forum_id'] );
// Return topic_id
return $topic_id;
}
/** Post Form Handlers ********************************************************/
/**
* Handles the front end topic submission
*
* @param string $action The requested action to compare this function to
*/
function bbp_new_topic_handler( $action = '' ) {
// Bail if action is not bbp-new-topic
if ( 'bbp-new-topic' !== $action ) {
return;
}
// Nonce check
if ( ! bbp_verify_nonce_request( 'bbp-new-topic' ) ) {
bbp_add_error( 'bbp_new_topic_nonce', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
return;
}
// Define local variable(s)
$view_all = false;
$forum_id = $topic_author = 0;
$topic_title = $topic_content = '';
$anonymous_data = array();
$terms = array( bbp_get_topic_tag_tax_id() => array() );
/** Topic Author **********************************************************/
// User is anonymous
if ( bbp_is_anonymous() ) {
// Filter anonymous data (variable is used later)
$anonymous_data = bbp_filter_anonymous_post_data();
// Anonymous data checks out, so set cookies, etc...
bbp_set_current_anonymous_user_data( $anonymous_data );
// User is logged in
} else {
// User cannot create topics
if ( ! current_user_can( 'publish_topics' ) ) {
bbp_add_error( 'bbp_topic_permission', __( '<strong>Error</strong>: You do not have permission to create new topics.', 'bbpress' ) );
return;
}
// Topic author is current user
$topic_author = bbp_get_current_user_id();
}
// Remove kses filters from title and content for capable users and if the nonce is verified
if ( current_user_can( 'unfiltered_html' ) && ! empty( $_POST['_bbp_unfiltered_html_topic'] ) && wp_create_nonce( 'bbp-unfiltered-html-topic_new' ) === $_POST['_bbp_unfiltered_html_topic'] ) {
remove_filter( 'bbp_new_topic_pre_title', 'wp_filter_kses' );
remove_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad', 10 );
remove_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 );
}
/** Topic Title ***********************************************************/
if ( ! empty( $_POST['bbp_topic_title'] ) ) {
$topic_title = sanitize_text_field( $_POST['bbp_topic_title'] );
}
// Filter and sanitize
$topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
// No topic title
if ( empty( $topic_title ) ) {
bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
}
// Title too long
if ( bbp_is_title_too_long( $topic_title ) ) {
bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
}
/** Topic Content *********************************************************/
if ( ! empty( $_POST['bbp_topic_content'] ) ) {
$topic_content = $_POST['bbp_topic_content'];
}
// Filter and sanitize
$topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
// No topic content
if ( empty( $topic_content ) ) {
bbp_add_error( 'bbp_topic_content', __( '<strong>Error</strong>: Your topic cannot be empty.', 'bbpress' ) );
}
/** Topic Forum ***********************************************************/
// Error check the POST'ed topic id
if ( isset( $_POST['bbp_forum_id'] ) ) {
// Empty Forum id was passed
if ( empty( $_POST['bbp_forum_id'] ) ) {
bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
// Forum id is not a number
} elseif ( ! is_numeric( $_POST['bbp_forum_id'] ) ) {
bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID must be a number.', 'bbpress' ) );
// Forum id might be valid
} else {
// Get the forum id
$posted_forum_id = intval( $_POST['bbp_forum_id'] );
// Forum id is empty
if ( 0 === $posted_forum_id ) {
bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
// Forum id is a negative number
} elseif ( 0 > $posted_forum_id ) {
bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID cannot be a negative number.', 'bbpress' ) );
// Forum does not exist
} elseif ( ! bbp_get_forum( $posted_forum_id ) ) {
bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum does not exist.', 'bbpress' ) );
// Use the POST'ed forum id
} else {
$forum_id = $posted_forum_id;
}
}
}
// Forum exists
if ( ! empty( $forum_id ) ) {
// Forum is a category
if ( bbp_is_forum_category( $forum_id ) ) {
bbp_add_error( 'bbp_new_topic_forum_category', __( '<strong>Error</strong>: This forum is a category. No topics can be created in this forum.', 'bbpress' ) );
// Forum is not a category
} else {
// Forum is closed and user cannot access
if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
bbp_add_error( 'bbp_new_topic_forum_closed', __( '<strong>Error</strong>: This forum has been closed to new topics.', 'bbpress' ) );
}
// Forum is private and user cannot access
if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
bbp_add_error( 'bbp_new_topic_forum_private', __( '<strong>Error</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
// Forum is hidden and user cannot access
} elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
bbp_add_error( 'bbp_new_topic_forum_hidden', __( '<strong>Error</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
}
}
}
/** Topic Flooding ********************************************************/
if ( ! bbp_check_for_flood( $anonymous_data, $topic_author ) ) {
bbp_add_error( 'bbp_topic_flood', __( '<strong>Error</strong>: Slow down; you move too fast.', 'bbpress' ) );
}
/** Topic Duplicate *******************************************************/
$dupe_args = array(
'post_type' => bbp_get_topic_post_type(),
'post_author' => $topic_author,
'post_content' => $topic_content,
'post_parent' => $forum_id,
'anonymous_data' => $anonymous_data
);
if ( ! bbp_check_for_duplicate( $dupe_args ) ) {
bbp_add_error( 'bbp_topic_duplicate', __( '<strong>Error</strong>: Duplicate topic detected; it looks as though you’ve already said that.', 'bbpress' ) );
}
/** Topic Bad Words *******************************************************/
if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content, true ) ) {
bbp_add_error( 'bbp_topic_moderation', __( '<strong>Error</strong>: Your topic cannot be created at this time.', 'bbpress' ) );
}
/** Topic Status **********************************************************/
// Get available topic statuses
$topic_statuses = bbp_get_topic_statuses();
// Default to published
$topic_status = bbp_get_public_status_id();
// Maybe force into pending
if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
$topic_status = bbp_get_pending_status_id();
// Check for possible posted topic status
} elseif ( ! empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
// Allow capable users to explicitly override the status
if ( current_user_can( 'moderate', $forum_id ) ) {
$topic_status = sanitize_key( $_POST['bbp_topic_status'] );
// Not capable
} else {
bbp_add_error( 'bbp_new_topic_status', __( '<strong>Error</strong>: You do not have permission to do that.', 'bbpress' ) );
}
}
/** Topic Tags ************************************************************/
if ( bbp_allow_topic_tags() && ! empty( $_POST['bbp_topic_tags'] ) ) {
// Escape tag input
$terms = sanitize_text_field( $_POST['bbp_topic_tags'] );
// Explode by comma
if ( strstr( $terms, ',' ) ) {
$terms = explode( ',', $terms );
}
// Add topic tag ID as main key
$terms = array( bbp_get_topic_tag_tax_id() => $terms );
}
/** Additional Actions (Before Save) **************************************/
do_action( 'bbp_new_topic_pre_extras', $forum_id );
// Bail if errors
if ( bbp_has_errors() ) {
return;
}
/** No Errors *************************************************************/
// Add the content of the form to $topic_data as an array.
// Just in time manipulation of topic data before being created
$topic_data = apply_filters( 'bbp_new_topic_pre_insert', array(
'post_author' => $topic_author,
'post_title' => $topic_title,
'post_content' => $topic_content,
'post_status' => $topic_status,
'post_parent' => $forum_id,
'post_type' => bbp_get_topic_post_type(),
'tax_input' => $terms,
'comment_status' => 'closed'
) );
// Insert topic
$topic_id = wp_insert_post( $topic_data, true );
/** No Errors *************************************************************/
if ( ! empty( $topic_id ) && ! is_wp_error( $topic_id ) ) {
/** Close Check *******************************************************/
// If the topic is closed, close it properly
if ( ( get_post_field( 'post_status', $topic_id ) === bbp_get_closed_status_id() ) || ( $topic_data['post_status'] === bbp_get_closed_status_id() ) ) {
// Close the topic
bbp_close_topic( $topic_id );
}
/** Trash Check *******************************************************/
// If the forum is trash, or the topic_status is switched to
// trash, trash the topic properly
if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $topic_data['post_status'] === bbp_get_trash_status_id() ) ) {
// Trash the topic
wp_trash_post( $topic_id );
// Force view=all
$view_all = true;
}
/** Spam Check ********************************************************/
// If the topic is spam, officially spam this topic
if ( $topic_data['post_status'] === bbp_get_spam_status_id() ) {
add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
// Force view=all
$view_all = true;
}
/** Update counts, etc... *********************************************/
do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
/** Additional Actions (After Save) ***********************************/
do_action( 'bbp_new_topic_post_extras', $topic_id );
/** Redirect **********************************************************/
// Redirect to
$redirect_to = bbp_get_redirect_to();
// Get the topic URL
$redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
// Add view all?
if ( bbp_get_view_all() || ! empty( $view_all ) ) {
// User can moderate, so redirect to topic with view all set
if ( current_user_can( 'moderate', $topic_id ) ) {
$redirect_url = bbp_add_view_all( $redirect_url );
// User cannot moderate, so redirect to forum
} else {
$redirect_url = bbp_get_forum_permalink( $forum_id );
}
}
// Allow to be filtered
$redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id );
/** Successful Save ***************************************************/
// Redirect back to new topic
bbp_redirect( $redirect_url );
/** Errors ****************************************************************/
// WP_Error
} elseif ( is_wp_error( $topic_id ) ) {
bbp_add_error( 'bbp_topic_error', sprintf( __( '<strong>Error</strong>: The following problem(s) occurred: %s', 'bbpress' ), $topic_id->get_error_message() ) );
// Generic error
} else {
bbp_add_error( 'bbp_topic_error', __( '<strong>Error</strong>: The topic was not created.', 'bbpress' ) );
}
}
/**
* Handles the front end edit topic submission
*
* @param string $action The requested action to compare this function to
*/
function bbp_edit_topic_handler( $action = '' ) {
// Bail if action is not bbp-edit-topic
if ( 'bbp-edit-topic' !== $action ) {
return;
}
// Define local variable(s)
$revisions_removed = false;
$topic = $topic_id = $topic_author = $forum_id = 0;
$topic_title = $topic_content = $topic_edit_reason = '';
$anonymous_data = array();
/** Topic *****************************************************************/
// Topic id was not passed
if ( empty( $_POST['bbp_topic_id'] ) ) {
bbp_add_error( 'bbp_edit_topic_id', __( '<strong>Error</strong>: Topic ID not found.', 'bbpress' ) );
return;
// Topic id was passed
} elseif ( is_numeric( $_POST['bbp_topic_id'] ) ) {
$topic_id = (int) $_POST['bbp_topic_id'];
$topic = bbp_get_topic( $topic_id );
}
// Topic does not exist
if ( empty( $topic ) ) {
bbp_add_error( 'bbp_edit_topic_not_found', __( '<strong>Error</strong>: The topic you want to edit was not found.', 'bbpress' ) );
return;
// Topic exists
} else {
// Check users ability to create new topic
if ( ! bbp_is_topic_anonymous( $topic_id ) ) {
// User cannot edit this topic
if ( ! current_user_can( 'edit_topic', $topic_id ) ) {
bbp_add_error( 'bbp_edit_topic_permission', __( '<strong>Error</strong>: You do not have permission to edit that topic.', 'bbpress' ) );
}
// Set topic author
$topic_author = bbp_get_topic_author_id( $topic_id );
// It is an anonymous post
} else {
// Filter anonymous data
$anonymous_data = bbp_filter_anonymous_post_data();
}
}
// Nonce check
if ( ! bbp_verify_nonce_request( 'bbp-edit-topic_' . $topic_id ) ) {
bbp_add_error( 'bbp_edit_topic_nonce', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
return;
}
// Remove kses filters from title and content for capable users and if the nonce is verified
if ( current_user_can( 'unfiltered_html' ) && ! empty( $_POST['_bbp_unfiltered_html_topic'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-topic_' . $topic_id ) === $_POST['_bbp_unfiltered_html_topic'] ) ) {
remove_filter( 'bbp_edit_topic_pre_title', 'wp_filter_kses' );
remove_filter( 'bbp_edit_topic_pre_content', 'bbp_encode_bad', 10 );
remove_filter( 'bbp_edit_topic_pre_content', 'bbp_filter_kses', 30 );
}
/** Topic Forum ***********************************************************/
// Forum id was not passed
if ( empty( $_POST['bbp_forum_id'] ) ) {
bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is missing.', 'bbpress' ) );
// Forum id was passed
} elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) {
$forum_id = (int) $_POST['bbp_forum_id'];
}
// Current forum this topic is in
$current_forum_id = bbp_get_topic_forum_id( $topic_id );
// Forum exists
if ( ! empty( $forum_id ) && ( $forum_id !== $current_forum_id ) ) {
// Forum is a category
if ( bbp_is_forum_category( $forum_id ) ) {
bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>Error</strong>: This forum is a category. No topics can be created in it.', 'bbpress' ) );
// Forum is not a category
} else {
// Forum is closed and user cannot access
if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>Error</strong>: This forum has been closed to new topics.', 'bbpress' ) );
}
// Forum is private and user cannot access
if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>Error</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
// Forum is hidden and user cannot access
} elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>Error</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
}
}
}
/** Topic Title ***********************************************************/
if ( ! empty( $_POST['bbp_topic_title'] ) ) {
$topic_title = sanitize_text_field( $_POST['bbp_topic_title'] );
}
// Filter and sanitize
$topic_title = apply_filters( 'bbp_edit_topic_pre_title', $topic_title, $topic_id );
// No topic title
if ( empty( $topic_title ) ) {
bbp_add_error( 'bbp_edit_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
}
// Title too long
if ( bbp_is_title_too_long( $topic_title ) ) {
bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
}
/** Topic Content *********************************************************/
if ( ! empty( $_POST['bbp_topic_content'] ) ) {
$topic_content = $_POST['bbp_topic_content'];
}
// Filter and sanitize
$topic_content = apply_filters( 'bbp_edit_topic_pre_content', $topic_content, $topic_id );
// No topic content
if ( empty( $topic_content ) ) {
bbp_add_error( 'bbp_edit_topic_content', __( '<strong>Error</strong>: Your topic cannot be empty.', 'bbpress' ) );
}
/** Topic Bad Words *******************************************************/
if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content, true ) ) {
bbp_add_error( 'bbp_topic_moderation', __( '<strong>Error</strong>: Your topic cannot be edited at this time.', 'bbpress' ) );
}
/** Topic Status **********************************************************/
// Get available topic statuses
$topic_statuses = bbp_get_topic_statuses( $topic_id );
// Use existing post_status
$topic_status = $topic->post_status;
// Maybe force into pending
if ( bbp_is_topic_public( $topic->ID ) && ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
$topic_status = bbp_get_pending_status_id();
// Check for possible posted topic status
} elseif ( ! empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
// Allow capable users to explicitly override the status
if ( current_user_can( 'moderate', $forum_id ) ) {
$topic_status = sanitize_key( $_POST['bbp_topic_status'] );
// Not capable
} else {
bbp_add_error( 'bbp_edit_topic_status', __( '<strong>Error</strong>: You do not have permission to do that.', 'bbpress' ) );
}
}
/** Topic Tags ************************************************************/
// Either replace terms
if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags', $topic_id ) && ! empty( $_POST['bbp_topic_tags'] ) ) {
// Escape tag input
$terms = sanitize_text_field( $_POST['bbp_topic_tags'] );
// Explode by comma
if ( strstr( $terms, ',' ) ) {
$terms = explode( ',', $terms );
}
// Add topic tag ID as main key
$terms = array( bbp_get_topic_tag_tax_id() => $terms );
// ...or remove them.
} elseif ( isset( $_POST['bbp_topic_tags'] ) ) {
$terms = array( bbp_get_topic_tag_tax_id() => array() );
// Existing terms
} else {
$terms = array( bbp_get_topic_tag_tax_id() => explode( ',', bbp_get_topic_tag_names( $topic_id, ',' ) ) );
}
/** Additional Actions (Before Save) **************************************/
do_action( 'bbp_edit_topic_pre_extras', $topic_id );
// Bail if errors
if ( bbp_has_errors() ) {
return;
}
/** No Errors *************************************************************/
// Add the content of the form to $topic_data as an array
// Just in time manipulation of topic data before being edited
$topic_data = apply_filters( 'bbp_edit_topic_pre_insert', array(
'ID' => $topic_id,
'post_title' => $topic_title,
'post_content' => $topic_content,
'post_status' => $topic_status,
'post_parent' => $forum_id,
'post_author' => $topic_author,
'post_type' => bbp_get_topic_post_type(),
'tax_input' => $terms,
) );
// Toggle revisions to avoid duplicates
if ( post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) {
$revisions_removed = true;
remove_post_type_support( bbp_get_topic_post_type(), 'revisions' );
}
// Insert topic
$topic_id = wp_update_post( $topic_data );
// Toggle revisions back on
if ( true === $revisions_removed ) {
$revisions_removed = false;
add_post_type_support( bbp_get_topic_post_type(), 'revisions' );
}
/** No Errors *************************************************************/
if ( ! empty( $topic_id ) && ! is_wp_error( $topic_id ) ) {
// Update counts, etc...
do_action( 'bbp_edit_topic', $topic_id, $forum_id, $anonymous_data, $topic_author, true /* Is edit */ );
/** Revisions *********************************************************/
// Revision Reason
if ( ! empty( $_POST['bbp_topic_edit_reason'] ) ) {
$topic_edit_reason = sanitize_text_field( $_POST['bbp_topic_edit_reason'] );
}
// Update revision log
if ( ! empty( $_POST['bbp_log_topic_edit'] ) && ( '1' === $_POST['bbp_log_topic_edit'] ) ) {
$revision_id = wp_save_post_revision( $topic_id );
if ( ! empty( $revision_id ) ) {
bbp_update_topic_revision_log( array(
'topic_id' => $topic_id,
'revision_id' => $revision_id,
'author_id' => bbp_get_current_user_id(),
'reason' => $topic_edit_reason
) );
}
}
/** Move Topic ********************************************************/
// If the new forum id is not equal to the old forum id, run the
// bbp_move_topic action and pass the topic's forum id as the
// first arg and topic id as the second to update counts.
if ( $forum_id !== $topic->post_parent ) {
bbp_move_topic_handler( $topic_id, $topic->post_parent, $forum_id );
}
/** Additional Actions (After Save) ***********************************/
do_action( 'bbp_edit_topic_post_extras', $topic_id );
/** Redirect **********************************************************/
// Redirect to
$redirect_to = bbp_get_redirect_to();
// View all?
$view_all = bbp_get_view_all( 'edit_others_replies' );
// Get the topic URL
$topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
// Add view all?
if ( ! empty( $view_all ) ) {
$topic_url = bbp_add_view_all( $topic_url );
}
// Allow to be filtered
$topic_url = apply_filters( 'bbp_edit_topic_redirect_to', $topic_url, $view_all, $redirect_to );
/** Successful Edit ***************************************************/
// Redirect back to new topic
bbp_redirect( $topic_url );
/** Errors ****************************************************************/
} else {
$append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
bbp_add_error( 'bbp_topic_error', __( '<strong>Error</strong>: The following problem(s) have been found with your topic:' . $append_error . 'Please try again.', 'bbpress' ) );
}
}
/**
* Handle all the extra meta stuff from posting a new topic
*
* @param int $topic_id Optional. Topic id
* @param int $forum_id Optional. Forum id
* @param array $anonymous_data Optional - if it's an anonymous post. Do not
* supply if supplying $author_id. Should be
* sanitized (see {@link bbp_filter_anonymous_post_data()}
* @param int $author_id Author id
* @param bool $is_edit Optional. Is the post being edited? Defaults to false.
*/
function bbp_update_topic( $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $author_id = 0, $is_edit = false ) {
// Validate the ID's passed from 'bbp_new_topic' action
$topic_id = bbp_get_topic_id( $topic_id );
$forum_id = bbp_get_forum_id( $forum_id );
// Get the current user ID
$user_id = bbp_get_current_user_id();
// Bail if there is no topic
if ( empty( $topic_id ) ) {
return;
}
// Check author_id, fallback to current user ID
if ( empty( $author_id ) ) {
$author_id = $user_id;
}
// Check forum_id, fallback to post_parent or meta
if ( empty( $forum_id ) ) {
$forum_id = bbp_get_topic_forum_id( $topic_id );
}
// Update locks
update_post_meta( $topic_id, '_edit_last', $user_id );
delete_post_meta( $topic_id, '_edit_lock' );
// Forum/Topic meta (early, for use in downstream functions)
bbp_update_topic_forum_id( $topic_id, $forum_id );
bbp_update_topic_topic_id( $topic_id, $topic_id );
// If anonymous post, store name, email, website and ip in post_meta.
if ( ! empty( $anonymous_data ) ) {
// Update anonymous meta data (not cookies)
bbp_update_anonymous_post_author( $topic_id, $anonymous_data, bbp_get_topic_post_type() );
// Set transient for throttle check (only on new, not edit)
if ( empty( $is_edit ) ) {
set_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted', time(), HOUR_IN_SECONDS );
}
}
// Handle Subscription Checkbox
if ( bbp_is_subscriptions_active() && ! empty( $author_id ) ) {
// Check if subscribed
$subscribed = bbp_is_user_subscribed( $author_id, $topic_id );
// Check for action
$subscheck = ( ! empty( $_POST['bbp_topic_subscription'] ) && ( 'bbp_subscribe' === $_POST['bbp_topic_subscription'] ) )
? true
: false;
// Subscribed and unsubscribing
if ( ( true === $subscribed ) && ( false === $subscheck ) ) {
bbp_remove_user_subscription( $author_id, $topic_id );
// Not subscribed and subscribing
} elseif ( ( false === $subscribed ) && ( true === $subscheck ) ) {
bbp_add_user_subscription( $author_id, $topic_id );
}
}
// Get the topic types
$topic_types = bbp_get_topic_types( $topic_id );
// Sticky check after 'bbp_new_topic' action so forum ID meta is set
if ( ! empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array_keys( $topic_types ), true ) ) {
// What's the caps?
if ( current_user_can( 'moderate', $topic_id ) ) {
// What's the haps?
switch ( $_POST['bbp_stick_topic'] ) {
// Sticky in this forum
case 'stick' :
bbp_stick_topic( $topic_id );
break;
// Super sticky in all forums
case 'super' :
bbp_stick_topic( $topic_id, true );
break;
// Unsticky from everywhere
case 'unstick' :
default :
bbp_unstick_topic( $topic_id );
break;
}
}
}
// Update associated topic values if this is a new topic
if ( empty( $is_edit ) ) {
// Update poster activity time
bbp_update_user_last_posted( $author_id );
// Update poster IP
update_post_meta( $topic_id, '_bbp_author_ip', bbp_current_author_ip(), false );
// Last active time
$last_active = get_post_field( 'post_date', $topic_id );
// Reply topic meta
bbp_update_topic_last_reply_id ( $topic_id, 0 );
bbp_update_topic_last_active_id ( $topic_id, $topic_id );
bbp_update_topic_last_active_time ( $topic_id, $last_active );
bbp_update_topic_reply_count ( $topic_id, 0 );
bbp_update_topic_reply_count_hidden ( $topic_id, 0 );
bbp_update_topic_voice_count ( $topic_id );
// Walk up ancestors and do the dirty work
bbp_update_topic_walker( $topic_id, $last_active, $forum_id, 0, false );
}
// Bump the custom query cache
wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );
}
/**
* Walks up the post_parent tree from the current topic_id, and updates the
* meta data of forums above it. This calls several functions that all run
* manual queries against the database to get their results. As such, this
* function can be costly to run but is necessary to keep everything accurate.
*
* @since 2.0.0 bbPress (r2800)
*
* @param int $topic_id Topic id
* @param string $last_active_time Optional. Last active time
* @param int $forum_id Optional. Forum id
* @param int $reply_id Optional. Reply id
* @param bool $refresh Reset all the previous parameters? Defaults to true.
*/
function bbp_update_topic_walker( $topic_id, $last_active_time = '', $forum_id = 0, $reply_id = 0, $refresh = true ) {
// Validate topic_id
$topic_id = bbp_get_topic_id( $topic_id );
// Define local variable(s)
$active_id = 0;
// Topic was passed
if ( ! empty( $topic_id ) ) {
// Get the forum ID if none was passed
if ( empty( $forum_id ) ) {
$forum_id = bbp_get_topic_forum_id( $topic_id );
}
// Set the active_id based on topic_id/reply_id
$active_id = empty( $reply_id ) ? $topic_id : $reply_id;
}
// Get topic ancestors
$ancestors = array_values( array_unique( array_merge( array( $forum_id ), (array) get_post_ancestors( $topic_id ) ) ) );
// Topic status
$topic_status = get_post_status( $topic_id );
// If we want a full refresh, unset any of the possibly passed variables
if ( true === $refresh ) {
$forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
$topic_status = bbp_get_public_status_id();
}
// Loop through ancestors
if ( ! empty( $ancestors ) ) {
foreach ( $ancestors as $ancestor ) {
// If ancestor is a forum, update counts
if ( bbp_is_forum( $ancestor ) ) {
// Get the forum
$forum = bbp_get_forum( $ancestor );
// Update the forum
bbp_update_forum( array(
'forum_id' => $forum->ID,
'post_parent' => $forum->post_parent,
'last_topic_id' => $topic_id,
'last_reply_id' => $reply_id,
'last_active_id' => $active_id,
'last_active_time' => $last_active_time,
'last_active_status' => $topic_status
) );
}
}
}
}
/**
* Handle the moving of a topic from one forum to another. This includes walking
* up the old and new branches and updating the counts.
*
* @since 2.0.0 bbPress (r2907)
*
* @param int $topic_id The topic id.
* @param int $old_forum_id Old forum id.
* @param int $new_forum_id New forum id.
*/
function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) {
// Validate parameters
$topic_id = bbp_get_topic_id( $topic_id );
$old_forum_id = bbp_get_forum_id( $old_forum_id );
$new_forum_id = bbp_get_forum_id( $new_forum_id );
// Clean old and new forum caches before proceeding, to ensure subsequent
// calls to forum objects are using updated data.
clean_post_cache( $old_forum_id );
clean_post_cache( $new_forum_id );
// Update topic forum's ID
bbp_update_topic_forum_id( $topic_id, $new_forum_id );
// Update topic post parent with the new forum ID
wp_update_post( array(
'ID' => $topic_id,
'post_parent' => $new_forum_id,
) );
/** Stickies **************************************************************/
// Get forum stickies
$old_stickies = bbp_get_stickies( $old_forum_id );
// Only proceed if stickies are found
if ( ! empty( $old_stickies ) ) {
// Define local variables
$updated_stickies = array();
// Loop through stickies of forum and add misses to the updated array
foreach ( (array) $old_stickies as $sticky_topic_id ) {
if ( $topic_id !== $sticky_topic_id ) {
$updated_stickies[] = $sticky_topic_id;
}
}
// If stickies are different, update or delete them
if ( $updated_stickies !== $old_stickies ) {
// No more stickies so delete the meta
if ( empty( $updated_stickies ) ) {
delete_post_meta( $old_forum_id, '_bbp_sticky_topics' );
// Still stickies so update the meta
} else {
update_post_meta( $old_forum_id, '_bbp_sticky_topics', $updated_stickies );
}