/wp-content/plugins/bbpress/includes/common/shortcodes.php

https://bitbucket.org/adatux_/uakami · PHP · 737 lines · 238 code · 152 blank · 347 comment · 28 complexity · 447579a396f29ef4d1e3581b5f0f6e10 MD5 · raw file

  1. <?php
  2. /**
  3. * bbPress Shortcodes
  4. *
  5. * @package bbPress
  6. * @subpackage Shortcodes
  7. */
  8. // Exit if accessed directly
  9. if ( !defined( 'ABSPATH' ) ) exit;
  10. if ( !class_exists( 'BBP_Shortcodes' ) ) :
  11. /**
  12. * bbPress Shortcode Class
  13. *
  14. * @since bbPress (r3031)
  15. */
  16. class BBP_Shortcodes {
  17. /** Vars ******************************************************************/
  18. /**
  19. * @var array Shortcode => function
  20. */
  21. public $codes = array();
  22. /** Functions *************************************************************/
  23. /**
  24. * Add the register_shortcodes action to bbp_init
  25. *
  26. * @since bbPress (r3031)
  27. *
  28. * @uses setup_globals()
  29. * @uses add_shortcodes()
  30. */
  31. public function __construct() {
  32. $this->setup_globals();
  33. $this->add_shortcodes();
  34. }
  35. /**
  36. * Shortcode globals
  37. *
  38. * @since bbPress (r3143)
  39. * @access private
  40. *
  41. * @uses apply_filters()
  42. */
  43. private function setup_globals() {
  44. // Setup the shortcodes
  45. $this->codes = apply_filters( 'bbp_shortcodes', array(
  46. /** Forums ********************************************************/
  47. 'bbp-forum-index' => array( $this, 'display_forum_index' ), // Forum Index
  48. 'bbp-forum-form' => array( $this, 'display_forum_form' ), // Topic form
  49. 'bbp-single-forum' => array( $this, 'display_forum' ), // Specific forum - pass an 'id' attribute
  50. /** Topics ********************************************************/
  51. 'bbp-topic-index' => array( $this, 'display_topic_index' ), // Topic index
  52. 'bbp-topic-form' => array( $this, 'display_topic_form' ), // Topic form
  53. 'bbp-single-topic' => array( $this, 'display_topic' ), // Specific topic - pass an 'id' attribute
  54. /** Topic Tags ****************************************************/
  55. 'bbp-topic-tags' => array( $this, 'display_topic_tags' ), // All topic tags in a cloud
  56. 'bbp-single-tag' => array( $this, 'display_topics_of_tag' ), // Topics of Tag
  57. /** Replies *******************************************************/
  58. 'bbp-reply-form' => array( $this, 'display_reply_form' ), // Reply form
  59. 'bbp-single-reply' => array( $this, 'display_reply' ), // Specific reply - pass an 'id' attribute
  60. /** Views *********************************************************/
  61. 'bbp-single-view' => array( $this, 'display_view' ), // Single view
  62. /** Account *******************************************************/
  63. 'bbp-login' => array( $this, 'display_login' ), // Login
  64. 'bbp-register' => array( $this, 'display_register' ), // Register
  65. 'bbp-lost-pass' => array( $this, 'display_lost_pass' ), // Lost Password
  66. ) );
  67. }
  68. /**
  69. * Register the bbPress shortcodes
  70. *
  71. * @since bbPress (r3031)
  72. *
  73. * @uses add_shortcode()
  74. * @uses do_action()
  75. */
  76. private function add_shortcodes() {
  77. foreach( (array) $this->codes as $code => $function ) {
  78. add_shortcode( $code, $function );
  79. }
  80. }
  81. /**
  82. * Unset some globals in the $bbp object that hold query related info
  83. *
  84. * @since bbPress (r3034)
  85. */
  86. private function unset_globals() {
  87. $bbp = bbpress();
  88. // Unset global queries
  89. $bbp->forum_query = new stdClass;
  90. $bbp->topic_query = new stdClass;
  91. $bbp->reply_query = new stdClass;
  92. // Unset global ID's
  93. $bbp->current_forum_id = 0;
  94. $bbp->current_topic_id = 0;
  95. $bbp->current_reply_id = 0;
  96. $bbp->current_topic_tag_id = 0;
  97. // Reset the post data
  98. wp_reset_postdata();
  99. }
  100. /** Output Buffers ********************************************************/
  101. /**
  102. * Start an output buffer.
  103. *
  104. * This is used to put the contents of the shortcode into a variable rather
  105. * than outputting the HTML at run-time. This allows shortcodes to appear
  106. * in the correct location in the_content() instead of when it's created.
  107. *
  108. * @since bbPress (r3079)
  109. *
  110. * @param string $query_name
  111. *
  112. * @uses bbp_set_query_name()
  113. * @uses ob_start()
  114. */
  115. private function start( $query_name = '' ) {
  116. // Set query name
  117. bbp_set_query_name( $query_name );
  118. // Remove 'bbp_replace_the_content' filter to prevent infinite loops
  119. remove_filter( 'the_content', 'bbp_replace_the_content' );
  120. // Start output buffer
  121. ob_start();
  122. }
  123. /**
  124. * Return the contents of the output buffer and flush its contents.
  125. *
  126. * @since bbPress( r3079)
  127. *
  128. * @uses BBP_Shortcodes::unset_globals() Cleans up global values
  129. * @return string Contents of output buffer.
  130. */
  131. private function end() {
  132. // Put output into usable variable
  133. $output = ob_get_contents();
  134. // Unset globals
  135. $this->unset_globals();
  136. // Flush the output buffer
  137. ob_end_clean();
  138. // Reset the query name
  139. bbp_reset_query_name();
  140. // Add 'bbp_replace_the_content' filter back (@see $this::start())
  141. add_filter( 'the_content', 'bbp_replace_the_content' );
  142. return $output;
  143. }
  144. /** Forum shortcodes ******************************************************/
  145. /**
  146. * Display an index of all visible root level forums in an output buffer
  147. * and return to ensure that post/page contents are displayed first.
  148. *
  149. * @since bbPress (r3031)
  150. *
  151. * @param array $attr
  152. * @param string $content
  153. * @uses bbp_has_forums()
  154. * @uses get_template_part()
  155. * @return string
  156. */
  157. public function display_forum_index() {
  158. // Unset globals
  159. $this->unset_globals();
  160. // Start output buffer
  161. $this->start( 'bbp_forum_archive' );
  162. bbp_get_template_part( 'content', 'archive-forum' );
  163. // Return contents of output buffer
  164. return $this->end();
  165. }
  166. /**
  167. * Display the contents of a specific forum ID in an output buffer
  168. * and return to ensure that post/page contents are displayed first.
  169. *
  170. * @since bbPress (r3031)
  171. *
  172. * @param array $attr
  173. * @param string $content
  174. * @uses get_template_part()
  175. * @uses bbp_single_forum_description()
  176. * @return string
  177. */
  178. public function display_forum( $attr, $content = '' ) {
  179. // Sanity check required info
  180. if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
  181. return $content;
  182. // Set passed attribute to $forum_id for clarity
  183. $forum_id = bbpress()->current_forum_id = $attr['id'];
  184. // Bail if ID passed is not a forum
  185. if ( !bbp_is_forum( $forum_id ) )
  186. return $content;
  187. // Start output buffer
  188. $this->start( 'bbp_single_forum' );
  189. // Check forum caps
  190. if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
  191. bbp_get_template_part( 'content', 'single-forum' );
  192. // Forum is private and user does not have caps
  193. } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
  194. bbp_get_template_part( 'feedback', 'no-access' );
  195. }
  196. // Return contents of output buffer
  197. return $this->end();
  198. }
  199. /**
  200. * Display the forum form in an output buffer and return to ensure
  201. * post/page contents are displayed first.
  202. *
  203. * @since bbPress (r3566)
  204. *
  205. * @uses get_template_part()
  206. */
  207. public function display_forum_form() {
  208. // Start output buffer
  209. $this->start( 'bbp_forum_form' );
  210. // Output templates
  211. bbp_get_template_part( 'form', 'forum' );
  212. // Return contents of output buffer
  213. return $this->end();
  214. }
  215. /** Topic shortcodes ******************************************************/
  216. /**
  217. * Display an index of all visible root level topics in an output buffer
  218. * and return to ensure that post/page contents are displayed first.
  219. *
  220. * @since bbPress (r3031)
  221. *
  222. * @param array $attr
  223. * @param string $content
  224. * @uses bbp_get_hidden_forum_ids()
  225. * @uses get_template_part()
  226. * @return string
  227. */
  228. public function display_topic_index() {
  229. // Unset globals
  230. $this->unset_globals();
  231. // Filter the query
  232. if ( ! bbp_is_topic_archive() ) {
  233. add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'display_topic_index_query' ) );
  234. }
  235. // Start output buffer
  236. $this->start( 'bbp_topic_archive' );
  237. // Output template
  238. bbp_get_template_part( 'content', 'archive-topic' );
  239. // Return contents of output buffer
  240. return $this->end();
  241. }
  242. /**
  243. * Display the contents of a specific topic ID in an output buffer
  244. * and return to ensure that post/page contents are displayed first.
  245. *
  246. * @since bbPress (r3031)
  247. *
  248. * @param array $attr
  249. * @param string $content
  250. * @uses get_template_part()
  251. * @return string
  252. */
  253. public function display_topic( $attr, $content = '' ) {
  254. // Sanity check required info
  255. if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
  256. return $content;
  257. // Unset globals
  258. $this->unset_globals();
  259. // Set passed attribute to $forum_id for clarity
  260. $topic_id = bbpress()->current_topic_id = $attr['id'];
  261. $forum_id = bbp_get_topic_forum_id( $topic_id );
  262. // Bail if ID passed is not a topic
  263. if ( !bbp_is_topic( $topic_id ) )
  264. return $content;
  265. // Reset the queries if not in theme compat
  266. if ( !bbp_is_theme_compat_active() ) {
  267. $bbp = bbpress();
  268. // Reset necessary forum_query attributes for topics loop to function
  269. $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
  270. $bbp->forum_query->in_the_loop = true;
  271. $bbp->forum_query->post = get_post( $forum_id );
  272. // Reset necessary topic_query attributes for topics loop to function
  273. $bbp->topic_query->query_vars['post_type'] = bbp_get_topic_post_type();
  274. $bbp->topic_query->in_the_loop = true;
  275. $bbp->topic_query->post = get_post( $topic_id );
  276. }
  277. // Start output buffer
  278. $this->start( 'bbp_single_topic' );
  279. // Check forum caps
  280. if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
  281. bbp_get_template_part( 'content', 'single-topic' );
  282. // Forum is private and user does not have caps
  283. } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
  284. bbp_get_template_part( 'feedback', 'no-access' );
  285. }
  286. // Return contents of output buffer
  287. return $this->end();
  288. }
  289. /**
  290. * Display the topic form in an output buffer and return to ensure
  291. * post/page contents are displayed first.
  292. *
  293. * @since bbPress (r3031)
  294. *
  295. * @uses get_template_part()
  296. */
  297. public function display_topic_form() {
  298. // Start output buffer
  299. $this->start( 'bbp_topic_form' );
  300. // Output templates
  301. bbp_get_template_part( 'form', 'topic' );
  302. // Return contents of output buffer
  303. return $this->end();
  304. }
  305. /** Replies ***************************************************************/
  306. /**
  307. * Display the contents of a specific reply 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_reply( $attr, $content = '' ) {
  318. // Sanity check required info
  319. if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
  320. return $content;
  321. // Unset globals
  322. $this->unset_globals();
  323. // Set passed attribute to $reply_id for clarity
  324. $reply_id = bbpress()->current_reply_id = $attr['id'];
  325. $forum_id = bbp_get_reply_forum_id( $reply_id );
  326. // Bail if ID passed is not a reply
  327. if ( !bbp_is_reply( $reply_id ) )
  328. return $content;
  329. // Reset the queries if not in theme compat
  330. if ( !bbp_is_theme_compat_active() ) {
  331. $bbp = bbpress();
  332. // Reset necessary forum_query attributes for replys loop to function
  333. $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
  334. $bbp->forum_query->in_the_loop = true;
  335. $bbp->forum_query->post = get_post( $forum_id );
  336. // Reset necessary reply_query attributes for replys loop to function
  337. $bbp->reply_query->query_vars['post_type'] = bbp_get_reply_post_type();
  338. $bbp->reply_query->in_the_loop = true;
  339. $bbp->reply_query->post = get_post( $reply_id );
  340. }
  341. // Start output buffer
  342. $this->start( 'bbp_single_reply' );
  343. // Check forum caps
  344. if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
  345. bbp_get_template_part( 'content', 'single-reply' );
  346. // Forum is private and user does not have caps
  347. } elseif ( bbp_is_forum_private( $forum_id, false ) ) {
  348. bbp_get_template_part( 'feedback', 'no-access' );
  349. }
  350. // Return contents of output buffer
  351. return $this->end();
  352. }
  353. /**
  354. * Display the reply form in an output buffer and return to ensure
  355. * post/page contents are displayed first.
  356. *
  357. * @since bbPress (r3031)
  358. *
  359. * @uses get_template_part()
  360. */
  361. public function display_reply_form() {
  362. // Start output buffer
  363. $this->start( 'bbp_reply_form' );
  364. // Output templates
  365. bbp_get_template_part( 'form', 'reply' );
  366. // Return contents of output buffer
  367. return $this->end();
  368. }
  369. /** Topic Tags ************************************************************/
  370. /**
  371. * Display a tag cloud of all topic tags in an output buffer and return to
  372. * ensure that post/page contents are displayed first.
  373. *
  374. * @since bbPress (r3110)
  375. *
  376. * @return string
  377. */
  378. public function display_topic_tags() {
  379. // Unset globals
  380. $this->unset_globals();
  381. // Start output buffer
  382. $this->start( 'bbp_topic_tags' );
  383. // Output the topic tags
  384. wp_tag_cloud( array(
  385. 'smallest' => 9,
  386. 'largest' => 38,
  387. 'number' => 80,
  388. 'taxonomy' => bbp_get_topic_tag_tax_id()
  389. ) );
  390. // Return contents of output buffer
  391. return $this->end();
  392. }
  393. /**
  394. * Display the contents of a specific topic tag in an output buffer
  395. * and return to ensure that post/page contents are displayed first.
  396. *
  397. * @since bbPress (r3110)
  398. *
  399. * @param array $attr
  400. * @param string $content
  401. * @uses get_template_part()
  402. * @return string
  403. */
  404. public function display_topics_of_tag( $attr, $content = '' ) {
  405. // Sanity check required info
  406. if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
  407. return $content;
  408. // Unset globals
  409. $this->unset_globals();
  410. // Filter the query
  411. if ( ! bbp_is_topic_tag() ) {
  412. add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'display_topics_of_tag_query' ) );
  413. }
  414. // Start output buffer
  415. $this->start( 'bbp_topic_tag' );
  416. // Set passed attribute to $ag_id for clarity
  417. bbpress()->current_topic_tag_id = $tag_id = $attr['id'];
  418. // Output template
  419. bbp_get_template_part( 'content', 'archive-topic' );
  420. // Return contents of output buffer
  421. return $this->end();
  422. }
  423. /**
  424. * Display the contents of a specific topic tag in an output buffer
  425. * and return to ensure that post/page contents are displayed first.
  426. *
  427. * @since bbPress (r3346)
  428. *
  429. * @param array $attr
  430. * @param string $content
  431. * @uses get_template_part()
  432. * @return string
  433. */
  434. public function display_topic_tag_form() {
  435. // Unset globals
  436. $this->unset_globals();
  437. // Start output buffer
  438. $this->start( 'bbp_topic_tag_edit' );
  439. // Output template
  440. bbp_get_template_part( 'content', 'topic-tag-edit' );
  441. // Return contents of output buffer
  442. return $this->end();
  443. }
  444. /** Views *****************************************************************/
  445. /**
  446. * Display the contents of a specific view in an output buffer and return to
  447. * ensure that post/page contents are displayed first.
  448. *
  449. * @since bbPress (r3031)
  450. *
  451. * @param array $attr
  452. * @param string $content
  453. * @uses get_template_part()
  454. * @uses bbp_single_forum_description()
  455. * @return string
  456. */
  457. public function display_view( $attr, $content = '' ) {
  458. // Sanity check required info
  459. if ( empty( $attr['id'] ) )
  460. return $content;
  461. // Set passed attribute to $view_id for clarity
  462. $view_id = $attr['id'];
  463. // Start output buffer
  464. $this->start( 'bbp_single_view' );
  465. // Unset globals
  466. $this->unset_globals();
  467. // Load the view
  468. bbp_view_query( $view_id );
  469. // Output template
  470. bbp_get_template_part( 'content', 'single-view' );
  471. // Return contents of output buffer
  472. return $this->end();
  473. }
  474. /** Account ***************************************************************/
  475. /**
  476. * Display a login form
  477. *
  478. * @since bbPress (r3302)
  479. *
  480. * @return string
  481. */
  482. public function display_login() {
  483. // Unset globals
  484. $this->unset_globals();
  485. // Start output buffer
  486. $this->start( 'bbp_login' );
  487. // Output templates
  488. if ( !is_user_logged_in() )
  489. bbp_get_template_part( 'form', 'user-login' );
  490. else
  491. bbp_get_template_part( 'feedback', 'logged-in' );
  492. // Return contents of output buffer
  493. return $this->end();
  494. }
  495. /**
  496. * Display a register form
  497. *
  498. * @since bbPress (r3302)
  499. *
  500. * @return string
  501. */
  502. public function display_register() {
  503. // Unset globals
  504. $this->unset_globals();
  505. // Start output buffer
  506. $this->start( 'bbp_register' );
  507. // Output templates
  508. if ( !is_user_logged_in() )
  509. bbp_get_template_part( 'form', 'user-register' );
  510. else
  511. bbp_get_template_part( 'feedback', 'logged-in' );
  512. // Return contents of output buffer
  513. return $this->end();
  514. }
  515. /**
  516. * Display a lost password form
  517. *
  518. * @since bbPress (r3302)
  519. *
  520. * @return string
  521. */
  522. public function display_lost_pass() {
  523. // Unset globals
  524. $this->unset_globals();
  525. // Start output buffer
  526. $this->start( 'bbp_lost_pass' );
  527. // Output templates
  528. if ( !is_user_logged_in() )
  529. bbp_get_template_part( 'form', 'user-lost-pass' );
  530. else
  531. bbp_get_template_part( 'feedback', 'logged-in' );
  532. // Return contents of output buffer
  533. return $this->end();
  534. }
  535. /** Other *****************************************************************/
  536. /**
  537. * Display a breadcrumb
  538. *
  539. * @since bbPress (r3302)
  540. *
  541. * @return string
  542. */
  543. public function display_breadcrumb() {
  544. // Unset globals
  545. $this->unset_globals();
  546. // Start output buffer
  547. $this->start();
  548. // Output breadcrumb
  549. bbp_breadcrumb();
  550. // Return contents of output buffer
  551. return $this->end();
  552. }
  553. /** Query Filters *********************************************************/
  554. /**
  555. * Filter the query for the topic index
  556. *
  557. * @since bbPress (r3637)
  558. *
  559. * @param array $args
  560. * @return array
  561. */
  562. public function display_topic_index_query( $args = array() ) {
  563. $args['author'] = 0;
  564. $args['show_stickies'] = true;
  565. $args['order'] = 'DESC';
  566. return $args;
  567. }
  568. /**
  569. * Filter the query for topic tags
  570. *
  571. * @since bbPress (r3637)
  572. *
  573. * @param array $args
  574. * @return array
  575. */
  576. public function display_topics_of_tag_query( $args = array() ) {
  577. $args['tax_query'] = array( array(
  578. 'taxonomy' => bbp_get_topic_tag_tax_id(),
  579. 'field' => 'id',
  580. 'terms' => bbpress()->current_topic_tag_id
  581. ) );
  582. return $args;
  583. }
  584. }
  585. endif;