PluginProbe
bbPress / 2.0-beta-3b
bbPress v2.0-beta-3b
2.6.17 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 All 72 releases
bbpress / bbp-includes / bbp-core-shortcodes.php

bbp-core-shortcodes.php in bbPress 2.0-beta-3b, at bbp-includes/bbp-core-shortcodes.php

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