PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/bfay/maniacal-kitten
PHP | 3431 lines | 1394 code | 698 blank | 1339 comment | 356 complexity | db1f3a9066c61ae7b7a6d0cdf08ac377 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 Topic 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 topic to function properly.
  14. *
  15. * @since bbPress (r3349)
  16. *
  17. * @uses bbp_parse_args()
  18. * @uses bbp_get_topic_post_type()
  19. * @uses wp_insert_post()
  20. * @uses update_post_meta()
  21. *
  22. * @param array $topic_data Forum post data
  23. * @param arrap $topic_meta Forum meta data
  24. */
  25. function bbp_insert_topic( $topic_data = array(), $topic_meta = array() ) {
  26. // Parse arguments against default values
  27. $topic_data = bbp_parse_args( $topic_data, array(
  28. 'post_parent' => 0, // forum ID
  29. 'post_status' => bbp_get_public_status_id(),
  30. 'post_type' => bbp_get_topic_post_type(),
  31. 'post_author' => bbp_get_current_user_id(),
  32. 'post_password' => '',
  33. 'post_content' => '',
  34. 'post_title' => '',
  35. 'comment_status' => 'closed',
  36. 'menu_order' => 0,
  37. ), 'insert_topic' );
  38. // Insert topic
  39. $topic_id = wp_insert_post( $topic_data );
  40. // Bail if no topic was added
  41. if ( empty( $topic_id ) )
  42. return false;
  43. // Parse arguments against default values
  44. $topic_meta = bbp_parse_args( $topic_meta, array(
  45. 'author_ip' => bbp_current_author_ip(),
  46. 'forum_id' => 0,
  47. 'topic_id' => $topic_id,
  48. 'voice_count' => 1,
  49. 'reply_count' => 0,
  50. 'reply_count_hidden' => 0,
  51. 'last_reply_id' => 0,
  52. 'last_active_id' => $topic_id,
  53. 'last_active_time' => get_post_field( 'post_date', $topic_id, 'db' ),
  54. ), 'insert_topic_meta' );
  55. // Insert topic meta
  56. foreach ( $topic_meta as $meta_key => $meta_value )
  57. update_post_meta( $topic_id, '_bbp_' . $meta_key, $meta_value );
  58. // Update the forum
  59. $forum_id = bbp_get_topic_forum_id( $topic_id );
  60. if ( !empty( $forum_id ) )
  61. bbp_update_forum( array( 'forum_id' => $forum_id ) );
  62. // Return new topic ID
  63. return $topic_id;
  64. }
  65. /** Post Form Handlers ********************************************************/
  66. /**
  67. * Handles the front end topic submission
  68. *
  69. * @param string $action The requested action to compare this function to
  70. * @uses bbp_add_error() To add an error message
  71. * @uses bbp_verify_nonce_request() To verify the nonce and check the referer
  72. * @uses bbp_is_anonymous() To check if an anonymous post is being made
  73. * @uses current_user_can() To check if the current user can publish topic
  74. * @uses bbp_get_current_user_id() To get the current user id
  75. * @uses bbp_filter_anonymous_post_data() To filter anonymous data
  76. * @uses bbp_set_current_anonymous_user_data() To set the anonymous user cookies
  77. * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
  78. * @uses esc_attr() For sanitization
  79. * @uses bbp_is_forum_category() To check if the forum is a category
  80. * @uses bbp_is_forum_closed() To check if the forum is closed
  81. * @uses bbp_is_forum_private() To check if the forum is private
  82. * @uses bbp_check_for_flood() To check for flooding
  83. * @uses bbp_check_for_duplicate() To check for duplicates
  84. * @uses bbp_get_topic_post_type() To get the topic post type
  85. * @uses remove_filter() To remove kses filters if needed
  86. * @uses apply_filters() Calls 'bbp_new_topic_pre_title' with the content
  87. * @uses apply_filters() Calls 'bbp_new_topic_pre_content' with the content
  88. * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
  89. * @uses wp_insert_post() To insert the topic
  90. * @uses do_action() Calls 'bbp_new_topic' with the topic id, forum id,
  91. * anonymous data and reply author
  92. * @uses bbp_stick_topic() To stick or super stick the topic
  93. * @uses bbp_unstick_topic() To unstick the topic
  94. * @uses bbp_get_topic_permalink() To get the topic permalink
  95. * @uses wp_safe_redirect() To redirect to the topic link
  96. * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
  97. * messages
  98. */
  99. function bbp_new_topic_handler( $action = '' ) {
  100. // Bail if action is not bbp-new-topic
  101. if ( 'bbp-new-topic' !== $action )
  102. return;
  103. // Nonce check
  104. if ( ! bbp_verify_nonce_request( 'bbp-new-topic' ) ) {
  105. bbp_add_error( 'bbp_new_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
  106. return;
  107. }
  108. // Define local variable(s)
  109. $view_all = false;
  110. $forum_id = $topic_author = $anonymous_data = 0;
  111. $topic_title = $topic_content = '';
  112. $terms = array( bbp_get_topic_tag_tax_id() => array() );
  113. /** Topic Author **********************************************************/
  114. // User is anonymous
  115. if ( bbp_is_anonymous() ) {
  116. // Filter anonymous data
  117. $anonymous_data = bbp_filter_anonymous_post_data();
  118. // Anonymous data checks out, so set cookies, etc...
  119. if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
  120. bbp_set_current_anonymous_user_data( $anonymous_data );
  121. }
  122. // User is logged in
  123. } else {
  124. // User cannot create topics
  125. if ( !current_user_can( 'publish_topics' ) ) {
  126. bbp_add_error( 'bbp_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new topics.', 'bbpress' ) );
  127. return;
  128. }
  129. // Topic author is current user
  130. $topic_author = bbp_get_current_user_id();
  131. }
  132. // Remove kses filters from title and content for capable users and if the nonce is verified
  133. if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && wp_create_nonce( 'bbp-unfiltered-html-topic_new' ) == $_POST['_bbp_unfiltered_html_topic'] ) {
  134. remove_filter( 'bbp_new_topic_pre_title', 'wp_filter_kses' );
  135. remove_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad', 10 );
  136. remove_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 );
  137. }
  138. /** Topic Title ***********************************************************/
  139. if ( !empty( $_POST['bbp_topic_title'] ) )
  140. $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) );
  141. // Filter and sanitize
  142. $topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
  143. // No topic title
  144. if ( empty( $topic_title ) )
  145. bbp_add_error( 'bbp_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
  146. /** Topic Content *********************************************************/
  147. if ( !empty( $_POST['bbp_topic_content'] ) )
  148. $topic_content = $_POST['bbp_topic_content'];
  149. // Filter and sanitize
  150. $topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
  151. // No topic content
  152. if ( empty( $topic_content ) )
  153. bbp_add_error( 'bbp_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
  154. /** Topic Forum ***********************************************************/
  155. // Forum id was not passed
  156. if ( empty( $_POST['bbp_forum_id'] ) ) {
  157. bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
  158. // Forum id was passed
  159. } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) {
  160. $forum_id = (int) $_POST['bbp_forum_id'];
  161. }
  162. // Forum exists
  163. if ( !empty( $forum_id ) ) {
  164. // Forum is a category
  165. if ( bbp_is_forum_category( $forum_id ) ) {
  166. bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in this forum.', 'bbpress' ) );
  167. // Forum is not a category
  168. } else {
  169. // Forum is closed and user cannot access
  170. if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) ) {
  171. bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
  172. }
  173. // Forum is private and user cannot access
  174. if ( bbp_is_forum_private( $forum_id ) ) {
  175. if ( !current_user_can( 'read_private_forums' ) ) {
  176. bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
  177. }
  178. // Forum is hidden and user cannot access
  179. } elseif ( bbp_is_forum_hidden( $forum_id ) ) {
  180. if ( !current_user_can( 'read_hidden_forums' ) ) {
  181. bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
  182. }
  183. }
  184. }
  185. }
  186. /** Topic Flooding ********************************************************/
  187. if ( !bbp_check_for_flood( $anonymous_data, $topic_author ) )
  188. bbp_add_error( 'bbp_topic_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
  189. /** Topic Duplicate *******************************************************/
  190. if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_topic_post_type(), 'post_author' => $topic_author, 'post_content' => $topic_content, 'anonymous_data' => $anonymous_data ) ) )
  191. bbp_add_error( 'bbp_topic_duplicate', __( '<strong>ERROR</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that!', 'bbpress' ) );
  192. /** Topic Blacklist *******************************************************/
  193. if ( !bbp_check_for_blacklist( $anonymous_data, $topic_author, $topic_title, $topic_content ) )
  194. bbp_add_error( 'bbp_topic_blacklist', __( '<strong>ERROR</strong>: Your topic cannot be created at this time.', 'bbpress' ) );
  195. /** Topic Status **********************************************************/
  196. // Maybe put into moderation
  197. if ( !bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
  198. $topic_status = bbp_get_pending_status_id();
  199. // Default to published
  200. } else {
  201. $topic_status = bbp_get_public_status_id();
  202. }
  203. /** Topic Tags ************************************************************/
  204. if ( bbp_allow_topic_tags() && !empty( $_POST['bbp_topic_tags'] ) ) {
  205. // Escape tag input
  206. $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
  207. // Explode by comma
  208. if ( strstr( $terms, ',' ) ) {
  209. $terms = explode( ',', $terms );
  210. }
  211. // Add topic tag ID as main key
  212. $terms = array( bbp_get_topic_tag_tax_id() => $terms );
  213. }
  214. /** Additional Actions (Before Save) **************************************/
  215. do_action( 'bbp_new_topic_pre_extras', $forum_id );
  216. // Bail if errors
  217. if ( bbp_has_errors() )
  218. return;
  219. /** No Errors *************************************************************/
  220. // Add the content of the form to $topic_data as an array.
  221. // Just in time manipulation of topic data before being created
  222. $topic_data = apply_filters( 'bbp_new_topic_pre_insert', array(
  223. 'post_author' => $topic_author,
  224. 'post_title' => $topic_title,
  225. 'post_content' => $topic_content,
  226. 'post_status' => $topic_status,
  227. 'post_parent' => $forum_id,
  228. 'post_type' => bbp_get_topic_post_type(),
  229. 'tax_input' => $terms,
  230. 'comment_status' => 'closed'
  231. ) );
  232. // Insert topic
  233. $topic_id = wp_insert_post( $topic_data );
  234. /** No Errors *************************************************************/
  235. if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
  236. /** Trash Check *******************************************************/
  237. // If the forum is trash, or the topic_status is switched to
  238. // trash, trash it properly
  239. if ( ( get_post_field( 'post_status', $forum_id ) == bbp_get_trash_status_id() ) || ( $topic_data['post_status'] == bbp_get_trash_status_id() ) ) {
  240. // Trash the reply
  241. wp_trash_post( $topic_id );
  242. // Force view=all
  243. $view_all = true;
  244. }
  245. /** Spam Check ********************************************************/
  246. // If reply or topic are spam, officially spam this reply
  247. if ( $topic_data['post_status'] == bbp_get_spam_status_id() ) {
  248. add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
  249. // Force view=all
  250. $view_all = true;
  251. }
  252. /** Update counts, etc... *********************************************/
  253. do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
  254. /** Stickies **********************************************************/
  255. // Sticky check after 'bbp_new_topic' action so forum ID meta is set
  256. if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) {
  257. // What's the haps?
  258. switch ( $_POST['bbp_stick_topic'] ) {
  259. // Sticky in this forum
  260. case 'stick' :
  261. bbp_stick_topic( $topic_id );
  262. break;
  263. // Super sticky in all forums
  264. case 'super' :
  265. bbp_stick_topic( $topic_id, true );
  266. break;
  267. // We can avoid this as it is a new topic
  268. case 'unstick' :
  269. default :
  270. break;
  271. }
  272. }
  273. /** Additional Actions (After Save) ***********************************/
  274. do_action( 'bbp_new_topic_post_extras', $topic_id );
  275. /** Redirect **********************************************************/
  276. // Redirect to
  277. $redirect_to = bbp_get_redirect_to();
  278. // Get the topic URL
  279. $redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
  280. // Add view all?
  281. if ( bbp_get_view_all() || !empty( $view_all ) ) {
  282. // User can moderate, so redirect to topic with view all set
  283. if ( current_user_can( 'moderate' ) ) {
  284. $redirect_url = bbp_add_view_all( $redirect_url );
  285. // User cannot moderate, so redirect to forum
  286. } else {
  287. $redirect_url = bbp_get_forum_permalink( $forum_id );
  288. }
  289. }
  290. // Allow to be filtered
  291. $redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id );
  292. /** Successful Save ***************************************************/
  293. // Redirect back to new topic
  294. wp_safe_redirect( $redirect_url );
  295. // For good measure
  296. exit();
  297. // Errors
  298. } else {
  299. $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
  300. bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error, 'bbpress' ) );
  301. }
  302. }
  303. /**
  304. * Handles the front end edit topic submission
  305. *
  306. * @param string $action The requested action to compare this function to
  307. * @uses bbp_add_error() To add an error message
  308. * @uses bbp_get_topic() To get the topic
  309. * @uses bbp_verify_nonce_request() To verify the nonce and check the request
  310. * @uses bbp_is_topic_anonymous() To check if topic is by an anonymous user
  311. * @uses current_user_can() To check if the current user can edit the topic
  312. * @uses bbp_filter_anonymous_post_data() To filter anonymous data
  313. * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
  314. * @uses esc_attr() For sanitization
  315. * @uses bbp_is_forum_category() To check if the forum is a category
  316. * @uses bbp_is_forum_closed() To check if the forum is closed
  317. * @uses bbp_is_forum_private() To check if the forum is private
  318. * @uses remove_filter() To remove kses filters if needed
  319. * @uses apply_filters() Calls 'bbp_edit_topic_pre_title' with the title and
  320. * topic id
  321. * @uses apply_filters() Calls 'bbp_edit_topic_pre_content' with the content
  322. * and topic id
  323. * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
  324. * @uses wp_save_post_revision() To save a topic revision
  325. * @uses bbp_update_topic_revision_log() To update the topic revision log
  326. * @uses bbp_stick_topic() To stick or super stick the topic
  327. * @uses bbp_unstick_topic() To unstick the topic
  328. * @uses wp_update_post() To update the topic
  329. * @uses do_action() Calls 'bbp_edit_topic' with the topic id, forum id,
  330. * anonymous data and reply author
  331. * @uses bbp_move_topic_handler() To handle movement of a topic from one forum
  332. * to another
  333. * @uses bbp_get_topic_permalink() To get the topic permalink
  334. * @uses wp_safe_redirect() To redirect to the topic link
  335. * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
  336. * messages
  337. */
  338. function bbp_edit_topic_handler( $action = '' ) {
  339. // Bail if action is not bbp-edit-topic
  340. if ( 'bbp-edit-topic' !== $action )
  341. return;
  342. // Define local variable(s)
  343. $revisions_removed = false;
  344. $topic = $topic_id = $topic_author = $forum_id = $anonymous_data = 0;
  345. $topic_title = $topic_content = $topic_edit_reason = '';
  346. /** Topic *****************************************************************/
  347. // Topic id was not passed
  348. if ( empty( $_POST['bbp_topic_id'] ) ) {
  349. bbp_add_error( 'bbp_edit_topic_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
  350. return;
  351. // Topic id was passed
  352. } elseif ( is_numeric( $_POST['bbp_topic_id'] ) ) {
  353. $topic_id = (int) $_POST['bbp_topic_id'];
  354. $topic = bbp_get_topic( $topic_id );
  355. }
  356. // Topic does not exist
  357. if ( empty( $topic ) ) {
  358. bbp_add_error( 'bbp_edit_topic_not_found', __( '<strong>ERROR</strong>: The topic you want to edit was not found.', 'bbpress' ) );
  359. return;
  360. // Topic exists
  361. } else {
  362. // Check users ability to create new topic
  363. if ( ! bbp_is_topic_anonymous( $topic_id ) ) {
  364. // User cannot edit this topic
  365. if ( !current_user_can( 'edit_topic', $topic_id ) ) {
  366. bbp_add_error( 'bbp_edit_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that topic.', 'bbpress' ) );
  367. }
  368. // Set topic author
  369. $topic_author = bbp_get_topic_author_id( $topic_id );
  370. // It is an anonymous post
  371. } else {
  372. // Filter anonymous data
  373. $anonymous_data = bbp_filter_anonymous_post_data( array(), true );
  374. }
  375. }
  376. // Nonce check
  377. if ( ! bbp_verify_nonce_request( 'bbp-edit-topic_' . $topic_id ) ) {
  378. bbp_add_error( 'bbp_edit_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
  379. return;
  380. }
  381. // Remove kses filters from title and content for capable users and if the nonce is verified
  382. if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-topic_' . $topic_id ) == $_POST['_bbp_unfiltered_html_topic'] ) ) {
  383. remove_filter( 'bbp_edit_topic_pre_title', 'wp_filter_kses' );
  384. remove_filter( 'bbp_edit_topic_pre_content', 'bbp_encode_bad', 10 );
  385. remove_filter( 'bbp_edit_topic_pre_content', 'bbp_filter_kses', 30 );
  386. }
  387. /** Topic Forum ***********************************************************/
  388. // Forum id was not passed
  389. if ( empty( $_POST['bbp_forum_id'] ) ) {
  390. bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
  391. // Forum id was passed
  392. } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) {
  393. $forum_id = (int) $_POST['bbp_forum_id'];
  394. }
  395. // Current forum this topic is in
  396. $current_forum_id = bbp_get_topic_forum_id( $topic_id );
  397. // Forum exists
  398. if ( !empty( $forum_id ) && ( $forum_id !== $current_forum_id ) ) {
  399. // Forum is a category
  400. if ( bbp_is_forum_category( $forum_id ) ) {
  401. bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in it.', 'bbpress' ) );
  402. // Forum is not a category
  403. } else {
  404. // Forum is closed and user cannot access
  405. if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) ) {
  406. bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
  407. }
  408. // Forum is private and user cannot access
  409. if ( bbp_is_forum_private( $forum_id ) ) {
  410. if ( !current_user_can( 'read_private_forums' ) ) {
  411. bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
  412. }
  413. // Forum is hidden and user cannot access
  414. } elseif ( bbp_is_forum_hidden( $forum_id ) ) {
  415. if ( !current_user_can( 'read_hidden_forums' ) ) {
  416. bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
  417. }
  418. }
  419. }
  420. }
  421. /** Topic Title ***********************************************************/
  422. if ( !empty( $_POST['bbp_topic_title'] ) )
  423. $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) );
  424. // Filter and sanitize
  425. $topic_title = apply_filters( 'bbp_edit_topic_pre_title', $topic_title, $topic_id );
  426. // No topic title
  427. if ( empty( $topic_title ) )
  428. bbp_add_error( 'bbp_edit_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
  429. /** Topic Content *********************************************************/
  430. if ( !empty( $_POST['bbp_topic_content'] ) )
  431. $topic_content = $_POST['bbp_topic_content'];
  432. // Filter and sanitize
  433. $topic_content = apply_filters( 'bbp_edit_topic_pre_content', $topic_content, $topic_id );
  434. // No topic content
  435. if ( empty( $topic_content ) )
  436. bbp_add_error( 'bbp_edit_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
  437. /** Topic Blacklist *******************************************************/
  438. if ( !bbp_check_for_blacklist( $anonymous_data, $topic_author, $topic_title, $topic_content ) )
  439. bbp_add_error( 'bbp_topic_blacklist', __( '<strong>ERROR</strong>: Your topic cannot be edited at this time.', 'bbpress' ) );
  440. /** Topic Status **********************************************************/
  441. // Maybe put into moderation
  442. if ( !bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
  443. // Set post status to pending if public or closed
  444. if ( in_array( $topic->post_status, array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ) ) {
  445. $topic_status = bbp_get_pending_status_id();
  446. }
  447. // Use existing post_status
  448. } else {
  449. $topic_status = $topic->post_status;
  450. }
  451. /** Topic Tags ************************************************************/
  452. // Either replace terms
  453. if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags' ) && ! empty( $_POST['bbp_topic_tags'] ) ) {
  454. // Escape tag input
  455. $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
  456. // Explode by comma
  457. if ( strstr( $terms, ',' ) )
  458. $terms = explode( ',', $terms );
  459. // Add topic tag ID as main key
  460. $terms = array( bbp_get_topic_tag_tax_id() => $terms );
  461. // ...or remove them.
  462. } elseif ( isset( $_POST['bbp_topic_tags'] ) ) {
  463. $terms = array( bbp_get_topic_tag_tax_id() => array() );
  464. // Existing terms
  465. } else {
  466. $terms = array( bbp_get_topic_tag_tax_id() => explode( ',', bbp_get_topic_tag_names( $topic_id, ',' ) ) );
  467. }
  468. /** Additional Actions (Before Save) **************************************/
  469. do_action( 'bbp_edit_topic_pre_extras', $topic_id );
  470. // Bail if errors
  471. if ( bbp_has_errors() )
  472. return;
  473. /** No Errors *************************************************************/
  474. // Add the content of the form to $topic_data as an array
  475. // Just in time manipulation of topic data before being edited
  476. $topic_data = apply_filters( 'bbp_edit_topic_pre_insert', array(
  477. 'ID' => $topic_id,
  478. 'post_title' => $topic_title,
  479. 'post_content' => $topic_content,
  480. 'post_status' => $topic_status,
  481. 'post_parent' => $forum_id,
  482. 'post_author' => $topic_author,
  483. 'post_type' => bbp_get_topic_post_type(),
  484. 'tax_input' => $terms,
  485. ) );
  486. // Toggle revisions to avoid duplicates
  487. if ( post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) {
  488. $revisions_removed = true;
  489. remove_post_type_support( bbp_get_topic_post_type(), 'revisions' );
  490. }
  491. // Insert topic
  492. $topic_id = wp_update_post( $topic_data );
  493. // Toggle revisions back on
  494. if ( true === $revisions_removed ) {
  495. $revisions_removed = false;
  496. add_post_type_support( bbp_get_topic_post_type(), 'revisions' );
  497. }
  498. /** No Errors *************************************************************/
  499. if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
  500. // Update counts, etc...
  501. do_action( 'bbp_edit_topic', $topic_id, $forum_id, $anonymous_data, $topic_author , true /* Is edit */ );
  502. /** Revisions *********************************************************/
  503. // Revision Reason
  504. if ( !empty( $_POST['bbp_topic_edit_reason'] ) ) {
  505. $topic_edit_reason = esc_attr( strip_tags( $_POST['bbp_topic_edit_reason'] ) );
  506. }
  507. // Update revision log
  508. if ( !empty( $_POST['bbp_log_topic_edit'] ) && ( 1 == $_POST['bbp_log_topic_edit'] ) ) {
  509. $revision_id = wp_save_post_revision( $topic_id );
  510. if ( ! empty( $revision_id ) ) {
  511. bbp_update_topic_revision_log( array(
  512. 'topic_id' => $topic_id,
  513. 'revision_id' => $revision_id,
  514. 'author_id' => bbp_get_current_user_id(),
  515. 'reason' => $topic_edit_reason
  516. ) );
  517. }
  518. }
  519. /** Move Topic ********************************************************/
  520. // If the new forum id is not equal to the old forum id, run the
  521. // bbp_move_topic action and pass the topic's forum id as the
  522. // first arg and topic id as the second to update counts.
  523. if ( $forum_id != $topic->post_parent ) {
  524. bbp_move_topic_handler( $topic_id, $topic->post_parent, $forum_id );
  525. }
  526. /** Stickies **********************************************************/
  527. if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) {
  528. // What's the dilly?
  529. switch ( $_POST['bbp_stick_topic'] ) {
  530. // Sticky in forum
  531. case 'stick' :
  532. bbp_stick_topic( $topic_id );
  533. break;
  534. // Sticky in all forums
  535. case 'super' :
  536. bbp_stick_topic( $topic_id, true );
  537. break;
  538. // Normal
  539. case 'unstick' :
  540. default :
  541. bbp_unstick_topic( $topic_id );
  542. break;
  543. }
  544. }
  545. /** Additional Actions (After Save) ***********************************/
  546. do_action( 'bbp_edit_topic_post_extras', $topic_id );
  547. /** Redirect **********************************************************/
  548. // Redirect to
  549. $redirect_to = bbp_get_redirect_to();
  550. // View all?
  551. $view_all = bbp_get_view_all();
  552. // Get the topic URL
  553. $topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
  554. // Add view all?
  555. if ( !empty( $view_all ) )
  556. $topic_url = bbp_add_view_all( $topic_url );
  557. // Allow to be filtered
  558. $topic_url = apply_filters( 'bbp_edit_topic_redirect_to', $topic_url, $view_all, $redirect_to );
  559. /** Successful Edit ***************************************************/
  560. // Redirect back to new topic
  561. wp_safe_redirect( $topic_url );
  562. // For good measure
  563. exit();
  564. /** Errors ****************************************************************/
  565. } else {
  566. $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
  567. bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error . 'Please try again.', 'bbpress' ) );
  568. }
  569. }
  570. /**
  571. * Handle all the extra meta stuff from posting a new topic
  572. *
  573. * @param int $topic_id Optional. Topic id
  574. * @param int $forum_id Optional. Forum id
  575. * @param bool|array $anonymous_data Optional logged-out user data.
  576. * @param int $author_id Author id
  577. * @param bool $is_edit Optional. Is the post being edited? Defaults to false.
  578. * @uses bbp_get_topic_id() To get the topic id
  579. * @uses bbp_get_forum_id() To get the forum id
  580. * @uses bbp_get_current_user_id() To get the current user id
  581. * @yses bbp_get_topic_forum_id() To get the topic forum id
  582. * @uses update_post_meta() To update the topic metas
  583. * @uses set_transient() To update the flood check transient for the ip
  584. * @uses bbp_update_user_last_posted() To update the users last posted time
  585. * @uses bbp_is_subscriptions_active() To check if the subscriptions feature is
  586. * activated or not
  587. * @uses bbp_is_user_subscribed() To check if the user is subscribed
  588. * @uses bbp_remove_user_subscription() To remove the user's subscription
  589. * @uses bbp_add_user_subscription() To add the user's subscription
  590. * @uses bbp_update_topic_forum_id() To update the topic's forum id
  591. * @uses bbp_update_topic_topic_id() To update the topic's topic id
  592. * @uses bbp_update_topic_last_reply_id() To update the last reply id topic meta
  593. * @uses bbp_update_topic_last_active_id() To update the topic last active id
  594. * @uses bbp_update_topic_last_active_time() To update the last active topic meta
  595. * @uses bbp_update_topic_reply_count() To update the topic reply count
  596. * @uses bbp_update_topic_reply_count_hidden() To udpate the topic hidden reply count
  597. * @uses bbp_update_topic_voice_count() To update the topic voice count
  598. * @uses bbp_update_topic_walker() To udpate the topic's ancestors
  599. */
  600. function bbp_update_topic( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) {
  601. // Validate the ID's passed from 'bbp_new_topic' action
  602. $topic_id = bbp_get_topic_id( $topic_id );
  603. $forum_id = bbp_get_forum_id( $forum_id );
  604. // Bail if there is no topic
  605. if ( empty( $topic_id ) )
  606. return;
  607. // Check author_id
  608. if ( empty( $author_id ) )
  609. $author_id = bbp_get_current_user_id();
  610. // Check forum_id
  611. if ( empty( $forum_id ) )
  612. $forum_id = bbp_get_topic_forum_id( $topic_id );
  613. // If anonymous post, store name, email, website and ip in post_meta.
  614. // It expects anonymous_data to be sanitized.
  615. // Check bbp_filter_anonymous_post_data() for sanitization.
  616. if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
  617. // Parse arguments against default values
  618. $r = bbp_parse_args( $anonymous_data, array(
  619. 'bbp_anonymous_name' => '',
  620. 'bbp_anonymous_email' => '',
  621. 'bbp_anonymous_website' => '',
  622. ), 'update_topic' );
  623. // Update all anonymous metas
  624. foreach( $r as $anon_key => $anon_value ) {
  625. update_post_meta( $topic_id, '_' . $anon_key, (string) $anon_value, false );
  626. }
  627. // Set transient for throttle check (only on new, not edit)
  628. if ( empty( $is_edit ) ) {
  629. set_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted', time() );
  630. }
  631. } else {
  632. if ( empty( $is_edit ) && !current_user_can( 'throttle' ) ) {
  633. bbp_update_user_last_posted( $author_id );
  634. }
  635. }
  636. // Handle Subscription Checkbox
  637. if ( bbp_is_subscriptions_active() && !empty( $author_id ) ) {
  638. $subscribed = bbp_is_user_subscribed( $author_id, $topic_id );
  639. $subscheck = ( !empty( $_POST['bbp_topic_subscription'] ) && ( 'bbp_subscribe' == $_POST['bbp_topic_subscription'] ) ) ? true : false;
  640. // Subscribed and unsubscribing
  641. if ( true == $subscribed && false == $subscheck ) {
  642. bbp_remove_user_subscription( $author_id, $topic_id );
  643. // Subscribing
  644. } elseif ( false == $subscribed && true == $subscheck ) {
  645. bbp_add_user_subscription( $author_id, $topic_id );
  646. }
  647. }
  648. // Forum topic meta
  649. bbp_update_topic_forum_id( $topic_id, $forum_id );
  650. bbp_update_topic_topic_id( $topic_id, $topic_id );
  651. // Update associated topic values if this is a new topic
  652. if ( empty( $is_edit ) ) {
  653. // Update poster IP if not editing
  654. update_post_meta( $topic_id, '_bbp_author_ip', bbp_current_author_ip(), false );
  655. // Last active time
  656. $last_active = current_time( 'mysql' );
  657. // Reply topic meta
  658. bbp_update_topic_last_reply_id ( $topic_id, 0 );
  659. bbp_update_topic_last_active_id ( $topic_id, $topic_id );
  660. bbp_update_topic_last_active_time ( $topic_id, $last_active );
  661. bbp_update_topic_reply_count ( $topic_id, 0 );
  662. bbp_update_topic_reply_count_hidden ( $topic_id, 0 );
  663. bbp_update_topic_voice_count ( $topic_id );
  664. // Walk up ancestors and do the dirty work
  665. bbp_update_topic_walker( $topic_id, $last_active, $forum_id, 0, false );
  666. }
  667. }
  668. /**
  669. * Walks up the post_parent tree from the current topic_id, and updates the
  670. * counts of forums above it. This calls a few internal functions that all run
  671. * manual queries against the database to get their results. As such, this
  672. * function can be costly to run but is necessary to keep everything accurate.
  673. *
  674. * @since bbPress (r2800)
  675. * @param int $topic_id Topic id
  676. * @param string $last_active_time Optional. Last active time
  677. * @param int $forum_id Optional. Forum id
  678. * @param int $reply_id Optional. Reply id
  679. * @param bool $refresh Reset all the previous parameters? Defaults to true.
  680. * @uses bbp_get_topic_id() To get the topic id
  681. * @uses bbp_get_topic_forum_id() To get the topic forum id
  682. * @uses get_post_ancestors() To get the topic's ancestors
  683. * @uses bbp_is_forum() To check if the ancestor is a forum
  684. * @uses bbp_update_forum() To update the forum
  685. */
  686. function bbp_update_topic_walker( $topic_id, $last_active_time = '', $forum_id = 0, $reply_id = 0, $refresh = true ) {
  687. // Validate topic_id
  688. $topic_id = bbp_get_topic_id( $topic_id );
  689. // Define local variable(s)
  690. $active_id = 0;
  691. // Topic was passed
  692. if ( !empty( $topic_id ) ) {
  693. // Get the forum ID if none was passed
  694. if ( empty( $forum_id ) ) {
  695. $forum_id = bbp_get_topic_forum_id( $topic_id );
  696. }
  697. // Set the active_id based on topic_id/reply_id
  698. $active_id = empty( $reply_id ) ? $topic_id : $reply_id;
  699. }
  700. // Get topic ancestors
  701. $ancestors = array_values( array_unique( array_merge( array( $forum_id ), (array) get_post_ancestors( $topic_id ) ) ) );
  702. // Topic status
  703. $topic_status = get_post_status( $topic_id );
  704. // If we want a full refresh, unset any of the possibly passed variables
  705. if ( true == $refresh ) {
  706. $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
  707. $topic_status = bbp_get_public_status_id();
  708. }
  709. // Loop through ancestors
  710. if ( !empty( $ancestors ) ) {
  711. foreach ( $ancestors as $ancestor ) {
  712. // If ancestor is a forum, update counts
  713. if ( bbp_is_forum( $ancestor ) ) {
  714. // Update the forum
  715. bbp_update_forum( array(
  716. 'forum_id' => $ancestor,
  717. 'last_topic_id' => $topic_id,
  718. 'last_reply_id' => $reply_id,
  719. 'last_active_id' => $active_id,
  720. 'last_active_time' => 0,
  721. 'last_active_status' => $topic_status
  722. ) );
  723. }
  724. }
  725. }
  726. }
  727. /**
  728. * Handle the moving of a topic from one forum to another. This includes walking
  729. * up the old and new branches and updating the counts.
  730. *
  731. * @param int $topic_id Topic id
  732. * @param int $old_forum_id Old forum id
  733. * @param int $new_forum_id New forum id
  734. * @uses bbp_get_topic_id() To get the topic id
  735. * @uses bbp_get_forum_id() To get the forum id
  736. * @uses bbp_get_stickies() To get the old forums sticky topics
  737. * @uses delete_post_meta() To delete the forum sticky meta
  738. * @uses update_post_meta() To update the old forum sticky meta
  739. * @uses bbp_stick_topic() To stick the topic in the new forum
  740. * @uses bbp_get_reply_post_type() To get the reply post type
  741. * @uses bbp_get_all_child_ids() To get the public child ids
  742. * @uses bbp_update_reply_forum_id() To update the reply forum id
  743. * @uses bbp_update_topic_forum_id() To update the topic forum id
  744. * @uses get_post_ancestors() To get the topic's ancestors
  745. * @uses bbp_is_forum() To check if the ancestor is a forum
  746. * @uses bbp_update_forum() To update the forum
  747. */
  748. function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) {
  749. // Validate parameters
  750. $topic_id = bbp_get_topic_id( $topic_id );
  751. $old_forum_id = bbp_get_forum_id( $old_forum_id );
  752. $new_forum_id = bbp_get_forum_id( $new_forum_id );
  753. // Update topic forum's ID
  754. bbp_update_topic_forum_id( $topic_id, $new_forum_id );
  755. /** Stickies **************************************************************/
  756. // Get forum stickies
  757. $old_stickies = bbp_get_stickies( $old_forum_id );
  758. // Only proceed if stickies are found
  759. if ( !empty( $old_stickies ) ) {
  760. // Define local variables
  761. $updated_stickies = array();
  762. // Loop through stickies of forum and add misses to the updated array
  763. foreach ( (array) $old_stickies as $sticky_topic_id ) {
  764. if ( $topic_id != $sticky_topic_id ) {
  765. $updated_stickies[] = $sticky_topic_id;
  766. }
  767. }
  768. // If stickies are different, update or delete them
  769. if ( $updated_stickies != $old_stickies ) {
  770. // No more stickies so delete the meta
  771. if ( empty( $updated_stickies ) ) {
  772. delete_post_meta( $old_forum_id, '_bbp_sticky_topics' );
  773. // Still stickies so update the meta
  774. } else {
  775. update_post_meta( $old_forum_id, '_bbp_sticky_topics', $updated_stickies );
  776. }
  777. // Topic was sticky, so restick in new forum
  778. bbp_stick_topic( $topic_id );
  779. }
  780. }
  781. /** Topic Replies *********************************************************/
  782. // Get the topics replies
  783. $replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() );
  784. // Update the forum_id of all replies in the topic
  785. foreach ( $replies as $reply_id ) {
  786. bbp_update_reply_forum_id( $reply_id, $new_forum_id );
  787. }
  788. /** Old forum_id **********************************************************/
  789. // Get topic ancestors
  790. $old_forum_ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), (array) get_post_ancestors( $old_forum_id ) ) ) );
  791. // Loop through ancestors and update them
  792. if ( !empty( $old_forum_ancestors ) ) {
  793. foreach ( $old_forum_ancestors as $ancestor ) {
  794. if ( bbp_is_forum( $ancestor ) ) {
  795. bbp_update_forum( array(
  796. 'forum_id' => $ancestor,
  797. ) );
  798. }
  799. }
  800. }
  801. /** New forum_id **********************************************************/
  802. // Make sure we're not walking twice
  803. if ( !in_array( $new_forum_id, $old_forum_ancestors ) ) {
  804. // Get topic ancestors
  805. $new_forum_ancestors = array_values( array_unique( array_merge( array( $new_forum_id ), (array) get_post_ancestors( $new_forum_id ) ) ) );
  806. // Make sure we're not walking twice
  807. $new_forum_ancestors = array_diff( $new_forum_ancestors, $old_forum_ancestors );
  808. // Loop through ancestors and update them
  809. if ( !empty( $new_forum_ancestors ) ) {
  810. foreach ( $new_forum_ancestors as $ancestor ) {
  811. if ( bbp_is_forum( $ancestor ) ) {
  812. bbp_update_forum( array(
  813. 'forum_id' => $ancestor,
  814. ) );
  815. }
  816. }
  817. }
  818. }
  819. }
  820. /**
  821. * Merge topic handler
  822. *
  823. * Handles the front end merge topic submission
  824. *
  825. * @since bbPress (r2756)
  826. *
  827. * @param string $action The requested action to compare this function to
  828. * @uses bbp_add_error() To add an error message
  829. * @uses bbp_get_topic() To get the topics
  830. * @uses bbp_verify_nonce_request() To verify the nonce and check the request
  831. * @uses current_user_can() To check if the current user can edit the topics
  832. * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
  833. * @uses do_action() Calls 'bbp_merge_topic' with the destination and source
  834. * topic ids
  835. * @uses bbp_get_topic_subscribers() To get the source topic subscribers
  836. * @uses bbp_add_user_subscription() To add the user subscription
  837. * @uses bbp_remove_user_subscription() To remove the user subscription
  838. * @uses bbp_get_topic_favoriters() To get the source topic favoriters
  839. * @uses bbp_add_user_favorite() To add the user favorite
  840. * @uses bbp_remove_user_favorite() To remove the user favorite
  841. * @uses wp_get_post_terms() To get the source topic tags
  842. * @uses wp_set_post_terms() To set the topic tags
  843. * @uses wp_delete_object_term_relationships() To delete the topic tags
  844. * @uses bbp_open_topic() To open the topic
  845. * @uses bbp_unstick_topic() To unstick the topic
  846. * @uses bbp_get_reply_post_type() To get the reply post type
  847. * @uses get_posts() To get the replies
  848. * @uses wp_update_post() To update the topic
  849. * @uses bbp_update_reply_topic_id() To update the reply topic id
  850. * @uses bbp_get_topic_forum_id() To get the topic forum id
  851. * @uses bbp_update_reply_forum_id() To update the reply forum id
  852. * @uses do_action() Calls 'bbp_merged_topic_reply' with the reply id and
  853. * destination topic id
  854. * @uses do_action() Calls 'bbp_merged_topic' with the destination and source
  855. * topic ids and source topic's forum id
  856. * @uses bbp_get_topic_permalink() To get the topic permalink
  857. * @uses wp_safe_redirect() To redirect to the topic link
  858. */
  859. function bbp_merge_topic_handler( $action = '' ) {
  860. // Bail if action is not bbp-merge-topic
  861. if ( 'bbp-merge-topic' !== $action )
  862. return;
  863. // Define local variable(s)
  864. $source_topic_id = $destination_topic_id = 0;
  865. $source_topic = $destination_topic = 0;
  866. $subscribers = $favoriters = $replies = array();
  867. /** Source Topic **********************************************************/
  868. // Topic id
  869. if ( empty( $_POST['bbp_topic_id'] ) ) {
  870. bbp_add_error( 'bbp_merge_topic_source_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
  871. } else {
  872. $source_topic_id = (int) $_POST['bbp_topic_id'];
  873. }
  874. // Nonce check
  875. if ( ! bbp_verify_nonce_request( 'bbp-merge-topic_' . $source_topic_id ) ) {
  876. bbp_add_error( 'bbp_merge_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
  877. return;
  878. // Source topic not found
  879. } elseif ( !$source_topic = bbp_get_topic( $source_topic_id ) ) {
  880. bbp_add_error( 'bbp_merge_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to merge was not found.', 'bbpress' ) );
  881. return;
  882. }
  883. // Cannot edit source topic
  884. if ( !current_user_can( 'edit_topic', $source_topic->ID ) ) {
  885. bbp_add_error( 'bbp_merge_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
  886. return;
  887. }
  888. /** Destination Topic *****************************************************/
  889. // Topic id
  890. if ( empty( $_POST['bbp_destination_topic'] ) )
  891. bbp_add_error( 'bbp_merge_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found.', 'bbpress' ) );
  892. else
  893. $destination_topic_id = (int) $_POST['bbp_destination_topic'];
  894. // Destination topic not found
  895. if ( !$destination_topic = bbp_get_topic( $destination_topic_id ) )
  896. bbp_add_error( 'bbp_merge_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to merge to was not found.', 'bbpress' ) );
  897. // Cannot edit destination topic
  898. if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
  899. bbp_add_error( 'bbp_merge_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic.', 'bbpress' ) );
  900. // Bail if errors
  901. if ( bbp_has_errors() )
  902. return;
  903. /** No Errors *************************************************************/
  904. // Update counts, etc...
  905. do_action( 'bbp_merge_topic', $destination_topic->ID, $source_topic->ID );
  906. /** Date Check ************************************************************/
  907. // Check if the destination topic is older than the source topic
  908. if ( strtotime( $source_topic->post_date ) < strtotime( $destination_topic->post_date ) ) {
  909. // Set destination topic post_date to 1 second before source topic
  910. $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $source_topic->post_date ) - 1 );
  911. $postarr = array(
  912. 'ID' => $destination_topic_id,
  913. 'post_date' => $destination_post_date,
  914. 'post_date_gmt' => get_gmt_from_date( $destination_post_date )
  915. );
  916. // Update destination topic
  917. wp_update_post( $postarr );
  918. }
  919. /** Subscriptions *********************************************************/
  920. // Get subscribers from source topic
  921. $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
  922. // Remove the topic from everybody's subscriptions
  923. if ( !empty( $subscribers ) ) {
  924. // Loop through each user
  925. foreach ( (array) $subscribers as $subscriber ) {
  926. // Shift the subscriber if told to
  927. if ( !empty( $_POST['bbp_topic_subscribers'] ) && ( 1 == $_POST['bbp_topic_subscribers'] ) && bbp_is_subscriptions_active() )
  928. bbp_add_user_subscription( $subscriber, $destination_topic->ID );
  929. // Remove old subscription
  930. bbp_remove_user_subscription( $subscriber, $source_topic->ID );
  931. }
  932. }
  933. /** Favorites *************************************************************/
  934. // Get favoriters from source topic
  935. $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
  936. // Remove the topic from everybody's favorites
  937. if ( !empty( $favoriters ) ) {
  938. // Loop through each user
  939. foreach ( (array) $favoriters as $favoriter ) {
  940. // Shift the favoriter if told to
  941. if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] )
  942. bbp_add_user_favorite( $favoriter, $destination_topic->ID );
  943. // Remove old favorite
  944. bbp_remove_user_favorite( $favoriter, $source_topic->ID );
  945. }
  946. }
  947. /** Tags ******************************************************************/
  948. // Get the source topic tags
  949. $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
  950. // Tags to possibly merge
  951. if ( !empty( $source_topic_tags ) && !is_wp_error( $source_topic_tags ) ) {
  952. // Shift the tags if told to
  953. if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) )
  954. wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
  955. // Delete the tags from the source topic
  956. wp_delete_object_term_relationships( $source_topic->ID, bbp_get_topic_tag_tax_id() );
  957. }
  958. /** Source Topic **********************************************************/
  959. // Status
  960. bbp_open_topic( $source_topic->ID );
  961. // Sticky
  962. bbp_unstick_topic( $source_topic->ID );
  963. // Get the replies of the source topic
  964. $replies = (array) get_posts( array(
  965. 'post_parent' => $source_topic->ID,
  966. 'post_type' => bbp_get_reply_post_type(),
  967. 'posts_per_page' => -1,
  968. 'order' => 'ASC'
  969. ) );
  970. // Prepend the source topic to its replies array for processing
  971. array_unshift( $replies, $source_topic );
  972. if ( !empty( $replies ) ) {
  973. /** Merge Replies *****************************************************/
  974. // Change the post_parent of each reply to the destination topic id
  975. foreach ( $replies as $reply ) {
  976. $postarr = array(
  977. 'ID' => $reply->ID,
  978. 'post_title' => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
  979. 'post_name' => false,
  980. 'post_type' => bbp_get_reply_post_type(),
  981. 'post_parent' => $destination_topic->ID,
  982. 'guid' => ''
  983. );
  984. wp_update_post( $postarr );
  985. // Adjust reply meta values
  986. bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID );
  987. bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
  988. // Do additional actions per merged reply
  989. do_action( 'bbp_merged_topic_reply', $reply->ID, $destination_topic->ID );
  990. }
  991. }
  992. /** Successful Merge ******************************************************/
  993. // Update topic's last meta data
  994. bbp_update_topic_last_reply_id ( $destination_topic->ID );
  995. bbp_update_topic_last_active_id ( $destination_topic->ID );
  996. bbp_update_topic_last_active_time( $destination_topic->ID );
  997. // Send the post parent of the source topic as it has been shifted
  998. // (possibly to a new forum) so we need to update the counts of the
  999. // old forum as well as the new one
  1000. do_action( 'bbp_merged_topic', $destination_topic->ID, $source_topic->ID, $source_topic->post_parent );
  1001. // Redirect back to new topic
  1002. wp_safe_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
  1003. // For good measure
  1004. exit();
  1005. }
  1006. /**
  1007. * Fix counts on topic merge
  1008. *
  1009. * When a topic is merged, update the counts of source and destination topic
  1010. * and their forums.
  1011. *
  1012. * @since bbPress (r2756)
  1013. *
  1014. * @param int $destination_topic_id Destination topic id
  1015. * @param int $source_topic_id Source topic id
  1016. * @param int $source_topic_forum Source topic's forum id
  1017. * @uses bbp_update_forum_topic_count() To update the forum topic counts
  1018. * @uses bbp_update_forum_reply_count() To update the forum reply counts
  1019. * @uses bbp_update_topic_reply_count() To update the topic reply counts
  1020. * @uses bbp_update_topic_voice_count() To update the topic voice counts
  1021. * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
  1022. * count
  1023. * @uses do_action() Calls 'bbp_merge_topic_count' with the destination topic
  1024. * id, source topic id & source topic forum id
  1025. */
  1026. function bbp_merge_topic_count( $destination_topic_id, $source_topic_id, $source_topic_forum_id ) {
  1027. /** Source Topic **********************************************************/
  1028. // Forum Topic Counts
  1029. bbp_update_forum_topic_count( $source_topic_forum_id );
  1030. // Forum Reply Counts
  1031. bbp_update_forum_reply_count( $source_topic_forum_id );
  1032. /** Destination Topic *****************************************************/
  1033. // Topic Reply Counts
  1034. bbp_update_topic_reply_count( $destination_topic_id );
  1035. // Topic Hidden Reply Counts
  1036. bbp_update_topic_reply_count_hidden( $destination_topic_id );
  1037. // Topic Voice Counts
  1038. bbp_update_topic_voice_count( $destination_topic_id );
  1039. do_action( 'bbp_merge_topic_count', $destination_topic_id, $source_topic_id, $source_topic_forum_id );
  1040. }
  1041. /**
  1042. * Split topic handler
  1043. *
  1044. * Handles the front end split topic submission
  1045. *
  1046. * @since bbPress (r2756)
  1047. *
  1048. * @param string $action The requested action to compare this function to
  1049. * @uses bbp_add_error() To add an error message
  1050. * @uses bbp_get_reply() To get the reply
  1051. * @uses bbp_get_topic() To get the topics
  1052. * @uses bbp_verify_nonce_request() To verify the nonce and check the request
  1053. * @uses current_user_can() To check if the current user can edit the topics
  1054. * @uses bbp_get_topic_post_type() To get the topic post type
  1055. * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
  1056. * @uses do_action() Calls 'bbp_pre_split_topic' with the from reply id, source
  1057. * and destination topic ids
  1058. * @uses bbp_get_topic_subscribers() To get the source topic subscribers
  1059. * @uses bbp_add_user_subscription() To add the user subscription
  1060. * @uses bbp_get_topic_favoriters() To get the source topic favoriters
  1061. * @uses bbp_add_user_favorite() To add the user favorite
  1062. * @uses…

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