/includes/core/functions.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress Core Functions 
  5. * 
  6. * @package bbPress 
  7. * @subpackage Functions 
  8. */ 
  9.  
  10. // Exit if accessed directly 
  11. if ( !defined( 'ABSPATH' ) ) exit; 
  12.  
  13. /** Versions ******************************************************************/ 
  14.  
  15. /** 
  16. * Output the bbPress version 
  17. * 
  18. * @since bbPress (r3468) 
  19. * @uses bbp_get_version() To get the bbPress version 
  20. */ 
  21. function bbp_version() { 
  22. echo bbp_get_version(); 
  23. /** 
  24. * Return the bbPress version 
  25. * 
  26. * @since bbPress (r3468) 
  27. * @retrun string The bbPress version 
  28. */ 
  29. function bbp_get_version() { 
  30. return bbpress()->version; 
  31.  
  32. /** 
  33. * Output the bbPress database version 
  34. * 
  35. * @since bbPress (r3468) 
  36. * @uses bbp_get_version() To get the bbPress version 
  37. */ 
  38. function bbp_db_version() { 
  39. echo bbp_get_db_version(); 
  40. /** 
  41. * Return the bbPress database version 
  42. * 
  43. * @since bbPress (r3468) 
  44. * @retrun string The bbPress version 
  45. */ 
  46. function bbp_get_db_version() { 
  47. return bbpress()->db_version; 
  48.  
  49. /** 
  50. * Output the bbPress database version directly from the database 
  51. * 
  52. * @since bbPress (r3468) 
  53. * @uses bbp_get_version() To get the current bbPress version 
  54. */ 
  55. function bbp_db_version_raw() { 
  56. echo bbp_get_db_version_raw(); 
  57. /** 
  58. * Return the bbPress database version directly from the database 
  59. * 
  60. * @since bbPress (r3468) 
  61. * @retrun string The current bbPress version 
  62. */ 
  63. function bbp_get_db_version_raw() { 
  64. return get_option( '_bbp_db_version', '' ); 
  65.  
  66. /** Post Meta *****************************************************************/ 
  67.  
  68. /** 
  69. * Update a posts forum meta ID 
  70. * 
  71. * @since bbPress (r3181) 
  72. * 
  73. * @param int $post_id The post to update 
  74. * @param int $forum_id The forum 
  75. */ 
  76. function bbp_update_forum_id( $post_id, $forum_id ) { 
  77.  
  78. // Allow the forum ID to be updated 'just in time' before save 
  79. $forum_id = apply_filters( 'bbp_update_forum_id', $forum_id, $post_id ); 
  80.  
  81. // Update the post meta forum ID 
  82. update_post_meta( $post_id, '_bbp_forum_id', (int) $forum_id ); 
  83.  
  84. /** 
  85. * Update a posts topic meta ID 
  86. * 
  87. * @since bbPress (r3181) 
  88. * 
  89. * @param int $post_id The post to update 
  90. * @param int $forum_id The forum 
  91. */ 
  92. function bbp_update_topic_id( $post_id, $topic_id ) { 
  93.  
  94. // Allow the topic ID to be updated 'just in time' before save 
  95. $topic_id = apply_filters( 'bbp_update_topic_id', $topic_id, $post_id ); 
  96.  
  97. // Update the post meta topic ID 
  98. update_post_meta( $post_id, '_bbp_topic_id', (int) $topic_id ); 
  99.  
  100. /** 
  101. * Update a posts reply meta ID 
  102. * 
  103. * @since bbPress (r3181) 
  104. * 
  105. * @param int $post_id The post to update 
  106. * @param int $forum_id The forum 
  107. */ 
  108. function bbp_update_reply_id( $post_id, $reply_id ) { 
  109.  
  110. // Allow the reply ID to be updated 'just in time' before save 
  111. $reply_id = apply_filters( 'bbp_update_reply_id', $reply_id, $post_id ); 
  112.  
  113. // Update the post meta reply ID 
  114. update_post_meta( $post_id, '_bbp_reply_id', (int) $reply_id ); 
  115.  
  116. /** Views *********************************************************************/ 
  117.  
  118. /** 
  119. * Get the registered views 
  120. * 
  121. * Does nothing much other than return the {@link $bbp->views} variable 
  122. * 
  123. * @since bbPress (r2789) 
  124. * 
  125. * @return array Views 
  126. */ 
  127. function bbp_get_views() { 
  128. return bbpress()->views; 
  129.  
  130. /** 
  131. * Register a bbPress view 
  132. * 
  133. * @todo Implement feeds - See {@link http://trac.bbpress.org/ticket/1422} 
  134. * 
  135. * @since bbPress (r2789) 
  136. * 
  137. * @param string $view View name 
  138. * @param string $title View title 
  139. * @param mixed $query_args {@link bbp_has_topics()} arguments. 
  140. * @param bool $feed Have a feed for the view? Defaults to true. NOT IMPLEMENTED 
  141. * @param string $capability Capability that the current user must have 
  142. * @uses sanitize_title() To sanitize the view name 
  143. * @uses esc_html() To sanitize the view title 
  144. * @return array The just registered (but processed) view 
  145. */ 
  146. function bbp_register_view( $view, $title, $query_args = '', $feed = true, $capability = '' ) { 
  147.  
  148. // Bail if user does not have capability 
  149. if ( ! empty( $capability ) && ! current_user_can( $capability ) ) 
  150. return false; 
  151.  
  152. $bbp = bbpress(); 
  153. $view = sanitize_title( $view ); 
  154. $title = esc_html( $title ); 
  155.  
  156. if ( empty( $view ) || empty( $title ) ) 
  157. return false; 
  158.  
  159. $query_args = bbp_parse_args( $query_args, '', 'register_view' ); 
  160.  
  161. // Set show_stickies to false if it wasn't supplied 
  162. if ( !isset( $query_args['show_stickies'] ) ) 
  163. $query_args['show_stickies'] = false; 
  164.  
  165. $bbp->views[$view] = array( 
  166. 'title' => $title,  
  167. 'query' => $query_args,  
  168. 'feed' => $feed 
  169. ); 
  170.  
  171. return $bbp->views[$view]; 
  172.  
  173. /** 
  174. * Deregister a bbPress view 
  175. * 
  176. * @since bbPress (r2789) 
  177. * 
  178. * @param string $view View name 
  179. * @uses sanitize_title() To sanitize the view name 
  180. * @return bool False if the view doesn't exist, true on success 
  181. */ 
  182. function bbp_deregister_view( $view ) { 
  183. $bbp = bbpress(); 
  184. $view = sanitize_title( $view ); 
  185.  
  186. if ( !isset( $bbp->views[$view] ) ) 
  187. return false; 
  188.  
  189. unset( $bbp->views[$view] ); 
  190.  
  191. return true; 
  192.  
  193. /** 
  194. * Run the view's query 
  195. * 
  196. * @since bbPress (r2789) 
  197. * 
  198. * @param string $view Optional. View id 
  199. * @param mixed $new_args New arguments. See {@link bbp_has_topics()} 
  200. * @uses bbp_get_view_id() To get the view id 
  201. * @uses bbp_get_view_query_args() To get the view query args 
  202. * @uses sanitize_title() To sanitize the view name 
  203. * @uses bbp_has_topics() To make the topics query 
  204. * @return bool False if the view doesn't exist, otherwise if topics are there 
  205. */ 
  206. function bbp_view_query( $view = '', $new_args = '' ) { 
  207.  
  208. $view = bbp_get_view_id( $view ); 
  209. if ( empty( $view ) ) 
  210. return false; 
  211.  
  212. $query_args = bbp_get_view_query_args( $view ); 
  213.  
  214. if ( !empty( $new_args ) ) { 
  215. $new_args = bbp_parse_args( $new_args, '', 'view_query' ); 
  216. $query_args = array_merge( $query_args, $new_args ); 
  217.  
  218. return bbp_has_topics( $query_args ); 
  219.  
  220. /** 
  221. * Return the view's query arguments 
  222. * 
  223. * @since bbPress (r2789) 
  224. * 
  225. * @param string $view View name 
  226. * @uses bbp_get_view_id() To get the view id 
  227. * @return array Query arguments 
  228. */ 
  229. function bbp_get_view_query_args( $view ) { 
  230. $view = bbp_get_view_id( $view ); 
  231. $retval = !empty( $view ) ? bbpress()->views[$view]['query'] : false; 
  232.  
  233. return apply_filters( 'bbp_get_view_query_args', $retval, $view ); 
  234.  
  235. /** Errors ********************************************************************/ 
  236.  
  237. /** 
  238. * Adds an error message to later be output in the theme 
  239. * 
  240. * @since bbPress (r3381) 
  241. * 
  242. * @see WP_Error() 
  243. * @uses WP_Error::add(); 
  244. * 
  245. * @param string $code Unique code for the error message 
  246. * @param string $message Translated error message 
  247. * @param string $data Any additional data passed with the error message 
  248. */ 
  249. function bbp_add_error( $code = '', $message = '', $data = '' ) { 
  250. bbpress()->errors->add( $code, $message, $data ); 
  251.  
  252. /** 
  253. * Check if error messages exist in queue 
  254. * 
  255. * @since bbPress (r3381) 
  256. * 
  257. * @see WP_Error() 
  258. * 
  259. * @uses is_wp_error() 
  260. * @usese WP_Error::get_error_codes() 
  261. */ 
  262. function bbp_has_errors() { 
  263. $has_errors = bbpress()->errors->get_error_codes() ? true : false; 
  264.  
  265. return apply_filters( 'bbp_has_errors', $has_errors, bbpress()->errors ); 
  266.  
  267. /** Mentions ******************************************************************/ 
  268.  
  269. /** 
  270. * Set the pattern used for matching usernames for mentions. 
  271. * 
  272. * Moved into its own function to allow filtering of the regex pattern 
  273. * anywhere mentions might be used. 
  274. * 
  275. * @since bbPress (r4997) 
  276. * @deprecated 2.6.0 bbp_make_clickable() 
  277. * 
  278. * @return string Pattern to match usernames with 
  279. */ 
  280. function bbp_find_mentions_pattern() { 
  281. return apply_filters( 'bbp_find_mentions_pattern', '/[@]+([A-Za-z0-9-_\.@]+)\b/' ); 
  282.  
  283. /** 
  284. * Searches through the content to locate usernames, designated by an @ sign. 
  285. * 
  286. * @since bbPress (r4323) 
  287. * @deprecated 2.6.0 bbp_make_clickable() 
  288. * 
  289. * @param string $content The content 
  290. * @return bool|array $usernames Existing usernames. False if no matches. 
  291. */ 
  292. function bbp_find_mentions( $content = '' ) { 
  293. $pattern = bbp_find_mentions_pattern(); 
  294. preg_match_all( $pattern, $content, $usernames ); 
  295. $usernames = array_unique( array_filter( $usernames[1] ) ); 
  296.  
  297. // Bail if no usernames 
  298. if ( empty( $usernames ) ) { 
  299. $usernames = false; 
  300.  
  301. return apply_filters( 'bbp_find_mentions', $usernames, $pattern, $content ); 
  302.  
  303. /** 
  304. * Finds and links @-mentioned users in the content 
  305. * 
  306. * @since bbPress (r4323) 
  307. * @deprecated 2.6.0 bbp_make_clickable() 
  308. * 
  309. * @uses bbp_find_mentions() To get usernames in content areas 
  310. * @return string $content Content filtered for mentions 
  311. */ 
  312. function bbp_mention_filter( $content = '' ) { 
  313.  
  314. // Get Usernames and bail if none exist 
  315. $usernames = bbp_find_mentions( $content ); 
  316. if ( empty( $usernames ) ) 
  317. return $content; 
  318.  
  319. // Loop through usernames and link to profiles 
  320. foreach ( (array) $usernames as $username ) { 
  321.  
  322. // Skip if username does not exist or user is not active 
  323. $user = get_user_by( 'slug', $username ); 
  324. if ( empty( $user->ID ) || bbp_is_user_inactive( $user->ID ) ) 
  325. continue; 
  326.  
  327. // Replace name in content 
  328. $content = preg_replace( '/(@' . $username . '\b)/', sprintf( '<a href="%1$s" rel="nofollow">@%2$s</a>', bbp_get_user_profile_url( $user->ID ), $username ), $content ); 
  329.  
  330. // Return modified content 
  331. return $content; 
  332.  
  333. /** Post Statuses *************************************************************/ 
  334.  
  335. /** 
  336. * Return the public post status ID 
  337. * 
  338. * @since bbPress (r3504) 
  339. * 
  340. * @return string 
  341. */ 
  342. function bbp_get_public_status_id() { 
  343. return bbpress()->public_status_id; 
  344.  
  345. /** 
  346. * Return the pending post status ID 
  347. * 
  348. * @since bbPress (r3581) 
  349. * 
  350. * @return string 
  351. */ 
  352. function bbp_get_pending_status_id() { 
  353. return bbpress()->pending_status_id; 
  354.  
  355. /** 
  356. * Return the private post status ID 
  357. * 
  358. * @since bbPress (r3504) 
  359. * 
  360. * @return string 
  361. */ 
  362. function bbp_get_private_status_id() { 
  363. return bbpress()->private_status_id; 
  364.  
  365. /** 
  366. * Return the hidden post status ID 
  367. * 
  368. * @since bbPress (r3504) 
  369. * 
  370. * @return string 
  371. */ 
  372. function bbp_get_hidden_status_id() { 
  373. return bbpress()->hidden_status_id; 
  374.  
  375. /** 
  376. * Return the closed post status ID 
  377. * 
  378. * @since bbPress (r3504) 
  379. * 
  380. * @return string 
  381. */ 
  382. function bbp_get_closed_status_id() { 
  383. return bbpress()->closed_status_id; 
  384.  
  385. /** 
  386. * Return the spam post status ID 
  387. * 
  388. * @since bbPress (r3504) 
  389. * 
  390. * @return string 
  391. */ 
  392. function bbp_get_spam_status_id() { 
  393. return bbpress()->spam_status_id; 
  394.  
  395. /** 
  396. * Return the trash post status ID 
  397. * 
  398. * @since bbPress (r3504) 
  399. * 
  400. * @return string 
  401. */ 
  402. function bbp_get_trash_status_id() { 
  403. return bbpress()->trash_status_id; 
  404.  
  405. /** 
  406. * Return the orphan post status ID 
  407. * 
  408. * @since bbPress (r3504) 
  409. * 
  410. * @return string 
  411. */ 
  412. function bbp_get_orphan_status_id() { 
  413. return bbpress()->orphan_status_id; 
  414.  
  415. /** Rewrite IDs ***************************************************************/ 
  416.  
  417. /** 
  418. * Return the unique ID for user profile rewrite rules 
  419. * 
  420. * @since bbPress (r3762) 
  421. * @return string 
  422. */ 
  423. function bbp_get_user_rewrite_id() { 
  424. return bbpress()->user_id; 
  425.  
  426. /** 
  427. * Return the unique ID for all edit rewrite rules (forum|topic|reply|tag|user) 
  428. * 
  429. * @since bbPress (r3762) 
  430. * @return string 
  431. */ 
  432. function bbp_get_edit_rewrite_id() { 
  433. return bbpress()->edit_id; 
  434.  
  435. /** 
  436. * Return the unique ID for all search rewrite rules 
  437. * 
  438. * @since bbPress (r4579) 
  439. * 
  440. * @return string 
  441. */ 
  442. function bbp_get_search_rewrite_id() { 
  443. return bbpress()->search_id; 
  444.  
  445. /** 
  446. * Return the unique ID for user topics rewrite rules 
  447. * 
  448. * @since bbPress (r4321) 
  449. * @return string 
  450. */ 
  451. function bbp_get_user_topics_rewrite_id() { 
  452. return bbpress()->tops_id; 
  453.  
  454. /** 
  455. * Return the unique ID for user replies rewrite rules 
  456. * 
  457. * @since bbPress (r4321) 
  458. * @return string 
  459. */ 
  460. function bbp_get_user_replies_rewrite_id() { 
  461. return bbpress()->reps_id; 
  462.  
  463. /** 
  464. * Return the unique ID for user caps rewrite rules 
  465. * 
  466. * @since bbPress (r4181) 
  467. * @return string 
  468. */ 
  469. function bbp_get_user_favorites_rewrite_id() { 
  470. return bbpress()->favs_id; 
  471.  
  472. /** 
  473. * Return the unique ID for user caps rewrite rules 
  474. * 
  475. * @since bbPress (r4181) 
  476. * @return string 
  477. */ 
  478. function bbp_get_user_subscriptions_rewrite_id() { 
  479. return bbpress()->subs_id; 
  480.  
  481. /** 
  482. * Return the unique ID for topic view rewrite rules 
  483. * 
  484. * @since bbPress (r3762) 
  485. * @return string 
  486. */ 
  487. function bbp_get_view_rewrite_id() { 
  488. return bbpress()->view_id; 
  489.  
  490. /** Rewrite Extras ************************************************************/ 
  491.  
  492. /** 
  493. * Get the id used for paginated requests 
  494. * 
  495. * @since bbPress (r4926) 
  496. * @return string 
  497. */ 
  498. function bbp_get_paged_rewrite_id() { 
  499. return bbpress()->paged_id; 
  500.  
  501. /** 
  502. * Get the slug used for paginated requests 
  503. * 
  504. * @since bbPress (r4926) 
  505. * @global object $wp_rewrite The WP_Rewrite object 
  506. * @return string 
  507. */ 
  508. function bbp_get_paged_slug() { 
  509. global $wp_rewrite; 
  510. return $wp_rewrite->pagination_base; 
  511.  
  512. /** 
  513. * Delete a blogs rewrite rules, so that they are automatically rebuilt on 
  514. * the subsequent page load. 
  515. * 
  516. * @since bbPress (r4198) 
  517. */ 
  518. function bbp_delete_rewrite_rules() { 
  519. delete_option( 'rewrite_rules' ); 
  520.  
  521. /** Requests ******************************************************************/ 
  522.  
  523. /** 
  524. * Return true|false if this is a POST request 
  525. * 
  526. * @since bbPress (r4790) 
  527. * @return bool 
  528. */ 
  529. function bbp_is_post_request() { 
  530. return (bool) ( 'POST' === strtoupper( $_SERVER['REQUEST_METHOD'] ) ); 
  531.  
  532. /** 
  533. * Return true|false if this is a GET request 
  534. * 
  535. * @since bbPress (r4790) 
  536. * @return bool 
  537. */ 
  538. function bbp_is_get_request() { 
  539. return (bool) ( 'GET' === strtoupper( $_SERVER['REQUEST_METHOD'] ) ); 
.