PageRenderTime 77ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/bbpress/includes/core/functions.php

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