WordPress.org

Make WordPress Core

Ticket #11334: 11334.3.diff

File 11334.3.diff, 7.0 KB (added by boonebgorges, 5 years ago)
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 2e0fdb7..e6c994f 100644
    function get_page_of_comment( $comment_ID, $args = array() ) { 
    13671367        if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
    13681368                return get_page_of_comment( $comment->comment_parent, $args );
    13691369
    1370         $allowedtypes = array(
    1371                 'comment' => '',
    1372                 'pingback' => 'pingback',
    1373                 'trackback' => 'trackback',
    1374         );
     1370        $older_comment_counts = wp_cache_get( $comment_ID, 'comment_pages' );
     1371
     1372        if ( ! is_array( $older_comment_counts ) ) {
     1373                $older_comment_counts = array();
     1374        }
     1375
     1376        $comment_type = $args['type'];
     1377        if ( isset( $older_comment_counts[ $comment_type ] ) ) {
     1378                $older_comment_count = (int) $older_comment_counts[ $comment_type ];
     1379        } else {
     1380                switch ( $comment_type ) {
     1381                        case 'comment' :
     1382                                $comment_type_where = " and comment_type = 'comment'";
     1383                                break;
     1384
     1385                        case 'pingback' :
     1386                                $comment_type_where = " and comment_type = 'comment'";
     1387                                break;
     1388
     1389                        case 'trackback' :
     1390                                $comment_type_where = " and comment_type = 'comment'";
     1391                                break;
     1392
     1393                        case 'pings' :
     1394                                $comment_type_where = " and comment_type = 'comment'";
     1395                                break;
    13751396
    1376         $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
     1397                        case 'all' :
     1398                        default :
     1399                                $comment_type_where = '';
     1400                                break;
     1401                }
     1402
     1403                // Count comments older than this one.
     1404                $older_comment_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comment_type_where, $comment->comment_post_ID, $comment->comment_date_gmt ) );
    13771405
    1378         // Count comments older than this one
    1379         $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );
     1406                $older_comment_counts[ $comment_type ] = (int) $older_comment_count;
     1407                wp_cache_add( $comment_ID, $older_comment_counts, 'comment_pages' );
     1408        }
    13801409
    13811410        // No older comments? Then it's page #1.
    1382         if ( 0 == $oldercoms )
     1411        if ( 0 == $older_comment_count ) {
    13831412                return 1;
     1413        }
    13841414
    13851415        // Divide comments older than this one by comments per page to get this comment's page number
    1386         return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
     1416        return ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
    13871417}
    13881418
    13891419/**
    function update_comment_cache($comments) { 
    27922822                wp_cache_add($comment->comment_ID, $comment, 'comment');
    27932823}
    27942824
     2825/**
     2826 * Clears the cache used by `get_page_of_comment()`.
     2827 *
     2828 * @since 4.2.0
     2829 *
     2830 * @param string $new_status New comment status.
     2831 * @param string $old_status Old comment status.
     2832 * @param object $comment    Comment object.
     2833 * @return bool True on successful cache invalidation, false on failure.
     2834 */
     2835function wp_clean_page_of_comment_cache( $new_status, $old_status, $comment ) {
     2836        return wp_cache_delete( $comment->comment_ID, 'comment_pages' );
     2837}
     2838
    27952839//
    27962840// Internal
    27972841//
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index 21419f2..895b532 100644
    add_action( 'post_updated', 'wp_save_post_revision', 
    255255add_action( 'publish_post',               '_publish_post_hook',                       5, 1 );
    256256add_action( 'transition_post_status',     '_transition_post_status',                  5, 3 );
    257257add_action( 'transition_post_status',     '_update_term_count_on_transition_post_status', 10, 3 );
     258add_action( 'transition_comment_status',  'wp_clean_page_of_comment_cache',          10, 3 );
    258259add_action( 'comment_form',               'wp_comment_form_unfiltered_html_nonce'          );
    259260add_action( 'wp_scheduled_delete',        'wp_scheduled_delete'                            );
    260261add_action( 'wp_scheduled_auto_draft_delete', 'wp_delete_auto_drafts'                      );
  • tests/phpunit/tests/comment/getPageOfComment.php

    diff --git tests/phpunit/tests/comment/getPageOfComment.php tests/phpunit/tests/comment/getPageOfComment.php
    index 5e03a16..c4260fd 100644
    class Tests_Comment_GetPageOfComment extends WP_UnitTestCase { 
    3838                $this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' =>  3 ) ) );
    3939                $this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 10 ) ) );
    4040        }
     41
     42        /**
     43         * @ticket 11334
     44         */
     45        public function test_subsequent_calls_should_hit_cache() {
     46                global $wpdb;
     47
     48                $p = $this->factory->post->create();
     49                $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     50
     51                // Prime cache.
     52                $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     53
     54                $num_queries = $wpdb->num_queries;
     55                $page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     56
     57                $this->assertSame( $page_1, $page_2 );
     58                $this->assertSame( $num_queries, $wpdb->num_queries );
     59        }
     60
     61        /**
     62         * @ticket 11334
     63         */
     64        public function test_cache_hits_should_be_sensitive_to_comment_type() {
     65                global $wpdb;
     66
     67                $p = $this->factory->post->create();
     68                $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     69
     70                // Prime cache for trackbacks.
     71                $page_1 = get_page_of_comment( $c, array( 'per_page' => 3, 'type' => 'trackback' ) );
     72
     73                $num_queries = $wpdb->num_queries;
     74                $page_2 = get_page_of_comment( $c, array( 'per_page' => 3, 'type' => 'comment' ) );
     75
     76                $this->assertNotEquals( $num_queries, $wpdb->num_queries );
     77        }
     78
     79        /**
     80         * @ticket 11334
     81         */
     82        public function test_cache_should_be_invalidated_when_comment_is_approved() {
     83                $p = $this->factory->post->create();
     84                $c = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0 ) );
     85
     86                // Prime cache.
     87                $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     88
     89                // Approve comment.
     90                wp_set_comment_status( $c, 'approve' );
     91
     92                $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
     93        }
     94
     95        /**
     96         * @ticket 11334
     97         */
     98        public function test_cache_should_be_invalidated_when_comment_is_deleted() {
     99                $p = $this->factory->post->create();
     100                $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     101
     102                // Prime cache.
     103                $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     104
     105                // Trash comment.
     106                wp_trash_comment( $c );
     107
     108                $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
     109        }
     110
     111        /**
     112         * @ticket 11334
     113         */
     114        public function test_cache_should_be_invalidated_when_comment_is_spammed() {
     115                $p = $this->factory->post->create();
     116                $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     117
     118                // Prime cache.
     119                $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     120
     121                // Spam comment.
     122                wp_spam_comment( $c );
     123
     124                $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
     125        }
    41126}