[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/topics/ -> functions.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress Topic Functions
   5   *
   6   * @package bbPress
   7   * @subpackage Functions
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /** Insert ********************************************************************/
  14  
  15  /**
  16   * A wrapper for wp_insert_post() that also includes the necessary meta values
  17   * for the topic to function properly.
  18   *
  19   * @since 2.0.0 bbPress (r3349)
  20   *
  21   * @param array $topic_data Forum post data
  22   * @param arrap $topic_meta Forum meta data
  23   */
  24  function bbp_insert_topic( $topic_data = array(), $topic_meta = array() ) {
  25  
  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  
  39      // Insert topic
  40      $topic_id = wp_insert_post( $topic_data, false );
  41  
  42      // Bail if no topic was added
  43      if ( empty( $topic_id ) ) {
  44          return false;
  45      }
  46  
  47      // Parse arguments against default values
  48      $topic_meta = bbp_parse_args( $topic_meta, array(
  49          'author_ip'          => bbp_current_author_ip(),
  50          'forum_id'           => 0,
  51          'topic_id'           => $topic_id,
  52          'voice_count'        => 1,
  53          'reply_count'        => 0,
  54          'reply_count_hidden' => 0,
  55          'last_reply_id'      => 0,
  56          'last_active_id'     => $topic_id,
  57          'last_active_time'   => get_post_field( 'post_date', $topic_id, 'db' )
  58      ), 'insert_topic_meta' );
  59  
  60      // Insert topic meta
  61      foreach ( $topic_meta as $meta_key => $meta_value ) {
  62  
  63          // Prefix if not prefixed
  64          if ( '_bbp_' !== substr( $meta_key, 0, 5 ) ) {
  65              $meta_key = '_bbp_' . $meta_key;
  66          }
  67  
  68          // Update the meta
  69          update_post_meta( $topic_id, $meta_key, $meta_value );
  70      }
  71  
  72      // Update the topic and hierarchy
  73      bbp_update_topic( $topic_id, $topic_meta['forum_id'], array(), $topic_data['post_author'], false );
  74  
  75      /**
  76       * Fires after topic has been inserted via `bbp_insert_topic`.
  77       *
  78       * @since 2.6.0 bbPress (r6036)
  79       *
  80       * @param int $topic_id               The topic id.
  81       * @param int $topic_meta['forum_id'] The topic forum meta.
  82       */
  83      do_action( 'bbp_insert_topic', (int) $topic_id, (int) $topic_meta['forum_id'] );
  84  
  85      // Return topic_id
  86      return $topic_id;
  87  }
  88  
  89  /** Post Form Handlers ********************************************************/
  90  
  91  /**
  92   * Handles the front end topic submission
  93   *
  94   * @param string $action The requested action to compare this function to
  95   */
  96  function bbp_new_topic_handler( $action = '' ) {
  97  
  98      // Bail if action is not bbp-new-topic
  99      if ( 'bbp-new-topic' !== $action ) {
 100          return;
 101      }
 102  
 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  
 109      // Define local variable(s)
 110      $view_all = false;
 111      $forum_id = $topic_author = 0;
 112      $topic_title = $topic_content = '';
 113      $anonymous_data = array();
 114      $terms = array( bbp_get_topic_tag_tax_id() => array() );
 115  
 116      /** Topic Author **********************************************************/
 117  
 118      // User is anonymous
 119      if ( bbp_is_anonymous() ) {
 120  
 121          // Filter anonymous data (variable is used later)
 122          $anonymous_data = bbp_filter_anonymous_post_data();
 123  
 124          // Anonymous data checks out, so set cookies, etc...
 125          bbp_set_current_anonymous_user_data( $anonymous_data );
 126  
 127      // User is logged in
 128      } else {
 129  
 130          // User cannot create topics
 131          if ( ! current_user_can( 'publish_topics' ) ) {
 132              bbp_add_error( 'bbp_topic_permission', __( '<strong>ERROR</strong>: You do not have permission to create new topics.', 'bbpress' ) );
 133              return;
 134          }
 135  
 136          // Topic author is current user
 137          $topic_author = bbp_get_current_user_id();
 138      }
 139  
 140      // Remove kses filters from title and content for capable users and if the nonce is verified
 141      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'] ) {
 142          remove_filter( 'bbp_new_topic_pre_title',   'wp_filter_kses'      );
 143          remove_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad',  10 );
 144          remove_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 );
 145      }
 146  
 147      /** Topic Title ***********************************************************/
 148  
 149      if ( ! empty( $_POST['bbp_topic_title'] ) ) {
 150          $topic_title = sanitize_text_field( $_POST['bbp_topic_title'] );
 151      }
 152  
 153      // Filter and sanitize
 154      $topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
 155  
 156      // No topic title
 157      if ( empty( $topic_title ) ) {
 158          bbp_add_error( 'bbp_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
 159      }
 160  
 161      // Title too long
 162      if ( bbp_is_title_too_long( $topic_title ) ) {
 163          bbp_add_error( 'bbp_topic_title', __( '<strong>ERROR</strong>: Your title is too long.', 'bbpress' ) );
 164      }
 165  
 166      /** Topic Content *********************************************************/
 167  
 168      if ( ! empty( $_POST['bbp_topic_content'] ) ) {
 169          $topic_content = $_POST['bbp_topic_content'];
 170      }
 171  
 172      // Filter and sanitize
 173      $topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
 174  
 175      // No topic content
 176      if ( empty( $topic_content ) ) {
 177          bbp_add_error( 'bbp_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
 178      }
 179  
 180      /** Topic Forum ***********************************************************/
 181  
 182      // Error check the POST'ed topic id
 183      if ( isset( $_POST['bbp_forum_id'] ) ) {
 184  
 185          // Empty Forum id was passed
 186          if ( empty( $_POST['bbp_forum_id'] ) ) {
 187              bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
 188  
 189          // Forum id is not a number
 190          } elseif ( ! is_numeric( $_POST['bbp_forum_id'] ) ) {
 191              bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID must be a number.', 'bbpress' ) );
 192  
 193          // Forum id might be valid
 194          } else {
 195  
 196              // Get the forum id
 197              $posted_forum_id = intval( $_POST['bbp_forum_id'] );
 198  
 199              // Forum id is empty
 200              if ( 0 === $posted_forum_id ) {
 201                  bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
 202  
 203              // Forum id is a negative number
 204              } elseif ( 0 > $posted_forum_id ) {
 205                  bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID cannot be a negative number.', 'bbpress' ) );
 206  
 207              // Forum does not exist
 208              } elseif ( ! bbp_get_forum( $posted_forum_id ) ) {
 209                  bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum does not exist.', 'bbpress' ) );
 210  
 211              // Use the POST'ed forum id
 212              } else {
 213                  $forum_id = $posted_forum_id;
 214              }
 215          }
 216      }
 217  
 218      // Forum exists
 219      if ( ! empty( $forum_id ) ) {
 220  
 221          // Forum is a category
 222          if ( bbp_is_forum_category( $forum_id ) ) {
 223              bbp_add_error( 'bbp_new_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in this forum.', 'bbpress' ) );
 224  
 225          // Forum is not a category
 226          } else {
 227  
 228              // Forum is closed and user cannot access
 229              if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
 230                  bbp_add_error( 'bbp_new_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
 231              }
 232  
 233              // Forum is private and user cannot access
 234              if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
 235                  bbp_add_error( 'bbp_new_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
 236  
 237              // Forum is hidden and user cannot access
 238              } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
 239                  bbp_add_error( 'bbp_new_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
 240              }
 241          }
 242      }
 243  
 244      /** Topic Flooding ********************************************************/
 245  
 246      if ( ! bbp_check_for_flood( $anonymous_data, $topic_author ) ) {
 247          bbp_add_error( 'bbp_topic_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
 248      }
 249  
 250      /** Topic Duplicate *******************************************************/
 251  
 252      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 ) ) ) {
 253          bbp_add_error( 'bbp_topic_duplicate', __( '<strong>ERROR</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that.', 'bbpress' ) );
 254      }
 255  
 256      /** Topic Bad Words *******************************************************/
 257  
 258      if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content, true ) ) {
 259          bbp_add_error( 'bbp_topic_moderation', __( '<strong>ERROR</strong>: Your topic cannot be created at this time.', 'bbpress' ) );
 260      }
 261  
 262      /** Topic Status **********************************************************/
 263  
 264      // Get available topic statuses
 265      $topic_statuses = bbp_get_topic_statuses();
 266  
 267      // Maybe put into moderation
 268      if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
 269          $topic_status = bbp_get_pending_status_id();
 270  
 271      // Check possible topic status ID's
 272      } elseif ( ! empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
 273          $topic_status = sanitize_key( $_POST['bbp_topic_status'] );
 274  
 275      // Default to published if nothing else
 276      } else {
 277          $topic_status = bbp_get_public_status_id();
 278      }
 279  
 280      /** Topic Tags ************************************************************/
 281  
 282      if ( bbp_allow_topic_tags() && ! empty( $_POST['bbp_topic_tags'] ) ) {
 283  
 284          // Escape tag input
 285          $terms = sanitize_text_field( $_POST['bbp_topic_tags'] );
 286  
 287          // Explode by comma
 288          if ( strstr( $terms, ',' ) ) {
 289              $terms = explode( ',', $terms );
 290          }
 291  
 292          // Add topic tag ID as main key
 293          $terms = array( bbp_get_topic_tag_tax_id() => $terms );
 294      }
 295  
 296      /** Additional Actions (Before Save) **************************************/
 297  
 298      do_action( 'bbp_new_topic_pre_extras', $forum_id );
 299  
 300      // Bail if errors
 301      if ( bbp_has_errors() ) {
 302          return;
 303      }
 304  
 305      /** No Errors *************************************************************/
 306  
 307      // Add the content of the form to $topic_data as an array.
 308      // Just in time manipulation of topic data before being created
 309      $topic_data = apply_filters( 'bbp_new_topic_pre_insert', array(
 310          'post_author'    => $topic_author,
 311          'post_title'     => $topic_title,
 312          'post_content'   => $topic_content,
 313          'post_status'    => $topic_status,
 314          'post_parent'    => $forum_id,
 315          'post_type'      => bbp_get_topic_post_type(),
 316          'tax_input'      => $terms,
 317          'comment_status' => 'closed'
 318      ) );
 319  
 320      // Insert topic
 321      $topic_id = wp_insert_post( $topic_data, true );
 322  
 323      /** No Errors *************************************************************/
 324  
 325      if ( ! empty( $topic_id ) && ! is_wp_error( $topic_id ) ) {
 326  
 327          /** Close Check *******************************************************/
 328  
 329          // If the topic is closed, close it properly
 330          if ( ( get_post_field( 'post_status', $topic_id ) === bbp_get_closed_status_id() ) || ( $topic_data['post_status'] === bbp_get_closed_status_id() ) ) {
 331  
 332              // Close the topic
 333              bbp_close_topic( $topic_id );
 334          }
 335  
 336          /** Trash Check *******************************************************/
 337  
 338          // If the forum is trash, or the topic_status is switched to
 339          // trash, trash the topic properly
 340          if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $topic_data['post_status'] === bbp_get_trash_status_id() ) ) {
 341  
 342              // Trash the topic
 343              wp_trash_post( $topic_id );
 344  
 345              // Force view=all
 346              $view_all = true;
 347          }
 348  
 349          /** Spam Check ********************************************************/
 350  
 351          // If the topic is spam, officially spam this topic
 352          if ( $topic_data['post_status'] === bbp_get_spam_status_id() ) {
 353              add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
 354  
 355              // Force view=all
 356              $view_all = true;
 357          }
 358  
 359          /** Update counts, etc... *********************************************/
 360  
 361          do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
 362  
 363          /** Additional Actions (After Save) ***********************************/
 364  
 365          do_action( 'bbp_new_topic_post_extras', $topic_id );
 366  
 367          /** Redirect **********************************************************/
 368  
 369          // Redirect to
 370          $redirect_to = bbp_get_redirect_to();
 371  
 372          // Get the topic URL
 373          $redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
 374  
 375          // Add view all?
 376          if ( bbp_get_view_all() || ! empty( $view_all ) ) {
 377  
 378              // User can moderate, so redirect to topic with view all set
 379              if ( current_user_can( 'moderate', $topic_id ) ) {
 380                  $redirect_url = bbp_add_view_all( $redirect_url );
 381  
 382              // User cannot moderate, so redirect to forum
 383              } else {
 384                  $redirect_url = bbp_get_forum_permalink( $forum_id );
 385              }
 386          }
 387  
 388          // Allow to be filtered
 389          $redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id );
 390  
 391          /** Successful Save ***************************************************/
 392  
 393          // Redirect back to new topic
 394          bbp_redirect( $redirect_url );
 395  
 396      /** Errors ****************************************************************/
 397  
 398      // WP_Error
 399      } elseif ( is_wp_error( $topic_id ) ) {
 400          bbp_add_error( 'bbp_topic_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) occurred: %s', 'bbpress' ), $topic_id->get_error_message() ) );
 401  
 402      // Generic error
 403      } else {
 404          bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The topic was not created.', 'bbpress' ) );
 405      }
 406  }
 407  
 408  /**
 409   * Handles the front end edit topic submission
 410   *
 411   * @param string $action The requested action to compare this function to
 412   */
 413  function bbp_edit_topic_handler( $action = '' ) {
 414  
 415      // Bail if action is not bbp-edit-topic
 416      if ( 'bbp-edit-topic' !== $action ) {
 417          return;
 418      }
 419  
 420      // Define local variable(s)
 421      $revisions_removed = false;
 422      $topic = $topic_id = $topic_author = $forum_id = 0;
 423      $topic_title = $topic_content = $topic_edit_reason = '';
 424      $anonymous_data = array();
 425  
 426      /** Topic *****************************************************************/
 427  
 428      // Topic id was not passed
 429      if ( empty( $_POST['bbp_topic_id'] ) ) {
 430          bbp_add_error( 'bbp_edit_topic_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
 431          return;
 432  
 433      // Topic id was passed
 434      } elseif ( is_numeric( $_POST['bbp_topic_id'] ) ) {
 435          $topic_id = (int) $_POST['bbp_topic_id'];
 436          $topic    = bbp_get_topic( $topic_id );
 437      }
 438  
 439      // Topic does not exist
 440      if ( empty( $topic ) ) {
 441          bbp_add_error( 'bbp_edit_topic_not_found', __( '<strong>ERROR</strong>: The topic you want to edit was not found.', 'bbpress' ) );
 442          return;
 443  
 444      // Topic exists
 445      } else {
 446  
 447          // Check users ability to create new topic
 448          if ( ! bbp_is_topic_anonymous( $topic_id ) ) {
 449  
 450              // User cannot edit this topic
 451              if ( ! current_user_can( 'edit_topic', $topic_id ) ) {
 452                  bbp_add_error( 'bbp_edit_topic_permission', __( '<strong>ERROR</strong>: You do not have permission to edit that topic.', 'bbpress' ) );
 453              }
 454  
 455              // Set topic author
 456              $topic_author = bbp_get_topic_author_id( $topic_id );
 457  
 458          // It is an anonymous post
 459          } else {
 460  
 461              // Filter anonymous data
 462              $anonymous_data = bbp_filter_anonymous_post_data();
 463          }
 464      }
 465  
 466      // Nonce check
 467      if ( ! bbp_verify_nonce_request( 'bbp-edit-topic_' . $topic_id ) ) {
 468          bbp_add_error( 'bbp_edit_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
 469          return;
 470      }
 471  
 472      // Remove kses filters from title and content for capable users and if the nonce is verified
 473      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'] ) ) {
 474          remove_filter( 'bbp_edit_topic_pre_title',   'wp_filter_kses'      );
 475          remove_filter( 'bbp_edit_topic_pre_content', 'bbp_encode_bad',  10 );
 476          remove_filter( 'bbp_edit_topic_pre_content', 'bbp_filter_kses', 30 );
 477      }
 478  
 479      /** Topic Forum ***********************************************************/
 480  
 481      // Forum id was not passed
 482      if ( empty( $_POST['bbp_forum_id'] ) ) {
 483          bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
 484  
 485      // Forum id was passed
 486      } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) {
 487          $forum_id = (int) $_POST['bbp_forum_id'];
 488      }
 489  
 490      // Current forum this topic is in
 491      $current_forum_id = bbp_get_topic_forum_id( $topic_id );
 492  
 493      // Forum exists
 494      if ( ! empty( $forum_id ) && ( $forum_id !== $current_forum_id ) ) {
 495  
 496          // Forum is a category
 497          if ( bbp_is_forum_category( $forum_id ) ) {
 498              bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in it.', 'bbpress' ) );
 499  
 500          // Forum is not a category
 501          } else {
 502  
 503              // Forum is closed and user cannot access
 504              if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
 505                  bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
 506              }
 507  
 508              // Forum is private and user cannot access
 509              if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
 510                  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' ) );
 511  
 512              // Forum is hidden and user cannot access
 513              } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
 514                  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' ) );
 515              }
 516          }
 517      }
 518  
 519      /** Topic Title ***********************************************************/
 520  
 521      if ( ! empty( $_POST['bbp_topic_title'] ) ) {
 522          $topic_title = sanitize_text_field( $_POST['bbp_topic_title'] );
 523      }
 524  
 525      // Filter and sanitize
 526      $topic_title = apply_filters( 'bbp_edit_topic_pre_title', $topic_title, $topic_id );
 527  
 528      // No topic title
 529      if ( empty( $topic_title ) ) {
 530          bbp_add_error( 'bbp_edit_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
 531      }
 532  
 533      // Title too long
 534      if ( bbp_is_title_too_long( $topic_title ) ) {
 535          bbp_add_error( 'bbp_topic_title', __( '<strong>ERROR</strong>: Your title is too long.', 'bbpress' ) );
 536      }
 537  
 538      /** Topic Content *********************************************************/
 539  
 540      if ( ! empty( $_POST['bbp_topic_content'] ) ) {
 541          $topic_content = $_POST['bbp_topic_content'];
 542      }
 543  
 544      // Filter and sanitize
 545      $topic_content = apply_filters( 'bbp_edit_topic_pre_content', $topic_content, $topic_id );
 546  
 547      // No topic content
 548      if ( empty( $topic_content ) ) {
 549          bbp_add_error( 'bbp_edit_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
 550      }
 551  
 552      /** Topic Bad Words *******************************************************/
 553  
 554      if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content, true ) ) {
 555          bbp_add_error( 'bbp_topic_moderation', __( '<strong>ERROR</strong>: Your topic cannot be edited at this time.', 'bbpress' ) );
 556      }
 557  
 558      /** Topic Status **********************************************************/
 559  
 560      // Get available topic statuses
 561      $topic_statuses = bbp_get_topic_statuses( $topic_id );
 562  
 563      // Maybe put into moderation
 564      if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
 565  
 566          // Set post status to pending if public or closed
 567          if ( bbp_is_topic_public( $topic->ID ) ) {
 568              $topic_status = bbp_get_pending_status_id();
 569          }
 570  
 571      // Check possible topic status ID's
 572      } elseif ( ! empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
 573          $topic_status = sanitize_key( $_POST['bbp_topic_status'] );
 574  
 575      // Use existing post_status
 576      } else {
 577          $topic_status = $topic->post_status;
 578      }
 579  
 580      /** Topic Tags ************************************************************/
 581  
 582      // Either replace terms
 583      if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags', $topic_id ) && ! empty( $_POST['bbp_topic_tags'] ) ) {
 584  
 585          // Escape tag input
 586          $terms = sanitize_text_field( $_POST['bbp_topic_tags'] );
 587  
 588          // Explode by comma
 589          if ( strstr( $terms, ',' ) ) {
 590              $terms = explode( ',', $terms );
 591          }
 592  
 593          // Add topic tag ID as main key
 594          $terms = array( bbp_get_topic_tag_tax_id() => $terms );
 595  
 596      // ...or remove them.
 597      } elseif ( isset( $_POST['bbp_topic_tags'] ) ) {
 598          $terms = array( bbp_get_topic_tag_tax_id() => array() );
 599  
 600      // Existing terms
 601      } else {
 602          $terms = array( bbp_get_topic_tag_tax_id() => explode( ',', bbp_get_topic_tag_names( $topic_id, ',' ) ) );
 603      }
 604  
 605      /** Additional Actions (Before Save) **************************************/
 606  
 607      do_action( 'bbp_edit_topic_pre_extras', $topic_id );
 608  
 609      // Bail if errors
 610      if ( bbp_has_errors() ) {
 611          return;
 612      }
 613  
 614      /** No Errors *************************************************************/
 615  
 616      // Add the content of the form to $topic_data as an array
 617      // Just in time manipulation of topic data before being edited
 618      $topic_data = apply_filters( 'bbp_edit_topic_pre_insert', array(
 619          'ID'           => $topic_id,
 620          'post_title'   => $topic_title,
 621          'post_content' => $topic_content,
 622          'post_status'  => $topic_status,
 623          'post_parent'  => $forum_id,
 624          'post_author'  => $topic_author,
 625          'post_type'    => bbp_get_topic_post_type(),
 626          'tax_input'    => $terms,
 627      ) );
 628  
 629      // Toggle revisions to avoid duplicates
 630      if ( post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) {
 631          $revisions_removed = true;
 632          remove_post_type_support( bbp_get_topic_post_type(), 'revisions' );
 633      }
 634  
 635      // Insert topic
 636      $topic_id = wp_update_post( $topic_data );
 637  
 638      // Toggle revisions back on
 639      if ( true === $revisions_removed ) {
 640          $revisions_removed = false;
 641          add_post_type_support( bbp_get_topic_post_type(), 'revisions' );
 642      }
 643  
 644      /** No Errors *************************************************************/
 645  
 646      if ( ! empty( $topic_id ) && ! is_wp_error( $topic_id ) ) {
 647  
 648          // Update counts, etc...
 649          do_action( 'bbp_edit_topic', $topic_id, $forum_id, $anonymous_data, $topic_author , true /* Is edit */ );
 650  
 651          /** Revisions *********************************************************/
 652  
 653          // Update locks
 654          update_post_meta( $topic_id, '_edit_last', bbp_get_current_user_id() );
 655          delete_post_meta( $topic_id, '_edit_lock' );
 656  
 657          // Revision Reason
 658          if ( ! empty( $_POST['bbp_topic_edit_reason'] ) ) {
 659              $topic_edit_reason = sanitize_text_field( $_POST['bbp_topic_edit_reason'] );
 660          }
 661  
 662          // Update revision log
 663          if ( ! empty( $_POST['bbp_log_topic_edit'] ) && ( "1" === $_POST['bbp_log_topic_edit'] ) )  {
 664              $revision_id = wp_save_post_revision( $topic_id );
 665              if ( ! empty( $revision_id ) ) {
 666                  bbp_update_topic_revision_log( array(
 667                      'topic_id'    => $topic_id,
 668                      'revision_id' => $revision_id,
 669                      'author_id'   => bbp_get_current_user_id(),
 670                      'reason'      => $topic_edit_reason
 671                  ) );
 672              }
 673          }
 674  
 675          /** Move Topic ********************************************************/
 676  
 677          // If the new forum id is not equal to the old forum id, run the
 678          // bbp_move_topic action and pass the topic's forum id as the
 679          // first arg and topic id as the second to update counts.
 680          if ( $forum_id !== $topic->post_parent ) {
 681              bbp_move_topic_handler( $topic_id, $topic->post_parent, $forum_id );
 682          }
 683  
 684          /** Additional Actions (After Save) ***********************************/
 685  
 686          do_action( 'bbp_edit_topic_post_extras', $topic_id );
 687  
 688          /** Redirect **********************************************************/
 689  
 690          // Redirect to
 691          $redirect_to = bbp_get_redirect_to();
 692  
 693          // View all?
 694          $view_all = bbp_get_view_all( 'edit_others_replies' );
 695  
 696          // Get the topic URL
 697          $topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
 698  
 699          // Add view all?
 700          if ( ! empty( $view_all ) ) {
 701              $topic_url = bbp_add_view_all( $topic_url );
 702          }
 703  
 704          // Allow to be filtered
 705          $topic_url = apply_filters( 'bbp_edit_topic_redirect_to', $topic_url, $view_all, $redirect_to );
 706  
 707          /** Successful Edit ***************************************************/
 708  
 709          // Redirect back to new topic
 710          bbp_redirect( $topic_url );
 711  
 712      /** Errors ****************************************************************/
 713  
 714      } else {
 715          $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
 716          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' ) );
 717      }
 718  }
 719  
 720  /**
 721   * Handle all the extra meta stuff from posting a new topic
 722   *
 723   * @param int $topic_id Optional. Topic id
 724   * @param int $forum_id Optional. Forum id
 725   * @param array $anonymous_data Optional - if it's an anonymous post. Do not
 726   *                              supply if supplying $author_id. Should be
 727   *                              sanitized (see {@link bbp_filter_anonymous_post_data()}
 728   * @param int $author_id Author id
 729   * @param bool $is_edit Optional. Is the post being edited? Defaults to false.
 730   */
 731  function bbp_update_topic( $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $author_id = 0, $is_edit = false ) {
 732  
 733      // Validate the ID's passed from 'bbp_new_topic' action
 734      $topic_id = bbp_get_topic_id( $topic_id );
 735      $forum_id = bbp_get_forum_id( $forum_id );
 736  
 737      // Bail if there is no topic
 738      if ( empty( $topic_id ) ) {
 739          return;
 740      }
 741  
 742      // Check author_id
 743      if ( empty( $author_id ) ) {
 744          $author_id = bbp_get_current_user_id();
 745      }
 746  
 747      // Forum/Topic meta (early, for use in downstream functions)
 748      bbp_update_topic_forum_id( $topic_id, $forum_id );
 749      bbp_update_topic_topic_id( $topic_id, $topic_id );
 750  
 751      // Get the topic types
 752      $topic_types = bbp_get_topic_types( $topic_id );
 753  
 754      // Sticky check after 'bbp_new_topic' action so forum ID meta is set
 755      if ( ! empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array_keys( $topic_types ), true ) ) {
 756  
 757          // What's the caps?
 758          if ( current_user_can( 'moderate', $topic_id ) ) {
 759  
 760              // What's the haps?
 761              switch ( $_POST['bbp_stick_topic'] ) {
 762  
 763                  // Sticky in this forum
 764                  case 'stick'   :
 765                      bbp_stick_topic( $topic_id );
 766                      break;
 767  
 768                  // Super sticky in all forums
 769                  case 'super'   :
 770                      bbp_stick_topic( $topic_id, true );
 771                      break;
 772  
 773                  // We can avoid this as it is a new topic
 774                  case 'unstick' :
 775                  default        :
 776                      break;
 777              }
 778          }
 779      }
 780  
 781      // If anonymous post, store name, email, website and ip in post_meta.
 782      if ( ! empty( $anonymous_data ) ) {
 783  
 784          // Update anonymous meta data (not cookies)
 785          bbp_update_anonymous_post_author( $topic_id, $anonymous_data, bbp_get_topic_post_type() );
 786  
 787          // Set transient for throttle check (only on new, not edit)
 788          if ( empty( $is_edit ) ) {
 789              set_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted', time() );
 790          }
 791  
 792      } else {
 793          if ( empty( $is_edit ) && ! current_user_can( 'throttle' ) ) {
 794              bbp_update_user_last_posted( $author_id );
 795          }
 796      }
 797  
 798      // Handle Subscription Checkbox
 799      if ( bbp_is_subscriptions_active() && ! empty( $author_id ) ) {
 800  
 801          // Check if subscribed
 802          $subscribed = bbp_is_user_subscribed( $author_id, $topic_id );
 803  
 804          // Check for action
 805          $subscheck  = ( ! empty( $_POST['bbp_topic_subscription'] ) && ( 'bbp_subscribe' === $_POST['bbp_topic_subscription'] ) )
 806              ? true
 807              : false;
 808  
 809          // Subscribed and unsubscribing
 810          if ( ( true === $subscribed ) && ( false === $subscheck ) ) {
 811              bbp_remove_user_subscription( $author_id, $topic_id );
 812  
 813          // Not subscribed and subscribing
 814          } elseif ( ( false === $subscribed ) && ( true === $subscheck ) ) {
 815              bbp_add_user_subscription( $author_id, $topic_id );
 816          }
 817      }
 818  
 819      // Update associated topic values if this is a new topic
 820      if ( empty( $is_edit ) ) {
 821  
 822          // Update poster IP if not editing
 823          update_post_meta( $topic_id, '_bbp_author_ip', bbp_current_author_ip(), false );
 824  
 825          // Last active time
 826          $last_active = get_post_field( 'post_date', $topic_id );
 827  
 828          // Reply topic meta
 829          bbp_update_topic_last_reply_id      ( $topic_id, 0            );
 830          bbp_update_topic_last_active_id     ( $topic_id, $topic_id    );
 831          bbp_update_topic_last_active_time   ( $topic_id, $last_active );
 832          bbp_update_topic_reply_count        ( $topic_id, 0            );
 833          bbp_update_topic_reply_count_hidden ( $topic_id, 0            );
 834          bbp_update_topic_voice_count        ( $topic_id               );
 835  
 836          // Walk up ancestors and do the dirty work
 837          bbp_update_topic_walker( $topic_id, $last_active, $forum_id, 0, false );
 838      }
 839  
 840      // Bump the custom query cache
 841      wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );
 842  }
 843  
 844  /**
 845   * Walks up the post_parent tree from the current topic_id, and updates the
 846   * meta data of forums above it. This calls several functions that all run
 847   * manual queries against the database to get their results. As such, this
 848   * function can be costly to run but is necessary to keep everything accurate.
 849   *
 850   * @since 2.0.0 bbPress (r2800)
 851   *
 852   * @param int $topic_id Topic id
 853   * @param string $last_active_time Optional. Last active time
 854   * @param int $forum_id Optional. Forum id
 855   * @param int $reply_id Optional. Reply id
 856   * @param bool $refresh Reset all the previous parameters? Defaults to true.
 857   */
 858  function bbp_update_topic_walker( $topic_id, $last_active_time = '', $forum_id = 0, $reply_id = 0, $refresh = true ) {
 859  
 860      // Validate topic_id
 861      $topic_id  = bbp_get_topic_id( $topic_id );
 862  
 863      // Define local variable(s)
 864      $active_id = 0;
 865  
 866      // Topic was passed
 867      if ( ! empty( $topic_id ) ) {
 868  
 869          // Get the forum ID if none was passed
 870          if ( empty( $forum_id )  ) {
 871              $forum_id = bbp_get_topic_forum_id( $topic_id );
 872          }
 873  
 874          // Set the active_id based on topic_id/reply_id
 875          $active_id = empty( $reply_id ) ? $topic_id : $reply_id;
 876      }
 877  
 878      // Get topic ancestors
 879      $ancestors = array_values( array_unique( array_merge( array( $forum_id ), (array) get_post_ancestors( $topic_id ) ) ) );
 880  
 881      // Topic status
 882      $topic_status = get_post_status( $topic_id );
 883  
 884      // If we want a full refresh, unset any of the possibly passed variables
 885      if ( true === $refresh ) {
 886          $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
 887          $topic_status = bbp_get_public_status_id();
 888      }
 889  
 890      // Loop through ancestors
 891      if ( ! empty( $ancestors ) ) {
 892          foreach ( $ancestors as $ancestor ) {
 893  
 894              // If ancestor is a forum, update counts
 895              if ( bbp_is_forum( $ancestor ) ) {
 896  
 897                  // Get the forum
 898                  $forum = bbp_get_forum( $ancestor );
 899  
 900                  // Update the forum
 901                  bbp_update_forum( array(
 902                      'forum_id'           => $forum->ID,
 903                      'post_parent'        => $forum->post_parent,
 904                      'last_topic_id'      => $topic_id,
 905                      'last_reply_id'      => $reply_id,
 906                      'last_active_id'     => $active_id,
 907                      'last_active_time'   => $last_active_time,
 908                      'last_active_status' => $topic_status
 909                  ) );
 910              }
 911          }
 912      }
 913  }
 914  
 915  /**
 916   * Handle the moving of a topic from one forum to another. This includes walking
 917   * up the old and new branches and updating the counts.
 918   *
 919   * @since 2.0.0 bbPress (r2907)
 920   *
 921   * @param int $topic_id     The topic id.
 922   * @param int $old_forum_id Old forum id.
 923   * @param int $new_forum_id New forum id.
 924   */
 925  function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) {
 926  
 927      // Validate parameters
 928      $topic_id     = bbp_get_topic_id( $topic_id     );
 929      $old_forum_id = bbp_get_forum_id( $old_forum_id );
 930      $new_forum_id = bbp_get_forum_id( $new_forum_id );
 931  
 932      // Clean old and new forum caches before proceeding, to ensure subsequent
 933      // calls to forum objects are using updated data.
 934      clean_post_cache( $old_forum_id );
 935      clean_post_cache( $new_forum_id );
 936  
 937      // Update topic forum's ID
 938      bbp_update_topic_forum_id( $topic_id, $new_forum_id );
 939  
 940      // Update topic post parent with the new forum ID
 941      wp_update_post( array(
 942          'ID'          => $topic_id,
 943          'post_parent' => $new_forum_id,
 944      ) );
 945  
 946      /** Stickies **************************************************************/
 947  
 948      // Get forum stickies
 949      $old_stickies = bbp_get_stickies( $old_forum_id );
 950  
 951      // Only proceed if stickies are found
 952      if ( ! empty( $old_stickies ) ) {
 953  
 954          // Define local variables
 955          $updated_stickies = array();
 956  
 957          // Loop through stickies of forum and add misses to the updated array
 958          foreach ( (array) $old_stickies as $sticky_topic_id ) {
 959              if ( $topic_id !== $sticky_topic_id ) {
 960                  $updated_stickies[] = $sticky_topic_id;
 961              }
 962          }
 963  
 964          // If stickies are different, update or delete them
 965          if ( $updated_stickies !== $old_stickies ) {
 966  
 967              // No more stickies so delete the meta
 968              if ( empty( $updated_stickies ) ) {
 969                  delete_post_meta( $old_forum_id, '_bbp_sticky_topics' );
 970  
 971              // Still stickies so update the meta
 972              } else {
 973                  update_post_meta( $old_forum_id, '_bbp_sticky_topics', $updated_stickies );
 974              }
 975  
 976              // Topic was sticky, so restick in new forum
 977              bbp_stick_topic( $topic_id );
 978          }
 979      }
 980  
 981      /** Topic Replies *********************************************************/
 982  
 983      // Get the topics replies
 984      $replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() );
 985  
 986      // Update the forum_id of all replies in the topic
 987      foreach ( $replies as $reply_id ) {
 988          bbp_update_reply_forum_id( $reply_id, $new_forum_id );
 989      }
 990  
 991      /** Old forum_id **********************************************************/
 992  
 993      // Get topic ancestors
 994      $old_forum_ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), (array) get_post_ancestors( $old_forum_id ) ) ) );
 995  
 996      // Public counts
 997      if ( bbp_is_topic_public( $topic_id ) ) {
 998  
 999          // Update old forum counts.
1000          bbp_decrease_forum_topic_count( $old_forum_id );
1001  
1002          // Update new forum counts.
1003          bbp_increase_forum_topic_count( $new_forum_id );
1004  
1005      // Non-public counts
1006      } else {
1007  
1008          // Update old forum counts.
1009          bbp_decrease_forum_topic_count_hidden( $old_forum_id );
1010  
1011          // Update new forum counts.
1012          bbp_increase_forum_topic_count_hidden( $new_forum_id );
1013      }
1014  
1015      // Get reply counts.
1016      $public_reply_count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
1017      $hidden_reply_count = bbp_get_non_public_child_count( $topic_id, bbp_get_reply_post_type() );
1018  
1019      // Bump reply counts.
1020      bbp_bump_forum_reply_count( $old_forum_id, -$public_reply_count );
1021      bbp_bump_forum_reply_count( $new_forum_id, $public_reply_count );
1022      bbp_bump_forum_reply_count_hidden( $old_forum_id, -$hidden_reply_count );
1023      bbp_bump_forum_reply_count_hidden( $new_forum_id, $hidden_reply_count );
1024  
1025      // Loop through ancestors and update them
1026      if ( ! empty( $old_forum_ancestors ) ) {
1027          foreach ( $old_forum_ancestors as $ancestor ) {
1028              if ( bbp_is_forum( $ancestor ) ) {
1029                  bbp_update_forum( array(
1030                      'forum_id' => $ancestor,
1031                  ) );
1032              }
1033          }
1034      }
1035  
1036      /** New forum_id **********************************************************/
1037  
1038      // Make sure we're not walking twice
1039      if ( ! in_array( $new_forum_id, $old_forum_ancestors, true ) ) {
1040  
1041          // Get topic ancestors
1042          $new_forum_ancestors = array_values( array_unique( array_merge( array( $new_forum_id ), (array) get_post_ancestors( $new_forum_id ) ) ) );
1043  
1044          // Make sure we're not walking twice
1045          $new_forum_ancestors = array_diff( $new_forum_ancestors, $old_forum_ancestors );
1046  
1047          // Loop through ancestors and update them
1048          if ( ! empty( $new_forum_ancestors ) ) {
1049              foreach ( $new_forum_ancestors as $ancestor ) {
1050                  if ( bbp_is_forum( $ancestor ) ) {
1051                      bbp_update_forum( array(
1052                          'forum_id' => $ancestor,
1053                      ) );
1054                  }
1055              }
1056          }
1057      }
1058  }
1059  
1060  /**
1061   * Merge topic handler
1062   *
1063   * Handles the front end merge topic submission
1064   *
1065   * @since 2.0.0 bbPress (r2756)
1066   *
1067   * @param string $action The requested action to compare this function to
1068   */
1069  function bbp_merge_topic_handler( $action = '' ) {
1070  
1071      // Bail if action is not bbp-merge-topic
1072      if ( 'bbp-merge-topic' !== $action ) {
1073          return;
1074      }
1075  
1076      // Define local variable(s)
1077      $source_topic_id = $destination_topic_id = 0;
1078      $source_topic = $destination_topic = 0;
1079      $subscribers = $favoriters = $replies = array();
1080  
1081      /** Source Topic **********************************************************/
1082  
1083      // Topic id
1084      if ( empty( $_POST['bbp_topic_id'] ) ) {
1085          bbp_add_error( 'bbp_merge_topic_source_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
1086      } else {
1087          $source_topic_id = (int) $_POST['bbp_topic_id'];
1088      }
1089  
1090      // Nonce check
1091      if ( ! bbp_verify_nonce_request( 'bbp-merge-topic_' . $source_topic_id ) ) {
1092          bbp_add_error( 'bbp_merge_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
1093          return;
1094  
1095      // Source topic not found
1096      } elseif ( ! $source_topic = bbp_get_topic( $source_topic_id ) ) {
1097          bbp_add_error( 'bbp_merge_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to merge was not found.', 'bbpress' ) );
1098          return;
1099      }
1100  
1101      // Cannot edit source topic
1102      if ( ! current_user_can( 'edit_topic', $source_topic->ID ) ) {
1103          bbp_add_error( 'bbp_merge_topic_source_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the source topic.', 'bbpress' ) );
1104          return;
1105      }
1106  
1107      /** Destination Topic *****************************************************/
1108  
1109      // Topic id
1110      if ( empty( $_POST['bbp_destination_topic'] ) ) {
1111          bbp_add_error( 'bbp_merge_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found.', 'bbpress' ) );
1112      } else {
1113          $destination_topic_id = (int) $_POST['bbp_destination_topic'];
1114      }
1115  
1116      // Destination topic not found
1117      if ( ! $destination_topic = bbp_get_topic( $destination_topic_id ) ) {
1118          bbp_add_error( 'bbp_merge_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to merge to was not found.', 'bbpress' ) );
1119      }
1120  
1121      // Cannot edit destination topic
1122      if ( ! current_user_can( 'edit_topic', $destination_topic->ID ) ) {
1123          bbp_add_error( 'bbp_merge_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the destination topic.', 'bbpress' ) );
1124      }
1125  
1126      // Bail if errors
1127      if ( bbp_has_errors() ) {
1128          return;
1129      }
1130  
1131      /** No Errors *************************************************************/
1132  
1133      // Update counts, etc...
1134      do_action( 'bbp_merge_topic', $destination_topic->ID, $source_topic->ID );
1135  
1136      /** Date Check ************************************************************/
1137  
1138      // Check if the destination topic is older than the source topic
1139      if ( strtotime( $source_topic->post_date ) < strtotime( $destination_topic->post_date ) ) {
1140  
1141          // Set destination topic post_date to 1 second before source topic
1142          $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $source_topic->post_date ) - 1 );
1143  
1144          // Update destination topic
1145          wp_update_post( array(
1146              'ID'            => $destination_topic_id,
1147              'post_date'     => $destination_post_date,
1148              'post_date_gmt' => get_gmt_from_date( $destination_post_date )
1149          ) );
1150      }
1151  
1152      /** Engagements ***********************************************************/
1153  
1154      // Get engagements from source topic
1155      $engagements = bbp_get_topic_engagements( $source_topic->ID );
1156  
1157      // Maybe migrate engagements
1158      if ( ! empty( $engagements ) ) {
1159          foreach ( $engagements as $engager ) {
1160              bbp_add_user_engagement( $engager, $destination_topic->ID );
1161          }
1162      }
1163  
1164      /** Subscriptions *********************************************************/
1165  
1166      // Get subscribers from source topic
1167      $subscribers = bbp_get_subscribers( $source_topic->ID );
1168  
1169      // Maybe migrate subscriptions
1170      if ( ! empty( $subscribers ) && ! empty( $_POST['bbp_topic_subscribers'] ) && ( '1' === $_POST['bbp_topic_subscribers'] ) ) {
1171          foreach ( $subscribers as $subscriber ) {
1172              bbp_add_user_subscription( $subscriber, $destination_topic->ID );
1173          }
1174      }
1175  
1176      /** Favorites *************************************************************/
1177  
1178      // Get favoriters from source topic
1179      $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
1180  
1181      // Maybe migrate favorites
1182      if ( ! empty( $favoriters ) && ! empty( $_POST['bbp_topic_favoriters'] ) && ( '1' === $_POST['bbp_topic_favoriters'] ) ) {
1183          foreach ( $favoriters as $favoriter ) {
1184              bbp_add_user_favorite( $favoriter, $destination_topic->ID );
1185          }
1186      }
1187  
1188      /** Tags ******************************************************************/
1189  
1190      // Get the source topic tags
1191      $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
1192  
1193      // Tags to possibly merge
1194      if ( ! empty( $source_topic_tags ) && ! is_wp_error( $source_topic_tags ) ) {
1195  
1196          // Shift the tags if told to
1197          if ( ! empty( $_POST['bbp_topic_tags'] ) && ( "1" === $_POST['bbp_topic_tags'] ) ) {
1198              wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
1199          }
1200  
1201          // Delete the tags from the source topic
1202          wp_delete_object_term_relationships( $source_topic->ID, bbp_get_topic_tag_tax_id() );
1203      }
1204  
1205      /** Source Topic **********************************************************/
1206  
1207      // Status
1208      bbp_open_topic( $source_topic->ID );
1209  
1210      // Sticky
1211      bbp_unstick_topic( $source_topic->ID );
1212  
1213      // Delete source topic's last & count meta data
1214      delete_post_meta( $source_topic->ID, '_bbp_last_reply_id'      );
1215      delete_post_meta( $source_topic->ID, '_bbp_last_active_id'     );
1216      delete_post_meta( $source_topic->ID, '_bbp_last_active_time'   );
1217      delete_post_meta( $source_topic->ID, '_bbp_voice_count'        );
1218      delete_post_meta( $source_topic->ID, '_bbp_reply_count'        );
1219      delete_post_meta( $source_topic->ID, '_bbp_reply_count_hidden' );
1220  
1221      // Delete source topics user relationships
1222      delete_post_meta( $source_topic->ID, '_bbp_favorite'     );
1223      delete_post_meta( $source_topic->ID, '_bbp_subscription' );
1224      delete_post_meta( $source_topic->ID, '_bbp_engagement'   );
1225  
1226      // Get the replies of the source topic
1227      $replies = (array) get_posts( array(
1228          'post_parent'    => $source_topic->ID,
1229          'post_type'      => bbp_get_reply_post_type(),
1230          'posts_per_page' => -1,
1231          'order'          => 'ASC'
1232      ) );
1233  
1234      // Prepend the source topic to its replies array for processing
1235      array_unshift( $replies, $source_topic );
1236  
1237      if ( ! empty( $replies ) ) {
1238  
1239          /** Merge Replies *****************************************************/
1240  
1241          // Change the post_parent of each reply to the destination topic id
1242          foreach ( $replies as $reply ) {
1243  
1244              // Update the reply
1245              wp_update_post( array(
1246                  'ID'          => $reply->ID,
1247                  'post_title'  => '',
1248                  'post_name'   => false,
1249                  'post_type'   => bbp_get_reply_post_type(),
1250                  'post_parent' => $destination_topic->ID,
1251                  'guid'        => ''
1252              ) );
1253  
1254              // Adjust reply meta values
1255              bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID                           );
1256              bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
1257  
1258              // Update the reply position
1259              bbp_update_reply_position( $reply->ID );
1260  
1261              // Do additional actions per merged reply
1262              do_action( 'bbp_merged_topic_reply', $reply->ID, $destination_topic->ID );
1263          }
1264      }
1265  
1266      /** Successful Merge ******************************************************/
1267  
1268      // Update topic's last meta data
1269      bbp_update_topic_last_reply_id   ( $destination_topic->ID );
1270      bbp_update_topic_last_active_id  ( $destination_topic->ID );
1271      bbp_update_topic_last_active_time( $destination_topic->ID );
1272  
1273      // Send the post parent of the source topic as it has been shifted
1274      // (possibly to a new forum) so we need to update the counts of the
1275      // old forum as well as the new one
1276      do_action( 'bbp_merged_topic', $destination_topic->ID, $source_topic->ID, $source_topic->post_parent );
1277  
1278      // Redirect back to new topic
1279      bbp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
1280  }
1281  
1282  /**
1283   * Fix counts on topic merge
1284   *
1285   * When a topic is merged, update the counts of source and destination topic
1286   * and their forums.
1287   *
1288   * @since 2.0.0 bbPress (r2756)
1289   *
1290   * @param int $destination_topic_id Destination topic id
1291   * @param int $source_topic_id Source topic id
1292   * @param int $source_topic_forum_id Source topic's forum id
1293   */
1294  function bbp_merge_topic_count( $destination_topic_id, $source_topic_id, $source_topic_forum_id ) {
1295  
1296      /** Source Topic **********************************************************/
1297  
1298      // Forum Topic Counts
1299      bbp_update_forum_topic_count( $source_topic_forum_id );
1300  
1301      // Forum Reply Counts
1302      bbp_update_forum_reply_count( $source_topic_forum_id );
1303  
1304      /** Destination Topic *****************************************************/
1305  
1306      // Topic Reply Counts
1307      bbp_update_topic_reply_count( $destination_topic_id );
1308  
1309      // Topic Hidden Reply Counts
1310      bbp_update_topic_reply_count_hidden( $destination_topic_id );
1311  
1312      // Topic Voice Counts
1313      bbp_update_topic_voice_count( $destination_topic_id );
1314  
1315      do_action( 'bbp_merge_topic_count', $destination_topic_id, $source_topic_id, $source_topic_forum_id );
1316  }
1317  
1318  /**
1319   * Split topic handler
1320   *
1321   * Handles the front end split topic submission
1322   *
1323   * @since 2.0.0 bbPress (r2756)
1324   *
1325   * @param string $action The requested action to compare this function to
1326   */
1327  function bbp_split_topic_handler( $action = '' ) {
1328  
1329      // Bail if action is not 'bbp-split-topic'
1330      if ( 'bbp-split-topic' !== $action ) {
1331          return;
1332      }
1333  
1334      // Prevent debug notices
1335      $from_reply_id = $destination_topic_id = 0;
1336      $destination_topic_title = '';
1337      $destination_topic = $from_reply = $source_topic = '';
1338      $split_option = false;
1339  
1340      /** Split Reply ***********************************************************/
1341  
1342      if ( empty( $_POST['bbp_reply_id'] ) ) {
1343          bbp_add_error( 'bbp_split_topic_reply_id', __( '<strong>ERROR</strong>: A reply ID is required.', 'bbpress' ) );
1344      } else {
1345          $from_reply_id = (int) $_POST['bbp_reply_id'];
1346      }
1347  
1348      $from_reply = bbp_get_reply( $from_reply_id );
1349  
1350      // Reply exists
1351      if ( empty( $from_reply ) ) {
1352          bbp_add_error( 'bbp_split_topic_r_not_found', __( '<strong>ERROR</strong>: The reply you want to split from was not found.', 'bbpress' ) );
1353      }
1354  
1355      /** Topic to Split ********************************************************/
1356  
1357      // Get the topic being split
1358      $source_topic = bbp_get_topic( $from_reply->post_parent );
1359  
1360      // No topic
1361      if ( empty( $source_topic ) ) {
1362          bbp_add_error( 'bbp_split_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to split was not found.', 'bbpress' ) );
1363      }
1364  
1365      // Nonce check failed
1366      if ( ! bbp_verify_nonce_request( 'bbp-split-topic_' . $source_topic->ID ) ) {
1367          bbp_add_error( 'bbp_split_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
1368          return;
1369      }
1370  
1371      // Use cannot edit topic
1372      if ( ! current_user_can( 'edit_topic', $source_topic->ID ) ) {
1373          bbp_add_error( 'bbp_split_topic_source_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the source topic.', 'bbpress' ) );
1374      }
1375  
1376      // How to Split
1377      if ( ! empty( $_POST['bbp_topic_split_option'] ) ) {
1378          $split_option = sanitize_key( $_POST['bbp_topic_split_option'] );
1379      }
1380  
1381      // Invalid split option
1382      if ( empty( $split_option ) || ! in_array( $split_option, array( 'existing', 'reply' ), true ) ) {
1383          bbp_add_error( 'bbp_split_topic_option', __( '<strong>ERROR</strong>: You need to choose a valid split option.', 'bbpress' ) );
1384  
1385      // Valid Split Option
1386      } else {
1387  
1388          // What kind of split
1389          switch ( $split_option ) {
1390  
1391              // Into an existing topic
1392              case 'existing' :
1393  
1394                  // Get destination topic id
1395                  if ( empty( $_POST['bbp_destination_topic'] ) ) {
1396                      bbp_add_error( 'bbp_split_topic_destination_id', __( '<strong>ERROR</strong>: A topic ID is required.', 'bbpress' ) );
1397                  } else {
1398                      $destination_topic_id = (int) $_POST['bbp_destination_topic'];
1399                  }
1400  
1401                  // Get the destination topic
1402                  $destination_topic = bbp_get_topic( $destination_topic_id );
1403  
1404                  // No destination topic
1405                  if ( empty( $destination_topic ) ) {
1406                      bbp_add_error( 'bbp_split_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to split to was not found.', 'bbpress' ) );
1407                  }
1408  
1409                  // User cannot edit the destination topic
1410                  if ( ! current_user_can( 'edit_topic', $destination_topic->ID ) ) {
1411                      bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the destination topic.', 'bbpress' ) );
1412                  }
1413  
1414                  break;
1415  
1416              // Split at reply into a new topic
1417              case 'reply' :
1418              default :
1419  
1420                  // User needs to be able to publish topics
1421                  if ( current_user_can( 'publish_topics' ) ) {
1422  
1423                      // Use the new title that was passed
1424                      if ( ! empty( $_POST['bbp_topic_split_destination_title'] ) ) {
1425                          $destination_topic_title = sanitize_text_field( $_POST['bbp_topic_split_destination_title'] );
1426  
1427                      // Use the source topic title
1428                      } else {
1429                          $destination_topic_title = $source_topic->post_title;
1430                      }
1431  
1432                      // Update the topic
1433                      $destination_topic_id = wp_update_post( array(
1434                          'ID'          => $from_reply->ID,
1435                          'post_title'  => $destination_topic_title,
1436                          'post_name'   => false,
1437                          'post_type'   => bbp_get_topic_post_type(),
1438                          'post_parent' => $source_topic->post_parent,
1439                          'menu_order'  => 0,
1440                          'guid'        => ''
1441                      ) );
1442                      $destination_topic = bbp_get_topic( $destination_topic_id );
1443  
1444                      // Make sure the new topic knows its a topic
1445                      bbp_update_topic_topic_id( $from_reply->ID );
1446  
1447                      // Shouldn't happen
1448                      if ( false === $destination_topic_id || is_wp_error( $destination_topic_id ) || empty( $destination_topic ) ) {
1449                          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' ) );
1450                      }
1451  
1452                  // User cannot publish posts
1453                  } else {
1454                      bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have permission to create new topics. The reply could not be converted into a topic.', 'bbpress' ) );
1455                  }
1456  
1457                  break;
1458          }
1459      }
1460  
1461      // Bail if there are errors
1462      if ( bbp_has_errors() ) {
1463          return;
1464      }
1465  
1466      /** No Errors - Do the Spit ***********************************************/
1467  
1468      // Update counts, etc...
1469      do_action( 'bbp_pre_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
1470  
1471      /** Date Check ************************************************************/
1472  
1473      // Check if the destination topic is older than the from reply
1474      if ( strtotime( $from_reply->post_date ) < strtotime( $destination_topic->post_date ) ) {
1475  
1476          // Set destination topic post_date to 1 second before from reply
1477          $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $from_reply->post_date ) - 1 );
1478  
1479          // Update destination topic
1480          wp_update_post( array(
1481              'ID'            => $destination_topic_id,
1482              'post_date'     => $destination_post_date,
1483              'post_date_gmt' => get_gmt_from_date( $destination_post_date )
1484          ) );
1485      }
1486  
1487      /** Subscriptions *********************************************************/
1488  
1489      // Copy the subscribers
1490      if ( ! empty( $_POST['bbp_topic_subscribers'] ) && "1" === $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active() ) {
1491  
1492          // Get the subscribers
1493          $subscribers = bbp_get_subscribers( $source_topic->ID );
1494  
1495          if ( ! empty( $subscribers ) ) {
1496  
1497              // Add subscribers to new topic
1498              foreach ( (array) $subscribers as $subscriber ) {
1499                  bbp_add_user_subscription( $subscriber, $destination_topic->ID );
1500              }
1501          }
1502      }
1503  
1504      /** Favorites *************************************************************/
1505  
1506      // Copy the favoriters if told to
1507      if ( ! empty( $_POST['bbp_topic_favoriters'] ) && ( "1" === $_POST['bbp_topic_favoriters'] ) ) {
1508  
1509          // Get the favoriters
1510          $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
1511  
1512          if ( ! empty( $favoriters ) ) {
1513  
1514              // Add the favoriters to new topic
1515              foreach ( (array) $favoriters as $favoriter ) {
1516                  bbp_add_user_favorite( $favoriter, $destination_topic->ID );
1517              }
1518          }
1519      }
1520  
1521      /** Tags ******************************************************************/
1522  
1523      // Copy the tags if told to
1524      if ( ! empty( $_POST['bbp_topic_tags'] ) && ( "1" === $_POST['bbp_topic_tags'] ) ) {
1525  
1526          // Get the source topic tags
1527          $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
1528  
1529          if ( ! empty( $source_topic_tags ) ) {
1530              wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
1531          }
1532      }
1533  
1534      /** Split Replies *********************************************************/
1535  
1536      // get_posts() is not used because it doesn't allow us to use '>='
1537      // comparision without a filter.
1538      $bbp_db  = bbp_db();
1539      $query   = $bbp_db->prepare( "SELECT * FROM {$bbp_db->posts} WHERE {$bbp_db->posts}.post_date >= %s AND {$bbp_db->posts}.post_parent = %d AND {$bbp_db->posts}.post_type = %s ORDER BY {$bbp_db->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() );
1540      $replies = (array) $bbp_db->get_results( $query );
1541  
1542      // Make sure there are replies to loop through
1543      if ( ! empty( $replies ) && ! is_wp_error( $replies ) ) {
1544  
1545          // Save reply ids
1546          $reply_ids = array();
1547  
1548          // Change the post_parent of each reply to the destination topic id
1549          foreach ( $replies as $reply ) {
1550  
1551              // Update the reply
1552              wp_update_post( array(
1553                  'ID'          => $reply->ID,
1554                  'post_title'  => '',
1555                  'post_name'   => false, // will be automatically generated
1556                  'post_parent' => $destination_topic->ID,
1557                  'guid'        => ''
1558              ) );
1559  
1560              // Gather reply ids
1561              $reply_ids[] = $reply->ID;
1562  
1563              // Adjust reply meta values
1564              bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID                           );
1565              bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
1566  
1567              // Adjust reply position
1568              bbp_update_reply_position( $reply->ID );
1569  
1570              // Adjust reply to values
1571              $reply_to = bbp_get_reply_to( $reply->ID );
1572  
1573              // Not a reply to a reply that moved over
1574              if ( ! in_array( $reply_to, $reply_ids, true ) ) {
1575                  bbp_update_reply_to( $reply->ID, 0 );
1576              }
1577  
1578              // New topic from reply can't be a reply to
1579              if ( ( $from_reply->ID === $destination_topic->ID ) && ( $from_reply->ID === $reply_to ) ) {
1580                  bbp_update_reply_to( $reply->ID, 0 );
1581              }
1582  
1583              // Do additional actions per split reply
1584              do_action( 'bbp_split_topic_reply', $reply->ID, $destination_topic->ID );
1585          }
1586  
1587          // Remove reply to from new topic
1588          if ( $from_reply->ID === $destination_topic->ID ) {
1589              delete_post_meta( $from_reply->ID, '_bbp_reply_to' );
1590          }
1591  
1592          // Set the last reply ID and freshness
1593          $last_reply_id = $reply->ID;
1594          $freshness     = $reply->post_date;
1595  
1596      // Set the last reply ID and freshness to the from_reply
1597      } else {
1598          $last_reply_id = $from_reply->ID;
1599          $freshness     = $from_reply->post_date;
1600      }
1601  
1602      // It is a new topic and we need to set some default metas to make
1603      // the topic display in bbp_has_topics() list
1604      if ( 'reply' === $split_option ) {
1605          bbp_update_topic_last_reply_id   ( $destination_topic->ID, $last_reply_id );
1606          bbp_update_topic_last_active_id  ( $destination_topic->ID, $last_reply_id );
1607          bbp_update_topic_last_active_time( $destination_topic->ID, $freshness     );
1608      }
1609  
1610      // Update source topic ID last active
1611      bbp_update_topic_last_reply_id   ( $source_topic->ID );
1612      bbp_update_topic_last_active_id  ( $source_topic->ID );
1613      bbp_update_topic_last_active_time( $source_topic->ID );
1614  
1615      /** Successful Split ******************************************************/
1616  
1617      // Update counts, etc...
1618      do_action( 'bbp_post_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
1619  
1620      // Redirect back to the topic
1621      bbp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
1622  }
1623  
1624  /**
1625   * Fix counts on topic split
1626   *
1627   * When a topic is split, update the counts of source and destination topic
1628   * and their forums.
1629   *
1630   * @since 2.0.0 bbPress (r2756)
1631   *
1632   * @param int $from_reply_id From reply id
1633   * @param int $source_topic_id Source topic id
1634   * @param int $destination_topic_id Destination topic id
1635   */
1636  function bbp_split_topic_count( $from_reply_id, $source_topic_id, $destination_topic_id ) {
1637  
1638      // Forum Topic Counts
1639      bbp_update_forum_topic_count( bbp_get_topic_forum_id( $destination_topic_id ) );
1640  
1641      // Forum Reply Counts
1642      bbp_update_forum_reply_count( bbp_get_topic_forum_id( $destination_topic_id ) );
1643  
1644      // Topic Reply Counts
1645      bbp_update_topic_reply_count( $source_topic_id      );
1646      bbp_update_topic_reply_count( $destination_topic_id );
1647  
1648      // Topic Hidden Reply Counts
1649      bbp_update_topic_reply_count_hidden( $source_topic_id      );
1650      bbp_update_topic_reply_count_hidden( $destination_topic_id );
1651  
1652      // Topic Voice Counts
1653      bbp_update_topic_voice_count( $source_topic_id      );
1654      bbp_update_topic_voice_count( $destination_topic_id );
1655  
1656      do_action( 'bbp_split_topic_count', $from_reply_id, $source_topic_id, $destination_topic_id );
1657  }
1658  
1659  /**
1660   * Handles the front end tag management (renaming, merging, destroying)
1661   *
1662   * @since 2.0.0 bbPress (r2768)
1663   *
1664   * @param string $action The requested action to compare this function to
1665   */
1666  function bbp_edit_topic_tag_handler( $action = '' ) {
1667  
1668      // Bail if required POST actions aren't passed
1669      if ( empty( $_POST['tag-id'] ) ) {
1670          return;
1671      }
1672  
1673      // Setup possible get actions
1674      $possible_actions = array(
1675          'bbp-update-topic-tag',
1676          'bbp-merge-topic-tag',
1677          'bbp-delete-topic-tag'
1678      );
1679  
1680      // Bail if actions aren't meant for this function
1681      if ( ! in_array( $action, $possible_actions, true ) ) {
1682          return;
1683      }
1684  
1685      // Setup vars
1686      $tag_id = (int) $_POST['tag-id'];
1687      $tag    = get_term( $tag_id, bbp_get_topic_tag_tax_id() );
1688  
1689      // Tag does not exist
1690      if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1691          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() ) );
1692          return;
1693      }
1694  
1695      // What action are we trying to perform?
1696      switch ( $action ) {
1697  
1698          // Update tag
1699          case 'bbp-update-topic-tag' :
1700  
1701              // Nonce check
1702              if ( ! bbp_verify_nonce_request( 'update-tag_' . $tag_id ) ) {
1703                  bbp_add_error( 'bbp_manage_topic_tag_update_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
1704                  return;
1705              }
1706  
1707              // Can user edit topic tags?
1708              if ( ! current_user_can( 'edit_topic_tag', $tag_id ) ) {
1709                  bbp_add_error( 'bbp_manage_topic_tag_update_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the topic tags.', 'bbpress' ) );
1710                  return;
1711              }
1712  
1713              // No tag name was provided
1714              if ( empty( $_POST['tag-name'] ) || ! $name = $_POST['tag-name'] ) {
1715                  bbp_add_error( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
1716                  return;
1717              }
1718  
1719              // Attempt to update the tag
1720              $slug        = ! empty( $_POST['tag-slug']        ) ? $_POST['tag-slug']        : '';
1721              $description = ! empty( $_POST['tag-description'] ) ? $_POST['tag-description'] : '';
1722              $tag         = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array(
1723                  'name'        => $name,
1724                  'slug'        => $slug,
1725                  'description' => $description
1726              ) );
1727  
1728              // Cannot update tag
1729              if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1730                  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() ) );
1731                  return;
1732              }
1733  
1734              // Redirect
1735              $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() );
1736  
1737              // Update counts, etc...
1738              do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug, $description );
1739  
1740              break;
1741  
1742          // Merge two tags
1743          case 'bbp-merge-topic-tag'  :
1744  
1745              // Nonce check
1746              if ( ! bbp_verify_nonce_request( 'merge-tag_' . $tag_id ) ) {
1747                  bbp_add_error( 'bbp_manage_topic_tag_merge_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
1748                  return;
1749              }
1750  
1751              // Can user edit topic tags?
1752              if ( ! current_user_can( 'edit_topic_tags' ) ) {
1753                  bbp_add_error( 'bbp_manage_topic_tag_merge_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the topic tags.', 'bbpress' ) );
1754                  return;
1755              }
1756  
1757              // No tag name was provided
1758              if ( empty( $_POST['tag-existing-name'] ) || ! $name = $_POST['tag-existing-name'] ) {
1759                  bbp_add_error( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
1760                  return;
1761              }
1762  
1763              // If term does not exist, create it
1764              if ( ! $tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) ) {
1765                  $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() );
1766              }
1767  
1768              // Problem inserting the new term
1769              if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1770                  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() ) );
1771                  return;
1772              }
1773  
1774              // Merging in to...
1775              $to_tag = $tag['term_id'];
1776  
1777              // Attempting to merge a tag into itself
1778              if ( $tag_id === $to_tag ) {
1779                  bbp_add_error( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', 'bbpress' ) );
1780                  return;
1781              }
1782  
1783              // Delete the old term
1784              $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array(
1785                  'default'       => $to_tag,
1786                  'force_default' => true
1787              ) );
1788  
1789              // Error merging the terms
1790              if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1791                  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() ) );
1792                  return;
1793              }
1794  
1795              // Redirect
1796              $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() );
1797  
1798              // Update counts, etc...
1799              do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag );
1800  
1801              break;
1802  
1803          // Delete tag
1804          case 'bbp-delete-topic-tag' :
1805  
1806              // Nonce check
1807              if ( ! bbp_verify_nonce_request( 'delete-tag_' . $tag_id ) ) {
1808                  bbp_add_error( 'bbp_manage_topic_tag_delete_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
1809                  return;
1810              }
1811  
1812              // Can user delete topic tags?
1813              if ( ! current_user_can( 'delete_topic_tag', $tag_id ) ) {
1814                  bbp_add_error( 'bbp_manage_topic_tag_delete_permission', __( '<strong>ERROR</strong>: You do not have permission to delete the topic tags.', 'bbpress' ) );
1815                  return;
1816              }
1817  
1818              // Attempt to delete term
1819              $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() );
1820  
1821              // Error deleting term
1822              if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1823                  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() ) );
1824                  return;
1825              }
1826  
1827              // We don't have any other place to go other than home! Or we may die because of the 404 disease
1828              $redirect = bbp_get_forums_url();
1829  
1830              // Update counts, etc...
1831              do_action( 'bbp_delete_topic_tag', $tag_id, $tag );
1832  
1833              break;
1834      }
1835  
1836      /** Successful Moderation *************************************************/
1837  
1838      // Redirect back
1839      $redirect = ( ! empty( $redirect ) && ! is_wp_error( $redirect ) ) ? $redirect : home_url();
1840      bbp_redirect( $redirect );
1841  }
1842  
1843  /** Helpers *******************************************************************/
1844  
1845  /**
1846   * Return an associative array of available topic statuses
1847   *
1848   * @since 2.4.0 bbPress (r5059)
1849   *
1850   * @param int $topic_id   Optional. Topic id.
1851   *
1852   * @return array
1853   */
1854  function bbp_get_topic_statuses( $topic_id = 0 ) {
1855  
1856      // Filter & return
1857      return (array) apply_filters( 'bbp_get_topic_statuses', array(
1858          bbp_get_public_status_id()  => _x( 'Open',    'Open the topic',      'bbpress' ),
1859          bbp_get_closed_status_id()  => _x( 'Closed',  'Close the topic',     'bbpress' ),
1860          bbp_get_spam_status_id()    => _x( 'Spam',    'Spam the topic',      'bbpress' ),
1861          bbp_get_trash_status_id()   => _x( 'Trash',   'Trash the topic',     'bbpress' ),
1862          bbp_get_pending_status_id() => _x( 'Pending', 'Unapprove the topic', 'bbpress' )
1863      ), $topic_id );
1864  }
1865  
1866  /**
1867   * Return an associative array of topic sticky types
1868   *
1869   * @since 2.4.0 bbPress (r5059)
1870   *
1871   * @param int $topic_id   Optional. Topic id.
1872   *
1873   * @return array
1874   */
1875  function bbp_get_topic_types( $topic_id = 0 ) {
1876  
1877      // Filter & return
1878      return (array) apply_filters( 'bbp_get_topic_types', array(
1879          'unstick' => _x( 'Normal',       'Unstick a topic',         'bbpress' ),
1880          'stick'   => _x( 'Sticky',       'Make topic sticky',       'bbpress' ),
1881          'super'   => _x( 'Super Sticky', 'Make topic super sticky', 'bbpress' )
1882      ), $topic_id );
1883  }
1884  
1885  /**
1886   * Return array of available topic toggle actions
1887   *
1888   * @since 2.6.0 bbPress (r6133)
1889   *
1890   * @param int $topic_id   Optional. Topic id.
1891   *
1892   * @return array
1893   */
1894  function bbp_get_topic_toggles( $topic_id = 0 ) {
1895  
1896      // Filter & return
1897      return (array) apply_filters( 'bbp_get_toggle_topic_actions', array(
1898          'bbp_toggle_topic_close',
1899          'bbp_toggle_topic_stick',
1900          'bbp_toggle_topic_spam',
1901          'bbp_toggle_topic_trash',
1902          'bbp_toggle_topic_approve'
1903      ), $topic_id );
1904  }
1905  
1906  /**
1907   * Return array of public topic statuses.
1908   *
1909   * @since 2.6.0 bbPress (r6383)
1910   *
1911   * @return array
1912   */
1913  function bbp_get_public_topic_statuses() {
1914      $statuses = array(
1915          bbp_get_public_status_id(),
1916          bbp_get_closed_status_id()
1917      );
1918  
1919      // Filter & return
1920      return (array) apply_filters( 'bbp_get_public_topic_statuses', $statuses );
1921  }
1922  
1923  /**
1924   * Return array of non-public topic statuses.
1925   *
1926   * @since 2.6.0 bbPress (r6642)
1927   *
1928   * @return array
1929   */
1930  function bbp_get_non_public_topic_statuses() {
1931      $statuses = array(
1932          bbp_get_trash_status_id(),
1933          bbp_get_spam_status_id(),
1934          bbp_get_pending_status_id()
1935      );
1936  
1937      // Filter & return
1938      return (array) apply_filters( 'bbp_get_non_public_topic_statuses', $statuses );
1939  }
1940  
1941  /** Stickies ******************************************************************/
1942  
1943  /**
1944   * Return sticky topics of a forum
1945   *
1946   * @since 2.0.0 bbPress (r2592)
1947   *
1948   * @param int $forum_id Optional. If not passed, super stickies are returned.
1949   * @return array IDs of sticky topics of a forum or super stickies
1950   */
1951  function bbp_get_stickies( $forum_id = 0 ) {
1952  
1953      // Get stickies (maybe super if empty)
1954      $stickies = empty( $forum_id )
1955          ? bbp_get_super_stickies()
1956          : get_post_meta( $forum_id, '_bbp_sticky_topics', true );
1957  
1958      // Cast as array
1959      $stickies = ( empty( $stickies ) || ! is_array( $stickies ) )
1960          ? array()
1961          : wp_parse_id_list( $stickies );
1962  
1963      // Filter & return
1964      return (array) apply_filters( 'bbp_get_stickies', $stickies, $forum_id );
1965  }
1966  
1967  /**
1968   * Return topics stuck to front page of the forums
1969   *
1970   * @since 2.0.0 bbPress (r2592)
1971   *
1972   * @return array IDs of super sticky topics
1973   */
1974  function bbp_get_super_stickies() {
1975  
1976      // Get super stickies
1977      $stickies = get_option( '_bbp_super_sticky_topics', array() );
1978  
1979      // Cast as array
1980      $stickies = ( empty( $stickies ) || ! is_array( $stickies ) )
1981          ? array()
1982          : wp_parse_id_list( $stickies );
1983  
1984      // Filter & return
1985      return (array) apply_filters( 'bbp_get_super_stickies', $stickies );
1986  }
1987  
1988  /** Topics Actions ************************************************************/
1989  
1990  /**
1991   * Handles the front end opening/closing, spamming/unspamming,
1992   * sticking/unsticking and trashing/untrashing/deleting of topics
1993   *
1994   * @since 2.0.0 bbPress (r2727)
1995   *
1996   * @param string $action The requested action to compare this function to
1997   */
1998  function bbp_toggle_topic_handler( $action = '' ) {
1999  
2000      // Bail if required GET actions aren't passed
2001      if ( empty( $_GET['topic_id'] ) ) {
2002          return;
2003      }
2004  
2005      // What's the topic id?
2006      $topic_id = bbp_get_topic_id( (int) $_GET['topic_id'] );
2007  
2008      // Get possible topic-handler toggles
2009      $toggles = bbp_get_topic_toggles( $topic_id );
2010  
2011      // Bail if actions aren't meant for this function
2012      if ( ! in_array( $action, $toggles, true ) ) {
2013          return;
2014      }
2015  
2016      // Make sure topic exists
2017      $topic = bbp_get_topic( $topic_id );
2018      if ( empty( $topic ) ) {
2019          bbp_add_error( 'bbp_toggle_topic_missing', __( '<strong>ERROR:</strong> This topic could not be found or no longer exists.', 'bbpress' ) );
2020          return;
2021      }
2022  
2023      // What is the user doing here?
2024      if ( ! current_user_can( 'edit_topic', $topic_id ) || ( 'bbp_toggle_topic_trash' === $action && ! current_user_can( 'delete_topic', $topic_id ) ) ) {
2025          bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have permission to do that.', 'bbpress' ) );
2026          return;
2027      }
2028  
2029      // Sub-action?
2030      $sub_action = ! empty( $_GET['sub_action'] )
2031          ? sanitize_key( $_GET['sub_action'] )
2032          : false;
2033  
2034      // Preliminary array
2035      $post_data = array( 'ID' => $topic_id );
2036  
2037      // Do the topic toggling
2038      $retval = bbp_toggle_topic( array(
2039          'id'         => $topic_id,
2040          'action'     => $action,
2041          'sub_action' => $sub_action,
2042          'data'       => $post_data
2043      ) );
2044  
2045      // Do additional topic toggle actions
2046      do_action( 'bbp_toggle_topic_handler', $retval['status'], $post_data, $action );
2047  
2048      // No errors
2049      if ( ( false !== $retval['status'] ) && ! is_wp_error( $retval['status'] ) ) {
2050          bbp_redirect( $retval['redirect_to'] );
2051  
2052      // Handle errors
2053      } else {
2054          bbp_add_error( 'bbp_toggle_topic', $retval['message'] );
2055      }
2056  }
2057  
2058  /**
2059   * Do the actual topic toggling
2060   *
2061   * This function is used by `bbp_toggle_topic_handler()` to do the actual heavy
2062   * lifting when it comes to toggling topic. It only really makes sense to call
2063   * within that context, so if you need to call this function directly, make sure
2064   * you're also doing what the handler does too.
2065   *
2066   * @since 2.6.0  bbPress (r6133)
2067   * @access private
2068   *
2069   * @param array $args
2070   */
2071  function bbp_toggle_topic( $args = array() ) {
2072  
2073      // Parse the arguments
2074      $r = bbp_parse_args( $args, array(
2075          'id'         => 0,
2076          'action'     => '',
2077          'sub_action' => '',
2078          'data'       => array()
2079      ) );
2080  
2081      // Build the nonce suffix
2082      $nonce_suffix = bbp_get_topic_post_type() . '_' . (int) $r['id'];
2083  
2084      // Default return values
2085      $retval = array(
2086          'status'      => 0,
2087          'message'     => '',
2088          'redirect_to' => bbp_get_topic_permalink( $r['id'], bbp_get_redirect_to() ),
2089          'view_all'    => false
2090      );
2091  
2092      // What action are we trying to perform?
2093      switch ( $r['action'] ) {
2094  
2095          // Toggle approve/unapprove
2096          case 'bbp_toggle_topic_approve' :
2097              check_ajax_referer( "approve-{$nonce_suffix}" );
2098  
2099              $is_pending         = bbp_is_topic_pending( $r['id'] );
2100              $retval['view_all'] = ! $is_pending;
2101  
2102              // Toggle
2103              $retval['status'] = ( true === $is_pending )
2104                  ? bbp_approve_topic( $r['id'] )
2105                  : bbp_unapprove_topic( $r['id'] );
2106  
2107              // Feedback
2108              $retval['message'] = ( true === $is_pending )
2109                  ? __( '<strong>ERROR</strong>: There was a problem approving the topic.',   'bbpress' )
2110                  : __( '<strong>ERROR</strong>: There was a problem unapproving the topic.', 'bbpress' );
2111  
2112              break;
2113  
2114          // Toggle open/close
2115          case 'bbp_toggle_topic_close' :
2116              check_ajax_referer( "close-{$nonce_suffix}" );
2117  
2118              $is_open = bbp_is_topic_open( $r['id'] );
2119  
2120              // Toggle
2121              $retval['status'] = ( true === $is_open )
2122                  ? bbp_close_topic( $r['id'] )
2123                  : bbp_open_topic( $r['id'] );
2124  
2125              // Feedback
2126              $retval['message'] = ( true === $is_open )
2127                  ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress' )
2128                  : __( '<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress' );
2129  
2130              break;
2131  
2132          // Toggle sticky/super-sticky/unstick
2133          case 'bbp_toggle_topic_stick' :
2134              check_ajax_referer( "stick-{$nonce_suffix}" );
2135  
2136              $is_sticky = bbp_is_topic_sticky( $r['id'] );
2137              $is_super  = false === $is_sticky && ! empty( $_GET['super'] ) && ( "1" === $_GET['super'] ) ? true : false;
2138  
2139              // Toggle
2140              $retval['status'] = ( true === $is_sticky )
2141                  ? bbp_unstick_topic( $r['id'] )
2142                  : bbp_stick_topic( $r['id'], $is_super );
2143  
2144              // Feedback
2145              $retval['message'] = ( true === $is_sticky )
2146                  ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress' )
2147                  : __( '<strong>ERROR</strong>: There was a problem sticking the topic.',   'bbpress' );
2148  
2149              break;
2150  
2151          // Toggle spam
2152          case 'bbp_toggle_topic_spam' :
2153              check_ajax_referer( "spam-{$nonce_suffix}" );
2154  
2155              $is_spam            = bbp_is_topic_spam( $r['id'] );
2156              $retval['view_all'] = ! $is_spam;
2157  
2158              // Toggle
2159              $retval['status'] = ( true === $is_spam )
2160                  ? bbp_unspam_topic( $r['id'] )
2161                  : bbp_spam_topic( $r['id'] );
2162  
2163              // Feedback
2164              $retval['message'] = ( true === $is_spam )
2165                  ? __( '<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress' )
2166                  : __( '<strong>ERROR</strong>: There was a problem marking the topic as spam.',   'bbpress' );
2167  
2168              break;
2169  
2170          // Toggle trash
2171          case 'bbp_toggle_topic_trash' :
2172  
2173              switch ( $r['sub_action'] ) {
2174                  case 'trash':
2175                      check_ajax_referer( "trash-{$nonce_suffix}" );
2176  
2177                      $retval['view_all']    = true;
2178                      $retval['status']      = wp_trash_post( $r['id'] );
2179                      $retval['message']     = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );
2180                      $retval['redirect_to'] = current_user_can( 'view_trash' )
2181                          ? bbp_get_topic_permalink( $r['id'] )
2182                          : bbp_get_forum_permalink( bbp_get_topic_forum_id( $r['id'] ) );
2183  
2184                      break;
2185  
2186                  case 'untrash':
2187                      check_ajax_referer( "untrash-{$nonce_suffix}" );
2188  
2189                      $retval['status']      = wp_untrash_post( $r['id'] );
2190                      $retval['message']     = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );
2191                      $retval['redirect_to'] = bbp_get_topic_permalink( $r['id'] );
2192  
2193                      break;
2194  
2195                  case 'delete':
2196                      check_ajax_referer( "delete-{$nonce_suffix}" );
2197  
2198                      $retval['status']      = wp_delete_post( $r['id'] );
2199                      $retval['message']     = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );
2200                      $retval['redirect_to'] = bbp_get_forum_permalink( $retval['status']->post_parent );
2201  
2202                      break;
2203              }
2204  
2205              break;
2206      }
2207  
2208      // Add view all if needed
2209      if ( ! empty( $retval['view_all'] ) ) {
2210          $retval['redirect_to'] = bbp_add_view_all( $retval['redirect_to'], true );
2211      }
2212  
2213      // Filter & return
2214      return apply_filters( 'bbp_toggle_topic', $retval, $r, $args );
2215  }
2216  
2217  /** Favorites & Subscriptions *************************************************/
2218  
2219  /**
2220   * Remove a deleted topic from all user favorites
2221   *
2222   * @since 2.0.0 bbPress (r2652)
2223   *
2224   * @param int $topic_id Get the topic id to remove
2225   */
2226  function bbp_remove_topic_from_all_favorites( $topic_id = 0 ) {
2227      $topic_id = bbp_get_topic_id( $topic_id );
2228  
2229      // Bail if no topic
2230      if ( empty( $topic_id ) ) {
2231          return;
2232      }
2233  
2234      // Get users
2235      $users = (array) bbp_get_topic_favoriters( $topic_id );
2236  
2237      // Users exist
2238      if ( ! empty( $users ) ) {
2239  
2240          // Loop through users
2241          foreach ( $users as $user ) {
2242  
2243              // Remove each user
2244              bbp_remove_user_favorite( $user, $topic_id );
2245          }
2246      }
2247  }
2248  
2249  /**
2250   * Remove a deleted topic from all user subscriptions
2251   *
2252   * @since 2.0.0 bbPress (r2652)
2253   *
2254   * @param int $topic_id Get the topic id to remove
2255   */
2256  function bbp_remove_topic_from_all_subscriptions( $topic_id = 0 ) {
2257  
2258      // Subscriptions are not active
2259      if ( ! bbp_is_subscriptions_active() ) {
2260          return;
2261      }
2262  
2263      // Bail if no topic
2264      $topic_id = bbp_get_topic_id( $topic_id );
2265      if ( empty( $topic_id ) ) {
2266          return;
2267      }
2268  
2269      // Remove all users
2270      return bbp_remove_object_from_all_users( $topic_id, '_bbp_subscription', 'post' );
2271  }
2272  
2273  /** Count Bumpers *************************************************************/
2274  
2275  /**
2276   * Bump the total reply count of a topic
2277   *
2278   * @since 2.1.0 bbPress (r3825)
2279   *
2280   * @param int $topic_id   Optional. Topic id.
2281   * @param int $difference Optional. Default 1
2282   * @return int Topic reply count
2283   */
2284  function bbp_bump_topic_reply_count( $topic_id = 0, $difference = 1 ) {
2285  
2286      // Bail if no bump
2287      if ( empty( $difference ) ) {
2288          return false;
2289      }
2290  
2291      // Get counts
2292      $topic_id    = bbp_get_topic_id( $topic_id );
2293      $reply_count = bbp_get_topic_reply_count( $topic_id, true );
2294      $difference  = (int) $difference;
2295      $new_count   = (int) ( $reply_count + $difference );
2296  
2297      // Update this topic id's reply count
2298      update_post_meta( $topic_id, '_bbp_reply_count', $new_count );
2299  
2300      // Filter & return
2301      return (int) apply_filters( 'bbp_bump_topic_reply_count', $new_count, $topic_id, $difference );
2302  }
2303  
2304  /**
2305   * Increase the total reply count of a topic by one.
2306   *
2307   * @since 2.6.0 bbPress (r6036)
2308   *
2309   * @param int $topic_id The topic id.
2310   *
2311   * @return void
2312   */
2313  function bbp_increase_topic_reply_count( $topic_id = 0 ) {
2314  
2315      // Bail early if no id is passed.
2316      if ( empty( $topic_id ) ) {
2317          return;
2318      }
2319  
2320      // If it's a reply, get the topic id.
2321      if ( bbp_is_reply( $topic_id ) ) {
2322          $reply_id = $topic_id;
2323          $topic_id = bbp_get_reply_topic_id( $reply_id );
2324  
2325          // Update inverse based on item status
2326          if ( ! bbp_is_reply_public( $reply_id ) ) {
2327              bbp_increase_topic_reply_count_hidden( $topic_id );
2328              return;
2329          }
2330      }
2331  
2332      // Bump up
2333      bbp_bump_topic_reply_count( $topic_id );
2334  }
2335  
2336  /**
2337   * Decrease the total reply count of a topic by one.
2338   *
2339   * @since 2.6.0 bbPress (r6036)
2340   *
2341   * @param int $topic_id The topic id.
2342   *
2343   * @return void
2344   */
2345  function bbp_decrease_topic_reply_count( $topic_id = 0 ) {
2346  
2347      // Bail early if no id is passed.
2348      if ( empty( $topic_id ) ) {
2349          return;
2350      }
2351  
2352      // If it's a reply, get the topic id.
2353      if ( bbp_is_reply( $topic_id ) ) {
2354          $reply_id = $topic_id;
2355          $topic_id = bbp_get_reply_topic_id( $reply_id );
2356  
2357          // Update inverse based on item status
2358          if ( ! bbp_is_reply_public( $reply_id ) ) {
2359              bbp_decrease_topic_reply_count_hidden( $topic_id );
2360              return;
2361          }
2362      }
2363  
2364      // Bump down
2365      bbp_bump_topic_reply_count( $topic_id, -1 );
2366  }
2367  
2368  /**
2369   * Bump the total hidden reply count of a topic
2370   *
2371   * @since 2.1.0 bbPress (r3825)
2372   *
2373   * @param int $topic_id   Optional. Topic id.
2374   * @param int $difference Optional. Default 1
2375   * @return int Topic hidden reply count
2376   */
2377  function bbp_bump_topic_reply_count_hidden( $topic_id = 0, $difference = 1 ) {
2378  
2379      // Bail if no bump
2380      if ( empty( $difference ) ) {
2381          return false;
2382      }
2383  
2384      // Get counts
2385      $topic_id    = bbp_get_topic_id( $topic_id );
2386      $reply_count = bbp_get_topic_reply_count_hidden( $topic_id, true );
2387      $difference  = (int) $difference;
2388      $new_count   = (int) ( $reply_count + $difference );
2389  
2390      // Update this topic id's hidden reply count
2391      update_post_meta( $topic_id, '_bbp_reply_count_hidden', $new_count );
2392  
2393      // Filter & return
2394      return (int) apply_filters( 'bbp_bump_topic_reply_count_hidden', $new_count, $topic_id, $difference );
2395  }
2396  
2397  /**
2398   * Increase the total hidden reply count of a topic by one.
2399   *
2400   * @since 2.6.0 bbPress (r6036)
2401   *
2402   * @param int $topic_id The topic id.
2403   *
2404   * @return void
2405   */
2406  function bbp_increase_topic_reply_count_hidden( $topic_id = 0 ) {
2407  
2408      // Bail early if no id is passed.
2409      if ( empty( $topic_id ) ) {
2410          return;
2411      }
2412  
2413      // If it's a reply, get the topic id.
2414      if ( bbp_is_reply( $topic_id ) ) {
2415          $reply_id = $topic_id;
2416          $topic_id = bbp_get_reply_topic_id( $reply_id );
2417  
2418          // Update inverse based on item status
2419          if ( bbp_is_reply_public( $reply_id ) ) {
2420              bbp_increase_topic_reply_count( $topic_id );
2421              return;
2422          }
2423      }
2424  
2425      // Bump up
2426      bbp_bump_topic_reply_count_hidden( $topic_id );
2427  }
2428  
2429  /**
2430   * Decrease the total hidden reply count of a topic by one.
2431   *
2432   * @since 2.6.0 bbPress (r6036)
2433   *
2434   * @param int $topic_id The topic id.
2435   *
2436   * @return void
2437   */
2438  function bbp_decrease_topic_reply_count_hidden( $topic_id = 0 ) {
2439  
2440      // Bail early if no id is passed.
2441      if ( empty( $topic_id ) ) {
2442          return;
2443      }
2444  
2445      // If it's a reply, get the topic id.
2446      if ( bbp_is_reply( $topic_id ) ) {
2447          $reply_id = $topic_id;
2448          $topic_id = bbp_get_reply_topic_id( $reply_id );
2449  
2450          // Update inverse based on item status
2451          if ( bbp_is_reply_public( $reply_id ) ) {
2452              bbp_decrease_topic_reply_count( $topic_id );
2453              return;
2454          }
2455      }
2456  
2457      // Bump down
2458      bbp_bump_topic_reply_count_hidden( $topic_id, -1 );
2459  }
2460  
2461  /**
2462   * Update counts after a topic is inserted via `bbp_insert_topic`.
2463   *
2464   * @since 2.6.0 bbPress (r6036)
2465   *
2466   * @param int $topic_id The topic id.
2467   * @param int $forum_id The forum id.
2468   *
2469   * @return void
2470   */
2471  function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) {
2472  
2473      // If the topic is public, update the forum topic counts.
2474      if ( bbp_is_topic_public( $topic_id ) ) {
2475          bbp_increase_forum_topic_count( $forum_id );
2476  
2477      // If the topic isn't public only update the forum topic hidden count.
2478      } else {
2479          bbp_increase_forum_topic_count_hidden( $forum_id );
2480      }
2481  }
2482  
2483  /** Topic Updaters ************************************************************/
2484  
2485  /**
2486   * Update the topic's forum id
2487   *
2488   * @since 2.0.0 bbPress (r2855)
2489   *
2490   * @param int $topic_id Optional. Topic id to update
2491   * @param int $forum_id Optional. Forum id
2492   * @return int Forum id
2493   */
2494  function bbp_update_topic_forum_id( $topic_id = 0, $forum_id = 0 ) {
2495  
2496      // If it's a reply, then get the parent (topic id)
2497      $topic_id = bbp_is_reply( $topic_id )
2498          ? bbp_get_reply_topic_id( $topic_id )
2499          : bbp_get_topic_id( $topic_id );
2500  
2501      // Forum ID fallback
2502      if ( empty( $forum_id ) ) {
2503          $forum_id = get_post_field( 'post_parent', $topic_id );
2504      }
2505  
2506      $forum_id = (int) $forum_id;
2507  
2508      update_post_meta( $topic_id, '_bbp_forum_id', $forum_id );
2509  
2510      // Filter & return
2511      return (int) apply_filters( 'bbp_update_topic_forum_id', $forum_id, $topic_id );
2512  }
2513  
2514  /**
2515   * Update the topic's topic id
2516   *
2517   * @since 2.0.0 bbPress (r2954)
2518   *
2519   * @param int $topic_id Optional. Topic id to update
2520   * @return int Topic id
2521   */
2522  function bbp_update_topic_topic_id( $topic_id = 0 ) {
2523      $topic_id = bbp_get_topic_id( $topic_id );
2524  
2525      update_post_meta( $topic_id, '_bbp_topic_id', $topic_id );
2526  
2527      // Filter & return
2528      return apply_filters( 'bbp_update_topic_topic_id', $topic_id );
2529  }
2530  
2531  /**
2532   * Adjust the total reply count of a topic
2533   *
2534   * @since 2.0.0 bbPress (r2467)
2535   *
2536   * @param int $topic_id Optional. Topic id to update
2537   * @param int $reply_count Optional. Set the reply count manually.
2538   * @return int Topic reply count
2539   */
2540  function bbp_update_topic_reply_count( $topic_id = 0, $reply_count = false ) {
2541  
2542      // If it's a reply, then get the parent (topic id)
2543      $topic_id = bbp_is_reply( $topic_id )
2544          ? bbp_get_reply_topic_id( $topic_id )
2545          : bbp_get_topic_id( $topic_id );
2546  
2547      // Get replies of topic if not passed
2548      $reply_count = ! is_int( $reply_count )
2549          ? bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() )
2550          : (int) $reply_count;
2551  
2552      update_post_meta( $topic_id, '_bbp_reply_count', $reply_count );
2553  
2554      // Filter & return
2555      return (int) apply_filters( 'bbp_update_topic_reply_count', $reply_count, $topic_id );
2556  }
2557  
2558  /**
2559   * Adjust the total hidden reply count of a topic (hidden includes trashed,
2560   * spammed and pending replies)
2561   *
2562   * @since 2.0.0 bbPress (r2740)
2563   *
2564   * @param int $topic_id Optional. Topic id to update
2565   * @param int $reply_count Optional. Set the reply count manually
2566   * @return int Topic hidden reply count
2567   */
2568  function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = false ) {
2569  
2570      // If it's a reply, then get the parent (topic id)
2571      $topic_id = bbp_is_reply( $topic_id )
2572          ? bbp_get_reply_topic_id( $topic_id )
2573          : bbp_get_topic_id( $topic_id );
2574  
2575      // Get replies of topic
2576      $reply_count = ! is_int( $reply_count )
2577          ? bbp_get_non_public_child_count( $topic_id, bbp_get_reply_post_type() )
2578          : (int) $reply_count;
2579  
2580      update_post_meta( $topic_id, '_bbp_reply_count_hidden', $reply_count );
2581  
2582      // Filter & return
2583      return (int) apply_filters( 'bbp_update_topic_reply_count_hidden', $reply_count, $topic_id );
2584  }
2585  
2586  /**
2587   * Update the topic with the last active post ID
2588   *
2589   * @since 2.0.0 bbPress (r2888)
2590   *
2591   * @param int $topic_id Optional. Topic id to update
2592   * @param int $active_id Optional. active id
2593   * @return int Active id
2594   */
2595  function bbp_update_topic_last_active_id( $topic_id = 0, $active_id = 0 ) {
2596  
2597      // If it's a reply, then get the parent (topic id)
2598      $topic_id = bbp_is_reply( $topic_id )
2599          ? bbp_get_reply_topic_id( $topic_id )
2600          : bbp_get_topic_id( $topic_id );
2601  
2602      // Get last public active id if not passed
2603      if ( empty( $active_id ) ) {
2604          $active_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
2605      }
2606  
2607      // Adjust last_id's based on last_reply post_type
2608      if ( empty( $active_id ) || ! bbp_is_reply( $active_id ) ) {
2609          $active_id = $topic_id;
2610      }
2611  
2612      $active_id = (int) $active_id;
2613  
2614      // Update only if published
2615      update_post_meta( $topic_id, '_bbp_last_active_id', $active_id );
2616  
2617      // Filter & return
2618      return (int) apply_filters( 'bbp_update_topic_last_active_id', $active_id, $topic_id );
2619  }
2620  
2621  /**
2622   * Update the topics last active date/time (aka freshness)
2623   *
2624   * @since 2.0.0 bbPress (r2680)
2625   *
2626   * @param int    $topic_id Optional. Topic id.
2627   * @param string $new_time Optional. New time in mysql format.
2628   * @return string MySQL timestamp of last active reply
2629   */
2630  function bbp_update_topic_last_active_time( $topic_id = 0, $new_time = '' ) {
2631  
2632      // If it's a reply, then get the parent (topic id)
2633      $topic_id = bbp_is_reply( $topic_id )
2634          ? bbp_get_reply_topic_id( $topic_id )
2635          : bbp_get_topic_id( $topic_id );
2636  
2637      // Check time and use current if empty
2638      if ( empty( $new_time ) ) {
2639          $new_time = get_post_field( 'post_date', bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ) );
2640      }
2641  
2642      // Update only if published
2643      if ( ! empty( $new_time ) ) {
2644          update_post_meta( $topic_id, '_bbp_last_active_time', $new_time );
2645      }
2646  
2647      // Filter & return
2648      return apply_filters( 'bbp_update_topic_last_active_time', $new_time, $topic_id );
2649  }
2650  
2651  /**
2652   * Update the topic with the most recent reply ID
2653   *
2654   * @since 2.0.0 bbPress (r2625)
2655   *
2656   * @param int $topic_id Optional. Topic id to update
2657   * @param int $reply_id Optional. Reply id
2658   * @return int Reply id
2659   */
2660  function bbp_update_topic_last_reply_id( $topic_id = 0, $reply_id = 0 ) {
2661  
2662      // If it's a reply, then get the parent (topic id)
2663      if ( empty( $reply_id ) && bbp_is_reply( $topic_id ) ) {
2664          $reply_id = bbp_get_reply_id( $topic_id );
2665          $topic_id = bbp_get_reply_topic_id( $reply_id );
2666      } else {
2667          $reply_id = bbp_get_reply_id( $reply_id );
2668          $topic_id = bbp_get_topic_id( $topic_id );
2669      }
2670  
2671      if ( empty( $reply_id ) ) {
2672          $reply_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
2673      }
2674  
2675      // Adjust last_id's based on last_reply post_type
2676      if ( empty( $reply_id ) || ! bbp_is_reply( $reply_id ) ) {
2677          $reply_id = 0;
2678      }
2679  
2680      $reply_id = (int) $reply_id;
2681  
2682      // Update if reply is published
2683      update_post_meta( $topic_id, '_bbp_last_reply_id', $reply_id );
2684  
2685      // Filter & return
2686      return (int) apply_filters( 'bbp_update_topic_last_reply_id', $reply_id, $topic_id );
2687  }
2688  
2689  /**
2690   * Adjust the total voice count of a topic
2691   *
2692   * @since 2.0.0 bbPress (r2567)
2693   * @since 2.6.0 bbPress (r6515) This must be called after any engagement changes
2694   *
2695   * @param int $topic_id Optional. Topic id to update
2696   * @return int Voice count
2697   */
2698  function bbp_update_topic_voice_count( $topic_id = 0 ) {
2699  
2700      // If it's a reply, then get the parent (topic id)
2701      $topic_id = bbp_is_reply( $topic_id )
2702          ? bbp_get_reply_topic_id( $topic_id )
2703          : bbp_get_topic_id( $topic_id );
2704  
2705      // Bail if no topic ID
2706      if ( empty( $topic_id ) ) {
2707          return;
2708      }
2709  
2710      // Count the engagements
2711      $count = count( bbp_get_topic_engagements( $topic_id ) );
2712  
2713      // Update the voice count for this topic id
2714      update_post_meta( $topic_id, '_bbp_voice_count', $count );
2715  
2716      // Filter & return
2717      return (int) apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id );
2718  }
2719  
2720  /**
2721   * Adjust the total anonymous reply count of a topic
2722   *
2723   * @since 2.0.0 bbPress (r2567)
2724   *
2725   * @param int $topic_id Optional. Topic id to update
2726   * @return int Anonymous reply count
2727   */
2728  function bbp_update_topic_anonymous_reply_count( $topic_id = 0 ) {
2729  
2730      // If it's a reply, then get the parent (topic id)
2731      $topic_id = bbp_is_reply( $topic_id )
2732          ? bbp_get_reply_topic_id( $topic_id )
2733          : bbp_get_topic_id( $topic_id );
2734  
2735      // Query the DB to get anonymous replies in this topic
2736      $bbp_db  = bbp_db();
2737      $query   = $bbp_db->prepare( "SELECT COUNT( ID ) FROM {$bbp_db->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() );
2738      $replies = (int) $bbp_db->get_var( $query );
2739  
2740      update_post_meta( $topic_id, '_bbp_anonymous_reply_count', $replies );
2741  
2742      // Filter & return
2743      return (int) apply_filters( 'bbp_update_topic_anonymous_reply_count', $replies, $topic_id );
2744  }
2745  
2746  /**
2747   * Update the revision log of the topic
2748   *
2749   * @since 2.0.0 bbPress (r2782)
2750   *
2751   * @param array $args Supports these args:
2752   *  - topic_id: Topic id
2753   *  - author_id: Author id
2754   *  - reason: Reason for editing
2755   *  - revision_id: Revision id
2756   * @return mixed False on failure, true on success
2757   */
2758  function bbp_update_topic_revision_log( $args = array() ) {
2759  
2760      // Parse arguments against default values
2761      $r = bbp_parse_args( $args, array(
2762          'reason'      => '',
2763          'topic_id'    => 0,
2764          'author_id'   => 0,
2765          'revision_id' => 0
2766      ), 'update_topic_revision_log' );
2767  
2768      // Populate the variables
2769      $r['reason']      = bbp_format_revision_reason( $r['reason'] );
2770      $r['topic_id']    = bbp_get_topic_id( $r['topic_id'] );
2771      $r['author_id']   = bbp_get_user_id ( $r['author_id'], false, true );
2772      $r['revision_id'] = (int) $r['revision_id'];
2773  
2774      // Get the logs and append the new one to those
2775      $revision_log                      = bbp_get_topic_raw_revision_log( $r['topic_id'] );
2776      $revision_log[ $r['revision_id'] ] = array( 'author' => $r['author_id'], 'reason' => $r['reason'] );
2777  
2778      // Finally, update
2779      return update_post_meta( $r['topic_id'], '_bbp_revision_log', $revision_log );
2780  }
2781  
2782  /** Topic Actions *************************************************************/
2783  
2784  /**
2785   * Closes a topic
2786   *
2787   * @since 2.0.0 bbPress (r2740)
2788   *
2789   * @param int $topic_id Topic id
2790   * @return mixed False or {@link WP_Error} on failure, topic id on success
2791   */
2792  function bbp_close_topic( $topic_id = 0 ) {
2793  
2794      // Get topic
2795      $topic = bbp_get_topic( $topic_id );
2796      if ( empty( $topic ) ) {
2797          return $topic;
2798      }
2799  
2800      // Get previous topic status meta
2801      $status       = bbp_get_closed_status_id();
2802      $topic_status = get_post_meta( $topic_id, '_bbp_status', true );
2803  
2804      // Bail if already closed and topic status meta exists
2805      if ( $status === $topic->post_status && ! empty( $topic_status ) ) {
2806          return false;
2807      }
2808  
2809      // Set status meta public
2810      $topic_status = $topic->post_status;
2811  
2812      // Execute pre close code
2813      do_action( 'bbp_close_topic', $topic_id );
2814  
2815      // Add pre close status
2816      add_post_meta( $topic_id, '_bbp_status', $topic_status );
2817  
2818      // Set closed status
2819      $topic->post_status = $status;
2820  
2821      // Toggle revisions off as we are not altering content
2822      if ( post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) {
2823          $revisions_removed = true;
2824          remove_post_type_support( bbp_get_topic_post_type(), 'revisions' );
2825      }
2826  
2827      // Update topic
2828      $topic_id = wp_update_post( $topic );
2829  
2830      // Toggle revisions back on
2831      if ( true === $revisions_removed ) {
2832          $revisions_removed = false;
2833          add_post_type_support( bbp_get_topic_post_type(), 'revisions' );
2834      }
2835  
2836      // Execute post close code
2837      do_action( 'bbp_closed_topic', $topic_id );
2838  
2839      // Return topic_id
2840      return $topic_id;
2841  }
2842  
2843  /**
2844   * Opens a topic
2845   *
2846   * @since 2.0.0 bbPress (r2740)
2847   *
2848   * @param int $topic_id Topic id
2849   * @return mixed False or {@link WP_Error} on failure, topic id on success
2850   */
2851  function bbp_open_topic( $topic_id = 0 ) {
2852  
2853      // Get topic
2854      $topic = bbp_get_topic( $topic_id );
2855      if ( empty( $topic ) ) {
2856          return $topic;
2857      }
2858  
2859      // Bail if not closed
2860      if ( bbp_get_closed_status_id() !== $topic->post_status ) {
2861          return false;
2862      }
2863  
2864      // Execute pre open code
2865      do_action( 'bbp_open_topic', $topic_id );
2866  
2867      // Get previous status
2868      $topic_status = get_post_meta( $topic_id, '_bbp_status', true );
2869  
2870      // If no previous status, default to publish
2871      if ( empty( $topic_status ) ) {
2872          $topic_status = bbp_get_public_status_id();
2873      }
2874  
2875      // Set previous status
2876      $topic->post_status = $topic_status;
2877  
2878      // Remove old status meta
2879      delete_post_meta( $topic_id, '_bbp_status' );
2880  
2881      // Toggle revisions off as we are not altering content
2882      if ( post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) {
2883          $revisions_removed = true;
2884          remove_post_type_support( bbp_get_topic_post_type(), 'revisions' );
2885      }
2886  
2887      // Update topic
2888      $topic_id = wp_update_post( $topic );
2889  
2890      // Toggle revisions back on
2891      if ( true === $revisions_removed ) {
2892          $revisions_removed = false;
2893          add_post_type_support( bbp_get_topic_post_type(), 'revisions' );
2894      }
2895  
2896      // Execute post open code
2897      do_action( 'bbp_opened_topic', $topic_id );
2898  
2899      // Return topic_id
2900      return $topic_id;
2901  }
2902  
2903  /**
2904   * Marks a topic as spam
2905   *
2906   * @since 2.0.0 bbPress (r2740)
2907   *
2908   * @param int $topic_id Topic id
2909   * @return mixed False or {@link WP_Error} on failure, topic id on success
2910   */
2911  function bbp_spam_topic( $topic_id = 0 ) {
2912  
2913      // Get the topic
2914      $topic = bbp_get_topic( $topic_id );
2915      if ( empty( $topic ) ) {
2916          return $topic;
2917      }
2918  
2919      // Get new status
2920      $status = bbp_get_spam_status_id();
2921  
2922      // Bail if topic is spam
2923      if ( $status === $topic->post_status ) {
2924          return false;
2925      }
2926  
2927      // Add the original post status as post meta for future restoration
2928      add_post_meta( $topic_id, '_bbp_spam_meta_status', $topic->post_status );
2929  
2930      // Execute pre spam code
2931      do_action( 'bbp_spam_topic', $topic_id );
2932  
2933      // Set post status to spam
2934      $topic->post_status = $status;
2935  
2936      // Empty the topic of its tags
2937      $topic->tax_input = bbp_spam_topic_tags( $topic_id );
2938  
2939      // No revisions
2940      remove_action( 'pre_post_update', 'wp_save_post_revision' );
2941  
2942      // Update the topic
2943      $topic_id = wp_update_post( $topic );
2944  
2945      // Execute post spam code
2946      do_action( 'bbp_spammed_topic', $topic_id );
2947  
2948      // Return topic_id
2949      return $topic_id;
2950  }
2951  
2952  /**
2953   * Trash replies to a topic when it's marked as spam
2954   *
2955   * Usually you'll want to do this before the topic itself is marked as spam.
2956   *
2957   * @since 2.6.0 bbPress (r5405)
2958   *
2959   * @param int $topic_id
2960   */
2961  function bbp_spam_topic_replies( $topic_id = 0 ) {
2962  
2963      // Validation
2964      $topic_id = bbp_get_topic_id( $topic_id );
2965  
2966      // Topic is being spammed, so its replies are trashed
2967      $replies = new WP_Query( array(
2968          'fields'         => 'id=>parent',
2969          'post_type'      => bbp_get_reply_post_type(),
2970          'post_status'    => bbp_get_public_status_id(),
2971          'post_parent'    => $topic_id,
2972          'posts_per_page' => -1,
2973  
2974          // Performance
2975          'nopaging'               => true,
2976          'suppress_filters'       => true,
2977          'update_post_term_cache' => false,
2978          'update_post_meta_cache' => false,
2979          'ignore_sticky_posts'    => true,
2980          'no_found_rows'          => true
2981      ) );
2982  
2983      if ( ! empty( $replies->posts ) ) {
2984  
2985          // Prevent debug notices
2986          $pre_spammed_replies = array();
2987  
2988          // Loop through replies, trash them, and add them to array
2989          foreach ( $replies->posts as $reply ) {
2990              wp_trash_post( $reply->ID );
2991              $pre_spammed_replies[] = $reply->ID;
2992          }
2993  
2994          // Set a post_meta entry of the replies that were trashed by this action.
2995          // This is so we can possibly untrash them, without untrashing replies
2996          // that were purposefully trashed before.
2997          update_post_meta( $topic_id, '_bbp_pre_spammed_replies', $pre_spammed_replies );
2998  
2999          // Reset the global post data after looping through the above WP_Query
3000          wp_reset_postdata();
3001      }
3002  }
3003  
3004  /**
3005   * Store the tags to a topic in post meta before it's marked as spam so they
3006   * can be retrieved and unspammed later.
3007   *
3008   * Usually you'll want to do this before the topic itself is marked as spam.
3009   *
3010   * @since 2.6.0 bbPress (r5405)
3011   *
3012   * @param int $topic_id
3013   */
3014  function bbp_spam_topic_tags( $topic_id = 0 ) {
3015  
3016      // Validation
3017      $topic_id = bbp_get_topic_id( $topic_id );
3018  
3019      // Get topic tags
3020      $terms = get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() );
3021  
3022      // Define local variable(s)
3023      $term_names = array();
3024  
3025      // Topic has tags
3026      if ( ! empty( $terms ) ) {
3027  
3028          // Loop through and collect term names
3029          foreach ( $terms as $term ) {
3030              $term_names[] = $term->name;
3031          }
3032  
3033          // Topic terms have slugs
3034          if ( ! empty( $term_names ) ) {
3035  
3036              // Add the original post status as post meta for future restoration
3037              add_post_meta( $topic_id, '_bbp_spam_topic_tags', $term_names );
3038          }
3039      }
3040  
3041      return array( bbp_get_topic_tag_tax_id() => '' );
3042  }
3043  
3044  /**
3045   * Unspams a topic
3046   *
3047   * @since 2.0.0 bbPress (r2740)
3048   *
3049   * @param int $topic_id Topic id
3050   * @return mixed False or {@link WP_Error} on failure, topic id on success
3051   */
3052  function bbp_unspam_topic( $topic_id = 0 ) {
3053  
3054      // Get the topic
3055      $topic = bbp_get_topic( $topic_id );
3056      if ( empty( $topic ) ) {
3057          return $topic;
3058      }
3059  
3060      // Bail if already not spam
3061      if ( bbp_get_spam_status_id() !== $topic->post_status ) {
3062          return false;
3063      }
3064  
3065      // Execute pre unspam code
3066      do_action( 'bbp_unspam_topic', $topic_id );
3067  
3068      // Get pre spam status
3069      $topic_status = get_post_meta( $topic_id, '_bbp_spam_meta_status', true );
3070  
3071      // If no previous status, default to publish
3072      if ( empty( $topic_status ) ) {
3073          $topic_status = bbp_get_public_status_id();
3074      }
3075  
3076      // Set post status to pre spam
3077      $topic->post_status = $topic_status;
3078      $topic->tax_input   = bbp_unspam_topic_tags( $topic_id );
3079  
3080      // Delete pre spam meta
3081      delete_post_meta( $topic_id, '_bbp_spam_meta_status' );
3082  
3083      // No revisions
3084      remove_action( 'pre_post_update', 'wp_save_post_revision' );
3085  
3086      // Update the topic
3087      $topic_id = wp_update_post( $topic );
3088  
3089      // Execute post unspam code
3090      do_action( 'bbp_unspammed_topic', $topic_id );
3091  
3092      // Return topic_id
3093      return $topic_id;
3094  }
3095  
3096  /**
3097   * Untrash replies to a topic previously marked as spam.
3098   *
3099   * Usually you'll want to do this after the topic is unspammed.
3100   *
3101   * @since 2.6.0 bbPress (r5405)
3102   *
3103   * @param int $topic_id
3104   */
3105  function bbp_unspam_topic_replies( $topic_id = 0 ) {
3106  
3107      // Validation
3108      $topic_id = bbp_get_topic_id( $topic_id );
3109  
3110      // Get the replies that were not previously trashed
3111      $pre_spammed_replies = get_post_meta( $topic_id, '_bbp_pre_spammed_replies', true );
3112  
3113      // There are replies to untrash
3114      if ( ! empty( $pre_spammed_replies ) ) {
3115  
3116          // Maybe reverse the trashed replies array
3117          if ( is_array( $pre_spammed_replies ) ) {
3118              $pre_spammed_replies = array_reverse( $pre_spammed_replies );
3119          }
3120  
3121          // Loop through replies
3122          foreach ( (array) $pre_spammed_replies as $reply ) {
3123              wp_untrash_post( $reply );
3124          }
3125      }
3126  
3127      // Clear the trasheed reply meta for the topic
3128      delete_post_meta( $topic_id, '_bbp_pre_spammed_replies' );
3129  }
3130  
3131  /**
3132   * Retrieve tags to a topic from post meta before it's unmarked as spam so they.
3133   *
3134   * Usually you'll want to do this before the topic itself is unmarked as spam.
3135   *
3136   * @since 2.6.0 bbPress (r5405)
3137   *
3138   * @param int $topic_id
3139   */
3140  function bbp_unspam_topic_tags( $topic_id = 0 ) {
3141  
3142      // Validation
3143      $topic_id = bbp_get_topic_id( $topic_id );
3144  
3145      // Get pre-spam topic tags
3146      $terms = get_post_meta( $topic_id, '_bbp_spam_topic_tags', true );
3147  
3148      // Delete pre-spam topic tag meta
3149      if ( ! empty( $terms ) ) {
3150          delete_post_meta( $topic_id, '_bbp_spam_topic_tags' );
3151      }
3152  
3153      return array( bbp_get_topic_tag_tax_id() => $terms );
3154  }
3155  
3156  /**
3157   * Sticks a topic to a forum or front
3158   *
3159   * @since 2.0.0 bbPress (r2754)
3160   *
3161   * @param int $topic_id Optional. Topic id
3162   * @param int $super Should we make the topic a super sticky?
3163   * @return bool True on success, false on failure
3164   */
3165  function bbp_stick_topic( $topic_id = 0, $super = false ) {
3166  
3167      // Validation
3168      $topic_id = bbp_get_topic_id( $topic_id );
3169  
3170      // Bail if a topic is not a topic (prevents revisions as stickies)
3171      if ( ! bbp_is_topic( $topic_id ) ) {
3172          return false;
3173      }
3174  
3175      do_action( 'bbp_stick_topic', $topic_id, $super );
3176  
3177      // Maybe get the forum ID if not getting supers
3178      $forum_id = empty( $super )
3179          ? bbp_get_topic_forum_id( $topic_id )
3180          : 0;
3181  
3182      // Get the stickies, maybe from the forum ID
3183      $stickies = bbp_get_stickies( $forum_id );
3184  
3185      // Add the topic to the stickies
3186      $stickies[] = $topic_id;
3187  
3188      // Pull out duplicates and empties
3189      $stickies = array_unique( array_filter( $stickies ) );
3190  
3191      // Unset incorrectly stuck revisions
3192      foreach ( (array) $stickies as $key => $id ) {
3193          if ( ! bbp_is_topic( $id ) ) {
3194              unset( $stickies[ $key ] );
3195          }
3196      }
3197  
3198      // Reset keys
3199      $stickies = array_values( $stickies );
3200  
3201      // Update
3202      $success  = ! empty( $super )
3203          ? update_option( '_bbp_super_sticky_topics', $stickies )
3204          : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
3205  
3206      do_action( 'bbp_stuck_topic', $topic_id, $super, $success );
3207  
3208      return (bool) $success;
3209  }
3210  
3211  /**
3212   * Approves a pending topic
3213   *
3214   * @since 2.6.0 bbPress (r5503)
3215   *
3216   * @param int $topic_id Topic id
3217   * @return mixed False or {@link WP_Error} on failure, topic id on success
3218   */
3219  function bbp_approve_topic( $topic_id = 0 ) {
3220  
3221      // Get topic
3222      $topic = bbp_get_topic( $topic_id );
3223      if ( empty( $topic ) ) {
3224          return $topic;
3225      }
3226  
3227      // Get new status
3228      $status = bbp_get_public_status_id();
3229  
3230      // Bail if already approved
3231      if ( $status === $topic->post_status ) {
3232          return false;
3233      }
3234  
3235      // Execute pre pending code
3236      do_action( 'bbp_approve_topic', $topic_id );
3237  
3238      // Set publish status
3239      $topic->post_status = $status;
3240  
3241      // No revisions
3242      remove_action( 'pre_post_update', 'wp_save_post_revision' );
3243  
3244      // Update topic
3245      $topic_id = wp_update_post( $topic );
3246  
3247      // Execute post pending code
3248      do_action( 'bbp_approved_topic', $topic_id );
3249  
3250      // Return topic_id
3251      return $topic_id;
3252  }
3253  
3254  /**
3255   * Unapproves a topic
3256   *
3257   * @since 2.6.0 bbPress (r5503)
3258   *
3259   * @param int $topic_id Topic id
3260   * @return mixed False or {@link WP_Error} on failure, topic id on success
3261   */
3262  function bbp_unapprove_topic( $topic_id = 0 ) {
3263  
3264      // Get topic
3265      $topic = bbp_get_topic( $topic_id );
3266      if ( empty( $topic ) ) {
3267          return $topic;
3268      }
3269  
3270      // Get new status
3271      $status = bbp_get_pending_status_id();
3272  
3273      // Bail if already unapproved
3274      if ( ! bbp_is_topic_public( $topic_id ) ) {
3275          return false;
3276      }
3277  
3278      // Execute pre open code
3279      do_action( 'bbp_unapprove_topic', $topic_id );
3280  
3281      // Set pending status
3282      $topic->post_status = $status;
3283  
3284      // No revisions
3285      remove_action( 'pre_post_update', 'wp_save_post_revision' );
3286  
3287      // Update topic
3288      $topic_id = wp_update_post( $topic );
3289  
3290      // Execute post open code
3291      do_action( 'bbp_unapproved_topic', $topic_id );
3292  
3293      // Return topic_id
3294      return $topic_id;
3295  }
3296  
3297  /**
3298   * Unsticks a topic both from front and it's forum
3299   *
3300   * @since 2.0.0 bbPress (r2754)
3301   *
3302   * @param int $topic_id Optional. Topic id
3303   * @return bool Always true.
3304   */
3305  function bbp_unstick_topic( $topic_id = 0 ) {
3306  
3307      // Get topic sticky status
3308      $topic_id = bbp_get_topic_id( $topic_id );
3309      $super    = bbp_is_topic_super_sticky( $topic_id );
3310      $forum_id = empty( $super ) ? bbp_get_topic_forum_id( $topic_id ) : 0;
3311      $stickies = bbp_get_stickies( $forum_id );
3312      $offset   = array_search( $topic_id, $stickies );
3313  
3314      do_action( 'bbp_unstick_topic', $topic_id );
3315  
3316      // Nothing to unstick
3317      if ( empty( $stickies ) ) {
3318          $success = true;
3319  
3320      // Topic not in stickies
3321      } elseif ( ! in_array( $topic_id, $stickies, true ) ) {
3322          $success = true;
3323  
3324      // Topic not in stickies
3325      } elseif ( false === $offset ) {
3326          $success = true;
3327  
3328      // Splice out the offset
3329      } else {
3330          array_splice( $stickies, $offset, 1 );
3331  
3332          if ( empty( $stickies ) ) {
3333              $success = ! empty( $super )
3334                  ? delete_option( '_bbp_super_sticky_topics' )
3335                  : delete_post_meta( $forum_id, '_bbp_sticky_topics' );
3336          } else {
3337              $success = ! empty( $super )
3338                  ? update_option( '_bbp_super_sticky_topics', $stickies )
3339                  : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
3340          }
3341      }
3342  
3343      do_action( 'bbp_unstuck_topic', $topic_id, $success );
3344  
3345      return (bool) $success;
3346  }
3347  
3348  /** Before Delete/Trash/Untrash ***********************************************/
3349  
3350  /**
3351   * Called before deleting a topic.
3352   *
3353   * This function is supplemental to the actual topic deletion which is
3354   * handled by WordPress core API functions. It is used to clean up after
3355   * a topic that is being deleted.
3356   */
3357  function bbp_delete_topic( $topic_id = 0 ) {
3358  
3359      // Validate topic ID
3360      $topic_id = bbp_get_topic_id( $topic_id );
3361  
3362      if ( empty( $topic_id ) || ! bbp_is_topic( $topic_id ) ) {
3363          return false;
3364      }
3365  
3366      do_action( 'bbp_delete_topic', $topic_id );
3367  }
3368  
3369  /**
3370   * Delete replies to a topic when it's deleted
3371   *
3372   * Usually you'll want to do this before the topic itself is deleted.
3373   *
3374   * @since 2.6.0 bbPress (r5405)
3375   *
3376   * @param int $topic_id
3377   */
3378  function bbp_delete_topic_replies( $topic_id = 0 ) {
3379  
3380      // Validate topic ID
3381      $topic_id = bbp_get_topic_id( $topic_id );
3382  
3383      // Topic is being permanently deleted, so its replies gotta go too
3384      // Note that we get all post statuses here
3385      $replies = new WP_Query( array(
3386          'fields'         => 'id=>parent',
3387          'post_type'      => bbp_get_reply_post_type(),
3388          'post_status'    => array_keys( get_post_stati() ),
3389          'post_parent'    => $topic_id,
3390          'posts_per_page' => -1,
3391  
3392          // Performance
3393          'nopaging'               => true,
3394          'suppress_filters'       => true,
3395          'update_post_term_cache' => false,
3396          'update_post_meta_cache' => false,
3397          'ignore_sticky_posts'    => true,
3398          'no_found_rows'          => true
3399      ) );
3400  
3401      // Loop through and delete child replies
3402      if ( ! empty( $replies->posts ) ) {
3403          foreach ( $replies->posts as $reply ) {
3404              wp_delete_post( $reply->ID, true );
3405          }
3406  
3407          // Reset the $post global
3408          wp_reset_postdata();
3409      }
3410  }
3411  
3412  /**
3413   * Called before trashing a topic
3414   *
3415   * This function is supplemental to the actual topic being trashed which is
3416   * handled by WordPress core API functions. It is used to clean up after
3417   * a topic that is being trashed.
3418   */
3419  function bbp_trash_topic( $topic_id = 0 ) {
3420  
3421      // Validate topic ID
3422      $topic_id = bbp_get_topic_id( $topic_id );
3423  
3424      if ( empty( $topic_id ) || ! bbp_is_topic( $topic_id ) ) {
3425          return false;
3426      }
3427  
3428      do_action( 'bbp_trash_topic', $topic_id );
3429  }
3430  
3431  /**
3432   * Trash replies to a topic when it's trashed.
3433   *
3434   * Usually you'll want to do this before the topic itself is marked as spam.
3435   *
3436   * @since 2.6.0 bbPress (r5405)
3437   *
3438   * @param int $topic_id
3439   */
3440  function bbp_trash_topic_replies( $topic_id = 0 ) {
3441  
3442      // Validate topic ID
3443      $topic_id = bbp_get_topic_id( $topic_id );
3444  
3445      // Topic is being trashed, so its replies are trashed too
3446      $replies = new WP_Query( array(
3447          'fields'         => 'id=>parent',
3448          'post_type'      => bbp_get_reply_post_type(),
3449          'post_status'    => bbp_get_public_status_id(),
3450          'post_parent'    => $topic_id,
3451          'posts_per_page' => -1,
3452  
3453          // Performance
3454          'nopaging'               => true,
3455          'suppress_filters'       => true,
3456          'update_post_term_cache' => false,
3457          'update_post_meta_cache' => false,
3458          'ignore_sticky_posts'    => true,
3459          'no_found_rows'          => true
3460      ) );
3461  
3462      if ( ! empty( $replies->posts ) ) {
3463  
3464          // Prevent debug notices
3465          $pre_trashed_replies = array();
3466  
3467          // Loop through replies, trash them, and add them to array
3468          foreach ( $replies->posts as $reply ) {
3469              wp_trash_post( $reply->ID );
3470              $pre_trashed_replies[] = $reply->ID;
3471          }
3472  
3473          // Set a post_meta entry of the replies that were trashed by this action.
3474          // This is so we can possibly untrash them, without untrashing replies
3475          // that were purposefully trashed before.
3476          update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
3477  
3478          // Reset the $post global
3479          wp_reset_postdata();
3480      }
3481  }
3482  
3483  /**
3484   * Called before untrashing a topic
3485   */
3486  function bbp_untrash_topic( $topic_id = 0 ) {
3487      $topic_id = bbp_get_topic_id( $topic_id );
3488  
3489      if ( empty( $topic_id ) || ! bbp_is_topic( $topic_id ) ) {
3490          return false;
3491      }
3492  
3493      do_action( 'bbp_untrash_topic', $topic_id );
3494  }
3495  
3496  /**
3497   * Untrash replies to a topic previously trashed.
3498   *
3499   * Usually you'll want to do this after the topic is unspammed.
3500   *
3501   * @since 2.6.0 bbPress (r5405)
3502   *
3503   * @param int $topic_id
3504   */
3505  function bbp_untrash_topic_replies( $topic_id = 0 ) {
3506  
3507      // Validation
3508      $topic_id = bbp_get_topic_id( $topic_id );
3509  
3510      // Get the replies that were not previously trashed
3511      $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
3512  
3513      // There are replies to untrash
3514      if ( ! empty( $pre_trashed_replies ) ) {
3515  
3516          // Maybe reverse the trashed replies array
3517          if ( is_array( $pre_trashed_replies ) ) {
3518              $pre_trashed_replies = array_reverse( $pre_trashed_replies );
3519          }
3520  
3521          // Loop through replies
3522          foreach ( (array) $pre_trashed_replies as $reply ) {
3523              wp_untrash_post( $reply );
3524          }
3525      }
3526  
3527      // Clear the trashed reply meta for the topic
3528      delete_post_meta( $topic_id, '_bbp_pre_trashed_replies' );
3529  }
3530  
3531  /** After Delete/Trash/Untrash ************************************************/
3532  
3533  /**
3534   * Called after deleting a topic
3535   *
3536   * @since 2.0.0 bbPress (r2993)
3537   */
3538  function bbp_deleted_topic( $topic_id = 0 ) {
3539      $topic_id = bbp_get_topic_id( $topic_id );
3540  
3541      if ( empty( $topic_id ) || ! bbp_is_topic( $topic_id ) ) {
3542          return false;
3543      }
3544  
3545      do_action( 'bbp_deleted_topic', $topic_id );
3546  }
3547  
3548  /**
3549   * Called after trashing a topic
3550   *
3551   * @since 2.0.0 bbPress (r2993)
3552   */
3553  function bbp_trashed_topic( $topic_id = 0 ) {
3554      $topic_id = bbp_get_topic_id( $topic_id );
3555  
3556      if ( empty( $topic_id ) || ! bbp_is_topic( $topic_id ) ) {
3557          return false;
3558      }
3559  
3560      do_action( 'bbp_trashed_topic', $topic_id );
3561  }
3562  
3563  /**
3564   * Called after untrashing a topic
3565   *
3566   * @since 2.0.0 bbPress (r2993)
3567   */
3568  function bbp_untrashed_topic( $topic_id = 0 ) {
3569      $topic_id = bbp_get_topic_id( $topic_id );
3570  
3571      if ( empty( $topic_id ) || ! bbp_is_topic( $topic_id ) ) {
3572          return false;
3573      }
3574  
3575      do_action( 'bbp_untrashed_topic', $topic_id );
3576  }
3577  
3578  /** Settings ******************************************************************/
3579  
3580  /**
3581   * Return the topics per page setting
3582   *
3583   * @since 2.0.0 bbPress (r3540)
3584   * @return int
3585   */
3586  function bbp_get_topics_per_page( $default = 15 ) {
3587  
3588      // Get database option and cast as integer
3589      $retval = get_option( '_bbp_topics_per_page', $default );
3590  
3591      // If return val is empty, set it to default
3592      if ( empty( $retval ) ) {
3593          $retval = $default;
3594      }
3595  
3596      // Filter & return
3597      return (int) apply_filters( 'bbp_get_topics_per_page', $retval, $default );
3598  }
3599  
3600  /**
3601   * Return the topics per RSS page setting
3602   *
3603   * @since 2.0.0 bbPress (r3540)
3604   *
3605   * @param int $default Default replies per page (25)
3606   * @return int
3607   */
3608  function bbp_get_topics_per_rss_page( $default = 25 ) {
3609  
3610      // Get database option and cast as integer
3611      $retval = get_option( '_bbp_topics_per_rss_page', $default );
3612  
3613      // If return val is empty, set it to default
3614      if ( empty( $retval ) ) {
3615          $retval = $default;
3616      }
3617  
3618      // Filter & return
3619      return (int) apply_filters( 'bbp_get_topics_per_rss_page', $retval, $default );
3620  }
3621  
3622  /** Topic Tags ****************************************************************/
3623  
3624  /**
3625   * Get topic tags for a specific topic ID
3626   *
3627   * @since 2.6.0 bbPress (r5836)
3628   *
3629   * @param int $topic_id
3630   *
3631   * @return string
3632   */
3633  function bbp_get_topic_tags( $topic_id = 0 ) {
3634      $topic_id   = bbp_get_topic_id( $topic_id );
3635      $terms      = (array) get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() );
3636      $topic_tags = array_filter( $terms );
3637  
3638      // Filter & return
3639      return apply_filters( 'bbp_get_topic_tags', $topic_tags, $topic_id );
3640  }
3641  
3642  /**
3643   * Get topic tags for a specific topic ID
3644   *
3645   * @since 2.2.0 bbPress (r4165)
3646   *
3647   * @param int    $topic_id
3648   * @param string $sep
3649   *
3650   * @return string
3651   */
3652  function bbp_get_topic_tag_names( $topic_id = 0, $sep = ', ' ) {
3653      $topic_tags = bbp_get_topic_tags( $topic_id );
3654      $pluck      = wp_list_pluck( $topic_tags, 'name' );
3655      $terms      = ! empty( $pluck )
3656          ? implode( $sep, $pluck )
3657          : '';
3658  
3659      // Filter & return
3660      return apply_filters( 'bbp_get_topic_tag_names', $terms, $topic_id, $sep );
3661  }
3662  
3663  /**
3664   * Will update topic-tag count based on object type.
3665   *
3666   * Function for the default callback for topic-tag taxonomies.
3667   *
3668   * @see https://bbpress.trac.wordpress.org/ticket/3043
3669   * @access private
3670   *
3671   * @since 2.6.0 bbPress (r6253)
3672   *
3673   * @param array  $terms    List of Term taxonomy IDs.
3674   * @param object $taxonomy Current taxonomy object of terms.
3675   */
3676  function bbp_update_topic_tag_count( $terms, $taxonomy ) {
3677  
3678      // Bail if no object types are available
3679      if ( empty( $terms ) || empty( $taxonomy->object_type ) ) {
3680          return;
3681      }
3682  
3683      // Get object types
3684      $object_types = (array) $taxonomy->object_type;
3685  
3686      foreach ( $object_types as &$object_type ) {
3687          list( $object_type ) = explode( ':', $object_type );
3688      }
3689  
3690      $object_types = array_unique( $object_types );
3691  
3692      if ( ! empty( $object_types ) ) {
3693          $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
3694      }
3695  
3696      // Statuses to count
3697      $object_statuses = bbp_get_public_topic_statuses();
3698  
3699      // Get database
3700      $bbp_db = bbp_db();
3701  
3702      // Loop through terms, maybe update counts
3703      foreach ( (array) $terms as $term ) {
3704          $count = 0;
3705  
3706          // Get count, and bump it
3707          if ( ! empty( $object_types ) ) {
3708              $query    = "SELECT COUNT(*) FROM {$bbp_db->term_relationships}, {$bbp_db->posts} WHERE {$bbp_db->posts}.ID = {$bbp_db->term_relationships}.object_id AND post_status IN ('" . implode("', '", $object_statuses ) . "') AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d";
3709              $prepare  = $bbp_db->prepare( $query, $term );
3710              $count   += (int) $bbp_db->get_var( $prepare );
3711          }
3712  
3713          /** This action is documented in wp-includes/taxonomy.php */
3714          do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
3715          $bbp_db->update( $bbp_db->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
3716  
3717          /** This action is documented in wp-includes/taxonomy.php */
3718          do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
3719      }
3720  }
3721  
3722  /** Autoembed *****************************************************************/
3723  
3724  /**
3725   * Check if autoembeds are enabled and hook them in if so
3726   *
3727   * @since 2.1.0 bbPress (r3752)
3728   *
3729   * @global WP_Embed $wp_embed
3730   */
3731  function bbp_topic_content_autoembed() {
3732      global $wp_embed;
3733  
3734      if ( bbp_use_autoembed() && is_a( $wp_embed, 'WP_Embed' ) ) {
3735          add_filter( 'bbp_get_topic_content', array( $wp_embed, 'autoembed' ), 2 );
3736      }
3737  }
3738  
3739  /** Feeds *********************************************************************/
3740  
3741  /**
3742   * Output an RSS2 feed of topics, based on the query passed.
3743   *
3744   * @since 2.0.0 bbPress (r3171)
3745   *
3746   * @param array $topics_query
3747   */
3748  function bbp_display_topics_feed_rss2( $topics_query = array() ) {
3749  
3750      // User cannot access this forum
3751      if ( bbp_is_single_forum() && ! bbp_user_can_view_forum( array( 'forum_id' => bbp_get_forum_id() ) ) ) {
3752          return;
3753      }
3754  
3755      // Feed title
3756      $title = get_bloginfo_rss( 'name' ) . ' &#187; ' . esc_html__( 'All Topics', 'bbpress' );
3757      $title = apply_filters( 'wp_title_rss', $title );
3758  
3759      // Display the feed
3760      header( 'Content-Type: ' . feed_content_type( 'rss2' ) . '; charset=' . get_option( 'blog_charset' ), true );
3761      header( 'Status: 200 OK' );
3762      echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>
3763  
3764      <rss version="2.0"
3765          xmlns:content="http://purl.org/rss/1.0/modules/content/"
3766          xmlns:wfw="http://wellformedweb.org/CommentAPI/"
3767          xmlns:dc="http://purl.org/dc/elements/1.1/"
3768          xmlns:atom="http://www.w3.org/2005/Atom"
3769  
3770          <?php do_action( 'bbp_feed' ); ?>
3771      >
3772  
3773      <channel>
3774  
3775          <title><?php echo $title; // Already escaped ?></title>
3776          <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
3777          <link><?php self_link(); ?></link>
3778          <description><?php //?></description>
3779          <lastBuildDate><?php echo date( 'r' ); ?></lastBuildDate>
3780          <generator><?php echo esc_url_raw( 'https://bbpress.org/?v=' . convert_chars( bbp_get_version() ) ); ?></generator>
3781          <language><?php bloginfo_rss( 'language' ); ?></language>
3782  
3783          <?php do_action( 'bbp_feed_head' ); ?>
3784  
3785          <?php if ( bbp_has_topics( $topics_query ) ) : ?>
3786  
3787              <?php while ( bbp_topics() ) : bbp_the_topic(); ?>
3788  
3789                  <item>
3790                      <guid><?php bbp_topic_permalink(); ?></guid>
3791                      <title><![CDATA[<?php bbp_topic_title(); ?>]]></title>
3792                      <link><?php bbp_topic_permalink(); ?></link>
3793                      <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_meta( bbp_get_topic_id(), '_bbp_last_active_time', true ), false ); ?></pubDate>
3794                      <dc:creator><?php the_author() ?></dc:creator>
3795  
3796                      <?php if ( !post_password_required() ) : ?>
3797  
3798                      <description>
3799                          <![CDATA[
3800                          <p><?php printf( esc_html__( 'Replies: %s', 'bbpress' ), bbp_get_topic_reply_count() ); ?></p>
3801                          <?php bbp_topic_content(); ?>
3802                          ]]>
3803                      </description>
3804  
3805                      <?php rss_enclosure(); ?>
3806  
3807                      <?php endif; ?>
3808  
3809                      <?php do_action( 'bbp_feed_item' ); ?>
3810  
3811                  </item>
3812  
3813                  <?php endwhile; ?>
3814              <?php endif; ?>
3815  
3816          <?php do_action( 'bbp_feed_footer' ); ?>
3817  
3818      </channel>
3819      </rss>
3820  
3821  <?php
3822      exit();
3823  }
3824  
3825  /** Permissions ***************************************************************/
3826  
3827  /**
3828   * Redirect if unauthorized user is attempting to edit a topic
3829   *
3830   * @since 2.1.0 bbPress (r3605)
3831   */
3832  function bbp_check_topic_edit() {
3833  
3834      // Bail if not editing a topic
3835      if ( ! bbp_is_topic_edit() ) {
3836          return;
3837      }
3838  
3839      // User cannot edit topic, so redirect back to topic
3840      if ( ! current_user_can( 'edit_topic', bbp_get_topic_id() ) ) {
3841          bbp_redirect( bbp_get_topic_permalink() );
3842      }
3843  }
3844  
3845  /**
3846   * Redirect if unauthorized user is attempting to edit a topic tag
3847   *
3848   * @since 2.1.0 bbPress (r3605)
3849   */
3850  function bbp_check_topic_tag_edit() {
3851  
3852      // Bail if not editing a topic tag
3853      if ( ! bbp_is_topic_tag_edit() ) {
3854          return;
3855      }
3856  
3857      // Bail if current user cannot edit topic tags
3858      if ( ! current_user_can( 'edit_topic_tag', bbp_get_topic_tag_id() ) ) {
3859          bbp_redirect( bbp_get_topic_tag_link() );
3860      }
3861  }


Generated: Thu Nov 21 01:01:27 2019 Cross-referenced by PHPXref 0.7.1