Make WordPress Core

Ticket #11334: 11334.5.diff

File 11334.5.diff, 8.8 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index b1828c3..137f4ec 100644
    class WP_Comment_Query { 
    437437                }
    438438                $cache_key = "get_comments:$key:$last_changed";
    439439
    440                 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
     440                if ( false !== $cache = wp_cache_get( $cache_key, 'comment' ) ) {
    441441                        return $cache;
    442442                }
    443443
    class WP_Comment_Query { 
    809809                }
    810810
    811811                if ( 'ids' == $this->query_vars['fields'] ) {
    812                         $this->comments = $wpdb->get_col( $this->request );
    813                         return array_map( 'intval', $this->comments );
     812                        $this->comments = array_map( 'intval', $wpdb->get_col( $this->request ) );
     813                        wp_cache_add( $cache_key, $this->comments, 'comment' );
     814                        return $this->comments;
    814815                }
    815816
    816817                $results = $wpdb->get_results( $this->request );
    function get_comment_pages_count( $comments = null, $per_page = null, $threaded 
    14581459 *
    14591460 * @since 2.7.0
    14601461 *
    1461  * @param int $comment_ID Comment ID.
    1462  * @param array $args Optional args.
     1462 * @param int   $comment_ID Comment ID.
     1463 * @param array $args {
     1464 *      Array of optional arguments.
     1465 *      @type string     $type      Limit paginated comments to those matching a given type. Accepts 'comment',
     1466 *                                  'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
     1467 *                                  Default is 'all'.
     1468 *      @type int        $per_page  Per-page count to use when calculating pagination. Defaults to the value of the
     1469 *                                  'comments_per_page' option.
     1470 *      @type int|string $max_depth If greater than 1, comment page will be determined for the top-level parent of
     1471 *                                  `$comment_ID`. Defaults to the value of the 'thread_comments_depth' option.
     1472 * }
    14631473 * @return int|null Comment page number or null on error.
    14641474 */
    14651475function get_page_of_comment( $comment_ID, $args = array() ) {
    function get_page_of_comment( $comment_ID, $args = array() ) { 
    14911501        if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
    14921502                return get_page_of_comment( $comment->comment_parent, $args );
    14931503
    1494         $allowedtypes = array(
    1495                 'comment' => '',
    1496                 'pingback' => 'pingback',
    1497                 'trackback' => 'trackback',
     1504        $comment_args = array(
     1505                'type' => $args['type'],
     1506                'post_ID' => $comment->comment_post_ID,
     1507                'fields' => 'ids',
     1508                'status' => 'approve',
     1509                'date_query' => array(
     1510                        array(
     1511                                'column' => "$wpdb->comments.comment_date_gmt",
     1512                                'before' => $comment->comment_date_gmt,
     1513                        )
     1514                ),
    14981515        );
    14991516
    1500         $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
    1501 
    1502         // Count comments older than this one
    1503         $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 ) );
     1517        $older_comment_ids = get_comments( $comment_args );
     1518        $older_comment_count = count( $older_comment_ids );
    15041519
    15051520        // No older comments? Then it's page #1.
    1506         if ( 0 == $oldercoms )
     1521        if ( 0 == $older_comment_count ) {
    15071522                return 1;
     1523        }
    15081524
    15091525        // Divide comments older than this one by comments per page to get this comment's page number
    1510         return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
     1526        return intval( ceil( ( $older_comment_count + 1 ) / $args['per_page'] ) );
    15111527}
    15121528
    15131529/**
  • tests/phpunit/tests/comment/getPageOfComment.php

    diff --git tests/phpunit/tests/comment/getPageOfComment.php tests/phpunit/tests/comment/getPageOfComment.php
    index 5e03a16..a4fd4cd 100644
     
    66 */
    77class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
    88
    9         public function setUp() {
    10                 parent::setUp();
    11         }
    12 
    139        public function test_last_comment() {
    1410                $p = $this->factory->post->create();
    1511
    class Tests_Comment_GetPageOfComment extends WP_UnitTestCase { 
    3834                $this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' =>  3 ) ) );
    3935                $this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 10 ) ) );
    4036        }
     37
     38        public function test_type_pings() {
     39                $p = $this->factory->post->create();
     40                $now = time();
     41
     42                $trackbacks = array();
     43                for ( $i = 0; $i <= 3; $i++ ) {
     44                        $trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     45                        $now -= 10 * $i;
     46                }
     47
     48                $pingbacks = array();
     49                for ( $i = 0; $i <= 6; $i++ ) {
     50                        $pingbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'pingback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     51                        $now -= 10 * $i;
     52                }
     53
     54                $this->assertEquals( 2, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'trackback' ) ) );
     55                $this->assertEquals( 3, get_page_of_comment( $pingbacks[0], array( 'per_page' => 2, 'type' => 'pingback' ) ) );
     56                $this->assertEquals( 5, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'pings' ) ) );
     57        }
     58
     59        /**
     60         * @ticket 11334
     61         */
     62        public function test_subsequent_calls_should_hit_cache() {
     63                global $wpdb;
     64
     65                $p = $this->factory->post->create();
     66                $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     67
     68                // Prime cache.
     69                $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     70
     71                $num_queries = $wpdb->num_queries;
     72                $page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     73
     74                $this->assertSame( $page_1, $page_2 );
     75                $this->assertSame( $num_queries, $wpdb->num_queries );
     76        }
     77
     78        /**
     79         * @ticket 11334
     80         */
     81        public function test_cache_hits_should_be_sensitive_to_comment_type() {
     82                global $wpdb;
     83
     84                $p = $this->factory->post->create();
     85                $comment = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'comment' ) );
     86
     87                $now = time();
     88                $trackbacks = array();
     89                for ( $i = 0; $i <= 5; $i++ ) {
     90                        $trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 10 * $i ) ) ) );
     91                }
     92
     93                // Prime cache for trackbacks.
     94                $page_trackbacks = get_page_of_comment( $trackbacks[1], array( 'per_page' => 3, 'type' => 'trackback' ) );
     95                $this->assertEquals( 2, $page_trackbacks );
     96
     97                $num_queries = $wpdb->num_queries;
     98                $page_comments = get_page_of_comment( $comment, array( 'per_page' => 3, 'type' => 'comment' ) );
     99                $this->assertEquals( 1, $page_comments );
     100
     101                $this->assertNotEquals( $num_queries, $wpdb->num_queries );
     102        }
     103
     104        /**
     105         * @ticket 11334
     106         */
     107        public function test_cache_should_be_invalidated_when_comment_is_approved() {
     108                $p = $this->factory->post->create();
     109                $c = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0 ) );
     110
     111                // Prime cache.
     112                $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     113
     114                // Approve comment.
     115                wp_set_comment_status( $c, 'approve' );
     116
     117                $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
     118        }
     119
     120        /**
     121         * @ticket 11334
     122         */
     123        public function test_cache_should_be_invalidated_when_comment_is_deleted() {
     124                $p = $this->factory->post->create();
     125                $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     126
     127                // Prime cache.
     128                $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     129
     130                // Trash comment.
     131                wp_trash_comment( $c );
     132
     133                $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
     134        }
     135
     136        /**
     137         * @ticket 11334
     138         */
     139        public function test_cache_should_be_invalidated_when_comment_is_spammed() {
     140                $p = $this->factory->post->create();
     141                $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     142
     143                // Prime cache.
     144                $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     145
     146                // Spam comment.
     147                wp_spam_comment( $c );
     148
     149                $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
     150        }
     151
     152        /**
     153         * @ticket 11334
     154         */
     155        public function test_cache_should_be_invalidated_when_older_comment_is_published() {
     156                $now = time();
     157
     158                $p = $this->factory->post->create();
     159                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     160                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
     161                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
     162
     163                $this->assertEquals( 1, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
     164
     165                wp_set_comment_status( $c3, '1' );
     166
     167                $this->assertEquals( 2, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
     168        }
    41169}