PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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 wp_get_post_terms() To get the source topic tags
  1063. * @uses wp_set_post_terms() To set the topic tags
  1064. * @uses bbp_get_reply_post_type() To get the reply post type
  1065. * @uses wpdb::prepare() To prepare our sql query
  1066. * @uses wpdb::get_results() To execute the sql query and get results
  1067. * @uses wp_update_post() To update the replies
  1068. * @uses bbp_update_reply_topic_id() To update the reply topic id
  1069. * @uses bbp_get_topic_forum_id() To get the topic forum id
  1070. * @uses bbp_update_reply_forum_id() To update the reply forum id
  1071. * @uses do_action() Calls 'bbp_split_topic_reply' with the reply id and
  1072. * destination topic id
  1073. * @uses bbp_update_topic_last_reply_id() To update the topic last reply id
  1074. * @uses bbp_update_topic_last_active_time() To update the topic last active meta
  1075. * @uses do_action() Calls 'bbp_post_split_topic' with the destination and
  1076. * source topic ids and source topic's forum id
  1077. * @uses bbp_get_topic_permalink() To get the topic permalink
  1078. * @uses wp_safe_redirect() To redirect to the topic link
  1079. */
  1080. function bbp_split_topic_handler( $action = '' ) {
  1081. // Bail if action is not 'bbp-split-topic'
  1082. if ( 'bbp-split-topic' !== $action )
  1083. return;
  1084. global $wpdb;
  1085. // Prevent debug notices
  1086. $from_reply_id = $destination_topic_id = 0;
  1087. $destination_topic_title = '';
  1088. $destination_topic = $from_reply = $source_topic = '';
  1089. $split_option = false;
  1090. /** Split Reply ***********************************************************/
  1091. if ( empty( $_POST['bbp_reply_id'] ) )
  1092. bbp_add_error( 'bbp_split_topic_reply_id', __( '<strong>ERROR</strong>: Reply ID to split the topic from not found!', 'bbpress' ) );
  1093. else
  1094. $from_reply_id = (int) $_POST['bbp_reply_id'];
  1095. $from_reply = bbp_get_reply( $from_reply_id );
  1096. // Reply exists
  1097. if ( empty( $from_reply ) )
  1098. bbp_add_error( 'bbp_split_topic_r_not_found', __( '<strong>ERROR</strong>: The reply you want to split from was not found.', 'bbpress' ) );
  1099. /** Topic to Split ********************************************************/
  1100. // Get the topic being split
  1101. $source_topic = bbp_get_topic( $from_reply->post_parent );
  1102. // No topic
  1103. if ( empty( $source_topic ) )
  1104. bbp_add_error( 'bbp_split_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to split was not found.', 'bbpress' ) );
  1105. // Nonce check failed
  1106. if ( ! bbp_verify_nonce_request( 'bbp-split-topic_' . $source_topic->ID ) ) {
  1107. bbp_add_error( 'bbp_split_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
  1108. return;
  1109. }
  1110. // Use cannot edit topic
  1111. if ( !current_user_can( 'edit_topic', $source_topic->ID ) )
  1112. bbp_add_error( 'bbp_split_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
  1113. // How to Split
  1114. if ( !empty( $_POST['bbp_topic_split_option'] ) )
  1115. $split_option = (string) trim( $_POST['bbp_topic_split_option'] );
  1116. // Invalid split option
  1117. if ( empty( $split_option ) || !in_array( $split_option, array( 'existing', 'reply' ) ) ) {
  1118. bbp_add_error( 'bbp_split_topic_option', __( '<strong>ERROR</strong>: You need to choose a valid split option.', 'bbpress' ) );
  1119. // Valid Split Option
  1120. } else {
  1121. // What kind of split
  1122. switch ( $split_option ) {
  1123. // Into an existing topic
  1124. case 'existing' :
  1125. // Get destination topic id
  1126. if ( empty( $_POST['bbp_destination_topic'] ) )
  1127. bbp_add_error( 'bbp_split_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found!', 'bbpress' ) );
  1128. else
  1129. $destination_topic_id = (int) $_POST['bbp_destination_topic'];
  1130. // Get the destination topic
  1131. $destination_topic = bbp_get_topic( $destination_topic_id );
  1132. // No destination topic
  1133. if ( empty( $destination_topic ) )
  1134. bbp_add_error( 'bbp_split_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to split to was not found!', 'bbpress' ) );
  1135. // User cannot edit the destination topic
  1136. if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
  1137. bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', 'bbpress' ) );
  1138. break;
  1139. // Split at reply into a new topic
  1140. case 'reply' :
  1141. default :
  1142. // User needs to be able to publish topics
  1143. if ( current_user_can( 'publish_topics' ) ) {
  1144. // Use the new title that was passed
  1145. if ( !empty( $_POST['bbp_topic_split_destination_title'] ) ) {
  1146. $destination_topic_title = esc_attr( strip_tags( $_POST['bbp_topic_split_destination_title'] ) );
  1147. // Use the source topic title
  1148. } else {
  1149. $destination_topic_title = $source_topic->post_title;
  1150. }
  1151. // Setup the updated topic parameters
  1152. $postarr = array(
  1153. 'ID' => $from_reply->ID,
  1154. 'post_title' => $destination_topic_title,
  1155. 'post_name' => false,
  1156. 'post_type' => bbp_get_topic_post_type(),
  1157. 'post_parent' => $source_topic->post_parent,
  1158. 'guid' => ''
  1159. );
  1160. // Update the topic
  1161. $destination_topic_id = wp_update_post( $postarr );
  1162. $destination_topic = bbp_get_topic( $destination_topic_id );
  1163. // Make sure the new topic knows its a topic
  1164. bbp_update_topic_topic_id( $from_reply->ID );
  1165. // Shouldn't happen
  1166. if ( false == $destination_topic_id || is_wp_error( $destination_topic_id ) || empty( $destination_topic ) ) {
  1167. bbp_add_error( 'bbp_split_topic_destination_reply', __( '<strong>ERROR</strong>: There was a problem converting the reply into the topic. Please try again.', 'bbpress' ) );
  1168. }
  1169. // User cannot publish posts
  1170. } else {
  1171. bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to create new topics. The reply could not be converted into a topic.', 'bbpress' ) );
  1172. }
  1173. break;
  1174. }
  1175. }
  1176. // Bail if there are errors
  1177. if ( bbp_has_errors() )
  1178. return;
  1179. /** No Errors - Do the Spit ***********************************************/
  1180. // Update counts, etc...
  1181. do_action( 'bbp_pre_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
  1182. /** Date Check ************************************************************/
  1183. // Check if the destination topic is older than the from reply
  1184. if ( strtotime( $from_reply->post_date ) < strtotime( $destination_topic->post_date ) ) {
  1185. // Set destination topic post_date to 1 second before from reply
  1186. $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $from_reply->post_date ) - 1 );
  1187. $postarr = array(
  1188. 'ID' => $destination_topic_id,
  1189. 'post_date' => $destination_post_date,
  1190. 'post_date_gmt' => get_gmt_from_date( $destination_post_date )
  1191. );
  1192. // Update destination topic
  1193. wp_update_post( $postarr );
  1194. }
  1195. /** Subscriptions *********************************************************/
  1196. // Copy the subscribers
  1197. if ( !empty( $_POST['bbp_topic_subscribers'] ) && 1 == $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active() ) {
  1198. // Get the subscribers
  1199. $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
  1200. if ( !empty( $subscribers ) ) {
  1201. // Add subscribers to new topic
  1202. foreach ( (array) $subscribers as $subscriber ) {
  1203. bbp_add_user_subscription( $subscriber, $destination_topic->ID );
  1204. }
  1205. }
  1206. }
  1207. /** Favorites *************************************************************/
  1208. // Copy the favoriters if told to
  1209. if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] ) {
  1210. // Get the favoriters
  1211. $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
  1212. if ( !empty( $favoriters ) ) {
  1213. // Add the favoriters to new topic
  1214. foreach ( (array) $favoriters as $favoriter ) {
  1215. bbp_add_user_favorite( $favoriter, $destination_topic->ID );
  1216. }
  1217. }
  1218. }
  1219. /** Tags ******************************************************************/
  1220. // Copy the tags if told to
  1221. if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) ) {
  1222. // Get the source topic tags
  1223. $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
  1224. if ( !empty( $source_topic_tags ) ) {
  1225. wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
  1226. }
  1227. }
  1228. /** Split Replies *********************************************************/
  1229. // get_posts() is not used because it doesn't allow us to use '>='
  1230. // comparision without a filter.
  1231. $replies = (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_parent = %d AND {$wpdb->posts}.post_type = %s ORDER BY {$wpdb->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() ) );
  1232. // Make sure there are replies to loop through
  1233. if ( !empty( $replies ) && !is_wp_error( $replies ) ) {
  1234. // Calculate starting point for reply positions
  1235. switch ( $split_option ) {
  1236. // Get topic reply count for existing topic
  1237. case 'existing' :
  1238. $reply_position = bbp_get_topic_reply_count( $destination_topic->ID );
  1239. break;
  1240. // Account for new lead topic
  1241. case 'reply' :
  1242. $reply_position = 1;
  1243. break;
  1244. }
  1245. // Change the post_parent of each reply to the destination topic id
  1246. foreach ( $replies as $reply ) {
  1247. // Bump the reply position each iteration through the loop
  1248. $reply_position++;
  1249. // New reply data
  1250. $postarr = array(
  1251. 'ID' => $reply->ID,
  1252. 'post_title' => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
  1253. 'post_name' => false, // will be automatically generated
  1254. 'post_parent' => $destination_topic->ID,
  1255. 'post_position' => $reply_position,
  1256. 'guid' => ''
  1257. );
  1258. // Update the reply
  1259. wp_update_post( $postarr );
  1260. // Adjust reply meta values
  1261. bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID );
  1262. bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
  1263. // Do additional actions per split reply
  1264. do_action( 'bbp_split_topic_reply', $reply->ID, $destination_topic->ID );
  1265. }
  1266. // Set the last reply ID and freshness
  1267. $last_reply_id = $reply->ID;
  1268. $freshness = $reply->post_date;
  1269. // Set the last reply ID and freshness to the from_reply
  1270. } else {
  1271. $last_reply_id = $from_reply->ID;
  1272. $freshness = $from_reply->post_date;
  1273. }
  1274. // It is a new topic and we need to set some default metas to make
  1275. // the topic display in bbp_has_topics() list
  1276. if ( 'reply' == $split_option ) {
  1277. bbp_update_topic_last_reply_id ( $destination_topic->ID, $last_reply_id );
  1278. bbp_update_topic_last_active_id ( $destination_topic->ID, $last_reply_id );
  1279. bbp_update_topic_last_active_time( $destination_topic->ID, $freshness );
  1280. }
  1281. // Update source topic ID last active
  1282. bbp_update_topic_last_reply_id ( $source_topic->ID );
  1283. bbp_update_topic_last_active_id ( $source_topic->ID );
  1284. bbp_update_topic_last_active_time( $source_topic->ID );
  1285. /** Successful Split ******************************************************/
  1286. // Update counts, etc...
  1287. do_action( 'bbp_post_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
  1288. // Redirect back to the topic
  1289. wp_safe_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
  1290. // For good measure
  1291. exit();
  1292. }
  1293. /**
  1294. * Fix counts on topic split
  1295. *
  1296. * When a topic is split, update the counts of source and destination topic
  1297. * and their forums.
  1298. *
  1299. * @since bbPress (r2756)
  1300. *
  1301. * @param int $from_reply_id From reply id
  1302. * @param int $source_topic_id Source topic id
  1303. * @param int $destination_topic_id Destination topic id
  1304. * @uses bbp_update_forum_topic_count() To update the forum topic counts
  1305. * @uses bbp_update_forum_reply_count() To update the forum reply counts
  1306. * @uses bbp_update_topic_reply_count() To update the topic reply counts
  1307. * @uses bbp_update_topic_voice_count() To update the topic voice counts
  1308. * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
  1309. * count
  1310. * @uses do_action() Calls 'bbp_split_topic_count' with the from reply id,
  1311. * source topic id & destination topic id
  1312. */
  1313. function bbp_split_topic_count( $from_reply_id, $source_topic_id, $destination_topic_id ) {
  1314. // Forum Topic Counts
  1315. bbp_update_forum_topic_count( bbp_get_topic_forum_id( $destination_topic_id ) );
  1316. // Forum Reply Counts
  1317. bbp_update_forum_reply_count( bbp_get_topic_forum_id( $destination_topic_id ) );
  1318. // Topic Reply Counts
  1319. bbp_update_topic_reply_count( $source_topic_id );
  1320. bbp_update_topic_reply_count( $destination_topic_id );
  1321. // Topic Hidden Reply Counts
  1322. bbp_update_topic_reply_count_hidden( $source_topic_id );
  1323. bbp_update_topic_reply_count_hidden( $destination_topic_id );
  1324. // Topic Voice Counts
  1325. bbp_update_topic_voice_count( $source_topic_id );
  1326. bbp_update_topic_voice_count( $destination_topic_id );
  1327. do_action( 'bbp_split_topic_count', $from_reply_id, $source_topic_id, $destination_topic_id );
  1328. }
  1329. /**
  1330. * Handles the front end tag management (renaming, merging, destroying)
  1331. *
  1332. * @since bbPress (r2768)
  1333. *
  1334. * @param string $action The requested action to compare this function to
  1335. * @uses bbp_verify_nonce_request() To verify the nonce and check the request
  1336. * @uses current_user_can() To check if the current user can edit/delete tags
  1337. * @uses bbp_add_error() To add an error message
  1338. * @uses wp_update_term() To update the topic tag
  1339. * @uses get_term_link() To get the topic tag url
  1340. * @uses term_exists() To check if the topic tag already exists
  1341. * @uses wp_insert_term() To insert a topic tag
  1342. * @uses wp_delete_term() To delete the topic tag
  1343. * @uses home_url() To get the blog's home page url
  1344. * @uses do_action() Calls actions based on the actions with associated args
  1345. * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
  1346. * @uses wp_safe_redirect() To redirect to the url
  1347. */
  1348. function bbp_edit_topic_tag_handler( $action = '' ) {
  1349. // Bail if required POST actions aren't passed
  1350. if ( empty( $_POST['tag-id'] ) )
  1351. return;
  1352. // Setup possible get actions
  1353. $possible_actions = array(
  1354. 'bbp-update-topic-tag',
  1355. 'bbp-merge-topic-tag',
  1356. 'bbp-delete-topic-tag'
  1357. );
  1358. // Bail if actions aren't meant for this function
  1359. if ( !in_array( $action, $possible_actions ) )
  1360. return;
  1361. // Setup vars
  1362. $tag_id = (int) $_POST['tag-id'];
  1363. $tag = get_term( $tag_id, bbp_get_topic_tag_tax_id() );
  1364. // Tag does not exist
  1365. if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
  1366. bbp_add_error( 'bbp_manage_topic_invalid_tag', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while getting the tag: %s', 'bbpress' ), $tag->get_error_message() ) );
  1367. return;
  1368. }
  1369. // What action are we trying to perform?
  1370. switch ( $action ) {
  1371. // Update tag
  1372. case 'bbp-update-topic-tag' :
  1373. // Nonce check
  1374. if ( ! bbp_verify_nonce_request( 'update-tag_' . $tag_id ) ) {
  1375. bbp_add_error( 'bbp_manage_topic_tag_update_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
  1376. return;
  1377. }
  1378. // Can user edit topic tags?
  1379. if ( !current_user_can( 'edit_topic_tags' ) ) {
  1380. bbp_add_error( 'bbp_manage_topic_tag_update_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
  1381. return;
  1382. }
  1383. // No tag name was provided
  1384. if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) {
  1385. bbp_add_error( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
  1386. return;
  1387. }
  1388. // Attempt to update the tag
  1389. $slug = !empty( $_POST['tag-slug'] ) ? $_POST['tag-slug'] : '';
  1390. $tag = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'name' => $name, 'slug' => $slug ) );
  1391. // Cannot update tag
  1392. if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
  1393. bbp_add_error( 'bbp_manage_topic_tag_update_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while updating the tag: %s', 'bbpress' ), $tag->get_error_message() ) );
  1394. return;
  1395. }
  1396. // Redirect
  1397. $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() );
  1398. // Update counts, etc...
  1399. do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug );
  1400. break;
  1401. // Merge two tags
  1402. case 'bbp-merge-topic-tag' :
  1403. // Nonce check
  1404. if ( ! bbp_verify_nonce_request( 'merge-tag_' . $tag_id ) ) {
  1405. bbp_add_error( 'bbp_manage_topic_tag_merge_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
  1406. return;
  1407. }
  1408. // Can user edit topic tags?
  1409. if ( !current_user_can( 'edit_topic_tags' ) ) {
  1410. bbp_add_error( 'bbp_manage_topic_tag_merge_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
  1411. return;
  1412. }
  1413. // No tag name was provided
  1414. if ( empty( $_POST['tag-existing-name'] ) || !$name = $_POST['tag-existing-name'] ) {
  1415. bbp_add_error( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
  1416. return;
  1417. }
  1418. // If term does not exist, create it
  1419. if ( !$tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) )
  1420. $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() );
  1421. // Problem inserting the new term
  1422. if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
  1423. bbp_add_error( 'bbp_manage_topic_tag_merge_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while merging the tags: %s', 'bbpress' ), $tag->get_error_message() ) );
  1424. return;
  1425. }
  1426. // Merging in to...
  1427. $to_tag = $tag['term_id'];
  1428. // Attempting to merge a tag into itself
  1429. if ( $tag_id == $to_tag ) {
  1430. bbp_add_error( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', 'bbpress' ) );
  1431. return;
  1432. }
  1433. // Delete the old term
  1434. $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'default' => $to_tag, 'force_default' => true ) );
  1435. // Error merging the terms
  1436. if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
  1437. bbp_add_error( 'bbp_manage_topic_tag_merge_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while merging the tags: %s', 'bbpress' ), $tag->get_error_message() ) );
  1438. return;
  1439. }
  1440. // Redirect
  1441. $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() );
  1442. // Update counts, etc...
  1443. do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag );
  1444. break;
  1445. // Delete tag
  1446. case 'bbp-delete-topic-tag' :
  1447. // Nonce check
  1448. if ( ! bbp_verify_nonce_request( 'delete-tag_' . $tag_id ) ) {
  1449. bbp_add_error( 'bbp_manage_topic_tag_delete_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
  1450. return;
  1451. }
  1452. // Can user delete topic tags?
  1453. if ( !current_user_can( 'delete_topic_tags' ) ) {
  1454. bbp_add_error( 'bbp_manage_topic_tag_delete_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to delete the topic tags.', 'bbpress' ) );
  1455. return;
  1456. }
  1457. // Attempt to delete term
  1458. $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() );
  1459. // Error deleting term
  1460. if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
  1461. bbp_add_error( 'bbp_manage_topic_tag_delete_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while deleting the tag: %s', 'bbpress' ), $tag->get_error_message() ) );
  1462. return;
  1463. }
  1464. // We don't have any other place to go other than home! Or we may die because of the 404 disease
  1465. $redirect = home_url();
  1466. // Update counts, etc...
  1467. do_action( 'bbp_delete_topic_tag', $tag_id, $tag );
  1468. break;
  1469. }
  1470. /** Successful Moderation *************************************************/
  1471. // Redirect back
  1472. $redirect = ( !empty( $redirect ) && !is_wp_error( $redirect ) ) ? $redirect : home_url();
  1473. wp_safe_redirect( $redirect );
  1474. // For good measure
  1475. exit();
  1476. }
  1477. /** Stickies ******************************************************************/
  1478. /**
  1479. * Return sticky topics of a forum
  1480. *
  1481. * @since bbPress (r2592)
  1482. *
  1483. * @param int $forum_id Optional. If not passed, super stickies are returned.
  1484. * @uses bbp_get_super_stickies() To get the super stickies
  1485. * @uses get_post_meta() To get the forum stickies
  1486. * @uses apply_filters() Calls 'bbp_get_stickies' with the stickies and forum id
  1487. * @return array IDs of sticky topics of a forum or super stickies
  1488. */
  1489. function bbp_get_stickies( $forum_id = 0 ) {
  1490. $stickies = empty( $forum_id ) ? bbp_get_super_stickies() : get_post_meta( $forum_id, '_bbp_sticky_topics', true );
  1491. $stickies = ( empty( $stickies ) || !is_array( $stickies ) ) ? array() : $stickies;
  1492. return apply_filters( 'bbp_get_stickies', $stickies, (int) $forum_id );
  1493. }
  1494. /**
  1495. * Return topics stuck to front page of the forums
  1496. *
  1497. * @since bbPress (r2592)
  1498. *
  1499. * @uses get_option() To get super sticky topics
  1500. * @uses apply_filters() Calls 'bbp_get_super_stickies' with the stickies
  1501. * @return array IDs of super sticky topics
  1502. */
  1503. function bbp_get_super_stickies() {
  1504. $stickies = get_option( '_bbp_super_sticky_topics', array() );
  1505. $stickies = ( empty( $stickies ) || !is_array( $stickies ) ) ? array() : $stickies;
  1506. return apply_filters( 'bbp_get_super_stickies', $stickies );
  1507. }
  1508. /** Topics Actions ************************************************************/
  1509. /**
  1510. * Handles the front end opening/closing, spamming/unspamming,
  1511. * sticking/unsticking and trashing/untrashing/deleting of topics
  1512. *
  1513. * @since bbPress (r2727)
  1514. *
  1515. * @param string $action The requested action to compare this function to
  1516. * @uses bbp_get_topic() To get the topic
  1517. * @uses current_user_can() To check if the user is capable of editing or
  1518. * deleting the topic
  1519. * @uses bbp_get_topic_post_type() To get the topic post type
  1520. * @uses check_ajax_referer() To verify the nonce and check the referer
  1521. * @uses bbp_is_topic_open() To check if the topic is open
  1522. * @uses bbp_close_topic() To close the topic
  1523. * @uses bbp_open_topic() To open the topic
  1524. * @uses bbp_is_topic_sticky() To check if the topic is a sticky
  1525. * @uses bbp_unstick_topic() To unstick the topic
  1526. * @uses bbp_stick_topic() To stick the topic
  1527. * @uses bbp_is_topic_spam() To check if the topic is marked as spam
  1528. * @uses bbp_spam_topic() To make the topic as spam
  1529. * @uses bbp_unspam_topic() To unmark the topic as spam
  1530. * @uses wp_trash_post() To trash the topic
  1531. * @uses wp_untrash_post() To untrash the topic
  1532. * @uses wp_delete_post() To delete the topic
  1533. * @uses do_action() Calls 'bbp_toggle_topic_handler' with success, post data
  1534. * and action
  1535. * @uses bbp_get_forum_permalink() To get the forum link
  1536. * @uses bbp_get_topic_permalink() To get the topic link
  1537. * @uses add_query_arg() To add args to the url
  1538. * @uses wp_safe_redirect() To redirect to the topic
  1539. * @uses bbPress::errors:add() To log the error messages
  1540. */
  1541. function bbp_toggle_topic_handler( $action = '' ) {
  1542. // Bail if required GET actions aren't passed
  1543. if ( empty( $_GET['topic_id'] ) )
  1544. return;
  1545. // Setup possible get actions
  1546. $possible_actions = array(
  1547. 'bbp_toggle_topic_close',
  1548. 'bbp_toggle_topic_stick',
  1549. 'bbp_toggle_topic_spam',
  1550. 'bbp_toggle_topic_trash'
  1551. );
  1552. // Bail if actions aren't meant for this function
  1553. if ( !in_array( $action, $possible_actions ) )
  1554. return;
  1555. $failure = ''; // Empty failure string
  1556. $view_all = false; // Assume not viewing all
  1557. $topic_id = (int) $_GET['topic_id']; // What's the topic id?
  1558. $success = false; // Flag
  1559. $post_data = array( 'ID' => $topic_id ); // Prelim array
  1560. $redirect = ''; // Empty redirect URL
  1561. // Make sure topic exists
  1562. $topic = bbp_get_topic( $topic_id );
  1563. if ( empty( $topic ) )
  1564. return;
  1565. // What is the user doing here?
  1566. if ( !current_user_can( 'edit_topic', $topic->ID ) || ( 'bbp_toggle_topic_trash' == $action && !current_user_can( 'delete_topic', $topic->ID ) ) ) {
  1567. bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that.', 'bbpress' ) );
  1568. return;
  1569. }
  1570. // What action are we trying to perform?
  1571. switch ( $action ) {
  1572. // Toggle open/close
  1573. case 'bbp_toggle_topic_close' :
  1574. check_ajax_referer( 'close-topic_' . $topic_id );
  1575. $is_open = bbp_is_topic_open( $topic_id );
  1576. $success = $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id );
  1577. $failure = $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress' );
  1578. break;
  1579. // Toggle sticky/super-sticky/unstick
  1580. case 'bbp_toggle_topic_stick' :
  1581. check_ajax_referer( 'stick-topic_' . $topic_id );
  1582. $is_sticky = bbp_is_topic_sticky( $topic_id );
  1583. $is_super = ( empty( $is_sticky ) && !empty( $_GET['super'] ) && 1 == (int) $_GET['super'] ) ? true : false;
  1584. $success = $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super );
  1585. $failure = $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress' );
  1586. break;
  1587. // Toggle spam
  1588. case 'bbp_toggle_topic_spam' :
  1589. check_ajax_referer( 'spam-topic_' . $topic_id );
  1590. $is_spam = bbp_is_topic_spam( $topic_id );
  1591. $success = $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id );
  1592. $failure = $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'bbpress' );
  1593. $view_all = !$is_spam;
  1594. break;
  1595. // Toggle trash
  1596. case 'bbp_toggle_topic_trash' :
  1597. $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
  1598. if ( empty( $sub_action ) )
  1599. break;
  1600. switch ( $sub_action ) {
  1601. case 'trash':
  1602. check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id );
  1603. $view_all = true;
  1604. $success = wp_trash_post( $topic_id );
  1605. $failure = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );
  1606. break;
  1607. case 'untrash':
  1608. check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id );
  1609. $success = wp_untrash_post( $topic_id );
  1610. $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );
  1611. break;
  1612. case 'delete':
  1613. check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id );
  1614. $success = wp_delete_post( $topic_id );
  1615. $failure = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );
  1616. break;
  1617. }
  1618. break;
  1619. }
  1620. // Do additional topic toggle actions
  1621. do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action );
  1622. // No errors
  1623. if ( false != $success && !is_wp_error( $success ) ) {
  1624. // Redirect back to the topic's forum
  1625. if ( isset( $sub_action ) && ( 'delete' == $sub_action ) ) {
  1626. $redirect = bbp_get_forum_permalink( $success->post_parent );
  1627. // Redirect back to the topic
  1628. } else {
  1629. // Get the redirect detination
  1630. $permalink = bbp_get_topic_permalink( $topic_id );
  1631. $redirect = bbp_add_view_all( $permalink, $view_all );
  1632. }
  1633. wp_safe_redirect( $redirect );
  1634. // For good measure
  1635. exit();
  1636. // Handle errors
  1637. } else {
  1638. bbp_add_error( 'bbp_toggle_topic', $failure );
  1639. }
  1640. }
  1641. /** Favorites & Subscriptions *************************************************/
  1642. /**
  1643. * Remove a deleted topic from all users' favorites
  1644. *
  1645. * @since bbPress (r2652)
  1646. *
  1647. * @param int $topic_id Topic ID to remove
  1648. * @uses bbp_get_topic_favoriters() To get the topic's favoriters
  1649. * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
  1650. */
  1651. function bbp_remove_topic_from_all_favorites( $topic_id = 0 ) {
  1652. $topic_id = bbp_get_topic_id( $topic_id );
  1653. // Bail if no topic
  1654. if ( empty( $topic_id ) )
  1655. return;
  1656. // Get users
  1657. $users = (array) bbp_get_topic_favoriters( $topic_id );
  1658. // Users exist
  1659. if ( !empty( $users ) ) {
  1660. // Loop through users
  1661. foreach ( $users as $user ) {
  1662. // Remove each user
  1663. bbp_remove_user_favorite( $user, $topic_id );
  1664. }
  1665. }
  1666. }
  1667. /**
  1668. * Remove a deleted topic from all users' subscriptions
  1669. *
  1670. * @since bbPress (r2652)
  1671. *
  1672. * @param int $topic_id Topic ID to remove
  1673. * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
  1674. * @uses bbp_get_topic_subscribers() To get the topic subscribers
  1675. * @uses bbp_remove_user_subscription() To remove the user subscription
  1676. */
  1677. function bbp_remove_topic_from_all_subscriptions( $topic_id = 0 ) {
  1678. // Subscriptions are not active
  1679. if ( !bbp_is_subscriptions_active() )
  1680. return;
  1681. $topic_id = bbp_get_topic_id( $topic_id );
  1682. // Bail if no topic
  1683. if ( empty( $topic_id ) )
  1684. return;
  1685. // Get users
  1686. $users = (array) bbp_get_topic_subscribers( $topic_id );
  1687. // Users exist
  1688. if ( !empty( $users ) ) {
  1689. // Loop through users
  1690. foreach ( $users as $user ) {
  1691. // Remove each user
  1692. bbp_remove_user_subscription( $user, $topic_id );
  1693. }
  1694. }
  1695. }
  1696. /** Count Bumpers *************************************************************/
  1697. /**
  1698. * Bump the total reply count of a topic
  1699. *
  1700. * @since bbPress (r3825)
  1701. *
  1702. * @param int $topic_id Optional. Forum id.
  1703. * @param int $difference Optional. Default 1
  1704. * @param bool $update_ancestors Optional. Default true
  1705. * @uses bbp_get_topic_id() To get the topic id
  1706. * @uses update_post_meta() To update the topic's reply count meta
  1707. * @uses apply_filters() Calls 'bbp_bump_topic_reply_count' with the reply
  1708. * count, topic id, and difference
  1709. * @return int Forum reply count
  1710. */
  1711. function bbp_bump_topic_reply_count( $topic_id = 0, $difference = 1 ) {
  1712. // Get counts
  1713. $topic_id = bbp_get_topic_id( $topic_id );
  1714. $reply_count = bbp_get_topic_reply_count( $topic_id, true );
  1715. $new_count = (int) $reply_count + (int) $difference;
  1716. // Update this topic id's reply count
  1717. update_post_meta( $topic_id, '_bbp_reply_count', (int) $new_count );
  1718. return (int) apply_filters( 'bbp_bump_topic_reply_count', (int) $new_count, $topic_id, (int) $difference );
  1719. }
  1720. /**
  1721. * Bump the total hidden reply count of a topic
  1722. *
  1723. * @since bbPress (r3825)
  1724. *
  1725. * @param int $topic_id Optional. Forum id.
  1726. * @param int $difference Optional. Default 1
  1727. * @uses bbp_get_topic_id() To get the topic id
  1728. * @uses update_post_meta() To update the topic's reply count meta
  1729. * @uses apply_filters() Calls 'bbp_bump_topic_reply_count_hidden' with the
  1730. * reply count, topic id, and difference
  1731. * @return int Forum hidden reply count
  1732. */
  1733. function bbp_bump_topic_reply_count_hidden( $topic_id = 0, $difference = 1 ) {
  1734. // Get counts
  1735. $topic_id = bbp_get_topic_id( $topic_id );
  1736. $reply_count = bbp_get_topic_reply_count_hidden( $topic_id, true );
  1737. $new_count = (int) $reply_count + (int) $difference;
  1738. // Update this topic id's hidder reply count
  1739. update_post_meta( $topic_id, '_bbp_reply_count_hidden', (int) $new_count );
  1740. return (int) apply_filters( 'bbp_bump_topic_reply_count_hidden', (int) $new_count, $topic_id, (int) $difference );
  1741. }
  1742. /** Topic Updaters ************************************************************/
  1743. /**
  1744. * Update the topic's forum id
  1745. *
  1746. * @since bbPress (r2855)
  1747. *
  1748. * @param int $topic_id Optional. Topic id to update
  1749. * @param int $forum_id Optional. Forum id
  1750. * @uses bbp_is_reply() TO check if the passed topic id is a reply
  1751. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1752. * @uses bbp_get_topic_id() To get the topic id
  1753. * @uses get_post_field() To get the post parent of the topic id
  1754. * @uses bbp_get_forum_id() To get the forum id
  1755. * @uses update_post_meta() To update the topic forum id meta
  1756. * @uses apply_filters() Calls 'bbp_update_topic_forum_id' with the forum id
  1757. * and topic id
  1758. * @return int Forum id
  1759. */
  1760. function bbp_update_topic_forum_id( $topic_id = 0, $forum_id = 0 ) {
  1761. // If it's a reply, then get the parent (topic id)
  1762. if ( bbp_is_reply( $topic_id ) ) {
  1763. $topic_id = bbp_get_reply_topic_id( $topic_id );
  1764. } else {
  1765. $topic_id = bbp_get_topic_id( $topic_id );
  1766. }
  1767. if ( empty( $forum_id ) ) {
  1768. $forum_id = get_post_field( 'post_parent', $topic_id );
  1769. }
  1770. update_post_meta( $topic_id, '_bbp_forum_id', (int) $forum_id );
  1771. return apply_filters( 'bbp_update_topic_forum_id', (int) $forum_id, $topic_id );
  1772. }
  1773. /**
  1774. * Update the topic's topic id
  1775. *
  1776. * @since bbPress (r2954)
  1777. *
  1778. * @param int $topic_id Optional. Topic id to update
  1779. * @uses bbp_get_topic_id() To get the topic id
  1780. * @uses update_post_meta() To update the topic's topic id meta
  1781. * @uses apply_filters() Calls 'bbp_update_topic_topic_id' with the topic id
  1782. * @return int Topic id
  1783. */
  1784. function bbp_update_topic_topic_id( $topic_id = 0 ) {
  1785. $topic_id = bbp_get_topic_id( $topic_id );
  1786. update_post_meta( $topic_id, '_bbp_topic_id', (int) $topic_id );
  1787. return apply_filters( 'bbp_update_topic_topic_id', (int) $topic_id );
  1788. }
  1789. /**
  1790. * Adjust the total reply count of a topic
  1791. *
  1792. * @since bbPress (r2467)
  1793. *
  1794. * @param int $topic_id Optional. Topic id to update
  1795. * @param int $reply_count Optional. Set the reply count manually.
  1796. * @uses bbp_is_reply() To check if the passed topic id is a reply
  1797. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1798. * @uses bbp_get_topic_id() To get the topic id
  1799. * @uses bbp_get_reply_post_type() To get the reply post type
  1800. * @uses bbp_get_public_child_count() To get the reply count
  1801. * @uses update_post_meta() To update the topic reply count meta
  1802. * @uses apply_filters() Calls 'bbp_update_topic_reply_count' with the reply
  1803. * count and topic id
  1804. * @return int Topic reply count
  1805. */
  1806. function bbp_update_topic_reply_count( $topic_id = 0, $reply_count = 0 ) {
  1807. // If it's a reply, then get the parent (topic id)
  1808. if ( bbp_is_reply( $topic_id ) ) {
  1809. $topic_id = bbp_get_reply_topic_id( $topic_id );
  1810. } else {
  1811. $topic_id = bbp_get_topic_id( $topic_id );
  1812. }
  1813. // Get replies of topic if not passed
  1814. if ( empty( $reply_count ) ) {
  1815. $reply_count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
  1816. }
  1817. update_post_meta( $topic_id, '_bbp_reply_count', (int) $reply_count );
  1818. return apply_filters( 'bbp_update_topic_reply_count', (int) $reply_count, $topic_id );
  1819. }
  1820. /**
  1821. * Adjust the total hidden reply count of a topic (hidden includes trashed and spammed replies)
  1822. *
  1823. * @since bbPress (r2740)
  1824. *
  1825. * @param int $topic_id Optional. Topic id to update
  1826. * @param int $reply_count Optional. Set the reply count manually
  1827. * @uses bbp_is_reply() To check if the passed topic id is a reply
  1828. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1829. * @uses bbp_get_topic_id() To get the topic id
  1830. * @uses bbp_get_reply_post_type() To get the reply post type
  1831. * @uses wpdb::prepare() To prepare our sql query
  1832. * @uses wpdb::get_var() To execute our query and get the var back
  1833. * @uses update_post_meta() To update the topic hidden reply count meta
  1834. * @uses apply_filters() Calls 'bbp_update_topic_reply_count_hidden' with the
  1835. * hidden reply count and topic id
  1836. * @return int Topic hidden reply count
  1837. */
  1838. function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = 0 ) {
  1839. global $wpdb;
  1840. // If it's a reply, then get the parent (topic id)
  1841. if ( bbp_is_reply( $topic_id ) ) {
  1842. $topic_id = bbp_get_reply_topic_id( $topic_id );
  1843. } else {
  1844. $topic_id = bbp_get_topic_id( $topic_id );
  1845. }
  1846. // Get replies of topic
  1847. if ( empty( $reply_count ) ) {
  1848. $reply_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "') AND post_type = '%s';", $topic_id, bbp_get_reply_post_type() ) );
  1849. }
  1850. update_post_meta( $topic_id, '_bbp_reply_count_hidden', (int) $reply_count );
  1851. return apply_filters( 'bbp_update_topic_reply_count_hidden', (int) $reply_count, $topic_id );
  1852. }
  1853. /**
  1854. * Update the topic with the last active post ID
  1855. *
  1856. * @since bbPress (r2888)
  1857. *
  1858. * @param int $topic_id Optional. Topic id to update
  1859. * @param int $active_id Optional. active id
  1860. * @uses bbp_is_reply() To check if the passed topic id is a reply
  1861. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1862. * @uses bbp_get_topic_id() To get the topic id
  1863. * @uses bbp_get_reply_post_type() To get the reply post type
  1864. * @uses bbp_get_public_child_last_id() To get the last public reply id
  1865. * @uses bbp_get_active_id() To get the active id
  1866. * @uses update_post_meta() To update the topic last active id meta
  1867. * @uses apply_filters() Calls 'bbp_update_topic_last_active_id' with the active
  1868. * id and topic id
  1869. * @return int Active id
  1870. */
  1871. function bbp_update_topic_last_active_id( $topic_id = 0, $active_id = 0 ) {
  1872. // If it's a reply, then get the parent (topic id)
  1873. if ( bbp_is_reply( $topic_id ) ) {
  1874. $topic_id = bbp_get_reply_topic_id( $topic_id );
  1875. } else {
  1876. $topic_id = bbp_get_topic_id( $topic_id );
  1877. }
  1878. if ( empty( $active_id ) ) {
  1879. $active_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
  1880. }
  1881. // Adjust last_id's based on last_reply post_type
  1882. if ( empty( $active_id ) || !bbp_is_reply( $active_id ) ) {
  1883. $active_id = $topic_id;
  1884. }
  1885. // Update only if published
  1886. if ( bbp_get_public_status_id() == get_post_status( $active_id ) ) {
  1887. update_post_meta( $topic_id, '_bbp_last_active_id', (int) $active_id );
  1888. }
  1889. return apply_filters( 'bbp_update_topic_last_active_id', (int) $active_id, $topic_id );
  1890. }
  1891. /**
  1892. * Update the topics last active date/time (aka freshness)
  1893. *
  1894. * @since bbPress (r2680)
  1895. *
  1896. * @param int $topic_id Optional. Topic id
  1897. * @param string $new_time Optional. New time in mysql format
  1898. * @uses bbp_get_topic_id() To get the topic id
  1899. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1900. * @uses current_time() To get the current time
  1901. * @uses update_post_meta() To update the topic last active meta
  1902. * @return bool True on success, false on failure
  1903. */
  1904. function bbp_update_topic_last_active_time( $topic_id = 0, $new_time = '' ) {
  1905. // If it's a reply, then get the parent (topic id)
  1906. if ( bbp_is_reply( $topic_id ) ) {
  1907. $topic_id = bbp_get_reply_topic_id( $topic_id );
  1908. } else {
  1909. $topic_id = bbp_get_topic_id( $topic_id );
  1910. }
  1911. // Check time and use current if empty
  1912. if ( empty( $new_time ) ) {
  1913. $new_time = get_post_field( 'post_date', bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ) );
  1914. }
  1915. // Update only if published
  1916. if ( !empty( $new_time ) ) {
  1917. update_post_meta( $topic_id, '_bbp_last_active_time', $new_time );
  1918. }
  1919. return apply_filters( 'bbp_update_topic_last_active_time', $new_time, $topic_id );
  1920. }
  1921. /**
  1922. * Update the topic with the most recent reply ID
  1923. *
  1924. * @since bbPress (r2625)
  1925. *
  1926. * @param int $topic_id Optional. Topic id to update
  1927. * @param int $reply_id Optional. Reply id
  1928. * @uses bbp_is_reply() To check if the passed topic id is a reply
  1929. * @uses bbp_get_reply_id() To get the reply id
  1930. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1931. * @uses bbp_get_topic_id() To get the topic id
  1932. * @uses bbp_get_reply_post_type() To get the reply post type
  1933. * @uses bbp_get_public_child_last_id() To get the last public reply id
  1934. * @uses update_post_meta() To update the topic last reply id meta
  1935. * @uses apply_filters() Calls 'bbp_update_topic_last_reply_id' with the reply
  1936. * id and topic id
  1937. * @return int Reply id
  1938. */
  1939. function bbp_update_topic_last_reply_id( $topic_id = 0, $reply_id = 0 ) {
  1940. // If it's a reply, then get the parent (topic id)
  1941. if ( empty( $reply_id ) && bbp_is_reply( $topic_id ) ) {
  1942. $reply_id = bbp_get_reply_id( $topic_id );
  1943. $topic_id = bbp_get_reply_topic_id( $reply_id );
  1944. } else {
  1945. $reply_id = bbp_get_reply_id( $reply_id );
  1946. $topic_id = bbp_get_topic_id( $topic_id );
  1947. }
  1948. if ( empty( $reply_id ) ) {
  1949. $reply_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
  1950. }
  1951. // Adjust last_id's based on last_reply post_type
  1952. if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) ) {
  1953. $reply_id = 0;
  1954. }
  1955. // Update if reply is published
  1956. if ( bbp_is_reply_published( $reply_id ) ) {
  1957. update_post_meta( $topic_id, '_bbp_last_reply_id', (int) $reply_id );
  1958. }
  1959. return apply_filters( 'bbp_update_topic_last_reply_id', (int) $reply_id, $topic_id );
  1960. }
  1961. /**
  1962. * Adjust the total voice count of a topic
  1963. *
  1964. * @since bbPress (r2567)
  1965. *
  1966. * @param int $topic_id Optional. Topic id to update
  1967. * @uses bbp_is_reply() To check if the passed topic id is a reply
  1968. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1969. * @uses bbp_get_topic_id() To get the topic id
  1970. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1971. * @uses bbp_get_reply_post_type() To get the reply post type
  1972. * @uses bbp_get_topic_post_type() To get the topic post type
  1973. * @uses wpdb::prepare() To prepare our sql query
  1974. * @uses wpdb::get_col() To execute our query and get the column back
  1975. * @uses update_post_meta() To update the topic voice count meta
  1976. * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice
  1977. * count and topic id
  1978. * @return int Voice count
  1979. */
  1980. function bbp_update_topic_voice_count( $topic_id = 0 ) {
  1981. global $wpdb;
  1982. // If it's a reply, then get the parent (topic id)
  1983. if ( bbp_is_reply( $topic_id ) ) {
  1984. $topic_id = bbp_get_reply_topic_id( $topic_id );
  1985. } elseif ( bbp_is_topic( $topic_id ) ) {
  1986. $topic_id = bbp_get_topic_id( $topic_id );
  1987. } else {
  1988. return;
  1989. }
  1990. // Query the DB to get voices in this topic
  1991. $voices = $wpdb->get_col( $wpdb->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' ) OR ( ID = %d AND post_type = '%s' );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) );
  1992. // If there's an error, make sure we have at least have 1 voice
  1993. $voices = ( empty( $voices ) || is_wp_error( $voices ) ) ? 1 : $voices[0];
  1994. // Update the voice count for this topic id
  1995. update_post_meta( $topic_id, '_bbp_voice_count', (int) $voices );
  1996. return apply_filters( 'bbp_update_topic_voice_count', (int) $voices, $topic_id );
  1997. }
  1998. /**
  1999. * Adjust the total anonymous reply count of a topic
  2000. *
  2001. * @since bbPress (r2567)
  2002. *
  2003. * @param int $topic_id Optional. Topic id to update
  2004. * @uses bbp_is_reply() To check if the passed topic id is a reply
  2005. * @uses bbp_get_reply_topic_id() To get the reply topic id
  2006. * @uses bbp_get_topic_id() To get the topic id
  2007. * @uses bbp_get_reply_topic_id() To get the reply topic id
  2008. * @uses bbp_get_reply_post_type() To get the reply post type
  2009. * @uses bbp_get_topic_post_type() To get the topic post type
  2010. * @uses wpdb::prepare() To prepare our sql query
  2011. * @uses wpdb::get_col() To execute our query and get the column back
  2012. * @uses update_post_meta() To update the topic anonymous reply count meta
  2013. * @uses apply_filters() Calls 'bbp_update_topic_anonymous_reply_count' with the
  2014. * anonymous reply count and topic id
  2015. * @return int Anonymous reply count
  2016. */
  2017. function bbp_update_topic_anonymous_reply_count( $topic_id = 0 ) {
  2018. global $wpdb;
  2019. // If it's a reply, then get the parent (topic id)
  2020. if ( bbp_is_reply( $topic_id ) ) {
  2021. $topic_id = bbp_get_reply_topic_id( $topic_id );
  2022. } elseif ( bbp_is_topic( $topic_id ) ) {
  2023. $topic_id = bbp_get_topic_id( $topic_id );
  2024. } else {
  2025. return;
  2026. }
  2027. $anonymous_replies = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( ID ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' AND post_author = 0 ) OR ( ID = %d AND post_type = '%s' AND post_author = 0 );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) );
  2028. update_post_meta( $topic_id, '_bbp_anonymous_reply_count', (int) $anonymous_replies );
  2029. return apply_filters( 'bbp_update_topic_anonymous_reply_count', (int) $anonymous_replies, $topic_id );
  2030. }
  2031. /**
  2032. * Update the revision log of the topic
  2033. *
  2034. * @since bbPress (r2782)
  2035. *
  2036. * @param mixed $args Supports these args:
  2037. * - topic_id: Topic id
  2038. * - author_id: Author id
  2039. * - reason: Reason for editing
  2040. * - revision_id: Revision id
  2041. * @uses bbp_get_topic_id() To get the topic id
  2042. * @uses bbp_format_revision_reason() To format the reason
  2043. * @uses bbp_get_topic_raw_revision_log() To get the raw topic revision log
  2044. * @uses update_post_meta() To update the topic revision log meta
  2045. * @return mixed False on failure, true on success
  2046. */
  2047. function bbp_update_topic_revision_log( $args = '' ) {
  2048. // Parse arguments against default values
  2049. $r = bbp_parse_args( $args, array(
  2050. 'reason' => '',
  2051. 'topic_id' => 0,
  2052. 'author_id' => 0,
  2053. 'revision_id' => 0
  2054. ), 'update_topic_revision_log' );
  2055. // Populate the variables
  2056. $r['reason'] = bbp_format_revision_reason( $r['reason'] );
  2057. $r['topic_id'] = bbp_get_topic_id( $r['topic_id'] );
  2058. $r['author_id'] = bbp_get_user_id ( $r['author_id'], false, true );
  2059. $r['revision_id'] = (int) $r['revision_id'];
  2060. // Get the logs and append the new one to those
  2061. $revision_log = bbp_get_topic_raw_revision_log( $r['topic_id'] );
  2062. $revision_log[ $r['revision_id'] ] = array( 'author' => $r['author_id'], 'reason' => $r['reason'] );
  2063. // Finally, update
  2064. return update_post_meta( $r['topic_id'], '_bbp_revision_log', $revision_log );
  2065. }
  2066. /** Topic Actions *************************************************************/
  2067. /**
  2068. * Closes a topic
  2069. *
  2070. * @since bbPress (r2740)
  2071. *
  2072. * @param int $topic_id Topic id
  2073. * @uses get_post() To get the topic
  2074. * @uses do_action() Calls 'bbp_close_topic' with the topic id
  2075. * @uses add_post_meta() To add the previous status to a meta
  2076. * @uses wp_insert_post() To update the topic with the new status
  2077. * @uses do_action() Calls 'bbp_opened_topic' with the topic id
  2078. * @return mixed False or {@link WP_Error} on failure, topic id on success
  2079. */
  2080. function bbp_close_topic( $topic_id = 0 ) {
  2081. // Get topic
  2082. if ( !$topic = get_post( $topic_id, ARRAY_A ) )
  2083. return $topic;
  2084. // Bail if already closed
  2085. if ( bbp_get_closed_status_id() == $topic['post_status'] )
  2086. return false;
  2087. // Execute pre close code
  2088. do_action( 'bbp_close_topic', $topic_id );
  2089. // Add pre close status
  2090. add_post_meta( $topic_id, '_bbp_status', $topic['post_status'] );
  2091. // Set closed status
  2092. $topic['post_status'] = bbp_get_closed_status_id();
  2093. // No revisions
  2094. remove_action( 'pre_post_update', 'wp_save_post_revision' );
  2095. // Update topic
  2096. $topic_id = wp_insert_post( $topic );
  2097. // Execute post close code
  2098. do_action( 'bbp_closed_topic', $topic_id );
  2099. // Return topic_id
  2100. return $topic_id;
  2101. }
  2102. /**
  2103. * Opens a topic
  2104. *
  2105. * @since bbPress (r2740)
  2106. *
  2107. * @param int $topic_id Topic id
  2108. * @uses get_post() To get the topic
  2109. * @uses do_action() Calls 'bbp_open_topic' with the topic id
  2110. * @uses get_post_meta() To get the previous status
  2111. * @uses delete_post_meta() To delete the previous status meta
  2112. * @uses wp_insert_post() To update the topic with the new status
  2113. * @uses do_action() Calls 'bbp_opened_topic' with the topic id
  2114. * @return mixed False or {@link WP_Error} on failure, topic id on success
  2115. */
  2116. function bbp_open_topic( $topic_id = 0 ) {
  2117. // Get topic
  2118. if ( !$topic = get_post( $topic_id, ARRAY_A ) )
  2119. return $topic;
  2120. // Bail if already open
  2121. if ( bbp_get_closed_status_id() != $topic['post_status'])
  2122. return false;
  2123. // Execute pre open code
  2124. do_action( 'bbp_open_topic', $topic_id );
  2125. // Get previous status
  2126. $topic_status = get_post_meta( $topic_id, '_bbp_status', true );
  2127. // Set previous status
  2128. $topic['post_status'] = $topic_status;
  2129. // Remove old status meta
  2130. delete_post_meta( $topic_id, '_bbp_status' );
  2131. // No revisions
  2132. remove_action( 'pre_post_update', 'wp_save_post_revision' );
  2133. // Update topic
  2134. $topic_id = wp_insert_post( $topic );
  2135. // Execute post open code
  2136. do_action( 'bbp_opened_topic', $topic_id );
  2137. // Return topic_id
  2138. return $topic_id;
  2139. }
  2140. /**
  2141. * Marks a topic as spam
  2142. *
  2143. * @since bbPress (r2740)
  2144. *
  2145. * @param int $topic_id Topic id
  2146. * @uses get_post() To get the topic
  2147. * @uses do_action() Calls 'bbp_spam_topic' with the topic id
  2148. * @uses add_post_meta() To add the previous status to a meta
  2149. * @uses wp_insert_post() To update the topic with the new status
  2150. * @uses do_action() Calls 'bbp_spammed_topic' with the topic id
  2151. * @return mixed False or {@link WP_Error} on failure, topic id on success
  2152. */
  2153. function bbp_spam_topic( $topic_id = 0 ) {
  2154. // Get the topic
  2155. if ( !$topic = get_post( $topic_id, ARRAY_A ) )
  2156. return $topic;
  2157. // Bail if topic is spam
  2158. if ( bbp_get_spam_status_id() == $topic['post_status'] )
  2159. return false;
  2160. // Execute pre spam code
  2161. do_action( 'bbp_spam_topic', $topic_id );
  2162. /** Trash Replies *********************************************************/
  2163. // Topic is being spammed, so its replies are trashed
  2164. $replies = new WP_Query( array(
  2165. 'suppress_filters' => true,
  2166. 'post_type' => bbp_get_reply_post_type(),
  2167. 'post_status' => bbp_get_public_status_id(),
  2168. 'post_parent' => $topic_id,
  2169. 'posts_per_page' => -1,
  2170. 'nopaging' => true,
  2171. 'fields' => 'id=>parent'
  2172. ) );
  2173. if ( !empty( $replies->posts ) ) {
  2174. // Prevent debug notices
  2175. $pre_spammed_replies = array();
  2176. // Loop through replies, trash them, and add them to array
  2177. foreach ( $replies->posts as $reply ) {
  2178. wp_trash_post( $reply->ID );
  2179. $pre_spammed_replies[] = $reply->ID;
  2180. }
  2181. // Set a post_meta entry of the replies that were trashed by this action.
  2182. // This is so we can possibly untrash them, without untrashing replies
  2183. // that were purposefully trashed before.
  2184. update_post_meta( $topic_id, '_bbp_pre_spammed_replies', $pre_spammed_replies );
  2185. // Reset the $post global
  2186. wp_reset_postdata();
  2187. }
  2188. // Cleanup
  2189. unset( $replies );
  2190. /** Topic Tags ************************************************************/
  2191. // Add the original post status as post meta for future restoration
  2192. add_post_meta( $topic_id, '_bbp_spam_meta_status', $topic['post_status'] );
  2193. // Get topic tags
  2194. $terms = get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() );
  2195. // Define local variable(s)
  2196. $term_names = array();
  2197. // Topic has tags
  2198. if ( !empty( $terms ) ) {
  2199. // Loop through and collect term names
  2200. foreach( $terms as $term ) {
  2201. $term_names[] = $term->name;
  2202. }
  2203. // Topic terms have slugs
  2204. if ( !empty( $term_names ) ) {
  2205. // Add the original post status as post meta for future restoration
  2206. add_post_meta( $topic_id, '_bbp_spam_topic_tags', $term_names );
  2207. // Empty the topic of its tags
  2208. $topic['tax_input'] = array( bbp_get_topic_tag_tax_id() => '' );
  2209. }
  2210. }
  2211. // Set post status to spam
  2212. $topic['post_status'] = bbp_get_spam_status_id();
  2213. // No revisions
  2214. remove_action( 'pre_post_update', 'wp_save_post_revision' );
  2215. // Update the topic
  2216. $topic_id = wp_insert_post( $topic );
  2217. // Execute post spam code
  2218. do_action( 'bbp_spammed_topic', $topic_id );
  2219. // Return topic_id
  2220. return $topic_id;
  2221. }
  2222. /**
  2223. * Unspams a topic
  2224. *
  2225. * @since bbPress (r2740)
  2226. *
  2227. * @param int $topic_id Topic id
  2228. * @uses get_post() To get the topic
  2229. * @uses do_action() Calls 'bbp_unspam_topic' with the topic id
  2230. * @uses get_post_meta() To get the previous status
  2231. * @uses delete_post_meta() To delete the previous status meta
  2232. * @uses wp_insert_post() To update the topic with the new status
  2233. * @uses do_action() Calls 'bbp_unspammed_topic' with the topic id
  2234. * @return mixed False or {@link WP_Error} on failure, topic id on success
  2235. */
  2236. function bbp_unspam_topic( $topic_id = 0 ) {
  2237. // Get the topic
  2238. if ( !$topic = get_post( $topic_id, ARRAY_A ) )
  2239. return $topic;
  2240. // Bail if already not spam
  2241. if ( bbp_get_spam_status_id() != $topic['post_status'] )
  2242. return false;
  2243. // Execute pre unspam code
  2244. do_action( 'bbp_unspam_topic', $topic_id );
  2245. /** Untrash Replies *******************************************************/
  2246. // Get the replies that were not previously trashed
  2247. $pre_spammed_replies = get_post_meta( $topic_id, '_bbp_pre_spammed_replies', true );
  2248. // There are replies to untrash
  2249. if ( !empty( $pre_spammed_replies ) ) {
  2250. // Maybe reverse the trashed replies array
  2251. if ( is_array( $pre_spammed_replies ) )
  2252. $pre_spammed_replies = array_reverse( $pre_spammed_replies );
  2253. // Loop through replies
  2254. foreach ( (array) $pre_spammed_replies as $reply ) {
  2255. wp_untrash_post( $reply );
  2256. }
  2257. }
  2258. /** Topic Tags ************************************************************/
  2259. // Get pre-spam topic tags
  2260. $terms = get_post_meta( $topic_id, '_bbp_spam_topic_tags', true );
  2261. // Topic had tags before it was spammed
  2262. if ( !empty( $terms ) ) {
  2263. // Set the tax_input of the topic
  2264. $topic['tax_input'] = array( bbp_get_topic_tag_tax_id() => $terms );
  2265. // Delete pre-spam topic tag meta
  2266. delete_post_meta( $topic_id, '_bbp_spam_topic_tags' );
  2267. }
  2268. /** Topic Status **********************************************************/
  2269. // Get pre spam status
  2270. $topic_status = get_post_meta( $topic_id, '_bbp_spam_meta_status', true );
  2271. // If no previous status, default to publish
  2272. if ( empty( $topic_status ) ) {
  2273. $topic_status = bbp_get_public_status_id();
  2274. }
  2275. // Set post status to pre spam
  2276. $topic['post_status'] = $topic_status;
  2277. // Delete pre spam meta
  2278. delete_post_meta( $topic_id, '_bbp_spam_meta_status' );
  2279. // No revisions
  2280. remove_action( 'pre_post_update', 'wp_save_post_revision' );
  2281. // Update the topic
  2282. $topic_id = wp_insert_post( $topic );
  2283. // Execute post unspam code
  2284. do_action( 'bbp_unspammed_topic', $topic_id );
  2285. // Return topic_id
  2286. return $topic_id;
  2287. }
  2288. /**
  2289. * Sticks a topic to a forum or front
  2290. *
  2291. * @since bbPress (r2754)
  2292. *
  2293. * @param int $topic_id Optional. Topic id
  2294. * @param int $super Should we make the topic a super sticky?
  2295. * @uses bbp_get_topic_id() To get the topic id
  2296. * @uses bbp_unstick_topic() To unstick the topic
  2297. * @uses bbp_get_topic_forum_id() To get the topic forum id
  2298. * @uses bbp_get_stickies() To get the stickies
  2299. * @uses do_action() 'bbp_stick_topic' with topic id and bool super
  2300. * @uses update_option() To update the super stickies option
  2301. * @uses update_post_meta() To update the forum stickies meta
  2302. * @uses do_action() Calls 'bbp_sticked_topic' with the topic id, bool super
  2303. * and success
  2304. * @return bool True on success, false on failure
  2305. */
  2306. function bbp_stick_topic( $topic_id = 0, $super = false ) {
  2307. $topic_id = bbp_get_topic_id( $topic_id );
  2308. // Bail if a topic is not a topic (prevents revisions as stickies)
  2309. if ( ! bbp_is_topic( $topic_id ) )
  2310. return false;
  2311. // We may have a super sticky to which we want to convert into a normal
  2312. // sticky and vice versa; unstick the topic first to avoid any possible error.
  2313. bbp_unstick_topic( $topic_id );
  2314. $forum_id = empty( $super ) ? bbp_get_topic_forum_id( $topic_id ) : 0;
  2315. $stickies = bbp_get_stickies( $forum_id );
  2316. do_action( 'bbp_stick_topic', $topic_id, $super );
  2317. if ( !is_array( $stickies ) ) {
  2318. $stickies = array( $topic_id );
  2319. } else {
  2320. $stickies[] = $topic_id;
  2321. }
  2322. // Pull out duplicates and empties
  2323. $stickies = array_unique( array_filter( $stickies ) );
  2324. // Unset incorrectly stuck revisions
  2325. foreach ( (array) $stickies as $key => $id ) {
  2326. if ( ! bbp_is_topic( $id ) ) {
  2327. unset( $stickies[$key] );
  2328. }
  2329. }
  2330. // Reset keys
  2331. $stickies = array_values( $stickies );
  2332. $success = !empty( $super ) ? update_option( '_bbp_super_sticky_topics', $stickies ) : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
  2333. do_action( 'bbp_sticked_topic', $topic_id, $super, $success );
  2334. return (bool) $success;
  2335. }
  2336. /**
  2337. * Unsticks a topic both from front and it's forum
  2338. *
  2339. * @since bbPress (r2754)
  2340. *
  2341. * @param int $topic_id Optional. Topic id
  2342. * @uses bbp_get_topic_id() To get the topic id
  2343. * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
  2344. * @uses bbp_get_topic_forum_id() To get the topic forum id
  2345. * @uses bbp_get_stickies() To get the forum stickies
  2346. * @uses do_action() Calls 'bbp_unstick_topic' with the topic id
  2347. * @uses delete_option() To delete the super stickies option
  2348. * @uses update_option() To update the super stickies option
  2349. * @uses delete_post_meta() To delete the forum stickies meta
  2350. * @uses update_post_meta() To update the forum stickies meta
  2351. * @uses do_action() Calls 'bbp_unsticked_topic' with the topic id and success
  2352. * @return bool Always true.
  2353. */
  2354. function bbp_unstick_topic( $topic_id = 0 ) {
  2355. $topic_id = bbp_get_topic_id( $topic_id );
  2356. $super = bbp_is_topic_super_sticky( $topic_id );
  2357. $forum_id = empty( $super ) ? bbp_get_topic_forum_id( $topic_id ) : 0;
  2358. $stickies = bbp_get_stickies( $forum_id );
  2359. $offset = array_search( $topic_id, $stickies );
  2360. do_action( 'bbp_unstick_topic', $topic_id );
  2361. if ( empty( $stickies ) ) {
  2362. $success = true;
  2363. } elseif ( !in_array( $topic_id, $stickies ) ) {
  2364. $success = true;
  2365. } elseif ( false === $offset ) {
  2366. $success = true;
  2367. } else {
  2368. array_splice( $stickies, $offset, 1 );
  2369. if ( empty( $stickies ) ) {
  2370. $success = !empty( $super ) ? delete_option( '_bbp_super_sticky_topics' ) : delete_post_meta( $forum_id, '_bbp_sticky_topics' );
  2371. } else {
  2372. $success = !empty( $super ) ? update_option( '_bbp_super_sticky_topics', $stickies ) : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
  2373. }
  2374. }
  2375. do_action( 'bbp_unsticked_topic', $topic_id, $success );
  2376. return (bool) $success;
  2377. }
  2378. /** Before Delete/Trash/Untrash ***********************************************/
  2379. /**
  2380. * Called before deleting a topic.
  2381. *
  2382. * This function is supplemental to the actual topic deletion which is
  2383. * handled by WordPress core API functions. It is used to clean up after
  2384. * a topic that is being deleted.
  2385. *
  2386. * @uses bbp_get_topic_id() To get the topic id
  2387. * @uses bbp_is_topic() To check if the passed id is a topic
  2388. * @uses do_action() Calls 'bbp_delete_topic' with the topic id
  2389. * @uses bbp_has_replies() To check if the topic has replies
  2390. * @uses bbp_replies() To loop through the replies
  2391. * @uses bbp_the_reply() To set a reply as the current reply in the loop
  2392. * @uses bbp_get_reply_id() To get the reply id
  2393. * @uses wp_delete_post() To delete the reply
  2394. */
  2395. function bbp_delete_topic( $topic_id = 0 ) {
  2396. // Validate topic ID
  2397. $topic_id = bbp_get_topic_id( $topic_id );
  2398. if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
  2399. return false;
  2400. do_action( 'bbp_delete_topic', $topic_id );
  2401. // Topic is being permanently deleted, so its replies gotta go too
  2402. // Note that we get all post statuses here
  2403. $replies = new WP_Query( array(
  2404. 'suppress_filters' => true,
  2405. 'post_type' => bbp_get_reply_post_type(),
  2406. 'post_status' => array_keys( get_post_stati() ),
  2407. 'post_parent' => $topic_id,
  2408. 'posts_per_page' => -1,
  2409. 'nopaging' => true,
  2410. 'fields' => 'id=>parent'
  2411. ) );
  2412. // Loop through and delete child replies
  2413. if ( ! empty( $replies->posts ) ) {
  2414. foreach ( $replies->posts as $reply ) {
  2415. wp_delete_post( $reply->ID, true );
  2416. }
  2417. // Reset the $post global
  2418. wp_reset_postdata();
  2419. }
  2420. // Cleanup
  2421. unset( $replies );
  2422. }
  2423. /**
  2424. * Called before trashing a topic
  2425. *
  2426. * This function is supplemental to the actual topic being trashed which is
  2427. * handled by WordPress core API functions. It is used to clean up after
  2428. * a topic that is being trashed.
  2429. *
  2430. * @uses bbp_get_topic_id() To get the topic id
  2431. * @uses bbp_is_topic() To check if the passed id is a topic
  2432. * @uses do_action() Calls 'bbp_trash_topic' with the topic id
  2433. * @uses wp_trash_post() To trash the reply
  2434. * @uses update_post_meta() To save a list of just trashed replies for future use
  2435. */
  2436. function bbp_trash_topic( $topic_id = 0 ) {
  2437. // Validate topic ID
  2438. $topic_id = bbp_get_topic_id( $topic_id );
  2439. if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
  2440. return false;
  2441. do_action( 'bbp_trash_topic', $topic_id );
  2442. // Topic is being trashed, so its replies are trashed too
  2443. $replies = new WP_Query( array(
  2444. 'suppress_filters' => true,
  2445. 'post_type' => bbp_get_reply_post_type(),
  2446. 'post_status' => bbp_get_public_status_id(),
  2447. 'post_parent' => $topic_id,
  2448. 'posts_per_page' => -1,
  2449. 'nopaging' => true,
  2450. 'fields' => 'id=>parent'
  2451. ) );
  2452. if ( !empty( $replies->posts ) ) {
  2453. // Prevent debug notices
  2454. $pre_trashed_replies = array();
  2455. // Loop through replies, trash them, and add them to array
  2456. foreach ( $replies->posts as $reply ) {
  2457. wp_trash_post( $reply->ID );
  2458. $pre_trashed_replies[] = $reply->ID;
  2459. }
  2460. // Set a post_meta entry of the replies that were trashed by this action.
  2461. // This is so we can possibly untrash them, without untrashing replies
  2462. // that were purposefully trashed before.
  2463. update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
  2464. // Reset the $post global
  2465. wp_reset_postdata();
  2466. }
  2467. // Cleanup
  2468. unset( $replies );
  2469. }
  2470. /**
  2471. * Called before untrashing a topic
  2472. *
  2473. * @uses bbp_get_topic_id() To get the topic id
  2474. * @uses bbp_is_topic() To check if the passed id is a topic
  2475. * @uses do_action() Calls 'bbp_untrash_topic' with the topic id
  2476. * @uses get_post_meta() To get the list of replies which were trashed with the
  2477. * topic
  2478. * @uses wp_untrash_post() To untrash the reply
  2479. */
  2480. function bbp_untrash_topic( $topic_id = 0 ) {
  2481. $topic_id = bbp_get_topic_id( $topic_id );
  2482. if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
  2483. return false;
  2484. do_action( 'bbp_untrash_topic', $topic_id );
  2485. // Get the replies that were not previously trashed
  2486. $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
  2487. // There are replies to untrash
  2488. if ( !empty( $pre_trashed_replies ) ) {
  2489. // Maybe reverse the trashed replies array
  2490. if ( is_array( $pre_trashed_replies ) )
  2491. $pre_trashed_replies = array_reverse( $pre_trashed_replies );
  2492. // Loop through replies
  2493. foreach ( (array) $pre_trashed_replies as $reply ) {
  2494. wp_untrash_post( $reply );
  2495. }
  2496. }
  2497. }
  2498. /** After Delete/Trash/Untrash ************************************************/
  2499. /**
  2500. * Called after deleting a topic
  2501. *
  2502. * @uses bbp_get_topic_id() To get the topic id
  2503. * @uses bbp_is_topic() To check if the passed id is a topic
  2504. * @uses do_action() Calls 'bbp_deleted_topic' with the topic id
  2505. */
  2506. function bbp_deleted_topic( $topic_id = 0 ) {
  2507. $topic_id = bbp_get_topic_id( $topic_id );
  2508. if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
  2509. return false;
  2510. do_action( 'bbp_deleted_topic', $topic_id );
  2511. }
  2512. /**
  2513. * Called after trashing a topic
  2514. *
  2515. * @uses bbp_get_topic_id() To get the topic id
  2516. * @uses bbp_is_topic() To check if the passed id is a topic
  2517. * @uses do_action() Calls 'bbp_trashed_topic' with the topic id
  2518. */
  2519. function bbp_trashed_topic( $topic_id = 0 ) {
  2520. $topic_id = bbp_get_topic_id( $topic_id );
  2521. if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
  2522. return false;
  2523. do_action( 'bbp_trashed_topic', $topic_id );
  2524. }
  2525. /**
  2526. * Called after untrashing a topic
  2527. *
  2528. * @uses bbp_get_topic_id() To get the topic id
  2529. * @uses bbp_is_topic() To check if the passed id is a topic
  2530. * @uses do_action() Calls 'bbp_untrashed_topic' with the topic id
  2531. */
  2532. function bbp_untrashed_topic( $topic_id = 0 ) {
  2533. $topic_id = bbp_get_topic_id( $topic_id );
  2534. if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
  2535. return false;
  2536. do_action( 'bbp_untrashed_topic', $topic_id );
  2537. }
  2538. /** Settings ******************************************************************/
  2539. /**
  2540. * Return the topics per page setting
  2541. *
  2542. * @since bbPress (r3540)
  2543. *
  2544. * @param int $default Default replies per page (15)
  2545. * @uses get_option() To get the setting
  2546. * @uses apply_filters() To allow the return value to be manipulated
  2547. * @return int
  2548. */
  2549. function bbp_get_topics_per_page( $default = 15 ) {
  2550. // Get database option and cast as integer
  2551. $retval = get_option( '_bbp_topics_per_page', $default );
  2552. // If return val is empty, set it to default
  2553. if ( empty( $retval ) )
  2554. $retval = $default;
  2555. // Filter and return
  2556. return (int) apply_filters( 'bbp_get_topics_per_page', $retval, $default );
  2557. }
  2558. /**
  2559. * Return the topics per RSS page setting
  2560. *
  2561. * @since bbPress (r3540)
  2562. *
  2563. * @param int $default Default replies per page (25)
  2564. * @uses get_option() To get the setting
  2565. * @uses apply_filters() To allow the return value to be manipulated
  2566. * @return int
  2567. */
  2568. function bbp_get_topics_per_rss_page( $default = 25 ) {
  2569. // Get database option and cast as integer
  2570. $retval = get_option( '_bbp_topics_per_rss_page', $default );
  2571. // If return val is empty, set it to default
  2572. if ( empty( $retval ) )
  2573. $retval = $default;
  2574. // Filter and return
  2575. return (int) apply_filters( 'bbp_get_topics_per_rss_page', $retval, $default );
  2576. }
  2577. /** Topic Tags ****************************************************************/
  2578. /**
  2579. * Get topic tags for a specific topic ID
  2580. *
  2581. * @since bbPress (r4165)
  2582. *
  2583. * @param int $topic_id
  2584. * @param string $sep
  2585. * @return string
  2586. */
  2587. function bbp_get_topic_tag_names( $topic_id = 0, $sep = ', ' ) {
  2588. $topic_id = bbp_get_topic_id( $topic_id );
  2589. $topic_tags = array_filter( (array) get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() ) );
  2590. $terms = array();
  2591. foreach( $topic_tags as $term ) {
  2592. $terms[] = $term->name;
  2593. }
  2594. $terms = !empty( $terms ) ? implode( $sep, $terms ) : '';
  2595. return apply_filters( 'bbp_get_topic_tags', $terms, $topic_id );
  2596. }
  2597. /** Autoembed *****************************************************************/
  2598. /**
  2599. * Check if autoembeds are enabled and hook them in if so
  2600. *
  2601. * @since bbPress (r3752)
  2602. * @global WP_Embed $wp_embed
  2603. */
  2604. function bbp_topic_content_autoembed() {
  2605. global $wp_embed;
  2606. if ( bbp_use_autoembed() && is_a( $wp_embed, 'WP_Embed' ) ) {
  2607. add_filter( 'bbp_get_topic_content', array( $wp_embed, 'autoembed' ), 2 );
  2608. }
  2609. }
  2610. /** Feeds *********************************************************************/
  2611. /**
  2612. * Output an RSS2 feed of topics, based on the query passed.
  2613. *
  2614. * @since bbPress (r3171)
  2615. *
  2616. * @uses bbp_version()
  2617. * @uses bbp_is_single_topic()
  2618. * @uses bbp_user_can_view_forum()
  2619. * @uses bbp_get_topic_forum_id()
  2620. * @uses bbp_show_load_topic()
  2621. * @uses bbp_topic_permalink()
  2622. * @uses bbp_topic_title()
  2623. * @uses bbp_get_topic_reply_count()
  2624. * @uses bbp_topic_content()
  2625. * @uses bbp_has_topics()
  2626. * @uses bbp_topics()
  2627. * @uses bbp_the_topic()
  2628. * @uses get_wp_title_rss()
  2629. * @uses get_option()
  2630. * @uses bloginfo_rss
  2631. * @uses self_link()
  2632. * @uses the_author()
  2633. * @uses get_post_time()
  2634. * @uses rss_enclosure()
  2635. * @uses do_action()
  2636. * @uses apply_filters()
  2637. *
  2638. * @param array $topics_query
  2639. */
  2640. function bbp_display_topics_feed_rss2( $topics_query = array() ) {
  2641. // User cannot access this forum
  2642. if ( bbp_is_single_forum() && !bbp_user_can_view_forum( array( 'forum_id' => bbp_get_forum_id() ) ) )
  2643. return;
  2644. // Display the feed
  2645. header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . get_option( 'blog_charset' ), true );
  2646. header( 'Status: 200 OK' );
  2647. echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>
  2648. <rss version="2.0"
  2649. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  2650. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  2651. xmlns:dc="http://purl.org/dc/elements/1.1/"
  2652. xmlns:atom="http://www.w3.org/2005/Atom"
  2653. <?php do_action( 'bbp_feed' ); ?>
  2654. >
  2655. <channel>
  2656. <title><?php bloginfo_rss( 'name' ); ?> &#187; <?php _e( 'All Topics', 'bbpress' ); ?></title>
  2657. <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
  2658. <link><?php self_link(); ?></link>
  2659. <description><?php //?></description>
  2660. <pubDate><?php echo mysql2date( 'D, d M Y H:i:s O', current_time( 'mysql' ), false ); ?></pubDate>
  2661. <generator>http://bbpress.org/?v=<?php bbp_version(); ?></generator>
  2662. <language><?php bloginfo_rss( 'language' ); ?></language>
  2663. <?php do_action( 'bbp_feed_head' ); ?>
  2664. <?php if ( bbp_has_topics( $topics_query ) ) : ?>
  2665. <?php while ( bbp_topics() ) : bbp_the_topic(); ?>
  2666. <item>
  2667. <guid><?php bbp_topic_permalink(); ?></guid>
  2668. <title><![CDATA[<?php bbp_topic_title(); ?>]]></title>
  2669. <link><?php bbp_topic_permalink(); ?></link>
  2670. <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_meta( bbp_get_topic_id(), '_bbp_last_active_time', true ) ); ?></pubDate>
  2671. <dc:creator><?php the_author() ?></dc:creator>
  2672. <?php if ( !post_password_required() ) : ?>
  2673. <description>
  2674. <![CDATA[
  2675. <p><?php printf( __( 'Replies: %s', 'bbpress' ), bbp_get_topic_reply_count() ); ?></p>
  2676. <?php bbp_topic_content(); ?>
  2677. ]]>
  2678. </description>
  2679. <?php rss_enclosure(); ?>
  2680. <?php endif; ?>
  2681. <?php do_action( 'bbp_feed_item' ); ?>
  2682. </item>
  2683. <?php endwhile; ?>
  2684. <?php endif; ?>
  2685. <?php do_action( 'bbp_feed_footer' ); ?>
  2686. </channel>
  2687. </rss>
  2688. <?php
  2689. exit();
  2690. }
  2691. /** Permissions ***************************************************************/
  2692. /**
  2693. * Redirect if unathorized user is attempting to edit a topic
  2694. *
  2695. * @since bbPress (r3605)
  2696. *
  2697. * @uses bbp_is_topic_edit()
  2698. * @uses current_user_can()
  2699. * @uses bbp_get_topic_id()
  2700. * @uses wp_safe_redirect()
  2701. * @uses bbp_get_topic_permalink()
  2702. */
  2703. function bbp_check_topic_edit() {
  2704. // Bail if not editing a topic
  2705. if ( !bbp_is_topic_edit() )
  2706. return;
  2707. // User cannot edit topic, so redirect back to topic
  2708. if ( !current_user_can( 'edit_topic', bbp_get_topic_id() ) ) {
  2709. wp_safe_redirect( bbp_get_topic_permalink() );
  2710. exit();
  2711. }
  2712. }
  2713. /**
  2714. * Redirect if unathorized user is attempting to edit a topic tag
  2715. *
  2716. * @since bbPress (r3605)
  2717. *
  2718. * @uses bbp_is_topic_tag_edit()
  2719. * @uses current_user_can()
  2720. * @uses bbp_get_topic_tag_id()
  2721. * @uses wp_safe_redirect()
  2722. * @uses bbp_get_topic_tag_link()
  2723. */
  2724. function bbp_check_topic_tag_edit() {
  2725. // Bail if not editing a topic tag
  2726. if ( !bbp_is_topic_tag_edit() )
  2727. return;
  2728. // Bail if current user cannot edit topic tags
  2729. if ( !current_user_can( 'edit_topic_tags', bbp_get_topic_tag_id() ) ) {
  2730. wp_safe_redirect( bbp_get_topic_tag_link() );
  2731. exit();
  2732. }
  2733. }