PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/bfay/maniacal-kitten
PHP | 2216 lines | 859 code | 425 blank | 932 comment | 200 complexity | 5a764146bf4a8fa03d348a0d2371c936 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * bbPress Forum Functions
  4. *
  5. * @package bbPress
  6. * @subpackage Functions
  7. */
  8. // Exit if accessed directly
  9. if ( !defined( 'ABSPATH' ) ) exit;
  10. /** Insert ********************************************************************/
  11. /**
  12. * A wrapper for wp_insert_post() that also includes the necessary meta values
  13. * for the forum to function properly.
  14. *
  15. * @since bbPress (r3349)
  16. *
  17. * @uses bbp_parse_args()
  18. * @uses bbp_get_forum_post_type()
  19. * @uses wp_insert_post()
  20. * @uses update_post_meta()
  21. *
  22. * @param array $forum_data Forum post data
  23. * @param arrap $forum_meta Forum meta data
  24. */
  25. function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) {
  26. // Forum
  27. $default_forum = array(
  28. 'post_parent' => 0, // forum ID
  29. 'post_status' => bbp_get_public_status_id(),
  30. 'post_type' => bbp_get_forum_post_type(),
  31. 'post_author' => bbp_get_current_user_id(),
  32. 'post_password' => '',
  33. 'post_content' => '',
  34. 'post_title' => '',
  35. 'menu_order' => 0,
  36. 'comment_status' => 'closed'
  37. );
  38. $forum_data = bbp_parse_args( $forum_data, $default_forum, 'insert_forum' );
  39. // Insert forum
  40. $forum_id = wp_insert_post( $forum_data );
  41. // Bail if no forum was added
  42. if ( empty( $forum_id ) )
  43. return false;
  44. // Forum meta
  45. $default_meta = array(
  46. 'reply_count' => 0,
  47. 'topic_count' => 0,
  48. 'topic_count_hidden' => 0,
  49. 'total_reply_count' => 0,
  50. 'total_topic_count' => 0,
  51. 'last_topic_id' => 0,
  52. 'last_reply_id' => 0,
  53. 'last_active_id' => 0,
  54. 'last_active_time' => 0,
  55. 'forum_subforum_count' => 0,
  56. );
  57. $forum_meta = bbp_parse_args( $forum_meta, $default_meta, 'insert_forum_meta' );
  58. // Insert forum meta
  59. foreach ( $forum_meta as $meta_key => $meta_value )
  60. update_post_meta( $forum_id, '_bbp_' . $meta_key, $meta_value );
  61. // Return new forum ID
  62. return $forum_id;
  63. }
  64. /** Post Form Handlers ********************************************************/
  65. /**
  66. * Handles the front end forum submission
  67. *
  68. * @param string $action The requested action to compare this function to
  69. * @uses bbp_add_error() To add an error message
  70. * @uses bbp_verify_nonce_request() To verify the nonce and check the request
  71. * @uses bbp_is_anonymous() To check if an anonymous post is being made
  72. * @uses current_user_can() To check if the current user can publish forum
  73. * @uses bbp_get_current_user_id() To get the current user id
  74. * @uses bbp_filter_anonymous_post_data() To filter anonymous data
  75. * @uses bbp_set_current_anonymous_user_data() To set the anonymous user cookies
  76. * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
  77. * @uses esc_attr() For sanitization
  78. * @uses bbp_is_forum_category() To check if the forum is a category
  79. * @uses bbp_is_forum_closed() To check if the forum is closed
  80. * @uses bbp_is_forum_private() To check if the forum is private
  81. * @uses bbp_check_for_flood() To check for flooding
  82. * @uses bbp_check_for_duplicate() To check for duplicates
  83. * @uses bbp_get_forum_post_type() To get the forum post type
  84. * @uses remove_filter() To remove kses filters if needed
  85. * @uses apply_filters() Calls 'bbp_new_forum_pre_title' with the content
  86. * @uses apply_filters() Calls 'bbp_new_forum_pre_content' with the content
  87. * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
  88. * @uses wp_insert_post() To insert the forum
  89. * @uses do_action() Calls 'bbp_new_forum' with the forum id, forum id,
  90. * anonymous data and reply author
  91. * @uses bbp_stick_forum() To stick or super stick the forum
  92. * @uses bbp_unstick_forum() To unstick the forum
  93. * @uses bbp_get_forum_permalink() To get the forum permalink
  94. * @uses wp_safe_redirect() To redirect to the forum link
  95. * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
  96. * messages
  97. */
  98. function bbp_new_forum_handler( $action = '' ) {
  99. // Bail if action is not bbp-new-forum
  100. if ( 'bbp-new-forum' !== $action )
  101. return;
  102. // Nonce check
  103. if ( ! bbp_verify_nonce_request( 'bbp-new-forum' ) ) {
  104. bbp_add_error( 'bbp_new_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
  105. return;
  106. }
  107. // Define local variable(s)
  108. $view_all = $anonymous_data = false;
  109. $forum_parent_id = $forum_author = 0;
  110. $forum_title = $forum_content = '';
  111. /** Forum Author **********************************************************/
  112. // User cannot create forums
  113. if ( !current_user_can( 'publish_forums' ) ) {
  114. bbp_add_error( 'bbp_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new forums.', 'bbpress' ) );
  115. return;
  116. }
  117. // Forum author is current user
  118. $forum_author = bbp_get_current_user_id();
  119. // Remove kses filters from title and content for capable users and if the nonce is verified
  120. if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_forum'] ) && wp_create_nonce( 'bbp-unfiltered-html-forum_new' ) == $_POST['_bbp_unfiltered_html_forum'] ) {
  121. remove_filter( 'bbp_new_forum_pre_title', 'wp_filter_kses' );
  122. remove_filter( 'bbp_new_forum_pre_content', 'bbp_encode_bad', 10 );
  123. remove_filter( 'bbp_new_forum_pre_content', 'bbp_filter_kses', 30 );
  124. }
  125. /** Forum Title ***********************************************************/
  126. if ( !empty( $_POST['bbp_forum_title'] ) )
  127. $forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) );
  128. // Filter and sanitize
  129. $forum_title = apply_filters( 'bbp_new_forum_pre_title', $forum_title );
  130. // No forum title
  131. if ( empty( $forum_title ) )
  132. bbp_add_error( 'bbp_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) );
  133. /** Forum Content *********************************************************/
  134. if ( !empty( $_POST['bbp_forum_content'] ) )
  135. $forum_content = $_POST['bbp_forum_content'];
  136. // Filter and sanitize
  137. $forum_content = apply_filters( 'bbp_new_forum_pre_content', $forum_content );
  138. // No forum content
  139. if ( empty( $forum_content ) )
  140. bbp_add_error( 'bbp_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) );
  141. /** Forum Parent **********************************************************/
  142. // Forum parent was passed (the norm)
  143. if ( !empty( $_POST['bbp_forum_parent_id'] ) )
  144. $forum_parent_id = (int) $_POST['bbp_forum_parent_id'];
  145. // Filter and sanitize
  146. $forum_parent_id = apply_filters( 'bbp_new_forum_pre_parent_id', $forum_parent_id );
  147. // No forum parent was passed (should never happen)
  148. if ( empty( $forum_parent_id ) ) {
  149. bbp_add_error( 'bbp_new_forum_missing_parent', __( '<strong>ERROR</strong>: Your forum must have a parent.', 'bbpress' ) );
  150. // Forum exists
  151. } elseif ( !empty( $forum_parent_id ) ) {
  152. // Forum is a category
  153. if ( bbp_is_forum_category( $forum_parent_id ) ) {
  154. bbp_add_error( 'bbp_new_forum_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No forums can be created in this forum.', 'bbpress' ) );
  155. }
  156. // Forum is closed and user cannot access
  157. if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) {
  158. bbp_add_error( 'bbp_new_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) );
  159. }
  160. // Forum is private and user cannot access
  161. if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) {
  162. bbp_add_error( 'bbp_new_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
  163. }
  164. // Forum is hidden and user cannot access
  165. if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) {
  166. bbp_add_error( 'bbp_new_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
  167. }
  168. }
  169. /** Forum Flooding ********************************************************/
  170. if ( !bbp_check_for_flood( $anonymous_data, $forum_author ) )
  171. bbp_add_error( 'bbp_forum_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
  172. /** Forum Duplicate *******************************************************/
  173. if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_forum_post_type(), 'post_author' => $forum_author, 'post_content' => $forum_content, 'anonymous_data' => $anonymous_data ) ) )
  174. bbp_add_error( 'bbp_forum_duplicate', __( '<strong>ERROR</strong>: This forum already exists.', 'bbpress' ) );
  175. /** Forum Blacklist *******************************************************/
  176. if ( !bbp_check_for_blacklist( $anonymous_data, $forum_author, $forum_title, $forum_content ) )
  177. bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be created at this time.', 'bbpress' ) );
  178. /** Forum Moderation ******************************************************/
  179. $post_status = bbp_get_public_status_id();
  180. if ( !bbp_check_for_moderation( $anonymous_data, $forum_author, $forum_title, $forum_content ) )
  181. $post_status = bbp_get_pending_status_id();
  182. /** Additional Actions (Before Save) **************************************/
  183. do_action( 'bbp_new_forum_pre_extras', $forum_parent_id );
  184. // Bail if errors
  185. if ( bbp_has_errors() )
  186. return;
  187. /** No Errors *************************************************************/
  188. // Add the content of the form to $forum_data as an array
  189. // Just in time manipulation of forum data before being created
  190. $forum_data = apply_filters( 'bbp_new_forum_pre_insert', array(
  191. 'post_author' => $forum_author,
  192. 'post_title' => $forum_title,
  193. 'post_content' => $forum_content,
  194. 'post_parent' => $forum_parent_id,
  195. 'post_status' => $post_status,
  196. 'post_type' => bbp_get_forum_post_type(),
  197. 'comment_status' => 'closed'
  198. ) );
  199. // Insert forum
  200. $forum_id = wp_insert_post( $forum_data );
  201. /** No Errors *************************************************************/
  202. if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) {
  203. /** Trash Check *******************************************************/
  204. // If the forum is trash, or the forum_status is switched to
  205. // trash, trash it properly
  206. if ( ( get_post_field( 'post_status', $forum_id ) == bbp_get_trash_status_id() ) || ( $forum_data['post_status'] == bbp_get_trash_status_id() ) ) {
  207. // Trash the reply
  208. wp_trash_post( $forum_id );
  209. // Force view=all
  210. $view_all = true;
  211. }
  212. /** Spam Check ********************************************************/
  213. // If reply or forum are spam, officially spam this reply
  214. if ( $forum_data['post_status'] == bbp_get_spam_status_id() ) {
  215. add_post_meta( $forum_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
  216. // Force view=all
  217. $view_all = true;
  218. }
  219. /** Update counts, etc... *********************************************/
  220. $forum_args = array(
  221. 'forum_id' => $forum_id,
  222. 'post_parent' => $forum_parent_id,
  223. 'forum_author' => $forum_author,
  224. 'last_topic_id' => 0,
  225. 'last_reply_id' => 0,
  226. 'last_active_id' => 0,
  227. 'last_active_time' => 0,
  228. 'last_active_status' => bbp_get_public_status_id()
  229. );
  230. do_action( 'bbp_new_forum', $forum_args );
  231. /** Additional Actions (After Save) ***********************************/
  232. do_action( 'bbp_new_forum_post_extras', $forum_id );
  233. /** Redirect **********************************************************/
  234. // Redirect to
  235. $redirect_to = bbp_get_redirect_to();
  236. // Get the forum URL
  237. $redirect_url = bbp_get_forum_permalink( $forum_id, $redirect_to );
  238. // Add view all?
  239. if ( bbp_get_view_all() || !empty( $view_all ) ) {
  240. // User can moderate, so redirect to forum with view all set
  241. if ( current_user_can( 'moderate' ) ) {
  242. $redirect_url = bbp_add_view_all( $redirect_url );
  243. // User cannot moderate, so redirect to forum
  244. } else {
  245. $redirect_url = bbp_get_forum_permalink( $forum_id );
  246. }
  247. }
  248. // Allow to be filtered
  249. $redirect_url = apply_filters( 'bbp_new_forum_redirect_to', $redirect_url, $redirect_to );
  250. /** Successful Save ***************************************************/
  251. // Redirect back to new forum
  252. wp_safe_redirect( $redirect_url );
  253. // For good measure
  254. exit();
  255. // Errors
  256. } else {
  257. $append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : '';
  258. bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error, 'bbpress' ) );
  259. }
  260. }
  261. /**
  262. * Handles the front end edit forum submission
  263. *
  264. * @param string $action The requested action to compare this function to
  265. * @uses bbPress:errors::add() To log various error messages
  266. * @uses bbp_get_forum() To get the forum
  267. * @uses bbp_verify_nonce_request() To verify the nonce and check the request
  268. * @uses bbp_is_forum_anonymous() To check if forum is by an anonymous user
  269. * @uses current_user_can() To check if the current user can edit the forum
  270. * @uses bbp_filter_anonymous_post_data() To filter anonymous data
  271. * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
  272. * @uses esc_attr() For sanitization
  273. * @uses bbp_is_forum_category() To check if the forum is a category
  274. * @uses bbp_is_forum_closed() To check if the forum is closed
  275. * @uses bbp_is_forum_private() To check if the forum is private
  276. * @uses remove_filter() To remove kses filters if needed
  277. * @uses apply_filters() Calls 'bbp_edit_forum_pre_title' with the title and
  278. * forum id
  279. * @uses apply_filters() Calls 'bbp_edit_forum_pre_content' with the content
  280. * and forum id
  281. * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
  282. * @uses wp_save_post_revision() To save a forum revision
  283. * @uses bbp_update_forum_revision_log() To update the forum revision log
  284. * @uses wp_update_post() To update the forum
  285. * @uses do_action() Calls 'bbp_edit_forum' with the forum id, forum id,
  286. * anonymous data and reply author
  287. * @uses bbp_move_forum_handler() To handle movement of a forum from one forum
  288. * to another
  289. * @uses bbp_get_forum_permalink() To get the forum permalink
  290. * @uses wp_safe_redirect() To redirect to the forum link
  291. * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
  292. * messages
  293. */
  294. function bbp_edit_forum_handler( $action = '' ) {
  295. // Bail if action is not bbp-edit-forum
  296. if ( 'bbp-edit-forum' !== $action )
  297. return;
  298. // Define local variable(s)
  299. $anonymous_data = array();
  300. $forum = $forum_id = $forum_parent_id = 0;
  301. $forum_title = $forum_content = $forum_edit_reason = '';
  302. /** Forum *****************************************************************/
  303. // Forum id was not passed
  304. if ( empty( $_POST['bbp_forum_id'] ) ) {
  305. bbp_add_error( 'bbp_edit_forum_id', __( '<strong>ERROR</strong>: Forum ID not found.', 'bbpress' ) );
  306. return;
  307. // Forum id was passed
  308. } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) {
  309. $forum_id = (int) $_POST['bbp_forum_id'];
  310. $forum = bbp_get_forum( $forum_id );
  311. }
  312. // Nonce check
  313. if ( ! bbp_verify_nonce_request( 'bbp-edit-forum_' . $forum_id ) ) {
  314. bbp_add_error( 'bbp_edit_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
  315. return;
  316. // Forum does not exist
  317. } elseif ( empty( $forum ) ) {
  318. bbp_add_error( 'bbp_edit_forum_not_found', __( '<strong>ERROR</strong>: The forum you want to edit was not found.', 'bbpress' ) );
  319. return;
  320. // User cannot edit this forum
  321. } elseif ( !current_user_can( 'edit_forum', $forum_id ) ) {
  322. bbp_add_error( 'bbp_edit_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that forum.', 'bbpress' ) );
  323. return;
  324. }
  325. // Remove kses filters from title and content for capable users and if the nonce is verified
  326. if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_forum'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-forum_' . $forum_id ) == $_POST['_bbp_unfiltered_html_forum'] ) ) {
  327. remove_filter( 'bbp_edit_forum_pre_title', 'wp_filter_kses' );
  328. remove_filter( 'bbp_edit_forum_pre_content', 'bbp_encode_bad', 10 );
  329. remove_filter( 'bbp_edit_forum_pre_content', 'bbp_filter_kses', 30 );
  330. }
  331. /** Forum Parent ***********************************************************/
  332. // Forum parent id was passed
  333. if ( is_numeric( $_POST['bbp_forum_parent_id'] ) ) {
  334. $forum_parent_id = (int) $_POST['bbp_forum_parent_id'];
  335. }
  336. // Current forum this forum is in
  337. $current_parent_forum_id = bbp_get_forum_parent_id( $forum_id );
  338. // Forum exists
  339. if ( !empty( $forum_parent_id ) && ( $forum_parent_id !== $current_parent_forum_id ) ) {
  340. // Forum is closed and user cannot access
  341. if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) {
  342. bbp_add_error( 'bbp_edit_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) );
  343. }
  344. // Forum is private and user cannot access
  345. if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) {
  346. bbp_add_error( 'bbp_edit_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
  347. }
  348. // Forum is hidden and user cannot access
  349. if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) {
  350. bbp_add_error( 'bbp_edit_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
  351. }
  352. }
  353. /** Forum Title ***********************************************************/
  354. if ( !empty( $_POST['bbp_forum_title'] ) )
  355. $forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) );
  356. // Filter and sanitize
  357. $forum_title = apply_filters( 'bbp_edit_forum_pre_title', $forum_title, $forum_id );
  358. // No forum title
  359. if ( empty( $forum_title ) )
  360. bbp_add_error( 'bbp_edit_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) );
  361. /** Forum Content *********************************************************/
  362. if ( !empty( $_POST['bbp_forum_content'] ) )
  363. $forum_content = $_POST['bbp_forum_content'];
  364. // Filter and sanitize
  365. $forum_content = apply_filters( 'bbp_edit_forum_pre_content', $forum_content, $forum_id );
  366. // No forum content
  367. if ( empty( $forum_content ) )
  368. bbp_add_error( 'bbp_edit_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) );
  369. /** Forum Blacklist *******************************************************/
  370. if ( !bbp_check_for_blacklist( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) )
  371. bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be edited at this time.', 'bbpress' ) );
  372. /** Forum Moderation ******************************************************/
  373. $post_status = bbp_get_public_status_id();
  374. if ( !bbp_check_for_moderation( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) )
  375. $post_status = bbp_get_pending_status_id();
  376. /** Additional Actions (Before Save) **************************************/
  377. do_action( 'bbp_edit_forum_pre_extras', $forum_id );
  378. // Bail if errors
  379. if ( bbp_has_errors() )
  380. return;
  381. /** No Errors *************************************************************/
  382. // Add the content of the form to $forum_data as an array
  383. // Just in time manipulation of forum data before being edited
  384. $forum_data = apply_filters( 'bbp_edit_forum_pre_insert', array(
  385. 'ID' => $forum_id,
  386. 'post_title' => $forum_title,
  387. 'post_content' => $forum_content,
  388. 'post_status' => $post_status,
  389. 'post_parent' => $forum_parent_id
  390. ) );
  391. // Insert forum
  392. $forum_id = wp_update_post( $forum_data );
  393. /** Revisions *************************************************************/
  394. /**
  395. * @todo omitted for 2.1
  396. // Revision Reason
  397. if ( !empty( $_POST['bbp_forum_edit_reason'] ) )
  398. $forum_edit_reason = esc_attr( strip_tags( $_POST['bbp_forum_edit_reason'] ) );
  399. // Update revision log
  400. if ( !empty( $_POST['bbp_log_forum_edit'] ) && ( 1 == $_POST['bbp_log_forum_edit'] ) && ( $revision_id = wp_save_post_revision( $forum_id ) ) ) {
  401. bbp_update_forum_revision_log( array(
  402. 'forum_id' => $forum_id,
  403. 'revision_id' => $revision_id,
  404. 'author_id' => bbp_get_current_user_id(),
  405. 'reason' => $forum_edit_reason
  406. ) );
  407. }
  408. */
  409. /** No Errors *************************************************************/
  410. if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) {
  411. // Update counts, etc...
  412. $forum_args = array(
  413. 'forum_id' => $forum_id,
  414. 'post_parent' => $forum_parent_id,
  415. 'forum_author' => $forum->post_author,
  416. 'last_topic_id' => 0,
  417. 'last_reply_id' => 0,
  418. 'last_active_id' => 0,
  419. 'last_active_time' => 0,
  420. 'last_active_status' => bbp_get_public_status_id()
  421. );
  422. do_action( 'bbp_edit_forum', $forum_args );
  423. // If the new forum parent id is not equal to the old forum parent
  424. // id, run the bbp_move_forum action and pass the forum's parent id
  425. // as the first arg and new forum parent id as the second.
  426. // @todo implement
  427. //if ( $forum_id != $forum->post_parent )
  428. // bbp_move_forum_handler( $forum_parent_id, $forum->post_parent, $forum_id );
  429. /** Additional Actions (After Save) ***********************************/
  430. do_action( 'bbp_edit_forum_post_extras', $forum_id );
  431. /** Redirect **********************************************************/
  432. // Redirect to
  433. $redirect_to = bbp_get_redirect_to();
  434. // View all?
  435. $view_all = bbp_get_view_all();
  436. // Get the forum URL
  437. $forum_url = bbp_get_forum_permalink( $forum_id, $redirect_to );
  438. // Add view all?
  439. if ( !empty( $view_all ) )
  440. $forum_url = bbp_add_view_all( $forum_url );
  441. // Allow to be filtered
  442. $forum_url = apply_filters( 'bbp_edit_forum_redirect_to', $forum_url, $view_all, $redirect_to );
  443. /** Successful Edit ***************************************************/
  444. // Redirect back to new forum
  445. wp_safe_redirect( $forum_url );
  446. // For good measure
  447. exit();
  448. /** Errors ****************************************************************/
  449. } else {
  450. $append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : '';
  451. bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error . 'Please try again.', 'bbpress' ) );
  452. }
  453. }
  454. /**
  455. * Handle the saving of core forum metadata (Status, Visibility, and Type)
  456. *
  457. * @since bbPress (r3678)
  458. * @param int $forum_id
  459. * @uses bbp_is_forum_closed() To check if forum is closed
  460. * @uses bbp_close_forum() To close forum
  461. * @uses bbp_open_forum() To open forum
  462. * @uses bbp_is_forum_category() To check if forum is a category
  463. * @uses bbp_categorize_forum() To turn forum into a category
  464. * @uses bbp_normalize_forum() To turn category into forum
  465. * @uses bbp_get_public_status_id() To get the public status ID
  466. * @uses bbp_get_private_status_id() To get the private status ID
  467. * @uses bbp_get_hidden_status_id() To get the hidden status ID
  468. * @uses bbp_get_forum_visibility() To get the forums visibility
  469. * @uses bbp_hide_forum() To hide a forum
  470. * @uses bbp_privatize_forum() To make a forum private
  471. * @uses bbp_publicize_forum() To make a forum public
  472. * @return If forum ID is empty
  473. */
  474. function bbp_save_forum_extras( $forum_id = 0 ) {
  475. // Validate the forum ID
  476. $forum_id = bbp_get_forum_id( $forum_id );
  477. // Bail if forum ID is empty
  478. if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) )
  479. return;
  480. /** Forum Status ******************************************************/
  481. if ( !empty( $_POST['bbp_forum_status'] ) && in_array( $_POST['bbp_forum_status'], array( 'open', 'closed' ) ) ) {
  482. if ( 'closed' == $_POST['bbp_forum_status'] && !bbp_is_forum_closed( $forum_id, false ) ) {
  483. bbp_close_forum( $forum_id );
  484. } elseif ( 'open' == $_POST['bbp_forum_status'] && bbp_is_forum_closed( $forum_id, false ) ) {
  485. bbp_open_forum( $forum_id );
  486. }
  487. }
  488. /** Forum Type ********************************************************/
  489. if ( !empty( $_POST['bbp_forum_type'] ) && in_array( $_POST['bbp_forum_type'], array( 'forum', 'category' ) ) ) {
  490. if ( 'category' == $_POST['bbp_forum_type'] && !bbp_is_forum_category( $forum_id ) ) {
  491. bbp_categorize_forum( $forum_id );
  492. } elseif ( 'forum' == $_POST['bbp_forum_type'] && bbp_is_forum_category( $forum_id ) ) {
  493. bbp_normalize_forum( $forum_id );
  494. }
  495. }
  496. /** Forum Visibility **************************************************/
  497. if ( !empty( $_POST['bbp_forum_visibility'] ) && in_array( $_POST['bbp_forum_visibility'], array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id() ) ) ) {
  498. // Get forums current visibility
  499. $visibility = bbp_get_forum_visibility( $forum_id );
  500. // What is the new forum visibility setting?
  501. switch ( $_POST['bbp_forum_visibility'] ) {
  502. // Hidden
  503. case bbp_get_hidden_status_id() :
  504. bbp_hide_forum( $forum_id, $visibility );
  505. break;
  506. // Private
  507. case bbp_get_private_status_id() :
  508. bbp_privatize_forum( $forum_id, $visibility );
  509. break;
  510. // Publish (default)
  511. case bbp_get_public_status_id() :
  512. default :
  513. bbp_publicize_forum( $forum_id, $visibility );
  514. break;
  515. }
  516. }
  517. }
  518. /** Forum Actions *************************************************************/
  519. /**
  520. * Closes a forum
  521. *
  522. * @since bbPress (r2746)
  523. *
  524. * @param int $forum_id forum id
  525. * @uses do_action() Calls 'bbp_close_forum' with the forum id
  526. * @uses update_post_meta() To add the previous status to a meta
  527. * @uses do_action() Calls 'bbp_opened_forum' with the forum id
  528. * @return mixed False or {@link WP_Error} on failure, forum id on success
  529. */
  530. function bbp_close_forum( $forum_id = 0 ) {
  531. $forum_id = bbp_get_forum_id( $forum_id );
  532. do_action( 'bbp_close_forum', $forum_id );
  533. update_post_meta( $forum_id, '_bbp_status', 'closed' );
  534. do_action( 'bbp_closed_forum', $forum_id );
  535. return $forum_id;
  536. }
  537. /**
  538. * Opens a forum
  539. *
  540. * @since bbPress (r2746)
  541. *
  542. * @param int $forum_id forum id
  543. * @uses do_action() Calls 'bbp_open_forum' with the forum id
  544. * @uses get_post_meta() To get the previous status
  545. * @uses update_post_meta() To delete the previous status meta
  546. * @uses do_action() Calls 'bbp_opened_forum' with the forum id
  547. * @return mixed False or {@link WP_Error} on failure, forum id on success
  548. */
  549. function bbp_open_forum( $forum_id = 0 ) {
  550. $forum_id = bbp_get_forum_id( $forum_id );
  551. do_action( 'bbp_open_forum', $forum_id );
  552. update_post_meta( $forum_id, '_bbp_status', 'open' );
  553. do_action( 'bbp_opened_forum', $forum_id );
  554. return $forum_id;
  555. }
  556. /**
  557. * Make the forum a category
  558. *
  559. * @since bbPress (r2746)
  560. *
  561. * @param int $forum_id Optional. Forum id
  562. * @uses update_post_meta() To update the forum category meta
  563. * @return bool False on failure, true on success
  564. */
  565. function bbp_categorize_forum( $forum_id = 0 ) {
  566. $forum_id = bbp_get_forum_id( $forum_id );
  567. do_action( 'bbp_categorize_forum', $forum_id );
  568. update_post_meta( $forum_id, '_bbp_forum_type', 'category' );
  569. do_action( 'bbp_categorized_forum', $forum_id );
  570. return $forum_id;
  571. }
  572. /**
  573. * Remove the category status from a forum
  574. *
  575. * @since bbPress (r2746)
  576. *
  577. * @param int $forum_id Optional. Forum id
  578. * @uses delete_post_meta() To delete the forum category meta
  579. * @return bool False on failure, true on success
  580. */
  581. function bbp_normalize_forum( $forum_id = 0 ) {
  582. $forum_id = bbp_get_forum_id( $forum_id );
  583. do_action( 'bbp_normalize_forum', $forum_id );
  584. update_post_meta( $forum_id, '_bbp_forum_type', 'forum' );
  585. do_action( 'bbp_normalized_forum', $forum_id );
  586. return $forum_id;
  587. }
  588. /**
  589. * Mark the forum as public
  590. *
  591. * @since bbPress (r2746)
  592. *
  593. * @param int $forum_id Optional. Forum id
  594. * @uses update_post_meta() To update the forum private meta
  595. * @return bool False on failure, true on success
  596. */
  597. function bbp_publicize_forum( $forum_id = 0, $current_visibility = '' ) {
  598. $forum_id = bbp_get_forum_id( $forum_id );
  599. do_action( 'bbp_publicize_forum', $forum_id );
  600. // Get private forums
  601. $private = bbp_get_private_forum_ids();
  602. // Find this forum in the array
  603. if ( in_array( $forum_id, $private ) ) {
  604. $offset = array_search( $forum_id, $private );
  605. // Splice around it
  606. array_splice( $private, $offset, 1 );
  607. // Update private forums minus this one
  608. update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) );
  609. }
  610. // Get hidden forums
  611. $hidden = bbp_get_hidden_forum_ids();
  612. // Find this forum in the array
  613. if ( in_array( $forum_id, $hidden ) ) {
  614. $offset = array_search( $forum_id, $hidden );
  615. // Splice around it
  616. array_splice( $hidden, $offset, 1 );
  617. // Update hidden forums minus this one
  618. update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) );
  619. }
  620. // Only run queries if visibility is changing
  621. if ( bbp_get_public_status_id() != $current_visibility ) {
  622. // Update forum post_status
  623. global $wpdb;
  624. $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_public_status_id() ), array( 'ID' => $forum_id ) );
  625. wp_transition_post_status( bbp_get_public_status_id(), $current_visibility, get_post( $forum_id ) );
  626. }
  627. do_action( 'bbp_publicized_forum', $forum_id );
  628. return $forum_id;
  629. }
  630. /**
  631. * Mark the forum as private
  632. *
  633. * @since bbPress (r2746)
  634. *
  635. * @param int $forum_id Optional. Forum id
  636. * @uses update_post_meta() To update the forum private meta
  637. * @return bool False on failure, true on success
  638. */
  639. function bbp_privatize_forum( $forum_id = 0, $current_visibility = '' ) {
  640. $forum_id = bbp_get_forum_id( $forum_id );
  641. do_action( 'bbp_privatize_forum', $forum_id );
  642. // Only run queries if visibility is changing
  643. if ( bbp_get_private_status_id() != $current_visibility ) {
  644. // Get hidden forums
  645. $hidden = bbp_get_hidden_forum_ids();
  646. // Find this forum in the array
  647. if ( in_array( $forum_id, $hidden ) ) {
  648. $offset = array_search( $forum_id, $hidden );
  649. // Splice around it
  650. array_splice( $hidden, $offset, 1 );
  651. // Update hidden forums minus this one
  652. update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) );
  653. }
  654. // Add to '_bbp_private_forums' site option
  655. $private = bbp_get_private_forum_ids();
  656. $private[] = $forum_id;
  657. update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) );
  658. // Update forums visibility setting
  659. global $wpdb;
  660. $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_private_status_id() ), array( 'ID' => $forum_id ) );
  661. wp_transition_post_status( bbp_get_private_status_id(), $current_visibility, get_post( $forum_id ) );
  662. }
  663. do_action( 'bbp_privatized_forum', $forum_id );
  664. return $forum_id;
  665. }
  666. /**
  667. * Mark the forum as hidden
  668. *
  669. * @since bbPress (r2996)
  670. *
  671. * @param int $forum_id Optional. Forum id
  672. * @uses update_post_meta() To update the forum private meta
  673. * @return bool False on failure, true on success
  674. */
  675. function bbp_hide_forum( $forum_id = 0, $current_visibility = '' ) {
  676. $forum_id = bbp_get_forum_id( $forum_id );
  677. do_action( 'bbp_hide_forum', $forum_id );
  678. // Only run queries if visibility is changing
  679. if ( bbp_get_hidden_status_id() != $current_visibility ) {
  680. // Get private forums
  681. $private = bbp_get_private_forum_ids();
  682. // Find this forum in the array
  683. if ( in_array( $forum_id, $private ) ) {
  684. $offset = array_search( $forum_id, $private );
  685. // Splice around it
  686. array_splice( $private, $offset, 1 );
  687. // Update private forums minus this one
  688. update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) );
  689. }
  690. // Add to '_bbp_hidden_forums' site option
  691. $hidden = bbp_get_hidden_forum_ids();
  692. $hidden[] = $forum_id;
  693. update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) );
  694. // Update forums visibility setting
  695. global $wpdb;
  696. $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_hidden_status_id() ), array( 'ID' => $forum_id ) );
  697. wp_transition_post_status( bbp_get_hidden_status_id(), $current_visibility, get_post( $forum_id ) );
  698. }
  699. do_action( 'bbp_hid_forum', $forum_id );
  700. return $forum_id;
  701. }
  702. /** Count Bumpers *************************************************************/
  703. /**
  704. * Bump the total topic count of a forum
  705. *
  706. * @since bbPress (r3825)
  707. *
  708. * @param int $forum_id Optional. Forum id.
  709. * @param int $difference Optional. Default 1
  710. * @param bool $update_ancestors Optional. Default true
  711. * @uses bbp_get_forum_id() To get the forum id
  712. * @uses update_post_meta() To update the forum's topic count meta
  713. * @uses apply_filters() Calls 'bbp_bump_forum_topic_count' with the topic
  714. * count, forum id, and difference
  715. * @return int Forum topic count
  716. */
  717. function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) {
  718. // Get some counts
  719. $forum_id = bbp_get_forum_id( $forum_id );
  720. $topic_count = bbp_get_forum_topic_count( $forum_id, false, true );
  721. $total_topic_count = bbp_get_forum_topic_count( $forum_id, true, true );
  722. // Update this forum id
  723. update_post_meta( $forum_id, '_bbp_topic_count', (int) $topic_count + (int) $difference );
  724. update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topic_count + (int) $difference );
  725. // Check for ancestors
  726. if ( true === $update_ancestors ) {
  727. // Get post ancestors
  728. $forum = get_post( $forum_id );
  729. $ancestors = get_post_ancestors( $forum );
  730. // If has ancestors, loop through them...
  731. if ( !empty( $ancestors ) ) {
  732. foreach ( (array) $ancestors as $parent_forum_id ) {
  733. // Get forum counts
  734. $parent_topic_count = bbp_get_forum_topic_count( $parent_forum_id, false, true );
  735. $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true, true );
  736. // Update counts
  737. update_post_meta( $parent_forum_id, '_bbp_topic_count', (int) $parent_topic_count + (int) $difference );
  738. update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) $parent_total_topic_count + (int) $difference );
  739. }
  740. }
  741. }
  742. return (int) apply_filters( 'bbp_bump_forum_topic_count', (int) $total_topic_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors );
  743. }
  744. /**
  745. * Bump the total hidden topic count of a forum
  746. *
  747. * @since bbPress (r3825)
  748. *
  749. * @param int $forum_id Optional. Forum id.
  750. * @param int $difference Optional. Default 1
  751. * @uses bbp_get_forum_id() To get the forum id
  752. * @uses update_post_meta() To update the forum's topic count meta
  753. * @uses apply_filters() Calls 'bbp_bump_forum_topic_count_hidden' with the
  754. * topic count, forum id, and difference
  755. * @return int Forum hidden topic count
  756. */
  757. function bbp_bump_forum_topic_count_hidden( $forum_id = 0, $difference = 1 ) {
  758. // Get some counts
  759. $forum_id = bbp_get_forum_id( $forum_id );
  760. $topic_count = bbp_get_forum_topic_count_hidden( $forum_id, true );
  761. $new_count = (int) $topic_count + (int) $difference;
  762. // Update this forum id
  763. update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $new_count );
  764. return (int) apply_filters( 'bbp_bump_forum_topic_count_hidden', (int) $new_count, $forum_id, (int) $difference );
  765. }
  766. /**
  767. * Bump the total topic count of a forum
  768. *
  769. * @since bbPress (r3825)
  770. *
  771. * @param int $forum_id Optional. Forum id.
  772. * @param int $difference Optional. Default 1
  773. * @param bool $update_ancestors Optional. Default true
  774. * @uses bbp_get_forum_id() To get the forum id
  775. * @uses update_post_meta() To update the forum's topic count meta
  776. * @uses apply_filters() Calls 'bbp_bump_forum_reply_count' with the topic
  777. * count, forum id, and difference
  778. * @return int Forum topic count
  779. */
  780. function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) {
  781. // Get some counts
  782. $forum_id = bbp_get_forum_id( $forum_id );
  783. $topic_count = bbp_get_forum_reply_count( $forum_id, false, true );
  784. $total_reply_count = bbp_get_forum_reply_count( $forum_id, true, true );
  785. // Update this forum id
  786. update_post_meta( $forum_id, '_bbp_reply_count', (int) $topic_count + (int) $difference );
  787. update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_reply_count + (int) $difference );
  788. // Check for ancestors
  789. if ( true === $update_ancestors ) {
  790. // Get post ancestors
  791. $forum = get_post( $forum_id );
  792. $ancestors = get_post_ancestors( $forum );
  793. // If has ancestors, loop through them...
  794. if ( !empty( $ancestors ) ) {
  795. foreach ( (array) $ancestors as $parent_forum_id ) {
  796. // Get forum counts
  797. $parent_topic_count = bbp_get_forum_reply_count( $parent_forum_id, false, true );
  798. $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true, true );
  799. // Update counts
  800. update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) $parent_topic_count + (int) $difference );
  801. update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) $parent_total_reply_count + (int) $difference );
  802. }
  803. }
  804. }
  805. return (int) apply_filters( 'bbp_bump_forum_reply_count', (int) $total_reply_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors );
  806. }
  807. /** Forum Updaters ************************************************************/
  808. /**
  809. * Update the forum last topic id
  810. *
  811. * @since bbPress (r2625)
  812. *
  813. * @param int $forum_id Optional. Forum id
  814. * @param int $topic_id Optional. Topic id
  815. * @uses bbp_get_forum_id() To get the forum id
  816. * @uses bbp_forum_query_subforum_ids() To get the subforum ids
  817. * @uses bbp_update_forum_last_topic_id() To update the last topic id of child
  818. * forums
  819. * @uses get_posts() To get the most recent topic in the forum
  820. * @uses update_post_meta() To update the forum's last active id meta
  821. * @uses apply_filters() Calls 'bbp_update_forum_last_topic_id' with the last
  822. * reply id and forum id
  823. * @return bool True on success, false on failure
  824. */
  825. function bbp_update_forum_last_topic_id( $forum_id = 0, $topic_id = 0 ) {
  826. $forum_id = bbp_get_forum_id( $forum_id );
  827. // Define local variable(s)
  828. $children_last_topic = 0;
  829. // Do some calculation if not manually set
  830. if ( empty( $topic_id ) ) {
  831. // Loop through children and add together forum reply counts
  832. $children = bbp_forum_query_subforum_ids( $forum_id );
  833. if ( !empty( $children ) ) {
  834. foreach ( (array) $children as $child ) {
  835. $children_last_topic = bbp_update_forum_last_topic_id( $child ); // Recursive
  836. }
  837. }
  838. // Setup recent topic query vars
  839. $post_vars = array(
  840. 'post_parent' => $forum_id,
  841. 'post_type' => bbp_get_topic_post_type(),
  842. 'meta_key' => '_bbp_last_active_time',
  843. 'orderby' => 'meta_value',
  844. 'numberposts' => 1
  845. );
  846. // Get the most recent topic in this forum_id
  847. $recent_topic = get_posts( $post_vars );
  848. if ( !empty( $recent_topic ) ) {
  849. $topic_id = $recent_topic[0]->ID;
  850. }
  851. }
  852. // Cast as integer in case of empty or string
  853. $topic_id = (int) $topic_id;
  854. $children_last_topic = (int) $children_last_topic;
  855. // If child forums have higher id, use that instead
  856. if ( !empty( $children ) && ( $children_last_topic > $topic_id ) )
  857. $topic_id = $children_last_topic;
  858. // Update the last public topic ID
  859. if ( bbp_is_topic_published( $topic_id ) )
  860. update_post_meta( $forum_id, '_bbp_last_topic_id', $topic_id );
  861. return (int) apply_filters( 'bbp_update_forum_last_topic_id', $topic_id, $forum_id );
  862. }
  863. /**
  864. * Update the forum last reply id
  865. *
  866. * @since bbPress (r2625)
  867. *
  868. * @param int $forum_id Optional. Forum id
  869. * @param int $reply_id Optional. Reply id
  870. * @uses bbp_get_forum_id() To get the forum id
  871. * @uses bbp_forum_query_subforum_ids() To get the subforum ids
  872. * @uses bbp_update_forum_last_reply_id() To update the last reply id of child
  873. * forums
  874. * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum
  875. * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id
  876. * @uses bbp_is_reply_published() To make sure the reply is published
  877. * @uses update_post_meta() To update the forum's last active id meta
  878. * @uses apply_filters() Calls 'bbp_update_forum_last_reply_id' with the last
  879. * reply id and forum id
  880. * @return bool True on success, false on failure
  881. */
  882. function bbp_update_forum_last_reply_id( $forum_id = 0, $reply_id = 0 ) {
  883. $forum_id = bbp_get_forum_id( $forum_id );
  884. // Define local variable(s)
  885. $children_last_reply = 0;
  886. // Do some calculation if not manually set
  887. if ( empty( $reply_id ) ) {
  888. // Loop through children and get the most recent reply id
  889. $children = bbp_forum_query_subforum_ids( $forum_id );
  890. if ( !empty( $children ) ) {
  891. foreach ( (array) $children as $child ) {
  892. $children_last_reply = bbp_update_forum_last_reply_id( $child ); // Recursive
  893. }
  894. }
  895. // If this forum has topics...
  896. $topic_ids = bbp_forum_query_topic_ids( $forum_id );
  897. if ( !empty( $topic_ids ) ) {
  898. // ...get the most recent reply from those topics...
  899. $reply_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids );
  900. // ...and compare it to the most recent topic id...
  901. $reply_id = ( $reply_id > max( $topic_ids ) ) ? $reply_id : max( $topic_ids );
  902. }
  903. }
  904. // Cast as integer in case of empty or string
  905. $reply_id = (int) $reply_id;
  906. $children_last_reply = (int) $children_last_reply;
  907. // If child forums have higher ID, check for newer reply id
  908. if ( !empty( $children ) && ( $children_last_reply > $reply_id ) )
  909. $reply_id = $children_last_reply;
  910. // Update the last public reply ID
  911. if ( bbp_is_reply_published( $reply_id ) )
  912. update_post_meta( $forum_id, '_bbp_last_reply_id', $reply_id );
  913. return (int) apply_filters( 'bbp_update_forum_last_reply_id', $reply_id, $forum_id );
  914. }
  915. /**
  916. * Update the forum last active post id
  917. *
  918. * @since bbPress (r2860)
  919. *
  920. * @param int $forum_id Optional. Forum id
  921. * @param int $active_id Optional. Active post id
  922. * @uses bbp_get_forum_id() To get the forum id
  923. * @uses bbp_forum_query_subforum_ids() To get the subforum ids
  924. * @uses bbp_update_forum_last_active_id() To update the last active id of
  925. * child forums
  926. * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum
  927. * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id
  928. * @uses get_post_status() To make sure the reply is published
  929. * @uses update_post_meta() To update the forum's last active id meta
  930. * @uses apply_filters() Calls 'bbp_update_forum_last_active_id' with the last
  931. * active post id and forum id
  932. * @return bool True on success, false on failure
  933. */
  934. function bbp_update_forum_last_active_id( $forum_id = 0, $active_id = 0 ) {
  935. $forum_id = bbp_get_forum_id( $forum_id );
  936. // Define local variable(s)
  937. $children_last_active = 0;
  938. // Do some calculation if not manually set
  939. if ( empty( $active_id ) ) {
  940. // Loop through children and add together forum reply counts
  941. $children = bbp_forum_query_subforum_ids( $forum_id );
  942. if ( !empty( $children ) ) {
  943. foreach ( (array) $children as $child ) {
  944. $children_last_active = bbp_update_forum_last_active_id( $child, $active_id );
  945. }
  946. }
  947. // Don't count replies if the forum is a category
  948. $topic_ids = bbp_forum_query_topic_ids( $forum_id );
  949. if ( !empty( $topic_ids ) ) {
  950. $active_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids );
  951. $active_id = $active_id > max( $topic_ids ) ? $active_id : max( $topic_ids );
  952. // Forum has no topics
  953. } else {
  954. $active_id = 0;
  955. }
  956. }
  957. // Cast as integer in case of empty or string
  958. $active_id = (int) $active_id;
  959. $children_last_active = (int) $children_last_active;
  960. // If child forums have higher id, use that instead
  961. if ( !empty( $children ) && ( $children_last_active > $active_id ) )
  962. $active_id = $children_last_active;
  963. // Update only if published
  964. if ( bbp_get_public_status_id() == get_post_status( $active_id ) )
  965. update_post_meta( $forum_id, '_bbp_last_active_id', (int) $active_id );
  966. return (int) apply_filters( 'bbp_update_forum_last_active_id', (int) $active_id, $forum_id );
  967. }
  968. /**
  969. * Update the forums last active date/time (aka freshness)
  970. *
  971. * @since bbPress (r2680)
  972. *
  973. * @param int $forum_id Optional. Topic id
  974. * @param string $new_time Optional. New time in mysql format
  975. * @uses bbp_get_forum_id() To get the forum id
  976. * @uses bbp_get_forum_last_active_id() To get the forum's last post id
  977. * @uses get_post_field() To get the post date of the forum's last post
  978. * @uses update_post_meta() To update the forum last active time
  979. * @uses apply_filters() Calls 'bbp_update_forum_last_active' with the new time
  980. * and forum id
  981. * @return bool True on success, false on failure
  982. */
  983. function bbp_update_forum_last_active_time( $forum_id = 0, $new_time = '' ) {
  984. $forum_id = bbp_get_forum_id( $forum_id );
  985. // Check time and use current if empty
  986. if ( empty( $new_time ) )
  987. $new_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $forum_id ) );
  988. // Update only if there is a time
  989. if ( !empty( $new_time ) )
  990. update_post_meta( $forum_id, '_bbp_last_active_time', $new_time );
  991. return (int) apply_filters( 'bbp_update_forum_last_active', $new_time, $forum_id );
  992. }
  993. /**
  994. * Update the forum sub-forum count
  995. *
  996. * @since bbPress (r2625)
  997. *
  998. * @param int $forum_id Optional. Forum id
  999. * @uses bbp_get_forum_id() To get the forum id
  1000. * @return bool True on success, false on failure
  1001. */
  1002. function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = 0 ) {
  1003. $forum_id = bbp_get_forum_id( $forum_id );
  1004. if ( empty( $subforums ) )
  1005. $subforums = count( bbp_forum_query_subforum_ids( $forum_id ) );
  1006. update_post_meta( $forum_id, '_bbp_forum_subforum_count', (int) $subforums );
  1007. return (int) apply_filters( 'bbp_update_forum_subforum_count', (int) $subforums, $forum_id );
  1008. }
  1009. /**
  1010. * Adjust the total topic count of a forum
  1011. *
  1012. * @since bbPress (r2464)
  1013. *
  1014. * @param int $forum_id Optional. Forum id or topic id. It is checked whether it
  1015. * is a topic or a forum. If it's a topic, its parent,
  1016. * i.e. the forum is automatically retrieved.
  1017. * @param bool $total_count Optional. To return the total count or normal
  1018. * count?
  1019. * @uses bbp_get_forum_id() To get the forum id
  1020. * @uses bbp_forum_query_subforum_ids() To get the subforum ids
  1021. * @uses bbp_update_forum_topic_count() To update the forum topic count
  1022. * @uses bbp_forum_query_topic_ids() To get the forum topic ids
  1023. * @uses update_post_meta() To update the forum's topic count meta
  1024. * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the topic
  1025. * count and forum id
  1026. * @return int Forum topic count
  1027. */
  1028. function bbp_update_forum_topic_count( $forum_id = 0 ) {
  1029. $forum_id = bbp_get_forum_id( $forum_id );
  1030. $children_topic_count = 0;
  1031. // Loop through subforums and add together forum topic counts
  1032. $children = bbp_forum_query_subforum_ids( $forum_id );
  1033. if ( !empty( $children ) ) {
  1034. foreach ( (array) $children as $child ) {
  1035. $children_topic_count += bbp_update_forum_topic_count( $child ); // Recursive
  1036. }
  1037. }
  1038. // Get total topics for this forum
  1039. $topics = (int) count( bbp_forum_query_topic_ids( $forum_id ) );
  1040. // Calculate total topics in this forum
  1041. $total_topics = $topics + $children_topic_count;
  1042. // Update the count
  1043. update_post_meta( $forum_id, '_bbp_topic_count', (int) $topics );
  1044. update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topics );
  1045. return (int) apply_filters( 'bbp_update_forum_topic_count', (int) $total_topics, $forum_id );
  1046. }
  1047. /**
  1048. * Adjust the total hidden topic count of a forum (hidden includes trashed and spammed topics)
  1049. *
  1050. * @since bbPress (r2888)
  1051. *
  1052. * @param int $forum_id Optional. Topic id to update
  1053. * @param int $topic_count Optional. Set the topic count manually
  1054. * @uses bbp_is_topic() To check if the supplied id is a topic
  1055. * @uses bbp_get_topic_id() To get the topic id
  1056. * @uses bbp_get_topic_forum_id() To get the topic forum id
  1057. * @uses bbp_get_forum_id() To get the forum id
  1058. * @uses wpdb::prepare() To prepare our sql query
  1059. * @uses wpdb::get_col() To execute our query and get the column back
  1060. * @uses update_post_meta() To update the forum hidden topic count meta
  1061. * @uses apply_filters() Calls 'bbp_update_forum_topic_count_hidden' with the
  1062. * hidden topic count and forum id
  1063. * @return int Topic hidden topic count
  1064. */
  1065. function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) {
  1066. global $wpdb;
  1067. // If topic_id was passed as $forum_id, then get its forum
  1068. if ( bbp_is_topic( $forum_id ) ) {
  1069. $topic_id = bbp_get_topic_id( $forum_id );
  1070. $forum_id = bbp_get_topic_forum_id( $topic_id );
  1071. // $forum_id is not a topic_id, so validate and proceed
  1072. } else {
  1073. $forum_id = bbp_get_forum_id( $forum_id );
  1074. }
  1075. // Can't update what isn't there
  1076. if ( !empty( $forum_id ) ) {
  1077. // Get topics of forum
  1078. if ( empty( $topic_count ) )
  1079. $topic_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "') AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) );
  1080. // Update the count
  1081. update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count );
  1082. }
  1083. return (int) apply_filters( 'bbp_update_forum_topic_count_hidden', (int) $topic_count, $forum_id );
  1084. }
  1085. /**
  1086. * Adjust the total reply count of a forum
  1087. *
  1088. * @since bbPress (r2464)
  1089. *
  1090. * @param int $forum_id Optional. Forum id or topic id. It is checked whether it
  1091. * is a topic or a forum. If it's a topic, its parent,
  1092. * i.e. the forum is automatically retrieved.
  1093. * @param bool $total_count Optional. To return the total count or normal
  1094. * count?
  1095. * @uses bbp_get_forum_id() To get the forum id
  1096. * @uses bbp_forum_query_subforum_ids() To get the subforum ids
  1097. * @uses bbp_update_forum_reply_count() To update the forum reply count
  1098. * @uses bbp_forum_query_topic_ids() To get the forum topic ids
  1099. * @uses wpdb::prepare() To prepare the sql statement
  1100. * @uses wpdb::get_var() To execute the query and get the var back
  1101. * @uses update_post_meta() To update the forum's reply count meta
  1102. * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the reply
  1103. * count and forum id
  1104. * @return int Forum reply count
  1105. */
  1106. function bbp_update_forum_reply_count( $forum_id = 0 ) {
  1107. global $wpdb;
  1108. $forum_id = bbp_get_forum_id( $forum_id );
  1109. $children_reply_count = 0;
  1110. // Loop through children and add together forum reply counts
  1111. $children = bbp_forum_query_subforum_ids( $forum_id );
  1112. if ( !empty( $children ) ) {
  1113. foreach ( (array) $children as $child ) {
  1114. $children_reply_count += bbp_update_forum_reply_count( $child );
  1115. }
  1116. }
  1117. // Don't count replies if the forum is a category
  1118. $topic_ids = bbp_forum_query_topic_ids( $forum_id );
  1119. if ( !empty( $topic_ids ) ) {
  1120. $reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
  1121. } else {
  1122. $reply_count = 0;
  1123. }
  1124. // Calculate total replies in this forum
  1125. $total_replies = (int) $reply_count + $children_reply_count;
  1126. // Update the count
  1127. update_post_meta( $forum_id, '_bbp_reply_count', (int) $reply_count );
  1128. update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_replies );
  1129. return (int) apply_filters( 'bbp_update_forum_reply_count', (int) $total_replies, $forum_id );
  1130. }
  1131. /**
  1132. * Updates the counts of a forum.
  1133. *
  1134. * This calls a few internal functions that all run manual queries against the
  1135. * database to get their results. As such, this function can be costly to run
  1136. * but is necessary to keep everything accurate.
  1137. *
  1138. * @since bbPress (r2908)
  1139. *
  1140. * @param mixed $args Supports these arguments:
  1141. * - forum_id: Forum id
  1142. * - last_topic_id: Last topic id
  1143. * - last_reply_id: Last reply id
  1144. * - last_active_id: Last active post id
  1145. * - last_active_time: last active time
  1146. * @uses bbp_update_forum_last_topic_id() To update the forum last topic id
  1147. * @uses bbp_update_forum_last_reply_id() To update the forum last reply id
  1148. * @uses bbp_update_forum_last_active_id() To update the last active post id
  1149. * @uses get_post_field() To get the post date of the last active id
  1150. * @uses bbp_update_forum_last_active_time() To update the last active time
  1151. * @uses bbp_update_forum_subforum_count() To update the subforum count
  1152. * @uses bbp_update_forum_topic_count() To update the forum topic count
  1153. * @uses bbp_update_forum_reply_count() To update the forum reply count
  1154. * @uses bbp_update_forum_topic_count_hidden() To update the hidden topic count
  1155. */
  1156. function bbp_update_forum( $args = '' ) {
  1157. // Parse arguments against default values
  1158. $r = bbp_parse_args( $args, array(
  1159. 'forum_id' => 0,
  1160. 'post_parent' => 0,
  1161. 'last_topic_id' => 0,
  1162. 'last_reply_id' => 0,
  1163. 'last_active_id' => 0,
  1164. 'last_active_time' => 0,
  1165. 'last_active_status' => bbp_get_public_status_id()
  1166. ), 'update_forum' );
  1167. // Last topic and reply ID's
  1168. bbp_update_forum_last_topic_id( $r['forum_id'], $r['last_topic_id'] );
  1169. bbp_update_forum_last_reply_id( $r['forum_id'], $r['last_reply_id'] );
  1170. // Active dance
  1171. $r['last_active_id'] = bbp_update_forum_last_active_id( $r['forum_id'], $r['last_active_id'] );
  1172. // If no active time was passed, get it from the last_active_id
  1173. if ( empty( $r['last_active_time'] ) ) {
  1174. $r['last_active_time'] = get_post_field( 'post_date', $r['last_active_id'] );
  1175. }
  1176. if ( bbp_get_public_status_id() == $r['last_active_status'] ) {
  1177. bbp_update_forum_last_active_time( $r['forum_id'], $r['last_active_time'] );
  1178. }
  1179. // Counts
  1180. bbp_update_forum_subforum_count ( $r['forum_id'] );
  1181. bbp_update_forum_reply_count ( $r['forum_id'] );
  1182. bbp_update_forum_topic_count ( $r['forum_id'] );
  1183. bbp_update_forum_topic_count_hidden( $r['forum_id'] );
  1184. // Update the parent forum if one was passed
  1185. if ( !empty( $r['post_parent'] ) && is_numeric( $r['post_parent'] ) ) {
  1186. bbp_update_forum( array(
  1187. 'forum_id' => $r['post_parent'],
  1188. 'post_parent' => get_post_field( 'post_parent', $r['post_parent'] )
  1189. ) );
  1190. }
  1191. }
  1192. /** Queries *******************************************************************/
  1193. /**
  1194. * Returns the hidden forum ids
  1195. *
  1196. * Only hidden forum ids are returned. Public and private ids are not.
  1197. *
  1198. * @since bbPress (r3007)
  1199. *
  1200. * @uses get_option() Returns the unserialized array of hidden forum ids
  1201. * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids
  1202. * and forum id
  1203. */
  1204. function bbp_get_hidden_forum_ids() {
  1205. $forum_ids = get_option( '_bbp_hidden_forums', array() );
  1206. return apply_filters( 'bbp_get_hidden_forum_ids', (array) $forum_ids );
  1207. }
  1208. /**
  1209. * Returns the private forum ids
  1210. *
  1211. * Only private forum ids are returned. Public and hidden ids are not.
  1212. *
  1213. * @since bbPress (r3007)
  1214. *
  1215. * @uses get_option() Returns the unserialized array of private forum ids
  1216. * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids
  1217. * and forum id
  1218. */
  1219. function bbp_get_private_forum_ids() {
  1220. $forum_ids = get_option( '_bbp_private_forums', array() );
  1221. return apply_filters( 'bbp_get_private_forum_ids', (array) $forum_ids );
  1222. }
  1223. /**
  1224. * Returns a meta_query that either includes or excludes hidden forum IDs
  1225. * from a query.
  1226. *
  1227. * @since bbPress (r3291)
  1228. *
  1229. * @param string Optional. The type of value to return. (string|array|meta_query)
  1230. *
  1231. * @uses bbp_is_user_keymaster()
  1232. * @uses bbp_get_hidden_forum_ids()
  1233. * @uses bbp_get_private_forum_ids()
  1234. * @uses apply_filters()
  1235. */
  1236. function bbp_exclude_forum_ids( $type = 'string' ) {
  1237. // Setup arrays
  1238. $private = $hidden = $meta_query = $forum_ids = array();
  1239. // Default return value
  1240. switch ( $type ) {
  1241. case 'string' :
  1242. $retval = '';
  1243. break;
  1244. case 'array' :
  1245. $retval = array();
  1246. break;
  1247. case 'meta_query' :
  1248. $retval = array( array() ) ;
  1249. break;
  1250. }
  1251. // Exclude for everyone but keymasters
  1252. if ( ! bbp_is_user_keymaster() ) {
  1253. // Private forums
  1254. if ( !current_user_can( 'read_private_forums' ) )
  1255. $private = bbp_get_private_forum_ids();
  1256. // Hidden forums
  1257. if ( !current_user_can( 'read_hidden_forums' ) )
  1258. $hidden = bbp_get_hidden_forum_ids();
  1259. // Merge private and hidden forums together
  1260. $forum_ids = (array) array_filter( array_merge( $private, $hidden ) );
  1261. // There are forums that need to be excluded
  1262. if ( !empty( $forum_ids ) ) {
  1263. switch ( $type ) {
  1264. // Separate forum ID's into a comma separated string
  1265. case 'string' :
  1266. $retval = implode( ',', $forum_ids );
  1267. break;
  1268. // Use forum_ids array
  1269. case 'array' :
  1270. $retval = $forum_ids;
  1271. break;
  1272. // Build a meta_query
  1273. case 'meta_query' :
  1274. $retval = array(
  1275. 'key' => '_bbp_forum_id',
  1276. 'value' => implode( ',', $forum_ids ),
  1277. 'type' => 'numeric',
  1278. 'compare' => ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!='
  1279. );
  1280. break;
  1281. }
  1282. }
  1283. }
  1284. // Filter and return the results
  1285. return apply_filters( 'bbp_exclude_forum_ids', $retval, $forum_ids, $type );
  1286. }
  1287. /**
  1288. * Adjusts forum, topic, and reply queries to exclude items that might be
  1289. * contained inside hidden or private forums that the user does not have the
  1290. * capability to view.
  1291. *
  1292. * Doing it with an action allows us to trap all WP_Query's rather than needing
  1293. * to hardcode this logic into each query. It also protects forum content for
  1294. * plugins that might be doing their own queries.
  1295. *
  1296. * @since bbPress (r3291)
  1297. *
  1298. * @param WP_Query $posts_query
  1299. *
  1300. * @uses apply_filters()
  1301. * @uses bbp_exclude_forum_ids()
  1302. * @uses bbp_get_topic_post_type()
  1303. * @uses bbp_get_reply_post_type()
  1304. * @return WP_Query
  1305. */
  1306. function bbp_pre_get_posts_normalize_forum_visibility( $posts_query = null ) {
  1307. // Bail if all forums are explicitly allowed
  1308. if ( true === apply_filters( 'bbp_include_all_forums', false, $posts_query ) ) {
  1309. return;
  1310. }
  1311. // Bail if $posts_query is not an object or of incorrect class
  1312. if ( !is_object( $posts_query ) || !is_a( $posts_query, 'WP_Query' ) ) {
  1313. return;
  1314. }
  1315. // Bail if filters are suppressed on this query
  1316. if ( true == $posts_query->get( 'suppress_filters' ) ) {
  1317. return;
  1318. }
  1319. // Get query post types array .
  1320. $post_types = (array) $posts_query->get( 'post_type' );
  1321. // Forums
  1322. if ( in_array( bbp_get_forum_post_type() , $post_types ) ) {
  1323. // Prevent accidental wp-admin post_row override
  1324. if ( is_admin() && isset( $_REQUEST['post_status'] ) ) {
  1325. return;
  1326. }
  1327. /** Default ***********************************************************/
  1328. // Get any existing post status
  1329. $post_stati = $posts_query->get( 'post_status' );
  1330. // Default to public status
  1331. if ( empty( $post_stati ) ) {
  1332. $post_stati[] = bbp_get_public_status_id();
  1333. // Split the status string
  1334. } elseif ( is_string( $post_stati ) ) {
  1335. $post_stati = explode( ',', $post_stati );
  1336. }
  1337. /** Private ***********************************************************/
  1338. // Remove bbp_get_private_status_id() if user is not capable
  1339. if ( ! current_user_can( 'read_private_forums' ) ) {
  1340. $key = array_search( bbp_get_private_status_id(), $post_stati );
  1341. if ( !empty( $key ) ) {
  1342. unset( $post_stati[$key] );
  1343. }
  1344. // ...or add it if they are
  1345. } else {
  1346. $post_stati[] = bbp_get_private_status_id();
  1347. }
  1348. /** Hidden ************************************************************/
  1349. // Remove bbp_get_hidden_status_id() if user is not capable
  1350. if ( ! current_user_can( 'read_hidden_forums' ) ) {
  1351. $key = array_search( bbp_get_hidden_status_id(), $post_stati );
  1352. if ( !empty( $key ) ) {
  1353. unset( $post_stati[$key] );
  1354. }
  1355. // ...or add it if they are
  1356. } else {
  1357. $post_stati[] = bbp_get_hidden_status_id();
  1358. }
  1359. // Add the statuses
  1360. $posts_query->set( 'post_status', $post_stati );
  1361. }
  1362. // Topics Or Replies
  1363. if ( array_intersect( array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ), $post_types ) ) {
  1364. // Get forums to exclude
  1365. $forum_ids = bbp_exclude_forum_ids( 'meta_query' );
  1366. // Bail if no forums to exclude
  1367. if ( ! array_filter( $forum_ids ) ) {
  1368. return;
  1369. }
  1370. // Get any existing meta queries
  1371. $meta_query = $posts_query->get( 'meta_query' );
  1372. // Add our meta query to existing
  1373. $meta_query[] = $forum_ids;
  1374. // Set the meta_query var
  1375. $posts_query->set( 'meta_query', $meta_query );
  1376. }
  1377. }
  1378. /**
  1379. * Returns the forum's topic ids
  1380. *
  1381. * Only topics with published and closed statuses are returned
  1382. *
  1383. * @since bbPress (r2908)
  1384. *
  1385. * @param int $forum_id Forum id
  1386. * @uses bbp_get_topic_post_type() To get the topic post type
  1387. * @uses bbp_get_public_child_ids() To get the topic ids
  1388. * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids
  1389. * and forum id
  1390. */
  1391. function bbp_forum_query_topic_ids( $forum_id ) {
  1392. $topic_ids = bbp_get_public_child_ids( $forum_id, bbp_get_topic_post_type() );
  1393. return apply_filters( 'bbp_forum_query_topic_ids', $topic_ids, $forum_id );
  1394. }
  1395. /**
  1396. * Returns the forum's subforum ids
  1397. *
  1398. * Only forums with published status are returned
  1399. *
  1400. * @since bbPress (r2908)
  1401. *
  1402. * @param int $forum_id Forum id
  1403. * @uses bbp_get_forum_post_type() To get the forum post type
  1404. * @uses bbp_get_public_child_ids() To get the forum ids
  1405. * @uses apply_filters() Calls 'bbp_forum_query_subforum_ids' with the subforum
  1406. * ids and forum id
  1407. */
  1408. function bbp_forum_query_subforum_ids( $forum_id ) {
  1409. $subforum_ids = bbp_get_all_child_ids( $forum_id, bbp_get_forum_post_type() );
  1410. //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' );
  1411. return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id );
  1412. }
  1413. /**
  1414. * Callback to sort forum ID's based on last active time
  1415. *
  1416. * @since bbPress (r3789)
  1417. * @param int $a First forum ID to compare
  1418. * @param int $b Second forum ID to compare
  1419. * @return Position change based on sort
  1420. */
  1421. function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) {
  1422. $ta = get_post_meta( $a, '_bbp_last_active_time', true );
  1423. $tb = get_post_meta( $b, '_bbp_last_active_time', true );
  1424. return ( $ta < $tb ) ? -1 : 1;
  1425. }
  1426. /**
  1427. * Returns the forum's last reply id
  1428. *
  1429. * @since bbPress (r2908)
  1430. *
  1431. * @param int $forum_id Forum id
  1432. * @param int $topic_ids Optional. Topic ids
  1433. * @uses wp_cache_get() To check for cache and retrieve it
  1434. * @uses bbp_forum_query_topic_ids() To get the forum's topic ids
  1435. * @uses wpdb::prepare() To prepare the query
  1436. * @uses wpdb::get_var() To execute the query and get the var back
  1437. * @uses bbp_get_reply_post_type() To get the reply post type
  1438. * @uses wp_cache_set() To set the cache for future use
  1439. * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id
  1440. * and forum id
  1441. */
  1442. function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) {
  1443. global $wpdb;
  1444. $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';
  1445. $reply_id = (int) wp_cache_get( $cache_id, 'bbpress_posts' );
  1446. if ( empty( $reply_id ) ) {
  1447. if ( empty( $topic_ids ) ) {
  1448. $topic_ids = bbp_forum_query_topic_ids( $forum_id );
  1449. }
  1450. if ( !empty( $topic_ids ) ) {
  1451. $reply_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
  1452. wp_cache_set( $cache_id, $reply_id, 'bbpress_posts' ); // May be (int) 0
  1453. } else {
  1454. wp_cache_set( $cache_id, '0', 'bbpress_posts' );
  1455. }
  1456. }
  1457. return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id );
  1458. }
  1459. /** Listeners *****************************************************************/
  1460. /**
  1461. * Check if it's a hidden forum or a topic or reply of a hidden forum and if
  1462. * the user can't view it, then sets a 404
  1463. *
  1464. * @since bbPress (r2996)
  1465. *
  1466. * @uses current_user_can() To check if the current user can read private forums
  1467. * @uses is_singular() To check if it's a singular page
  1468. * @uses bbp_is_user_keymaster() To check if user is a keymaster
  1469. * @uses bbp_get_forum_post_type() To get the forum post type
  1470. * @uses bbp_get_topic_post_type() To get the topic post type
  1471. * @uses bbp_get_reply_post_type() TO get the reply post type
  1472. * @uses bbp_get_topic_forum_id() To get the topic forum id
  1473. * @uses bbp_get_reply_forum_id() To get the reply forum id
  1474. * @uses bbp_is_forum_hidden() To check if the forum is hidden or not
  1475. * @uses bbp_set_404() To set a 404 status
  1476. */
  1477. function bbp_forum_enforce_hidden() {
  1478. // Bail if not viewing a single item or if user has caps
  1479. if ( !is_singular() || bbp_is_user_keymaster() || current_user_can( 'read_hidden_forums' ) )
  1480. return;
  1481. global $wp_query;
  1482. // Define local variable
  1483. $forum_id = 0;
  1484. // Check post type
  1485. switch ( $wp_query->get( 'post_type' ) ) {
  1486. // Forum
  1487. case bbp_get_forum_post_type() :
  1488. $forum_id = bbp_get_forum_id( $wp_query->post->ID );
  1489. break;
  1490. // Topic
  1491. case bbp_get_topic_post_type() :
  1492. $forum_id = bbp_get_topic_forum_id( $wp_query->post->ID );
  1493. break;
  1494. // Reply
  1495. case bbp_get_reply_post_type() :
  1496. $forum_id = bbp_get_reply_forum_id( $wp_query->post->ID );
  1497. break;
  1498. }
  1499. // If forum is explicitly hidden and user not capable, set 404
  1500. if ( !empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
  1501. bbp_set_404();
  1502. }
  1503. /**
  1504. * Check if it's a private forum or a topic or reply of a private forum and if
  1505. * the user can't view it, then sets a 404
  1506. *
  1507. * @since bbPress (r2996)
  1508. *
  1509. * @uses current_user_can() To check if the current user can read private forums
  1510. * @uses is_singular() To check if it's a singular page
  1511. * @uses bbp_is_user_keymaster() To check if user is a keymaster
  1512. * @uses bbp_get_forum_post_type() To get the forum post type
  1513. * @uses bbp_get_topic_post_type() To get the topic post type
  1514. * @uses bbp_get_reply_post_type() TO get the reply post type
  1515. * @uses bbp_get_topic_forum_id() To get the topic forum id
  1516. * @uses bbp_get_reply_forum_id() To get the reply forum id
  1517. * @uses bbp_is_forum_private() To check if the forum is private or not
  1518. * @uses bbp_set_404() To set a 404 status
  1519. */
  1520. function bbp_forum_enforce_private() {
  1521. // Bail if not viewing a single item or if user has caps
  1522. if ( !is_singular() || bbp_is_user_keymaster() || current_user_can( 'read_private_forums' ) )
  1523. return;
  1524. global $wp_query;
  1525. // Define local variable
  1526. $forum_id = 0;
  1527. // Check post type
  1528. switch ( $wp_query->get( 'post_type' ) ) {
  1529. // Forum
  1530. case bbp_get_forum_post_type() :
  1531. $forum_id = bbp_get_forum_id( $wp_query->post->ID );
  1532. break;
  1533. // Topic
  1534. case bbp_get_topic_post_type() :
  1535. $forum_id = bbp_get_topic_forum_id( $wp_query->post->ID );
  1536. break;
  1537. // Reply
  1538. case bbp_get_reply_post_type() :
  1539. $forum_id = bbp_get_reply_forum_id( $wp_query->post->ID );
  1540. break;
  1541. }
  1542. // If forum is explicitly hidden and user not capable, set 404
  1543. if ( !empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
  1544. bbp_set_404();
  1545. }
  1546. /** Permissions ***************************************************************/
  1547. /**
  1548. * Redirect if unathorized user is attempting to edit a forum
  1549. *
  1550. * @since bbPress (r3607)
  1551. *
  1552. * @uses bbp_is_forum_edit()
  1553. * @uses current_user_can()
  1554. * @uses bbp_get_forum_id()
  1555. * @uses wp_safe_redirect()
  1556. * @uses bbp_get_forum_permalink()
  1557. */
  1558. function bbp_check_forum_edit() {
  1559. // Bail if not editing a topic
  1560. if ( !bbp_is_forum_edit() )
  1561. return;
  1562. // User cannot edit topic, so redirect back to reply
  1563. if ( !current_user_can( 'edit_forum', bbp_get_forum_id() ) ) {
  1564. wp_safe_redirect( bbp_get_forum_permalink() );
  1565. exit();
  1566. }
  1567. }
  1568. /**
  1569. * Delete all topics (and their replies) for a specific forum ID
  1570. *
  1571. * @since bbPress (r3668)
  1572. *
  1573. * @param int $forum_id
  1574. * @uses bbp_get_forum_id() To validate the forum ID
  1575. * @uses bbp_is_forum() To make sure it's a forum
  1576. * @uses bbp_get_topic_post_type() To get the topic post type
  1577. * @uses bbp_topics() To make sure there are topics to loop through
  1578. * @uses wp_trash_post() To trash the post
  1579. * @uses update_post_meta() To update the forum meta of trashed topics
  1580. * @return If forum is not valid
  1581. */
  1582. function bbp_delete_forum_topics( $forum_id = 0 ) {
  1583. // Validate forum ID
  1584. $forum_id = bbp_get_forum_id( $forum_id );
  1585. if ( empty( $forum_id ) )
  1586. return;
  1587. // Forum is being permanently deleted, so its content has go too
  1588. // Note that we get all post statuses here
  1589. $topics = new WP_Query( array(
  1590. 'suppress_filters' => true,
  1591. 'post_type' => bbp_get_topic_post_type(),
  1592. 'post_parent' => $forum_id,
  1593. 'post_status' => array_keys( get_post_stati() ),
  1594. 'posts_per_page' => -1,
  1595. 'nopaging' => true,
  1596. 'fields' => 'id=>parent'
  1597. ) );
  1598. // Loop through and delete child topics. Topic replies will get deleted by
  1599. // the bbp_delete_topic() action.
  1600. if ( !empty( $topics->posts ) ) {
  1601. foreach ( $topics->posts as $topic ) {
  1602. wp_delete_post( $topic->ID, true );
  1603. }
  1604. // Reset the $post global
  1605. wp_reset_postdata();
  1606. }
  1607. // Cleanup
  1608. unset( $topics );
  1609. }
  1610. /**
  1611. * Trash all topics inside a forum
  1612. *
  1613. * @since bbPress (r3668)
  1614. *
  1615. * @param int $forum_id
  1616. * @uses bbp_get_forum_id() To validate the forum ID
  1617. * @uses bbp_is_forum() To make sure it's a forum
  1618. * @uses bbp_get_public_status_id() To return public post status
  1619. * @uses bbp_get_closed_status_id() To return closed post status
  1620. * @uses bbp_get_pending_status_id() To return pending post status
  1621. * @uses bbp_get_topic_post_type() To get the topic post type
  1622. * @uses wp_trash_post() To trash the post
  1623. * @uses update_post_meta() To update the forum meta of trashed topics
  1624. * @return If forum is not valid
  1625. */
  1626. function bbp_trash_forum_topics( $forum_id = 0 ) {
  1627. // Validate forum ID
  1628. $forum_id = bbp_get_forum_id( $forum_id );
  1629. if ( empty( $forum_id ) )
  1630. return;
  1631. // Allowed post statuses to pre-trash
  1632. $post_stati = join( ',', array(
  1633. bbp_get_public_status_id(),
  1634. bbp_get_closed_status_id(),
  1635. bbp_get_pending_status_id()
  1636. ) );
  1637. // Forum is being trashed, so its topics and replies are trashed too
  1638. $topics = new WP_Query( array(
  1639. 'suppress_filters' => true,
  1640. 'post_type' => bbp_get_topic_post_type(),
  1641. 'post_parent' => $forum_id,
  1642. 'post_status' => $post_stati,
  1643. 'posts_per_page' => -1,
  1644. 'nopaging' => true,
  1645. 'fields' => 'id=>parent'
  1646. ) );
  1647. // Loop through and trash child topics. Topic replies will get trashed by
  1648. // the bbp_trash_topic() action.
  1649. if ( !empty( $topics->posts ) ) {
  1650. // Prevent debug notices
  1651. $pre_trashed_topics = array();
  1652. // Loop through topics, trash them, and add them to array
  1653. foreach ( $topics->posts as $topic ) {
  1654. wp_trash_post( $topic->ID, true );
  1655. $pre_trashed_topics[] = $topic->ID;
  1656. }
  1657. // Set a post_meta entry of the topics that were trashed by this action.
  1658. // This is so we can possibly untrash them, without untrashing topics
  1659. // that were purposefully trashed before.
  1660. update_post_meta( $forum_id, '_bbp_pre_trashed_topics', $pre_trashed_topics );
  1661. // Reset the $post global
  1662. wp_reset_postdata();
  1663. }
  1664. // Cleanup
  1665. unset( $topics );
  1666. }
  1667. /**
  1668. * Trash all topics inside a forum
  1669. *
  1670. * @since bbPress (r3668)
  1671. *
  1672. * @param int $forum_id
  1673. * @uses bbp_get_forum_id() To validate the forum ID
  1674. * @uses bbp_is_forum() To make sure it's a forum
  1675. * @uses get_post_meta() To update the forum meta of trashed topics
  1676. * @uses wp_untrash_post() To trash the post
  1677. * @return If forum is not valid
  1678. */
  1679. function bbp_untrash_forum_topics( $forum_id = 0 ) {
  1680. // Validate forum ID
  1681. $forum_id = bbp_get_forum_id( $forum_id );
  1682. if ( empty( $forum_id ) )
  1683. return;
  1684. // Get the topics that were not previously trashed
  1685. $pre_trashed_topics = get_post_meta( $forum_id, '_bbp_pre_trashed_topics', true );
  1686. // There are topics to untrash
  1687. if ( !empty( $pre_trashed_topics ) ) {
  1688. // Maybe reverse the trashed topics array
  1689. if ( is_array( $pre_trashed_topics ) )
  1690. $pre_trashed_topics = array_reverse( $pre_trashed_topics );
  1691. // Loop through topics
  1692. foreach ( (array) $pre_trashed_topics as $topic ) {
  1693. wp_untrash_post( $topic );
  1694. }
  1695. }
  1696. }
  1697. /** Before Delete/Trash/Untrash ***********************************************/
  1698. /**
  1699. * Called before deleting a forum.
  1700. *
  1701. * This function is supplemental to the actual forum deletion which is
  1702. * handled by WordPress core API functions. It is used to clean up after
  1703. * a forum that is being deleted.
  1704. *
  1705. * @since bbPress (r3668)
  1706. * @uses bbp_get_forum_id() To get the forum id
  1707. * @uses bbp_is_forum() To check if the passed id is a forum
  1708. * @uses do_action() Calls 'bbp_delete_forum' with the forum id
  1709. */
  1710. function bbp_delete_forum( $forum_id = 0 ) {
  1711. $forum_id = bbp_get_forum_id( $forum_id );
  1712. if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
  1713. return false;
  1714. do_action( 'bbp_delete_forum', $forum_id );
  1715. }
  1716. /**
  1717. * Called before trashing a forum
  1718. *
  1719. * This function is supplemental to the actual forum being trashed which is
  1720. * handled by WordPress core API functions. It is used to clean up after
  1721. * a forum that is being trashed.
  1722. *
  1723. * @since bbPress (r3668)
  1724. * @uses bbp_get_forum_id() To get the forum id
  1725. * @uses bbp_is_forum() To check if the passed id is a forum
  1726. * @uses do_action() Calls 'bbp_trash_forum' with the forum id
  1727. */
  1728. function bbp_trash_forum( $forum_id = 0 ) {
  1729. $forum_id = bbp_get_forum_id( $forum_id );
  1730. if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
  1731. return false;
  1732. do_action( 'bbp_trash_forum', $forum_id );
  1733. }
  1734. /**
  1735. * Called before untrashing a forum
  1736. *
  1737. * @since bbPress (r3668)
  1738. * @uses bbp_get_forum_id() To get the forum id
  1739. * @uses bbp_is_forum() To check if the passed id is a forum
  1740. * @uses do_action() Calls 'bbp_untrash_forum' with the forum id
  1741. */
  1742. function bbp_untrash_forum( $forum_id = 0 ) {
  1743. $forum_id = bbp_get_forum_id( $forum_id );
  1744. if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
  1745. return false;
  1746. do_action( 'bbp_untrash_forum', $forum_id );
  1747. }
  1748. /** After Delete/Trash/Untrash ************************************************/
  1749. /**
  1750. * Called after deleting a forum
  1751. *
  1752. * @since bbPress (r3668)
  1753. * @uses bbp_get_forum_id() To get the forum id
  1754. * @uses bbp_is_forum() To check if the passed id is a forum
  1755. * @uses do_action() Calls 'bbp_deleted_forum' with the forum id
  1756. */
  1757. function bbp_deleted_forum( $forum_id = 0 ) {
  1758. $forum_id = bbp_get_forum_id( $forum_id );
  1759. if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
  1760. return false;
  1761. do_action( 'bbp_deleted_forum', $forum_id );
  1762. }
  1763. /**
  1764. * Called after trashing a forum
  1765. *
  1766. * @since bbPress (r3668)
  1767. * @uses bbp_get_forum_id() To get the forum id
  1768. * @uses bbp_is_forum() To check if the passed id is a forum
  1769. * @uses do_action() Calls 'bbp_trashed_forum' with the forum id
  1770. */
  1771. function bbp_trashed_forum( $forum_id = 0 ) {
  1772. $forum_id = bbp_get_forum_id( $forum_id );
  1773. if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
  1774. return false;
  1775. do_action( 'bbp_trashed_forum', $forum_id );
  1776. }
  1777. /**
  1778. * Called after untrashing a forum
  1779. *
  1780. * @since bbPress (r3668)
  1781. * @uses bbp_get_forum_id() To get the forum id
  1782. * @uses bbp_is_forum() To check if the passed id is a forum
  1783. * @uses do_action() Calls 'bbp_untrashed_forum' with the forum id
  1784. */
  1785. function bbp_untrashed_forum( $forum_id = 0 ) {
  1786. $forum_id = bbp_get_forum_id( $forum_id );
  1787. if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
  1788. return false;
  1789. do_action( 'bbp_untrashed_forum', $forum_id );
  1790. }