This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
/
bbp-forum-functions.php
2201 lines (1775 loc) · 74.2 KB
/
bbp-forum-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 Forum Functions
*
* @package bbPress
* @subpackage Functions
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/** Insert ********************************************************************/
/**
* A wrapper for wp_insert_post() that also includes the necessary meta values
* for the forum to function properly.
*
* @since bbPress (r3349)
*
* @uses bbp_parse_args()
* @uses bbp_get_forum_post_type()
* @uses wp_insert_post()
* @uses update_post_meta()
*
* @param array $forum_data Forum post data
* @param arrap $forum_meta Forum meta data
*/
function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) {
// Forum
$default_forum = array(
'post_parent' => 0, // forum ID
'post_status' => bbp_get_public_status_id(),
'post_type' => bbp_get_forum_post_type(),
'post_author' => bbp_get_current_user_id(),
'post_password' => '',
'post_content' => '',
'post_title' => '',
'menu_order' => 0,
'comment_status' => 'closed'
);
$forum_data = bbp_parse_args( $forum_data, $default_forum, 'insert_forum' );
// Insert forum
$forum_id = wp_insert_post( $forum_data );
// Bail if no forum was added
if ( empty( $forum_id ) )
return false;
// Forum meta
$default_meta = array(
'reply_count' => 0,
'topic_count' => 0,
'topic_count_hidden' => 0,
'total_reply_count' => 0,
'total_topic_count' => 0,
'last_topic_id' => 0,
'last_reply_id' => 0,
'last_active_id' => 0,
'last_active_time' => 0,
'forum_subforum_count' => 0,
);
$forum_meta = bbp_parse_args( $forum_meta, $default_meta, 'insert_forum_meta' );
// Insert forum meta
foreach ( $forum_meta as $meta_key => $meta_value )
update_post_meta( $forum_id, '_bbp_' . $meta_key, $meta_value );
// Return new forum ID
return $forum_id;
}
/** Post Form Handlers ********************************************************/
/**
* Handles the front end forum submission
*
* @uses bbPress:errors::add() To log various error messages
* @uses check_admin_referer() To verify the nonce and check the referer
* @uses bbp_is_anonymous() To check if an anonymous post is being made
* @uses current_user_can() To check if the current user can publish forum
* @uses bbp_get_current_user_id() To get the current user id
* @uses bbp_filter_anonymous_post_data() To filter anonymous data
* @uses bbp_set_current_anonymous_user_data() To set the anonymous user cookies
* @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
* @uses esc_attr() For sanitization
* @uses bbp_is_forum_category() To check if the forum is a category
* @uses bbp_is_forum_closed() To check if the forum is closed
* @uses bbp_is_forum_private() To check if the forum is private
* @uses bbp_check_for_flood() To check for flooding
* @uses bbp_check_for_duplicate() To check for duplicates
* @uses bbp_get_forum_post_type() To get the forum post type
* @uses remove_filter() To remove 'wp_filter_kses' filters if needed
* @uses apply_filters() Calls 'bbp_new_forum_pre_title' with the content
* @uses apply_filters() Calls 'bbp_new_forum_pre_content' with the content
* @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
* @uses wp_insert_post() To insert the forum
* @uses do_action() Calls 'bbp_new_forum' with the forum id, forum id,
* anonymous data and reply author
* @uses bbp_stick_forum() To stick or super stick the forum
* @uses bbp_unstick_forum() To unstick the forum
* @uses bbp_get_forum_permalink() To get the forum permalink
* @uses wp_safe_redirect() To redirect to the forum link
* @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
* messages
*/
function bbp_new_forum_handler() {
// Bail if not a POST action
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
return;
// Bail if action is not bbp-new-forum
if ( empty( $_POST['action'] ) || ( 'bbp-new-forum' !== $_POST['action'] ) )
return;
// Nonce check
check_admin_referer( 'bbp-new-forum' );
// Define local variable(s)
$view_all = $anonymous_data = false;
$forum_parent_id = $forum_author = 0;
$forum_title = $forum_content = '';
/** Forum Author **********************************************************/
// User cannot create forums
if ( !current_user_can( 'publish_forums' ) ) {
bbp_add_error( 'bbp_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new forums.', 'bbpress' ) );
}
// Forum author is current user
$forum_author = bbp_get_current_user_id();
// Remove wp_filter_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_forum'] ) && wp_create_nonce( 'bbp-unfiltered-html-forum_new' ) == $_POST['_bbp_unfiltered_html_forum'] ) {
remove_filter( 'bbp_new_forum_pre_title', 'wp_filter_kses' );
remove_filter( 'bbp_new_forum_pre_content', 'wp_filter_kses' );
}
/** Forum Title ***********************************************************/
if ( !empty( $_POST['bbp_forum_title'] ) )
$forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) );
// Filter and sanitize
$forum_title = apply_filters( 'bbp_new_forum_pre_title', $forum_title );
// No forum title
if ( empty( $forum_title ) )
bbp_add_error( 'bbp_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) );
/** Forum Content *********************************************************/
if ( !empty( $_POST['bbp_forum_content'] ) )
$forum_content = $_POST['bbp_forum_content'];
// Filter and sanitize
$forum_content = apply_filters( 'bbp_new_forum_pre_content', $forum_content );
// No forum content
if ( empty( $forum_content ) )
bbp_add_error( 'bbp_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) );
/** Forum Parent **********************************************************/
// Forum parent was passed (the norm)
if ( !empty( $_POST['bbp_forum_parent_id'] ) )
$forum_parent_id = (int) $_POST['bbp_forum_parent_id'];
// Filter and sanitize
$forum_parent_id = apply_filters( 'bbp_new_forum_pre_parent_id', $forum_parent_id );
// No forum parent was passed (should never happen)
if ( empty( $forum_parent_id ) ) {
bbp_add_error( 'bbp_new_forum_missing_parent', __( '<strong>ERROR</strong>: Your forum must have a parent.', 'bbpress' ) );
// Forum exists
} elseif ( !empty( $forum_parent_id ) ) {
// Forum is a category
if ( bbp_is_forum_category( $forum_parent_id ) ) {
bbp_add_error( 'bbp_new_forum_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No forums can be created in this forum.', 'bbpress' ) );
}
// Forum is closed and user cannot access
if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) {
bbp_add_error( 'bbp_new_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) );
}
// Forum is private and user cannot access
if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) {
bbp_add_error( 'bbp_new_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
}
// Forum is hidden and user cannot access
if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) {
bbp_add_error( 'bbp_new_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
}
}
/** Forum Flooding ********************************************************/
if ( !bbp_check_for_flood( $anonymous_data, $forum_author ) )
bbp_add_error( 'bbp_forum_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
/** Forum Duplicate *******************************************************/
if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_forum_post_type(), 'post_author' => $forum_author, 'post_content' => $forum_content, 'anonymous_data' => $anonymous_data ) ) )
bbp_add_error( 'bbp_forum_duplicate', __( '<strong>ERROR</strong>: This forum already exists.', 'bbpress' ) );
/** Forum Blacklist *******************************************************/
if ( !bbp_check_for_blacklist( $anonymous_data, $forum_author, $forum_title, $forum_content ) )
bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be created at this time.', 'bbpress' ) );
/** Forum Moderation ******************************************************/
$post_status = bbp_get_public_status_id();
if ( !bbp_check_for_moderation( $anonymous_data, $forum_author, $forum_title, $forum_content ) )
$post_status = bbp_get_pending_status_id();
/** Additional Actions (Before Save) **************************************/
do_action( 'bbp_new_forum_pre_extras' );
/** No Errors *************************************************************/
if ( !bbp_has_errors() ) {
/** Create new forum **************************************************/
// Add the content of the form to $forum_data as an array
$forum_data = array(
'post_author' => $forum_author,
'post_title' => $forum_title,
'post_content' => $forum_content,
'post_parent' => $forum_parent_id,
'post_status' => $post_status,
'post_type' => bbp_get_forum_post_type(),
'comment_status' => 'closed'
);
// Just in time manipulation of forum data before being created
$forum_data = apply_filters( 'bbp_new_forum_pre_insert', $forum_data );
// Insert forum
$forum_id = wp_insert_post( $forum_data );
/** No Errors *********************************************************/
if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) {
/** Trash Check ***************************************************/
// If the forum is trash, or the forum_status is switched to
// trash, trash it properly
if ( ( get_post_field( 'post_status', $forum_id ) == bbp_get_trash_status_id() ) || ( $forum_data['post_status'] == bbp_get_trash_status_id() ) ) {
// Trash the reply
wp_trash_post( $forum_id );
// Force view=all
$view_all = true;
}
/** Spam Check ****************************************************/
// If reply or forum are spam, officially spam this reply
if ( $forum_data['post_status'] == bbp_get_spam_status_id() ) {
add_post_meta( $forum_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
// Force view=all
$view_all = true;
}
/** Update counts, etc... *****************************************/
$forum_args = array(
'forum_id' => $forum_id,
'post_parent' => $forum_parent_id,
'forum_author' => $forum_author,
'last_topic_id' => 0,
'last_reply_id' => 0,
'last_active_id' => 0,
'last_active_time' => 0,
'last_active_status' => bbp_get_public_status_id()
);
do_action( 'bbp_new_forum', $forum_args );
/** Additional Actions (After Save) *******************************/
do_action( 'bbp_new_forum_post_extras', $forum_id );
/** Redirect ******************************************************/
// Redirect to
$redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
// Get the forum URL
$redirect_url = bbp_get_forum_permalink( $forum_id, $redirect_to );
// Add view all?
if ( bbp_get_view_all() || !empty( $view_all ) ) {
// User can moderate, so redirect to forum with view all set
if ( current_user_can( 'moderate' ) ) {
$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_forum_redirect_to', $redirect_url, $redirect_to );
/** Successful Save ***********************************************/
// Redirect back to new forum
wp_safe_redirect( $redirect_url );
// For good measure
exit();
// Errors
} else {
$append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : '';
bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error, 'bbpress' ) );
}
}
}
/**
* Handles the front end edit forum submission
*
* @uses bbPress:errors::add() To log various error messages
* @uses bbp_get_forum() To get the forum
* @uses check_admin_referer() To verify the nonce and check the referer
* @uses bbp_is_forum_anonymous() To check if forum is by an anonymous user
* @uses current_user_can() To check if the current user can edit the forum
* @uses bbp_filter_anonymous_post_data() To filter anonymous data
* @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
* @uses esc_attr() For sanitization
* @uses bbp_is_forum_category() To check if the forum is a category
* @uses bbp_is_forum_closed() To check if the forum is closed
* @uses bbp_is_forum_private() To check if the forum is private
* @uses remove_filter() To remove 'wp_filter_kses' filters if needed
* @uses apply_filters() Calls 'bbp_edit_forum_pre_title' with the title and
* forum id
* @uses apply_filters() Calls 'bbp_edit_forum_pre_content' with the content
* and forum id
* @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
* @uses wp_save_post_revision() To save a forum revision
* @uses bbp_update_forum_revision_log() To update the forum revision log
* @uses wp_update_post() To update the forum
* @uses do_action() Calls 'bbp_edit_forum' with the forum id, forum id,
* anonymous data and reply author
* @uses bbp_move_forum_handler() To handle movement of a forum from one forum
* to another
* @uses bbp_get_forum_permalink() To get the forum permalink
* @uses wp_safe_redirect() To redirect to the forum link
* @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
* messages
*/
function bbp_edit_forum_handler() {
// Bail if not a POST action
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
return;
// Bail if action is not bbp-edit-forum
if ( empty( $_POST['action'] ) || ( 'bbp-edit-forum' !== $_POST['action'] ) )
return;
// Define local variable(s)
$anonymous_data = array();
$forum = $forum_id = $forum_parent_id = 0;
$forum_title = $forum_content = $forum_edit_reason = '';
/** Forum *****************************************************************/
// Forum id was not passed
if ( empty( $_POST['bbp_forum_id'] ) ) {
bbp_add_error( 'bbp_edit_forum_id', __( '<strong>ERROR</strong>: Forum ID not found.', 'bbpress' ) );
// Forum id was passed
} elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) {
$forum_id = (int) $_POST['bbp_forum_id'];
$forum = bbp_get_forum( $forum_id );
}
// Forum does not exist
if ( empty( $forum ) ) {
bbp_add_error( 'bbp_edit_forum_not_found', __( '<strong>ERROR</strong>: The forum you want to edit was not found.', 'bbpress' ) );
// Forum exists
} else {
// Nonce check
check_admin_referer( 'bbp-edit-forum_' . $forum_id );
// User cannot edit this forum
if ( !current_user_can( 'edit_forum', $forum_id ) ) {
bbp_add_error( 'bbp_edit_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that forum.', 'bbpress' ) );
}
}
// Remove wp_filter_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_forum'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-forum_' . $forum_id ) == $_POST['_bbp_unfiltered_html_forum'] ) ) {
remove_filter( 'bbp_edit_forum_pre_title', 'wp_filter_kses' );
remove_filter( 'bbp_edit_forum_pre_content', 'wp_filter_kses' );
}
/** Forum Parent ***********************************************************/
// Forum parent id was passed
if ( is_numeric( $_POST['bbp_forum_parent_id'] ) ) {
$forum_parent_id = (int) $_POST['bbp_forum_parent_id'];
}
// Current forum this forum is in
$current_parent_forum_id = bbp_get_forum_parent_id( $forum_id );
// Forum exists
if ( !empty( $forum_parent_id ) && ( $forum_parent_id !== $current_parent_forum_id ) ) {
// Forum is closed and user cannot access
if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) {
bbp_add_error( 'bbp_edit_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) );
}
// Forum is private and user cannot access
if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) {
bbp_add_error( 'bbp_edit_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
}
// Forum is hidden and user cannot access
if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) {
bbp_add_error( 'bbp_edit_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
}
}
/** Forum Title ***********************************************************/
if ( !empty( $_POST['bbp_forum_title'] ) )
$forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) );
// Filter and sanitize
$forum_title = apply_filters( 'bbp_edit_forum_pre_title', $forum_title, $forum_id );
// No forum title
if ( empty( $forum_title ) )
bbp_add_error( 'bbp_edit_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) );
/** Forum Content *********************************************************/
if ( !empty( $_POST['bbp_forum_content'] ) )
$forum_content = $_POST['bbp_forum_content'];
// Filter and sanitize
$forum_content = apply_filters( 'bbp_edit_forum_pre_content', $forum_content, $forum_id );
// No forum content
if ( empty( $forum_content ) )
bbp_add_error( 'bbp_edit_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) );
/** Forum Blacklist *******************************************************/
if ( !bbp_check_for_blacklist( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) )
bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be edited at this time.', 'bbpress' ) );
/** Forum Moderation ******************************************************/
$post_status = bbp_get_public_status_id();
if ( !bbp_check_for_moderation( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) )
$post_status = bbp_get_pending_status_id();
/** Additional Actions (Before Save) **************************************/
do_action( 'bbp_edit_forum_pre_extras', $forum_id );
/** No Errors *************************************************************/
if ( !bbp_has_errors() ) {
/** Update the forum **************************************************/
// Add the content of the form to $forum_data as an array
$forum_data = array(
'ID' => $forum_id,
'post_title' => $forum_title,
'post_content' => $forum_content,
'post_status' => $post_status,
'post_parent' => $forum_parent_id
);
// Just in time manipulation of forum data before being edited
$forum_data = apply_filters( 'bbp_edit_forum_pre_insert', $forum_data );
// Insert forum
$forum_id = wp_update_post( $forum_data );
/** Revisions *********************************************************/
/**
* @todo omitted for 2.1
// Revision Reason
if ( !empty( $_POST['bbp_forum_edit_reason'] ) )
$forum_edit_reason = esc_attr( strip_tags( $_POST['bbp_forum_edit_reason'] ) );
// Update revision log
if ( !empty( $_POST['bbp_log_forum_edit'] ) && ( 1 == $_POST['bbp_log_forum_edit'] ) && ( $revision_id = wp_save_post_revision( $forum_id ) ) ) {
bbp_update_forum_revision_log( array(
'forum_id' => $forum_id,
'revision_id' => $revision_id,
'author_id' => bbp_get_current_user_id(),
'reason' => $forum_edit_reason
) );
}
*
*/
/** No Errors *********************************************************/
if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) {
// Update counts, etc...
$forum_args = array(
'forum_id' => $forum_id,
'post_parent' => $forum_parent_id,
'forum_author' => $forum->post_author,
'last_topic_id' => 0,
'last_reply_id' => 0,
'last_active_id' => 0,
'last_active_time' => 0,
'last_active_status' => bbp_get_public_status_id()
);
do_action( 'bbp_edit_forum', $forum_args );
// If the new forum parent id is not equal to the old forum parent
// id, run the bbp_move_forum action and pass the forum's parent id
// as the first arg and new forum parent id as the second.
// @todo implement
//if ( $forum_id != $forum->post_parent )
// bbp_move_forum_handler( $forum_parent_id, $forum->post_parent, $forum_id );
/** Additional Actions (After Save) *******************************/
do_action( 'bbp_edit_forum_post_extras', $forum_id );
/** Redirect ******************************************************/
// Redirect to
$redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
// View all?
$view_all = bbp_get_view_all();
// Get the forum URL
$forum_url = bbp_get_forum_permalink( $forum_id, $redirect_to );
// Add view all?
if ( !empty( $view_all ) )
$forum_url = bbp_add_view_all( $forum_url );
// Allow to be filtered
$forum_url = apply_filters( 'bbp_edit_forum_redirect_to', $forum_url, $view_all, $redirect_to );
/** Successful Edit ***********************************************/
// Redirect back to new forum
wp_safe_redirect( $forum_url );
// For good measure
exit();
/** Errors ************************************************************/
} else {
$append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : '';
bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error . 'Please try again.', 'bbpress' ) );
}
}
}
/**
* Handle the saving of core forum metadata (Status, Visibility, and Type)
*
* @since bbPress (r3678)
* @param int $forum_id
* @uses bbp_is_forum_closed() To check if forum is closed
* @uses bbp_close_forum() To close forum
* @uses bbp_open_forum() To open forum
* @uses bbp_is_forum_category() To check if forum is a category
* @uses bbp_categorize_forum() To turn forum into a category
* @uses bbp_normalize_forum() To turn category into forum
* @uses bbp_get_public_status_id() To get the public status ID
* @uses bbp_get_private_status_id() To get the private status ID
* @uses bbp_get_hidden_status_id() To get the hidden status ID
* @uses bbp_get_forum_visibility() To get the forums visibility
* @uses bbp_hide_forum() To hide a forum
* @uses bbp_privatize_forum() To make a forum private
* @uses bbp_publicize_forum() To make a forum public
* @return If forum ID is empty
*/
function bbp_save_forum_extras( $forum_id = 0 ) {
// Validate the forum ID
$forum_id = bbp_get_forum_id( $forum_id );
// Bail if forum ID is empty
if ( empty( $forum_id ) )
return;
/** Forum Status ******************************************************/
if ( !empty( $_POST['bbp_forum_status'] ) && in_array( $_POST['bbp_forum_status'], array( 'open', 'closed' ) ) ) {
if ( 'closed' == $_POST['bbp_forum_status'] && !bbp_is_forum_closed( $forum_id, false ) ) {
bbp_close_forum( $forum_id );
} elseif ( 'open' == $_POST['bbp_forum_status'] && bbp_is_forum_closed( $forum_id, false ) ) {
bbp_open_forum( $forum_id );
}
}
/** Forum Type ********************************************************/
if ( !empty( $_POST['bbp_forum_type'] ) && in_array( $_POST['bbp_forum_type'], array( 'forum', 'category' ) ) ) {
if ( 'category' == $_POST['bbp_forum_type'] && !bbp_is_forum_category( $forum_id ) ) {
bbp_categorize_forum( $forum_id );
} elseif ( 'forum' == $_POST['bbp_forum_type'] && bbp_is_forum_category( $forum_id ) ) {
bbp_normalize_forum( $forum_id );
}
}
/** Forum Visibility **************************************************/
if ( !empty( $_POST['bbp_forum_visibility'] ) && in_array( $_POST['bbp_forum_visibility'], array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id() ) ) ) {
// Get forums current visibility
$visibility = bbp_get_forum_visibility( $forum_id );
// What is the new forum visibility setting?
switch ( $_POST['bbp_forum_visibility'] ) {
// Hidden
case bbp_get_hidden_status_id() :
bbp_hide_forum( $forum_id, $visibility );
break;
// Private
case bbp_get_private_status_id() :
bbp_privatize_forum( $forum_id, $visibility );
break;
// Publish (default)
case bbp_get_public_status_id() :
default :
bbp_publicize_forum( $forum_id, $visibility );
break;
}
}
}
/** Walk **********************************************************************/
/**
* Walk the forum tree
*
* @param object $forums Forums
* @param int $depth Depth
* @param int $current Current forum
* @param array $r Parsed arguments, supported by the walker. If you want to
* use your own walker, pass the 'walker' arg with the walker.
* The walker defaults to {@link BBP_Walker_Forum}
* @return object Walked forum tree
*/
function bbp_walk_forum( $forums, $depth, $current, $r ) {
$walker = empty( $r['walker'] ) ? new BBP_Walker_Forum : $r['walker'];
$args = array( $forums, $depth, $r, $current );
return call_user_func_array( array( &$walker, 'walk' ), $args );
}
/** Forum Actions *************************************************************/
/**
* Closes a forum
*
* @since bbPress (r2746)
*
* @param int $forum_id forum id
* @uses wp_get_single_post() To get the forum
* @uses do_action() Calls 'bbp_close_forum' with the forum id
* @uses update_post_meta() To add the previous status to a meta
* @uses do_action() Calls 'bbp_opened_forum' with the forum id
* @return mixed False or {@link WP_Error} on failure, forum id on success
*/
function bbp_close_forum( $forum_id = 0 ) {
$forum_id = bbp_get_forum_id( $forum_id );
do_action( 'bbp_close_forum', $forum_id );
update_post_meta( $forum_id, '_bbp_status', 'closed' );
do_action( 'bbp_closed_forum', $forum_id );
return $forum_id;
}
/**
* Opens a forum
*
* @since bbPress (r2746)
*
* @param int $forum_id forum id
* @uses wp_get_single_post() To get the forum
* @uses do_action() Calls 'bbp_open_forum' with the forum id
* @uses get_post_meta() To get the previous status
* @uses update_post_meta() To delete the previous status meta
* @uses do_action() Calls 'bbp_opened_forum' with the forum id
* @return mixed False or {@link WP_Error} on failure, forum id on success
*/
function bbp_open_forum( $forum_id = 0 ) {
$forum_id = bbp_get_forum_id( $forum_id );
do_action( 'bbp_open_forum', $forum_id );
update_post_meta( $forum_id, '_bbp_status', 'open' );
do_action( 'bbp_opened_forum', $forum_id );
return $forum_id;
}
/**
* Make the forum a category
*
* @since bbPress (r2746)
*
* @param int $forum_id Optional. Forum id
* @uses update_post_meta() To update the forum category meta
* @return bool False on failure, true on success
*/
function bbp_categorize_forum( $forum_id = 0 ) {
$forum_id = bbp_get_forum_id( $forum_id );
do_action( 'bbp_categorize_forum', $forum_id );
update_post_meta( $forum_id, '_bbp_forum_type', 'category' );
do_action( 'bbp_categorized_forum', $forum_id );
return $forum_id;
}
/**
* Remove the category status from a forum
*
* @since bbPress (r2746)
*
* @param int $forum_id Optional. Forum id
* @uses delete_post_meta() To delete the forum category meta
* @return bool False on failure, true on success
*/
function bbp_normalize_forum( $forum_id = 0 ) {
$forum_id = bbp_get_forum_id( $forum_id );
do_action( 'bbp_normalize_forum', $forum_id );
update_post_meta( $forum_id, '_bbp_forum_type', 'forum' );
do_action( 'bbp_normalized_forum', $forum_id );
return $forum_id;
}
/**
* Mark the forum as public
*
* @since bbPress (r2746)
*
* @param int $forum_id Optional. Forum id
* @uses update_post_meta() To update the forum private meta
* @return bool False on failure, true on success
*/
function bbp_publicize_forum( $forum_id = 0, $current_visibility = '' ) {
$forum_id = bbp_get_forum_id( $forum_id );
do_action( 'bbp_publicize_forum', $forum_id );
// Get private forums
$private = bbp_get_private_forum_ids();
// Find this forum in the array
if ( in_array( $forum_id, $private ) ) {
$offset = array_search( $forum_id, $private );
// Splice around it
array_splice( $private, $offset, 1 );
// Update private forums minus this one
update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) );
}
// Get hidden forums
$hidden = bbp_get_hidden_forum_ids();
// Find this forum in the array
if ( in_array( $forum_id, $hidden ) ) {
$offset = array_search( $forum_id, $hidden );
// Splice around it
array_splice( $hidden, $offset, 1 );
// Update hidden forums minus this one
update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) );
}
// Only run queries if visibility is changing
if ( bbp_get_public_status_id() != $current_visibility ) {
// Update forum post_status
global $wpdb;
$wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_public_status_id() ), array( 'ID' => $forum_id ) );
wp_transition_post_status( bbp_get_public_status_id(), $current_visibility, get_post( $forum_id ) );
}
do_action( 'bbp_publicized_forum', $forum_id );
return $forum_id;
}
/**
* Mark the forum as private
*
* @since bbPress (r2746)
*
* @param int $forum_id Optional. Forum id
* @uses update_post_meta() To update the forum private meta
* @return bool False on failure, true on success
*/
function bbp_privatize_forum( $forum_id = 0, $current_visibility = '' ) {
$forum_id = bbp_get_forum_id( $forum_id );
do_action( 'bbp_privatize_forum', $forum_id );
// Only run queries if visibility is changing
if ( bbp_get_private_status_id() != $current_visibility ) {
// Get hidden forums
$hidden = bbp_get_hidden_forum_ids();
// Find this forum in the array
if ( in_array( $forum_id, $hidden ) ) {
$offset = array_search( $forum_id, $hidden );
// Splice around it
array_splice( $hidden, $offset, 1 );
// Update hidden forums minus this one
update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) );
}
// Add to '_bbp_private_forums' site option
$private = bbp_get_private_forum_ids();
$private[] = $forum_id;
update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) );
// Update forums visibility setting
global $wpdb;
$wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_private_status_id() ), array( 'ID' => $forum_id ) );
wp_transition_post_status( bbp_get_private_status_id(), $current_visibility, get_post( $forum_id ) );
}
do_action( 'bbp_privatized_forum', $forum_id );
return $forum_id;
}
/**
* Mark the forum as hidden
*
* @since bbPress (r2996)
*
* @param int $forum_id Optional. Forum id
* @uses update_post_meta() To update the forum private meta
* @return bool False on failure, true on success
*/
function bbp_hide_forum( $forum_id = 0, $current_visibility = '' ) {
$forum_id = bbp_get_forum_id( $forum_id );
do_action( 'bbp_hide_forum', $forum_id );
// Only run queries if visibility is changing
if ( bbp_get_hidden_status_id() != $current_visibility ) {
// Get private forums
$private = bbp_get_private_forum_ids();
// Find this forum in the array
if ( in_array( $forum_id, $private ) ) {
$offset = array_search( $forum_id, $private );
// Splice around it
array_splice( $private, $offset, 1 );
// Update private forums minus this one
update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) );
}
// Add to '_bbp_hidden_forums' site option
$hidden = bbp_get_hidden_forum_ids();
$hidden[] = $forum_id;
update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) );
// Update forums visibility setting
global $wpdb;
$wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_hidden_status_id() ), array( 'ID' => $forum_id ) );
wp_transition_post_status( bbp_get_hidden_status_id(), $current_visibility, get_post( $forum_id ) );
}
do_action( 'bbp_hid_forum', $forum_id );
return $forum_id;
}
/** Count Bumpers *************************************************************/
/**
* Bump the total topic count of a forum
*
* @since bbPress (r3825)
*
* @param int $forum_id Optional. Forum id.
* @param int $difference Optional. Default 1
* @param bool $update_ancestors Optional. Default true
* @uses bbp_get_forum_id() To get the forum id
* @uses update_post_meta() To update the forum's topic count meta
* @uses apply_filters() Calls 'bbp_bump_forum_topic_count' with the topic
* count, forum id, and difference
* @return int Forum topic count
*/
function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) {
// Get some counts
$forum_id = bbp_get_forum_id( $forum_id );
$topic_count = bbp_get_forum_topic_count( $forum_id, false );
$total_topic_count = bbp_get_forum_topic_count( $forum_id, true );
// Update this forum id
update_post_meta( $forum_id, '_bbp_topic_count', (int) $topic_count + (int) $difference );
update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topic_count + (int) $difference );
// Check for ancestors
if ( true === $update_ancestors ) {
// Get post ancestors
$forum = get_post( $forum_id );
$ancestors = get_post_ancestors( $forum );
// If has ancestors, loop through them...
if ( !empty( $ancestors ) ) {
foreach ( (array) $ancestors as $parent_forum_id ) {
// Get forum counts
$parent_topic_count = bbp_get_forum_topic_count( $parent_forum_id, false );
$parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true );
// Update counts
update_post_meta( $parent_forum_id, '_bbp_topic_count', (int) $parent_topic_count + (int) $difference );
update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) $parent_total_topic_count + (int) $difference );
}
}
}
return (int) apply_filters( 'bbp_bump_forum_topic_count', (int) $total_topic_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors );
}
/**
* Bump the total hidden topic count of a forum
*
* @since bbPress (r3825)
*
* @param int $forum_id Optional. Forum id.
* @param int $difference Optional. Default 1
* @uses bbp_get_forum_id() To get the forum id