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

689 lines 16.6 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 // Redirect 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-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 ) );
104 }
105
106 /**
107 * Register the bbPress shortcodes
108 *
109 * @since bbPress (r3031)
110 *
111 * @uses add_shortcode()
112 * @uses do_action()
113 */
114 function _add_shortcodes() {
115
116 // Loop through the shortcodes
117 foreach( $this->codes as $code => $function )
118
119 // Add each shortcode
120 add_shortcode( $code, $function );
121
122 // Custom shortcodes
123 do_action( 'bbp_register_shortcodes' );
124 }
125
126 /**
127 * Unset some globals in the $bbp object that hold query related info
128 *
129 * @since bbPress (r3034)
130 *
131 * @global bbPress $bbp
132 */
133 function _unset_globals() {
134 global $bbp;
135
136 // Unset global queries
137 $bbp->forum_query = null;
138 $bbp->topic_query = null;
139 $bbp->reply_query = null;
140
141 // Unset global ID's
142 $bbp->current_forum_id = null;
143 $bbp->current_topic_id = null;
144 $bbp->current_reply_id = null;
145
146 // Reset the post data
147 wp_reset_postdata();
148 }
149
150 /** Output Buffers ********************************************************/
151
152 /**
153 * Start an output buffer.
154 *
155 * This is used to put the contents of the shortcode into a variable rather
156 * than outputting the HTML at run-time. This allows shortcodes to appear
157 * in the correct location in the_content() instead of when it's created.
158 *
159 * @since bbPress (r3079)
160 * @uses ob_start()
161 */
162 function _ob_start() {
163 ob_start();
164 }
165
166 /**
167 * Return the contents of the output buffer and flush its contents.
168 *
169 * @since bbPress( r3079)
170 *
171 * @uses BBP_Shortcodes::_unset_globals() Cleans up global values
172 * @return string Contents of output buffer.
173 */
174 function _ob_end() {
175
176 // Put output into usable variable
177 $output = ob_get_contents();
178
179 // Unset globals
180 $this->_unset_globals();
181
182 // Flush the output buffer
183 ob_end_clean();
184
185 return $output;
186 }
187
188 /** Forum shortcodes ******************************************************/
189
190 /**
191 * Display an index of all visible root level forums in an output buffer
192 * and return to ensure that post/page contents are displayed first.
193 *
194 * @since bbPress (r3031)
195 *
196 * @param array $attr
197 * @param string $content
198 * @uses bbp_has_forums()
199 * @uses current_theme_supports()
200 * @uses get_template_part()
201 * @return string
202 */
203 function display_forum_index() {
204
205 // Start output buffer
206 $this->_ob_start();
207
208 // Load the forums index
209 if ( bbp_has_forums() )
210 bbp_get_template_part( 'bbpress/loop', 'forums' );
211
212 // No forums
213 else
214 bbp_get_template_part( 'bbpress/no', 'forums' );
215
216 // Return contents of output buffer
217 return $this->_ob_end();
218 }
219
220 /**
221 * Display the contents of a specific forum ID in an output buffer
222 * and return to ensure that post/page contents are displayed first.
223 *
224 * @since bbPress (r3031)
225 *
226 * @param array $attr
227 * @param string $content
228 * @uses bbp_has_topics()
229 * @uses current_theme_supports()
230 * @uses get_template_part()
231 * @uses bbp_single_forum_description()
232 * @return string
233 */
234 function display_forum( $attr, $content = '' ) {
235 global $bbp;
236
237 // Sanity check required info
238 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
239 return $content;
240
241 // Set passed attribute to $forum_id for clarity
242 $forum_id = $attr['id'];
243
244 // Bail if ID passed is not a forum
245 if ( !bbp_is_forum( $forum_id ) )
246 return $content;
247
248 // Start output buffer
249 $this->_ob_start();
250
251 // Display breadcrumb if a subforum
252 bbp_get_template_part( 'bbpress/nav', 'breadcrumb' );
253
254 // Password protected
255 if ( post_password_required() ) {
256
257 // Output the password form
258 bbp_get_template_part( 'bbpress/form', 'protected' );
259
260 // Not password protected, or password is already approved
261 } else {
262
263 // Check forum caps
264 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
265
266 // Forum description
267 bbp_single_forum_description( array( 'forum_id' => $forum_id ) );
268
269 /** Sub forums ****************************************************/
270
271 // Check if forum has subforums first
272 if ( bbp_get_forum_subforum_count( $forum_id ) ) {
273
274 // Forum query
275 $forum_query = array( 'post_parent' => $forum_id );
276
277 // Load the sub forums
278 if ( bbp_has_forums( $forum_query ) )
279 bbp_get_template_part( 'bbpress/loop', 'forums' );
280 }
281
282 /** Topics ********************************************************/
283
284 // Skip if forum is a category
285 if ( !bbp_is_forum_category( $forum_id ) ) {
286
287 // Unset globals
288 $this->_unset_globals();
289
290 // Reset necessary forum_query attributes for topics loop to function
291 $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
292 $bbp->forum_query->in_the_loop = true;
293 $bbp->forum_query->post = get_post( $forum_id );
294
295 // Query defaults
296 $topics_query = array(
297 'author' => 0,
298 'post_parent' => $forum_id,
299 'show_stickies' => true,
300 );
301
302 // Load the topic index
303 if ( bbp_has_topics( $topics_query ) ) {
304 bbp_get_template_part( 'bbpress/pagination', 'topics' );
305 bbp_get_template_part( 'bbpress/loop', 'topics' );
306 bbp_get_template_part( 'bbpress/pagination', 'topics' );
307 bbp_get_template_part( 'bbpress/form', 'topic' );
308
309 // No topics
310 } else {
311 bbp_get_template_part( 'bbpress/no', 'topics' );
312 bbp_get_template_part( 'bbpress/form', 'topic' );
313 }
314 }
315
316 // Forum is private and user does not have caps
317 } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
318 bbp_get_template_part( 'bbpress/no', 'access' );
319 }
320 }
321
322 // Return contents of output buffer
323 return $this->_ob_end();
324 }
325
326 /** Topic shortcodes ******************************************************/
327
328 /**
329 * Display an index of all visible root level topics in an output buffer
330 * and return to ensure that post/page contents are displayed first.
331 *
332 * @since bbPress (r3031)
333 *
334 * @param array $attr
335 * @param string $content
336 * @uses bbp_get_hidden_forum_ids()
337 * @uses bbp_has_topics()
338 * @uses current_theme_supports()
339 * @uses get_template_part()
340 * @return string
341 */
342 function display_topic_index() {
343
344 // Query defaults
345 $topics_query = array(
346 'author' => 0,
347 'show_stickies' => true,
348 'order' => 'DESC',
349 );
350
351 // Remove any topics from hidden forums
352 $topics_query = bbp_exclude_forum_ids( $topics_query );
353
354 // Unset globals
355 $this->_unset_globals();
356
357 // Start output buffer
358 ob_start();
359
360 // Load the topic index
361 if ( bbp_has_topics( $topics_query ) ) {
362 bbp_get_template_part( 'bbpress/pagination', 'topics' );
363 bbp_get_template_part( 'bbpress/loop', 'topics' );
364 bbp_get_template_part( 'bbpress/pagination', 'topics' );
365
366 // No topics
367 } else {
368 bbp_get_template_part( 'bbpress/no', 'topics' );
369 }
370
371 // Return contents of output buffer
372 return $this->_ob_end();
373 }
374
375 /**
376 * Display the contents of a specific topic ID in an output buffer
377 * and return to ensure that post/page contents are displayed first.
378 *
379 * @since bbPress (r3031)
380 *
381 * @global bbPress $bbp
382 *
383 * @param array $attr
384 * @param string $content
385 * @uses current_theme_supports()
386 * @uses get_template_part()
387 * @return string
388 */
389 function display_topic( $attr, $content = '' ) {
390 global $bbp;
391
392 // Sanity check required info
393 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
394 return $content;
395
396 // Set passed attribute to $forum_id for clarity
397 $topic_id = $attr['id'];
398 $forum_id = bbp_get_topic_forum_id( $topic_id );
399
400 // Bail if ID passed is not a forum
401 if ( !bbp_is_topic( $topic_id ) )
402 return $content;
403
404 // Setup the meta_query
405 $replies_query['meta_query'] = array( array(
406 'key' => '_bbp_topic_id',
407 'value' => $topic_id,
408 'compare' => '='
409 ) );
410
411 // Unset globals
412 $this->_unset_globals();
413
414 // Reset necessary forum_query attributes for topics loop to function
415 $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
416 $bbp->forum_query->in_the_loop = true;
417 $bbp->forum_query->post = get_post( $forum_id );
418
419 // Reset necessary topic_query attributes for topics loop to function
420 $bbp->topic_query->query_vars['post_type'] = bbp_get_topic_post_type();
421 $bbp->topic_query->in_the_loop = true;
422 $bbp->topic_query->post = get_post( $topic_id );
423
424 // Start output buffer
425 $this->_ob_start();
426
427 // Breadcrumb
428 bbp_get_template_part( 'bbpress/nav', 'breadcrumb' );
429
430 // Password protected
431 if ( post_password_required() ) {
432
433 // Output the password form
434 bbp_get_template_part( 'bbpress/form', 'protected' );
435
436 // Not password protected, or password is already approved
437 } else {
438
439 // Check forum caps
440 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
441
442 // Load the topic
443 if ( bbp_has_replies( $replies_query ) ) {
444
445 // Tags
446 bbp_topic_tag_list( $topic_id );
447
448 // Topic description
449 bbp_single_topic_description( array( 'topic_id' => $topic_id ) );
450
451 // Template files
452 bbp_get_template_part( 'bbpress/single', 'topic' );
453 bbp_get_template_part( 'bbpress/pagination', 'replies' );
454 bbp_get_template_part( 'bbpress/loop', 'replies' );
455 bbp_get_template_part( 'bbpress/pagination', 'replies' );
456 bbp_get_template_part( 'bbpress/form', 'reply' );
457
458 // No replies
459 } else {
460 bbp_get_template_part( 'bbpress/single', 'topic' );
461 bbp_get_template_part( 'bbpress/form', 'reply' );
462 }
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( 'bbpress/no', 'access' );
467 }
468 }
469
470 // Return contents of output buffer
471 return $this->_ob_end();
472 }
473
474 /**
475 * Display the topic form in an output buffer and return to ensure
476 * post/page contents are displayed first.
477 *
478 * @since bbPress (r3031)
479 *
480 * @uses current_theme_supports()
481 * @uses get_template_part()
482 */
483 function display_topic_form() {
484
485 // Start output buffer
486 $this->_ob_start();
487
488 // Output templates
489 bbp_get_template_part( 'bbpress/form', 'topic' );
490
491 // Return contents of output buffer
492 return $this->_ob_end();
493 }
494
495 /** Replies ***************************************************************/
496
497 /**
498 * Display the reply form in an output buffer and return to ensure
499 * post/page contents are displayed first.
500 *
501 * @since bbPress (r3031)
502 *
503 * @uses current_theme_supports()
504 * @uses get_template_part()
505 */
506 function display_reply_form() {
507
508 // Start output buffer
509 $this->_ob_start();
510
511 // Output templates
512 bbp_get_template_part( 'bbpress/form', 'reply' );
513
514 // Return contents of output buffer
515 return $this->_ob_end();
516 }
517
518 /** Topic Tags ************************************************************/
519
520 /**
521 * Display a tag cloud of all topic tags in an output buffer and return to
522 * ensure that post/page contents are displayed first.
523 *
524 * @since bbPress (r3110)
525 *
526 * @global bbPress $bbp
527 *
528 * @return string
529 */
530 function display_topic_tags() {
531 global $bbp;
532
533 // Unset globals
534 $this->_unset_globals();
535
536 // Start output buffer
537 $this->_ob_start();
538
539 // Output the topic tags
540 wp_tag_cloud( array(
541 'smallest' => 9,
542 'largest' => 38,
543 'number' => 80,
544 'taxonomy' => $bbp->topic_tag_id
545 ) );
546
547 // Return contents of output buffer
548 return $this->_ob_end();
549 }
550
551 /**
552 * Display the contents of a specific topic tag in an output buffer
553 * and return to ensure that post/page contents are displayed first.
554 *
555 * @since bbPress (r3110)
556 *
557 * @global bbPress $bbp
558 *
559 * @param array $attr
560 * @param string $content
561 * @uses current_theme_supports()
562 * @uses get_template_part()
563 * @return string
564 */
565 function display_topics_of_tag( $attr, $content = '' ) {
566 global $bbp;
567
568 // Sanity check required info
569 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
570 return $content;
571
572 // Set passed attribute to $ag_id for clarity
573 $tag_id = $attr['id'];
574
575 // Setup tax query
576 $args = array( 'tax_query' => array( array(
577 'taxonomy' => $bbp->topic_tag_id,
578 'field' => 'id',
579 'terms' => $tag_id
580 ) ) );
581
582 // Unset globals
583 $this->_unset_globals();
584
585 // Start output buffer
586 $this->_ob_start();
587
588 // Tag description
589 bbp_topic_tag_description();
590
591 // Load the topics
592 if ( bbp_has_topics( $args ) ) {
593
594 // Template files
595 bbp_get_template_part( 'bbpress/pagination', 'topics' );
596 bbp_get_template_part( 'bbpress/loop', 'topics' );
597 bbp_get_template_part( 'bbpress/pagination', 'topics' );
598 bbp_get_template_part( 'bbpress/form', 'topic-tag' );
599
600 // No topics
601 } else {
602 bbp_get_template_part( 'bbpress/no', 'topics' );
603 }
604
605 // Return contents of output buffer
606 return $this->_ob_end();
607 }
608
609 /** Views *****************************************************************/
610
611 /**
612 * Display the contents of a specific view in an output buffer and return to
613 * ensure that post/page contents are displayed first.
614 *
615 * @since bbPress (r3031)
616 *
617 * @param array $attr
618 * @param string $content
619 * @uses bbp_has_topics()
620 * @uses current_theme_supports()
621 * @uses get_template_part()
622 * @uses bbp_single_forum_description()
623 * @return string
624 */
625 function display_view( $attr, $content = '' ) {
626 global $bbp;
627
628 // Sanity check required info
629 if ( empty( $attr['id'] ) )
630 return $content;
631
632 // Set passed attribute to $view_id for clarity
633 $view_id = $attr['id'];
634
635 // Start output buffer
636 $this->_ob_start();
637
638 // Display breadcrumb if a subforum
639 bbp_get_template_part( 'bbpress/nav', 'breadcrumb' );
640
641 // Password protected
642 if ( post_password_required() ) {
643
644 // Output the password form
645 bbp_get_template_part( 'bbpress/form', 'protected' );
646
647 // Not password protected, or password is already approved
648 } else {
649
650 /** Topics ********************************************************/
651
652 // Unset globals
653 $this->_unset_globals();
654
655 // Load the topic index
656 if ( bbp_view_query( $view_id ) ) {
657 bbp_get_template_part( 'bbpress/pagination', 'topics' );
658 bbp_get_template_part( 'bbpress/loop', 'topics' );
659 bbp_get_template_part( 'bbpress/pagination', 'topics' );
660 bbp_get_template_part( 'bbpress/form', 'topic' );
661
662 // No topics
663 } else {
664 bbp_get_template_part( 'bbpress/no', 'topics' );
665 bbp_get_template_part( 'bbpress/form', 'topic' );
666 }
667 }
668
669 // Return contents of output buffer
670 return $this->_ob_end();
671 }
672 }
673 endif;
674
675 /**
676 * Register the bbPress shortcodes
677 *
678 * @since bbPress (r3031)
679 *
680 * @global bbPress $bbp
681 * @uses BBP_Shortcodes
682 */
683 function bbp_register_shortcodes() {
684 global $bbp;
685
686 $bbp->shortcodes = new BBP_Shortcodes();
687 }
688
689 ?>