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


Generated: Tue Dec 1 01:01:27 2020 Cross-referenced by PHPXref 0.7.1