diff -r 3d4e9c994f10 -r a86126ab1dd4 wp/wp-includes/comment.php --- a/wp/wp-includes/comment.php Tue Oct 22 16:11:46 2019 +0200 +++ b/wp/wp-includes/comment.php Tue Dec 15 13:49:49 2020 +0100 @@ -10,15 +10,15 @@ * Check whether a comment passes internal checks to be allowed to add. * * If manual comment moderation is set in the administration, then all checks, - * regardless of their type and whitelist, will fail and the function will + * regardless of their type and substance, will fail and the function will * return false. * * If the number of links exceeds the amount in the administration, then the - * check fails. If any of the parameter contents match the blacklist of words, + * check fails. If any of the parameter contents contain any disallowed words, * then the check fails. * * If the comment author was approved before, then the comment is automatically - * whitelisted. + * approved. * * If all checks pass, the function will return true. * @@ -48,7 +48,8 @@ $comment = apply_filters( 'comment_text', $comment, null, array() ); // Check for the number of external links if a max allowed number is set. - if ( $max_links = get_option( 'comment_max_links' ) ) { + $max_links = get_option( 'comment_max_links' ); + if ( $max_links ) { $num_links = preg_match_all( '/]*href/i', $comment, $out ); /** @@ -125,8 +126,8 @@ * as well as whether there are any moderation keywords (if set) present in the author * email address. If both checks pass, return true. Otherwise, return false. */ - if ( 1 == get_option( 'comment_whitelist' ) ) { - if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) { + if ( 1 == get_option( 'comment_previously_approved' ) ) { + if ( 'trackback' !== $comment_type && 'pingback' !== $comment_type && '' !== $author && '' !== $email ) { $comment_user = get_user_by( 'email', wp_unslash( $email ) ); if ( ! empty( $comment_user->ID ) ) { $ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE user_id = %d AND comment_approved = '1' LIMIT 1", $comment_user->ID ) ); @@ -153,25 +154,25 @@ * @since 2.0.0 * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query. * - * @param int $post_id The ID of the post. - * @param array $args Optional. See WP_Comment_Query::__construct() for information on accepted arguments. - * @return int|array $comments The approved comments, or number of comments if `$count` - * argument is true. + * @param int $post_id The ID of the post. + * @param array $args Optional. See WP_Comment_Query::__construct() for information on accepted arguments. + * @return int|array The approved comments, or number of comments if `$count` + * argument is true. */ function get_approved_comments( $post_id, $args = array() ) { if ( ! $post_id ) { return array(); } - $defaults = array( + $defaults = array( 'status' => 1, 'post_id' => $post_id, 'order' => 'ASC', ); - $r = wp_parse_args( $args, $defaults ); + $parsed_args = wp_parse_args( $args, $defaults ); $query = new WP_Comment_Query; - return $query->query( $r ); + return $query->query( $parsed_args ); } /** @@ -183,11 +184,12 @@ * * @since 2.0.0 * - * @global WP_Comment $comment + * @global WP_Comment $comment Global comment object. * * @param WP_Comment|string|int $comment Comment to retrieve. - * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to - * a WP_Comment object, an associative array, or a numeric array, respectively. Default OBJECT. + * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which + * correspond to a WP_Comment object, an associative array, or a numeric array, + * respectively. Default OBJECT. * @return WP_Comment|array|null Depends on $output value. */ function get_comment( &$comment = null, $output = OBJECT ) { @@ -212,15 +214,15 @@ * * @since 2.3.0 * - * @param mixed $_comment Comment data. + * @param WP_Comment $_comment Comment data. */ $_comment = apply_filters( 'get_comment', $_comment ); - if ( $output == OBJECT ) { + if ( OBJECT == $output ) { return $_comment; - } elseif ( $output == ARRAY_A ) { + } elseif ( ARRAY_A == $output ) { return $_comment->to_array(); - } elseif ( $output == ARRAY_N ) { + } elseif ( ARRAY_N == $output ) { return array_values( $_comment->to_array() ); } return $_comment; @@ -250,7 +252,7 @@ * * @since 2.7.0 * - * @return array List of comment statuses. + * @return string[] List of comment status labels keyed by status. */ function get_comment_statuses() { $status = array( @@ -282,6 +284,7 @@ default: $supports = 'comments'; $option = 'comment'; + break; } // Set the status. @@ -337,7 +340,7 @@ $comment_modified_date = $wpdb->get_var( "SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" ); break; case 'server': - $add_seconds_server = date( 'Z' ); + $add_seconds_server = gmdate( 'Z' ); $comment_modified_date = $wpdb->get_var( $wpdb->prepare( "SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server ) ); break; @@ -353,17 +356,27 @@ } /** - * The amount of comments in a post or total comments. + * Retrieves the total comment counts for the whole site or a single post. * - * A lot like wp_count_comments(), in that they both return comment stats (albeit with different types). - * The wp_count_comments() actually caches, but this function does not. + * Unlike wp_count_comments(), this function always returns the live comment counts without caching. * * @since 2.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * - * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide. - * @return array The amount of spam, approved, awaiting moderation, and total comments. + * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that + * comment counts for the whole site will be retrieved. + * @return array() { + * The number of comments keyed by their status. + * + * @type int $approved The number of approved comments. + * @type int $awaiting_moderation The number of comments awaiting moderation (a.k.a. pending). + * @type int $spam The number of spam comments. + * @type int $trash The number of trashed comments. + * @type int $post-trashed The number of comments for posts that are in the trash. + * @type int $total_comments The total number of non-trashed comments, including spam. + * @type int $all The total number of pending or approved comments. + * } */ function get_comment_count( $post_id = 0 ) { global $wpdb; @@ -422,23 +435,25 @@ } } - return $comment_count; + return array_map( 'intval', $comment_count ); } // -// Comment meta functions +// Comment meta functions. // /** * Add meta data field to a comment. * * @since 2.9.0 - * @link https://codex.wordpress.org/Function_Reference/add_comment_meta + * + * @link https://developer.wordpress.org/reference/functions/add_comment_meta/ * - * @param int $comment_id Comment ID. - * @param string $meta_key Metadata name. - * @param mixed $meta_value Metadata value. - * @param bool $unique Optional, default is false. Whether the same key should not be added. + * @param int $comment_id Comment ID. + * @param string $meta_key Metadata name. + * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. + * @param bool $unique Optional. Whether the same key should not be added. + * Default false. * @return int|bool Meta ID on success, false on failure. */ function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) { @@ -453,11 +468,14 @@ * allows removing all metadata matching key, if needed. * * @since 2.9.0 - * @link https://codex.wordpress.org/Function_Reference/delete_comment_meta + * + * @link https://developer.wordpress.org/reference/functions/delete_comment_meta/ * - * @param int $comment_id comment ID - * @param string $meta_key Metadata name. - * @param mixed $meta_value Optional. Metadata value. + * @param int $comment_id Comment ID. + * @param string $meta_key Metadata name. + * @param mixed $meta_value Optional. Metadata value. If provided, + * rows will only be removed that match the value. + * Must be serializable if non-scalar. Default empty. * @return bool True on success, false on failure. */ function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) { @@ -468,13 +486,17 @@ * Retrieve comment meta field for a comment. * * @since 2.9.0 - * @link https://codex.wordpress.org/Function_Reference/get_comment_meta + * + * @link https://developer.wordpress.org/reference/functions/get_comment_meta/ * - * @param int $comment_id Comment ID. - * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys. - * @param bool $single Whether to return a single value. - * @return mixed Will be an array if $single is false. Will be value of meta data field if $single - * is true. + * @param int $comment_id Comment ID. + * @param string $key Optional. The meta key to retrieve. By default, + * returns data for all keys. + * @param bool $single Optional. Whether to return a single value. + * This parameter has no effect if $key is not specified. + * Default false. + * @return mixed An array if $single is false. The value of meta data field + * if $single is true. False for an invalid $comment_id. */ function get_comment_meta( $comment_id, $key = '', $single = false ) { return get_metadata( 'comment', $comment_id, $key, $single ); @@ -489,13 +511,18 @@ * If the meta field for the comment does not exist, it will be added. * * @since 2.9.0 - * @link https://codex.wordpress.org/Function_Reference/update_comment_meta + * + * @link https://developer.wordpress.org/reference/functions/update_comment_meta/ * - * @param int $comment_id Comment ID. - * @param string $meta_key Metadata key. - * @param mixed $meta_value Metadata value. - * @param mixed $prev_value Optional. Previous value to check before removing. - * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. + * @param int $comment_id Comment ID. + * @param string $meta_key Metadata key. + * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. + * @param mixed $prev_value Optional. Previous value to check before updating. + * If specified, only update existing metadata entries with + * this value. Otherwise, update all entries. Default empty. + * @return int|bool Meta ID if the key didn't exist, true on successful update, + * false on failure or if the value passed to the function + * is the same as the one that is already in the database. */ function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) { return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value ); @@ -534,7 +561,7 @@ * * @param WP_Comment $comment Comment object. * @param WP_User $user Comment author's user object. The user may not exist. - * @param boolean $cookies_consent Optional. Comment author's consent to store cookies. Default true. + * @param bool $cookies_consent Optional. Comment author's consent to store cookies. Default true. */ function wp_set_comment_cookies( $comment, $user, $cookies_consent = true ) { // If the user already exists, or the user opted out of cookies, don't set cookies. @@ -560,7 +587,9 @@ * @param int $seconds Comment cookie lifetime. Default 30000000. */ $comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 ); - $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) ); + + $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) ); + setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); @@ -630,22 +659,23 @@ * Validates whether this comment is allowed to be made. * * @since 2.0.0 - * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function to - * return a WP_Error object instead of dying. + * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function + * to return a WP_Error object instead of dying. + * @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`. * * @global wpdb $wpdb WordPress database abstraction object. * * @param array $commentdata Contains information on the comment. - * @param bool $avoid_die When true, a disallowed comment will result in the function + * @param bool $wp_error When true, a disallowed comment will result in the function * returning a WP_Error object, rather than executing wp_die(). * Default false. - * @return int|string|WP_Error Allowed comments return the approval status (0|1|'spam'). - * If `$avoid_die` is true, disallowed comments return a WP_Error. + * @return int|string|WP_Error Allowed comments return the approval status (0|1|'spam'|'trash'). + * If `$wp_error` is true, disallowed comments return a WP_Error. */ -function wp_allow_comment( $commentdata, $avoid_die = false ) { +function wp_allow_comment( $commentdata, $wp_error = false ) { global $wpdb; - // Simple duplicate check + // Simple duplicate check. // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content) $dupe = $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ", @@ -697,7 +727,7 @@ */ $comment_duplicate_message = apply_filters( 'comment_duplicate_message', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ) ); - if ( true === $avoid_die ) { + if ( $wp_error ) { return new WP_Error( 'comment_duplicate', $comment_duplicate_message, 409 ); } else { if ( wp_doing_ajax() ) { @@ -715,19 +745,20 @@ * * @since 2.3.0 * @since 4.7.0 The `$avoid_die` parameter was added. + * @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`. * * @param string $comment_author_IP Comment author's IP address. * @param string $comment_author_email Comment author's email. * @param string $comment_date_gmt GMT date the comment was posted. - * @param bool $avoid_die Whether to prevent executing wp_die() - * or die() if a comment flood is occurring. + * @param bool $wp_error Whether to return a WP_Error object instead of executing + * wp_die() or die() if a comment flood is occurring. */ do_action( 'check_comment_flood', $commentdata['comment_author_IP'], $commentdata['comment_author_email'], $commentdata['comment_date_gmt'], - $avoid_die + $wp_error ); /** @@ -736,13 +767,14 @@ * The default check is wp_check_comment_flood(). See check_comment_flood_db(). * * @since 4.7.0 + * @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`. * * @param bool $is_flood Is a comment flooding occurring? Default false. * @param string $comment_author_IP Comment author's IP address. * @param string $comment_author_email Comment author's email. * @param string $comment_date_gmt GMT date the comment was posted. - * @param bool $avoid_die Whether to prevent executing wp_die() - * or die() if a comment flood is occurring. + * @param bool $wp_error Whether to return a WP_Error object instead of executing + * wp_die() or die() if a comment flood is occurring. */ $is_flood = apply_filters( 'wp_is_comment_flood', @@ -750,7 +782,7 @@ $commentdata['comment_author_IP'], $commentdata['comment_author_email'], $commentdata['comment_date_gmt'], - $avoid_die + $wp_error ); if ( $is_flood ) { @@ -789,7 +821,7 @@ $approved = 0; } - if ( wp_blacklist_check( + if ( wp_check_comment_disallowed_list( $commentdata['comment_author'], $commentdata['comment_author_email'], $commentdata['comment_author_url'], @@ -805,14 +837,14 @@ * Filters a comment's approval status before it is set. * * @since 2.1.0 - * @since 4.9.0 Returning a WP_Error value from the filter will shortcircuit comment insertion and - * allow skipping further processing. + * @since 4.9.0 Returning a WP_Error value from the filter will short-circuit comment insertion + * and allow skipping further processing. * - * @param bool|string|WP_Error $approved The approval status. Accepts 1, 0, 'spam' or WP_Error. - * @param array $commentdata Comment data. + * @param int|string|WP_Error $approved The approval status. Accepts 1, 0, 'spam', 'trash', + * or WP_Error. + * @param array $commentdata Comment data. */ - $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata ); - return $approved; + return apply_filters( 'pre_comment_approved', $approved, $commentdata ); } /** @@ -844,8 +876,7 @@ * @param string $email Comment author's email address. * @param string $date MySQL time string. * @param bool $avoid_die When true, a disallowed comment will result in the function - * returning a WP_Error object, rather than executing wp_die(). - * Default false. + * returning without executing wp_die() or die(). Default false. * @return bool Whether comment flooding is occurring. */ function wp_check_comment_flood( $is_flood, $ip, $email, $date, $avoid_die = false ) { @@ -857,10 +888,11 @@ return $is_flood; } - // don't throttle admins or moderators + // Don't throttle admins or moderators. if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) { return false; } + $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ); if ( is_user_logged_in() ) { @@ -871,16 +903,19 @@ $check_column = '`comment_author_IP`'; } - $sql = $wpdb->prepare( + $sql = $wpdb->prepare( "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( $check_column = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1", $hour_ago, $user, $email ); + $lasttime = $wpdb->get_var( $sql ); + if ( $lasttime ) { $time_lastcomment = mysql2date( 'U', $lasttime, false ); $time_newcomment = mysql2date( 'U', $date, false ); + /** * Filters the comment flood status. * @@ -891,6 +926,7 @@ * @param int $time_newcomment Timestamp of when the new comment was posted. */ $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment ); + if ( $flood_die ) { /** * Fires before the comment flood message is triggered. @@ -902,7 +938,7 @@ */ do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment ); - if ( true === $avoid_die ) { + if ( $avoid_die ) { return true; } else { /** @@ -941,14 +977,19 @@ 'pingback' => array(), 'pings' => array(), ); - $count = count( $comments ); + + $count = count( $comments ); + for ( $i = 0; $i < $count; $i++ ) { $type = $comments[ $i ]->comment_type; + if ( empty( $type ) ) { $type = 'comment'; } + $comments_by_type[ $type ][] = &$comments[ $i ]; - if ( 'trackback' == $type || 'pingback' == $type ) { + + if ( 'trackback' === $type || 'pingback' === $type ) { $comments_by_type['pings'][] = &$comments[ $i ]; } } @@ -963,9 +1004,9 @@ * * @uses Walker_Comment * - * @global WP_Query $wp_query + * @global WP_Query $wp_query WordPress Query object. * - * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Defaults to $wp_query->comments. + * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Defaults to `$wp_query->comments`. * @param int $per_page Optional. Comments per page. * @param bool $threaded Optional. Control over flat or threaded comments. * @return int Number of comment pages. @@ -1022,14 +1063,16 @@ * * @param int $comment_ID Comment ID. * @param array $args { - * Array of optional arguments. - * @type string $type Limit paginated comments to those matching a given type. Accepts 'comment', - * 'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'. - * Default is 'all'. - * @type int $per_page Per-page count to use when calculating pagination. Defaults to the value of the - * 'comments_per_page' option. - * @type int|string $max_depth If greater than 1, comment page will be determined for the top-level parent of - * `$comment_ID`. Defaults to the value of the 'thread_comments_depth' option. + * Array of optional arguments. + * + * @type string $type Limit paginated comments to those matching a given type. + * Accepts 'comment', 'trackback', 'pingback', 'pings' + * (trackbacks and pingbacks), or 'all'. Default 'all'. + * @type int $per_page Per-page count to use when calculating pagination. + * Defaults to the value of the 'comments_per_page' option. + * @type int|string $max_depth If greater than 1, comment page will be determined + * for the top-level parent `$comment_ID`. + * Defaults to the value of the 'thread_comments_depth' option. * } * * @return int|null Comment page number or null on error. */ @@ -1038,7 +1081,8 @@ $page = null; - if ( ! $comment = get_comment( $comment_ID ) ) { + $comment = get_comment( $comment_ID ); + if ( ! $comment ) { return; } @@ -1080,7 +1124,7 @@ } } - // Find this comment's top level parent if threading is enabled + // Find this comment's top-level parent if threading is enabled. if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) { return get_page_of_comment( $comment->comment_parent, $args ); } @@ -1100,6 +1144,42 @@ ), ); + if ( is_user_logged_in() ) { + $comment_args['include_unapproved'] = array( get_current_user_id() ); + } else { + $unapproved_email = wp_get_unapproved_comment_author_email(); + + if ( $unapproved_email ) { + $comment_args['include_unapproved'] = array( $unapproved_email ); + } + } + + /** + * Filters the arguments used to query comments in get_page_of_comment(). + * + * @since 5.5.0 + * + * @see WP_Comment_Query::__construct() + * + * @param array $comment_args { + * Array of WP_Comment_Query arguments. + * + * @type string $type Limit paginated comments to those matching a given type. + * Accepts 'comment', 'trackback', 'pingback', 'pings' + * (trackbacks and pingbacks), or 'all'. Default 'all'. + * @type int $post_id ID of the post. + * @type string $fields Comment fields to return. + * @type bool $count Whether to return a comment count (true) or array + * of comment objects (false). + * @type string $status Comment status. + * @type int $parent Parent ID of comment to retrieve children of. + * @type array $date_query Date query clauses to limit comments by. See WP_Date_Query. + * @type array $include_unapproved Array of IDs or email addresses whose unapproved comments + * will be included in paginated comments. + * } + */ + $comment_args = apply_filters( 'get_page_of_comment_query_args', $comment_args ); + $comment_query = new WP_Comment_Query(); $older_comment_count = $comment_query->query( $comment_args ); @@ -1107,7 +1187,7 @@ if ( 0 == $older_comment_count ) { $page = 1; - // Divide comments older than this one by comments per page to get this comment's page number + // Divide comments older than this one by comments per page to get this comment's page number. } else { $page = ceil( ( $older_comment_count + 1 ) / $args['per_page'] ); } @@ -1150,7 +1230,7 @@ * * @global wpdb $wpdb WordPress database abstraction object. * - * @return array Maximum character length for the comment form fields. + * @return int[] Array of maximum lengths keyed by field name. */ function wp_get_comment_fields_max_lengths() { global $wpdb; @@ -1167,7 +1247,7 @@ $col_length = $wpdb->get_col_length( $wpdb->comments, $column ); $max_length = 0; - // No point if we can't get the DB column lengths + // No point if we can't get the DB column lengths. if ( is_wp_error( $col_length ) ) { break; } @@ -1193,7 +1273,7 @@ * * @since 4.5.0 * - * @param array $lengths Associative array `'field_name' => 'maximum length'`. + * @param int[] $lengths Array of maximum lengths keyed by field name. */ return apply_filters( 'wp_get_comment_fields_max_lengths', $lengths ); } @@ -1211,28 +1291,28 @@ $max_lengths = wp_get_comment_fields_max_lengths(); if ( isset( $comment_data['comment_author'] ) && mb_strlen( $comment_data['comment_author'], '8bit' ) > $max_lengths['comment_author'] ) { - return new WP_Error( 'comment_author_column_length', __( 'ERROR: your name is too long.' ), 200 ); + return new WP_Error( 'comment_author_column_length', __( 'Error: Your name is too long.' ), 200 ); } if ( isset( $comment_data['comment_author_email'] ) && strlen( $comment_data['comment_author_email'] ) > $max_lengths['comment_author_email'] ) { - return new WP_Error( 'comment_author_email_column_length', __( 'ERROR: your email address is too long.' ), 200 ); + return new WP_Error( 'comment_author_email_column_length', __( 'Error: Your email address is too long.' ), 200 ); } if ( isset( $comment_data['comment_author_url'] ) && strlen( $comment_data['comment_author_url'] ) > $max_lengths['comment_author_url'] ) { - return new WP_Error( 'comment_author_url_column_length', __( 'ERROR: your url is too long.' ), 200 ); + return new WP_Error( 'comment_author_url_column_length', __( 'Error: Your URL is too long.' ), 200 ); } if ( isset( $comment_data['comment_content'] ) && mb_strlen( $comment_data['comment_content'], '8bit' ) > $max_lengths['comment_content'] ) { - return new WP_Error( 'comment_content_column_length', __( 'ERROR: your comment is too long.' ), 200 ); + return new WP_Error( 'comment_content_column_length', __( 'Error: Your comment is too long.' ), 200 ); } return true; } /** - * Does comment contain blacklisted characters or words. + * Checks if a comment contains disallowed characters or words. * - * @since 1.5.0 + * @since 5.5.0 * * @param string $author The author of the comment * @param string $email The email of the comment @@ -1240,13 +1320,14 @@ * @param string $comment The comment content * @param string $user_ip The comment author's IP address * @param string $user_agent The author's browser user agent - * @return bool True if comment contains blacklisted content, false if comment does not + * @return bool True if comment contains disallowed content, false if comment does not */ -function wp_blacklist_check( $author, $email, $url, $comment, $user_ip, $user_agent ) { +function wp_check_comment_disallowed_list( $author, $email, $url, $comment, $user_ip, $user_agent ) { /** - * Fires before the comment is tested for blacklisted characters or words. + * Fires before the comment is tested for disallowed characters or words. * * @since 1.5.0 + * @deprecated 5.5.0 Use {@see 'wp_check_comment_disallowed_list'} instead. * * @param string $author Comment author. * @param string $email Comment author's email. @@ -1255,14 +1336,34 @@ * @param string $user_ip Comment author's IP address. * @param string $user_agent Comment author's browser user agent. */ - do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent ); - - $mod_keys = trim( get_option( 'blacklist_keys' ) ); - if ( '' == $mod_keys ) { - return false; // If moderation keys are empty + do_action_deprecated( + 'wp_blacklist_check', + array( $author, $email, $url, $comment, $user_ip, $user_agent ), + '5.5.0', + 'wp_check_comment_disallowed_list', + __( 'Please consider writing more inclusive code.' ) + ); + + /** + * Fires before the comment is tested for disallowed characters or words. + * + * @since 5.5.0 + * + * @param string $author Comment author. + * @param string $email Comment author's email. + * @param string $url Comment author's URL. + * @param string $comment Comment content. + * @param string $user_ip Comment author's IP address. + * @param string $user_agent Comment author's browser user agent. + */ + do_action( 'wp_check_comment_disallowed_list', $author, $email, $url, $comment, $user_ip, $user_agent ); + + $mod_keys = trim( get_option( 'disallowed_keys' ) ); + if ( '' === $mod_keys ) { + return false; // If moderation keys are empty. } - // Ensure HTML tags are not being used to bypass the blacklist. + // Ensure HTML tags are not being used to bypass the list of disallowed characters and words. $comment_without_html = wp_strip_all_tags( $comment ); $words = explode( "\n", $mod_keys ); @@ -1270,12 +1371,12 @@ foreach ( (array) $words as $word ) { $word = trim( $word ); - // Skip empty lines + // Skip empty lines. if ( empty( $word ) ) { continue; } - // Do some escaping magic so that '#' chars in the - // spam words don't break things: + // Do some escaping magic so that '#' chars + // in the spam words don't break things: $word = preg_quote( $word, '#' ); $pattern = "#$word#i"; @@ -1294,31 +1395,39 @@ } /** - * Retrieve total comments for blog or single post. - * - * The properties of the returned object contain the 'moderated', 'approved', - * and spam comments for either the entire blog or single post. Those properties - * contain the amount of comments that match the status. The 'total_comments' - * property contains the integer of total comments. + * Retrieves the total comment counts for the whole site or a single post. * * The comment stats are cached and then retrieved, if they already exist in the * cache. * + * @see get_comment_count() Which handles fetching the live comment counts. + * * @since 2.5.0 * - * @param int $post_id Optional. Post ID. - * @return object|array Comment stats. + * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that + * comment counts for the whole site will be retrieved. + * @return stdClass { + * The number of comments keyed by their status. + * + * @type int $approved The number of approved comments. + * @type int $moderated The number of comments awaiting moderation (a.k.a. pending). + * @type int $spam The number of spam comments. + * @type int $trash The number of trashed comments. + * @type int $post-trashed The number of comments for posts that are in the trash. + * @type int $total_comments The total number of non-trashed comments, including spam. + * @type int $all The total number of pending or approved comments. + * } */ function wp_count_comments( $post_id = 0 ) { $post_id = (int) $post_id; /** - * Filters the comments count for a given post. + * Filters the comments count for a given post or the whole site. * * @since 2.7.0 * - * @param array $count An empty array. - * @param int $post_id The post ID. + * @param array|stdClass $count An empty array or an object containing comment counts. + * @param int $post_id The post ID. Can be 0 to represent the whole site. */ $filtered = apply_filters( 'wp_count_comments', array(), $post_id ); if ( ! empty( $filtered ) ) { @@ -1343,8 +1452,8 @@ /** * Trashes or deletes a comment. * - * The comment is moved to trash instead of permanently deleted unless trash is - * disabled, item is already in the trash, or $force_delete is true. + * The comment is moved to Trash instead of permanently deleted unless Trash is + * disabled, item is already in the Trash, or $force_delete is true. * * The post comment count will be updated if the comment was approved and has a * post ID available. @@ -1354,16 +1463,17 @@ * @global wpdb $wpdb WordPress database abstraction object. * * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. - * @param bool $force_delete Whether to bypass trash and force deletion. Default is false. + * @param bool $force_delete Whether to bypass Trash and force deletion. Default false. * @return bool True on success, false on failure. */ function wp_delete_comment( $comment_id, $force_delete = false ) { global $wpdb; - if ( ! $comment = get_comment( $comment_id ) ) { + $comment = get_comment( $comment_id ); + if ( ! $comment ) { return false; } - if ( ! $force_delete && EMPTY_TRASH_DAYS && ! in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) ) { + if ( ! $force_delete && EMPTY_TRASH_DAYS && ! in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ), true ) ) { return wp_trash_comment( $comment_id ); } @@ -1385,7 +1495,7 @@ clean_comment_cache( $children ); } - // Delete metadata + // Delete metadata. $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) ); foreach ( $meta_ids as $mid ) { delete_metadata_by_mid( 'comment', $mid ); @@ -1407,7 +1517,7 @@ do_action( 'deleted_comment', $comment->comment_ID, $comment ); $post_id = $comment->comment_post_ID; - if ( $post_id && $comment->comment_approved == 1 ) { + if ( $post_id && 1 == $comment->comment_approved ) { wp_update_comment_count( $post_id ); } @@ -1417,13 +1527,14 @@ do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' ); wp_transition_comment_status( 'delete', $comment->comment_approved, $comment ); + return true; } /** * Moves a comment to the Trash * - * If trash is disabled, comment is permanently deleted. + * If Trash is disabled, comment is permanently deleted. * * @since 2.9.0 * @@ -1435,7 +1546,8 @@ return wp_delete_comment( $comment_id, true ); } - if ( ! $comment = get_comment( $comment_id ) ) { + $comment = get_comment( $comment_id ); + if ( ! $comment ) { return false; } @@ -1466,6 +1578,7 @@ * @param WP_Comment $comment The trashed comment. */ do_action( 'trashed_comment', $comment->comment_ID, $comment ); + return true; } @@ -1505,6 +1618,7 @@ if ( wp_set_comment_status( $comment, $status ) ) { delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); + /** * Fires immediately after a comment is restored from the Trash. * @@ -1515,6 +1629,7 @@ * @param WP_Comment $comment The untrashed comment. */ do_action( 'untrashed_comment', $comment->comment_ID, $comment ); + return true; } @@ -1551,6 +1666,7 @@ delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved ); add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() ); + /** * Fires immediately after a comment is marked as Spam. * @@ -1561,6 +1677,7 @@ * @param WP_Comment $comment The comment marked as spam. */ do_action( 'spammed_comment', $comment->comment_ID, $comment ); + return true; } @@ -1600,6 +1717,7 @@ if ( wp_set_comment_status( $comment, $status ) ) { delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); + /** * Fires immediately after a comment is unmarked as Spam. * @@ -1610,6 +1728,7 @@ * @param WP_Comment $comment The comment unmarked as spam. */ do_action( 'unspammed_comment', $comment->comment_ID, $comment ); + return true; } @@ -1622,7 +1741,7 @@ * @since 1.0.0 * * @param int|WP_Comment $comment_id Comment ID or WP_Comment object - * @return false|string Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure. + * @return string|false Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure. */ function wp_get_comment_status( $comment_id ) { $comment = get_comment( $comment_id ); @@ -1632,15 +1751,15 @@ $approved = $comment->comment_approved; - if ( $approved == null ) { + if ( null == $approved ) { return false; - } elseif ( $approved == '1' ) { + } elseif ( '1' == $approved ) { return 'approved'; - } elseif ( $approved == '0' ) { + } elseif ( '0' == $approved ) { return 'unapproved'; - } elseif ( $approved == 'spam' ) { + } elseif ( 'spam' === $approved ) { return 'spam'; - } elseif ( $approved == 'trash' ) { + } elseif ( 'trash' === $approved ) { return 'trash'; } else { return false; @@ -1652,30 +1771,30 @@ * * Calls hooks for comment status transitions. If the new comment status is not the same * as the previous comment status, then two hooks will be ran, the first is - * {@see 'transition_comment_status'} with new status, old status, and comment data. The - * next action called is {@see comment_$old_status_to_$new_status'}. It has the - * comment data. + * {@see 'transition_comment_status'} with new status, old status, and comment data. + * The next action called is {@see 'comment_$old_status_to_$new_status'}. It has + * the comment data. * - * The final action will run whether or not the comment statuses are the same. The - * action is named {@see 'comment_$new_status_$comment->comment_type'}. + * The final action will run whether or not the comment statuses are the same. + * The action is named {@see 'comment_$new_status_$comment->comment_type'}. * * @since 2.7.0 * - * @param string $new_status New comment status. - * @param string $old_status Previous comment status. - * @param object $comment Comment data. + * @param string $new_status New comment status. + * @param string $old_status Previous comment status. + * @param WP_Comment $comment Comment object. */ function wp_transition_comment_status( $new_status, $old_status, $comment ) { /* - * Translate raw statuses to human readable formats for the hooks. + * Translate raw statuses to human-readable formats for the hooks. * This is not a complete list of comment status, it's only the ones - * that need to be renamed + * that need to be renamed. */ $comment_statuses = array( 0 => 'unapproved', - 'hold' => 'unapproved', // wp_set_comment_status() uses "hold" + 'hold' => 'unapproved', // wp_set_comment_status() uses "hold". 1 => 'approved', - 'approve' => 'approved', // wp_set_comment_status() uses "approve" + 'approve' => 'approved', // wp_set_comment_status() uses "approve". ); if ( isset( $comment_statuses[ $new_status ] ) ) { $new_status = $comment_statuses[ $new_status ]; @@ -1684,7 +1803,7 @@ $old_status = $comment_statuses[ $old_status ]; } - // Call the hooks + // Call the hooks. if ( $new_status != $old_status ) { /** * Fires when the comment status is in transition. @@ -1693,7 +1812,7 @@ * * @param int|string $new_status The new comment status. * @param int|string $old_status The old comment status. - * @param object $comment The comment data. + * @param WP_Comment $comment Comment object. */ do_action( 'transition_comment_status', $new_status, $old_status, $comment ); /** @@ -1755,7 +1874,13 @@ * * @since 2.0.4 * - * @return array Comment author, email, url respectively. + * @return array { + * An array of current commenter variables. + * + * @type string $comment_author The name of the current commenter, or an empty string. + * @type string $comment_author_email The email address of the current commenter, or an empty string. + * @type string $comment_author_url The URL address of the current commenter, or an empty string. + * } */ function wp_get_current_commenter() { // Cookies should already be sanitized. @@ -1783,9 +1908,9 @@ * @param array $comment_author_data { * An array of current commenter variables. * - * @type string $comment_author The name of the author of the comment. Default empty. - * @type string $comment_author_email The email address of the `$comment_author`. Default empty. - * @type string $comment_author_url The URL address of the `$comment_author`. Default empty. + * @type string $comment_author The name of the current commenter, or an empty string. + * @type string $comment_author_email The email address of the current commenter, or an empty string. + * @type string $comment_author_url The URL address of the current commenter, or an empty string. * } */ return apply_filters( 'wp_get_current_commenter', compact( 'comment_author', 'comment_author_email', 'comment_author_url' ) ); @@ -1808,7 +1933,12 @@ $comment = get_comment( $comment_id ); if ( $comment && hash_equals( $_GET['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) { - $commenter_email = $comment->comment_author_email; + // The comment will only be viewable by the comment author for 1 minute. + $comment_preview_expires = strtotime( $comment->comment_date_gmt . '+1 minute' ); + + if ( time() < $comment_preview_expires ) { + $commenter_email = $comment->comment_author_email; + } } } @@ -1824,7 +1954,8 @@ * Inserts a comment into the database. * * @since 2.0.0 - * @since 4.4.0 Introduced `$comment_meta` argument. + * @since 4.4.0 Introduced the `$comment_meta` argument. + * @since 5.5.0 Default value for `$comment_type` argument changed to `comment`. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -1848,7 +1979,7 @@ * @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 0. - * @type string $comment_type Comment type. Default empty. + * @type string $comment_type Comment type. Default 'comment'. * @type array $comment_meta Optional. Array of key/value pairs to be stored in commentmeta for the * new comment. * @type int $user_id ID of the user who submitted the comment. Default 0. @@ -1872,7 +2003,7 @@ $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma']; $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved']; $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent']; - $comment_type = ! isset( $data['comment_type'] ) ? '' : $data['comment_type']; + $comment_type = empty( $data['comment_type'] ) ? 'comment' : $data['comment_type']; $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent']; $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id']; @@ -1884,7 +2015,7 @@ $id = (int) $wpdb->insert_id; - if ( $comment_approved == 1 ) { + if ( 1 == $comment_approved ) { wp_update_comment_count( $comment_post_ID ); foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { @@ -1931,7 +2062,7 @@ function wp_filter_comment( $commentdata ) { if ( isset( $commentdata['user_ID'] ) ) { /** - * Filters the comment author's user id before it is set. + * Filters the comment author's user ID before it is set. * * The first time this filter is evaluated, 'user_ID' is checked * (for back-compat), followed by the standard 'user_id' value. @@ -1985,13 +2116,13 @@ * * @since 2.1.0 * - * @param bool $block Whether plugin has already blocked comment. - * @param int $time_lastcomment Timestamp for last comment. - * @param int $time_newcomment Timestamp for new comment. + * @param bool $block Whether plugin has already blocked comment. + * @param int $time_lastcomment Timestamp for last comment. + * @param int $time_newcomment Timestamp for new comment. * @return bool Whether comment should be blocked. */ function wp_throttle_comment_flood( $block, $time_lastcomment, $time_newcomment ) { - if ( $block ) { // a plugin has already blocked... we'll let that decision stand + if ( $block ) { // A plugin has already blocked... we'll let that decision stand. return $block; } if ( ( $time_newcomment - $time_lastcomment ) < 15 ) { @@ -2014,9 +2145,11 @@ * See {@link https://core.trac.wordpress.org/ticket/9235} * * @since 1.5.0 - * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`. - * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function to - * return a WP_Error object instead of dying. + * @since 4.3.0 Introduced the `comment_agent` and `comment_author_IP` arguments. + * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function + * to return a WP_Error object instead of dying. + * @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`. + * @since 5.5.0 Introduced the `comment_type` argument. * * @see wp_insert_comment() * @global wpdb $wpdb WordPress database abstraction object. @@ -2031,6 +2164,7 @@ * @type string $comment_date The date the comment was submitted. Default is the current time. * @type string $comment_date_gmt The date the comment was submitted in the GMT timezone. * Default is `$comment_date` in the GMT timezone. + * @type string $comment_type Comment type. Default 'comment'. * @type int $comment_parent The ID of this comment's parent, if any. Default 0. * @type int $comment_post_ID The ID of the post that relates to the comment. * @type int $user_id The ID of the user who submitted the comment. Default 0. @@ -2040,15 +2174,16 @@ * @type string $comment_author_IP Comment author IP address in IPv4 format. Default is the value of * 'REMOTE_ADDR' in the `$_SERVER` superglobal sent in the original request. * } - * @param bool $avoid_die Should errors be returned as WP_Error objects instead of + * @param bool $wp_error Should errors be returned as WP_Error objects instead of * executing wp_die()? Default false. * @return int|false|WP_Error The ID of the comment on success, false or WP_Error on failure. */ -function wp_new_comment( $commentdata, $avoid_die = false ) { +function wp_new_comment( $commentdata, $wp_error = false ) { global $wpdb; if ( isset( $commentdata['user_ID'] ) ) { - $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID']; + $commentdata['user_ID'] = (int) $commentdata['user_ID']; + $commentdata['user_id'] = $commentdata['user_ID']; } $prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0; @@ -2064,14 +2199,17 @@ $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) { - $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID']; + $commentdata['user_ID'] = (int) $commentdata['user_ID']; + $commentdata['user_id'] = $commentdata['user_ID']; } elseif ( isset( $commentdata['user_id'] ) ) { $commentdata['user_id'] = (int) $commentdata['user_id']; } $commentdata['comment_parent'] = isset( $commentdata['comment_parent'] ) ? absint( $commentdata['comment_parent'] ) : 0; - $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status( $commentdata['comment_parent'] ) : ''; - $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0; + + $parent_status = ( $commentdata['comment_parent'] > 0 ) ? wp_get_comment_status( $commentdata['comment_parent'] ) : ''; + + $commentdata['comment_parent'] = ( 'approved' === $parent_status || 'unapproved' === $parent_status ) ? $commentdata['comment_parent'] : 0; if ( ! isset( $commentdata['comment_author_IP'] ) ) { $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; @@ -2091,9 +2229,13 @@ $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 ); } + if ( empty( $commentdata['comment_type'] ) ) { + $commentdata['comment_type'] = 'comment'; + } + $commentdata = wp_filter_comment( $commentdata ); - $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die ); + $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $wp_error ); if ( is_wp_error( $commentdata['comment_approved'] ) ) { return $commentdata['comment_approved']; } @@ -2110,7 +2252,7 @@ $commentdata = wp_filter_comment( $commentdata ); - $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die ); + $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $wp_error ); if ( is_wp_error( $commentdata['comment_approved'] ) ) { return $commentdata['comment_approved']; } @@ -2215,7 +2357,7 @@ * * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'. - * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false. + * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default false. * @return bool|WP_Error True on success, false or WP_Error on failure. */ function wp_set_comment_status( $comment_id, $comment_status, $wp_error = false ) { @@ -2245,7 +2387,7 @@ if ( ! $wpdb->update( $wpdb->comments, array( 'comment_approved' => $status ), array( 'comment_ID' => $comment_old->comment_ID ) ) ) { if ( $wp_error ) { - return new WP_Error( 'db_update_error', __( 'Could not update comment status' ), $wpdb->last_error ); + return new WP_Error( 'db_update_error', __( 'Could not update comment status.' ), $wpdb->last_error ); } else { return false; } @@ -2281,24 +2423,37 @@ * * @since 2.0.0 * @since 4.9.0 Add updating comment meta during comment update. + * @since 5.5.0 The `$wp_error` parameter was added. + * @since 5.5.0 The return values for an invalid comment or post ID + * were changed to false instead of 0. * * @global wpdb $wpdb WordPress database abstraction object. * * @param array $commentarr Contains information on the comment. - * @return int Comment was updated if value is 1, or was not updated if value is 0. + * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. + * @return int|false|WP_Error The value 1 if the comment was updated, 0 if not updated. + * False or a WP_Error object on failure. */ -function wp_update_comment( $commentarr ) { +function wp_update_comment( $commentarr, $wp_error = false ) { global $wpdb; - // First, get all of the original fields + // First, get all of the original fields. $comment = get_comment( $commentarr['comment_ID'], ARRAY_A ); if ( empty( $comment ) ) { - return 0; + if ( $wp_error ) { + return new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) ); + } else { + return false; + } } // Make sure that the comment post ID is valid (if specified). if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { - return 0; + if ( $wp_error ) { + return new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) ); + } else { + return false; + } } // Escape data pulled from DB. @@ -2327,9 +2482,9 @@ if ( ! isset( $data['comment_approved'] ) ) { $data['comment_approved'] = 1; - } elseif ( 'hold' == $data['comment_approved'] ) { + } elseif ( 'hold' === $data['comment_approved'] ) { $data['comment_approved'] = 0; - } elseif ( 'approve' == $data['comment_approved'] ) { + } elseif ( 'approve' === $data['comment_approved'] ) { $data['comment_approved'] = 1; } @@ -2342,18 +2497,37 @@ * Note: data being passed to the filter is already unslashed. * * @since 4.7.0 + * @since 5.5.0 Returning a WP_Error value from the filter will short-circuit comment update + * and allow skipping further processing. * - * @param array $data The new, processed comment data. - * @param array $comment The old, unslashed comment data. - * @param array $commentarr The new, raw comment data. + * @param array|WP_Error $data The new, processed comment data, or WP_Error. + * @param array $comment The old, unslashed comment data. + * @param array $commentarr The new, raw comment data. */ $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr ); + // Do not carry on on failure. + if ( is_wp_error( $data ) ) { + if ( $wp_error ) { + return $data; + } else { + return false; + } + } + $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' ) ); + if ( false === $rval ) { + if ( $wp_error ) { + return new WP_Error( 'db_update_error', __( 'Could not update comment in the database.' ), $wpdb->last_error ); + } else { + return false; + } + } + // If metadata is provided, store it. if ( isset( $commentarr['comment_meta'] ) && is_array( $commentarr['comment_meta'] ) ) { foreach ( $commentarr['comment_meta'] as $meta_key => $meta_value ) { @@ -2363,6 +2537,7 @@ clean_comment_cache( $comment_ID ); wp_update_comment_count( $comment_post_ID ); + /** * Fires immediately after a comment is updated in the database. * @@ -2375,8 +2550,11 @@ * @param array $data Comment data. */ do_action( 'edit_comment', $comment_ID, $data ); + $comment = get_comment( $comment_ID ); + wp_transition_comment_status( $comment->comment_approved, $old_status, $comment ); + return $rval; } @@ -2389,7 +2567,6 @@ * updated without having to call wp_update_comment_count() after. * * @since 2.5.0 - * @staticvar bool $_defer * * @param bool $defer * @return bool @@ -2399,7 +2576,7 @@ if ( is_bool( $defer ) ) { $_defer = $defer; - // flush any deferred counts + // Flush any deferred counts. if ( ! $defer ) { wp_update_comment_count( null, true ); } @@ -2420,10 +2597,9 @@ * IDs will be updated along with the current $post_id. * * @since 2.1.0 + * * @see wp_update_comment_count_now() For what could cause a false return value * - * @staticvar array $_deferred - * * @param int|null $post_id Post ID. * @param bool $do_deferred Optional. Whether to process previously deferred * post comment counts. Default false. @@ -2463,7 +2639,7 @@ * @global wpdb $wpdb WordPress database abstraction object. * * @param int $post_id Post ID - * @return bool True on success, false on '0' $post_id or if post with ID does not exist. + * @return bool True on success, false if the post does not exist. */ function wp_update_comment_count_now( $post_id ) { global $wpdb; @@ -2475,7 +2651,8 @@ wp_cache_delete( 'comments-0', 'counts' ); wp_cache_delete( "comments-{$post_id}", 'counts' ); - if ( ! $post = get_post( $post_id ) ) { + $post = get_post( $post_id ); + if ( ! $post ) { return false; } @@ -2486,9 +2663,9 @@ * * @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. + * @param int|null $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 ); @@ -2535,9 +2712,9 @@ * * @since 1.5.0 * - * @param string $url URL to ping. - * @param int $deprecated Not Used. - * @return false|string False on failure, string containing URI on success. + * @param string $url URL to ping. + * @param int $deprecated Not Used. + * @return string|false String containing URI on success, false on failure. */ function discover_pingback_server_uri( $url, $deprecated = '' ) { if ( ! empty( $deprecated ) ) { @@ -2554,7 +2731,7 @@ return false; } - //Do not search for a pingback server on our own uploads + // Do not search for a pingback server on our own uploads. $uploads_dir = wp_get_upload_dir(); if ( 0 === strpos( $url, $uploads_dir['baseurl'] ) ) { return false; @@ -2581,7 +2758,7 @@ return false; } - // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file) + // Now do a GET since we're going to look in the HTML headers (and we're sure it's not a binary file). $response = wp_safe_remote_get( $url, array( @@ -2600,14 +2777,14 @@ $pingback_link_offset_squote = strpos( $contents, $pingback_str_squote ); if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) { $quote = ( $pingback_link_offset_dquote ) ? '"' : '\''; - $pingback_link_offset = ( $quote == '"' ) ? $pingback_link_offset_dquote : $pingback_link_offset_squote; - $pingback_href_pos = @strpos( $contents, 'href=', $pingback_link_offset ); + $pingback_link_offset = ( '"' === $quote ) ? $pingback_link_offset_dquote : $pingback_link_offset_squote; + $pingback_href_pos = strpos( $contents, 'href=', $pingback_link_offset ); $pingback_href_start = $pingback_href_pos + 6; - $pingback_href_end = @strpos( $contents, $quote, $pingback_href_start ); + $pingback_href_end = strpos( $contents, $quote, $pingback_href_start ); $pingback_server_url_len = $pingback_href_end - $pingback_href_start; $pingback_server_url = substr( $contents, $pingback_href_start, $pingback_server_url_len ); - // We may find rel="pingback" but an incomplete pingback URL + // We may find rel="pingback" but an incomplete pingback URL. if ( $pingback_server_url_len > 0 ) { // We got it! return $pingback_server_url; } @@ -2626,27 +2803,55 @@ function do_all_pings() { global $wpdb; - // Do pingbacks - while ( $ping = $wpdb->get_row( "SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1" ) ) { - delete_metadata_by_mid( 'post', $ping->meta_id ); - pingback( $ping->post_content, $ping->ID ); - } - - // Do Enclosures - while ( $enclosure = $wpdb->get_row( "SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1" ) ) { - delete_metadata_by_mid( 'post', $enclosure->meta_id ); - do_enclose( $enclosure->post_content, $enclosure->ID ); + // Do pingbacks. + $pings = get_posts( + array( + 'post_type' => get_post_types(), + 'suppress_filters' => false, + 'nopaging' => true, + 'meta_key' => '_pingme', + 'fields' => 'ids', + ) + ); + + foreach ( $pings as $ping ) { + delete_post_meta( $ping, '_pingme' ); + pingback( null, $ping ); } - // Do Trackbacks - $trackbacks = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'" ); - if ( is_array( $trackbacks ) ) { - foreach ( $trackbacks as $trackback ) { - do_trackbacks( $trackback ); - } + // Do enclosures. + $enclosures = get_posts( + array( + 'post_type' => get_post_types(), + 'suppress_filters' => false, + 'nopaging' => true, + 'meta_key' => '_encloseme', + 'fields' => 'ids', + ) + ); + + foreach ( $enclosures as $enclosure ) { + delete_post_meta( $enclosure, '_encloseme' ); + do_enclose( null, $enclosure ); } - //Do Update Services/Generic Pings + // Do trackbacks. + $trackbacks = get_posts( + array( + 'post_type' => get_post_types(), + 'suppress_filters' => false, + 'nopaging' => true, + 'meta_key' => '_trackbackme', + 'fields' => 'ids', + ) + ); + + foreach ( $trackbacks as $trackback ) { + delete_post_meta( $trackback, '_trackbackme' ); + do_trackbacks( $trackback ); + } + + // Do Update Services/Generic Pings. generic_ping(); } @@ -2692,7 +2897,7 @@ if ( $to_ping ) { foreach ( (array) $to_ping as $tb_ping ) { $tb_ping = trim( $tb_ping ); - if ( ! in_array( $tb_ping, $pinged ) ) { + if ( ! in_array( $tb_ping, $pinged, true ) ) { trackback( $tb_ping, $post_title, $excerpt, $post->ID ); $pinged[] = $tb_ping; } else { @@ -2723,7 +2928,7 @@ $services = explode( "\n", $services ); foreach ( (array) $services as $service ) { $service = trim( $service ); - if ( '' != $service ) { + if ( '' !== $service ) { weblog_ping( $service ); } } @@ -2737,14 +2942,14 @@ * @since 0.71 * @since 4.7.0 `$post_id` can be a WP_Post object. * - * @param string $content Post content to check for links. If empty will retrieve from post. + * @param string $content Post content to check for links. If empty will retrieve from post. * @param int|WP_Post $post_id Post Object or ID. */ function pingback( $content, $post_id ) { - include_once( ABSPATH . WPINC . '/class-IXR.php' ); - include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' ); - - // original code by Mort (http://mort.mine.nu:8080) + include_once ABSPATH . WPINC . '/class-IXR.php'; + include_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php'; + + // Original code by Mort (http://mort.mine.nu:8080). $post_links = array(); $post = get_post( $post_id ); @@ -2758,33 +2963,41 @@ $content = $post->post_content; } - // Step 1 - // Parsing the post, external links (if any) are stored in the $post_links array + /* + * Step 1. + * Parsing the post, external links (if any) are stored in the $post_links array. + */ $post_links_temp = wp_extract_urls( $content ); - // Step 2. - // Walking thru the links array - // first we get rid of links pointing to sites, not to specific files - // Example: - // http://dummy-weblog.org - // http://dummy-weblog.org/ - // http://dummy-weblog.org/post.php - // We don't wanna ping first and second types, even if they have a valid - - foreach ( (array) $post_links_temp as $link_test ) : - if ( ! in_array( $link_test, $pung ) && ( url_to_postid( $link_test ) != $post->ID ) // If we haven't pung it already and it isn't a link to itself - && ! is_local_attachment( $link_test ) ) : // Also, let's never ping local attachments. - if ( $test = @parse_url( $link_test ) ) { + /* + * Step 2. + * Walking through the links array. + * First we get rid of links pointing to sites, not to specific files. + * Example: + * http://dummy-weblog.org + * http://dummy-weblog.org/ + * http://dummy-weblog.org/post.php + * We don't wanna ping first and second types, even if they have a valid . + */ + foreach ( (array) $post_links_temp as $link_test ) { + // If we haven't pung it already and it isn't a link to itself. + if ( ! in_array( $link_test, $pung, true ) && ( url_to_postid( $link_test ) != $post->ID ) + // Also, let's never ping local attachments. + && ! is_local_attachment( $link_test ) + ) { + $test = parse_url( $link_test ); + if ( $test ) { if ( isset( $test['query'] ) ) { $post_links[] = $link_test; - } elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) ) { + } elseif ( isset( $test['path'] ) && ( '/' !== $test['path'] ) && ( '' !== $test['path'] ) ) { $post_links[] = $link_test; } } - endif; - endforeach; + } + } $post_links = array_unique( $post_links ); + /** * Fires just before pinging back links found in a post. * @@ -2800,11 +3013,11 @@ $pingback_server_url = discover_pingback_server_uri( $pagelinkedto ); if ( $pingback_server_url ) { - @ set_time_limit( 60 ); - // Now, the RPC call + set_time_limit( 60 ); + // Now, the RPC call. $pagelinkedfrom = get_permalink( $post ); - // using a timeout of 3 seconds should be enough to cover slow servers + // Using a timeout of 3 seconds should be enough to cover slow servers. $client = new WP_HTTP_IXR_Client( $pingback_server_url ); $client->timeout = 3; /** @@ -2820,10 +3033,10 @@ * @param string $pagelinkedfrom URL of page linked from. */ $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . get_bloginfo( 'version' ), $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom ); - // when set to true, this outputs debug messages by itself + // When set to true, this outputs debug messages by itself. $client->debug = false; - if ( $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto ) || ( isset( $client->error->code ) && 48 == $client->error->code ) ) { // Already registered + if ( $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto ) || ( isset( $client->error->code ) && 48 == $client->error->code ) ) { // Already registered. add_ping( $post, $pagelinkedto ); } } @@ -2856,9 +3069,9 @@ * @global wpdb $wpdb WordPress database abstraction object. * * @param string $trackback_url URL to send trackbacks. - * @param string $title Title of post. - * @param string $excerpt Excerpt of post. - * @param int $ID Post ID. + * @param string $title Title of post. + * @param string $excerpt Excerpt of post. + * @param int $ID Post ID. * @return int|false|void Database query from update. */ function trackback( $trackback_url, $title, $excerpt, $ID ) { @@ -2896,18 +3109,18 @@ * @param string $path Path to send the ping. */ function weblog_ping( $server = '', $path = '' ) { - include_once( ABSPATH . WPINC . '/class-IXR.php' ); - include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' ); - - // using a timeout of 3 seconds should be enough to cover slow servers - $client = new WP_HTTP_IXR_Client( $server, ( ( ! strlen( trim( $path ) ) || ( '/' == $path ) ) ? false : $path ) ); + include_once ABSPATH . WPINC . '/class-IXR.php'; + include_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php'; + + // Using a timeout of 3 seconds should be enough to cover slow servers. + $client = new WP_HTTP_IXR_Client( $server, ( ( ! strlen( trim( $path ) ) || ( '/' === $path ) ) ? false : $path ) ); $client->timeout = 3; $client->useragent .= ' -- WordPress/' . get_bloginfo( 'version' ); - // when set to true, this outputs debug messages by itself + // When set to true, this outputs debug messages by itself. $client->debug = false; $home = trailingslashit( home_url() ); - if ( ! $client->query( 'weblogUpdates.extendedPing', get_option( 'blogname' ), $home, get_bloginfo( 'rss2_url' ) ) ) { // then try a normal ping + if ( ! $client->query( 'weblogUpdates.extendedPing', get_option( 'blogname' ), $home, get_bloginfo( 'rss2_url' ) ) ) { // Then try a normal ping. $client->query( 'weblogUpdates.ping', get_option( 'blogname' ), $home ); } } @@ -2916,6 +3129,7 @@ * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI * * @since 3.5.1 + * * @see wp_http_validate_url() * * @param string $source_uri @@ -2932,20 +3146,21 @@ * which reports that the pingback is already registered. * * @since 3.5.1 + * * @link https://www.hixie.ch/specs/pingback/pingback#TOC3 * * @param IXR_Error $ixr_error * @return IXR_Error */ function xmlrpc_pingback_error( $ixr_error ) { - if ( $ixr_error->code === 48 ) { + if ( 48 === $ixr_error->code ) { return $ixr_error; } return new IXR_Error( 0, '' ); } // -// Cache +// Cache. // /** @@ -3024,14 +3239,14 @@ } // -// Internal +// Internal. // /** * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts. * + * @since 2.7.0 * @access private - * @since 2.7.0 * * @param WP_Post $posts Post data object. * @param WP_Query $query Query object. @@ -3050,7 +3265,7 @@ * @param string[] $post_types An array of post type names. */ $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); - if ( ! in_array( $posts[0]->post_type, $post_types ) ) { + if ( ! in_array( $posts[0]->post_type, $post_types, true ) ) { return $posts; } @@ -3070,11 +3285,11 @@ /** * Close comments on an old post. Hooked to comments_open and pings_open. * + * @since 2.7.0 * @access private - * @since 2.7.0 * - * @param bool $open Comments open or closed - * @param int $post_id Post ID + * @param bool $open Comments open or closed. + * @param int $post_id Post ID. * @return bool $open */ function _close_comments_for_old_post( $open, $post_id ) { @@ -3095,7 +3310,7 @@ /** This filter is documented in wp-includes/comment.php */ $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); - if ( ! in_array( $post->post_type, $post_types ) ) { + if ( ! in_array( $post->post_type, $post_types, true ) ) { return $open; } @@ -3134,8 +3349,13 @@ */ function wp_handle_comment_submission( $comment_data ) { - $comment_post_ID = $comment_parent = $user_ID = 0; - $comment_author = $comment_author_email = $comment_author_url = $comment_content = null; + $comment_post_ID = 0; + $comment_parent = 0; + $user_ID = 0; + $comment_author = null; + $comment_author_email = null; + $comment_author_url = null; + $comment_content = null; if ( isset( $comment_data['comment_post_ID'] ) ) { $comment_post_ID = (int) $comment_data['comment_post_ID']; @@ -3176,7 +3396,7 @@ // get_post_status() will get the parent status for attachments. $status = get_post_status( $post ); - if ( ( 'private' == $status ) && ! current_user_can( 'read_post', $comment_post_ID ) ) { + if ( ( 'private' === $status ) && ! current_user_can( 'read_post', $comment_post_ID ) ) { return new WP_Error( 'comment_id_not_found' ); } @@ -3195,7 +3415,7 @@ return new WP_Error( 'comment_closed', __( 'Sorry, comments are closed for this item.' ), 403 ); - } elseif ( 'trash' == $status ) { + } elseif ( 'trash' === $status ) { /** * Fires when a comment is attempted on a trashed post. @@ -3250,7 +3470,7 @@ } - // If the user is logged in + // If the user is logged in. $user = wp_get_current_user(); if ( $user->exists() ) { if ( empty( $user->display_name ) ) { @@ -3264,8 +3484,8 @@ if ( ! isset( $comment_data['_wp_unfiltered_html_comment'] ) || ! wp_verify_nonce( $comment_data['_wp_unfiltered_html_comment'], 'unfiltered-html-comment_' . $comment_post_ID ) ) { - kses_remove_filters(); // start with a clean slate - kses_init_filters(); // set up the filters + kses_remove_filters(); // Start with a clean slate. + kses_init_filters(); // Set up the filters. remove_filter( 'pre_comment_content', 'wp_filter_post_kses' ); add_filter( 'pre_comment_content', 'wp_filter_kses' ); } @@ -3276,13 +3496,13 @@ } } - $comment_type = ''; + $comment_type = 'comment'; if ( get_option( 'require_name_email' ) && ! $user->exists() ) { if ( '' == $comment_author_email || '' == $comment_author ) { - return new WP_Error( 'require_name_email', __( 'ERROR: please fill the required fields (name, email).' ), 200 ); + return new WP_Error( 'require_name_email', __( 'Error: Please fill the required fields (name, email).' ), 200 ); } elseif ( ! is_email( $comment_author_email ) ) { - return new WP_Error( 'require_valid_email', __( 'ERROR: please enter a valid email address.' ), 200 ); + return new WP_Error( 'require_valid_email', __( 'Error: Please enter a valid email address.' ), 200 ); } } @@ -3307,7 +3527,7 @@ */ $allow_empty_comment = apply_filters( 'allow_empty_comment', false, $commentdata ); if ( '' === $comment_content && ! $allow_empty_comment ) { - return new WP_Error( 'require_valid_comment', __( 'ERROR: please type a comment.' ), 200 ); + return new WP_Error( 'require_valid_comment', __( 'Error: Please type your comment text.' ), 200 ); } $check_max_lengths = wp_check_comment_data_max_lengths( $commentdata ); @@ -3321,7 +3541,7 @@ } if ( ! $comment_id ) { - return new WP_Error( 'comment_save_error', __( 'ERROR: The comment could not be saved. Please try again later.' ), 500 ); + return new WP_Error( 'comment_save_error', __( 'Error: The comment could not be saved. Please try again later.' ), 500 ); } return get_comment( $comment_id ); @@ -3333,7 +3553,7 @@ * @since 4.9.6 * * @param array $exporters An array of personal data exporters. - * @return array $exporters An array of personal data exporters. + * @return array An array of personal data exporters. */ function wp_register_comment_personal_data_exporter( $exporters ) { $exporters['wordpress-comments'] = array( @@ -3351,7 +3571,7 @@ * * @param string $email_address The comment author email address. * @param int $page Comment page. - * @return array $return An array of personal data. + * @return array An array of personal data. */ function wp_comments_personal_data_exporter( $email_address, $page = 1 ) { // Limit us to 500 comments at a time to avoid timing out. @@ -3421,10 +3641,11 @@ } $data_to_export[] = array( - 'group_id' => 'comments', - 'group_label' => __( 'Comments' ), - 'item_id' => "comment-{$comment->comment_ID}", - 'data' => $comment_data_to_export, + 'group_id' => 'comments', + 'group_label' => __( 'Comments' ), + 'group_description' => __( 'User’s comment data.' ), + 'item_id' => "comment-{$comment->comment_ID}", + 'data' => $comment_data_to_export, ); } @@ -3441,8 +3662,8 @@ * * @since 4.9.6 * - * @param array $erasers An array of personal data erasers. - * @return array $erasers An array of personal data erasers. + * @param array $erasers An array of personal data erasers. + * @return array An array of personal data erasers. */ function wp_register_comment_personal_data_eraser( $erasers ) { $erasers['wordpress-comments'] = array( @@ -3458,8 +3679,8 @@ * * @since 4.9.6 * - * @param string $email_address The comment author email address. - * @param int $page Comment page. + * @param string $email_address The comment author email address. + * @param int $page Comment page. * @return array */ function wp_comments_personal_data_eraser( $email_address, $page = 1 ) { @@ -3511,10 +3732,10 @@ * * @since 4.9.6 * - * @param bool|string Whether to apply the comment anonymization (bool). - * Custom prevention message (string). Default true. - * @param WP_Comment $comment WP_Comment object. - * @param array $anonymized_comment Anonymized comment data. + * @param bool|string $anon_message Whether to apply the comment anonymization (bool) or a custom + * message (string). Default true. + * @param WP_Comment $comment WP_Comment object. + * @param array $anonymized_comment Anonymized comment data. */ $anon_message = apply_filters( 'wp_anonymize_comment', true, $comment, $anonymized_comment ); @@ -3522,7 +3743,7 @@ if ( $anon_message && is_string( $anon_message ) ) { $messages[] = esc_html( $anon_message ); } else { - /* translators: %d: Comment ID */ + /* translators: %d: Comment ID. */ $messages[] = sprintf( __( 'Comment %d contains personal data but could not be anonymized.' ), $comment_id ); } @@ -3563,3 +3784,100 @@ function wp_cache_set_comments_last_changed() { wp_cache_set( 'last_changed', microtime(), 'comment' ); } + +/** + * Updates the comment type for a batch of comments. + * + * @since 5.5.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + */ +function _wp_batch_update_comment_type() { + global $wpdb; + + $lock_name = 'update_comment_type.lock'; + + // Try to lock. + $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) ); + + if ( ! $lock_result ) { + $lock_result = get_option( $lock_name ); + + // Bail if we were unable to create a lock, or if the existing lock is still valid. + if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) { + wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' ); + return; + } + } + + // Update the lock, as by this point we've definitely got a lock, just need to fire the actions. + update_option( $lock_name, time() ); + + // Check if there's still an empty comment type. + $empty_comment_type = $wpdb->get_var( + "SELECT comment_ID FROM $wpdb->comments + WHERE comment_type = '' + LIMIT 1" + ); + + // No empty comment type, we're done here. + if ( ! $empty_comment_type ) { + update_option( 'finished_updating_comment_type', true ); + delete_option( $lock_name ); + return; + } + + // Empty comment type found? We'll need to run this script again. + wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' ); + + /** + * Filters the comment batch size for updating the comment type. + * + * @since 5.5.0 + * + * @param int $comment_batch_size The comment batch size. Default 100. + */ + $comment_batch_size = (int) apply_filters( 'wp_update_comment_type_batch_size', 100 ); + + // Get the IDs of the comments to update. + $comment_ids = $wpdb->get_col( + $wpdb->prepare( + "SELECT comment_ID + FROM {$wpdb->comments} + WHERE comment_type = '' + ORDER BY comment_ID DESC + LIMIT %d", + $comment_batch_size + ) + ); + + if ( $comment_ids ) { + $comment_id_list = implode( ',', $comment_ids ); + + // Update the `comment_type` field value to be `comment` for the next batch of comments. + $wpdb->query( + "UPDATE {$wpdb->comments} + SET comment_type = 'comment' + WHERE comment_type = '' + AND comment_ID IN ({$comment_id_list})" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared + ); + + // Make sure to clean the comment cache. + clean_comment_cache( $comment_ids ); + } + + delete_option( $lock_name ); +} + +/** + * In order to avoid the _wp_batch_update_comment_type() job being accidentally removed, + * check that it's still scheduled while we haven't finished updating comment types. + * + * @ignore + * @since 5.5.0 + */ +function _wp_check_for_scheduled_update_comment_type() { + if ( ! get_option( 'finished_updating_comment_type' ) && ! wp_next_scheduled( 'wp_update_comment_type_batch' ) ) { + wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'wp_update_comment_type_batch' ); + } +}