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

774 lines 18.3 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 * @return string
247 */
248 public function display_forum( $attr, $content = '' ) {
249
250 // Sanity check required info
251 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
252 return $content;
253
254 // Set passed attribute to $forum_id for clarity
255 $forum_id = bbpress()->current_forum_id = $attr['id'];
256
257 // Bail if ID passed is not a forum
258 if ( !bbp_is_forum( $forum_id ) )
259 return $content;
260
261 // Start output buffer
262 $this->start( 'bbp_single_forum' );
263
264 // Check forum caps
265 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
266 bbp_get_template_part( 'content', 'single-forum' );
267
268 // Forum is private and user does not have caps
269 } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
270 bbp_get_template_part( 'feedback', 'no-access' );
271 }
272
273 // Return contents of output buffer
274 return $this->end();
275 }
276
277 /**
278 * Display the forum form in an output buffer and return to ensure
279 * post/page contents are displayed first.
280 *
281 * @since bbPress (r3566)
282 *
283 * @uses get_template_part()
284 */
285 public function display_forum_form() {
286
287 // Start output buffer
288 $this->start( 'bbp_forum_form' );
289
290 // Output templates
291 bbp_get_template_part( 'form', 'forum' );
292
293 // Return contents of output buffer
294 return $this->end();
295 }
296
297 /** Topic shortcodes ******************************************************/
298
299 /**
300 * Display an index of all visible root level topics in an output buffer
301 * and return to ensure that post/page contents are displayed first.
302 *
303 * @since bbPress (r3031)
304 *
305 * @param array $attr
306 * @param string $content
307 * @uses bbp_get_hidden_forum_ids()
308 * @uses get_template_part()
309 * @return string
310 */
311 public function display_topic_index() {
312
313 // Unset globals
314 $this->unset_globals();
315
316 // Filter the query
317 if ( ! bbp_is_topic_archive() ) {
318 add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'display_topic_index_query' ) );
319 }
320
321 // Start output buffer
322 $this->start( 'bbp_topic_archive' );
323
324 // Output template
325 bbp_get_template_part( 'content', 'archive-topic' );
326
327 // Return contents of output buffer
328 return $this->end();
329 }
330
331 /**
332 * Display the contents of a specific topic ID in an output buffer
333 * and return to ensure that post/page contents are displayed first.
334 *
335 * @since bbPress (r3031)
336 *
337 * @param array $attr
338 * @param string $content
339 * @uses get_template_part()
340 * @return string
341 */
342 public function display_topic( $attr, $content = '' ) {
343
344 // Sanity check required info
345 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
346 return $content;
347
348 // Unset globals
349 $this->unset_globals();
350
351 // Set passed attribute to $forum_id for clarity
352 $topic_id = bbpress()->current_topic_id = $attr['id'];
353 $forum_id = bbp_get_topic_forum_id( $topic_id );
354
355 // Bail if ID passed is not a forum
356 if ( !bbp_is_topic( $topic_id ) )
357 return $content;
358
359 // Reset the queries if not in theme compat
360 if ( !bbp_is_theme_compat_active() ) {
361
362 $bbp = bbpress();
363
364 // Reset necessary forum_query attributes for topics loop to function
365 $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
366 $bbp->forum_query->in_the_loop = true;
367 $bbp->forum_query->post = get_post( $forum_id );
368
369 // Reset necessary topic_query attributes for topics loop to function
370 $bbp->topic_query->query_vars['post_type'] = bbp_get_topic_post_type();
371 $bbp->topic_query->in_the_loop = true;
372 $bbp->topic_query->post = get_post( $topic_id );
373 }
374
375 // Start output buffer
376 $this->start( 'bbp_single_topic' );
377
378 // Check forum caps
379 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
380 bbp_get_template_part( 'content', 'single-topic' );
381
382 // Forum is private and user does not have caps
383 } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
384 bbp_get_template_part( 'feedback', 'no-access' );
385 }
386
387 // Return contents of output buffer
388 return $this->end();
389 }
390
391 /**
392 * Display the topic form in an output buffer and return to ensure
393 * post/page contents are displayed first.
394 *
395 * @since bbPress (r3031)
396 *
397 * @uses get_template_part()
398 */
399 public function display_topic_form() {
400
401 // Start output buffer
402 $this->start( 'bbp_topic_form' );
403
404 // Output templates
405 bbp_get_template_part( 'form', 'topic' );
406
407 // Return contents of output buffer
408 return $this->end();
409 }
410
411 /** Replies ***************************************************************/
412
413 /**
414 * Display the contents of a specific reply ID in an output buffer
415 * and return to ensure that post/page contents are displayed first.
416 *
417 * @since bbPress (r3031)
418 *
419 * @param array $attr
420 * @param string $content
421 * @uses get_template_part()
422 * @return string
423 */
424 public function display_reply( $attr, $content = '' ) {
425
426 // Sanity check required info
427 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
428 return $content;
429
430 // Unset globals
431 $this->unset_globals();
432
433 // Set passed attribute to $reply_id for clarity
434 $reply_id = bbpress()->current_reply_id = $attr['id'];
435 $forum_id = bbp_get_reply_forum_id( $reply_id );
436
437 // Bail if ID passed is not a forum
438 if ( !bbp_is_reply( $reply_id ) )
439 return $content;
440
441 // Reset the queries if not in theme compat
442 if ( !bbp_is_theme_compat_active() ) {
443
444 $bbp = bbpress();
445
446 // Reset necessary forum_query attributes for replys loop to function
447 $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
448 $bbp->forum_query->in_the_loop = true;
449 $bbp->forum_query->post = get_post( $forum_id );
450
451 // Reset necessary reply_query attributes for replys loop to function
452 $bbp->reply_query->query_vars['post_type'] = bbp_get_reply_post_type();
453 $bbp->reply_query->in_the_loop = true;
454 $bbp->reply_query->post = get_post( $reply_id );
455 }
456
457 // Start output buffer
458 $this->start( 'bbp_single_reply' );
459
460 // Check forum caps
461 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
462 bbp_get_template_part( 'content', 'single-reply' );
463
464 // Forum is private and user does not have caps
465 } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
466 bbp_get_template_part( 'feedback', 'no-access' );
467 }
468
469 // Return contents of output buffer
470 return $this->end();
471 }
472
473 /**
474 * Display the reply form in an output buffer and return to ensure
475 * post/page contents are displayed first.
476 *
477 * @since bbPress (r3031)
478 *
479 * @uses get_template_part()
480 */
481 public function display_reply_form() {
482
483 // Start output buffer
484 $this->start( 'bbp_reply_form' );
485
486 // Output templates
487 bbp_get_template_part( 'form', 'reply' );
488
489 // Return contents of output buffer
490 return $this->end();
491 }
492
493 /** Topic Tags ************************************************************/
494
495 /**
496 * Display a tag cloud of all topic tags in an output buffer and return to
497 * ensure that post/page contents are displayed first.
498 *
499 * @since bbPress (r3110)
500 *
501 * @return string
502 */
503 public function display_topic_tags() {
504
505 // Unset globals
506 $this->unset_globals();
507
508 // Start output buffer
509 $this->start( 'bbp_topic_tags' );
510
511 // Output the topic tags
512 wp_tag_cloud( array(
513 'smallest' => 9,
514 'largest' => 38,
515 'number' => 80,
516 'taxonomy' => bbp_get_topic_tag_tax_id()
517 ) );
518
519 // Return contents of output buffer
520 return $this->end();
521 }
522
523 /**
524 * Display the contents of a specific topic tag in an output buffer
525 * and return to ensure that post/page contents are displayed first.
526 *
527 * @since bbPress (r3110)
528 *
529 * @param array $attr
530 * @param string $content
531 * @uses get_template_part()
532 * @return string
533 */
534 public function display_topics_of_tag( $attr, $content = '' ) {
535
536 // Sanity check required info
537 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
538 return $content;
539
540 // Unset globals
541 $this->unset_globals();
542
543 // Filter the query
544 if ( ! bbp_is_topic_tag() ) {
545 add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'display_topics_of_tag_query' ) );
546 }
547
548 // Start output buffer
549 $this->start( 'bbp_topic_tag' );
550
551 // Set passed attribute to $ag_id for clarity
552 bbpress()->current_topic_tag_id = $tag_id = $attr['id'];
553
554 // Output template
555 bbp_get_template_part( 'content', 'archive-topic' );
556
557 // Return contents of output buffer
558 return $this->end();
559 }
560
561 /**
562 * Display the contents of a specific topic tag in an output buffer
563 * and return to ensure that post/page contents are displayed first.
564 *
565 * @since bbPress (r3346)
566 *
567 * @param array $attr
568 * @param string $content
569 * @uses get_template_part()
570 * @return string
571 */
572 public function display_topic_tag_form() {
573
574 // Unset globals
575 $this->unset_globals();
576
577 // Start output buffer
578 $this->start( 'bbp_topic_tag_edit' );
579
580 // Output template
581 bbp_get_template_part( 'content', 'topic-tag-edit' );
582
583 // Return contents of output buffer
584 return $this->end();
585 }
586
587 /** Views *****************************************************************/
588
589 /**
590 * Display the contents of a specific view in an output buffer and return to
591 * ensure that post/page contents are displayed first.
592 *
593 * @since bbPress (r3031)
594 *
595 * @param array $attr
596 * @param string $content
597 * @uses get_template_part()
598 * @uses bbp_single_forum_description()
599 * @return string
600 */
601 public function display_view( $attr, $content = '' ) {
602
603 // Sanity check required info
604 if ( empty( $attr['id'] ) )
605 return $content;
606
607 // Set passed attribute to $view_id for clarity
608 $view_id = $attr['id'];
609
610 // Start output buffer
611 $this->start( 'bbp_single_view' );
612
613 // Unset globals
614 $this->unset_globals();
615
616 // Load the view
617 bbp_view_query( $view_id );
618
619 // Output template
620 bbp_get_template_part( 'content', 'single-view' );
621
622 // Return contents of output buffer
623 return $this->end();
624 }
625
626 /** Account ***************************************************************/
627
628 /**
629 * Display a login form
630 *
631 * @since bbPress (r3302)
632 *
633 * @return string
634 */
635 public function display_login() {
636
637 // Unset globals
638 $this->unset_globals();
639
640 // Start output buffer
641 $this->start( 'bbp_login' );
642
643 // Output templates
644 if ( !is_user_logged_in() )
645 bbp_get_template_part( 'form', 'user-login' );
646 else
647 bbp_get_template_part( 'feedback', 'logged-in' );
648
649 // Return contents of output buffer
650 return $this->end();
651 }
652
653 /**
654 * Display a register form
655 *
656 * @since bbPress (r3302)
657 *
658 * @return string
659 */
660 public function display_register() {
661
662 // Unset globals
663 $this->unset_globals();
664
665 // Start output buffer
666 $this->start( 'bbp_register' );
667
668 // Output templates
669 if ( !is_user_logged_in() )
670 bbp_get_template_part( 'form', 'user-register' );
671 else
672 bbp_get_template_part( 'feedback', 'logged-in' );
673
674 // Return contents of output buffer
675 return $this->end();
676 }
677
678 /**
679 * Display a lost password form
680 *
681 * @since bbPress (r3302)
682 *
683 * @return string
684 */
685 public function display_lost_pass() {
686
687 // Unset globals
688 $this->unset_globals();
689
690 // Start output buffer
691 $this->start( 'bbp_lost_pass' );
692
693 // Output templates
694 if ( !is_user_logged_in() )
695 bbp_get_template_part( 'form', 'user-lost-pass' );
696 else
697 bbp_get_template_part( 'feedback', 'logged-in' );
698
699 // Return contents of output buffer
700 return $this->end();
701 }
702
703 /** Other *****************************************************************/
704
705 /**
706 * Display a breadcrumb
707 *
708 * @since bbPress (r3302)
709 *
710 * @return string
711 */
712 public function display_breadcrumb() {
713
714 // Unset globals
715 $this->unset_globals();
716
717 // Start output buffer
718 $this->ob_start();
719
720 // Output breadcrumb
721 bbp_breadcrumb();
722
723 // Return contents of output buffer
724 return $this->end();
725 }
726
727 /** Query Filters *********************************************************/
728
729 /**
730 * Filter the query for the topic index
731 *
732 * @since bbPress (r3637)
733 *
734 * @param array $args
735 * @return array
736 */
737 public function display_topic_index_query( $args = array() ) {
738 $args['author'] = 0;
739 $args['show_stickies'] = true;
740 $args['order'] = 'DESC';
741 return $args;
742 }
743
744 /**
745 * Filter the query for topic tags
746 *
747 * @since bbPress (r3637)
748 *
749 * @param array $args
750 * @return array
751 */
752 public function display_topics_of_tag_query( $args = array() ) {
753 $args['tax_query'] = array( array(
754 'taxonomy' => bbp_get_topic_tag_tax_id(),
755 'field' => 'id',
756 'terms' => bbpress()->current_topic_tag_id
757 ) );
758
759 return $args;
760 }
761 }
762 endif;
763
764 /**
765 * Register the bbPress shortcodes
766 *
767 * @since bbPress (r3031)
768 *
769 * @uses BBP_Shortcodes
770 */
771 function bbp_register_shortcodes() {
772 bbpress()->shortcodes = new BBP_Shortcodes();
773 }
774