PageRenderTime 63ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 3538 lines | 1450 code | 715 blank | 1373 comment | 337 complexity | a8562137e608af5585f62a76307183a0 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.0

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

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