Make WordPress Core

Changeset 53336


Ignore:
Timestamp:
05/03/2022 02:31:12 PM (2 years ago)
Author:
hellofromTonya
Message:

Editor: Sets 'paged' query arg only when there are comments: build_comment_query_vars_from_block().

A SQL syntax error happened when a post has no comments and "Break comments into pages" is checked in Settings > Discussion. The fix sets the 'paged' query arg only when there are comments. When there are no comments, WP_Comment_Query sets the default 'paged' value to 1.

Props bernhard-reiter, luisherranz, czapla, cbravobernal, davidbaumwald, hellofromTonya.

Follow-up to [53142], [53138].
Fixes #55658.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r53332 r53336  
    13261326                $comment_args['paged'] = 1;
    13271327            } elseif ( 'newest' === $default_page ) {
    1328                 $comment_args['paged'] = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages;
     1328                $max_num_pages = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages;
     1329                if ( 0 !== $max_num_pages ) {
     1330                    $comment_args['paged'] = $max_num_pages;
     1331                }
    13291332            }
    13301333            // Set the `cpage` query var to ensure the previous and next pagination links are correct
  • trunk/tests/phpunit/tests/blocks/renderCommentTemplate.php

    r53329 r53336  
    137137        );
    138138    }
     139
     140    /**
     141     * Test that if pagination is set to display the last page by default (i.e. newest comments),
     142     * the query is set to look for page 1 (rather than page 0, which would cause an error).
     143     *
     144     * Regression: https://github.com/WordPress/gutenberg/issues/40758.
     145     *
     146     * @ticket 55658
     147     * @covers ::build_comment_query_vars_from_block
     148     */
     149    function test_build_comment_query_vars_from_block_pagination_with_no_comments() {
     150        $comments_per_page     = get_option( 'comments_per_page' );
     151        $default_comments_page = get_option( 'default_comments_page' );
     152
     153        update_option( 'comments_per_page', 50 );
     154        update_option( 'previous_default_page', 'newest' );
     155
     156        $post_without_comments = self::factory()->post->create_and_get(
     157            array(
     158                'post_type'    => 'post',
     159                'post_status'  => 'publish',
     160                'post_name'    => 'fluffycat',
     161                'post_title'   => 'Fluffy Cat',
     162                'post_content' => 'Fluffy Cat content',
     163                'post_excerpt' => 'Fluffy Cat',
     164            )
     165        );
     166
     167        $parsed_blocks = parse_blocks(
     168            '<!-- wp:comment-template --><!-- wp:comment-author-name /--><!-- wp:comment-content /--><!-- /wp:comment-template -->'
     169        );
     170
     171        $block = new WP_Block(
     172            $parsed_blocks[0],
     173            array(
     174                'postId' => $post_without_comments->ID,
     175            )
     176        );
     177
     178        $this->assertSameSetsWithIndex(
     179            array(
     180                'orderby'       => 'comment_date_gmt',
     181                'order'         => 'ASC',
     182                'status'        => 'approve',
     183                'no_found_rows' => false,
     184                'post_id'       => $post_without_comments->ID,
     185                'hierarchical'  => 'threaded',
     186                'number'        => 50,
     187            ),
     188            build_comment_query_vars_from_block( $block )
     189        );
     190
     191        update_option( 'comments_per_page', $comments_per_page );
     192        update_option( 'default_comments_page', $default_comments_page );
     193    }
     194
    139195
    140196    /**
Note: See TracChangeset for help on using the changeset viewer.