PluginProbe
bbPress / 2.2
bbPress v2.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 / includes / common / shortcodes.php

shortcodes.php in bbPress 2.2, at includes/common/shortcodes.php

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