PluginProbe
bbPress / 2.0.2
bbPress v2.0.2
trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.2.2 All 71 releases
bbpress / bbp-includes / bbp-core-shortcodes.php

bbp-core-shortcodes.php in bbPress 2.0.2, at bbp-includes/bbp-core-shortcodes.php

872 lines 20.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Shortcodes
5 *
6 * @package bbPress
7 * @subpackage Shortcodes
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 if ( !class_exists( 'BBP_Shortcodes' ) ) :
14 /**
15 * bbPress Shortcode Class
16 *
17 * @since bbPress (r3031)
18 */
19 class BBP_Shortcodes {
20
21 /** Vars ******************************************************************/
22
23 /**
24 * @var array Shortcode => function
25 */
26 public $codes = array();
27
28 /** Functions *************************************************************/
29
30 /**
31 * Add the register_shortcodes action to bbp_init
32 *
33 * @since bbPress (r3031)
34 *
35 * @uses setup_globals()
36 * @uses add_shortcodes()
37 */
38 public function __construct() {
39 $this->setup_globals();
40 $this->add_shortcodes();
41 }
42
43 /**
44 * Shortcode globals
45 *
46 * @since bbPress (r3143)
47 * @access private
48 *
49 * @uses apply_filters()
50 */
51 private function setup_globals() {
52
53 // Setup the shortcodes
54 $this->codes = apply_filters( 'bbp_shortcodes', array(
55
56 /** Forums ********************************************************/
57
58 // Forum Index
59 'bbp-forum-index' => array( $this, 'display_forum_index' ),
60
61 'bbp-single-forum' => array( $this, 'display_forum' ),
62
63 /** Topics ********************************************************/
64
65 // Topic index
66 'bbp-topic-index' => array( $this, 'display_topic_index' ),
67
68 // Topic form
69 'bbp-topic-form' => array( $this, 'display_topic_form' ),
70
71 // Specific topic - pass an 'id' attribute
72 'bbp-single-topic' => array( $this, 'display_topic' ),
73
74 /** Topic Tags ****************************************************/
75
76 // All topic tags in a cloud
77 'bbp-topic-tags' => array( $this, 'display_topic_tags' ),
78
79 // Topics of tag Tag
80 'bbp-single-topic-tag' => array( $this, 'display_topics_of_tag' ),
81
82 /** Replies *******************************************************/
83
84 // Reply form
85 'bbp-reply-form' => array( $this, 'display_reply_form' ),
86
87 /** Views *********************************************************/
88
89 // Single view
90 'bbp-single-view' => array( $this, 'display_view' ),
91
92 /** Account *******************************************************/
93
94 // Login
95 'bbp-login' => array( $this, 'display_login' ),
96
97 // Register
98 'bbp-register' => array( $this, 'display_register' ),
99
100 // LOst Password
101 'bbp-lost-pass' => array( $this, 'display_lost_pass' ),
102
103 ) );
104 }
105
106 /**
107 * Register the bbPress shortcodes
108 *
109 * @since bbPress (r3031)
110 *
111 * @uses add_shortcode()
112 * @uses do_action()
113 */
114 private function add_shortcodes() {
115
116 // Loop through and add the shortcodes
117 foreach( $this->codes as $code => $function )
118 add_shortcode( $code, $function );
119
120 // Custom shortcodes
121 do_action( 'bbp_register_shortcodes' );
122 }
123
124 /**
125 * Unset some globals in the $bbp object that hold query related info
126 *
127 * @since bbPress (r3034)
128 *
129 * @global bbPress $bbp
130 */
131 private function unset_globals() {
132 global $bbp;
133
134 // Unset global queries
135 $bbp->forum_query = new stdClass;
136 $bbp->topic_query = new stdClass;
137 $bbp->reply_query = new stdClass;
138
139 // Unset global ID's
140 $bbp->current_forum_id = 0;
141 $bbp->current_topic_id = 0;
142 $bbp->current_reply_id = 0;
143
144 // Reset the post data
145 wp_reset_postdata();
146 }
147
148 /** Output Buffers ********************************************************/
149
150 /**
151 * Start an output buffer.
152 *
153 * This is used to put the contents of the shortcode into a variable rather
154 * than outputting the HTML at run-time. This allows shortcodes to appear
155 * in the correct location in the_content() instead of when it's created.
156 *
157 * @since bbPress (r3079)
158 *
159 * @param string $query_name
160 *
161 * @uses bbp_set_query_name()
162 * @uses ob_start()
163 */
164 private function start( $query_name = '' ) {
165
166 // Set query name
167 bbp_set_query_name( $query_name );
168
169 // Start output buffer
170 ob_start();
171 }
172
173 /**
174 * Return the contents of the output buffer and flush its contents.
175 *
176 * @since bbPress( r3079)
177 *
178 * @uses BBP_Shortcodes::unset_globals() Cleans up global values
179 * @return string Contents of output buffer.
180 */
181 private function end() {
182
183 // Put output into usable variable
184 $output = ob_get_contents();
185
186 // Unset globals
187 $this->unset_globals();
188
189 // Flush the output buffer
190 ob_end_clean();
191
192 // Reset the query name
193 bbp_reset_query_name();
194
195 return $output;
196 }
197
198 /** Forum shortcodes ******************************************************/
199
200 /**
201 * Display an index of all visible root level forums in an output buffer
202 * and return to ensure that post/page contents are displayed first.
203 *
204 * @since bbPress (r3031)
205 *
206 * @param array $attr
207 * @param string $content
208 * @uses bbp_has_forums()
209 * @uses current_theme_supports()
210 * @uses get_template_part()
211 * @return string
212 */
213 public function display_forum_index() {
214
215 // Unset globals
216 $this->unset_globals();
217
218 // Start output buffer
219 $this->start( 'bbp_forum_archive' );
220
221 // Breadcrumb
222 bbp_breadcrumb();
223
224 // Before forums index
225 do_action( 'bbp_template_before_forums_index' );
226
227 // Load the forums index
228 if ( bbp_has_forums() )
229 bbp_get_template_part( 'bbpress/loop', 'forums' );
230
231 // No forums
232 else
233 bbp_get_template_part( 'bbpress/feedback', 'no-forums' );
234
235 // After forums index
236 do_action( 'bbp_template_after_forums_index' );
237
238 // Return contents of output buffer
239 return $this->end();
240 }
241
242 /**
243 * Display the contents of a specific forum ID in an output buffer
244 * and return to ensure that post/page contents are displayed first.
245 *
246 * @since bbPress (r3031)
247 *
248 * @param array $attr
249 * @param string $content
250 * @uses bbp_has_topics()
251 * @uses current_theme_supports()
252 * @uses get_template_part()
253 * @uses bbp_single_forum_description()
254 * @return string
255 */
256 public function display_forum( $attr, $content = '' ) {
257 global $bbp;
258
259 // Sanity check required info
260 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
261 return $content;
262
263 // Set passed attribute to $forum_id for clarity
264 $forum_id = $attr['id'];
265
266 // Bail if ID passed is not a forum
267 if ( !bbp_is_forum( $forum_id ) )
268 return $content;
269
270 // Start output buffer
271 $this->start( 'bbp_single_forum' );
272
273 // Check forum caps
274 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
275
276 // Breadcrumb
277 bbp_breadcrumb();
278
279 // Before single forum
280 do_action( 'bbp_template_before_single_forum' );
281
282 // Password protected
283 if ( post_password_required() ) {
284
285 // Output the password form
286 bbp_get_template_part( 'bbpress/form', 'protected' );
287
288 // Not password protected, or password is already approved
289 } else {
290
291 // Forum description
292 bbp_single_forum_description( array( 'forum_id' => $forum_id ) );
293
294 /** Sub forums ****************************************************/
295
296 // Check if forum has subforums first
297 if ( bbp_get_forum_subforum_count( $forum_id ) ) {
298
299 // Forum query
300 $forum_query = array( 'post_parent' => $forum_id );
301
302 // Load the sub forums
303 if ( bbp_has_forums( $forum_query ) )
304 bbp_get_template_part( 'bbpress/loop', 'forums' );
305 }
306
307 /** Topics ********************************************************/
308
309 // Skip if forum is a category
310 if ( !bbp_is_forum_category( $forum_id ) ) {
311
312 // Unset globals
313 $this->unset_globals();
314
315 // Reset necessary forum_query attributes for topics loop to function
316 $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
317 $bbp->forum_query->in_the_loop = true;
318 $bbp->forum_query->post = get_post( $forum_id );
319
320 // Query defaults
321 $topics_query = array(
322 'author' => 0,
323 'post_parent' => $forum_id,
324 'show_stickies' => true,
325 );
326
327 // Load the topic index
328 if ( bbp_has_topics( $topics_query ) ) {
329 bbp_get_template_part( 'bbpress/pagination', 'topics' );
330 bbp_get_template_part( 'bbpress/loop', 'topics' );
331 bbp_get_template_part( 'bbpress/pagination', 'topics' );
332 bbp_get_template_part( 'bbpress/form', 'topic' );
333
334 // No topics
335 } else {
336 bbp_get_template_part( 'bbpress/feedback', 'no-topics' );
337 bbp_get_template_part( 'bbpress/form', 'topic' );
338 }
339 }
340
341 // After single forum
342 do_action( 'bbp_template_after_single_forum' );
343 }
344
345 // Forum is private and user does not have caps
346 } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
347 bbp_get_template_part( 'bbpress/feedback', 'no-access' );
348 }
349
350 // Return contents of output buffer
351 return $this->end();
352 }
353
354 /** Topic shortcodes ******************************************************/
355
356 /**
357 * Display an index of all visible root level topics in an output buffer
358 * and return to ensure that post/page contents are displayed first.
359 *
360 * @since bbPress (r3031)
361 *
362 * @param array $attr
363 * @param string $content
364 * @uses bbp_get_hidden_forum_ids()
365 * @uses bbp_has_topics()
366 * @uses current_theme_supports()
367 * @uses get_template_part()
368 * @return string
369 */
370 public function display_topic_index() {
371
372 // Query defaults
373 $topics_query = array(
374 'author' => 0,
375 'show_stickies' => true,
376 'order' => 'DESC',
377 );
378
379 // Unset globals
380 $this->unset_globals();
381
382 // Start output buffer
383 $this->start( 'bbp_topic_archive' );
384
385 // Breadcrumb
386 bbp_breadcrumb();
387
388 // Before topics index
389 do_action( 'bbp_template_before_topics_index' );
390
391 // Load the topic index
392 if ( bbp_has_topics( $topics_query ) ) {
393 bbp_get_template_part( 'bbpress/pagination', 'topics' );
394 bbp_get_template_part( 'bbpress/loop', 'topics' );
395 bbp_get_template_part( 'bbpress/pagination', 'topics' );
396
397 // No topics
398 } else {
399 bbp_get_template_part( 'bbpress/feedback', 'no-topics' );
400 }
401
402 // After topics index
403 do_action( 'bbp_template_after_topics_index' );
404
405 // Return contents of output buffer
406 return $this->end();
407 }
408
409 /**
410 * Display the contents of a specific topic ID in an output buffer
411 * and return to ensure that post/page contents are displayed first.
412 *
413 * @since bbPress (r3031)
414 *
415 * @global bbPress $bbp
416 *
417 * @param array $attr
418 * @param string $content
419 * @uses current_theme_supports()
420 * @uses get_template_part()
421 * @return string
422 */
423 public function display_topic( $attr, $content = '' ) {
424 global $bbp;
425
426 // Sanity check required info
427 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
428 return $content;
429
430 // Set passed attribute to $forum_id for clarity
431 $topic_id = $attr['id'];
432 $forum_id = bbp_get_topic_forum_id( $topic_id );
433
434 // Bail if ID passed is not a forum
435 if ( !bbp_is_topic( $topic_id ) )
436 return $content;
437
438 // Setup the meta_query
439 $replies_query['meta_query'] = array( array(
440 'key' => '_bbp_topic_id',
441 'value' => $topic_id,
442 'compare' => '='
443 ) );
444
445 // Unset globals
446 $this->unset_globals();
447
448 // Reset the queries if not in theme compat
449 if ( !bbp_is_theme_compat_active() ) {
450
451 // Reset necessary forum_query attributes for topics loop to function
452 $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
453 $bbp->forum_query->in_the_loop = true;
454 $bbp->forum_query->post = get_post( $forum_id );
455
456 // Reset necessary topic_query attributes for topics loop to function
457 $bbp->topic_query->query_vars['post_type'] = bbp_get_topic_post_type();
458 $bbp->topic_query->in_the_loop = true;
459 $bbp->topic_query->post = get_post( $topic_id );
460 }
461
462 // Start output buffer
463 $this->start( 'bbp_single_topic' );
464
465 // Check forum caps
466 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
467
468 // Breadcrumb
469 bbp_breadcrumb();
470
471 // Before single topic
472 do_action( 'bbp_template_before_single_topic' );
473
474 // Password protected
475 if ( post_password_required() ) {
476
477 // Output the password form
478 bbp_get_template_part( 'bbpress/form', 'protected' );
479
480 // Not password protected, or password is already approved
481 } else {
482
483 // Tags
484 bbp_topic_tag_list( $topic_id );
485
486 // Topic description
487 bbp_single_topic_description( array( 'topic_id' => $topic_id ) );
488
489 // Template files
490 if ( bbp_show_lead_topic() )
491 bbp_get_template_part( 'bbpress/content', 'single-topic-lead' );
492
493 // Load the topic
494 if ( bbp_has_replies( $replies_query ) ) {
495 bbp_get_template_part( 'bbpress/pagination', 'replies' );
496 bbp_get_template_part( 'bbpress/loop', 'replies' );
497 bbp_get_template_part( 'bbpress/pagination', 'replies' );
498 }
499
500 // Reply form
501 bbp_get_template_part( 'bbpress/form', 'reply' );
502 }
503
504 // After single topic
505 do_action( 'bbp_template_after_single_topic' );
506
507 // Forum is private and user does not have caps
508 } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
509 bbp_get_template_part( 'bbpress/feedback', 'no-access' );
510 }
511
512 // Return contents of output buffer
513 return $this->end();
514 }
515
516 /**
517 * Display the topic form in an output buffer and return to ensure
518 * post/page contents are displayed first.
519 *
520 * @since bbPress (r3031)
521 *
522 * @uses current_theme_supports()
523 * @uses get_template_part()
524 */
525 public function display_topic_form() {
526
527 // Start output buffer
528 $this->start( 'bbp_topic_form' );
529
530 // Output templates
531 bbp_get_template_part( 'bbpress/form', 'topic' );
532
533 // Return contents of output buffer
534 return $this->end();
535 }
536
537 /** Replies ***************************************************************/
538
539 /**
540 * Display the reply form in an output buffer and return to ensure
541 * post/page contents are displayed first.
542 *
543 * @since bbPress (r3031)
544 *
545 * @uses current_theme_supports()
546 * @uses get_template_part()
547 */
548 public function display_reply_form() {
549
550 // Start output buffer
551 $this->start( 'bbp_reply_form' );
552
553 // Output templates
554 bbp_get_template_part( 'bbpress/form', 'reply' );
555
556 // Return contents of output buffer
557 return $this->end();
558 }
559
560 /** Topic Tags ************************************************************/
561
562 /**
563 * Display a tag cloud of all topic tags in an output buffer and return to
564 * ensure that post/page contents are displayed first.
565 *
566 * @since bbPress (r3110)
567 *
568 * @return string
569 */
570 public function display_topic_tags() {
571
572 // Unset globals
573 $this->unset_globals();
574
575 // Start output buffer
576 $this->start( 'bbp_topic_tags' );
577
578 // Output the topic tags
579 wp_tag_cloud( array(
580 'smallest' => 9,
581 'largest' => 38,
582 'number' => 80,
583 'taxonomy' => bbp_get_topic_tag_tax_id()
584 ) );
585
586 // Return contents of output buffer
587 return $this->end();
588 }
589
590 /**
591 * Display the contents of a specific topic tag in an output buffer
592 * and return to ensure that post/page contents are displayed first.
593 *
594 * @since bbPress (r3110)
595 *
596 * @param array $attr
597 * @param string $content
598 * @uses current_theme_supports()
599 * @uses get_template_part()
600 * @return string
601 */
602 public function display_topics_of_tag( $attr, $content = '' ) {
603
604 // Sanity check required info
605 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
606 return $content;
607
608 // Set passed attribute to $ag_id for clarity
609 $tag_id = $attr['id'];
610
611 // Setup tax query
612 $args = array( 'tax_query' => array( array(
613 'taxonomy' => bbp_get_topic_tag_tax_id(),
614 'field' => 'id',
615 'terms' => $tag_id
616 ) ) );
617
618 // Unset globals
619 $this->unset_globals();
620
621 // Start output buffer
622 $this->start( 'bbp_topics_of_tag' );
623
624 // Breadcrumb
625 bbp_breadcrumb();
626
627 // Tag description
628 bbp_topic_tag_description();
629
630 // Before tag topics
631 do_action( 'bbp_template_before_topic_tag' );
632
633 // Load the topics
634 if ( bbp_has_topics( $args ) ) {
635 bbp_get_template_part( 'bbpress/pagination', 'topics' );
636 bbp_get_template_part( 'bbpress/loop', 'topics' );
637 bbp_get_template_part( 'bbpress/pagination', 'topics' );
638
639 // No topics
640 } else {
641 bbp_get_template_part( 'bbpress/feedback', 'no-topics' );
642 }
643
644 // After tag topics
645 do_action( 'bbp_template_after_topic_tag' );
646
647 // Return contents of output buffer
648 return $this->end();
649 }
650
651 /**
652 * Display the contents of a specific topic tag in an output buffer
653 * and return to ensure that post/page contents are displayed first.
654 *
655 * @since bbPress (r3346)
656 *
657 * @param array $attr
658 * @param string $content
659 * @uses current_theme_supports()
660 * @uses get_template_part()
661 * @return string
662 */
663 public function display_topic_tag_form() {
664
665 // Unset globals
666 $this->unset_globals();
667
668 // Start output buffer
669 $this->start( 'bbp_topic_tag_edit' );
670
671 // Breadcrumb
672 bbp_breadcrumb();
673
674 // Tag description
675 bbp_topic_tag_description();
676
677 // Before tag topics
678 do_action( 'bbp_template_before_topic_tag_edit' );
679
680 // Tag editing form
681 bbp_get_template_part( 'bbpress/form', 'topic-tag' );
682
683 // After tag topics
684 do_action( 'bbp_template_after_topic_tag_edit' );
685
686 // Return contents of output buffer
687 return $this->end();
688 }
689
690 /** Views *****************************************************************/
691
692 /**
693 * Display the contents of a specific view in an output buffer and return to
694 * ensure that post/page contents are displayed first.
695 *
696 * @since bbPress (r3031)
697 *
698 * @param array $attr
699 * @param string $content
700 * @uses bbp_has_topics()
701 * @uses current_theme_supports()
702 * @uses get_template_part()
703 * @uses bbp_single_forum_description()
704 * @return string
705 */
706 public function display_view( $attr, $content = '' ) {
707 global $bbp;
708
709 // Sanity check required info
710 if ( empty( $attr['id'] ) )
711 return $content;
712
713 // Set passed attribute to $view_id for clarity
714 $view_id = $attr['id'];
715
716 // Start output buffer
717 $this->start( 'bbp_single_view' );
718
719 // Breadcrumb
720 bbp_breadcrumb();
721
722 // Password protected
723 if ( post_password_required() ) {
724
725 // Output the password form
726 bbp_get_template_part( 'bbpress/form', 'protected' );
727
728 // Not password protected, or password is already approved
729 } else {
730
731 /** Topics ********************************************************/
732
733 // Unset globals
734 $this->unset_globals();
735
736 // Load the topic index
737 if ( bbp_view_query( $view_id ) ) {
738 bbp_get_template_part( 'bbpress/pagination', 'topics' );
739 bbp_get_template_part( 'bbpress/loop', 'topics' );
740 bbp_get_template_part( 'bbpress/pagination', 'topics' );
741
742 // No topics
743 } else {
744 bbp_get_template_part( 'bbpress/feedback', 'no-topics' );
745 }
746 }
747
748 // Return contents of output buffer
749 return $this->end();
750 }
751
752 /** Account ***************************************************************/
753
754 /**
755 * Display a login form
756 *
757 * @since bbPress (r3302)
758 *
759 * @return string
760 */
761 public function display_login() {
762
763 // Unset globals
764 $this->unset_globals();
765
766 // Start output buffer
767 $this->start( 'bbp_login' );
768
769 // Output templates
770 if ( !is_user_logged_in() )
771 bbp_get_template_part( 'bbpress/form', 'user-login' );
772 else
773 bbp_get_template_part( 'bbpress/feedback', 'logged-in' );
774
775 // Return contents of output buffer
776 return $this->end();
777 }
778
779 /**
780 * Display a register form
781 *
782 * @since bbPress (r3302)
783 *
784 * @return string
785 */
786 public function display_register() {
787
788 // Unset globals
789 $this->unset_globals();
790
791 // Start output buffer
792 $this->start( 'bbp_register' );
793
794 // Output templates
795 if ( !is_user_logged_in() )
796 bbp_get_template_part( 'bbpress/form', 'user-register' );
797 else
798 bbp_get_template_part( 'bbpress/feedback', 'logged-in' );
799
800 // Return contents of output buffer
801 return $this->end();
802 }
803
804 /**
805 * Display a lost password form
806 *
807 * @since bbPress (r3302)
808 *
809 * @return string
810 */
811 public function display_lost_pass() {
812
813 // Unset globals
814 $this->unset_globals();
815
816 // Start output buffer
817 $this->start( 'bbp_lost_pass' );
818
819 // Output templates
820 if ( !is_user_logged_in() )
821 bbp_get_template_part( 'bbpress/form', 'user-lost-pass' );
822 else
823 bbp_get_template_part( 'bbpress/feedback', 'logged-in' );
824
825 // Return contents of output buffer
826 return $this->end();
827 }
828
829 /** Other *****************************************************************/
830
831 /**
832 * Display a breadcrumb
833 *
834 * @since bbPress (r3302)
835 *
836 * @return string
837 */
838 public function display_breadcrumb() {
839
840 // Unset globals
841 $this->unset_globals();
842
843 // Start output buffer
844 $this->ob_start();
845
846 // Output breadcrumb
847 bbp_breadcrumb();
848
849 // Return contents of output buffer
850 return $this->end();
851 }
852 }
853 endif;
854
855 /**
856 * Register the bbPress shortcodes
857 *
858 * @since bbPress (r3031)
859 *
860 * @global bbPress $bbp
861 * @uses BBP_Shortcodes
862 */
863 function bbp_register_shortcodes() {
864 global $bbp;
865
866 // Bail if bbPress is not loaded
867 if ( 'bbPress' !== get_class( $bbp ) ) return;
868
869 $bbp->shortcodes = new BBP_Shortcodes();
870 }
871
872 ?>