PluginProbe
bbPress / 2.1-rc1
bbPress v2.1-rc1
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.1-rc1, at bbp-includes/bbp-core-shortcodes.php

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