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

779 lines 18.4 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 // Start output buffer
380 $this->start( 'bbp_single_topic' );
381
382 // Check forum caps
383 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
384 bbp_get_template_part( 'content', 'single-topic' );
385
386 // Forum is private and user does not have caps
387 } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
388 bbp_get_template_part( 'feedback', 'no-access' );
389 }
390
391 // Return contents of output buffer
392 return $this->end();
393 }
394
395 /**
396 * Display the topic form in an output buffer and return to ensure
397 * post/page contents are displayed first.
398 *
399 * @since bbPress (r3031)
400 *
401 * @uses get_template_part()
402 */
403 public function display_topic_form() {
404
405 // Start output buffer
406 $this->start( 'bbp_topic_form' );
407
408 // Output templates
409 bbp_get_template_part( 'form', 'topic' );
410
411 // Return contents of output buffer
412 return $this->end();
413 }
414
415 /** Replies ***************************************************************/
416
417 /**
418 * Display the contents of a specific reply ID in an output buffer
419 * and return to ensure that post/page contents are displayed first.
420 *
421 * @since bbPress (r3031)
422 *
423 * @param array $attr
424 * @param string $content
425 * @uses get_template_part()
426 * @return string
427 */
428 public function display_reply( $attr, $content = '' ) {
429
430 // Sanity check required info
431 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
432 return $content;
433
434 // Unset globals
435 $this->unset_globals();
436
437 // Set passed attribute to $reply_id for clarity
438 $reply_id = bbpress()->current_reply_id = $attr['id'];
439 $forum_id = bbp_get_reply_forum_id( $reply_id );
440
441 // Bail if ID passed is not a forum
442 if ( !bbp_is_reply( $reply_id ) )
443 return $content;
444
445 // Reset the queries if not in theme compat
446 if ( !bbp_is_theme_compat_active() ) {
447
448 $bbp = bbpress();
449
450 // Reset necessary forum_query attributes for replys loop to function
451 $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
452 $bbp->forum_query->in_the_loop = true;
453 $bbp->forum_query->post = get_post( $forum_id );
454
455 // Reset necessary reply_query attributes for replys loop to function
456 $bbp->reply_query->query_vars['post_type'] = bbp_get_reply_post_type();
457 $bbp->reply_query->in_the_loop = true;
458 $bbp->reply_query->post = get_post( $reply_id );
459 }
460
461 // Start output buffer
462 $this->start( 'bbp_single_reply' );
463
464 // Check forum caps
465 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
466 bbp_get_template_part( 'content', 'single-reply' );
467
468 // Forum is private and user does not have caps
469 } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
470 bbp_get_template_part( 'feedback', 'no-access' );
471 }
472
473 // Return contents of output buffer
474 return $this->end();
475 }
476
477 /**
478 * Display the reply form in an output buffer and return to ensure
479 * post/page contents are displayed first.
480 *
481 * @since bbPress (r3031)
482 *
483 * @uses get_template_part()
484 */
485 public function display_reply_form() {
486
487 // Start output buffer
488 $this->start( 'bbp_reply_form' );
489
490 // Output templates
491 bbp_get_template_part( 'form', 'reply' );
492
493 // Return contents of output buffer
494 return $this->end();
495 }
496
497 /** Topic Tags ************************************************************/
498
499 /**
500 * Display a tag cloud of all topic tags in an output buffer and return to
501 * ensure that post/page contents are displayed first.
502 *
503 * @since bbPress (r3110)
504 *
505 * @return string
506 */
507 public function display_topic_tags() {
508
509 // Unset globals
510 $this->unset_globals();
511
512 // Start output buffer
513 $this->start( 'bbp_topic_tags' );
514
515 // Output the topic tags
516 wp_tag_cloud( array(
517 'smallest' => 9,
518 'largest' => 38,
519 'number' => 80,
520 'taxonomy' => bbp_get_topic_tag_tax_id()
521 ) );
522
523 // Return contents of output buffer
524 return $this->end();
525 }
526
527 /**
528 * Display the contents of a specific topic tag in an output buffer
529 * and return to ensure that post/page contents are displayed first.
530 *
531 * @since bbPress (r3110)
532 *
533 * @param array $attr
534 * @param string $content
535 * @uses get_template_part()
536 * @return string
537 */
538 public function display_topics_of_tag( $attr, $content = '' ) {
539
540 // Sanity check required info
541 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
542 return $content;
543
544 // Unset globals
545 $this->unset_globals();
546
547 // Filter the query
548 if ( ! bbp_is_topic_tag() ) {
549 add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'display_topics_of_tag_query' ) );
550 }
551
552 // Start output buffer
553 $this->start( 'bbp_topic_tag' );
554
555 // Set passed attribute to $ag_id for clarity
556 bbpress()->current_topic_tag_id = $tag_id = $attr['id'];
557
558 // Output template
559 bbp_get_template_part( 'content', 'archive-topic' );
560
561 // Return contents of output buffer
562 return $this->end();
563 }
564
565 /**
566 * Display the contents of a specific topic tag in an output buffer
567 * and return to ensure that post/page contents are displayed first.
568 *
569 * @since bbPress (r3346)
570 *
571 * @param array $attr
572 * @param string $content
573 * @uses get_template_part()
574 * @return string
575 */
576 public function display_topic_tag_form() {
577
578 // Unset globals
579 $this->unset_globals();
580
581 // Start output buffer
582 $this->start( 'bbp_topic_tag_edit' );
583
584 // Output template
585 bbp_get_template_part( 'content', 'topic-tag-edit' );
586
587 // Return contents of output buffer
588 return $this->end();
589 }
590
591 /** Views *****************************************************************/
592
593 /**
594 * Display the contents of a specific view in an output buffer and return to
595 * ensure that post/page contents are displayed first.
596 *
597 * @since bbPress (r3031)
598 *
599 * @param array $attr
600 * @param string $content
601 * @uses bbp_has_topics()
602 * @uses get_template_part()
603 * @uses bbp_single_forum_description()
604 * @return string
605 */
606 public function display_view( $attr, $content = '' ) {
607
608 // Sanity check required info
609 if ( empty( $attr['id'] ) )
610 return $content;
611
612 // Set passed attribute to $view_id for clarity
613 $view_id = $attr['id'];
614
615 // Start output buffer
616 $this->start( 'bbp_single_view' );
617
618 // Unset globals
619 $this->unset_globals();
620
621 // Load the view
622 bbp_view_query( $view_id );
623
624 // Output template
625 bbp_get_template_part( 'content', 'single-view' );
626
627 // Return contents of output buffer
628 return $this->end();
629 }
630
631 /** Account ***************************************************************/
632
633 /**
634 * Display a login form
635 *
636 * @since bbPress (r3302)
637 *
638 * @return string
639 */
640 public function display_login() {
641
642 // Unset globals
643 $this->unset_globals();
644
645 // Start output buffer
646 $this->start( 'bbp_login' );
647
648 // Output templates
649 if ( !is_user_logged_in() )
650 bbp_get_template_part( 'form', 'user-login' );
651 else
652 bbp_get_template_part( 'feedback', 'logged-in' );
653
654 // Return contents of output buffer
655 return $this->end();
656 }
657
658 /**
659 * Display a register form
660 *
661 * @since bbPress (r3302)
662 *
663 * @return string
664 */
665 public function display_register() {
666
667 // Unset globals
668 $this->unset_globals();
669
670 // Start output buffer
671 $this->start( 'bbp_register' );
672
673 // Output templates
674 if ( !is_user_logged_in() )
675 bbp_get_template_part( 'form', 'user-register' );
676 else
677 bbp_get_template_part( 'feedback', 'logged-in' );
678
679 // Return contents of output buffer
680 return $this->end();
681 }
682
683 /**
684 * Display a lost password form
685 *
686 * @since bbPress (r3302)
687 *
688 * @return string
689 */
690 public function display_lost_pass() {
691
692 // Unset globals
693 $this->unset_globals();
694
695 // Start output buffer
696 $this->start( 'bbp_lost_pass' );
697
698 // Output templates
699 if ( !is_user_logged_in() )
700 bbp_get_template_part( 'form', 'user-lost-pass' );
701 else
702 bbp_get_template_part( 'feedback', 'logged-in' );
703
704 // Return contents of output buffer
705 return $this->end();
706 }
707
708 /** Other *****************************************************************/
709
710 /**
711 * Display a breadcrumb
712 *
713 * @since bbPress (r3302)
714 *
715 * @return string
716 */
717 public function display_breadcrumb() {
718
719 // Unset globals
720 $this->unset_globals();
721
722 // Start output buffer
723 $this->ob_start();
724
725 // Output breadcrumb
726 bbp_breadcrumb();
727
728 // Return contents of output buffer
729 return $this->end();
730 }
731
732 /** Query Filters *********************************************************/
733
734 /**
735 * Filter the query for the topic index
736 *
737 * @since bbPress (r3637)
738 *
739 * @param array $args
740 * @return array
741 */
742 public function display_topic_index_query( $args = array() ) {
743 $args['author'] = 0;
744 $args['show_stickies'] = true;
745 $args['order'] = 'DESC';
746 return $args;
747 }
748
749 /**
750 * Filter the query for topic tags
751 *
752 * @since bbPress (r3637)
753 *
754 * @param array $args
755 * @return array
756 */
757 public function display_topics_of_tag_query( $args = array() ) {
758 $args['tax_query'] = array( array(
759 'taxonomy' => bbp_get_topic_tag_tax_id(),
760 'field' => 'id',
761 'terms' => bbpress()->current_topic_tag_id
762 ) );
763
764 return $args;
765 }
766 }
767 endif;
768
769 /**
770 * Register the bbPress shortcodes
771 *
772 * @since bbPress (r3031)
773 *
774 * @uses BBP_Shortcodes
775 */
776 function bbp_register_shortcodes() {
777 bbpress()->shortcodes = new BBP_Shortcodes();
778 }
779