[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * bbPress Reply 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 reply to function properly.
  18   *
  19   * @since 2.0.0 bbPress (r3349)
  20   *
  21   * @param array $reply_data Forum post data
  22   * @param arrap $reply_meta Forum meta data
  23   */
  24  function bbp_insert_reply( $reply_data = array(), $reply_meta = array() ) {
  25  
  26      // Parse arguments against default values
  27      $reply_data = bbp_parse_args( $reply_data, array(
  28          'post_parent'    => 0, // topic ID
  29          'post_type'      => bbp_get_reply_post_type(),
  30          'post_author'    => bbp_get_current_user_id(),
  31          'post_password'  => '',
  32          'post_content'   => '',
  33          'post_title'     => '',
  34          'menu_order'     => bbp_get_topic_reply_count( $reply_data['post_parent'], true ) + 1,
  35          'comment_status' => 'closed'
  36      ), 'insert_reply' );
  37  
  38      // Possibly override status based on parent topic
  39      if ( ! empty( $reply_data['post_parent'] ) && empty( $reply_data['post_status'] ) ) {
  40          $reply_data['post_status'] = bbp_get_topic_status( $reply_data['post_parent'] );
  41      }
  42  
  43      // Insert reply
  44      $reply_id = wp_insert_post( $reply_data, false );
  45  
  46      // Bail if no reply was added
  47      if ( empty( $reply_id ) ) {
  48          return false;
  49      }
  50  
  51      // Parse arguments against default values
  52      $reply_meta = bbp_parse_args( $reply_meta, array(
  53          'author_ip' => bbp_current_author_ip(),
  54          'forum_id'  => 0,
  55          'topic_id'  => 0,
  56          'reply_to'  => 0
  57      ), 'insert_reply_meta' );
  58  
  59      // Insert reply meta
  60      foreach ( $reply_meta as $meta_key => $meta_value ) {
  61  
  62          // Prefix if not prefixed
  63          if ( '_bbp_' !== substr( $meta_key, 0, 5 ) ) {
  64              $meta_key = '_bbp_' . $meta_key;
  65          }
  66  
  67          // Update the meta
  68          update_post_meta( $reply_id, $meta_key, $meta_value );
  69      }
  70  
  71      // Update the reply and hierarchy
  72      bbp_update_reply( $reply_id, $reply_meta['topic_id'], $reply_meta['forum_id'], array(), $reply_data['post_author'], false, $reply_meta['reply_to'] );
  73  
  74      /**
  75       * Fires after reply has been inserted via `bbp_insert_reply`.
  76       *
  77       * @since 2.6.0 bbPress (r6036)
  78       *
  79       * @param int $reply_id               The reply id.
  80       * @param int $reply_meta['topic_id'] The reply topic meta.
  81       * @param int $reply_meta['forum_id'] The reply forum meta.
  82       */
  83      do_action( 'bbp_insert_reply', (int) $reply_id, (int) $reply_meta['topic_id'], (int) $reply_meta['forum_id'] );
  84  
  85      // Return reply_id
  86      return $reply_id;
  87  }
  88  
  89  /**
  90   * Update counts after a reply is inserted via `bbp_insert_reply`.
  91   *
  92   * @since 2.6.0 bbPress (r6036)
  93   *
  94   * @param int $reply_id The reply id.
  95   * @param int $topic_id The topic id.
  96   * @param int $forum_id The forum id.
  97   *
  98   * @return void
  99   */
 100  function bbp_insert_reply_update_counts( $reply_id = 0, $topic_id = 0, $forum_id = 0 ) {
 101  
 102      // If the reply is public, update the reply counts.
 103      if ( bbp_is_reply_published( $reply_id ) ) {
 104          bbp_increase_topic_reply_count( $topic_id );
 105          bbp_increase_forum_reply_count( $forum_id );
 106  
 107      // If the reply isn't public only update the reply hidden counts.
 108      } else {
 109          bbp_increase_topic_reply_count_hidden( $topic_id );
 110          bbp_increase_forum_reply_count_hidden( $forum_id );
 111      }
 112  }
 113  
 114  /** Post Form Handlers ********************************************************/
 115  
 116  /**
 117   * Handles the front end reply submission
 118   *
 119   * @since 2.0.0 bbPress (r2574)
 120   *
 121   * @param string $action The requested action to compare this function to
 122   *                    id, anonymous data, reply author, edit (false), and
 123   *                    the reply to id
 124   */
 125  function bbp_new_reply_handler( $action = '' ) {
 126  
 127      // Bail if action is not bbp-new-reply
 128      if ( 'bbp-new-reply' !== $action ) {
 129          return;
 130      }
 131  
 132      // Nonce check
 133      if ( ! bbp_verify_nonce_request( 'bbp-new-reply' ) ) {
 134          bbp_add_error( 'bbp_new_reply_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
 135          return;
 136      }
 137  
 138      // Define local variable(s)
 139      $topic_id = $forum_id = $reply_author = $reply_to = 0;
 140      $reply_title = $reply_content = $terms = '';
 141      $anonymous_data = array();
 142  
 143      /** Reply Author **********************************************************/
 144  
 145      // User is anonymous
 146      if ( bbp_is_anonymous() ) {
 147  
 148          // Filter anonymous data (variable is used later)
 149          $anonymous_data = bbp_filter_anonymous_post_data();
 150  
 151          // Anonymous data checks out, so set cookies, etc...
 152          bbp_set_current_anonymous_user_data( $anonymous_data );
 153  
 154      // User is logged in
 155      } else {
 156  
 157          // User cannot create replies
 158          if ( ! current_user_can( 'publish_replies' ) ) {
 159              bbp_add_error( 'bbp_reply_permission', __( '<strong>ERROR</strong>: You do not have permission to reply.', 'bbpress' ) );
 160          }
 161  
 162          // Reply author is current user
 163          $reply_author = bbp_get_current_user_id();
 164      }
 165  
 166      /** Topic ID **************************************************************/
 167  
 168      // Topic id was not passed
 169      if ( empty( $_POST['bbp_topic_id'] ) ) {
 170          bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic ID is missing.', 'bbpress' ) );
 171  
 172      // Topic id is not a number
 173      } elseif ( ! is_numeric( $_POST['bbp_topic_id'] ) ) {
 174          bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic ID must be a number.', 'bbpress' ) );
 175  
 176      // Topic id might be valid
 177      } else {
 178  
 179          // Get the topic id
 180          $posted_topic_id = intval( $_POST['bbp_topic_id'] );
 181  
 182          // Topic id is a negative number
 183          if ( 0 > $posted_topic_id ) {
 184              bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic ID cannot be a negative number.', 'bbpress' ) );
 185  
 186          // Topic does not exist
 187          } elseif ( ! bbp_get_topic( $posted_topic_id ) ) {
 188              bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic does not exist.', 'bbpress' ) );
 189  
 190          // Use the POST'ed topic id
 191          } else {
 192              $topic_id = $posted_topic_id;
 193          }
 194      }
 195  
 196      /** Forum ID **************************************************************/
 197  
 198      // Try to use the forum id of the topic
 199      if ( ! isset( $_POST['bbp_forum_id'] ) && ! empty( $topic_id ) ) {
 200          $forum_id = bbp_get_topic_forum_id( $topic_id );
 201  
 202      // Error check the POST'ed forum id
 203      } elseif ( isset( $_POST['bbp_forum_id'] ) ) {
 204  
 205          // Empty Forum id was passed
 206          if ( empty( $_POST['bbp_forum_id'] ) ) {
 207              bbp_add_error( 'bbp_reply_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
 208  
 209          // Forum id is not a number
 210          } elseif ( ! is_numeric( $_POST['bbp_forum_id'] ) ) {
 211              bbp_add_error( 'bbp_reply_forum_id', __( '<strong>ERROR</strong>: Forum ID must be a number.', 'bbpress' ) );
 212  
 213          // Forum id might be valid
 214          } else {
 215  
 216              // Get the forum id
 217              $posted_forum_id = intval( $_POST['bbp_forum_id'] );
 218  
 219              // Forum id is empty
 220              if ( 0 === $posted_forum_id ) {
 221                  bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
 222  
 223              // Forum id is a negative number
 224              } elseif ( 0 > $posted_forum_id ) {
 225                  bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID cannot be a negative number.', 'bbpress' ) );
 226  
 227              // Forum does not exist
 228              } elseif ( ! bbp_get_forum( $posted_forum_id ) ) {
 229                  bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum does not exist.', 'bbpress' ) );
 230  
 231              // Use the POST'ed forum id
 232              } else {
 233                  $forum_id = $posted_forum_id;
 234              }
 235          }
 236      }
 237  
 238      // Forum exists
 239      if ( ! empty( $forum_id ) ) {
 240  
 241          // Forum is a category
 242          if ( bbp_is_forum_category( $forum_id ) ) {
 243              bbp_add_error( 'bbp_new_reply_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No replies can be created in this forum.', 'bbpress' ) );
 244  
 245          // Forum is not a category
 246          } else {
 247  
 248              // Forum is closed and user cannot access
 249              if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
 250                  bbp_add_error( 'bbp_new_reply_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new replies.', 'bbpress' ) );
 251              }
 252  
 253              // Forum is private and user cannot access
 254              if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
 255                  bbp_add_error( 'bbp_new_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
 256  
 257              // Forum is hidden and user cannot access
 258              } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
 259                  bbp_add_error( 'bbp_new_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
 260              }
 261          }
 262      }
 263  
 264      /** Unfiltered HTML *******************************************************/
 265  
 266      // Remove kses filters from title and content for capable users and if the nonce is verified
 267      if ( current_user_can( 'unfiltered_html' ) && ! empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $topic_id ) === $_POST['_bbp_unfiltered_html_reply'] ) {
 268          remove_filter( 'bbp_new_reply_pre_title',   'wp_filter_kses'      );
 269          remove_filter( 'bbp_new_reply_pre_content', 'bbp_encode_bad',  10 );
 270          remove_filter( 'bbp_new_reply_pre_content', 'bbp_filter_kses', 30 );
 271      }
 272  
 273      /** Reply Title ***********************************************************/
 274  
 275      if ( ! empty( $_POST['bbp_reply_title'] ) ) {
 276          $reply_title = sanitize_text_field( $_POST['bbp_reply_title'] );
 277      }
 278  
 279      // Filter and sanitize
 280      $reply_title = apply_filters( 'bbp_new_reply_pre_title', $reply_title );
 281  
 282      // Title too long
 283      if ( bbp_is_title_too_long( $reply_title ) ) {
 284          bbp_add_error( 'bbp_reply_title', __( '<strong>ERROR</strong>: Your title is too long.', 'bbpress' ) );
 285      }
 286  
 287      /** Reply Content *********************************************************/
 288  
 289      if ( ! empty( $_POST['bbp_reply_content'] ) ) {
 290          $reply_content = $_POST['bbp_reply_content'];
 291      }
 292  
 293      // Filter and sanitize
 294      $reply_content = apply_filters( 'bbp_new_reply_pre_content', $reply_content );
 295  
 296      // No reply content
 297      if ( empty( $reply_content ) ) {
 298          bbp_add_error( 'bbp_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress' ) );
 299      }
 300  
 301      /** Reply Flooding ********************************************************/
 302  
 303      if ( ! bbp_check_for_flood( $anonymous_data, $reply_author ) ) {
 304          bbp_add_error( 'bbp_reply_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
 305      }
 306  
 307      /** Reply Duplicate *******************************************************/
 308  
 309      if ( ! bbp_check_for_duplicate( array( 'post_type' => bbp_get_reply_post_type(), 'post_author' => $reply_author, 'post_content' => $reply_content, 'post_parent' => $topic_id, 'anonymous_data' => $anonymous_data ) ) ) {
 310          bbp_add_error( 'bbp_reply_duplicate', __( '<strong>ERROR</strong>: Duplicate reply detected; it looks as though you&#8217;ve already said that.', 'bbpress' ) );
 311      }
 312  
 313      /** Reply Bad Words *******************************************************/
 314  
 315      if ( ! bbp_check_for_moderation( $anonymous_data, $reply_author, $reply_title, $reply_content, true ) ) {
 316          bbp_add_error( 'bbp_reply_moderation', __( '<strong>ERROR</strong>: Your reply cannot be created at this time.', 'bbpress' ) );
 317      }
 318  
 319      /** Reply Status **********************************************************/
 320  
 321      // Maybe put into moderation
 322      if ( bbp_is_topic_pending( $topic_id ) || ! bbp_check_for_moderation( $anonymous_data, $reply_author, $reply_title, $reply_content ) ) {
 323          $reply_status = bbp_get_pending_status_id();
 324  
 325      // Default
 326      } else {
 327          $reply_status = bbp_get_public_status_id();
 328      }
 329  
 330      /** Reply To **************************************************************/
 331  
 332      // Handle Reply To of the reply; $_REQUEST for non-JS submissions
 333      if ( isset( $_REQUEST['bbp_reply_to'] ) ) {
 334          $reply_to = bbp_validate_reply_to( $_REQUEST['bbp_reply_to'] );
 335      }
 336  
 337      /** Topic Closed **********************************************************/
 338  
 339      // If topic is closed, moderators can still reply
 340      if ( bbp_is_topic_closed( $topic_id ) && ! current_user_can( 'moderate', $topic_id ) ) {
 341          bbp_add_error( 'bbp_reply_topic_closed', __( '<strong>ERROR</strong>: Topic is closed.', 'bbpress' ) );
 342      }
 343  
 344      /** Topic Tags ************************************************************/
 345  
 346      // Either replace terms
 347      if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags', $topic_id ) && ! empty( $_POST['bbp_topic_tags'] ) ) {
 348          $terms = sanitize_text_field( $_POST['bbp_topic_tags'] );
 349  
 350      // ...or remove them.
 351      } elseif ( isset( $_POST['bbp_topic_tags'] ) ) {
 352          $terms = '';
 353  
 354      // Existing terms
 355      } else {
 356          $terms = bbp_get_topic_tag_names( $topic_id );
 357      }
 358  
 359      /** Additional Actions (Before Save) **************************************/
 360  
 361      do_action( 'bbp_new_reply_pre_extras', $topic_id, $forum_id );
 362  
 363      // Bail if errors
 364      if ( bbp_has_errors() ) {
 365          return;
 366      }
 367  
 368      /** No Errors *************************************************************/
 369  
 370      // Add the content of the form to $reply_data as an array
 371      // Just in time manipulation of reply data before being created
 372      $reply_data = apply_filters( 'bbp_new_reply_pre_insert', array(
 373          'post_author'    => $reply_author,
 374          'post_title'     => $reply_title,
 375          'post_content'   => $reply_content,
 376          'post_status'    => $reply_status,
 377          'post_parent'    => $topic_id,
 378          'post_type'      => bbp_get_reply_post_type(),
 379          'comment_status' => 'closed',
 380          'menu_order'     => bbp_get_topic_reply_count( $topic_id, true ) + 1
 381      ) );
 382  
 383      // Insert reply
 384      $reply_id = wp_insert_post( $reply_data, true );
 385  
 386      /** No Errors *************************************************************/
 387  
 388      // Check for missing reply_id or error
 389      if ( ! empty( $reply_id ) && ! is_wp_error( $reply_id ) ) {
 390  
 391          /** Topic Tags ********************************************************/
 392  
 393          // Just in time manipulation of reply terms before being edited
 394          $terms = apply_filters( 'bbp_new_reply_pre_set_terms', $terms, $topic_id, $reply_id );
 395  
 396          // Insert terms
 397          $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false );
 398  
 399          // Term error
 400          if ( is_wp_error( $terms ) ) {
 401              bbp_add_error( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was a problem adding the tags to the topic.', 'bbpress' ) );
 402          }
 403  
 404          /** Trash Check *******************************************************/
 405  
 406          // If this reply starts as trash, add it to pre_trashed_replies
 407          // for the topic, so it is properly restored.
 408          if ( bbp_is_topic_trash( $topic_id ) || ( $reply_data['post_status'] === bbp_get_trash_status_id() ) ) {
 409  
 410              // Trash the reply
 411              wp_trash_post( $reply_id );
 412  
 413              // Only add to pre-trashed array if topic is trashed
 414              if ( bbp_is_topic_trash( $topic_id ) ) {
 415  
 416                  // Get pre_trashed_replies for topic
 417                  $pre_trashed_replies = (array) get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
 418  
 419                  // Add this reply to the end of the existing replies
 420                  $pre_trashed_replies[] = $reply_id;
 421  
 422                  // Update the pre_trashed_reply post meta
 423                  update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
 424              }
 425  
 426          /** Spam Check ********************************************************/
 427  
 428          // If reply or topic are spam, officially spam this reply
 429          } elseif ( bbp_is_topic_spam( $topic_id ) || ( $reply_data['post_status'] === bbp_get_spam_status_id() ) ) {
 430              add_post_meta( $reply_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
 431  
 432              // Only add to pre-spammed array if topic is spam
 433              if ( bbp_is_topic_spam( $topic_id ) ) {
 434  
 435                  // Get pre_spammed_replies for topic
 436                  $pre_spammed_replies = (array) get_post_meta( $topic_id, '_bbp_pre_spammed_replies', true );
 437  
 438                  // Add this reply to the end of the existing replies
 439                  $pre_spammed_replies[] = $reply_id;
 440  
 441                  // Update the pre_spammed_replies post meta
 442                  update_post_meta( $topic_id, '_bbp_pre_spammed_replies', $pre_spammed_replies );
 443              }
 444          }
 445  
 446          /** Update counts, etc... *********************************************/
 447  
 448          do_action( 'bbp_new_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author, false, $reply_to );
 449  
 450          /** Additional Actions (After Save) ***********************************/
 451  
 452          do_action( 'bbp_new_reply_post_extras', $reply_id );
 453  
 454          /** Redirect **********************************************************/
 455  
 456          // Redirect to
 457          $redirect_to = bbp_get_redirect_to();
 458  
 459          // Get the reply URL
 460          $reply_url = bbp_get_reply_url( $reply_id, $redirect_to );
 461  
 462          // Allow to be filtered
 463          $reply_url = apply_filters( 'bbp_new_reply_redirect_to', $reply_url, $redirect_to, $reply_id );
 464  
 465          /** Successful Save ***************************************************/
 466  
 467          // Redirect back to new reply
 468          bbp_redirect( $reply_url );
 469  
 470      /** Errors ****************************************************************/
 471  
 472      // WP_Error
 473      } elseif ( is_wp_error( $reply_id ) ) {
 474          bbp_add_error( 'bbp_reply_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) occurred: %s', 'bbpress' ), $reply_id->get_error_message() ) );
 475  
 476      // Generic error
 477      } else {
 478          bbp_add_error( 'bbp_reply_error', __( '<strong>ERROR</strong>: The reply was not created.', 'bbpress' ) );
 479      }
 480  }
 481  
 482  /**
 483   * Handles the front end edit reply submission
 484   *
 485   * @param string $action The requested action to compare this function to
 486   *                    id, anonymous data, reply author, bool true (for edit),
 487   *                    and the reply to id
 488   */
 489  function bbp_edit_reply_handler( $action = '' ) {
 490  
 491      // Bail if action is not bbp-edit-reply
 492      if ( 'bbp-edit-reply' !== $action ) {
 493          return;
 494      }
 495  
 496      // Define local variable(s)
 497      $revisions_removed = false;
 498      $reply = $reply_id = $reply_to = $reply_author = $topic_id = $forum_id = 0;
 499      $reply_title = $reply_content = $reply_edit_reason = $terms = '';
 500      $anonymous_data = array();
 501  
 502      /** Reply *****************************************************************/
 503  
 504      // Reply id was not passed
 505      if ( empty( $_POST['bbp_reply_id'] ) ) {
 506          bbp_add_error( 'bbp_edit_reply_id', __( '<strong>ERROR</strong>: Reply ID not found.', 'bbpress' ) );
 507          return;
 508  
 509      // Reply id was passed
 510      } elseif ( is_numeric( $_POST['bbp_reply_id'] ) ) {
 511          $reply_id = (int) $_POST['bbp_reply_id'];
 512          $reply    = bbp_get_reply( $reply_id );
 513      }
 514  
 515      // Nonce check
 516      if ( ! bbp_verify_nonce_request( 'bbp-edit-reply_' . $reply_id ) ) {
 517          bbp_add_error( 'bbp_edit_reply_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
 518          return;
 519      }
 520  
 521      // Reply does not exist
 522      if ( empty( $reply ) ) {
 523          bbp_add_error( 'bbp_edit_reply_not_found', __( '<strong>ERROR</strong>: The reply you want to edit was not found.', 'bbpress' ) );
 524          return;
 525  
 526      // Reply exists
 527      } else {
 528  
 529          // Check users ability to create new reply
 530          if ( ! bbp_is_reply_anonymous( $reply_id ) ) {
 531  
 532              // User cannot edit this reply
 533              if ( ! current_user_can( 'edit_reply', $reply_id ) ) {
 534                  bbp_add_error( 'bbp_edit_reply_permission', __( '<strong>ERROR</strong>: You do not have permission to edit that reply.', 'bbpress' ) );
 535                  return;
 536              }
 537  
 538              // Set reply author
 539              $reply_author = bbp_get_reply_author_id( $reply_id );
 540  
 541          // It is an anonymous post
 542          } else {
 543  
 544              // Filter anonymous data
 545              $anonymous_data = bbp_filter_anonymous_post_data();
 546          }
 547      }
 548  
 549      // Remove kses filters from title and content for capable users and if the nonce is verified
 550      if ( current_user_can( 'unfiltered_html' ) && ! empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $reply_id ) === $_POST['_bbp_unfiltered_html_reply'] ) {
 551          remove_filter( 'bbp_edit_reply_pre_title',   'wp_filter_kses'      );
 552          remove_filter( 'bbp_edit_reply_pre_content', 'bbp_encode_bad',  10 );
 553          remove_filter( 'bbp_edit_reply_pre_content', 'bbp_filter_kses', 30 );
 554      }
 555  
 556      /** Reply Topic ***********************************************************/
 557  
 558      $topic_id = bbp_get_reply_topic_id( $reply_id );
 559  
 560      /** Topic Forum ***********************************************************/
 561  
 562      $forum_id = bbp_get_topic_forum_id( $topic_id );
 563  
 564      // Forum exists
 565      if ( ! empty( $forum_id ) && ( $forum_id !== bbp_get_reply_forum_id( $reply_id ) ) ) {
 566  
 567          // Forum is a category
 568          if ( bbp_is_forum_category( $forum_id ) ) {
 569              bbp_add_error( 'bbp_edit_reply_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No replies can be created in this forum.', 'bbpress' ) );
 570  
 571          // Forum is not a category
 572          } else {
 573  
 574              // Forum is closed and user cannot access
 575              if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
 576                  bbp_add_error( 'bbp_edit_reply_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new replies.', 'bbpress' ) );
 577              }
 578  
 579              // Forum is private and user cannot access
 580              if ( bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
 581                  bbp_add_error( 'bbp_edit_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
 582  
 583              // Forum is hidden and user cannot access
 584              } elseif ( bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
 585                  bbp_add_error( 'bbp_edit_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
 586              }
 587          }
 588      }
 589  
 590      /** Reply Title ***********************************************************/
 591  
 592      if ( ! empty( $_POST['bbp_reply_title'] ) ) {
 593          $reply_title = sanitize_text_field( $_POST['bbp_reply_title'] );
 594      }
 595  
 596      // Filter and sanitize
 597      $reply_title = apply_filters( 'bbp_edit_reply_pre_title', $reply_title, $reply_id );
 598  
 599      // Title too long
 600      if ( bbp_is_title_too_long( $reply_title ) ) {
 601          bbp_add_error( 'bbp_reply_title', __( '<strong>ERROR</strong>: Your title is too long.', 'bbpress' ) );
 602      }
 603  
 604      /** Reply Content *********************************************************/
 605  
 606      if ( ! empty( $_POST['bbp_reply_content'] ) ) {
 607          $reply_content = $_POST['bbp_reply_content'];
 608      }
 609  
 610      // Filter and sanitize
 611      $reply_content = apply_filters( 'bbp_edit_reply_pre_content', $reply_content, $reply_id );
 612  
 613      // No reply content
 614      if ( empty( $reply_content ) ) {
 615          bbp_add_error( 'bbp_edit_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress' ) );
 616      }
 617  
 618      /** Reply Bad Words *******************************************************/
 619  
 620      if ( ! bbp_check_for_moderation( $anonymous_data, $reply_author, $reply_title, $reply_content, true ) ) {
 621          bbp_add_error( 'bbp_reply_moderation', __( '<strong>ERROR</strong>: Your reply cannot be edited at this time.', 'bbpress' ) );
 622      }
 623  
 624      /** Reply Status **********************************************************/
 625  
 626      // Maybe put into moderation
 627      if ( ! bbp_check_for_moderation( $anonymous_data, $reply_author, $reply_title, $reply_content ) ) {
 628  
 629          // Set post status to pending if public
 630          if ( bbp_get_public_status_id() === $reply->post_status ) {
 631              $reply_status = bbp_get_pending_status_id();
 632          }
 633  
 634      // Use existing post_status
 635      } else {
 636          $reply_status = $reply->post_status;
 637      }
 638  
 639      /** Reply To **************************************************************/
 640  
 641      // Handle Reply To of the reply; $_REQUEST for non-JS submissions
 642      if ( isset( $_REQUEST['bbp_reply_to'] ) && current_user_can( 'moderate', $reply_id ) ) {
 643          $reply_to = bbp_validate_reply_to( $_REQUEST['bbp_reply_to'], $reply_id );
 644      } elseif ( bbp_thread_replies() ) {
 645          $reply_to = bbp_get_reply_to( $reply_id );
 646      }
 647  
 648      /** Topic Tags ************************************************************/
 649  
 650      // Either replace terms
 651      if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags', $topic_id ) && ! empty( $_POST['bbp_topic_tags'] ) ) {
 652          $terms = sanitize_text_field( $_POST['bbp_topic_tags'] );
 653  
 654      // ...or remove them.
 655      } elseif ( isset( $_POST['bbp_topic_tags'] ) ) {
 656          $terms = '';
 657  
 658      // Existing terms
 659      } else {
 660          $terms = bbp_get_topic_tag_names( $topic_id );
 661      }
 662  
 663      /** Additional Actions (Before Save) **************************************/
 664  
 665      do_action( 'bbp_edit_reply_pre_extras', $reply_id );
 666  
 667      // Bail if errors
 668      if ( bbp_has_errors() ) {
 669          return;
 670      }
 671  
 672      /** No Errors *************************************************************/
 673  
 674      // Add the content of the form to $reply_data as an array
 675      // Just in time manipulation of reply data before being edited
 676      $reply_data = apply_filters( 'bbp_edit_reply_pre_insert', array(
 677          'ID'           => $reply_id,
 678          'post_title'   => $reply_title,
 679          'post_content' => $reply_content,
 680          'post_status'  => $reply_status,
 681          'post_parent'  => $topic_id,
 682          'post_author'  => $reply_author,
 683          'post_type'    => bbp_get_reply_post_type()
 684      ) );
 685  
 686      // Toggle revisions to avoid duplicates
 687      if ( post_type_supports( bbp_get_reply_post_type(), 'revisions' ) ) {
 688          $revisions_removed = true;
 689          remove_post_type_support( bbp_get_reply_post_type(), 'revisions' );
 690      }
 691  
 692      // Insert reply
 693      $reply_id = wp_update_post( $reply_data );
 694  
 695      // Toggle revisions back on
 696      if ( true === $revisions_removed ) {
 697          $revisions_removed = false;
 698          add_post_type_support( bbp_get_reply_post_type(), 'revisions' );
 699      }
 700  
 701      /** Topic Tags ************************************************************/
 702  
 703      // Just in time manipulation of reply terms before being edited
 704      $terms = apply_filters( 'bbp_edit_reply_pre_set_terms', $terms, $topic_id, $reply_id );
 705  
 706      // Insert terms
 707      $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false );
 708  
 709      // Term error
 710      if ( is_wp_error( $terms ) ) {
 711          bbp_add_error( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was a problem adding the tags to the topic.', 'bbpress' ) );
 712      }
 713  
 714      /** No Errors *************************************************************/
 715  
 716      if ( ! empty( $reply_id ) && ! is_wp_error( $reply_id ) ) {
 717  
 718          // Update counts, etc...
 719          do_action( 'bbp_edit_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author , true, $reply_to );
 720  
 721          /** Revisions *********************************************************/
 722  
 723          // Update locks
 724          update_post_meta( $reply_id, '_edit_last', bbp_get_current_user_id() );
 725          delete_post_meta( $reply_id, '_edit_lock' );
 726  
 727          // Revision Reason
 728          if ( ! empty( $_POST['bbp_reply_edit_reason'] ) ) {
 729              $reply_edit_reason = sanitize_text_field( $_POST['bbp_reply_edit_reason'] );
 730          }
 731  
 732          // Update revision log
 733          if ( ! empty( $_POST['bbp_log_reply_edit'] ) && ( "1" === $_POST['bbp_log_reply_edit'] ) ) {
 734              $revision_id = wp_save_post_revision( $reply_id );
 735              if ( ! empty( $revision_id ) ) {
 736                  bbp_update_reply_revision_log( array(
 737                      'reply_id'    => $reply_id,
 738                      'revision_id' => $revision_id,
 739                      'author_id'   => bbp_get_current_user_id(),
 740                      'reason'      => $reply_edit_reason
 741                  ) );
 742              }
 743          }
 744  
 745          /** Additional Actions (After Save) ***********************************/
 746  
 747          do_action( 'bbp_edit_reply_post_extras', $reply_id );
 748  
 749          /** Redirect **********************************************************/
 750  
 751          // Redirect to
 752          $redirect_to = bbp_get_redirect_to();
 753  
 754          // Get the reply URL
 755          $reply_url = bbp_get_reply_url( $reply_id, $redirect_to );
 756  
 757          // Allow to be filtered
 758          $reply_url = apply_filters( 'bbp_edit_reply_redirect_to', $reply_url, $redirect_to );
 759  
 760          /** Successful Edit ***************************************************/
 761  
 762          // Redirect back to new reply
 763          bbp_redirect( $reply_url );
 764  
 765      /** Errors ****************************************************************/
 766  
 767      } else {
 768          $append_error = ( is_wp_error( $reply_id ) && $reply_id->get_error_message() ) ? $reply_id->get_error_message() . ' ' : '';
 769          bbp_add_error( 'bbp_reply_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', 'bbpress' ) );
 770      }
 771  }
 772  
 773  /**
 774   * Handle all the extra meta stuff from posting a new reply or editing a reply
 775   *
 776   * @param int $reply_id Optional. Reply id
 777   * @param int $topic_id Optional. Topic id
 778   * @param int $forum_id Optional. Forum id
 779   * @param array $anonymous_data Optional - if it's an anonymous post. Do not
 780   *                              supply if supplying $author_id. Should be
 781   *                              sanitized (see {@link bbp_filter_anonymous_post_data()}
 782   * @param int $author_id Author id
 783   * @param bool $is_edit Optional. Is the post being edited? Defaults to false.
 784   * @param int $reply_to Optional. Reply to id
 785   */
 786  function bbp_update_reply( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $author_id = 0, $is_edit = false, $reply_to = 0 ) {
 787  
 788      // Validate the ID's passed from 'bbp_new_reply' action
 789      $reply_id = bbp_get_reply_id( $reply_id );
 790      $topic_id = bbp_get_topic_id( $topic_id );
 791      $forum_id = bbp_get_forum_id( $forum_id );
 792      $reply_to = bbp_validate_reply_to( $reply_to, $reply_id );
 793  
 794      // Bail if there is no reply
 795      if ( empty( $reply_id ) ) {
 796          return;
 797      }
 798  
 799      // Check author_id
 800      if ( empty( $author_id ) ) {
 801          $author_id = bbp_get_current_user_id();
 802      }
 803  
 804      // Check topic_id
 805      if ( empty( $topic_id ) ) {
 806          $topic_id = bbp_get_reply_topic_id( $reply_id );
 807      }
 808  
 809      // Check forum_id
 810      if ( ! empty( $topic_id ) && empty( $forum_id ) ) {
 811          $forum_id = bbp_get_topic_forum_id( $topic_id );
 812      }
 813  
 814      // If anonymous post, store name, email, website and ip in post_meta.
 815      if ( ! empty( $anonymous_data ) ) {
 816  
 817          // Update anonymous meta data (not cookies)
 818          bbp_update_anonymous_post_author( $reply_id, $anonymous_data, bbp_get_reply_post_type() );
 819  
 820          // Set transient for throttle check (only on new, not edit)
 821          if ( empty( $is_edit ) ) {
 822              set_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted', time() );
 823          }
 824  
 825      } else {
 826          if ( empty( $is_edit ) && ! current_user_can( 'throttle' ) ) {
 827              bbp_update_user_last_posted( $author_id );
 828          }
 829      }
 830  
 831      // Handle Subscription Checkbox
 832      if ( bbp_is_subscriptions_active() && ! empty( $author_id ) && ! empty( $topic_id ) ) {
 833  
 834          // Check if subscribed
 835          $subscribed = bbp_is_user_subscribed( $author_id, $topic_id );
 836  
 837          // Check for action
 838          $subscheck  = ( ! empty( $_POST['bbp_topic_subscription'] ) && ( 'bbp_subscribe' === $_POST['bbp_topic_subscription'] ) )
 839              ? true
 840              : false;
 841  
 842          // Subscribed and unsubscribing
 843          if ( ( true === $subscribed ) && ( false === $subscheck ) ) {
 844              bbp_remove_user_subscription( $author_id, $topic_id );
 845  
 846          // Not subscribed and subscribing
 847          } elseif ( ( false === $subscribed ) && ( true === $subscheck ) ) {
 848              bbp_add_user_subscription( $author_id, $topic_id );
 849          }
 850      }
 851  
 852      // Reply meta relating to reply position in tree
 853      bbp_update_reply_forum_id( $reply_id, $forum_id );
 854      bbp_update_reply_topic_id( $reply_id, $topic_id );
 855      bbp_update_reply_to      ( $reply_id, $reply_to );
 856  
 857      // Update associated topic values if this is a new reply
 858      if ( empty( $is_edit ) ) {
 859  
 860          // Update poster IP if not editing
 861          update_post_meta( $reply_id, '_bbp_author_ip', bbp_current_author_ip(), false );
 862  
 863          // Last active time
 864          $last_active_time = get_post_field( 'post_date', $reply_id );
 865  
 866          // Walk up ancestors and do the dirty work
 867          bbp_update_reply_walker( $reply_id, $last_active_time, $forum_id, $topic_id, false );
 868      }
 869  
 870      // Bump the custom query cache
 871      wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );
 872  }
 873  
 874  /**
 875   * Walk up the ancestor tree from the current reply, and update all the counts
 876   *
 877   * @since 2.0.0 bbPress (r2884)
 878   *
 879   * @param int $reply_id Optional. Reply id
 880   * @param string $last_active_time Optional. Last active time
 881   * @param int $forum_id Optional. Forum id
 882   * @param int $topic_id Optional. Topic id
 883   * @param bool $refresh If set to true, unsets all the previous parameters.
 884   *                       Defaults to true
 885   */
 886  function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 0, $topic_id = 0, $refresh = true ) {
 887  
 888      // Verify the reply ID
 889      $reply_id = bbp_get_reply_id( $reply_id );
 890  
 891      // Reply was passed
 892      if ( ! empty( $reply_id ) ) {
 893  
 894          // Get the topic ID if none was passed
 895          if ( empty( $topic_id ) ) {
 896              $topic_id = bbp_get_reply_topic_id( $reply_id );
 897          }
 898  
 899          // Get the forum ID if none was passed
 900          if ( empty( $forum_id ) ) {
 901              $forum_id = bbp_get_reply_forum_id( $reply_id );
 902          }
 903      }
 904  
 905      // Set the active_id based on topic_id/reply_id
 906      $active_id = empty( $reply_id ) ? $topic_id : $reply_id;
 907  
 908      // Setup ancestors array to walk up
 909      $ancestors = array_values( array_unique( array_merge( array( $topic_id, $forum_id ), (array) get_post_ancestors( $topic_id ) ) ) );
 910  
 911      // If we want a full refresh, unset any of the possibly passed variables
 912      if ( true === $refresh ) {
 913          $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
 914      }
 915  
 916      // Walk up ancestors
 917      if ( ! empty( $ancestors ) ) {
 918          foreach ( $ancestors as $ancestor ) {
 919  
 920              // Reply meta relating to most recent reply
 921              if ( bbp_is_reply( $ancestor ) ) {
 922                  // @todo - hierarchical replies
 923  
 924              // Topic meta relating to most recent reply
 925              } elseif ( bbp_is_topic( $ancestor ) ) {
 926  
 927                  // Only update if reply is published
 928                  if ( ! bbp_is_reply_pending( $reply_id ) ) {
 929  
 930                      // Last reply and active ID's
 931                      bbp_update_topic_last_reply_id ( $ancestor, $reply_id  );
 932                      bbp_update_topic_last_active_id( $ancestor, $active_id );
 933  
 934                      // Get the last active time if none was passed
 935                      $topic_last_active_time = $last_active_time;
 936                      if ( empty( $last_active_time ) ) {
 937                          $topic_last_active_time = get_post_field( 'post_date', bbp_get_topic_last_active_id( $ancestor ) );
 938                      }
 939  
 940                      bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time );
 941                  }
 942  
 943                  // Only update reply count if we've deleted a reply
 944                  if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ), true ) ) {
 945                      bbp_update_topic_reply_count(        $ancestor );
 946                      bbp_update_topic_reply_count_hidden( $ancestor );
 947                      bbp_update_topic_voice_count(        $ancestor );
 948                  }
 949  
 950              // Forum meta relating to most recent topic
 951              } elseif ( bbp_is_forum( $ancestor ) ) {
 952  
 953                  // Only update if reply is published
 954                  if ( bbp_is_reply_pending( $reply_id ) && ! bbp_is_topic_pending( $topic_id ) ) {
 955  
 956                      // Last topic and reply ID's
 957                      bbp_update_forum_last_topic_id( $ancestor, $topic_id );
 958                      bbp_update_forum_last_reply_id( $ancestor, $reply_id );
 959  
 960                      // Last Active
 961                      bbp_update_forum_last_active_id( $ancestor, $active_id );
 962  
 963                      // Get the last active time if none was passed
 964                      $forum_last_active_time = $last_active_time;
 965                      if ( empty( $last_active_time ) ) {
 966                          $forum_last_active_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $ancestor ) );
 967                      }
 968  
 969                      bbp_update_forum_last_active_time( $ancestor, $forum_last_active_time );
 970                  }
 971  
 972                  // Only update reply count if we've deleted a reply
 973                  if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ), true ) ) {
 974                      bbp_update_forum_reply_count( $ancestor );
 975                  }
 976              }
 977          }
 978      }
 979  }
 980  
 981  /** Reply Updaters ************************************************************/
 982  
 983  /**
 984   * Update the reply with its forum id it is in
 985   *
 986   * @since 2.0.0 bbPress (r2855)
 987   *
 988   * @param int $reply_id Optional. Reply id to update
 989   * @param int $forum_id Optional. Forum id
 990   * @return bool The forum id of the reply
 991   */
 992  function bbp_update_reply_forum_id( $reply_id = 0, $forum_id = 0 ) {
 993  
 994      // Validation
 995      $reply_id = bbp_get_reply_id( $reply_id );
 996      $forum_id = bbp_get_forum_id( $forum_id );
 997  
 998      // If no forum_id was passed, walk up ancestors and look for forum type
 999      if ( empty( $forum_id ) ) {
1000  
1001          // Get ancestors
1002          $ancestors = get_post_ancestors( $reply_id );
1003  
1004          // Loop through ancestors
1005          if ( ! empty( $ancestors ) ) {
1006              foreach ( $ancestors as $ancestor ) {
1007  
1008                  // Get first parent that is a forum
1009                  if ( get_post_field( 'post_type', $ancestor ) === bbp_get_forum_post_type() ) {
1010                      $forum_id = $ancestor;
1011  
1012                      // Found a forum, so exit the loop and continue
1013                      continue;
1014                  }
1015              }
1016          }
1017      }
1018  
1019      // Update the forum ID
1020      $retval = bbp_update_forum_id( $reply_id, $forum_id );
1021  
1022      // Filter & return
1023      return (int) apply_filters( 'bbp_update_reply_forum_id', $retval, $reply_id, $forum_id );
1024  }
1025  
1026  /**
1027   * Update the reply with its topic id it is in
1028   *
1029   * @since 2.0.0 bbPress (r2855)
1030   *
1031   * @param int $reply_id Optional. Reply id to update
1032   * @param int $topic_id Optional. Topic id
1033   * @return bool The topic id of the reply
1034   */
1035  function bbp_update_reply_topic_id( $reply_id = 0, $topic_id = 0 ) {
1036  
1037      // Validation
1038      $reply_id = bbp_get_reply_id( $reply_id );
1039      $topic_id = bbp_get_topic_id( $topic_id );
1040  
1041      // If no topic_id was passed, walk up ancestors and look for topic type
1042      if ( empty( $topic_id ) ) {
1043  
1044          // Get ancestors
1045          $ancestors = (array) get_post_ancestors( $reply_id );
1046  
1047          // Loop through ancestors
1048          if ( ! empty( $ancestors ) ) {
1049              foreach ( $ancestors as $ancestor ) {
1050  
1051                  // Get first parent that is a topic
1052                  if ( get_post_field( 'post_type', $ancestor ) === bbp_get_topic_post_type() ) {
1053                      $topic_id = $ancestor;
1054  
1055                      // Found a topic, so exit the loop and continue
1056                      continue;
1057                  }
1058              }
1059          }
1060      }
1061  
1062      // Update the topic ID
1063      $retval = bbp_update_topic_id( $reply_id, $topic_id );
1064  
1065      // Filter & return
1066      return (int) apply_filters( 'bbp_update_reply_topic_id', $retval, $reply_id, $topic_id );
1067  }
1068  
1069  /*
1070   * Update the meta data with its parent reply-to id, of a reply
1071   *
1072   * @since 2.4.0 bbPress (r4944)
1073   *
1074   * @param int $reply_id Reply id to update
1075   * @param int $reply_to Optional. Reply to id
1076   * @return bool The parent reply id of the reply
1077   */
1078  function bbp_update_reply_to( $reply_id = 0, $reply_to = 0 ) {
1079  
1080      // Validation
1081      $reply_id = bbp_get_reply_id( $reply_id );
1082      $reply_to = bbp_validate_reply_to( $reply_to, $reply_id );
1083  
1084      // Update or delete the `reply_to` postmeta
1085      if ( ! empty( $reply_id ) ) {
1086  
1087          // Update the reply to
1088          if ( ! empty( $reply_to ) ) {
1089              $reply_to = bbp_update_reply_to_id( $reply_id, $reply_to );
1090  
1091          // Delete the reply to
1092          } else {
1093              delete_post_meta( $reply_id, '_bbp_reply_to' );
1094          }
1095      }
1096  
1097      // Filter & return
1098      return (int) apply_filters( 'bbp_update_reply_to', $reply_to, $reply_id );
1099  }
1100  
1101  /**
1102   * Get all ancestors to a reply
1103   *
1104   * Because settings can be changed, this function does not care if hierarchical
1105   * replies are active or to what depth.
1106   *
1107   * @since 2.6.0 bbPress (r5390)
1108   *
1109   * @param int $reply_id
1110   * @return array
1111   */
1112  function bbp_get_reply_ancestors( $reply_id = 0 ) {
1113  
1114      // Validation
1115      $reply_id  = bbp_get_reply_id( $reply_id );
1116      $ancestors = array();
1117  
1118      // Reply id is valid
1119      if ( ! empty( $reply_id ) ) {
1120  
1121          // Try to get reply parent
1122          $reply_to = bbp_get_reply_to( $reply_id );
1123  
1124          // Reply has a hierarchical parent
1125          if ( ! empty( $reply_to ) ) {
1126  
1127              // Setup the current ID and current post as an ancestor
1128              $id        = $reply_to;
1129              $ancestors = array( $reply_to );
1130  
1131              // Get parent reply
1132              while ( $ancestor = bbp_get_reply( $id ) ) {
1133  
1134                  // Does parent have a parent?
1135                  $grampy_id = bbp_get_reply_to( $ancestor->ID );
1136  
1137                  // Loop detection: If the ancestor has been seen before, break.
1138                  if ( empty( $ancestor->post_parent ) || ( $grampy_id === $reply_id ) || in_array( $grampy_id, $ancestors, true ) ) {
1139                      break;
1140                  }
1141  
1142                  $id = $ancestors[] = $grampy_id;
1143              }
1144          }
1145      }
1146  
1147      // Filter & return
1148      return (array) apply_filters( 'bbp_get_reply_ancestors', $ancestors, $reply_id );
1149  }
1150  
1151  /**
1152   * Update the revision log of the reply
1153   *
1154   * @since 2.0.0 bbPress (r2782)
1155   *
1156   * @param array $args Supports these args:
1157   *  - reply_id: reply id
1158   *  - author_id: Author id
1159   *  - reason: Reason for editing
1160   *  - revision_id: Revision id
1161   * @return mixed False on failure, true on success
1162   */
1163  function bbp_update_reply_revision_log( $args = array() ) {
1164  
1165      // Parse arguments against default values
1166      $r = bbp_parse_args( $args, array(
1167          'reason'      => '',
1168          'reply_id'    => 0,
1169          'author_id'   => 0,
1170          'revision_id' => 0
1171      ), 'update_reply_revision_log' );
1172  
1173      // Populate the variables
1174      $r['reason']      = bbp_format_revision_reason( $r['reason'] );
1175      $r['reply_id']    = bbp_get_reply_id( $r['reply_id'] );
1176      $r['author_id']   = bbp_get_user_id ( $r['author_id'], false, true );
1177      $r['revision_id'] = (int) $r['revision_id'];
1178  
1179      // Get the logs and append the new one to those
1180      $revision_log                      = bbp_get_reply_raw_revision_log( $r['reply_id'] );
1181      $revision_log[ $r['revision_id'] ] = array( 'author' => $r['author_id'], 'reason' => $r['reason'] );
1182  
1183      // Finally, update
1184      update_post_meta( $r['reply_id'], '_bbp_revision_log', $revision_log );
1185  
1186      // Filter & return
1187      return apply_filters( 'bbp_update_reply_revision_log', $revision_log, $r['reply_id'] );
1188  }
1189  
1190  /**
1191   * Move reply handler
1192   *
1193   * Handles the front end move reply submission
1194   *
1195   * @since 2.3.0 bbPress (r4521)
1196   *
1197   * @param string $action The requested action to compare this function to
1198   */
1199  function bbp_move_reply_handler( $action = '' ) {
1200  
1201      // Bail if action is not 'bbp-move-reply'
1202      if ( 'bbp-move-reply' !== $action ) {
1203          return;
1204      }
1205  
1206      // Prevent debug notices
1207      $move_reply_id = $destination_topic_id = 0;
1208      $destination_topic_title = '';
1209      $destination_topic = $move_reply = $source_topic = '';
1210  
1211      /** Move Reply ***********************************************************/
1212  
1213      if ( empty( $_POST['bbp_reply_id'] ) ) {
1214          bbp_add_error( 'bbp_move_reply_reply_id', __( '<strong>ERROR</strong>: A reply ID is required', 'bbpress' ) );
1215      } else {
1216          $move_reply_id = (int) $_POST['bbp_reply_id'];
1217      }
1218  
1219      $move_reply = bbp_get_reply( $move_reply_id );
1220  
1221      // Reply exists
1222      if ( empty( $move_reply ) ) {
1223          bbp_add_error( 'bbp_mover_reply_r_not_found', __( '<strong>ERROR</strong>: The reply you want to move was not found.', 'bbpress' ) );
1224      }
1225  
1226      /** Topic to Move From ***************************************************/
1227  
1228      // Get the current topic a reply is in
1229      $source_topic = bbp_get_topic( $move_reply->post_parent );
1230  
1231      // No topic
1232      if ( empty( $source_topic ) ) {
1233          bbp_add_error( 'bbp_move_reply_source_not_found', __( '<strong>ERROR</strong>: The topic you want to move from was not found.', 'bbpress' ) );
1234      }
1235  
1236      // Nonce check failed
1237      if ( ! bbp_verify_nonce_request( 'bbp-move-reply_' . $move_reply->ID ) ) {
1238          bbp_add_error( 'bbp_move_reply_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
1239          return;
1240      }
1241  
1242      // Use cannot edit topic
1243      if ( ! current_user_can( 'edit_topic', $source_topic->ID ) ) {
1244          bbp_add_error( 'bbp_move_reply_source_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the source topic.', 'bbpress' ) );
1245      }
1246  
1247      // How to move
1248      if ( ! empty( $_POST['bbp_reply_move_option'] ) ) {
1249          $move_option = (string) trim( $_POST['bbp_reply_move_option'] );
1250      }
1251  
1252      // Invalid move option
1253      if ( empty( $move_option ) || ! in_array( $move_option, array( 'existing', 'topic' ), true ) ) {
1254          bbp_add_error( 'bbp_move_reply_option', __( '<strong>ERROR</strong>: You need to choose a valid move option.', 'bbpress' ) );
1255  
1256      // Valid move option
1257      } else {
1258  
1259          // What kind of move
1260          switch ( $move_option ) {
1261  
1262              // Into an existing topic
1263              case 'existing' :
1264  
1265                  // Get destination topic id
1266                  if ( empty( $_POST['bbp_destination_topic'] ) ) {
1267                      bbp_add_error( 'bbp_move_reply_destination_id', __( '<strong>ERROR</strong>: A topic ID is required.', 'bbpress' ) );
1268                  } else {
1269                      $destination_topic_id = (int) $_POST['bbp_destination_topic'];
1270                  }
1271  
1272                  // Get the destination topic
1273                  $destination_topic = bbp_get_topic( $destination_topic_id );
1274  
1275                  // No destination topic
1276                  if ( empty( $destination_topic ) ) {
1277                      bbp_add_error( 'bbp_move_reply_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to move to was not found.', 'bbpress' ) );
1278                  }
1279  
1280                  // User cannot edit the destination topic
1281                  if ( ! current_user_can( 'edit_topic', $destination_topic->ID ) ) {
1282                      bbp_add_error( 'bbp_move_reply_destination_permission', __( '<strong>ERROR</strong>: You do not have permission to edit the destination topic.', 'bbpress' ) );
1283                  }
1284  
1285                  // Bump the reply position
1286                  $reply_position = bbp_get_topic_reply_count( $destination_topic->ID, true ) + 1;
1287  
1288                  // Update the reply
1289                  wp_update_post( array(
1290                      'ID'          => $move_reply->ID,
1291                      'post_title'  => '',
1292                      'post_name'   => false, // will be automatically generated
1293                      'post_parent' => $destination_topic->ID,
1294                      'menu_order'  => $reply_position,
1295                      'guid'        => ''
1296                  ) );
1297  
1298                  // Adjust reply meta values
1299                  bbp_update_reply_topic_id( $move_reply->ID, $destination_topic->ID );
1300                  bbp_update_reply_forum_id( $move_reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
1301  
1302                  break;
1303  
1304              // Move reply to a new topic
1305              case 'topic' :
1306              default :
1307  
1308                  // User needs to be able to publish topics
1309                  if ( current_user_can( 'publish_topics' ) ) {
1310  
1311                      // Use the new title that was passed
1312                      if ( ! empty( $_POST['bbp_reply_move_destination_title'] ) ) {
1313                          $destination_topic_title = sanitize_text_field( $_POST['bbp_reply_move_destination_title'] );
1314  
1315                      // Use the source topic title
1316                      } else {
1317                          $destination_topic_title = $source_topic->post_title;
1318                      }
1319  
1320                      // Update the topic
1321                      $destination_topic_id = wp_update_post( array(
1322                          'ID'          => $move_reply->ID,
1323                          'post_title'  => $destination_topic_title,
1324                          'post_name'   => false,
1325                          'post_type'   => bbp_get_topic_post_type(),
1326                          'post_parent' => $source_topic->post_parent,
1327                          'guid'        => ''
1328                      ) );
1329                      $destination_topic = bbp_get_topic( $destination_topic_id );
1330  
1331                      // Make sure the new topic knows its a topic
1332                      bbp_update_topic_topic_id( $move_reply->ID );
1333  
1334                      // Shouldn't happen
1335                      if ( false === $destination_topic_id || is_wp_error( $destination_topic_id ) || empty( $destination_topic ) ) {
1336                          bbp_add_error( 'bbp_move_reply_destination_reply', __( '<strong>ERROR</strong>: There was a problem converting the reply into the topic. Please try again.', 'bbpress' ) );
1337                      }
1338  
1339                  // User cannot publish posts
1340                  } else {
1341                      bbp_add_error( 'bbp_move_reply_destination_permission', __( '<strong>ERROR</strong>: You do not have permission to create new topics. The reply could not be converted into a topic.', 'bbpress' ) );
1342                  }
1343  
1344                  break;
1345          }
1346      }
1347  
1348      // Bail if there are errors
1349      if ( bbp_has_errors() ) {
1350          return;
1351      }
1352  
1353      /** No Errors - Clean Up **************************************************/
1354  
1355      // Update counts, etc...
1356      do_action( 'bbp_pre_move_reply', $move_reply->ID, $source_topic->ID, $destination_topic->ID );
1357  
1358      /** Date Check ************************************************************/
1359  
1360      // Check if the destination topic is older than the move reply
1361      if ( strtotime( $move_reply->post_date ) < strtotime( $destination_topic->post_date ) ) {
1362  
1363          // Set destination topic post_date to 1 second before from reply
1364          $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $move_reply->post_date ) - 1 );
1365  
1366          // Update destination topic
1367          wp_update_post( array(
1368              'ID'            => $destination_topic_id,
1369              'post_date'     => $destination_post_date,
1370              'post_date_gmt' => get_gmt_from_date( $destination_post_date )
1371          ) );
1372      }
1373  
1374      // Set the last reply ID and freshness to the move_reply
1375      $last_reply_id = $move_reply->ID;
1376      $freshness     = $move_reply->post_date;
1377  
1378      // Get the reply to
1379      $parent = bbp_get_reply_to( $move_reply->ID );
1380  
1381      // Fix orphaned children
1382      $children = get_posts( array(
1383          'post_type'  => bbp_get_reply_post_type(),
1384          'meta_key'   => '_bbp_reply_to',
1385          'meta_type'  => 'NUMERIC',
1386          'meta_value' => $move_reply->ID,
1387      ) );
1388      foreach ( $children as $child ) {
1389          bbp_update_reply_to( $child->ID, $parent );
1390      }
1391  
1392      // Remove reply_to from moved reply
1393      delete_post_meta( $move_reply->ID, '_bbp_reply_to' );
1394  
1395      // It is a new topic and we need to set some default metas to make
1396      // the topic display in bbp_has_topics() list
1397      if ( 'topic' === $move_option ) {
1398          bbp_update_topic_last_reply_id   ( $destination_topic->ID, $last_reply_id );
1399          bbp_update_topic_last_active_id  ( $destination_topic->ID, $last_reply_id );
1400          bbp_update_topic_last_active_time( $destination_topic->ID, $freshness     );
1401  
1402      // Otherwise update the existing destination topic
1403      } else {
1404          bbp_update_topic_last_reply_id   ( $destination_topic->ID );
1405          bbp_update_topic_last_active_id  ( $destination_topic->ID );
1406          bbp_update_topic_last_active_time( $destination_topic->ID );
1407      }
1408  
1409      // Update source topic ID last active
1410      bbp_update_topic_last_reply_id   ( $source_topic->ID );
1411      bbp_update_topic_last_active_id  ( $source_topic->ID );
1412      bbp_update_topic_last_active_time( $source_topic->ID );
1413  
1414      /** Successful Move ******************************************************/
1415  
1416      // Update counts, etc...
1417      do_action( 'bbp_post_move_reply', $move_reply->ID, $source_topic->ID, $destination_topic->ID );
1418  
1419      // Redirect back to the topic
1420      bbp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
1421  }
1422  
1423  /**
1424   * Fix counts on reply move
1425   *
1426   * When a reply is moved, update the counts of source and destination topic
1427   * and their forums.
1428   *
1429   * @since 2.3.0 bbPress (r4521)
1430   *
1431   * @param int $move_reply_id Move reply id
1432   * @param int $source_topic_id Source topic id
1433   * @param int $destination_topic_id Destination topic id
1434   */
1435  function bbp_move_reply_count( $move_reply_id, $source_topic_id, $destination_topic_id ) {
1436  
1437      // Forum topic counts
1438      bbp_update_forum_topic_count( bbp_get_topic_forum_id( $destination_topic_id ) );
1439  
1440      // Forum reply counts
1441      bbp_update_forum_reply_count( bbp_get_topic_forum_id( $destination_topic_id ) );
1442  
1443      // Topic reply counts
1444      bbp_update_topic_reply_count( $source_topic_id      );
1445      bbp_update_topic_reply_count( $destination_topic_id );
1446  
1447      // Topic hidden reply counts
1448      bbp_update_topic_reply_count_hidden( $source_topic_id      );
1449      bbp_update_topic_reply_count_hidden( $destination_topic_id );
1450  
1451      // Topic voice counts
1452      bbp_update_topic_voice_count( $source_topic_id      );
1453      bbp_update_topic_voice_count( $destination_topic_id );
1454  
1455      do_action( 'bbp_move_reply_count', $move_reply_id, $source_topic_id, $destination_topic_id );
1456  }
1457  
1458  /** Reply Actions *************************************************************/
1459  
1460  /**
1461   * Handles the front end spamming/unspamming and trashing/untrashing/deleting of
1462   * replies
1463   *
1464   * @since 2.0.0 bbPress (r2740)
1465   *
1466   * @param string $action The requested action to compare this function to
1467   */
1468  function bbp_toggle_reply_handler( $action = '' ) {
1469  
1470      // Bail if required GET actions aren't passed
1471      if ( empty( $_GET['reply_id'] ) ) {
1472          return;
1473      }
1474  
1475      // What's the reply id?
1476      $reply_id = bbp_get_reply_id( (int) $_GET['reply_id'] );
1477  
1478      // Get possible reply-handler toggles
1479      $toggles = bbp_get_reply_toggles( $reply_id );
1480  
1481      // Bail if action isn't meant for this function
1482      if ( ! in_array( $action, $toggles, true ) ) {
1483          return;
1484      }
1485  
1486      // Make sure reply exists
1487      $reply = bbp_get_reply( $reply_id );
1488      if ( empty( $reply ) ) {
1489          bbp_add_error( 'bbp_toggle_reply_missing', __( '<strong>ERROR:</strong> This reply could not be found or no longer exists.', 'bbpress' ) );
1490          return;
1491      }
1492  
1493      // What is the user doing here?
1494      if ( ! current_user_can( 'edit_reply', $reply_id ) || ( 'bbp_toggle_reply_trash' === $action && ! current_user_can( 'delete_reply', $reply_id ) ) ) {
1495          bbp_add_error( 'bbp_toggle_reply_permission', __( '<strong>ERROR:</strong> You do not have permission to do that.', 'bbpress' ) );
1496          return;
1497      }
1498  
1499      // Sub-action?
1500      $sub_action = ! empty( $_GET['sub_action'] )
1501          ? sanitize_key( $_GET['sub_action'] )
1502          : false;
1503  
1504      // Preliminary array
1505      $post_data = array( 'ID' => $reply_id );
1506  
1507      // Do the reply toggling
1508      $retval = bbp_toggle_reply( array(
1509          'id'         => $reply_id,
1510          'action'     => $action,
1511          'sub_action' => $sub_action,
1512          'data'       => $post_data
1513      ) );
1514  
1515      // Do additional reply toggle actions
1516      do_action( 'bbp_toggle_reply_handler', $retval['status'], $post_data, $action );
1517  
1518      // Redirect back to reply
1519      if ( ( false !== $retval['status'] ) && ! is_wp_error( $retval['status'] ) ) {
1520          bbp_redirect( $retval['redirect_to'] );
1521  
1522      // Handle errors
1523      } else {
1524          bbp_add_error( 'bbp_toggle_reply', $retval['message'] );
1525      }
1526  }
1527  
1528  /**
1529   * Do the actual reply toggling
1530   *
1531   * This function is used by `bbp_toggle_reply_handler()` to do the actual heavy
1532   * lifting when it comes to toggling replies. It only really makes sense to call
1533   * within that context, so if you need to call this function directly, make sure
1534   * you're also doing what the handler does too.
1535   *
1536   * @since 2.6.0 bbPress (r6133)
1537   * @access private
1538   *
1539   * @param array $args
1540   */
1541  function bbp_toggle_reply( $args = array() ) {
1542  
1543      // Parse the arguments
1544      $r = bbp_parse_args( $args, array(
1545          'id'         => 0,
1546          'action'     => '',
1547          'sub_action' => '',
1548          'data'       => array()
1549      ) );
1550  
1551      // Build the nonce suffix
1552      $nonce_suffix = bbp_get_reply_post_type() . '_' . (int) $r['id'];
1553  
1554      // Default return values
1555      $retval = array(
1556          'status'      => 0,
1557          'message'     => '',
1558          'redirect_to' => bbp_get_reply_url( $r['id'], bbp_get_redirect_to() ),
1559          'view_all'    => false
1560      );
1561  
1562      // What action are we trying to perform?
1563      switch ( $r['action'] ) {
1564  
1565          // Toggle approve
1566          case 'bbp_toggle_reply_approve' :
1567              check_ajax_referer( "approve-{$nonce_suffix}" );
1568  
1569              $is_approve         = bbp_is_reply_pending( $r['id'] );
1570              $retval['status']   = $is_approve ? bbp_approve_reply( $r['id'] ) : bbp_unapprove_reply( $r['id'] );
1571              $retval['message']  = $is_approve ? __( '<strong>ERROR</strong>: There was a problem approving the reply.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem unapproving the reply.', 'bbpress' );
1572              $retval['view_all'] = ! $is_approve;
1573  
1574              break;
1575  
1576          // Toggle spam
1577          case 'bbp_toggle_reply_spam' :
1578              check_ajax_referer( "spam-{$nonce_suffix}" );
1579  
1580              $is_spam            = bbp_is_reply_spam( $r['id'] );
1581              $retval['status']   = $is_spam ? bbp_unspam_reply( $r['id'] ) : bbp_spam_reply( $r['id'] );
1582              $retval['message']  = $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the reply as spam.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the reply as spam.', 'bbpress' );
1583              $retval['view_all'] = ! $is_spam;
1584  
1585              break;
1586  
1587          // Toggle trash
1588          case 'bbp_toggle_reply_trash' :
1589  
1590              // Which subaction?
1591              switch ( $r['sub_action'] ) {
1592                  case 'trash':
1593                      check_ajax_referer( "trash-{$nonce_suffix}" );
1594  
1595                      $retval['view_all'] = true;
1596                      $retval['status']   = wp_trash_post( $r['id'] );
1597                      $retval['message']  = __( '<strong>ERROR</strong>: There was a problem trashing the reply.', 'bbpress' );
1598  
1599                      break;
1600  
1601                  case 'untrash':
1602                      check_ajax_referer( "untrash-{$nonce_suffix}" );
1603  
1604                      $retval['status']  = wp_untrash_post( $r['id'] );
1605                      $retval['message'] = __( '<strong>ERROR</strong>: There was a problem untrashing the reply.', 'bbpress' );
1606  
1607                      break;
1608  
1609                  case 'delete':
1610                      check_ajax_referer( "delete-{$nonce_suffix}" );
1611  
1612                      $retval['status']  = wp_delete_post( $r['id'] );
1613                      $retval['message'] = __( '<strong>ERROR</strong>: There was a problem deleting the reply.', 'bbpress' );
1614  
1615                      break;
1616              }
1617  
1618              break;
1619      }
1620  
1621      // Add view all if needed
1622      if ( ! empty( $retval['view_all'] ) ) {
1623          $retval['redirect_to'] = bbp_add_view_all( $retval['redirect_to'], true );
1624      }
1625  
1626      // Filter & return
1627      return apply_filters( 'bbp_toggle_reply', $retval, $r, $args );
1628  }
1629  
1630  /** Helpers *******************************************************************/
1631  
1632  /**
1633   * Return an associative array of available reply statuses
1634   *
1635   * @since 2.6.0 bbPress (r5399)
1636   *
1637   * @param int $reply_id   Optional. Reply id.
1638   *
1639   * @return array
1640   */
1641  function bbp_get_reply_statuses( $reply_id = 0 ) {
1642  
1643      // Filter & return
1644      return (array) apply_filters( 'bbp_get_reply_statuses', array(
1645          bbp_get_public_status_id()  => _x( 'Publish', 'Publish the reply',     'bbpress' ),
1646          bbp_get_spam_status_id()    => _x( 'Spam',    'Spam the reply',        'bbpress' ),
1647          bbp_get_trash_status_id()   => _x( 'Trash',   'Trash the reply',       'bbpress' ),
1648          bbp_get_pending_status_id() => _x( 'Pending', 'Mark reply as pending', 'bbpress' )
1649      ), $reply_id );
1650  }
1651  
1652  /**
1653   * Return array of available reply toggle actions
1654   *
1655   * @since 2.6.0 bbPress (r6133)
1656   *
1657   * @param int $reply_id   Optional. Reply id.
1658   *
1659   * @return array
1660   */
1661  function bbp_get_reply_toggles( $reply_id = 0 ) {
1662  
1663      // Filter & return
1664      return (array) apply_filters( 'bbp_get_toggle_reply_actions', array(
1665          'bbp_toggle_reply_spam',
1666          'bbp_toggle_reply_trash',
1667          'bbp_toggle_reply_approve'
1668      ), $reply_id );
1669  }
1670  
1671  /**
1672   * Return array of public reply statuses.
1673   *
1674   * @since 2.6.0 bbPress (r6705)
1675   *
1676   * @return array
1677   */
1678  function bbp_get_public_reply_statuses() {
1679      $statuses = array(
1680          bbp_get_public_status_id()
1681      );
1682  
1683      // Filter & return
1684      return (array) apply_filters( 'bbp_get_public_reply_statuses', $statuses );
1685  }
1686  
1687  /**
1688   * Return array of non-public reply statuses.
1689   *
1690   * @since 2.6.0 bbPress (r6791)
1691   *
1692   * @return array
1693   */
1694  function bbp_get_non_public_reply_statuses() {
1695      $statuses = array(
1696          bbp_get_trash_status_id(),
1697          bbp_get_spam_status_id(),
1698          bbp_get_pending_status_id()
1699      );
1700  
1701      // Filter & return
1702      return (array) apply_filters( 'bbp_get_non_public_reply_statuses', $statuses );
1703  }
1704  
1705  /** Reply Actions *************************************************************/
1706  
1707  /**
1708   * Marks a reply as spam
1709   *
1710   * @since 2.0.0 bbPress (r2740)
1711   *
1712   * @param int $reply_id Reply id
1713   * @return mixed False or {@link WP_Error} on failure, reply id on success
1714   */
1715  function bbp_spam_reply( $reply_id = 0 ) {
1716  
1717      // Get reply
1718      $reply = bbp_get_reply( $reply_id );
1719      if ( empty( $reply ) ) {
1720          return $reply;
1721      }
1722  
1723      // Bail if already spam
1724      if ( bbp_get_spam_status_id() === $reply->post_status ) {
1725          return false;
1726      }
1727  
1728      // Execute pre spam code
1729      do_action( 'bbp_spam_reply', $reply_id );
1730  
1731      // Add the original post status as post meta for future restoration
1732      add_post_meta( $reply_id, '_bbp_spam_meta_status', $reply->post_status );
1733  
1734      // Set post status to spam
1735      $reply->post_status = bbp_get_spam_status_id();
1736  
1737      // No revisions
1738      remove_action( 'pre_post_update', 'wp_save_post_revision' );
1739  
1740      // Update the reply
1741      $reply_id = wp_update_post( $reply );
1742  
1743      // Execute post spam code
1744      do_action( 'bbp_spammed_reply', $reply_id );
1745  
1746      // Return reply_id
1747      return $reply_id;
1748  }
1749  
1750  /**
1751   * Unspams a reply
1752   *
1753   * @since 2.0.0 bbPress (r2740)
1754   *
1755   * @param int $reply_id Reply id
1756   * @return mixed False or {@link WP_Error} on failure, reply id on success
1757   */
1758  function bbp_unspam_reply( $reply_id = 0 ) {
1759  
1760      // Get reply
1761      $reply = bbp_get_reply( $reply_id );
1762      if ( empty( $reply ) ) {
1763          return $reply;
1764      }
1765  
1766      // Bail if already not spam
1767      if ( bbp_get_spam_status_id() !== $reply->post_status ) {
1768          return false;
1769      }
1770  
1771      // Execute pre unspam code
1772      do_action( 'bbp_unspam_reply', $reply_id );
1773  
1774      // Get pre spam status
1775      $reply->post_status = get_post_meta( $reply_id, '_bbp_spam_meta_status', true );
1776  
1777      // If no previous status, default to publish
1778      if ( empty( $reply->post_status ) ) {
1779          $reply->post_status = bbp_get_public_status_id();
1780      }
1781  
1782      // Delete pre spam meta
1783      delete_post_meta( $reply_id, '_bbp_spam_meta_status' );
1784  
1785      // No revisions
1786      remove_action( 'pre_post_update', 'wp_save_post_revision' );
1787  
1788      // Update the reply
1789      $reply_id = wp_update_post( $reply );
1790  
1791      // Execute post unspam code
1792      do_action( 'bbp_unspammed_reply', $reply_id );
1793  
1794      // Return reply_id
1795      return $reply_id;
1796  }
1797  
1798  /**
1799   * Approves a reply
1800   *
1801   * @since 2.6.0 bbPress (r5506)
1802   *
1803   * @param int $reply_id Reply id
1804   * @return mixed False or {@link WP_Error} on failure, reply id on success
1805   */
1806  function bbp_approve_reply( $reply_id = 0 ) {
1807  
1808      // Get reply
1809      $reply = bbp_get_reply( $reply_id );
1810      if ( empty( $reply ) ) {
1811          return $reply;
1812      }
1813  
1814      // Get new status
1815      $status = bbp_get_public_status_id();
1816  
1817      // Bail if already approved
1818      if ( $status === $reply->post_status ) {
1819          return false;
1820      }
1821  
1822      // Execute pre pending code
1823      do_action( 'bbp_approve_reply', $reply_id );
1824  
1825      // Set publish status
1826      $reply->post_status = $status;
1827  
1828      // No revisions
1829      remove_action( 'pre_post_update', 'wp_save_post_revision' );
1830  
1831      // Update reply
1832      $reply_id = wp_update_post( $reply );
1833  
1834      // Execute post pending code
1835      do_action( 'bbp_approved_reply', $reply_id );
1836  
1837      // Return reply_id
1838      return $reply_id;
1839  }
1840  
1841  /**
1842   * Unapproves a reply
1843   *
1844   * @since 2.6.0 bbPress (r5506)
1845   *
1846   * @param int $reply_id Reply id
1847   * @return mixed False or {@link WP_Error} on failure, reply id on success
1848   */
1849  function bbp_unapprove_reply( $reply_id = 0 ) {
1850  
1851      // Get reply
1852      $reply = bbp_get_reply( $reply_id );
1853      if ( empty( $reply ) ) {
1854          return $reply;
1855      }
1856  
1857      // Get new status
1858      $status = bbp_get_pending_status_id();
1859  
1860      // Bail if already pending
1861      if ( $status === $reply->post_status ) {
1862          return false;
1863      }
1864  
1865      // Execute pre open code
1866      do_action( 'bbp_unapprove_reply', $reply_id );
1867  
1868      // Set pending status
1869      $reply->post_status = $status;
1870  
1871      // No revisions
1872      remove_action( 'pre_post_update', 'wp_save_post_revision' );
1873  
1874      // Update reply
1875      $reply_id = wp_update_post( $reply );
1876  
1877      // Execute post open code
1878      do_action( 'bbp_unapproved_reply', $reply_id );
1879  
1880      // Return reply_id
1881      return $reply_id;
1882  }
1883  
1884  /** Before Delete/Trash/Untrash ***********************************************/
1885  
1886  /**
1887   * Called before deleting a reply
1888   */
1889  function bbp_delete_reply( $reply_id = 0 ) {
1890      $reply_id = bbp_get_reply_id( $reply_id );
1891  
1892      if ( empty( $reply_id ) || ! bbp_is_reply( $reply_id ) ) {
1893          return false;
1894      }
1895  
1896      do_action( 'bbp_delete_reply', $reply_id );
1897  }
1898  
1899  /**
1900   * Called before trashing a reply
1901   */
1902  function bbp_trash_reply( $reply_id = 0 ) {
1903      $reply_id = bbp_get_reply_id( $reply_id );
1904  
1905      if ( empty( $reply_id ) || ! bbp_is_reply( $reply_id ) ) {
1906          return false;
1907      }
1908  
1909      do_action( 'bbp_trash_reply', $reply_id );
1910  }
1911  
1912  /**
1913   * Called before untrashing (restoring) a reply
1914   */
1915  function bbp_untrash_reply( $reply_id = 0 ) {
1916      $reply_id = bbp_get_reply_id( $reply_id );
1917  
1918      if ( empty( $reply_id ) || ! bbp_is_reply( $reply_id ) ) {
1919          return false;
1920      }
1921  
1922      do_action( 'bbp_untrash_reply', $reply_id );
1923  }
1924  
1925  /** After Delete/Trash/Untrash ************************************************/
1926  
1927  /**
1928   * Called after deleting a reply
1929   *
1930   * @since 2.0.0 bbPress (r2993)
1931   */
1932  function bbp_deleted_reply( $reply_id = 0 ) {
1933      $reply_id = bbp_get_reply_id( $reply_id );
1934  
1935      if ( empty( $reply_id ) || ! bbp_is_reply( $reply_id ) ) {
1936          return false;
1937      }
1938  
1939      do_action( 'bbp_deleted_reply', $reply_id );
1940  }
1941  
1942  /**
1943   * Called after trashing a reply
1944   *
1945   * @since 2.0.0 bbPress (r2993)
1946   */
1947  function bbp_trashed_reply( $reply_id = 0 ) {
1948      $reply_id = bbp_get_reply_id( $reply_id );
1949  
1950      if ( empty( $reply_id ) || ! bbp_is_reply( $reply_id ) ) {
1951          return false;
1952      }
1953  
1954      do_action( 'bbp_trashed_reply', $reply_id );
1955  }
1956  
1957  /**
1958   * Called after untrashing (restoring) a reply
1959   *
1960   * @since 2.0.0 bbPress (r2993)
1961   */
1962  function bbp_untrashed_reply( $reply_id = 0 ) {
1963      $reply_id = bbp_get_reply_id( $reply_id );
1964  
1965      if ( empty( $reply_id ) || ! bbp_is_reply( $reply_id ) ) {
1966          return false;
1967      }
1968  
1969      do_action( 'bbp_untrashed_reply', $reply_id );
1970  }
1971  
1972  /** Settings ******************************************************************/
1973  
1974  /**
1975   * Return the replies per page setting
1976   *
1977   * @since 2.0.0 bbPress (r3540)
1978   *
1979   * @param int $default Default replies per page (15)
1980   * @return int
1981   */
1982  function bbp_get_replies_per_page( $default = 15 ) {
1983  
1984      // Get database option and cast as integer
1985      $retval = get_option( '_bbp_replies_per_page', $default );
1986  
1987      // If return val is empty, set it to default
1988      if ( empty( $retval ) ) {
1989          $retval = $default;
1990      }
1991  
1992      // Filter & return
1993      return (int) apply_filters( 'bbp_get_replies_per_page', $retval, $default );
1994  }
1995  
1996  /**
1997   * Return the replies per RSS page setting
1998   *
1999   * @since 2.0.0 bbPress (r3540)
2000   *
2001   * @param int $default Default replies per page (25)
2002   * @return int
2003   */
2004  function bbp_get_replies_per_rss_page( $default = 25 ) {
2005  
2006      // Get database option and cast as integer
2007      $retval = get_option( '_bbp_replies_per_rss_page', $default );
2008  
2009      // If return val is empty, set it to default
2010      if ( empty( $retval ) ) {
2011          $retval = $default;
2012      }
2013  
2014      // Filter & return
2015      return (int) apply_filters( 'bbp_get_replies_per_rss_page', $retval, $default );
2016  }
2017  
2018  /** Autoembed *****************************************************************/
2019  
2020  /**
2021   * Check if autoembeds are enabled and hook them in if so
2022   *
2023   * @since 2.1.0 bbPress (r3752)
2024   *
2025   * @global WP_Embed $wp_embed
2026   */
2027  function bbp_reply_content_autoembed() {
2028      global $wp_embed;
2029  
2030      if ( bbp_use_autoembed() && is_a( $wp_embed, 'WP_Embed' ) ) {
2031          add_filter( 'bbp_get_reply_content', array( $wp_embed, 'autoembed' ), 2 );
2032      }
2033  }
2034  
2035  /** Filters *******************************************************************/
2036  
2037  /**
2038   * Used by bbp_has_replies() to add the lead topic post to the posts loop
2039   *
2040   * This function filters the 'post_where' of the WP_Query, and changes the query
2041   * to include both the topic AND its children in the same loop.
2042   *
2043   * @since 2.1.0 bbPress (r4058)
2044   *
2045   * @param string $where
2046   * @return string
2047   */
2048  function _bbp_has_replies_where( $where = '', $query = false ) {
2049  
2050      /** Bail ******************************************************************/
2051  
2052      // Bail if the sky is falling
2053      if ( empty( $where ) || empty( $query ) ) {
2054          return $where;
2055      }
2056  
2057      // Bail if no post_parent to replace
2058      if ( ! is_numeric( $query->get( 'post_parent' ) ) ) {
2059          return $where;
2060      }
2061  
2062      // Bail if not a topic and reply query
2063      if ( array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) !== $query->get( 'post_type' ) ) {
2064          return $where;
2065      }
2066  
2067      // Bail if including specific post ID's
2068      if ( $query->get( 'post__in' ) ) {
2069          return $where;
2070      }
2071  
2072      /** Proceed ***************************************************************/
2073  
2074      // Table name for posts
2075      $table_name = bbp_db()->prefix . 'posts';
2076  
2077      // Get the topic ID from the post_parent, set in bbp_has_replies()
2078      $topic_id   = bbp_get_topic_id( $query->get( 'post_parent' ) );
2079  
2080      // The texts to search for
2081      $search     = array(
2082          "FROM {$table_name} " ,
2083          "WHERE 1=1  AND {$table_name}.post_parent = {$topic_id}",
2084          ") AND {$table_name}.post_parent = {$topic_id}"
2085      );
2086  
2087      // The texts to replace them with
2088      $replace     = array(
2089          $search[0] . "FORCE INDEX (PRIMARY, post_parent) " ,
2090          "WHERE 1=1 AND ({$table_name}.ID = {$topic_id} OR {$table_name}.post_parent = {$topic_id})",
2091          ") AND ({$table_name}.ID = {$topic_id} OR {$table_name}.post_parent = {$topic_id})"
2092      );
2093  
2094      // Try to replace the search text with the replacement
2095      $new_where = str_replace( $search, $replace, $where );
2096      if ( ! empty( $new_where ) ) {
2097          $where = $new_where;
2098      }
2099  
2100      return $where;
2101  }
2102  
2103  /** Feeds *********************************************************************/
2104  
2105  /**
2106   * Output an RSS2 feed of replies, based on the query passed.
2107   *
2108   * @since 2.0.0 bbPress (r3171)
2109   *
2110   * @param array $replies_query
2111   */
2112  function bbp_display_replies_feed_rss2( $replies_query = array() ) {
2113  
2114      // User cannot access forum this topic is in
2115      if ( bbp_is_single_topic() && ! bbp_user_can_view_forum( array( 'forum_id' => bbp_get_topic_forum_id() ) ) ) {
2116          return;
2117      }
2118  
2119      // Adjust the title based on context
2120      if ( bbp_is_single_topic() ) {
2121          $title = get_wp_title_rss();
2122      } elseif ( ! bbp_show_lead_topic() ) {
2123          $title = get_bloginfo_rss( 'name' ) . ' &#187; ' .  __( 'All Posts',   'bbpress' );
2124      } else {
2125          $title = get_bloginfo_rss( 'name' ) . ' &#187; ' .  __( 'All Replies', 'bbpress' );
2126      }
2127  
2128      $title = apply_filters( 'wp_title_rss', $title );
2129  
2130      // Display the feed
2131      header( 'Content-Type: ' . feed_content_type( 'rss2' ) . '; charset=' . get_option( 'blog_charset' ), true );
2132      header( 'Status: 200 OK' );
2133      echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>
2134  
2135      <rss version="2.0"
2136          xmlns:content="http://purl.org/rss/1.0/modules/content/"
2137          xmlns:wfw="http://wellformedweb.org/CommentAPI/"
2138          xmlns:dc="http://purl.org/dc/elements/1.1/"
2139          xmlns:atom="http://www.w3.org/2005/Atom"
2140  
2141          <?php do_action( 'bbp_feed' ); ?>
2142      >
2143  
2144      <channel>
2145  
2146          <title><?php echo $title; // Already escaped ?></title>
2147          <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
2148          <link><?php self_link(); ?></link>
2149          <description><?php //?></description>
2150          <lastBuildDate><?php echo date( 'r' ); ?></lastBuildDate>
2151          <generator><?php echo esc_url_raw( 'https://bbpress.org/?v=' . convert_chars( bbp_get_version() ) ); ?></generator>
2152          <language><?php bloginfo_rss( 'language' ); ?></language>
2153  
2154          <?php do_action( 'bbp_feed_head' ); ?>
2155  
2156          <?php if ( bbp_is_single_topic() ) : ?>
2157              <?php if ( bbp_user_can_view_forum( array( 'forum_id' => bbp_get_topic_forum_id() ) ) ) : ?>
2158                  <?php if ( bbp_show_lead_topic() ) : ?>
2159  
2160                      <item>
2161                          <guid><?php bbp_topic_permalink(); ?></guid>
2162                          <title><![CDATA[<?php bbp_topic_title(); ?>]]></title>
2163                          <link><?php bbp_topic_permalink(); ?></link>
2164                          <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
2165                          <dc:creator><?php the_author(); ?></dc:creator>
2166  
2167                          <description>
2168                              <![CDATA[
2169                              <p><?php printf( __( 'Replies: %s', 'bbpress' ), bbp_get_topic_reply_count() ); ?></p>
2170                              <?php bbp_topic_content(); ?>
2171                              ]]>
2172                          </description>
2173  
2174                          <?php rss_enclosure(); ?>
2175  
2176                          <?php do_action( 'bbp_feed_item' ); ?>
2177  
2178                      </item>
2179  
2180                  <?php endif; ?>
2181              <?php endif; ?>
2182          <?php endif; ?>
2183  
2184          <?php if ( bbp_has_replies( $replies_query ) ) : ?>
2185              <?php while ( bbp_replies() ) : bbp_the_reply(); ?>
2186  
2187                  <item>
2188                      <guid><?php bbp_reply_url(); ?></guid>
2189                      <title><![CDATA[<?php bbp_reply_title(); ?>]]></title>
2190                      <link><?php bbp_reply_url(); ?></link>
2191                      <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
2192                      <dc:creator><?php the_author() ?></dc:creator>
2193  
2194                      <description>
2195                          <![CDATA[
2196                          <?php bbp_reply_content(); ?>
2197                          ]]>
2198                      </description>
2199  
2200                      <?php rss_enclosure(); ?>
2201  
2202                      <?php do_action( 'bbp_feed_item' ); ?>
2203  
2204                  </item>
2205  
2206              <?php endwhile; ?>
2207          <?php endif; ?>
2208  
2209          <?php do_action( 'bbp_feed_footer' ); ?>
2210  
2211      </channel>
2212      </rss>
2213  
2214  <?php
2215  
2216      // We're done here
2217      exit();
2218  }
2219  
2220  /** Permissions ***************************************************************/
2221  
2222  /**
2223   * Redirect if unauthorized user is attempting to edit a reply
2224   *
2225   * @since 2.1.0 bbPress (r3605)
2226   */
2227  function bbp_check_reply_edit() {
2228  
2229      // Bail if not editing a topic
2230      if ( ! bbp_is_reply_edit() ) {
2231          return;
2232      }
2233  
2234      // User cannot edit topic, so redirect back to reply
2235      if ( ! current_user_can( 'edit_reply', bbp_get_reply_id() ) ) {
2236          bbp_redirect( bbp_get_reply_url() );
2237      }
2238  }
2239  
2240  /** Reply Position ************************************************************/
2241  
2242  /**
2243   * Update the position of the reply.
2244   *
2245   * The reply position is stored in the menu_order column of the posts table.
2246   * This is done to prevent using a meta_query to retrieve posts in the proper
2247   * freshness order. By updating the menu_order accordingly, we're able to
2248   * leverage core WordPress query ordering much more effectively.
2249   *
2250   * @since 2.1.0 bbPress (r3933)
2251   *
2252   * @param int $reply_id
2253   * @param int $reply_position
2254   *
2255   * @return mixed
2256   */
2257  function bbp_update_reply_position( $reply_id = 0, $reply_position = false ) {
2258  
2259      // Bail if reply_id is empty
2260      $reply_id = bbp_get_reply_id( $reply_id );
2261      if ( empty( $reply_id ) ) {
2262          return false;
2263      }
2264  
2265      // Prepare the reply position
2266      $reply_position = is_numeric( $reply_position )
2267          ? (int) $reply_position
2268          : bbp_get_reply_position_raw( $reply_id, bbp_get_reply_topic_id( $reply_id ) );
2269  
2270      // Get the current reply position
2271      $current_position = get_post_field( 'menu_order', $reply_id );
2272  
2273      // Bail if no change
2274      if ( $reply_position === $current_position ) {
2275          return false;
2276      }
2277  
2278      // Filters not removed
2279      $removed = false;
2280  
2281      // Toggle revisions off as we are not altering content
2282      if ( has_filter( 'clean_post_cache', 'bbp_clean_post_cache' ) ) {
2283          $removed = true;
2284          remove_filter( 'clean_post_cache', 'bbp_clean_post_cache', 10, 2 );
2285      }
2286  
2287      // Update the replies' 'menu_order' with the reply position
2288      $bbp_db = bbp_db();
2289      $bbp_db->update( $bbp_db->posts, array( 'menu_order' => $reply_position ), array( 'ID' => $reply_id ) );
2290      clean_post_cache( $reply_id );
2291  
2292      // Toggle revisions back on
2293      if ( true === $removed ) {
2294          $removed = false;
2295          add_filter( 'clean_post_cache', 'bbp_clean_post_cache', 10, 2 );
2296      }
2297  
2298      return (int) $reply_position;
2299  }
2300  
2301  /**
2302   * Get the position of a reply by querying the DB directly for the replies
2303   * of a given topic.
2304   *
2305   * @since 2.1.0 bbPress (r3933)
2306   *
2307   * @param int $reply_id
2308   * @param int $topic_id
2309   */
2310  function bbp_get_reply_position_raw( $reply_id = 0, $topic_id = 0 ) {
2311  
2312      // Get required data
2313      $reply_position = 0;
2314      $reply_id       = bbp_get_reply_id( $reply_id );
2315      $topic_id       = ! empty( $topic_id )
2316          ? bbp_get_topic_id( $topic_id )
2317          : bbp_get_reply_topic_id( $reply_id );
2318  
2319      // If reply is actually the first post in a topic, return 0
2320      if ( $reply_id !== $topic_id ) {
2321  
2322          // Make sure the topic has replies before running another query
2323          $reply_count = bbp_get_topic_reply_count( $topic_id, false );
2324          if ( ! empty( $reply_count ) ) {
2325  
2326              // Get reply id's
2327              $topic_replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() );
2328              if ( ! empty( $topic_replies ) ) {
2329  
2330                  // Reverse replies array and search for current reply position
2331                  $topic_replies  = array_reverse( $topic_replies );
2332                  $reply_position = array_search( (string) $reply_id, $topic_replies );
2333  
2334                  // Bump the position to compensate for the lead topic post
2335                  $reply_position++;
2336              }
2337          }
2338      }
2339  
2340      return (int) $reply_position;
2341  }
2342  
2343  /** Hierarchical Replies ******************************************************/
2344  
2345  /**
2346   * Are replies threaded?
2347   *
2348   * @since 2.4.0 bbPress (r4944)
2349   * @since 2.6.0 bbPress (r6245) Always false on user profile reply pages
2350   *
2351   * @param bool $default Optional. Default value true
2352   *
2353   * @return bool Are replies threaded?
2354   */
2355  function bbp_thread_replies() {
2356      $depth = bbp_thread_replies_depth();
2357      $allow = bbp_allow_threaded_replies();
2358  
2359      // Never thread replies on user profile pages. It looks weird, and we know
2360      // it is undesirable for the majority of installations.
2361      if ( bbp_is_single_user_replies() ) {
2362          $retval = false;
2363      } else {
2364          $retval = (bool) ( ( $depth >= 2 ) && ( true === $allow ) );
2365      }
2366  
2367      // Filter & return
2368      return (bool) apply_filters( 'bbp_thread_replies', $retval, $depth, $allow );
2369  }
2370  
2371  /**
2372   * List threaded replies
2373   *
2374   * @since 2.4.0 bbPress (r4944)
2375   */
2376  function bbp_list_replies( $args = array() ) {
2377  
2378      // Get bbPress
2379      $bbp = bbpress();
2380  
2381      // Reset the reply depth
2382      $bbp->reply_query->reply_depth = 0;
2383  
2384      // In reply loop
2385      $bbp->reply_query->in_the_loop = true;
2386  
2387      // Parse arguments
2388      $r = bbp_parse_args( $args, array(
2389          'walker'       => new BBP_Walker_Reply,
2390          'max_depth'    => bbp_thread_replies_depth(),
2391          'style'        => 'ul',
2392          'callback'     => null,
2393          'end_callback' => null,
2394          'page'         => 1,
2395          'per_page'     => -1
2396      ), 'list_replies' );
2397  
2398      // Get replies to loop through in $_replies
2399      echo '<ul>' . $r['walker']->paged_walk( $bbp->reply_query->posts, $r['max_depth'], $r['page'], $r['per_page'], $r ) . '</ul>';
2400  
2401      $bbp->max_num_pages            = $r['walker']->max_pages;
2402      $bbp->reply_query->in_the_loop = false;
2403  }
2404  
2405  /**
2406   * Validate a `reply_to` field for hierarchical replies
2407   *
2408   * Checks for 2 scenarios:
2409   * -- The reply to ID is actually a reply
2410   * -- The reply to ID does not match the current reply
2411   *
2412   * @see https://bbpress.trac.wordpress.org/ticket/2588
2413   * @see https://bbpress.trac.wordpress.org/ticket/2586
2414   *
2415   * @since 2.5.4 bbPress (r5377)
2416   *
2417   * @param int $reply_to
2418   * @param int $reply_id
2419   *
2420   * @return int $reply_to
2421   */
2422  function bbp_validate_reply_to( $reply_to = 0, $reply_id = 0 ) {
2423  
2424      // The parent reply must actually be a reply
2425      if ( ! bbp_is_reply( $reply_to ) ) {
2426          $reply_to = 0;
2427      }
2428  
2429      // The parent reply cannot be itself
2430      if ( $reply_id === $reply_to ) {
2431          $reply_to = 0;
2432      }
2433  
2434      return (int) $reply_to;
2435  }


Generated: Wed Nov 13 01:01:28 2019 Cross-referenced by PHPXref 0.7.1