Comments: Ensure the proper comment count and pages for unapproved comments.

Previiously, unapproved comments can alter the comment count, returning incorrect page numbers.

Fixes #8973.

Props GregMulhauser, dd32, ryan, mrmist, hakre, solarissmoke, billerickson, ericlewis, SergeyBiryukov, chriscct7, dossy, lukecavanagh, renggo888, jdorner, matjack1, pento, audrasjb, imath, davidbaumwald, whyisjake.  


Built from https://develop.svn.wordpress.org/trunk@48133


git-svn-id: http://core.svn.wordpress.org/trunk@47902 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
whyisjake 2020-06-23 05:24:10 +00:00
parent 446d06c45d
commit 0c91c23277
3 changed files with 63 additions and 11 deletions

View File

@ -1330,6 +1330,7 @@ function wp_comment_form_unfiltered_html_nonce() {
* Will not try to get the comments if the post has none. * Will not try to get the comments if the post has none.
* *
* @since 1.5.0 * @since 1.5.0
* @since 5.5.0 Removed the need to use the $user_ID global.
* *
* @global WP_Query $wp_query WordPress Query object. * @global WP_Query $wp_query WordPress Query object.
* @global WP_Post $post Global post object. * @global WP_Post $post Global post object.
@ -1337,7 +1338,6 @@ function wp_comment_form_unfiltered_html_nonce() {
* @global int $id * @global int $id
* @global WP_Comment $comment Global comment object. * @global WP_Comment $comment Global comment object.
* @global string $user_login * @global string $user_login
* @global int $user_ID
* @global string $user_identity * @global string $user_identity
* @global bool $overridden_cpage * @global bool $overridden_cpage
* @global bool $withcomments * @global bool $withcomments
@ -1347,7 +1347,7 @@ function wp_comment_form_unfiltered_html_nonce() {
* Default false. * Default false.
*/ */
function comments_template( $file = '/comments.php', $separate_comments = false ) { function comments_template( $file = '/comments.php', $separate_comments = false ) {
global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage; global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage;
if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) { if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
return; return;
@ -1396,14 +1396,9 @@ function comments_template( $file = '/comments.php', $separate_comments = false
$comment_args['hierarchical'] = false; $comment_args['hierarchical'] = false;
} }
if ( $user_ID ) { $include_unapproved = wp_get_include_unapproved_comments_argument();
$comment_args['include_unapproved'] = array( $user_ID ); if ( $include_unapproved ) {
} else { $comment_args['include_unapproved'] = $include_unapproved;
$unapproved_email = wp_get_unapproved_comment_author_email();
if ( $unapproved_email ) {
$comment_args['include_unapproved'] = array( $unapproved_email );
}
} }
$per_page = 0; $per_page = 0;

View File

@ -1132,6 +1132,36 @@ function get_page_of_comment( $comment_ID, $args = array() ) {
), ),
); );
$include_unapproved = wp_get_include_unapproved_comments_argument();
if ( $include_unapproved ) {
$comment_args['include_unapproved'] = $include_unapproved;
}
/**
* 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 is '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(); $comment_query = new WP_Comment_Query();
$older_comment_count = $comment_query->query( $comment_args ); $older_comment_count = $comment_query->query( $comment_args );
@ -1896,6 +1926,33 @@ function wp_get_unapproved_comment_author_email() {
return $commenter_email; return $commenter_email;
} }
/**
* Get include unapproved comments query argument.
*
* Used to include unapproved comments of currrent commenters to
* keep them informed their comments were successfully saved.
*
* @since 5.5.0
*
* @return array The unapproved comments query argument.
*/
function wp_get_include_unapproved_comments_argument() {
$user_id = get_current_user_id();
$include_unapproved = array();
if ( $user_id ) {
$include_unapproved = array( $user_id );
} else {
$unapproved_email = wp_get_unapproved_comment_author_email();
if ( $unapproved_email ) {
$include_unapproved = array( $unapproved_email );
}
}
return $include_unapproved;
}
/** /**
* Inserts a comment into the database. * Inserts a comment into the database.
* *

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.5-alpha-48132'; $wp_version = '5.5-alpha-48133';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.