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


Generated: Mon Aug 2 01:01:34 2021 Cross-referenced by PHPXref 0.7.1