]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/comment.php
WordPress 4.5
[autoinstalls/wordpress.git] / wp-includes / comment.php
index 9586b8b74547508f287e83b2303c9052e36054b9..92354d6f17c0e99ed4a5f8abec5fcb8358aed4fb 100644 (file)
@@ -231,13 +231,10 @@ function get_comments( $args = '' ) {
  */
 function get_comment_statuses() {
        $status = array(
  */
 function get_comment_statuses() {
        $status = array(
-               'hold'          => __('Unapproved'),
-               /* translators: comment status  */
-               'approve'       => _x('Approved', 'adjective'),
-               /* translators: comment status */
-               'spam'          => _x('Spam', 'adjective'),
-               /* translators: comment status */
-               'trash'         => _x('Trash', 'adjective'),
+               'hold'          => __( 'Unapproved' ),
+               'approve'       => _x( 'Approved', 'comment status' ),
+               'spam'          => _x( 'Spam', 'comment status' ),
+               'trash'         => _x( 'Trash', 'comment status' ),
        );
 
        return $status;
        );
 
        return $status;
@@ -471,6 +468,30 @@ function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value =
        return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
 }
 
        return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
 }
 
+/**
+ * Queues comments for metadata lazy-loading.
+ *
+ * @since 4.5.0
+ *
+ * @param array $comments Array of comment objects.
+ */
+function wp_queue_comments_for_comment_meta_lazyload( $comments ) {
+       // Don't use `wp_list_pluck()` to avoid by-reference manipulation.
+       $comment_ids = array();
+       if ( is_array( $comments ) ) {
+               foreach ( $comments as $comment ) {
+                       if ( $comment instanceof WP_Comment ) {
+                               $comment_ids[] = $comment->comment_ID;
+                       }
+               }
+       }
+
+       if ( $comment_ids ) {
+               $lazyloader = wp_metadata_lazyloader();
+               $lazyloader->queue_objects( 'comment', $comment_ids );
+       }
+}
+
 /**
  * Sets the cookies used to store an unauthenticated commentator's identity. Typically used
  * to recall previous comments by this commentator that are still held in moderation.
 /**
  * Sets the cookies used to store an unauthenticated commentator's identity. Typically used
  * to recall previous comments by this commentator that are still held in moderation.
@@ -950,6 +971,61 @@ function get_page_of_comment( $comment_ID, $args = array() ) {
        return apply_filters( 'get_page_of_comment', (int) $page, $args, $original_args );
 }
 
        return apply_filters( 'get_page_of_comment', (int) $page, $args, $original_args );
 }
 
+/**
+ * Retrieves the maximum character lengths for the comment form fields.
+ *
+ * @since 4.5.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @return array Maximum character length for the comment form fields.
+ */
+function wp_get_comment_fields_max_lengths() {
+       global $wpdb;
+
+       $lengths = array(
+               'comment_author'       => 245,
+               'comment_author_email' => 100,
+               'comment_author_url'   => 200,
+               'comment_content'      => 65525,
+       );
+
+       if ( $wpdb->is_mysql ) {
+               foreach ( $lengths as $column => $length ) {
+                       $col_length = $wpdb->get_col_length( $wpdb->comments, $column );
+                       $max_length = 0;
+
+                       // No point if we can't get the DB column lengths
+                       if ( is_wp_error( $col_length ) ) {
+                               break;
+                       }
+
+                       if ( ! is_array( $col_length ) && (int) $col_length > 0 ) {
+                               $max_length = (int) $col_length;
+                       } elseif ( is_array( $col_length ) && isset( $col_length['length'] ) && intval( $col_length['length'] ) > 0 ) {
+                               $max_length = (int) $col_length['length'];
+
+                               if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) {
+                                       $max_length = $max_length - 10;
+                               }
+                       }
+
+                       if ( $max_length > 0 ) {
+                               $lengths[ $column ] = $max_length;
+                       }
+               }
+       }
+
+       /**
+        * Filters the lengths for the comment form fields.
+        *
+        * @since 4.5.0
+        *
+        * @param array $lengths Associative array `'field_name' => 'maximum length'`.
+        */
+       return apply_filters( 'wp_get_comment_fields_max_lengths', $lengths );
+}
+
 /**
  * Does comment contain blacklisted characters or words.
  *
 /**
  * Does comment contain blacklisted characters or words.
  *
@@ -1476,7 +1552,7 @@ function wp_get_current_commenter() {
  *     @type int        $comment_karma        The karma of the comment. Default 0.
  *     @type int        $comment_parent       ID of this comment's parent, if any. Default 0.
  *     @type int        $comment_post_ID      ID of the post that relates to the comment, if any.
  *     @type int        $comment_karma        The karma of the comment. Default 0.
  *     @type int        $comment_parent       ID of this comment's parent, if any. Default 0.
  *     @type int        $comment_post_ID      ID of the post that relates to the comment, if any.
- *                                            Default empty.
+ *                                            Default 0.
  *     @type string     $comment_type         Comment type. Default empty.
  *     @type array      $comment_meta         Optional. Array of key/value pairs to be stored in commentmeta for the
  *                                            new comment.
  *     @type string     $comment_type         Comment type. Default empty.
  *     @type array      $comment_meta         Optional. Array of key/value pairs to be stored in commentmeta for the
  *                                            new comment.
@@ -1496,7 +1572,7 @@ function wp_insert_comment( $commentdata ) {
        $comment_date     = ! isset( $data['comment_date'] )     ? current_time( 'mysql' )            : $data['comment_date'];
        $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
 
        $comment_date     = ! isset( $data['comment_date'] )     ? current_time( 'mysql' )            : $data['comment_date'];
        $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
 
-       $comment_post_ID  = ! isset( $data['comment_post_ID'] )  ? '' : $data['comment_post_ID'];
+       $comment_post_ID  = ! isset( $data['comment_post_ID'] )  ?  : $data['comment_post_ID'];
        $comment_content  = ! isset( $data['comment_content'] )  ? '' : $data['comment_content'];
        $comment_karma    = ! isset( $data['comment_karma'] )    ? 0  : $data['comment_karma'];
        $comment_approved = ! isset( $data['comment_approved'] ) ? 1  : $data['comment_approved'];
        $comment_content  = ! isset( $data['comment_content'] )  ? '' : $data['comment_content'];
        $comment_karma    = ! isset( $data['comment_karma'] )    ? 0  : $data['comment_karma'];
        $comment_approved = ! isset( $data['comment_approved'] ) ? 1  : $data['comment_approved'];
@@ -1575,7 +1651,7 @@ function wp_filter_comment($commentdata) {
         *
         * @since 1.5.0
         *
         *
         * @since 1.5.0
         *
-        * @param int $comment_agent The comment author's browser user agent.
+        * @param string $comment_agent The comment author's browser user agent.
         */
        $commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
        /** This filter is documented in wp-includes/comment.php */
         */
        $commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
        /** This filter is documented in wp-includes/comment.php */
@@ -1585,7 +1661,7 @@ function wp_filter_comment($commentdata) {
         *
         * @since 1.5.0
         *
         *
         * @since 1.5.0
         *
-        * @param int $comment_content The comment content.
+        * @param string $comment_content The comment content.
         */
        $commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
        /**
         */
        $commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
        /**
@@ -1593,7 +1669,7 @@ function wp_filter_comment($commentdata) {
         *
         * @since 1.5.0
         *
         *
         * @since 1.5.0
         *
-        * @param int $comment_author_ip The comment author's IP.
+        * @param string $comment_author_ip The comment author's IP.
         */
        $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
        /** This filter is documented in wp-includes/comment.php */
         */
        $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
        /** This filter is documented in wp-includes/comment.php */
@@ -1736,11 +1812,13 @@ function wp_new_comment( $commentdata ) {
         * Fires immediately after a comment is inserted into the database.
         *
         * @since 1.2.0
         * Fires immediately after a comment is inserted into the database.
         *
         * @since 1.2.0
+        * @since 4.5.0 The `$commentdata` parameter was added.
         *
         * @param int        $comment_ID       The comment ID.
         * @param int|string $comment_approved 1 if the comment is approved, 0 if not, 'spam' if spam.
         *
         * @param int        $comment_ID       The comment ID.
         * @param int|string $comment_approved 1 if the comment is approved, 0 if not, 'spam' if spam.
+        * @param array      $commentdata      Comment data.
         */
         */
-       do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] );
+       do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'], $commentdata );
 
        return $comment_ID;
 }
 
        return $comment_ID;
 }
@@ -1904,7 +1982,7 @@ function wp_update_comment($commentarr) {
        }
 
        // Make sure that the comment post ID is valid (if specified).
        }
 
        // Make sure that the comment post ID is valid (if specified).
-       if ( isset( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
+       if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
                return 0;
        }
 
                return 0;
        }
 
@@ -1942,7 +2020,7 @@ function wp_update_comment($commentarr) {
 
        $comment_ID = $data['comment_ID'];
        $comment_post_ID = $data['comment_post_ID'];
 
        $comment_ID = $data['comment_ID'];
        $comment_post_ID = $data['comment_post_ID'];
-       $keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id' );
+       $keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id', 'comment_agent', 'comment_author_IP' );
        $data = wp_array_slice_assoc( $data, $keys );
        $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
 
        $data = wp_array_slice_assoc( $data, $keys );
        $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
 
@@ -2006,13 +2084,19 @@ function wp_defer_comment_counting($defer=null) {
  *
  * @staticvar array $_deferred
  *
  *
  * @staticvar array $_deferred
  *
- * @param int $post_id Post ID
- * @param bool $do_deferred Whether to process previously deferred post comment counts
- * @return bool|void True on success, false on failure
+ * @param int|null $post_id     Post ID.
+ * @param bool     $do_deferred Optional. Whether to process previously deferred
+ *                              post comment counts. Default false.
+ * @return bool|void True on success, false on failure or if post with ID does
+ *                   not exist.
  */
 function wp_update_comment_count($post_id, $do_deferred=false) {
        static $_deferred = array();
 
  */
 function wp_update_comment_count($post_id, $do_deferred=false) {
        static $_deferred = array();
 
+       if ( empty( $post_id ) && ! $do_deferred ) {
+               return false;
+       }
+
        if ( $do_deferred ) {
                $_deferred = array_unique($_deferred);
                foreach ( $_deferred as $i => $_post_id ) {
        if ( $do_deferred ) {
                $_deferred = array_unique($_deferred);
                foreach ( $_deferred as $i => $_post_id ) {
@@ -2054,7 +2138,24 @@ function wp_update_comment_count_now($post_id) {
                return false;
 
        $old = (int) $post->comment_count;
                return false;
 
        $old = (int) $post->comment_count;
-       $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
+
+       /**
+        * Filters a post's comment count before it is updated in the database.
+        *
+        * @since 4.5.0
+        *
+        * @param int $new     The new comment count. Default null.
+        * @param int $old     The old comment count.
+        * @param int $post_id Post ID.
+        */
+       $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id );
+
+       if ( is_null( $new ) ) {
+               $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) );
+       } else {
+               $new = (int) $new;
+       }
+
        $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
 
        clean_post_cache( $post );
        $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
 
        clean_post_cache( $post );
@@ -2102,11 +2203,11 @@ function discover_pingback_server_uri( $url, $deprecated = '' ) {
        /** @todo Should use Filter Extension or custom preg_match instead. */
        $parsed_url = parse_url($url);
 
        /** @todo Should use Filter Extension or custom preg_match instead. */
        $parsed_url = parse_url($url);
 
-       if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
+       if ( ! isset( $parsed_url['host'] ) ) // Not a URL. This should never happen.
                return false;
 
        //Do not search for a pingback server on our own uploads
                return false;
 
        //Do not search for a pingback server on our own uploads
-       $uploads_dir = wp_upload_dir();
+       $uploads_dir = wp_get_upload_dir();
        if ( 0 === strpos($url, $uploads_dir['baseurl']) )
                return false;
 
        if ( 0 === strpos($url, $uploads_dir['baseurl']) )
                return false;
 
@@ -2458,15 +2559,24 @@ function xmlrpc_pingback_error( $ixr_error ) {
 //
 
 /**
 //
 
 /**
- * Removes comment ID from the comment cache.
+ * Removes a comment from the object cache.
  *
  * @since 2.3.0
  *
  *
  * @since 2.3.0
  *
- * @param int|array $ids Comment ID or array of comment IDs to remove from cache
+ * @param int|array $ids Comment ID or an array of comment IDs to remove from cache.
  */
 function clean_comment_cache($ids) {
        foreach ( (array) $ids as $id ) {
                wp_cache_delete( $id, 'comment' );
  */
 function clean_comment_cache($ids) {
        foreach ( (array) $ids as $id ) {
                wp_cache_delete( $id, 'comment' );
+
+               /**
+                * Fires immediately after a comment has been removed from the object cache.
+                *
+                * @since 4.5.0
+                *
+                * @param int $id Comment ID.
+                */
+               do_action( 'clean_comment_cache', $id );
        }
 
        wp_cache_set( 'last_changed', microtime(), 'comment' );
        }
 
        wp_cache_set( 'last_changed', microtime(), 'comment' );
@@ -2766,6 +2876,7 @@ function wp_handle_comment_submission( $comment_data ) {
        }
 
        $comment_type = '';
        }
 
        $comment_type = '';
+       $max_lengths = wp_get_comment_fields_max_lengths();
 
        if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
                if ( 6 > strlen( $comment_author_email ) || '' == $comment_author ) {
 
        if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
                if ( 6 > strlen( $comment_author_email ) || '' == $comment_author ) {
@@ -2775,8 +2886,22 @@ function wp_handle_comment_submission( $comment_data ) {
                }
        }
 
                }
        }
 
+       if ( isset( $comment_author ) && $max_lengths['comment_author'] < mb_strlen( $comment_author, '8bit' ) ) {
+               return new WP_Error( 'comment_author_column_length', __( '<strong>ERROR</strong>: your name is too long.' ), 200 );
+       }
+
+       if ( isset( $comment_author_email ) && $max_lengths['comment_author_email'] < strlen( $comment_author_email ) ) {
+               return new WP_Error( 'comment_author_email_column_length', __( '<strong>ERROR</strong>: your email address is too long.' ), 200 );
+       }
+
+       if ( isset( $comment_author_url ) && $max_lengths['comment_author_url'] < strlen( $comment_author_url ) ) {
+               return new WP_Error( 'comment_author_url_column_length', __( '<strong>ERROR</strong>: your url is too long.' ), 200 );
+       }
+
        if ( '' == $comment_content ) {
                return new WP_Error( 'require_valid_comment', __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
        if ( '' == $comment_content ) {
                return new WP_Error( 'require_valid_comment', __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
+       } elseif ( $max_lengths['comment_content'] < mb_strlen( $comment_content, '8bit' ) ) {
+               return new WP_Error( 'comment_content_column_length', __( '<strong>ERROR</strong>: your comment is too long.' ), 200 );
        }
 
        $commentdata = compact(
        }
 
        $commentdata = compact(