WordPress.org

Make WordPress Core

Ticket #11334: 11334.2.diff

File 11334.2.diff, 6.8 KB (added by jeremyfelt, 6 years ago)
  • src/wp-includes/comment.php

     
    868868        if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
    869869                return get_page_of_comment( $comment->comment_parent, $args );
    870870
    871         $allowedtypes = array(
    872                 'comment' => '',
    873                 'pingback' => 'pingback',
    874                 'trackback' => 'trackback',
    875         );
     871        switch ( $args['type'] ) {
     872                case 'comment':
     873                        $comment_type       = 'comment';
     874                $comment_type_where = " AND comment_type = ''";
     875                break;
     876                case 'pingback':
     877                        $comment_type       = 'pingback';
     878                        $comment_type_where = " AND comment_type = 'pingback'";
     879                        break;
     880                case 'trackback':
     881                        $comment_type       = 'trackback';
     882                        $comment_type_where = " AND comment_type = 'trackback'";
     883                        break;
     884                case 'pings':
     885                        $comment_type       = 'pings';
     886                        $comment_type_where = " AND ( comment_type = 'pingback' OR comment_type = 'trackback' )";
     887                        break;
     888                default:
     889                        $comment_type = 'all';
     890                        $comment_type_where = '';
     891        }
    876892
    877         $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
     893        $cache_key = 'post-' . $comment->comment_post_ID;
     894        // Check the cache and set it up if it's not set (so we can use replace later on)
     895        if ( false === $older_comments_cache = wp_cache_get( $cache_key, 'comment_pages' ) ) {
     896                $older_comments_cache = array();
     897                wp_cache_add( $cache_key, $older_comments_cache, 'comment_pages' );
     898        }
     899        $older_comments_cache = (array) $older_comments_cache;
    878900
    879         // Count comments older than this one
    880         $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 ) );
     901        // Get comments older than this comment
     902        $older_comments = ( isset( $older_comments_cache[ $comment_type ] ) && isset( $older_comments_cache[ $comment_type ][ $comment->comment_ID ] ) ) ? $older_comments_cache[ $comment_type ][ $comment->comment_ID ] : false;
     903        if ( false === $older_comments ) {
     904                $older_comments = $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 ) );
    881905
     906                $older_comments_cache[ $comment_type ][ $comment->comment_ID ] = $older_comments;
     907                wp_cache_replace( $cache_key, $older_comments_cache, 'comment_pages' );
     908        }
     909
    882910        // No older comments? Then it's page #1.
    883         if ( 0 == $oldercoms )
     911        if ( 0 == $older_comments )
    884912                return 1;
    885913
    886914        // Divide comments older than this one by comments per page to get this comment's page number
    887         return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
     915        return ceil( ( $older_comments + 1 ) / $args['per_page'] );
    888916}
    889917
    890918/**
     919 * Clears the cache used by get_page_of_comment(). Is designed to be attached to the
     920 * 'clear_page_of_comment_cache' action inside of wp_transition_comment_status();
     921 *
     922 * @since 3.7.0
     923 * @uses wp_cache_delete() Does the cache deleting.
     924 *
     925 * @param string $new_status Unused
     926 * @param string $old_status Unused
     927 * @param object $comment Comment object that had it's status changed
     928 * @return bool True on successful removal, false on failure
     929 */
     930function clear_page_of_comment_cache( $new_status, $old_status, $comment ) {
     931        return wp_cache_delete( 'post-' . $comment->comment_post_ID, 'comment_pages' );
     932}
     933
     934/**
    891935 * Does comment contain blacklisted characters or words.
    892936 *
    893937 * @since 1.5.0
  • src/wp-includes/default-filters.php

     
    255255add_action( 'comment_form',               'wp_comment_form_unfiltered_html_nonce'          );
    256256add_action( 'wp_scheduled_delete',        'wp_scheduled_delete'                            );
    257257add_action( 'wp_scheduled_auto_draft_delete', 'wp_delete_auto_drafts'                      );
     258add_action( 'transition_comment_status',  'clear_page_of_comment_cache',             10, 3 );
    258259add_action( 'admin_init',                 'send_frame_options_header',               10, 0 );
    259260add_action( 'importer_scheduled_cleanup', 'wp_delete_attachment'                           );
    260261add_action( 'upgrader_scheduled_cleanup', 'wp_delete_attachment'                           );
  • tests/phpunit/tests/comment.php

     
    1414                $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ) );
    1515                $this->assertEquals( 0, $result );
    1616        }
     17
     18        function test_get_page_of_comment() {
     19                $post = $this->factory->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post' ) );
     20
     21                // page 4
     22                $comment_last = $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-24 00:00:00' ) );
     23                $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-23 00:00:00' ) );
     24
     25                // page 3
     26                $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-22 00:00:00' ) );
     27                $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-21 00:00:00' ) );
     28                $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-20 00:00:00' ) );
     29
     30                // page 2
     31                $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-19 00:00:00' ) );
     32                $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-18 00:00:00' ) );
     33                $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-17 00:00:00' ) );
     34
     35                // page 1
     36                $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-16 00:00:00' ) );
     37                $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-15 00:00:00' ) );
     38                $comment_first = $this->factory->comment->create_post_comments( $post->ID, 1, array( 'comment_date' => '2013-09-14 00:00:00' ) );
     39
     40                $this->assertEquals( 4, get_page_of_comment( $comment_last[0],  array( 'per_page' =>  3 ) ) );
     41                $this->assertEquals( 2, get_page_of_comment( $comment_last[0],  array( 'per_page' => 10 ) ) );
     42
     43                $this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' =>  3 ) ) );
     44                $this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 10 ) ) );
     45        }
    1746}