forked from ddean4040/bbPress-Topics-for-Posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
1155 lines (893 loc) · 44.1 KB
/
index.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
/*
Plugin Name: bbPress Topics for Posts
Plugin URI: http://www.generalthreat.com/projects/bbpress-post-topics
Description: Give authors the option to replace the comments on a WordPress blog post with a topic from an integrated bbPress install
Author: David Dean
Version: 1.9-testing
Revision Date: 09/02/2013
Requires at least: WP 3.1, bbPress 2.0
Tested up to: WP 3.6 , bbPress 2.4
Author URI: http://www.generalthreat.com/
*/
include dirname( __FILE__ ) . '/compatibility.php';
include dirname( __FILE__ ) . '/template.php';
include dirname( __FILE__ ) . '/ajax.php';
/** load localization files if present */
if( file_exists( dirname( __FILE__ ) . '/' . dirname(plugin_basename(__FILE__)) . '-' . get_locale() . '.mo' ) ) {
load_plugin_textdomain( 'bbpress-post-topics', false, dirname(plugin_basename(__FILE__)) . '' );
}
class BBP_PostTopics {
var $topic_ID = false;
var $xmlrpc_post = false;
/**
* Create the bbPress Topics for Posts meta box for post types defined by
* 'bbppt_eligible_post_types' filter
*/
function add_meta_box() {
foreach( apply_filters( 'bbppt_eligible_post_types', array( 'post', 'page' )) as $post_type ) {
add_meta_box(
'bbpress-post-topic',
__('bbPress Topic for this Post', 'bbpress-post-topics'),
array( &$this, 'display_meta_box' ),
$post_type
);
}
}
function display_meta_box( $post ) {}
/**
* Add the bbPress topic option to the Discussion meta box
*/
function display_topic_option( $post ) {
/** Store the post being edited and restore it after looping over forums */
global $post;
$the_post = $post;
if( ! function_exists( 'bbp_has_forums' ) ) {
?><br /><p><?php _e( 'bbPress Topics for Posts has been enabled, but cannot detect your bbPress setup.', 'bbpress-post-topics' ); ?></p><?php
return;
}
$bbpress_topic_options = $this->get_topic_options_for_post( $post->ID );
$bbpress_topic_status = $bbpress_topic_options['enabled'] != false;
$bbpress_topic_display = $bbpress_topic_options['display'];
$bbpress_topic_slug = $bbpress_topic_options['topic_id'];
if($bbpress_topic_slug) {
$bbpress_topic = bbp_get_topic( $bbpress_topic_slug);
$bbpress_topic_slug = $bbpress_topic->post_name;
/** If a topic already exists, don't select default forum */
$bbpress_topic_options['forum_id'] = 0;
}
$forums = bbp_has_forums();
if(!$forums) {
?><br /><p><?php _e('bbPress Topics for Posts has been enabled, but you have not created any forums yet.','bbpress-post-topics'); ?></p><?php
return;
}
?>
<br />
<input type="hidden" name="bbpress_topic[form_displayed]" value="true" />
<label for="bbpress_topic_status" class="selectit"><input name="bbpress_topic[enabled]" type="checkbox" id="bbpress_topic_status" value="open" <?php checked($bbpress_topic_status); ?> /> <?php _e( 'Use a bbPress forum topic for comments on this post.', 'bbpress-post-topics' ); ?></label><br />
<div id="bbpress_topic_status_options" class="inside" style="display: <?php echo checked($bbpress_topic_status, true, false) ? 'block' : 'none' ?>;">
<h4>bbPress Topic Options</h4>
<label for="bbpress_topic_slug"><?php _e('Use an existing topic:', 'bbpress-post-topics' ) ?> </label> <input type="text" name="bbpress_topic[slug]" id="bbpress_topic_slug" placeholder="<?php _e( 'Topic ID or slug', 'bbpress-post-topics' ); ?>" value="<?php echo $bbpress_topic_slug ?>" <?php if( $bbpress_topic_options['forum_id'] ) echo ' disabled="true"'; ?> />
- OR - <label for="bbpress_topic_forum"><?php _e('Create a new topic in forum:', 'bbpress-post-topics' ); ?></label>
<select name="bbpress_topic[forum_id]" id="bbpress_topic_forum">
<option value="0" selected><?php _e('Select a Forum', 'bbpress-post-topics' ); ?></option>
<?php
$forum_dropdown_options = array(
'selected' => $bbpress_topic_options['forum_id'],
'options_only' => true
);
bbp_dropdown( $forum_dropdown_options );
?>
</select><br />
— <input type="checkbox" name="bbpress_topic[copy_tags]" id="bbpress_topic_copy_tags" <?php checked( $bbpress_topic_options['copy_tags'], 'on' ) ?> /> <label for="bbpress_topic_copy_tags"><?php _e( 'Copy post tags to new topic', 'bbpress-post-topics' ) ?></label>
<?php if( $import_date = get_post_meta( $post->ID, 'bbpress_discussion_tags_copied', true ) ) :
printf( '( ' . __( 'last copied %s ago', 'bbpress-post-topics' ) . ' )', human_time_diff( $import_date ) );
endif; ?>
<br />
<?php if( wp_count_comments( $post->ID )->total_comments > 0 ) : ?>
— <input type="checkbox" name="bbpress_topic[copy_comments]" id="bbpress_topic_copy_comments" <?php checked( $bbpress_topic_options['copy_comments'], 'on' ) ?> /> <label for="bbpress_topic_copy_comments"><?php _e( 'Copy comments to bbPress topic', 'bbpress-post-topics' ) ?></label>
<?php if( $import_date = get_post_meta( $post->ID, 'bbpress_discussion_comments_copied', true ) ) :
printf( '( ' . __( 'last copied %s ago', 'bbpress-post-topics' ) . ' )', human_time_diff( $import_date ) );
endif; ?>
<br />
<?php endif; ?>
— <input type="checkbox" name="bbpress_topic[use_defaults]" id="bbpress_topic_use_defaults" <?php checked( $bbpress_topic_options['use_defaults'] ) ?> /> <label for="bbpress_topic_use_defaults"><?php _e( 'Use default display settings', 'bbpress-post-topics' ) ?></label>
<div id="bbpress_topic_display_options" style="display: <?php echo checked( $bbpress_topic_options['use_defaults'], true, false ) ? 'none' : 'block' ?>; border-left: 1px solid #ccc; margin-left: 9px; padding-left: 5px;">
<label for=""><?php _e( 'On the post page, show:', 'bbpress-post-topics' ); ?></label><br />
<?php
$xreplies_sort_options = array(
'newest' => __( 'most recent', 'bbpress-post-topics' ),
'oldest' => __( 'oldest', 'bbpress-post-topics' )
);
$xreplies_count = isset($bbpress_topic_options['display-extras']['xcount']) ? $bbpress_topic_options['display-extras']['xcount'] : 5;
$xreplies_count_string = '<input type="text" name="bbpress_topic[display-extras][xcount]" value="' . $xreplies_count . '" class="small-text" maxlength="3" />';
$xsort_select_string = '<select name="bbpress_topic[display-extras][xsort]" id="bbpress_topic_display_sort">';
foreach($xreplies_sort_options as $option => $label) {
$xsort_select_string .= '<option value="' . $option . '" ' . selected( $bbpress_topic_options['display-extras']['xsort'], $option, false ) . '>' . $label . '</option>';
}
$xsort_select_string .= '</select>';
/** Build list of display formats, including custom ones */
$display_formats = array(
'topic' => __( 'Entire topic', 'bbpress-post-topics' ),
'replies' => __( 'Replies only', 'bbpress-post-topics' ),
'xreplies' => sprintf(__( 'Only the %s %s %s replies', 'bbpress-post-topics' ),'</label>', $xreplies_count_string, $xsort_select_string ),
'link' => __( 'A link to the topic', 'bbpress-post-topics' )
);
$display_formats = apply_filters( 'bbppt_display_format_options', $display_formats, $the_post->ID );
?>
<fieldset>
<?php foreach ($display_formats as $format_code => $format_label) : ?>
<input type="radio" name="bbpress_topic[display]" id="bbpress_topic_display_<?php echo $format_code ?>" value="<?php echo $format_code ?>" <?php checked($bbpress_topic_options['display'], $format_code ) ?> /><label for="bbpress_topic_display_<?php echo $format_code ?>"><?php echo $format_label ?></label><br />
<?php endforeach; ?>
</fieldset>
</div>
</div>
<script type="text/javascript">
/** hide topic options when not checked */
jQuery('#bbpress_topic_status').change(function() {
if(jQuery(this).attr('checked')) {
jQuery('#bbpress_topic_status_options').show();
} else {
jQuery('#bbpress_topic_status_options').hide();
}
});
/** hide display options when defaults are selected */
jQuery('#bbpress_topic_use_defaults').change(function() {
if(jQuery(this).attr('checked')) {
jQuery('#bbpress_topic_display_options').hide();
} else {
jQuery('#bbpress_topic_display_options').show();
}
});
/** disable topic slug field when a forum is selected to prevent confusion */
jQuery('#bbpress_topic_forum').change(function() {
if(jQuery(this).val() != 0) {
jQuery('#bbpress_topic_slug').attr('disabled','true');
} else {
jQuery('#bbpress_topic_slug').removeAttr('disabled');
}
});
</script>
<?php
do_action( 'bbppt_display_options_fields', $the_post->ID );
/** Restore the original post being edited */
$post = $the_post;
}
/**
* Note XMLRPC invocation so we can apply defaults to any posts created during this request
*/
function catch_xmlrpc_post( $callname ) {
$this->xmlrpc_post = true;
}
/**
* Process the user's bbPress topic selections when the post is saved
*/
function process_topic_option( $post_ID, $post ) {
/** Don't process on AJAX-based auto-drafts */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
/** Don't process on initial page load auto drafts */
if( $post->post_status == 'auto-draft' )
return;
/** Only process for post types we specify */
if( !in_array( $post->post_type, apply_filters( 'bbppt_eligible_post_types', array( 'post', 'page' ) ) ) ) {
return;
}
// TODO: combine existing topic and draft settings branches
if( isset( $_POST['bbpress_topic'] ) ) {
/**
* Post was created through the full post editor form
* Check the POST for settings
*/
if( ! empty( $_POST['bbpress_topic']['enabled'] ) ) {
$bbppt_options = $_POST['bbpress_topic'];
$create_topic = true;
$use_defaults = isset( $bbppt_options['use_defaults'] );
bbppt_debug( 'Processing a topic for regular post ' . $post_ID . ' with the following settings: ' . print_r( $bbppt_options, true ) );
bbppt_debug( 'Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults );
} else {
$create_topic = false;
$bbppt_options = array();
bbppt_debug( 'NOT creating a topic for regular post ' . $post_ID );
}
} else if( get_post_meta( $post_ID, 'bbpress_discussion_topic_id', true ) ) {
/**
* If post has an existing topic, use its settings
*/
$bbppt_options = $this->get_topic_options_for_post( $post_ID );
$create_topic = ( ! empty( $bbppt_options['enabled'] ) );
$use_defaults = ( ! empty( $bbppt_options['use_defaults'] ) );
bbppt_debug( 'Processing topic for existing post ' . $post_ID . ' with the following settings: ' . print_r( $bbppt_options, true ) );
bbppt_debug( 'Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults );
} else if( $this->has_draft_settings( $post ) ) {
/**
* If the post has draft settings saved, use draft settings
*/
$bbppt_options = $this->get_draft_settings( $post );
$create_topic = ( ! empty( $bbppt_options['enabled'] ) );
$use_defaults = ( ! empty( $bbppt_options['use_defaults'] ) );
bbppt_debug( 'Processing a topic for draft post ' . $post_ID . ' with the following settings: ' . print_r( $bbppt_options, true ) );
bbppt_debug( 'Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults );
} else {
/**
* Post was created through some other means (including XML-RPC) - use defaults
*/
$bbppt_options = get_option( 'bbpress_discussion_defaults' );
$create_topic = ( ! empty( $bbppt_options['enabled'] ) );
$use_defaults = true;
$bbppt_options['use_defaults'] = $use_defaults;
$bbppt_options['forum_id'] = apply_filters( 'bbpt_override_default_topic_forum', $bbppt_options['forum_id'], get_post_type( $post_ID ) );
bbppt_debug( 'Processing a topic for unattended post ' . $post_ID . ' with the following settings: ' . print_r( $bbppt_options, true ) );
bbppt_debug( 'Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults );
}
/** If post is saved as a draft, store selected settings for publication later */
if( in_array( $post->post_status, apply_filters( 'bbppt_draft_post_status', array( 'draft', 'future' ) ) ) ) {
$this->set_draft_settings( $bbppt_options, $post );
return;
}
/** Only process further when the post is published */
if( ! in_array( $post->post_status, apply_filters( 'bbppt_eligible_post_status', array( 'publish', 'private' ) ) ) )
return;
/**
* The user requested to use a bbPress topic for discussion
*/
if( $create_topic ) {
if( ! function_exists('bbp_has_forums') ) {
?><br /><p><?php _e('bbPress Topics for Posts cannot process this request because it cannot detect your bbPress setup.','bbpress-post-topics'); ?></p><?php
return;
}
$topic_slug = isset( $bbppt_options['slug'] ) ? $bbppt_options['slug'] : '' ;
$topic_forum = isset( $bbppt_options['forum_id'] ) ? (int)$bbppt_options['forum_id'] : 0 ;
if( ! $use_defaults ) {
$topic_display = isset($bbppt_options['display']) ? $bbppt_options['display'] : 'topic' ;
/** Store extra data for xreplies, as well as any custom display formats */
$topic_display_extras = apply_filters( 'bbppt_store_display_extras', $bbppt_options['display-extras'], $post );
}
if( ! empty( $topic_slug ) ) {
/** if user has selected an existing topic */
if( is_numeric( $topic_slug ) ) {
$topic = bbp_get_topic( (int)$topic_slug );
} else {
$topic = bbppt_get_topic_by_slug( $topic_slug );
}
if( $topic == null ) {
// return an error of some kind
wp_die( __( 'There was an error with your selected topic.', 'bbpress-post-topics' ), __( 'Error Locating bbPress Topic', 'bbpress-post-topics' ) );
} else {
$topic_ID = $topic->ID;
update_post_meta( $post_ID, 'use_bbpress_discussion_topic', true );
update_post_meta( $post_ID, 'bbpress_discussion_topic_id', $topic_ID );
/** Update topic with tags from the post */
if( ! empty( $bbppt_options['copy_tags'] ) ) {
$post_tags = wp_get_post_tags( $post_ID );
$post_tags = array_map( create_function( '$term', 'return $term->name;' ), $post_tags );
wp_set_post_terms( $topic_ID, join( ',', $post_tags ), bbp_get_topic_tag_tax_id(), true );
update_post_meta( $post_ID, 'bbpress_discussion_tags_copied', time() );
}
/** Export comments from the post to the new bbPress topic */
if( ! empty( $bbppt_options['copy_comments'] ) ) {
bbppt_import_comments( $post_ID, $topic_ID );
update_post_meta( $post_ID, 'bbpress_discussion_comments_copied', time() );
}
if( $use_defaults ) {
update_post_meta( $post_ID, 'bbpress_discussion_use_defaults', true );
} else {
delete_post_meta( $post_ID, 'bbpress_discussion_use_defaults' );
update_post_meta( $post_ID, 'bbpress_discussion_display_format', $topic_display );
update_post_meta( $post_ID, 'bbpress_discussion_display_extras', $topic_display_extras );
}
}
do_action( 'bbppt_topic_assigned', $post_ID, $topic_ID );
} else if($topic_forum != 0) {
/** if user has opted to create a new topic */
$topic_ID = $this->build_new_topic( $post, $topic_forum );
if( ! $topic_ID ) {
// return an error of some kind
wp_die(__('There was an error creating a new topic.','bbpress-post-topics'),__('Error Creating bbPress Topic','bbpress-post-topics'));
} else {
update_post_meta( $post_ID, 'use_bbpress_discussion_topic', true );
update_post_meta( $post_ID, 'bbpress_discussion_topic_id', $topic_ID );
/** Update topic with tags from the post */
if( $bbppt_options['copy_tags'] ) {
$post_tags = wp_get_post_tags( $post_ID );
$post_tags = array_map( create_function( '$term', 'return $term->name;' ), $post_tags );
wp_set_post_terms( $topic_ID, join( ',', $post_tags ), bbp_get_topic_tag_tax_id(), false );
update_post_meta( $post_ID, 'bbpress_discussion_tags_copied', time() );
}
/** Export comments from the post to the new bbPress topic */
if( $bbppt_options['copy_comments'] ) {
bbppt_import_comments( $post_ID, $topic_ID );
update_post_meta( $post_ID, 'bbpress_discussion_comments_copied', time() );
}
if( $use_defaults ) {
update_post_meta( $post_ID, 'bbpress_discussion_use_defaults', true );
} else {
update_post_meta( $post_ID, 'bbpress_discussion_display_format', $topic_display );
update_post_meta( $post_ID, 'bbpress_discussion_display_extras', $topic_display_extras );
}
}
do_action( 'bbppt_topic_created', $post_ID, $topic_ID );
}
} else {
delete_post_meta( $post_ID, 'use_bbpress_discussion_topic' );
delete_post_meta( $post_ID, 'bbpress_discussion_topic_id' );
delete_post_meta( $post_ID, 'bbpress_discussion_use_defaults' );
delete_post_meta( $post_ID, 'bbpress_discussion_display_format' );
delete_post_meta( $post_ID, 'bbpress_discussion_display_extras' );
delete_post_meta( $post_ID, 'bbpress_discussion_tags_copied' );
delete_post_meta( $post_ID, 'bbpress_discussion_comments_copied' );
}
$this->delete_draft_settings( $post );
do_action( 'bbppt_topic_associated', $post_ID, $topic_ID );
}
/**
* Create the new topic when selected, including shortcode substitution
* @param WP_Post $post post object to associate with new topic
* @param int $topic_forum ID of forum to hold new topic
*/
function build_new_topic( $post, $topic_forum ) {
$strings = get_option( 'bbpress_discussion_text' );
$author_info = get_userdata( $post->post_author );
if( isset( $strings['topic-text'] ) ) {
$topic_content = $strings['topic-text'];
} else {
$topic_content = "%excerpt<br />" . sprintf( __('[See the full post at: <a href="%s">%s</a>]','bbpress-post-topics'), '%url', '%url' );
}
$shortcodes = array(
'%title' => $post->post_title,
'%url' => get_permalink( $post->ID ),
'%author' => $author_info->user_nicename,
'%excerpt' => ( empty( $post->post_excerpt ) ? bbppt_post_discussion_get_the_content($post->post_content, 150) : apply_filters('the_excerpt', $post->post_excerpt) ),
'%post' => $post->post_content
);
$shortcodes = apply_filters( 'bbppt_shortcodes_output', $shortcodes, $post, $topic_forum );
$topic_content = str_replace( array_keys($shortcodes), array_values($shortcodes), $topic_content );
$topic_content = apply_filters( 'bbppt_topic_content', addslashes( $topic_content ), $post->ID );
$new_topic_data = array(
'post_parent' => (int)$topic_forum,
'post_author' => $post->post_author,
'post_content' => $topic_content,
'post_title' => $post->post_title,
'post_date' => $post->post_date,
'post_date_gmt' => $post->post_date_gmt
);
$new_topic_meta = array(
'forum_id' => (int)$topic_forum,
'last_active_time' => $post->post_date
);
$new_topic = bbp_insert_topic( $new_topic_data, $new_topic_meta );
return $new_topic;
}
/**
* Display the bbPress topic plugin template instead of the WordPress comments template
*/
function maybe_change_comments_template( $template ) {
global $post, $bbp;
if( ! function_exists( 'bbp_has_forums' ) ) return $template;
if( get_post_meta( $post->ID, 'use_bbpress_discussion_topic', true ) ) {
$topic_ID = get_post_meta( $post->ID, 'bbpress_discussion_topic_id', true);
$this->topic_ID = $topic_ID;
/** Handle posts where defaults were kept */
$settings = $this->get_topic_options_for_post( $post->ID );
switch( $settings['display'] ) {
case 'topic':
return bbppt_locate_template( 'comments-bbpress.php' );
break;
case 'xreplies':
add_filter( 'bbp_has_replies_query', 'bbppt_limit_replies_in_thread' );
case 'replies':
add_filter( 'bbp_has_replies_query', 'bbppt_remove_topic_from_thread' );
return bbppt_locate_template( 'comments-bbpress.php' );
break;
case 'link':
return bbppt_locate_template( 'comments-bbpress-link.php' );
break;
default:
return apply_filters( 'bbppt_template_display_format_' . $settings['display'], $template, $settings );
break;
}
}
return $template;
}
/**
* If a topic has been used for a post, give the number of replies in place of comment count
*/
function maybe_change_comments_number( $number, $post_ID ) {
if( ! function_exists( 'bbp_has_forums' ) ) return $number;
if( get_post_meta( $post_ID, 'use_bbpress_discussion_topic', true ) ) {
$topic_ID = get_post_meta( $post_ID, 'bbpress_discussion_topic_id', true );
return bbp_get_topic_reply_count( $topic_ID );
}
return $number;
}
/**
* Delete the bbPress topic associated with a post being deleted unless:
* - that topic is also associated with another post
* - cancelled by 'bbppt_do_delete_topic' filter
* @param int post_ID ID of the post being deleted
*/
function maybe_delete_topic( $post_ID ) {
if( get_post_meta( $post_ID, 'use_bbpress_discussion_topic', true ) ) {
$topic_ID = get_post_meta( $post_ID, 'bbpress_discussion_topic_id', true );
$affected_posts = get_posts(
array(
'post_type' => apply_filters( 'bbppt_eligible_post_types', array( 'post', 'page' ) ),
'meta_key' => 'bbpress_discussion_topic_id',
'meta_value' => $topic_ID
)
);
if( count( $affected_posts ) == 1 && apply_filters( 'bbppt_do_delete_topic', true, $topic_ID ) ) {
// Delete topic
bbp_delete_topic( $topic_ID );
}
}
}
/****************************
* General Discussion options
*/
/**
* Register our sections for the Discussion settings page
*/
function add_discussion_page_settings() {
register_setting( 'discussion', 'bbpress_discussion_defaults' );
register_setting( 'discussion', 'bbpress_discussion_text', array( &$this, 'sanitize_text_settings' ) );
add_settings_field( 'bbpress_discussion_defaults', __('bbPress Topics for Posts Defaults','bbpress-post-topics'), array(&$this,'general_discussion_settings'), 'discussion', 'default', array('label_for'=>'bbpress_discussion_defaults_enabled') );
add_settings_field( 'bbpress_discussion_text', __('bbPress Topics for Posts Strings','bbpress-post-topics'), array(&$this,'general_discussion_text_settings'), 'discussion', 'default' );
wp_register_script( 'bbppt-admin-script', plugins_url( 'inc/bbppt-admin.js', __FILE__ ), array('jquery') );
wp_localize_script( 'bbppt-admin-script', 'bbPPTStrings', array(
'disabledTitle' => __('Disabled - save changes or reload to enable','bbpress-post-topics'),
'imgSrc' => ADMIN_COOKIE_PATH . '/images/wpspin_light.gif'
));
}
/**
* Section for setting defaults for bbPress Topics for Posts
*/
function general_discussion_settings() {
?><div id="bbppt-discussion-settings"><?php
if( ! function_exists( 'bbp_has_forums' ) ) {
?>
<p><?php _e( 'You must install or enable bbPress to use this plugin.', 'bbpress-post-topics' ); ?></p>
<?php
return;
}
wp_enqueue_script('bbppt-admin-script');
$ex_options = array(
'enabled' => false,
'forum_id' => false,
'copy_tags' => false,
'copy_comments' => false,
'display' => false,
'display-extras' => false
);
$ex_options = wp_parse_args( get_option( 'bbpress_discussion_defaults' ), $ex_options );
$forum_dropdown_options = array(
'selected' => $ex_options['forum_id'],
'options_only' => true
);
$forum_select_string = '<select name="bbpress_discussion_defaults[forum_id]" id="bbpress_discussion_defaults_forum_id">';
$forum_select_string .= '<option value="0">' . __('Select a Forum','bbpress-post-topics') . '</option>';
$forum_select_string .= bbp_get_dropdown( $forum_dropdown_options );
$forum_select_string .= '</select>';
?>
<input type="checkbox" name="bbpress_discussion_defaults[enabled]" id="bbpress_discussion_defaults_enabled" <?php checked($ex_options['enabled'],'on') ?>>
<label for="bbpress_discussion_defaults_enabled"><?php printf(__('Create a new bbPress topic in %s %s for new posts','bbpress-post-topics'), '</label>', $forum_select_string); ?>
<?php if( isset($ex_options['enabled'] ) && $ex_options['enabled'] == 'on') : ?>
— <a class="button" id="create_topics" href="#" title="<?php _e('Create topics and apply these settings to all existing posts','bbpress-post-topics') ?>"><?php _e('Apply settings to existing posts', 'bbpress-post-topics'); ?></a>
<?php endif; ?>
<br />
<input type="checkbox" name="bbpress_discussion_defaults[copy_tags]" id="bbpress_discussion_defaults_copy_tags" <?php checked($ex_options['copy_tags'],'on') ?>>
<label for="bbpress_discussion_defaults_copy_tags"><?php _e('Copy post tags to new topics','bbpress-post-topics'); ?></label><br />
<input type="checkbox" name="bbpress_discussion_defaults[copy_comments]" id="bbpress_discussion_defaults_copy_comments" <?php checked($ex_options['copy_comments'],'on') ?>>
<label for="bbpress_discussion_defaults_copy_comments"><?php _e('Copy post comments to new topics (when available)','bbpress-post-topics'); ?></label><br />
<label for=""><?php _e( 'On the post page, show:', 'bbpress-post-topics' ); ?></label><br />
<?php
$xreplies_count = isset($ex_options['display-extras']['xcount']) ? $ex_options['display-extras']['xcount'] : 5;
$xreplies_count_string = '<input type="text" name="bbpress_discussion_defaults[display-extras][xcount]" id="bbpress_discussion_defaults_display-extras_xcount" value="' . $xreplies_count . '" class="small-text" maxlength="3" />';
$xreplies_sort_options = array(
'newest' => __( 'most recent', 'bbpress-post-topics' ),
'oldest' => __( 'oldest', 'bbpress-post-topics' )
);
$xsort_select_string = '<select name="bbpress_discussion_defaults[display-extras][xsort]" id="bbpress_discussion_defaults_display_sort">';
foreach($xreplies_sort_options as $option => $label) {
$xsort_select_string .= '<option value="' . $option . '" ' . selected( $ex_options['display-extras']['xsort'], $option, false ) . '>' . $label . '</option>';
}
$xsort_select_string .= '</select>';
/** Build list of display formats, including custom ones */
$display_formats = array(
'topic' => __( 'Entire topic', 'bbpress-post-topics' ),
'replies' => __( 'Replies only', 'bbpress-post-topics' ),
'xreplies' => sprintf(__( 'Only the %s %s %s replies', 'bbpress-post-topics' ),'</label>', $xreplies_count_string, $xsort_select_string ),
'link' => __( 'A link to the topic', 'bbpress-post-topics' ) . '</label>'
);
$display_formats = apply_filters( 'bbppt_display_format_options', $display_formats, 0 );
?>
<fieldset>
<?php foreach ($display_formats as $format_code => $format_label) : ?>
<input type="radio" name="bbpress_discussion_defaults[display]" id="bbpress_discussion_default_display_<?php echo $format_code ?>" value="<?php echo $format_code ?>" <?php checked($ex_options['display'], $format_code ) ?> /><label for="bbpress_discussion_default_display_<?php echo $format_code ?>"><?php echo $format_label ?></label><br />
<?php endforeach; ?>
</fieldset>
<?php
do_action( 'bbppt_display_options_fields', 0 );
?></div><?php
}
/**
* Section for setting strings for new topics and link
*/
function general_discussion_text_settings() {
?><div id="bbppt-discussion-text-settings"><?php
if( ! function_exists( 'bbp_has_forums' ) ) {
?>
<p><?php _e( 'You must install or enable bbPress to use this plugin.', 'bbpress-post-topics' ); ?></p>
<?php
return;
}
$text_options = get_option( 'bbpress_discussion_text' );
if(isset($text_options['topic-text'])) {
$topic_text_value = $text_options['topic-text'];
} else {
$topic_text_value = '%excerpt' . '<br />' . sprintf(__( '[See the full post at: <a href="%s">%s</a>]', 'bbpress-post-topics' ), '%url', '%title' );
}
$link_text_value = ( isset( $text_options['link-text'] ) ? $text_options['link-text'] : __( 'Follow this link to join the discussion', 'bbpress-post-topics' ) );
$shortcodes = array(
'%title' => __( 'Post title', 'bbpress-post-topics' ),
'%url' => __( 'Post Permalink', 'bbpress-post-topics' ),
'%author' => __( 'Post author\'s display name', 'bbpress-post-topics' ),
'%excerpt' => __( 'Post except (or a 150-character snippet)', 'bbpress-post-topics' ),
'%post' => __( 'Full post text', 'bbpress-post-topics' )
);
$shortcodes = apply_filters( 'bbppt_shortcodes_list', $shortcodes );
?>
<label for="bbpress_discussion_text_topic_text"><?php _e( 'Content of topic first post:', 'bbpress-post-topics' ) ?></label>
<p>
<textarea name="bbpress_discussion_text[topic-text]" id="bbpress_discussion_text_topic_text" class="large-text code"><?php echo $topic_text_value ?></textarea>
<small>
(<?php _e( 'Use the substitutions below:', 'bbpress-post-topics' ) ?>)<br />
<?php foreach( $shortcodes as $code => $description ) {
echo $code . ' — ' . $description . '<br />';
} ?>
</small>
</p>
<label for=""><?php _e( 'Link text (when showing only a link to the topic):', 'bbpress-post-topics' ) ?></label>
<input type="text" name="bbpress_discussion_text[link-text]" class="regular-text" id="bbpress_discussion_text_link_text" value="<?php echo $link_text_value ?>" />
<small>(<?php _e( 'Use %s to include the post name', 'bbpress-post-topics' ) ?>)</small>
<?php
do_action( 'bbppt_display_text_options_fields', 0 );
?></div><?php
}
/**
* Sanitize the general discussion strings
*/
function sanitize_text_settings( $strings ) {
if( isset( $strings['topic-text'] ) ) {
$strings['topic-text'] = wp_kses_post( $strings['topic-text'] );
}
return $strings;
}
/**
* Retrieve topic options for posts, including cases where defaults are used
* @param int $ID ID of post
* @param string $option_name Optional name of an option to filter by
*/
function get_topic_options_for_post( $ID, $option_name = null ) {
/** Conditions for applying default settings:
* 1 - post where keep defaults was checked
* 2 - new post
*/
$defaults = get_option( 'bbpress_discussion_defaults' );
if( ! array_key_exists( 'display-extras', $defaults ) ) {
$defaults['display-extras'] = array(
'xcount' => 5,
'xsort' => 'newest'
);
}
$strings = get_option( 'bbpress_discussion_text' );
$draft_settings = $this->get_draft_settings( $ID );
if( $draft_settings && $draft_settings['enabled'] ) {
/** Post has draft settings saved */
$options = $draft_settings;
/** Check whether draft settings specify default display */
if( $draft_settings['use_defaults'] ) {
$options = array_merge( $defaults, $options );
}
if( $topic_id = get_post_meta( $ID, 'bbpress_discussion_topic_id', true ) )
$options['topic_id'] = $topic_id;
} else if(
get_post_meta( $ID, 'bbpress_discussion_use_defaults', true ) ||
! get_post_meta( $ID, 'bbpress_discussion_topic_id', true )
) {
/** Post is using defaults, or is new - return default values */
$display_extras = maybe_unserialize( $defaults['display-extras'] );
$options = array(
'enabled' => get_post_meta( $ID, 'use_bbpress_discussion_topic', true ) || ! empty( $defaults['enabled'] ),
'use_defaults' => true,
'topic_id' => get_post_meta( $ID, 'bbpress_discussion_topic_id', true ),
'slug' => get_post_meta( $ID, 'bbpress_discussion_topic_id', true ),
'forum_id' => empty( $defaults['forum_id'] ) ? false: $defaults['forum_id'],
'copy_tags' => empty( $defaults['copy_tags'] ) ? false : $defaults['copy_tags'],
'copy_comments' => empty( $defaults['copy_comments'] ) ? false : $defaults['copy_comments'],
'display' => empty( $defaults['display'] ) ? false : $defaults['display'],
'display-extras' => $display_extras,
'text' => $strings
);
$options['forum_id'] = apply_filters( 'bbpt_override_default_topic_forum', $options['forum_id'], get_post_type( $ID ) );
} else {
/** Post is using custom settings - return those values */
/** Remove legacy post meta when post is accessed */
if( get_post_meta( $ID, 'bbpress_discussion_hide_topic', true ) ) {
update_post_meta( $ID, 'bbpress_discussion_display_format', 'replies' );
delete_post_meta( $ID, 'bbpress_discussion_hide_topic' );
$display = 'replies';
} else if( ! get_post_meta( $ID, 'bbpress_discussion_display_format', true ) ) {
update_post_meta( $ID, 'bbpress_discussion_display_format', 'topic' );
$display = 'topic';
} else {
$display = get_post_meta( $ID, 'bbpress_discussion_display_format', true );
}
$display_extras = maybe_unserialize( get_post_meta( $ID, 'bbpress_discussion_display_extras', true ) );
/** Fill in any missing fields with defaults */
if( ! empty( $display_extras ) ) {
foreach( $defaults['display-extras'] as $display_extra => $extra_value ) {
if( ! array_key_exists( $display_extra, $display_extras ) ) {
$display_extras[$display_extra] = $extra_value;
}
}
} else {
$display_extras = $defaults['display-extras'];
}
$options = array(
'enabled' => get_post_meta( $ID, 'use_bbpress_discussion_topic', true ),
'use_defaults' => false,
'topic_id' => get_post_meta( $ID, 'bbpress_discussion_topic_id', true ),
'slug' => get_post_meta( $ID, 'bbpress_discussion_topic_id', true ),
'copy_tags' => empty( $defaults['copy_tags'] ) ? false : $defaults['copy_tags'],
'copy_comments' => empty( $defaults['copy_comments'] ) ? false : $defaults['copy_comments'],
'display' => $display,
'display-extras' => $display_extras,
'text' => $strings
);
}
return $options;
}
/** Functions for managing options for posts that have not been published */
function has_draft_settings( $post = null ) {
$post = get_post( $post );
if( ! is_object( $post ) ) return false;
return get_post_meta( $post->ID, 'bbppt_draft_settings', true );
}
function get_draft_settings( $post = null ) {
return $this->has_draft_settings( $post );
}
function set_draft_settings( $settings, $post = null ) {
$post = get_post( $post );
if( ! is_object( $post ) ) return false;
if( ! array_key_exists( 'enabled', $settings ) )
$settings['enabled'] = false;
if( ! array_key_exists( 'use_defaults', $settings ) )
$settings['use_defaults'] = false;
else
$settings['use_defaults'] = true;
if( ! array_key_exists( 'topic_id', $settings ) )
$settings['topic_id'] = false;
if( ! array_key_exists( 'copy_tags', $settings ) )
$settings['copy_tags'] = false;
if( ! array_key_exists( 'copy_comments', $settings ) )
$settings['copy_comments'] = false;
update_post_meta( $post->ID, 'bbppt_draft_settings', $settings );
}
function delete_draft_settings( $post = null ) {
$post = get_post( $post );
if( ! is_object( $post ) ) return false;
return delete_post_meta( $post->ID, 'bbppt_draft_settings' );
}
}
$bbp_post_topics = new BBP_PostTopics;
//add_action( 'add_meta_boxes', array( &$bbp_post_topics, 'add_meta_box' ) );
add_action( 'post_comment_status_meta_box-options', array( &$bbp_post_topics, 'display_topic_option' ) );
add_action( 'save_post', array( &$bbp_post_topics, 'process_topic_option' ), 10, 2 );
add_action( 'admin_init', array( &$bbp_post_topics, 'add_discussion_page_settings' ) );
add_action( 'xmlrpc_call', array( &$bbp_post_topics, 'catch_xmlrpc_post' ) );
add_action( 'before_delete_post', array( &$bbp_post_topics, 'maybe_delete_topic' ) );
add_filter( 'comments_template', array( &$bbp_post_topics, 'maybe_change_comments_template' ) );
add_filter( 'get_comments_number', array( &$bbp_post_topics, 'maybe_change_comments_number' ), 10, 2 );
register_activation_hook( __FILE__, 'bbppt_activate' );
function bbppt_activate() {
/** Update global settings to new format */
$ex_options = get_option( 'bbpress_discussion_defaults' );
$text_options = get_option( 'bbpress_discussion_text' );
if($ex_options['hide_topic'] == 'on') {
$ex_options['display'] = 'replies';
} else {
$ex_options['display'] = 'topic';
}
unset($ex_options['hide_topic']);
/** Update link text storage to new format - old format was never released, but was available in dev version */
if( isset( $ex_options['display-extras'] ) && isset( $ex_options['display-extras']['link-text'] ) ) {
$text_options['link-text'] = $ex_options['display-extras']['link-text'];
unset($ex_options['display-extras']['link-text']);
}
update_option( 'bbpress_discussion_defaults', $ex_options );
/** Set default link text */
if( ! isset( $text_options['link-text'] ) ) {
$text_options = array(
'link-text' => __( 'Follow this link to join the discussion', 'bbpress-post-topics' )
);
}
update_option( 'bbpress_discussion_text', $text_options );
}
/****************************
* Utility functions
*/
/**
* Check for a bbPress topic with a post_name matching the slug provided
* @param string Post slug
* @return object|NULL the topic or NULL if not found
*/
function bbppt_get_topic_by_slug( $slug ) {
global $wpdb;
$topic = $wpdb->get_row( $wpdb->prepare('SELECT ID, post_name, post_parent FROM ' . $wpdb->posts . ' WHERE post_name = %s AND post_type = %s', $slug, bbp_get_topic_post_type()) );
if(is_null($topic)) return $topic;
return $topic;
}
/**
* Filter and limit the content for use in the bbPress topic
* @param text $content Post content to be filtered
* @param int $cut # of characters to keep in the excerpt (set to 0 for whole post)
* @return text filtered content
*/
function bbppt_post_discussion_get_the_content( $content, $cut = 0 ) {
$content = strip_shortcodes( $content );
$content = wp_html_excerpt( $content, $cut );
/** The `the_content_rss` filter will be removed in a future version! */
$content = apply_filters('the_content_rss', $content);
$content = apply_filters( 'bbppt_topic_content_before_link', $content );
return $content;
}
/**
* Remove the original topic post from the replies query for a forum thread
* Made for use with the bbp_has_replies_query filter
*/
function bbppt_remove_topic_from_thread( $bbp_args ) {
$bbp_args['post_type'] = bbp_get_reply_post_type();
return $bbp_args;
}
/**
* Return only the selected number of replies, sorted in the selected way
*/
function bbppt_limit_replies_in_thread( $bbp_args ) {
global $post, $bbp_post_topics;
$settings = $bbp_post_topics->get_topic_options_for_post( $post->ID );
$per_page = $settings['display-extras']['xcount'];
$sort = $settings['display-extras']['xsort'];
$bbp_args['posts_per_page'] = $per_page;
if($sort == 'newest') {
$bbp_args['orderby'] = 'date';
$bbp_args['order'] = 'DESC';
} else if($sort == 'oldest') {
$bbp_args['orderby'] = 'date';
$bbp_args['order'] = 'ASC';
}
return $bbp_args;
}
/**
* Locate a template file for bbPress Topics for Posts
* Search child and parent theme directories before falling back to files included with plugin
*/
function bbppt_locate_template( $template_name, $load = false ) {