diff -r be944660c56a -r 3d72ae0968f4 wp/wp-includes/comment.php --- a/wp/wp-includes/comment.php Wed Sep 21 18:19:35 2022 +0200 +++ b/wp/wp-includes/comment.php Tue Sep 27 16:37:53 2022 +0200 @@ -7,7 +7,7 @@ */ /** - * Check whether a comment passes internal checks to be allowed to add. + * Checks 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 substance, will fail and the function will @@ -149,15 +149,15 @@ } /** - * Retrieve the approved comments for post $post_id. + * Retrieves the approved comments for post $post_id. * * @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 The approved comments, or number of comments if `$count` - * argument is true. + * @return WP_Comment[]|int[]|int The approved comments, or number of comments if `$count` + * argument is true. */ function get_approved_comments( $post_id, $args = array() ) { if ( ! $post_id ) { @@ -229,7 +229,7 @@ } /** - * Retrieve a list of comments. + * Retrieves a list of comments. * * The comment list can be for the blog as a whole or for an individual post. * @@ -237,7 +237,7 @@ * * @param string|array $args Optional. Array or string of arguments. See WP_Comment_Query::__construct() * for information on accepted arguments. Default empty. - * @return int|array List of comments or number of found comments if `$count` argument is true. + * @return WP_Comment[]|int[]|int List of comments or number of found comments if `$count` argument is true. */ function get_comments( $args = '' ) { $query = new WP_Comment_Query; @@ -245,7 +245,7 @@ } /** - * Retrieve all of the WordPress supported comment statuses. + * Retrieves all of the WordPress supported comment statuses. * * Comments have a limited set of valid status values, this provides the comment * status values and descriptions. @@ -310,7 +310,7 @@ } /** - * The date the last comment was modified. + * Retrieves the date the last comment was modified. * * @since 1.5.0 * @since 4.7.0 Replaced caching the modified date in a local static variable @@ -358,15 +358,11 @@ /** * Retrieves the total comment counts for the whole site or a single post. * - * 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. Restrict the comment counts to the given post. Default 0, which indicates that * comment counts for the whole site will be retrieved. - * @return array() { + * @return int[] { * The number of comments keyed by their status. * * @type int $approved The number of approved comments. @@ -379,25 +375,8 @@ * } */ function get_comment_count( $post_id = 0 ) { - global $wpdb; - $post_id = (int) $post_id; - $where = ''; - if ( $post_id > 0 ) { - $where = $wpdb->prepare( 'WHERE comment_post_ID = %d', $post_id ); - } - - $totals = (array) $wpdb->get_results( - " - SELECT comment_approved, COUNT( * ) AS total - FROM {$wpdb->comments} - {$where} - GROUP BY comment_approved - ", - ARRAY_A - ); - $comment_count = array( 'approved' => 0, 'awaiting_moderation' => 0, @@ -408,32 +387,27 @@ 'all' => 0, ); - foreach ( $totals as $row ) { - switch ( $row['comment_approved'] ) { - case 'trash': - $comment_count['trash'] = $row['total']; - break; - case 'post-trashed': - $comment_count['post-trashed'] = $row['total']; - break; - case 'spam': - $comment_count['spam'] = $row['total']; - $comment_count['total_comments'] += $row['total']; - break; - case '1': - $comment_count['approved'] = $row['total']; - $comment_count['total_comments'] += $row['total']; - $comment_count['all'] += $row['total']; - break; - case '0': - $comment_count['awaiting_moderation'] = $row['total']; - $comment_count['total_comments'] += $row['total']; - $comment_count['all'] += $row['total']; - break; - default: - break; - } + $args = array( + 'count' => true, + 'update_comment_meta_cache' => false, + ); + if ( $post_id > 0 ) { + $args['post_id'] = $post_id; } + $mapping = array( + 'approved' => 'approve', + 'awaiting_moderation' => 'hold', + 'spam' => 'spam', + 'trash' => 'trash', + 'post-trashed' => 'post-trashed', + ); + $comment_count = array(); + foreach ( $mapping as $key => $value ) { + $comment_count[ $key ] = get_comments( array_merge( $args, array( 'status' => $value ) ) ); + } + + $comment_count['all'] = $comment_count['approved'] + $comment_count['awaiting_moderation']; + $comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam']; return array_map( 'intval', $comment_count ); } @@ -443,7 +417,7 @@ // /** - * Add meta data field to a comment. + * Adds meta data field to a comment. * * @since 2.9.0 * @@ -461,7 +435,7 @@ } /** - * Remove metadata matching criteria from a comment. + * Removes metadata matching criteria from a comment. * * You can match based on the key, or key and value. Removing based on key and * value, will keep from removing duplicate metadata with the same key. It also @@ -483,7 +457,7 @@ } /** - * Retrieve comment meta field for a comment. + * Retrieves comment meta field for a comment. * * @since 2.9.0 * @@ -505,7 +479,7 @@ } /** - * Update comment meta field based on comment ID. + * Updates comment meta field based on comment ID. * * Use the $prev_value parameter to differentiate between meta fields with the * same key and comment ID. @@ -1003,7 +977,7 @@ } /** - * Calculate the total number of comment pages. + * Calculates the total number of comment pages. * * @since 2.7.0 * @@ -1060,7 +1034,7 @@ } /** - * Calculate what page number a comment will appear on for comment paging. + * Calculates what page number a comment will appear on for comment paging. * * @since 2.7.0 * @@ -1488,7 +1462,7 @@ * @since 1.2.0 * @since 4.9.0 Added the `$comment` parameter. * - * @param int $comment_id The comment ID. + * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The comment to be deleted. */ do_action( 'delete_comment', $comment->comment_ID, $comment ); @@ -1516,7 +1490,7 @@ * @since 2.9.0 * @since 4.9.0 Added the `$comment` parameter. * - * @param int $comment_id The comment ID. + * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The deleted comment. */ do_action( 'deleted_comment', $comment->comment_ID, $comment ); @@ -1562,7 +1536,7 @@ * @since 2.9.0 * @since 4.9.0 Added the `$comment` parameter. * - * @param int $comment_id The comment ID. + * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The comment to be trashed. */ do_action( 'trash_comment', $comment->comment_ID, $comment ); @@ -1579,7 +1553,7 @@ * @since 2.9.0 * @since 4.9.0 Added the `$comment` parameter. * - * @param int $comment_id The comment ID. + * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The trashed comment. */ do_action( 'trashed_comment', $comment->comment_ID, $comment ); @@ -1610,7 +1584,7 @@ * @since 2.9.0 * @since 4.9.0 Added the `$comment` parameter. * - * @param int $comment_id The comment ID. + * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The comment to be untrashed. */ do_action( 'untrash_comment', $comment->comment_ID, $comment ); @@ -1630,7 +1604,7 @@ * @since 2.9.0 * @since 4.9.0 Added the `$comment` parameter. * - * @param int $comment_id The comment ID. + * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The untrashed comment. */ do_action( 'untrashed_comment', $comment->comment_ID, $comment ); @@ -1642,7 +1616,7 @@ } /** - * Marks a comment as Spam + * Marks a comment as Spam. * * @since 2.9.0 * @@ -1690,7 +1664,7 @@ } /** - * Removes a comment from the Spam + * Removes a comment from the Spam. * * @since 2.9.0 * @@ -1709,7 +1683,7 @@ * @since 2.9.0 * @since 4.9.0 Added the `$comment` parameter. * - * @param int $comment_id The comment ID. + * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The comment to be unmarked as spam. */ do_action( 'unspam_comment', $comment->comment_ID, $comment ); @@ -1729,7 +1703,7 @@ * @since 2.9.0 * @since 4.9.0 Added the `$comment` parameter. * - * @param int $comment_id The comment ID. + * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The comment unmarked as spam. */ do_action( 'unspammed_comment', $comment->comment_ID, $comment ); @@ -1741,7 +1715,7 @@ } /** - * The status of a comment by ID. + * Retrieves the status of a comment by comment ID. * * @since 1.0.0 * @@ -1772,7 +1746,7 @@ } /** - * Call hooks for when a comment status transition occurs. + * Calls hooks for when a comment status transition occurs. * * 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 @@ -1826,6 +1800,15 @@ * The dynamic portions of the hook name, `$old_status`, and `$new_status`, * refer to the old and new comment statuses, respectively. * + * Possible hook names include: + * + * - `comment_unapproved_to_approved` + * - `comment_spam_to_approved` + * - `comment_approved_to_unapproved` + * - `comment_spam_to_unapproved` + * - `comment_unapproved_to_spam` + * - `comment_approved_to_spam` + * * @since 2.7.0 * * @param WP_Comment $comment Comment object. @@ -1838,19 +1821,30 @@ * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`, * refer to the new comment status, and the type of comment, respectively. * - * Typical comment types include an empty string (standard comment), 'pingback', - * or 'trackback'. + * Typical comment types include 'comment', 'pingback', or 'trackback'. + * + * Possible hook names include: + * + * - `comment_approved_comment` + * - `comment_approved_pingback` + * - `comment_approved_trackback` + * - `comment_unapproved_comment` + * - `comment_unapproved_pingback` + * - `comment_unapproved_trackback` + * - `comment_spam_comment` + * - `comment_spam_pingback` + * - `comment_spam_trackback` * * @since 2.7.0 * - * @param int $comment_ID The comment ID. + * @param string $comment_ID The comment ID as a numeric string. * @param WP_Comment $comment Comment object. */ do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment ); } /** - * Clear the lastcommentmodified cached value when a comment status is changed. + * Clears the lastcommentmodified cached value when a comment status is changed. * * Deletes the lastcommentmodified cache key when a comment enters or leaves * 'approved' status. @@ -1863,14 +1857,16 @@ */ function _clear_modified_cache_on_transition_comment_status( $new_status, $old_status ) { if ( 'approved' === $new_status || 'approved' === $old_status ) { + $data = array(); foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { - wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' ); + $data[] = "lastcommentmodified:$timezone"; } + wp_cache_delete_multiple( $data, 'timeinfo' ); } } /** - * Get current commenter's name, email, and URL. + * Gets current commenter's name, email, and URL. * * Expects cookies content to already be sanitized. User of this function might * wish to recheck the returned array for validity. @@ -1922,7 +1918,7 @@ } /** - * Get unapproved comment author's email. + * Gets unapproved comment author's email. * * Used to allow the commenter to see their pending comment. * @@ -2025,9 +2021,11 @@ if ( 1 == $comment_approved ) { wp_update_comment_count( $comment_post_ID ); + $data = array(); foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { - wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' ); + $data[] = "lastcommentmodified:$timezone"; } + wp_cache_delete_multiple( $data, 'timeinfo' ); } clean_comment_cache( $id ); @@ -2119,7 +2117,7 @@ } /** - * Whether a comment should be blocked because of comment flood. + * Determines whether a comment should be blocked because of comment flood. * * @since 2.1.0 * @@ -2289,7 +2287,7 @@ } /** - * Send a comment moderation notification to the comment moderator. + * Sends a comment moderation notification to the comment moderator. * * @since 4.4.0 * @@ -2313,7 +2311,7 @@ } /** - * Send a notification of a new comment to the post author. + * Sends a notification of a new comment to the post author. * * @since 4.4.0 * @@ -2413,7 +2411,7 @@ * * @since 1.5.0 * - * @param int $comment_id Comment ID. + * @param string $comment_id Comment ID as a numeric string. * @param string $comment_status Current comment status. Possible values include * 'hold', '0', 'approve', '1', 'spam', and 'trash'. */ @@ -2569,7 +2567,7 @@ } /** - * Whether to defer comment counting. + * Determines whether to defer comment counting. * * When setting $defer to true, all post comment counts will not be updated * until $defer is set to false. When $defer is set to false, then all @@ -2804,7 +2802,7 @@ } /** - * Perform all pingbacks, enclosures, trackbacks, and send to pingback services. + * Performs all pingbacks, enclosures, trackbacks, and sends to pingback services. * * @since 2.1.0 * @since 5.6.0 Introduced `do_all_pings` action hook for individual services. @@ -2819,7 +2817,7 @@ } /** - * Perform all pingbacks. + * Performs all pingbacks. * * @since 5.6.0 */ @@ -2841,7 +2839,7 @@ } /** - * Perform all enclosures. + * Performs all enclosures. * * @since 5.6.0 */ @@ -2863,7 +2861,7 @@ } /** - * Perform all trackbacks. + * Performs all trackbacks. * * @since 5.6.0 */ @@ -2885,7 +2883,7 @@ } /** - * Perform trackbacks. + * Performs trackbacks. * * @since 1.5.0 * @since 4.7.0 `$post_id` can be a WP_Post object. @@ -3073,7 +3071,7 @@ } /** - * Check whether blog is public before returning sites. + * Checks whether blog is public before returning sites. * * @since 2.1.0 * @@ -3089,7 +3087,7 @@ } /** - * Send a Trackback. + * Sends a Trackback. * * Updates database when sending trackback to prevent duplicates. * @@ -3130,7 +3128,7 @@ } /** - * Send a pingback. + * Sends a pingback. * * @since 1.2.0 * @@ -3155,7 +3153,7 @@ } /** - * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI + * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI. * * @since 3.5.1 * @@ -3200,9 +3198,9 @@ * @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' ); - + $comment_ids = (array) $ids; + wp_cache_delete_multiple( $comment_ids, 'comment' ); + foreach ( $comment_ids as $id ) { /** * Fires immediately after a comment has been removed from the object cache. * @@ -3230,9 +3228,11 @@ * @param bool $update_meta_cache Whether to update commentmeta cache. Default true. */ function update_comment_cache( $comments, $update_meta_cache = true ) { + $data = array(); foreach ( (array) $comments as $comment ) { - wp_cache_add( $comment->comment_ID, $comment, 'comment' ); + $data[ $comment->comment_ID ] = $comment; } + wp_cache_add_multiple( $data, 'comment' ); if ( $update_meta_cache ) { // Avoid `wp_list_pluck()` in case `$comments` is passed by reference. @@ -3272,7 +3272,7 @@ // /** - * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts. + * Closes comments on old posts on the fly, without any extra DB queries. Hooked to the_posts. * * @since 2.7.0 * @access private @@ -3312,7 +3312,7 @@ } /** - * Close comments on an old post. Hooked to comments_open and pings_open. + * Closes comments on an old post. Hooked to comments_open and pings_open. * * @since 2.7.0 * @access private @@ -3529,7 +3529,7 @@ 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.' ), 200 ); } elseif ( ! is_email( $comment_author_email ) ) { return new WP_Error( 'require_valid_email', __( 'Error: Please enter a valid email address.' ), 200 ); }