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


Generated: Fri Apr 10 01:02:18 2020 Cross-referenced by PHPXref 0.7.1