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

711 lines 16.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 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 // Breadcrumb
252 bbp_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_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 // Tags
443 bbp_topic_tag_list( $topic_id );
444
445 // Topic description
446 bbp_single_topic_description( array( 'topic_id' => $topic_id ) );
447
448 // Load the topic
449 if ( bbp_has_replies( $replies_query ) ) {
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 // Breadcrumb
489 if ( bbp_is_forum() || bbp_is_topic_edit() )
490 bbp_breadcrumb();
491
492 // Editing a topic
493 if ( bbp_is_topic_edit() ) {
494
495 // Tags
496 bbp_topic_tag_list( get_the_ID() );
497
498 // Topic description
499 bbp_single_topic_description( array( 'topic_id' => get_the_ID() ) );
500
501 }
502
503 // Output templates
504 bbp_get_template_part( 'bbpress/form', 'topic' );
505
506 // Return contents of output buffer
507 return $this->_ob_end();
508 }
509
510 /** Replies ***************************************************************/
511
512 /**
513 * Display the reply form in an output buffer and return to ensure
514 * post/page contents are displayed first.
515 *
516 * @since bbPress (r3031)
517 *
518 * @uses current_theme_supports()
519 * @uses get_template_part()
520 */
521 function display_reply_form() {
522
523 // Start output buffer
524 $this->_ob_start();
525
526 // Breadcrumb
527 if ( bbp_is_reply_edit() )
528 bbp_breadcrumb();
529
530 // Output templates
531 bbp_get_template_part( 'bbpress/form', 'reply' );
532
533 // Return contents of output buffer
534 return $this->_ob_end();
535 }
536
537 /** Topic Tags ************************************************************/
538
539 /**
540 * Display a tag cloud of all topic tags in an output buffer and return to
541 * ensure that post/page contents are displayed first.
542 *
543 * @since bbPress (r3110)
544 *
545 * @global bbPress $bbp
546 *
547 * @return string
548 */
549 function display_topic_tags() {
550 global $bbp;
551
552 // Unset globals
553 $this->_unset_globals();
554
555 // Start output buffer
556 $this->_ob_start();
557
558 // Output the topic tags
559 wp_tag_cloud( array(
560 'smallest' => 9,
561 'largest' => 38,
562 'number' => 80,
563 'taxonomy' => $bbp->topic_tag_id
564 ) );
565
566 // Return contents of output buffer
567 return $this->_ob_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 (r3110)
575 *
576 * @global bbPress $bbp
577 *
578 * @param array $attr
579 * @param string $content
580 * @uses current_theme_supports()
581 * @uses get_template_part()
582 * @return string
583 */
584 function display_topics_of_tag( $attr, $content = '' ) {
585 global $bbp;
586
587 // Sanity check required info
588 if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
589 return $content;
590
591 // Set passed attribute to $ag_id for clarity
592 $tag_id = $attr['id'];
593
594 // Setup tax query
595 $args = array( 'tax_query' => array( array(
596 'taxonomy' => $bbp->topic_tag_id,
597 'field' => 'id',
598 'terms' => $tag_id
599 ) ) );
600
601 // Unset globals
602 $this->_unset_globals();
603
604 // Start output buffer
605 $this->_ob_start();
606
607 // Breadcrumb
608 bbp_breadcrumb();
609
610 // Tag description
611 bbp_topic_tag_description();
612
613 // Load the topics
614 if ( bbp_has_topics( $args ) ) {
615
616 // Template files
617 bbp_get_template_part( 'bbpress/pagination', 'topics' );
618 bbp_get_template_part( 'bbpress/loop', 'topics' );
619 bbp_get_template_part( 'bbpress/pagination', 'topics' );
620 bbp_get_template_part( 'bbpress/form', 'topic-tag' );
621
622 // No topics
623 } else {
624 bbp_get_template_part( 'bbpress/no', 'topics' );
625 }
626
627 // Return contents of output buffer
628 return $this->_ob_end();
629 }
630
631 /** Views *****************************************************************/
632
633 /**
634 * Display the contents of a specific view in an output buffer and return to
635 * ensure that post/page contents are displayed first.
636 *
637 * @since bbPress (r3031)
638 *
639 * @param array $attr
640 * @param string $content
641 * @uses bbp_has_topics()
642 * @uses current_theme_supports()
643 * @uses get_template_part()
644 * @uses bbp_single_forum_description()
645 * @return string
646 */
647 function display_view( $attr, $content = '' ) {
648 global $bbp;
649
650 // Sanity check required info
651 if ( empty( $attr['id'] ) )
652 return $content;
653
654 // Set passed attribute to $view_id for clarity
655 $view_id = $attr['id'];
656
657 // Start output buffer
658 $this->_ob_start();
659
660 // Breadcrumb
661 bbp_breadcrumb();
662
663 // Password protected
664 if ( post_password_required() ) {
665
666 // Output the password form
667 bbp_get_template_part( 'bbpress/form', 'protected' );
668
669 // Not password protected, or password is already approved
670 } else {
671
672 /** Topics ********************************************************/
673
674 // Unset globals
675 $this->_unset_globals();
676
677 // Load the topic index
678 if ( bbp_view_query( $view_id ) ) {
679 bbp_get_template_part( 'bbpress/pagination', 'topics' );
680 bbp_get_template_part( 'bbpress/loop', 'topics' );
681 bbp_get_template_part( 'bbpress/pagination', 'topics' );
682 bbp_get_template_part( 'bbpress/form', 'topic' );
683
684 // No topics
685 } else {
686 bbp_get_template_part( 'bbpress/no', 'topics' );
687 bbp_get_template_part( 'bbpress/form', 'topic' );
688 }
689 }
690
691 // Return contents of output buffer
692 return $this->_ob_end();
693 }
694 }
695 endif;
696
697 /**
698 * Register the bbPress shortcodes
699 *
700 * @since bbPress (r3031)
701 *
702 * @global bbPress $bbp
703 * @uses BBP_Shortcodes
704 */
705 function bbp_register_shortcodes() {
706 global $bbp;
707
708 $bbp->shortcodes = new BBP_Shortcodes();
709 }
710
711 ?>