[ Index ] |
PHP Cross Reference of BBPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * bbPress Shortcodes 5 * 6 * @package bbPress 7 * @subpackage Shortcodes 8 */ 9 10 // Exit if accessed directly 11 defined( 'ABSPATH' ) || exit; 12 13 if ( ! class_exists( 'BBP_Shortcodes' ) ) : 14 /** 15 * bbPress Shortcode Class 16 * 17 * @since 2.0.0 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 2.0.0 bbPress (r3031) 34 * 35 */ 36 public function __construct() { 37 $this->setup_globals(); 38 $this->add_shortcodes(); 39 } 40 41 /** 42 * Shortcode globals 43 * 44 * @since 2.0.0 bbPress (r3143) 45 * 46 * @access private 47 */ 48 private function setup_globals() { 49 50 // Setup the shortcodes 51 $this->codes = apply_filters( 'bbp_shortcodes', array( 52 53 /** Forums ********************************************************/ 54 55 'bbp-forum-index' => array( $this, 'display_forum_index' ), // Forum Index 56 'bbp-forum-form' => array( $this, 'display_forum_form' ), // Topic form 57 'bbp-single-forum' => array( $this, 'display_forum' ), // Specific forum - pass an 'id' attribute 58 59 /** Topics ********************************************************/ 60 61 'bbp-topic-index' => array( $this, 'display_topic_index' ), // Topic index 62 'bbp-topic-form' => array( $this, 'display_topic_form' ), // Topic form 63 'bbp-single-topic' => array( $this, 'display_topic' ), // Specific topic - pass an 'id' attribute 64 65 /** Topic Tags ****************************************************/ 66 67 'bbp-topic-tags' => array( $this, 'display_topic_tags' ), // All topic tags in a cloud 68 'bbp-single-tag' => array( $this, 'display_topics_of_tag' ), // Topics of Tag 69 70 /** Replies *******************************************************/ 71 72 'bbp-reply-form' => array( $this, 'display_reply_form' ), // Reply form 73 'bbp-single-reply' => array( $this, 'display_reply' ), // Specific reply - pass an 'id' attribute 74 75 /** Views *********************************************************/ 76 77 'bbp-single-view' => array( $this, 'display_view' ), // Single view 78 79 /** Search ********************************************************/ 80 81 'bbp-search-form' => array( $this, 'display_search_form' ), // Search form 82 'bbp-search' => array( $this, 'display_search' ), // Search 83 84 /** Account *******************************************************/ 85 86 'bbp-login' => array( $this, 'display_login' ), // Login 87 'bbp-register' => array( $this, 'display_register' ), // Register 88 'bbp-lost-pass' => array( $this, 'display_lost_pass' ), // Lost Password 89 90 /** Others *******************************************************/ 91 92 'bbp-stats' => array( $this, 'display_stats' ), // Stats 93 ) ); 94 } 95 96 /** 97 * Register the bbPress shortcodes 98 * 99 * @since 2.0.0 bbPress (r3031) 100 */ 101 private function add_shortcodes() { 102 foreach ( (array) $this->codes as $code => $function ) { 103 add_shortcode( $code, $function ); 104 } 105 } 106 107 /** 108 * Unset some globals in the $bbp object that hold query related info 109 * 110 * @since 2.0.0 bbPress (r3034) 111 */ 112 private function unset_globals() { 113 $bbp = bbpress(); 114 115 // Unset global queries 116 $bbp->forum_query = new WP_Query(); 117 $bbp->topic_query = new WP_Query(); 118 $bbp->reply_query = new WP_Query(); 119 $bbp->search_query = new WP_Query(); 120 121 // Unset global ID's 122 $bbp->current_view_id = 0; 123 $bbp->current_forum_id = 0; 124 $bbp->current_topic_id = 0; 125 $bbp->current_reply_id = 0; 126 $bbp->current_topic_tag_id = 0; 127 128 // Reset the post data 129 wp_reset_postdata(); 130 } 131 132 /** Output Buffers ********************************************************/ 133 134 /** 135 * Start an output buffer. 136 * 137 * This is used to put the contents of the shortcode into a variable rather 138 * than outputting the HTML at run-time. This allows shortcodes to appear 139 * in the correct location in the_content() instead of when it's created. 140 * 141 * @since 2.0.0 bbPress (r3079) 142 * 143 * @param string $query_name 144 */ 145 private function start( $query_name = '' ) { 146 147 // Set query name 148 bbp_set_query_name( $query_name ); 149 150 // Start output buffer 151 ob_start(); 152 } 153 154 /** 155 * Return the contents of the output buffer and flush its contents. 156 * 157 * @since 2.0.0 bbPress (r3079) 158 * 159 * @return string Contents of output buffer. 160 */ 161 private function end() { 162 163 // Unset globals 164 $this->unset_globals(); 165 166 // Get the query name, for filter 167 $query_name = bbp_get_query_name(); 168 169 // Reset the query name 170 bbp_reset_query_name(); 171 172 // Return and flush the output buffer 173 $output = ob_get_clean(); 174 175 // Filter & return 176 return apply_filters( 'bbp_display_shortcode', $output, $query_name ); 177 } 178 179 /** Forum shortcodes ******************************************************/ 180 181 /** 182 * Display an index of all visible root level forums in an output buffer 183 * and return to ensure that post/page contents are displayed first. 184 * 185 * @since 2.0.0 bbPress (r3031) 186 * 187 * @return string 188 */ 189 public function display_forum_index() { 190 191 // Unset globals 192 $this->unset_globals(); 193 194 // Start output buffer 195 $this->start( 'bbp_forum_archive' ); 196 197 bbp_get_template_part( 'content', 'archive-forum' ); 198 199 // Return contents of output buffer 200 return $this->end(); 201 } 202 203 /** 204 * Display the contents of a specific forum ID in an output buffer 205 * and return to ensure that post/page contents are displayed first. 206 * 207 * @since 2.0.0 bbPress (r3031) 208 * 209 * @param array $attr 210 * @param string $content 211 * @return string 212 */ 213 public function display_forum( $attr, $content = '' ) { 214 215 // Sanity check required info 216 if ( ! empty( $content ) || ( empty( $attr['id'] ) || ! is_numeric( $attr['id'] ) ) ) { 217 return $content; 218 } 219 220 // Set passed attribute to $forum_id for clarity 221 $forum_id = bbpress()->current_forum_id = $attr['id']; 222 223 // Bail if ID passed is not a forum 224 if ( ! bbp_is_forum( $forum_id ) ) { 225 return $content; 226 } 227 228 // Start output buffer 229 $this->start( 'bbp_single_forum' ); 230 231 // Check forum caps 232 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) { 233 bbp_get_template_part( 'content', 'single-forum' ); 234 235 // Forum is private and user does not have caps 236 } elseif ( bbp_is_forum_private( $forum_id, false ) ) { 237 bbp_get_template_part( 'feedback', 'no-access' ); 238 } 239 240 // Return contents of output buffer 241 return $this->end(); 242 } 243 244 /** 245 * Display the forum form in an output buffer and return to ensure 246 * post/page contents are displayed first. 247 * 248 * @since 2.1.0 bbPress (r3566) 249 */ 250 public function display_forum_form() { 251 252 // Start output buffer 253 $this->start( 'bbp_forum_form' ); 254 255 // Output templates 256 bbp_get_template_part( 'form', 'forum' ); 257 258 // Return contents of output buffer 259 return $this->end(); 260 } 261 262 /** Topic shortcodes ******************************************************/ 263 264 /** 265 * Display an index of all visible root level topics in an output buffer 266 * and return to ensure that post/page contents are displayed first. 267 * 268 * @since 2.0.0 bbPress (r3031) 269 * 270 * @return string 271 */ 272 public function display_topic_index() { 273 274 // Unset globals 275 $this->unset_globals(); 276 277 // Filter the query 278 if ( ! bbp_is_topic_archive() ) { 279 add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'display_topic_index_query' ) ); 280 } 281 282 // Start output buffer 283 $this->start( 'bbp_topic_archive' ); 284 285 // Output template 286 bbp_get_template_part( 'content', 'archive-topic' ); 287 288 // Return contents of output buffer 289 return $this->end(); 290 } 291 292 /** 293 * Display the contents of a specific topic ID in an output buffer 294 * and return to ensure that post/page contents are displayed first. 295 * 296 * @since 2.0.0 bbPress (r3031) 297 * 298 * @param array $attr 299 * @param string $content 300 * @return string 301 */ 302 public function display_topic( $attr, $content = '' ) { 303 304 // Sanity check required info 305 if ( ! empty( $content ) || ( empty( $attr['id'] ) || ! is_numeric( $attr['id'] ) ) ) { 306 return $content; 307 } 308 309 // Unset globals 310 $this->unset_globals(); 311 312 // Set passed attribute to $forum_id for clarity 313 $topic_id = bbpress()->current_topic_id = $attr['id']; 314 $forum_id = bbp_get_topic_forum_id( $topic_id ); 315 316 // Bail if ID passed is not a topic 317 if ( ! bbp_is_topic( $topic_id ) ) { 318 return $content; 319 } 320 321 // Reset the queries if not in theme compat 322 if ( ! bbp_is_theme_compat_active() ) { 323 324 $bbp = bbpress(); 325 326 // Reset necessary forum_query attributes for topics loop to function 327 $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type(); 328 $bbp->forum_query->in_the_loop = true; 329 $bbp->forum_query->post = get_post( $forum_id ); 330 331 // Reset necessary topic_query attributes for topics loop to function 332 $bbp->topic_query->query_vars['post_type'] = bbp_get_topic_post_type(); 333 $bbp->topic_query->in_the_loop = true; 334 $bbp->topic_query->post = get_post( $topic_id ); 335 } 336 337 // Start output buffer 338 $this->start( 'bbp_single_topic' ); 339 340 // Check forum caps 341 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) { 342 bbp_get_template_part( 'content', 'single-topic' ); 343 344 // Forum is private and user does not have caps 345 } elseif ( bbp_is_forum_private( $forum_id, false ) ) { 346 bbp_get_template_part( 'feedback', 'no-access' ); 347 } 348 349 // Return contents of output buffer 350 return $this->end(); 351 } 352 353 /** 354 * Display the topic form in an output buffer and return to ensure 355 * post/page contents are displayed first. 356 * 357 * Supports 'forum_id' attribute to display the topic form for a particular 358 * forum. This currently has styling issues from not being wrapped in 359 * <div id="bbpress-forums" class="bbpress-wrapper"></div> which will need to be sorted out later. 360 * 361 * @since 2.0.0 bbPress (r3031) 362 * 363 * @param array $attr 364 * @param string $content 365 * @return string 366 */ 367 public function display_topic_form( $attr = array(), $content = '' ) { 368 369 // Sanity check supplied info 370 if ( ! empty( $content ) || ( ! empty( $attr['forum_id'] ) && ( ! is_numeric( $attr['forum_id'] ) || ! bbp_is_forum( $attr['forum_id'] ) ) ) ) { 371 return $content; 372 } 373 374 // Unset globals 375 $this->unset_globals(); 376 377 // If forum id is set, use the 'bbp_single_forum' query name 378 if ( ! empty( $attr['forum_id'] ) ) { 379 380 // Set the global current_forum_id for future requests 381 bbpress()->current_forum_id = $forum_id = bbp_get_forum_id( $attr['forum_id'] ); 382 383 // Start output buffer 384 $this->start( 'bbp_single_forum' ); 385 386 // No forum id was passed 387 } else { 388 389 // Set the $forum_id variable to satisfy checks below 390 $forum_id = 0; 391 392 // Start output buffer 393 $this->start( 'bbp_topic_form' ); 394 } 395 396 // If the forum id is set, check forum caps else display normal topic form 397 if ( empty( $forum_id ) || bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) { 398 bbp_get_template_part( 'form', 'topic' ); 399 400 // Forum is private and user does not have caps 401 } elseif ( bbp_is_forum_private( $forum_id, false ) ) { 402 bbp_get_template_part( 'feedback', 'no-access' ); 403 } 404 405 // Return contents of output buffer 406 return $this->end(); 407 } 408 409 /** Replies ***************************************************************/ 410 411 /** 412 * Display the contents of a specific reply ID in an output buffer 413 * and return to ensure that post/page contents are displayed first. 414 * 415 * @since 2.0.0 bbPress (r3031) 416 * 417 * @param array $attr 418 * @param string $content 419 * @return string 420 */ 421 public function display_reply( $attr, $content = '' ) { 422 423 // Sanity check required info 424 if ( ! empty( $content ) || ( empty( $attr['id'] ) || ! is_numeric( $attr['id'] ) ) ) { 425 return $content; 426 } 427 428 // Unset globals 429 $this->unset_globals(); 430 431 // Set passed attribute to $reply_id for clarity 432 $reply_id = bbpress()->current_reply_id = $attr['id']; 433 $forum_id = bbp_get_reply_forum_id( $reply_id ); 434 435 // Bail if ID passed is not a reply 436 if ( ! bbp_is_reply( $reply_id ) ) { 437 return $content; 438 } 439 440 // Reset the queries if not in theme compat 441 if ( ! bbp_is_theme_compat_active() ) { 442 443 $bbp = bbpress(); 444 445 // Reset necessary forum_query attributes for reply loop to function 446 $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type(); 447 $bbp->forum_query->in_the_loop = true; 448 $bbp->forum_query->post = get_post( $forum_id ); 449 450 // Reset necessary reply_query attributes for reply loop to function 451 $bbp->reply_query->query_vars['post_type'] = bbp_get_reply_post_type(); 452 $bbp->reply_query->in_the_loop = true; 453 $bbp->reply_query->post = get_post( $reply_id ); 454 } 455 456 // Start output buffer 457 $this->start( 'bbp_single_reply' ); 458 459 // Check forum caps 460 if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) { 461 bbp_get_template_part( 'content', 'single-reply' ); 462 463 // Forum is private and user does not have caps 464 } elseif ( bbp_is_forum_private( $forum_id, false ) ) { 465 bbp_get_template_part( 'feedback', 'no-access' ); 466 } 467 468 // Return contents of output buffer 469 return $this->end(); 470 } 471 472 /** 473 * Display the reply form in an output buffer and return to ensure 474 * post/page contents are displayed first. 475 * 476 * @since 2.0.0 bbPress (r3031) 477 */ 478 public function display_reply_form() { 479 480 // Start output buffer 481 $this->start( 'bbp_reply_form' ); 482 483 // Output templates 484 bbp_get_template_part( 'form', 'reply' ); 485 486 // Return contents of output buffer 487 return $this->end(); 488 } 489 490 /** Topic Tags ************************************************************/ 491 492 /** 493 * Display a tag cloud of all topic tags in an output buffer and return to 494 * ensure that post/page contents are displayed first. 495 * 496 * @since 2.0.0 bbPress (r3110) 497 * 498 * @return string 499 */ 500 public function display_topic_tags() { 501 502 // Unset globals 503 $this->unset_globals(); 504 505 // Start output buffer 506 $this->start( 'bbp_topic_tags' ); 507 508 // Output the topic tags 509 wp_tag_cloud( array( 510 'smallest' => 9, 511 'largest' => 38, 512 'number' => 80, 513 'taxonomy' => bbp_get_topic_tag_tax_id() 514 ) ); 515 516 // Return contents of output buffer 517 return $this->end(); 518 } 519 520 /** 521 * Display the contents of a specific topic tag in an output buffer 522 * and return to ensure that post/page contents are displayed first. 523 * 524 * @since 2.0.0 bbPress (r3110) 525 * 526 * @param array $attr 527 * @param string $content 528 * @return string 529 */ 530 public function display_topics_of_tag( $attr, $content = '' ) { 531 532 // Sanity check required info 533 if ( ! empty( $content ) || ( empty( $attr['id'] ) || ! is_numeric( $attr['id'] ) ) ) { 534 return $content; 535 } 536 537 // Unset globals 538 $this->unset_globals(); 539 540 // Filter the query 541 if ( ! bbp_is_topic_tag() ) { 542 add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'display_topics_of_tag_query' ) ); 543 } 544 545 // Start output buffer 546 $this->start( 'bbp_topic_tag' ); 547 548 // Set passed attribute to $ag_id for clarity 549 bbpress()->current_topic_tag_id = $tag_id = $attr['id']; 550 551 // Output template 552 bbp_get_template_part( 'content', 'archive-topic' ); 553 554 // Return contents of output buffer 555 return $this->end(); 556 } 557 558 /** 559 * Display the contents of a specific topic tag in an output buffer 560 * and return to ensure that post/page contents are displayed first. 561 * 562 * @since 2.0.0 bbPress (r3346) 563 * 564 * @return string 565 */ 566 public function display_topic_tag_form() { 567 568 // Unset globals 569 $this->unset_globals(); 570 571 // Start output buffer 572 $this->start( 'bbp_topic_tag_edit' ); 573 574 // Output template 575 bbp_get_template_part( 'content', 'topic-tag-edit' ); 576 577 // Return contents of output buffer 578 return $this->end(); 579 } 580 581 /** Views *****************************************************************/ 582 583 /** 584 * Display the contents of a specific view in an output buffer and return to 585 * ensure that post/page contents are displayed first. 586 * 587 * @since 2.0.0 bbPress (r3031) 588 * 589 * @param array $attr 590 * @param string $content 591 * @return string 592 */ 593 public function display_view( $attr, $content = '' ) { 594 595 // Sanity check required info 596 if ( empty( $attr['id'] ) ) { 597 return $content; 598 } 599 600 // Set passed attribute to $view_id for clarity 601 $view_id = $attr['id']; 602 603 // Start output buffer 604 $this->start( 'bbp_single_view' ); 605 606 // Unset globals 607 $this->unset_globals(); 608 609 // Set the current view ID 610 bbpress()->current_view_id = $view_id; 611 612 // Load the view 613 bbp_view_query( $view_id ); 614 615 // Output template 616 bbp_get_template_part( 'content', 'single-view' ); 617 618 // Return contents of output buffer 619 return $this->end(); 620 } 621 622 /** Search ****************************************************************/ 623 624 /** 625 * Display the search form in an output buffer and return to ensure 626 * post/page contents are displayed first. 627 * 628 * @since 2.3.0 bbPress (r4585) 629 */ 630 public function display_search_form() { 631 632 // Bail if search is disabled 633 if ( ! bbp_allow_search() ) { 634 return; 635 } 636 637 // Start output buffer 638 $this->start( 'bbp_search_form' ); 639 640 // Output templates 641 bbp_get_template_part( 'form', 'search' ); 642 643 // Return contents of output buffer 644 return $this->end(); 645 } 646 647 /** 648 * Display the contents of search results in an output buffer and return to 649 * ensure that post/page contents are displayed first. 650 * 651 * @since 2.3.0 bbPress (r4579) 652 * 653 * @param array $attr 654 * @param string $content 655 */ 656 public function display_search( $attr, $content = '' ) { 657 658 // Sanity check required info 659 if ( ! empty( $content ) ) { 660 return $content; 661 } 662 663 // Bail if search is disabled 664 if ( ! bbp_allow_search() ) { 665 return; 666 } 667 668 // Trim search attribute if it's set 669 if ( isset( $attr['search'] ) ) { 670 $attr['search'] = trim( $attr['search'] ); 671 } 672 673 // Set passed attribute to $search_terms for clarity 674 $search_terms = empty( $attr['search'] ) 675 ? bbp_get_search_terms() 676 : $attr['search']; 677 678 // Get the rewrite ID (one time, to avoid repeated calls) 679 $rewrite_id = bbp_get_search_rewrite_id(); 680 681 // Unset globals 682 $this->unset_globals(); 683 684 // Set terms for query 685 set_query_var( $rewrite_id, $search_terms ); 686 687 // Start output buffer 688 $this->start( $rewrite_id ); 689 690 // Output template 691 bbp_get_template_part( 'content', 'search' ); 692 693 // Return contents of output buffer 694 return $this->end(); 695 } 696 697 /** Account ***************************************************************/ 698 699 /** 700 * Display a login form 701 * 702 * @since 2.0.0 bbPress (r3302) 703 * 704 * @return string 705 */ 706 public function display_login() { 707 708 // Unset globals 709 $this->unset_globals(); 710 711 // Start output buffer 712 $this->start( 'bbp_login' ); 713 714 // Output templates 715 if ( ! is_user_logged_in() ) { 716 bbp_get_template_part( 'form', 'user-login' ); 717 } else { 718 bbp_get_template_part( 'feedback', 'logged-in' ); 719 } 720 721 // Return contents of output buffer 722 return $this->end(); 723 } 724 725 /** 726 * Display a register form 727 * 728 * @since 2.0.0 bbPress (r3302) 729 * 730 * @return string 731 */ 732 public function display_register() { 733 734 // Unset globals 735 $this->unset_globals(); 736 737 // Start output buffer 738 $this->start( 'bbp_register' ); 739 740 // Output templates 741 if ( ! is_user_logged_in() ) { 742 bbp_get_template_part( 'form', 'user-register' ); 743 } else { 744 bbp_get_template_part( 'feedback', 'logged-in' ); 745 } 746 747 // Return contents of output buffer 748 return $this->end(); 749 } 750 751 /** 752 * Display a lost password form 753 * 754 * @since 2.0.0 bbPress (r3302) 755 * 756 * @return string 757 */ 758 public function display_lost_pass() { 759 760 // Unset globals 761 $this->unset_globals(); 762 763 // Start output buffer 764 $this->start( 'bbp_lost_pass' ); 765 766 // Output templates 767 if ( ! is_user_logged_in() ) { 768 bbp_get_template_part( 'form', 'user-lost-pass' ); 769 } else { 770 bbp_get_template_part( 'feedback', 'logged-in' ); 771 } 772 773 // Return contents of output buffer 774 return $this->end(); 775 } 776 777 /** Other *****************************************************************/ 778 779 /** 780 * Display forum statistics 781 * 782 * @since 2.3.0 bbPress (r4509) 783 * 784 * @return string 785 */ 786 public function display_stats() { 787 788 // Unset globals 789 $this->unset_globals(); 790 791 // Start output buffer 792 $this->start(); 793 794 // Output statistics 795 bbp_get_template_part( 'content', 'statistics' ); 796 797 // Return contents of output buffer 798 return $this->end(); 799 } 800 801 /** 802 * Display a breadcrumb 803 * 804 * @since 2.0.0 bbPress (r3302) 805 * 806 * @return string 807 */ 808 public function display_breadcrumb() { 809 810 // Unset globals 811 $this->unset_globals(); 812 813 // Start output buffer 814 $this->start(); 815 816 // Output breadcrumb 817 bbp_breadcrumb(); 818 819 // Return contents of output buffer 820 return $this->end(); 821 } 822 823 /** Query Filters *********************************************************/ 824 825 /** 826 * Filter the query for the topic index 827 * 828 * @since 2.1.0 bbPress (r3637) 829 * 830 * @param array $args 831 * @return array 832 */ 833 public function display_topic_index_query( $args = array() ) { 834 $args['author'] = 0; 835 $args['show_stickies'] = true; 836 $args['order'] = 'DESC'; 837 return $args; 838 } 839 840 /** 841 * Filter the query for topic tags 842 * 843 * @since 2.1.0 bbPress (r3637) 844 * 845 * @param array $args 846 * @return array 847 */ 848 public function display_topics_of_tag_query( $args = array() ) { 849 $args['tax_query'] = array( array( 850 'taxonomy' => bbp_get_topic_tag_tax_id(), 851 'field' => 'id', 852 'terms' => bbpress()->current_topic_tag_id 853 ) ); 854 855 return $args; 856 } 857 } 858 endif;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Dec 21 01:00:52 2024 | Cross-referenced by PHPXref 0.7.1 |