Make WordPress Core

Changeset 34535


Ignore:
Timestamp:
09/25/2015 05:10:40 AM (9 years ago)
Author:
boonebgorges
Message:

Use WP_Comment_Query in get_page_of_comment().

This change allows get_page_of_comment() to use WP_Comment_Query's native
caching mechanisms.

Props boonebgorges, Viper007Bond, wmertens, jeremyfelt.
Fixes #11334.

Location:
trunk
Files:
2 edited

Legend:

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

    r34534 r34535  
    822822 *
    823823 * @global wpdb $wpdb
    824  *
    825  * @param int $comment_ID Comment ID.
    826  * @param array $args Optional args.
     824 * @param int   $comment_ID Comment ID.
     825 * @param array $args {
     826 *      Array of optional arguments.
     827 *      @type string     $type      Limit paginated comments to those matching a given type. Accepts 'comment',
     828 *                                  'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
     829 *                                  Default is 'all'.
     830 *      @type int        $per_page  Per-page count to use when calculating pagination. Defaults to the value of the
     831 *                                  'comments_per_page' option.
     832 *      @type int|string $max_depth If greater than 1, comment page will be determined for the top-level parent of
     833 *                                  `$comment_ID`. Defaults to the value of the 'thread_comments_depth' option.
     834 * } *
    827835 * @return int|null Comment page number or null on error.
    828836 */
     
    856864        return get_page_of_comment( $comment->comment_parent, $args );
    857865
    858     $allowedtypes = array(
    859         'comment' => '',
    860         'pingback' => 'pingback',
    861         'trackback' => 'trackback',
     866    $comment_args = array(
     867        'type'       => $args['type'],
     868        'post_ID'    => $comment->comment_post_ID,
     869        'fields'     => 'ids',
     870        'status'     => 'approve',
     871        'date_query' => array(
     872            array(
     873                'column' => "$wpdb->comments.comment_date_gmt",
     874                'before' => $comment->comment_date_gmt,
     875            )
     876        ),
    862877    );
    863878
    864     $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
    865 
    866     // Count comments older than this one
    867     $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 ) );
     879    $older_comment_ids = get_comments( $comment_args );
     880    $older_comment_count = count( $older_comment_ids );
    868881
    869882    // No older comments? Then it's page #1.
    870     if ( 0 == $oldercoms )
     883    if ( 0 == $older_comment_count )
    871884        return 1;
    872885
    873886    // Divide comments older than this one by comments per page to get this comment's page number
    874     return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
     887    return ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
    875888}
    876889
  • trunk/tests/phpunit/tests/comment/getPageOfComment.php

    r31289 r34535  
    66 */
    77class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
    8 
    9     public function setUp() {
    10         parent::setUp();
    11     }
    128
    139    public function test_last_comment() {
     
    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}
Note: See TracChangeset for help on using the changeset viewer.