[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> comment.php (source)

   1  <?php
   2  /**
   3   * WordPress Comment Administration API.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   * @since 2.3.0
   8   */
   9  
  10  /**
  11   * Determines if a comment exists based on author and date.
  12   *
  13   * For best performance, use `$timezone = 'gmt'`, which queries a field that is properly indexed. The default value
  14   * for `$timezone` is 'blog' for legacy reasons.
  15   *
  16   * @since 2.0.0
  17   * @since 4.4.0 Added the `$timezone` parameter.
  18   *
  19   * @global wpdb $wpdb WordPress database abstraction object.
  20   *
  21   * @param string $comment_author Author of the comment.
  22   * @param string $comment_date   Date of the comment.
  23   * @param string $timezone       Timezone. Accepts 'blog' or 'gmt'. Default 'blog'.
  24   * @return string|null Comment post ID on success.
  25   */
  26  function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) {
  27      global $wpdb;
  28  
  29      $date_field = 'comment_date';
  30      if ( 'gmt' === $timezone ) {
  31          $date_field = 'comment_date_gmt';
  32      }
  33  
  34      return $wpdb->get_var(
  35          $wpdb->prepare(
  36              "SELECT comment_post_ID FROM $wpdb->comments
  37              WHERE comment_author = %s AND $date_field = %s",
  38              stripslashes( $comment_author ),
  39              stripslashes( $comment_date )
  40          )
  41      );
  42  }
  43  
  44  /**
  45   * Updates a comment with values provided in $_POST.
  46   *
  47   * @since 2.0.0
  48   * @since 5.5.0 A return value was added.
  49   * @since 7.1.0 The comment parent can be updated via `$_POST['comment_parent']`.
  50   * @since 7.1.5 Moving a comment to another post is checked against the user's capabilities,
  51   *              and the comment type can no longer be changed.
  52   *
  53   * @return int|WP_Error The value 1 if the comment was updated, 0 if not updated.
  54   *                      A WP_Error object on failure.
  55   */
  56  function edit_comment() {
  57      if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) ) {
  58          wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) );
  59      }
  60  
  61      if ( isset( $_POST['newcomment_author'] ) ) {
  62          $_POST['comment_author'] = $_POST['newcomment_author'];
  63      }
  64      if ( isset( $_POST['newcomment_author_email'] ) ) {
  65          $_POST['comment_author_email'] = $_POST['newcomment_author_email'];
  66      }
  67      if ( isset( $_POST['newcomment_author_url'] ) ) {
  68          $_POST['comment_author_url'] = $_POST['newcomment_author_url'];
  69      }
  70      if ( isset( $_POST['comment_status'] ) ) {
  71          $_POST['comment_approved'] = $_POST['comment_status'];
  72      }
  73      if ( isset( $_POST['content'] ) ) {
  74          $_POST['comment_content'] = $_POST['content'];
  75      }
  76      if ( isset( $_POST['comment_ID'] ) ) {
  77          $_POST['comment_ID'] = (int) $_POST['comment_ID'];
  78      }
  79  
  80      $comment_id = (int) $_POST['comment_ID'];
  81      $comment    = get_comment( $comment_id );
  82  
  83      /*
  84       * The comment type is not editable from the Edit Comment screen. Changing it
  85       * would turn a private note into a public comment or vice versa, and would
  86       * break the rule that replies share their parent's type.
  87       */
  88      if ( $comment && isset( $_POST['comment_type'] ) && $_POST['comment_type'] !== $comment->comment_type ) {
  89          return new WP_Error( 'comment_type_invalid', __( 'The comment type cannot be changed.' ) );
  90      }
  91  
  92      /*
  93       * The capability check above only establishes that the comment may be edited
  94       * where it currently sits, because 'edit_comment' maps to 'edit_post' on the
  95       * comment's current post. Moving the comment to another post has to be
  96       * authorized separately, otherwise a user who can edit a comment on their own
  97       * post could attach it to any post, including posts they can neither read nor
  98       * edit.
  99       */
 100  
 101      $comment_post_id = $comment ? (int) $comment->comment_post_ID : 0;
 102  
 103      if ( $comment && isset( $_POST['comment_post_ID'] ) ) {
 104          $target_post_id = (int) $_POST['comment_post_ID'];
 105  
 106          $_POST['comment_post_ID'] = $target_post_id;
 107  
 108          if ( $target_post_id !== $comment_post_id ) {
 109              $target_post_check = _wp_check_comment_target_post( $comment, $target_post_id );
 110  
 111              if ( is_wp_error( $target_post_check ) ) {
 112                  return $target_post_check;
 113              }
 114  
 115              $comment_post_id = $target_post_id;
 116          }
 117      }
 118  
 119      if ( isset( $_POST['comment_parent'] ) ) {
 120          $comment_parent = (int) $_POST['comment_parent'];
 121  
 122          $_POST['comment_parent'] = $comment_parent;
 123  
 124          if ( $comment && $comment_parent && $comment_parent !== (int) $comment->comment_parent ) {
 125              if ( ! get_option( 'thread_comments' ) ) {
 126                  return new WP_Error( 'comment_parent_invalid', __( 'The comment parent cannot be changed because threaded comments are disabled.' ) );
 127              }
 128  
 129              if ( $comment_parent === $comment_id ) {
 130                  return new WP_Error( 'comment_parent_invalid', __( 'A comment cannot be a reply to itself.' ) );
 131              }
 132  
 133              $parent              = get_comment( $comment_parent );
 134              $parent_status       = $parent ? wp_get_comment_status( $parent ) : false;
 135              $comment_status      = $_POST['comment_approved'] ?? $comment->comment_approved;
 136              $comment_is_approved = in_array( $comment_status, array( 1, '1', 'approve' ), true );
 137  
 138              // The parent must be a comment of the same type, on the same post, and publicly visible if the comment is approved.
 139              if (
 140                  ! $parent
 141                  || (int) $parent->comment_post_ID !== $comment_post_id
 142                  || $parent->comment_type !== $comment->comment_type
 143                  || in_array( $parent_status, array( 'spam', 'trash' ), true )
 144                  || ( $comment_is_approved && 'approved' !== $parent_status )
 145              ) {
 146                  return new WP_Error( 'comment_parent_invalid', __( 'Invalid parent comment.' ) );
 147              }
 148  
 149              // Walk up the new parent's ancestors to prevent creating a threading loop.
 150              $ancestors    = array();
 151              $ancestor     = $parent;
 152              $parent_depth = 1;
 153  
 154              while ( $ancestor && $ancestor->comment_parent && ! isset( $ancestors[ $ancestor->comment_ID ] ) ) {
 155                  if ( (int) $ancestor->comment_parent === $comment_id ) {
 156                      return new WP_Error( 'comment_parent_invalid', __( 'A comment cannot be a reply to one of its own replies.' ) );
 157                  }
 158  
 159                  $ancestors[ $ancestor->comment_ID ] = true;
 160                  ++$parent_depth;
 161  
 162                  $ancestor = get_comment( $ancestor->comment_parent );
 163              }
 164  
 165              $max_thread_depth = (int) get_option( 'thread_comments_depth' );
 166  
 167              if ( $max_thread_depth ) {
 168                  /*
 169                   * The comment's replies move with it, so the whole subtree must stay within
 170                   * the maximum depth. Measure its height one level of replies at a time,
 171                   * stopping as soon as the subtree cannot fit, which also bounds the loop
 172                   * should the stored comment hierarchy contain a cycle.
 173                   */
 174                  $subtree_height = 1;
 175                  $level_ids      = array( $comment_id );
 176  
 177                  while ( $level_ids && $parent_depth + $subtree_height <= $max_thread_depth ) {
 178                      $level_ids = get_comments(
 179                          array(
 180                              'parent__in' => $level_ids,
 181                              'fields'     => 'ids',
 182                              'status'     => 'any',
 183                              'orderby'    => 'none',
 184                          )
 185                      );
 186  
 187                      if ( $level_ids ) {
 188                          ++$subtree_height;
 189                      }
 190                  }
 191  
 192                  if ( $parent_depth + $subtree_height > $max_thread_depth ) {
 193                      return new WP_Error( 'comment_parent_invalid', __( 'The comment cannot be moved there because it or its replies would exceed the maximum threading depth.' ) );
 194                  }
 195              }
 196          }
 197      }
 198  
 199      foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) {
 200          if ( ! empty( $_POST[ 'hidden_' . $timeunit ] ) && $_POST[ 'hidden_' . $timeunit ] !== $_POST[ $timeunit ] ) {
 201              $_POST['edit_date'] = '1';
 202              break;
 203          }
 204      }
 205  
 206      if ( ! empty( $_POST['edit_date'] ) ) {
 207          $aa = $_POST['aa'];
 208          $mm = $_POST['mm'];
 209          $jj = $_POST['jj'];
 210          $hh = $_POST['hh'];
 211          $mn = $_POST['mn'];
 212          $ss = $_POST['ss'];
 213          $jj = ( $jj > 31 ) ? 31 : $jj;
 214          $hh = ( $hh > 23 ) ? $hh - 24 : $hh;
 215          $mn = ( $mn > 59 ) ? $mn - 60 : $mn;
 216          $ss = ( $ss > 59 ) ? $ss - 60 : $ss;
 217  
 218          $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
 219      }
 220  
 221      return wp_update_comment( $_POST, true );
 222  }
 223  
 224  /**
 225   * Checks that the current user may move a comment to another post.
 226   *
 227   * Mirrors the checks applied by the REST API comments controller when the
 228   * parent post of a comment or note is changed.
 229   *
 230   * @since 7.1.5
 231   * @access private
 232   *
 233   * @param WP_Comment $comment The comment being moved.
 234   * @param int        $post_id ID of the post the comment is being moved to.
 235   * @return true|WP_Error True if the comment may be moved to the post, WP_Error otherwise.
 236   */
 237  function _wp_check_comment_target_post( WP_Comment $comment, int $post_id ) {
 238      $post = $post_id ? get_post( $post_id ) : null;
 239  
 240      if ( ! $post ) {
 241          return new WP_Error( 'comment_post_invalid', __( 'Invalid post ID.' ) );
 242      }
 243  
 244      /*
 245       * Notes are editorial content, so they may only be attached to a post the
 246       * user can edit. Any other comment needs either comment moderation rights
 247       * or edit access to the post, matching what 'edit_comment' requires on the
 248       * post the comment is moving away from.
 249       */
 250      $is_note = 'note' === strtolower( $comment->comment_type );
 251  
 252      if ( $is_note ) {
 253          $can_target_post = current_user_can( 'edit_post', $post->ID );
 254      } else {
 255          $can_target_post = current_user_can( 'moderate_comments' ) || current_user_can( 'edit_post', $post->ID );
 256      }
 257  
 258      if ( ! $can_target_post ) {
 259          return new WP_Error( 'comment_post_invalid', __( 'Sorry, you are not allowed to move this comment to that post.' ) );
 260      }
 261  
 262      if ( ! $is_note ) {
 263          return true;
 264      }
 265  
 266      $supports_notes = false;
 267      $supports       = get_all_post_type_supports( $post->post_type );
 268  
 269      if ( isset( $supports['editor'] ) && is_array( $supports['editor'] ) ) {
 270          $supports_notes = array_any(
 271              $supports['editor'],
 272              static function ( $item ) {
 273                  return is_array( $item ) && ! empty( $item['notes'] );
 274              }
 275          );
 276      }
 277  
 278      if ( ! $supports_notes ) {
 279          return new WP_Error( 'comment_post_invalid', __( 'Sorry, this post type does not support notes.' ) );
 280      }
 281  
 282      if ( 'trash' === $post->post_status || ! current_user_can( 'read_post', $post->ID ) ) {
 283          return new WP_Error( 'comment_post_invalid', __( 'Sorry, you are not allowed to move this comment to that post.' ) );
 284      }
 285  
 286      return true;
 287  }
 288  
 289  /**
 290   * Returns a WP_Comment object based on comment ID.
 291   *
 292   * @since 2.0.0
 293   *
 294   * @param int $id ID of comment to retrieve.
 295   * @return WP_Comment|false Comment if found. False on failure.
 296   */
 297  function get_comment_to_edit( $id ) {
 298      $comment = get_comment( $id );
 299      if ( ! $comment ) {
 300          return false;
 301      }
 302  
 303      $comment->comment_ID      = (int) $comment->comment_ID;
 304      $comment->comment_post_ID = (int) $comment->comment_post_ID;
 305  
 306      $comment->comment_content = format_to_edit( $comment->comment_content );
 307      /**
 308       * Filters the comment content before editing.
 309       *
 310       * @since 2.0.0
 311       *
 312       * @param string $comment_content Comment content.
 313       */
 314      $comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
 315  
 316      $comment->comment_author       = format_to_edit( $comment->comment_author );
 317      $comment->comment_author_email = format_to_edit( $comment->comment_author_email );
 318      $comment->comment_author_url   = format_to_edit( $comment->comment_author_url );
 319      $comment->comment_author_url   = esc_url( $comment->comment_author_url );
 320  
 321      return $comment;
 322  }
 323  
 324  /**
 325   * Gets the number of pending comments on a post or posts.
 326   *
 327   * @since 2.3.0
 328   * @since 6.9.0 Exclude the 'note' comment type from the count.
 329   * @since 7.2.0 Exclude the 'reaction' comment type from the count.
 330   *
 331   * @global wpdb $wpdb WordPress database abstraction object.
 332   *
 333   * @param int|int[] $post_id Either a single Post ID or an array of Post IDs
 334   * @return int|int[] Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs
 335   */
 336  function get_pending_comments_num( $post_id ) {
 337      global $wpdb;
 338  
 339      $single = false;
 340      if ( ! is_array( $post_id ) ) {
 341          $post_id_array = (array) $post_id;
 342          $single        = true;
 343      } else {
 344          $post_id_array = $post_id;
 345      }
 346      $post_id_array = array_map( 'intval', $post_id_array );
 347      $post_id_in    = "'" . implode( "', '", $post_id_array ) . "'";
 348  
 349      $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' AND comment_type NOT IN ( 'note', 'reaction' ) GROUP BY comment_post_ID", ARRAY_A );
 350  
 351      if ( $single ) {
 352          if ( empty( $pending ) ) {
 353              return 0;
 354          } else {
 355              return absint( $pending[0]['num_comments'] );
 356          }
 357      }
 358  
 359      $pending_keyed = array();
 360  
 361      // Default to zero pending for all posts in request.
 362      foreach ( $post_id_array as $id ) {
 363          $pending_keyed[ $id ] = 0;
 364      }
 365  
 366      if ( ! empty( $pending ) ) {
 367          foreach ( $pending as $pend ) {
 368              $pending_keyed[ $pend['comment_post_ID'] ] = absint( $pend['num_comments'] );
 369          }
 370      }
 371  
 372      return $pending_keyed;
 373  }
 374  
 375  /**
 376   * Adds avatars to relevant places in admin.
 377   *
 378   * @since 2.5.0
 379   *
 380   * @param string $name User name.
 381   * @return string Avatar with the user name.
 382   */
 383  function floated_admin_avatar( $name ) {
 384      $avatar = get_avatar( get_comment(), 32, 'mystery' );
 385      return "$avatar $name";
 386  }
 387  
 388  /**
 389   * Enqueues comment shortcuts jQuery script.
 390   *
 391   * @since 2.7.0
 392   */
 393  function enqueue_comment_hotkeys_js() {
 394      if ( 'true' === get_user_option( 'comment_shortcuts' ) ) {
 395          wp_enqueue_script( 'jquery-table-hotkeys' );
 396      }
 397  }
 398  
 399  /**
 400   * Displays error message at bottom of comments.
 401   *
 402   * @since 2.5.0
 403   *
 404   * @param string $msg Error Message. Assumed to contain HTML and be sanitized.
 405   * @return never
 406   */
 407  function comment_footer_die( $msg ) {
 408      echo "<div class='wrap'><p>$msg</p></div>";
 409      require_once  ABSPATH . 'wp-admin/admin-footer.php';
 410      die;
 411  }


Generated : Thu Nov 19 08:20:01 2026 Cross-referenced by PHPXref