PageRenderTime 56ms CodeModel.GetById 19ms 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

Large files files are truncated, but you can click here to view the full file

  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_h…

Large files files are truncated, but you can click here to view the full file