[ 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   *
  51   * @return int|WP_Error The value 1 if the comment was updated, 0 if not updated.
  52   *                      A WP_Error object on failure.
  53   */
  54  function edit_comment() {
  55      if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) ) {
  56          wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) );
  57      }
  58  
  59      if ( isset( $_POST['newcomment_author'] ) ) {
  60          $_POST['comment_author'] = $_POST['newcomment_author'];
  61      }
  62      if ( isset( $_POST['newcomment_author_email'] ) ) {
  63          $_POST['comment_author_email'] = $_POST['newcomment_author_email'];
  64      }
  65      if ( isset( $_POST['newcomment_author_url'] ) ) {
  66          $_POST['comment_author_url'] = $_POST['newcomment_author_url'];
  67      }
  68      if ( isset( $_POST['comment_status'] ) ) {
  69          $_POST['comment_approved'] = $_POST['comment_status'];
  70      }
  71      if ( isset( $_POST['content'] ) ) {
  72          $_POST['comment_content'] = $_POST['content'];
  73      }
  74      if ( isset( $_POST['comment_ID'] ) ) {
  75          $_POST['comment_ID'] = (int) $_POST['comment_ID'];
  76      }
  77  
  78      if ( isset( $_POST['comment_parent'] ) ) {
  79          $comment_id     = (int) $_POST['comment_ID'];
  80          $comment_parent = (int) $_POST['comment_parent'];
  81  
  82          $_POST['comment_parent'] = $comment_parent;
  83  
  84          $comment = get_comment( $comment_id );
  85  
  86          if ( $comment && $comment_parent && $comment_parent !== (int) $comment->comment_parent ) {
  87              if ( ! get_option( 'thread_comments' ) ) {
  88                  return new WP_Error( 'comment_parent_invalid', __( 'The comment parent cannot be changed because threaded comments are disabled.' ) );
  89              }
  90  
  91              if ( $comment_parent === $comment_id ) {
  92                  return new WP_Error( 'comment_parent_invalid', __( 'A comment cannot be a reply to itself.' ) );
  93              }
  94  
  95              $parent = get_comment( $comment_parent );
  96  
  97              // The parent must be a comment of the same type, on the same post, and not in the Trash or marked as spam.
  98              if (
  99                  ! $parent
 100                  || (int) $parent->comment_post_ID !== (int) $comment->comment_post_ID
 101                  || $parent->comment_type !== $comment->comment_type
 102                  || in_array( wp_get_comment_status( $parent ), array( 'spam', 'trash' ), true )
 103              ) {
 104                  return new WP_Error( 'comment_parent_invalid', __( 'Invalid parent comment.' ) );
 105              }
 106  
 107              // Walk up the new parent's ancestors to prevent creating a threading loop.
 108              $ancestors    = array();
 109              $ancestor     = $parent;
 110              $parent_depth = 1;
 111  
 112              while ( $ancestor && $ancestor->comment_parent && ! isset( $ancestors[ $ancestor->comment_ID ] ) ) {
 113                  if ( (int) $ancestor->comment_parent === $comment_id ) {
 114                      return new WP_Error( 'comment_parent_invalid', __( 'A comment cannot be a reply to one of its own replies.' ) );
 115                  }
 116  
 117                  $ancestors[ $ancestor->comment_ID ] = true;
 118                  ++$parent_depth;
 119  
 120                  $ancestor = get_comment( $ancestor->comment_parent );
 121              }
 122  
 123              $max_thread_depth = (int) get_option( 'thread_comments_depth' );
 124  
 125              if ( $max_thread_depth ) {
 126                  /*
 127                   * The comment's replies move with it, so the whole subtree must stay within
 128                   * the maximum depth. Measure its height one level of replies at a time,
 129                   * stopping as soon as the subtree cannot fit, which also bounds the loop
 130                   * should the stored comment hierarchy contain a cycle.
 131                   */
 132                  $subtree_height = 1;
 133                  $level_ids      = array( $comment_id );
 134  
 135                  while ( $level_ids && $parent_depth + $subtree_height <= $max_thread_depth ) {
 136                      $level_ids = get_comments(
 137                          array(
 138                              'parent__in' => $level_ids,
 139                              'fields'     => 'ids',
 140                              'status'     => 'any',
 141                              'orderby'    => 'none',
 142                          )
 143                      );
 144  
 145                      if ( $level_ids ) {
 146                          ++$subtree_height;
 147                      }
 148                  }
 149  
 150                  if ( $parent_depth + $subtree_height > $max_thread_depth ) {
 151                      return new WP_Error( 'comment_parent_invalid', __( 'The comment cannot be moved there because it or its replies would exceed the maximum threading depth.' ) );
 152                  }
 153              }
 154          }
 155      }
 156  
 157      foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) {
 158          if ( ! empty( $_POST[ 'hidden_' . $timeunit ] ) && $_POST[ 'hidden_' . $timeunit ] !== $_POST[ $timeunit ] ) {
 159              $_POST['edit_date'] = '1';
 160              break;
 161          }
 162      }
 163  
 164      if ( ! empty( $_POST['edit_date'] ) ) {
 165          $aa = $_POST['aa'];
 166          $mm = $_POST['mm'];
 167          $jj = $_POST['jj'];
 168          $hh = $_POST['hh'];
 169          $mn = $_POST['mn'];
 170          $ss = $_POST['ss'];
 171          $jj = ( $jj > 31 ) ? 31 : $jj;
 172          $hh = ( $hh > 23 ) ? $hh - 24 : $hh;
 173          $mn = ( $mn > 59 ) ? $mn - 60 : $mn;
 174          $ss = ( $ss > 59 ) ? $ss - 60 : $ss;
 175  
 176          $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
 177      }
 178  
 179      return wp_update_comment( $_POST, true );
 180  }
 181  
 182  /**
 183   * Returns a WP_Comment object based on comment ID.
 184   *
 185   * @since 2.0.0
 186   *
 187   * @param int $id ID of comment to retrieve.
 188   * @return WP_Comment|false Comment if found. False on failure.
 189   */
 190  function get_comment_to_edit( $id ) {
 191      $comment = get_comment( $id );
 192      if ( ! $comment ) {
 193          return false;
 194      }
 195  
 196      $comment->comment_ID      = (int) $comment->comment_ID;
 197      $comment->comment_post_ID = (int) $comment->comment_post_ID;
 198  
 199      $comment->comment_content = format_to_edit( $comment->comment_content );
 200      /**
 201       * Filters the comment content before editing.
 202       *
 203       * @since 2.0.0
 204       *
 205       * @param string $comment_content Comment content.
 206       */
 207      $comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
 208  
 209      $comment->comment_author       = format_to_edit( $comment->comment_author );
 210      $comment->comment_author_email = format_to_edit( $comment->comment_author_email );
 211      $comment->comment_author_url   = format_to_edit( $comment->comment_author_url );
 212      $comment->comment_author_url   = esc_url( $comment->comment_author_url );
 213  
 214      return $comment;
 215  }
 216  
 217  /**
 218   * Gets the number of pending comments on a post or posts.
 219   *
 220   * @since 2.3.0
 221   * @since 6.9.0 Exclude the 'note' comment type from the count.
 222   *
 223   * @global wpdb $wpdb WordPress database abstraction object.
 224   *
 225   * @param int|int[] $post_id Either a single Post ID or an array of Post IDs
 226   * @return int|int[] Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs
 227   */
 228  function get_pending_comments_num( $post_id ) {
 229      global $wpdb;
 230  
 231      $single = false;
 232      if ( ! is_array( $post_id ) ) {
 233          $post_id_array = (array) $post_id;
 234          $single        = true;
 235      } else {
 236          $post_id_array = $post_id;
 237      }
 238      $post_id_array = array_map( 'intval', $post_id_array );
 239      $post_id_in    = "'" . implode( "', '", $post_id_array ) . "'";
 240  
 241      $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 != 'note' GROUP BY comment_post_ID", ARRAY_A );
 242  
 243      if ( $single ) {
 244          if ( empty( $pending ) ) {
 245              return 0;
 246          } else {
 247              return absint( $pending[0]['num_comments'] );
 248          }
 249      }
 250  
 251      $pending_keyed = array();
 252  
 253      // Default to zero pending for all posts in request.
 254      foreach ( $post_id_array as $id ) {
 255          $pending_keyed[ $id ] = 0;
 256      }
 257  
 258      if ( ! empty( $pending ) ) {
 259          foreach ( $pending as $pend ) {
 260              $pending_keyed[ $pend['comment_post_ID'] ] = absint( $pend['num_comments'] );
 261          }
 262      }
 263  
 264      return $pending_keyed;
 265  }
 266  
 267  /**
 268   * Adds avatars to relevant places in admin.
 269   *
 270   * @since 2.5.0
 271   *
 272   * @param string $name User name.
 273   * @return string Avatar with the user name.
 274   */
 275  function floated_admin_avatar( $name ) {
 276      $avatar = get_avatar( get_comment(), 32, 'mystery' );
 277      return "$avatar $name";
 278  }
 279  
 280  /**
 281   * Enqueues comment shortcuts jQuery script.
 282   *
 283   * @since 2.7.0
 284   */
 285  function enqueue_comment_hotkeys_js() {
 286      if ( 'true' === get_user_option( 'comment_shortcuts' ) ) {
 287          wp_enqueue_script( 'jquery-table-hotkeys' );
 288      }
 289  }
 290  
 291  /**
 292   * Displays error message at bottom of comments.
 293   *
 294   * @since 2.5.0
 295   *
 296   * @param string $msg Error Message. Assumed to contain HTML and be sanitized.
 297   */
 298  function comment_footer_die( $msg ) {
 299      echo "<div class='wrap'><p>$msg</p></div>";
 300      require_once  ABSPATH . 'wp-admin/admin-footer.php';
 301      die;
 302  }


Generated : Sat Jul 11 08:20:14 2026 Cross-referenced by PHPXref