WordPress.org

Make WordPress Core

Ticket #8071: 8071.8.diff

File 8071.8.diff, 30.3 KB (added by boonebgorges, 4 years ago)
  • src/wp-admin/includes/schema.php

    diff --git src/wp-admin/includes/schema.php src/wp-admin/includes/schema.php
    index ebbd917..3f2ac5d 100644
    function populate_options() { 
    482482        'close_comments_days_old' => 14,
    483483        'thread_comments' => 1,
    484484        'thread_comments_depth' => 5,
     485        'page_comments' => 1,
    485486        'comments_per_page' => 50,
    486487        'default_comments_page' => 'newest',
    487488        'comment_order' => 'asc',
    function populate_options() { 
    566567                'can_compress_scripts', 'page_uris', 'update_core', 'update_plugins', 'update_themes', 'doing_cron',
    567568                'random_seed', 'rss_excerpt_length', 'secret', 'use_linksupdate', 'default_comment_status_page',
    568569                'wporg_popular_tags', 'what_to_show', 'rss_language', 'language', 'enable_xmlrpc', 'enable_app',
    569                 'embed_autourls', 'default_post_edit_rows', 'page_comments',
     570                'embed_autourls', 'default_post_edit_rows',
    570571        );
    571572        foreach ( $unusedoptions as $option )
    572573                delete_option($option);
  • src/wp-admin/options-discussion.php

    diff --git src/wp-admin/options-discussion.php src/wp-admin/options-discussion.php
    index 02b4379..3d59195 100644
    printf( __('Enable threaded (nested) comments %s levels deep'), $thread_comments 
    9898
    9999?></label>
    100100<br />
     101<label for="page_comments">
     102<input name="page_comments" type="checkbox" id="page_comments" value="1" <?php checked( '1', get_option( 'page_comments' ) ); ?> />
    101103<?php
    102104$default_comments_page = '</label><label for="default_comments_page"><select name="default_comments_page" id="default_comments_page"><option value="newest"';
    103105if ( 'newest' == get_option('default_comments_page') ) $default_comments_page .= ' selected="selected"';
    $default_comments_page .= '>' . __('last') . '</option><option value="oldest"'; 
    105107if ( 'oldest' == get_option('default_comments_page') ) $default_comments_page .= ' selected="selected"';
    106108$default_comments_page .= '>' . __('first') . '</option></select>';
    107109
    108 printf( __('Break comments into pages with %1$s top level comments per page and the %2$s page displayed by default'), '<label for="comments_per_page"><input name="comments_per_page" type="number" step="1" min="0" id="comments_per_page" value="' . esc_attr(get_option('comments_per_page')) . '" class="small-text" />', $default_comments_page );
     110printf( __('Break comments into pages with %1$s top level comments per page and the %2$s page displayed by default'), '</label><label for="comments_per_page"><input name="comments_per_page" type="number" step="1" min="0" id="comments_per_page" value="' . esc_attr(get_option('comments_per_page')) . '" class="small-text" />', $default_comments_page );
    109111
    110112?></label>
    111113<br />
  • src/wp-admin/options.php

    diff --git src/wp-admin/options.php src/wp-admin/options.php
    index 017725d..6bad0fd 100644
    if ( is_multisite() && ! is_super_admin() && 'update' != $action ) { 
    8383
    8484$whitelist_options = array(
    8585        'general' => array( 'blogname', 'blogdescription', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'timezone_string', 'WPLANG' ),
    86         'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'comments_per_page', 'default_comments_page', 'comment_order', 'comment_registration' ),
     86        'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page', 'default_comments_page', 'comment_order', 'comment_registration' ),
    8787        'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type' ),
    8888        'reading' => array( 'posts_per_page', 'posts_per_rss', 'rss_use_excerpt', 'show_on_front', 'page_on_front', 'page_for_posts', 'blog_public' ),
    8989        'writing' => array( 'default_category', 'default_email_category', 'default_link_category', 'default_post_format' )
  • src/wp-includes/canonical.php

    diff --git src/wp-includes/canonical.php src/wp-includes/canonical.php
    index b658797..cb7d7ac 100644
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    318318                                }
    319319                        }
    320320
    321                         if ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) {
     321                        if ( get_option( 'page_comments' ) && ( ( 'newest' == get_option('default_comments_page') && get_query_var( 'cpage' ) > 0 ) || ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 ) ) ) {
    322322                                $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' );
    323323                                $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
    324324                        }
  • src/wp-includes/comment-functions.php

    diff --git src/wp-includes/comment-functions.php src/wp-includes/comment-functions.php
    index 7f7cbb6..f12a825 100644
    function get_comment_pages_count( $comments = null, $per_page = null, $threaded 
    807807        if ( empty($comments) )
    808808                return 0;
    809809
     810        if ( ! get_option( 'page_comments' ) ) {
     811                return 1;
     812        }
     813
    810814        if ( !isset($per_page) )
    811815                $per_page = (int) get_query_var('comments_per_page');
    812816        if ( 0 === $per_page )
    function get_comment_pages_count( $comments = null, $per_page = null, $threaded 
    828832}
    829833
    830834/**
     835 * Get the number of approved top-level comments a post has.
     836 *
     837 * @since 4.4.0
     838 *
     839 * @param int $post_id ID of the post.
     840 */
     841function wp_get_top_level_comments_count( $post_id ) {
     842        $top_level_query = new WP_Comment_Query();
     843        $top_level_count = $top_level_query->query( array(
     844                'count'   => true,
     845                'orderby' => false,
     846                'post_id' => $post_id,
     847                'parent'  => 0,
     848                'status'  => 'approve',
     849        ) );
     850
     851        return intval( $top_level_count );
     852}
     853
     854/**
     855 * Get comment pages count from the post ID.
     856 *
     857 * @since 4.4.0
     858 *
     859 * @param int $post_id ID of the post.
     860 * @return int
     861 */
     862function wp_get_comment_pages_count( $post_id ) {
     863        $per_page = get_option( 'comments_per_page' );
     864
     865        $per_page = (int) $per_page;
     866        if ( ! $per_page ) {
     867                return 1;
     868        }
     869
     870        $top_level_comment_count = wp_get_top_level_comments_count( $post_id );
     871
     872        if ( 0 === $top_level_comment_count ) {
     873                return 1;
     874        }
     875
     876        /**
     877         * Filter the threshold at which comment pagination is forced.
     878         *
     879         * Large numbers of comments cause severe performance problems when loaded on a single page, so we force comments
     880         * to be paginated beyond a certain threshold. The default threshold is 250 top-level comments.
     881         *
     882         * If your 'comments_per_page' value is higher than our threshold, we respect your value.
     883         *
     884         * @since 4.4.0
     885         *
     886         * @param int $threshold Number of comments a post must have before pagination is forced.
     887         */
     888        $paging_threshold = (int) apply_filters( 'comment_paging_threshold', 250 );
     889        if ( $top_level_comment_count > $paging_threshold && ! get_option( 'page_comments' ) ) {
     890                $per_page = $paging_threshold;
     891        }
     892
     893
     894        return (int) ceil( $top_level_comment_count / $per_page );
     895}
     896
     897/**
    831898 * Calculate what page number a comment will appear on for comment paging.
    832899 *
    833900 * @since 2.7.0
    function get_page_of_comment( $comment_ID, $args = array() ) { 
    860927        $original_args = $args;
    861928
    862929        // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option.
    863         if ( '' === $args['per_page'] ) {
    864                 $args['per_page'] = get_query_var( 'comments_per_page' );
    865         }
     930        if ( get_option( 'page_comments' ) ) {
     931                if ( '' === $args['per_page'] ) {
     932                        $args['per_page'] = get_query_var( 'comments_per_page' );
     933                }
    866934
    867         if ( '' === $args['per_page'] ) {
    868                 $args['per_page'] = get_option( 'comments_per_page' );
     935                if ( '' === $args['per_page'] ) {
     936                        $args['per_page'] = get_option( 'comments_per_page' );
     937                }
    869938        }
    870939
    871940        if ( empty($args['per_page']) ) {
  • src/wp-includes/comment-template.php

    diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php
    index 6174c0c..feceb50 100644
    function get_comment_link( $comment = null, $args = array() ) { 
    706706
    707707        // No 'cpage' is provided, so we calculate one.
    708708        } else {
    709                 if ( '' === $args['per_page'] ) {
     709                if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
    710710                        $args['per_page'] = get_option('comments_per_page');
    711711                }
    712712
    function comments_template( $file = '/comments.php', $separate_comments = false 
    12951295        } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
    12961296                $comment_args['offset'] = 0;
    12971297        } else {
    1298                 // If fetching the first page of 'newest', we need a top-level comment count.
    1299                 $top_level_query = new WP_Comment_Query();
    1300                 $top_level_count = $top_level_query->query( array(
    1301                         'count'   => true,
    1302                         'orderby' => false,
    1303                         'post_id' => $post->ID,
    1304                         'parent'  => 0,
    1305                 ) );
    1306 
    1307                 $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
     1298                $comment_args['offset'] = ( wp_get_comment_pages_count( $post->ID ) - 1 ) * $per_page;
    13081299        }
    13091300
    13101301        $comment_query = new WP_Comment_Query( $comment_args );
    function comments_template( $file = '/comments.php', $separate_comments = false 
    13451336        }
    13461337
    13471338        $overridden_cpage = false;
    1348         if ( '' == get_query_var('cpage') ) {
     1339        if ( '' == get_query_var( 'cpage' ) && $wp_query->max_num_comment_pages > 1 ) {
    13491340                set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
    13501341                $overridden_cpage = true;
    13511342        }
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index 75dadfc..7e80379 100644
    add_filter( 'default_option_link_manager_enabled', '__return_true' ); 
    329329// This option no longer exists; tell plugins we always support auto-embedding.
    330330add_filter( 'default_option_embed_autourls', '__return_true' );
    331331
    332 // This option no longer exists; tell plugins we want comment pagination.
    333 add_filter( 'pre_option_page_comments', '__return_true' );
    334 
    335332// Default settings for heartbeat
    336333add_filter( 'heartbeat_settings', 'wp_heartbeat_settings' );
    337334
  • tests/phpunit/tests/comment/getCommentsPagesCount.php

    diff --git tests/phpunit/tests/comment/getCommentsPagesCount.php tests/phpunit/tests/comment/getCommentsPagesCount.php
    index 6e742a8..4490cd8 100644
     
    1 <?php
    2 /**
    3  * Validate the logic of get_comments_pages_count
    4  * @group comment
    5  */
    6 class Tests_Comment_GetCommentsPagesCount extends WP_UnitTestCase {
    7         protected $option_page_comments;
    8         protected $option_comments_per_page;
    9         protected $option_thread_comments;
    10         protected $option_posts_per_rss;
    11 
    12         /**
    13          * setUp options
    14          */
    15         function setUp() {
    16                 parent::setUp();
    17                 $this->option_page_comments = get_option( 'page_comments' );
    18                 $this->option_page_comments = get_option( 'comments_per_page' );
    19                 $this->option_page_comments = get_option( 'thread_comments' );
    20                 $this->option_posts_per_rss = get_option( 'posts_per_rss' );
    21 
    22                 update_option( 'page_comments', true );
    23         }
    24 
    25         /**
    26          * tearDown options
    27          */
    28         function tearDown() {
    29                 update_option( 'page_comments', $this->option_page_comments );
    30                 update_option( 'comments_per_page', $this->option_page_comments );
    31                 update_option( 'thread_comments', $this->option_page_comments );
    32                 update_option( 'posts_per_rss', $this->option_posts_per_rss );
    33                 parent::tearDown();
    34         }
    35 
    36         /**
    37          * Validate get_comments_pages_count for empty comments
    38          */
    39         function test_empty() {
    40                 //setup post and comments
    41                 $post_id = $this->factory->post->create( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
    42                 $this->go_to( '/?p=' . $post_id );
    43 
    44                 global $wp_query;
    45                 unset( $wp_query->comments );
    46 
    47                 $comments = get_comments( array( 'post_id' => $post_id ) );
    48 
    49                 $this->assertEquals( 0, get_comment_pages_count( $comments, 10, false ) );
    50                 $this->assertEquals( 0, get_comment_pages_count( $comments, 1, false ) );
    51                 $this->assertEquals( 0, get_comment_pages_count( $comments, 0, false ) );
    52                 $this->assertEquals( 0, get_comment_pages_count( $comments, 10, true ) );
    53                 $this->assertEquals( 0, get_comment_pages_count( $comments, 5 ) );
    54                 $this->assertEquals( 0, get_comment_pages_count( $comments ) );
    55                 $this->assertequals( 0, get_comment_pages_count( null, 1 ) );
    56         }
    57 
    58         /**
    59          * Validate get_comments_pages_count for treaded comments
    60          */
    61         function test_threaded_comments( ) {
    62                 //setup post and comments
    63                 $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
    64                 $comments = $this->factory->comment->create_post_comments( $post->ID, 15 );
    65                 $this->factory->comment->create_post_comments( $post->ID, 6, array( 'comment_parent' => $comments[0] ) );
    66                 $comments = get_comments( array( 'post_id' => $post->ID ) );
    67 
    68                 $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
    69                 $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
    70                 $this->assertEquals( 4, get_comment_pages_count( $comments, 4, true ) );
    71         }
    72 
    73         /**
    74          * Validate get_comments_pages_count for option tread_comments
    75          */
    76         function test_option_thread_comments() {
    77 
    78                 //setup post and comments
    79                 $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
    80                 $comments = $this->factory->comment->create_post_comments( $post->ID, 15 );
    81                 $this->factory->comment->create_post_comments( $post->ID, 6, array('comment_parent' => $comments[0] ) );
    82                 $comments = get_comments( array( 'post_id' => $post->ID ) );
    83 
    84                 update_option( 'thread_comments', false );
    85 
    86                 $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
    87                 $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
    88                 $this->assertEquals( 3, get_comment_pages_count( $comments, 10, null ) );
    89                 $this->assertEquals( 3, get_comment_pages_count( $comments, 10 ) );
    90 
    91                 update_option( 'thread_comments', true );
    92 
    93                 $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
    94                 $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
    95                 $this->assertEquals( 2, get_comment_pages_count( $comments, 10, null ) );
    96                 $this->assertEquals( 2, get_comment_pages_count( $comments, 10 ) );
    97         }
    98 
    99         /**
    100          * Validate $wp_query logic of get_comment_pages_count
    101          */
    102         function test_wp_query_comments_per_page() {
    103                 global $wp_query;
    104 
    105                 update_option( 'posts_per_rss', 100 );
    106 
    107                 $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment-post', 'post_type' => 'post' ) );
    108                 $comments = $this->factory->comment->create_post_comments( $post->ID, 25 );
    109 
    110                 $wp_query = new WP_Query( array( 'p' => $post->ID, 'comments_per_page' => 10, 'feed' =>'comments-' ) );
    111 
    112                 update_option( 'comments_per_page', 25 );
    113 
    114                 $this->assertEquals( 3, get_comment_pages_count() );
    115                 $this->assertEquals( 2, get_comment_pages_count( null, 20 ) );
    116 
    117                 $wp_query = new WP_Query( array( 'p' => $post->ID,'comments_per_page' => null, 'feed' =>'comments-' ) );
    118 
    119                 $this->assertEquals( 1, get_comment_pages_count() );
    120                 $this->assertEquals( 5, get_comment_pages_count( null, 5 ) );
    121 
    122                 $wp_query->query_vars['comments_per_page'] = null;
    123 
    124                 update_option( 'comments_per_page', 5 );
    125 
    126                 $this->assertEquals( 5, get_comment_pages_count() );
    127                 $this->assertEquals( 3, get_comment_pages_count( null, 11 ) );
    128                 $this->assertEquals( 5, get_comment_pages_count( null, 0 ) );
    129         }
    130 
    131         /**
    132          * Validate max_num_comment_pages logic of get_comment_pages_count
    133          */
    134          function test_max_num_comment_pages() {
    135                 global $wp_query;
    136                 $wp_query = new WP_Query();
    137 
    138                 $org_max_num_comment_pages = $wp_query->max_num_comment_pages;
    139 
    140                 $wp_query->max_num_comment_pages = 7;
    141 
    142                 $this->assertEquals( 7, get_comment_pages_count() );
    143                 $this->assertEquals( 7, get_comment_pages_count( null, null, null ) );
    144                 $this->assertEquals( 0, get_comment_pages_count( array(), null, null ) );
    145 
    146                 $wp_query->max_num_comment_pages = $org_max_num_comment_pages;
    147          }
    148 }
     1<?php
     2/**
     3 * Validate the logic of get_comments_pages_count
     4 * @group comment
     5 */
     6class Tests_Comment_GetCommentsPagesCount extends WP_UnitTestCase {
     7        protected $option_page_comments;
     8        protected $option_comments_per_page;
     9        protected $option_thread_comments;
     10        protected $option_posts_per_rss;
     11
     12        /**
     13         * setUp options
     14         */
     15        function setUp() {
     16                parent::setUp();
     17                $this->option_page_comments = get_option( 'page_comments' );
     18                $this->option_page_comments = get_option( 'comments_per_page' );
     19                $this->option_page_comments = get_option( 'thread_comments' );
     20                $this->option_posts_per_rss = get_option( 'posts_per_rss' );
     21
     22                update_option( 'page_comments', true );
     23        }
     24
     25        /**
     26         * tearDown options
     27         */
     28        function tearDown() {
     29                update_option( 'page_comments', $this->option_page_comments );
     30                update_option( 'comments_per_page', $this->option_page_comments );
     31                update_option( 'thread_comments', $this->option_page_comments );
     32                update_option( 'posts_per_rss', $this->option_posts_per_rss );
     33                parent::tearDown();
     34        }
     35
     36        /**
     37         * Validate get_comments_pages_count for empty comments
     38         */
     39        function test_empty() {
     40                //setup post and comments
     41                $post_id = $this->factory->post->create( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
     42                $this->go_to( '/?p=' . $post_id );
     43
     44                global $wp_query;
     45                unset( $wp_query->comments );
     46
     47                $comments = get_comments( array( 'post_id' => $post_id ) );
     48
     49                $this->assertEquals( 0, get_comment_pages_count( $comments, 10, false ) );
     50                $this->assertEquals( 0, get_comment_pages_count( $comments, 1, false ) );
     51                $this->assertEquals( 0, get_comment_pages_count( $comments, 0, false ) );
     52                $this->assertEquals( 0, get_comment_pages_count( $comments, 10, true ) );
     53                $this->assertEquals( 0, get_comment_pages_count( $comments, 5 ) );
     54                $this->assertEquals( 0, get_comment_pages_count( $comments ) );
     55                $this->assertequals( 0, get_comment_pages_count( null, 1 ) );
     56        }
     57
     58        /**
     59         * Validate get_comments_pages_count for treaded comments
     60         */
     61        function test_threaded_comments( ) {
     62                //setup post and comments
     63                $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
     64                $comments = $this->factory->comment->create_post_comments( $post->ID, 15 );
     65                $this->factory->comment->create_post_comments( $post->ID, 6, array( 'comment_parent' => $comments[0] ) );
     66                $comments = get_comments( array( 'post_id' => $post->ID ) );
     67
     68                $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
     69                $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
     70                $this->assertEquals( 4, get_comment_pages_count( $comments, 4, true ) );
     71        }
     72
     73        /*
     74         * Validate get_comments_pages_count for option page_comments
     75         */
     76        function test_option_page_comments() {
     77                //setup post and comments
     78                $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
     79                $comments = $this->factory->comment->create_post_comments( $post->ID, 15 );
     80
     81                // comment paging disabled
     82                update_option( 'page_comments', false );
     83
     84                $this->assertEquals( 1, get_comment_pages_count( $comments, 10, false ) );
     85                $this->assertEquals( 1, get_comment_pages_count( $comments, 0, true ) );
     86                $this->assertEquals( 1, get_comment_pages_count( $comments, 2, true ) );
     87                // comment paging enabled
     88                update_option( 'page_comments', true );
     89                $this->assertEquals( 2, get_comment_pages_count( $comments, 10, false ) );
     90                $this->assertEquals( 3, get_comment_pages_count( $comments, 5, false ) );
     91        }
     92
     93        /**
     94         * Validate get_comments_pages_count for option tread_comments
     95         */
     96        function test_option_thread_comments() {
     97
     98                //setup post and comments
     99                $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment--post', 'post_type' => 'post' ) );
     100                $comments = $this->factory->comment->create_post_comments( $post->ID, 15 );
     101                $this->factory->comment->create_post_comments( $post->ID, 6, array('comment_parent' => $comments[0] ) );
     102                $comments = get_comments( array( 'post_id' => $post->ID ) );
     103
     104                update_option( 'thread_comments', false );
     105
     106                $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
     107                $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
     108                $this->assertEquals( 3, get_comment_pages_count( $comments, 10, null ) );
     109                $this->assertEquals( 3, get_comment_pages_count( $comments, 10 ) );
     110
     111                update_option( 'thread_comments', true );
     112
     113                $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
     114                $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
     115                $this->assertEquals( 2, get_comment_pages_count( $comments, 10, null ) );
     116                $this->assertEquals( 2, get_comment_pages_count( $comments, 10 ) );
     117        }
     118
     119        /**
     120         * Validate $wp_query logic of get_comment_pages_count
     121         */
     122        function test_wp_query_comments_per_page() {
     123                global $wp_query;
     124
     125                update_option( 'posts_per_rss', 100 );
     126
     127                $post = $this->factory->post->create_and_get( array( 'post_title' => 'comment-post', 'post_type' => 'post' ) );
     128                $comments = $this->factory->comment->create_post_comments( $post->ID, 25 );
     129
     130                $wp_query = new WP_Query( array( 'p' => $post->ID, 'comments_per_page' => 10, 'feed' =>'comments-' ) );
     131
     132                update_option( 'comments_per_page', 25 );
     133
     134                $this->assertEquals( 3, get_comment_pages_count() );
     135                $this->assertEquals( 2, get_comment_pages_count( null, 20 ) );
     136
     137                $wp_query = new WP_Query( array( 'p' => $post->ID,'comments_per_page' => null, 'feed' =>'comments-' ) );
     138
     139                $this->assertEquals( 1, get_comment_pages_count() );
     140                $this->assertEquals( 5, get_comment_pages_count( null, 5 ) );
     141
     142                $wp_query->query_vars['comments_per_page'] = null;
     143
     144                update_option( 'comments_per_page', 5 );
     145
     146                $this->assertEquals( 5, get_comment_pages_count() );
     147                $this->assertEquals( 3, get_comment_pages_count( null, 11 ) );
     148                $this->assertEquals( 5, get_comment_pages_count( null, 0 ) );
     149        }
     150
     151        /**
     152         * Validate max_num_comment_pages logic of get_comment_pages_count
     153         */
     154         function test_max_num_comment_pages() {
     155                global $wp_query;
     156                $wp_query = new WP_Query();
     157
     158                $org_max_num_comment_pages = $wp_query->max_num_comment_pages;
     159
     160                $wp_query->max_num_comment_pages = 7;
     161
     162                $this->assertEquals( 7, get_comment_pages_count() );
     163                $this->assertEquals( 7, get_comment_pages_count( null, null, null ) );
     164                $this->assertEquals( 0, get_comment_pages_count( array(), null, null ) );
     165
     166                $wp_query->max_num_comment_pages = $org_max_num_comment_pages;
     167         }
     168}
  • new file tests/phpunit/tests/comment/wpGetCommentPagesCount.php

    diff --git tests/phpunit/tests/comment/wpGetCommentPagesCount.php tests/phpunit/tests/comment/wpGetCommentPagesCount.php
    new file mode 100644
    index 0000000..30ebf2f
    - +  
     1<?php
     2
     3/**
     4 * @group comment
     5 * @ticket 8071
     6 */
     7class Tests_Comment_WpGetCommentPagesCount extends WP_UnitTestCase {
     8        protected static $post_id;
     9
     10        public static function wpSetUpBeforeClass( $factory ) {
     11                self::$post_id = $factory->post->create();
     12        }
     13
     14        public static function wpTearDownAfterClass() {
     15                wp_delete_post( self::$post_id, true );
     16        }
     17
     18        public function test_nonexistent_post_should_have_1_page() {
     19                $this->assertSame( 1, wp_get_comment_pages_count( 12345 ) );
     20        }
     21
     22        public function test_post_with_no_comments_should_have_1_page_when_page_comments_is_enabled() {
     23                update_option( 'page_comments', 1 );
     24                $this->assertSame( 1, wp_get_comment_pages_count( self::$post_id, 20 ) );
     25        }
     26
     27        public function test_post_with_no_comments_should_have_1_page_when_page_comments_is_disabled() {
     28                update_option( 'page_comments', 0 );
     29                $this->assertSame( 1, wp_get_comment_pages_count( self::$post_id, 20 ) );
     30        }
     31
     32        public function test_comments_per_page_should_be_used_as_fallback_for_per_page() {
     33                update_option( 'comments_per_page', 2 );
     34
     35                $comments = $this->factory->comment->create_many( 3, array(
     36                        'comment_post_ID' => self::$post_id,
     37                ) );
     38
     39                $this->assertSame( 2, wp_get_comment_pages_count( self::$post_id ) );
     40        }
     41
     42        public function test_comments_per_page_should_be_respected_even_when_over_paging_threshold_when_page_comments_is_enabled() {
     43                update_option( 'page_comments', 1 );
     44                update_option( 'comments_per_page', 3 );
     45
     46                $comments = $this->factory->comment->create_many( 3, array(
     47                        'comment_post_ID' => self::$post_id,
     48                ) );
     49
     50                add_filter( 'comment_paging_threshold', array( $this, 'set_threshold_to_2' ) );
     51                $found = wp_get_comment_pages_count( self::$post_id, 20 );
     52                remove_filter( 'comment_paging_threshold', array( $this, 'set_threshold_to_2' ) );
     53
     54                $this->assertSame( 1, $found );
     55        }
     56
     57        public function test_comments_per_page_should_be_overridden_by_paging_threshold_when_page_comments_is_disabled() {
     58                update_option( 'page_comments', 0 );
     59                update_option( 'comments_per_page', 3 );
     60
     61                $comments = $this->factory->comment->create_many( 3, array(
     62                        'comment_post_ID' => self::$post_id,
     63                ) );
     64
     65                add_filter( 'comment_paging_threshold', array( $this, 'set_threshold_to_2' ) );
     66                $found = wp_get_comment_pages_count( self::$post_id, 20 );
     67                remove_filter( 'comment_paging_threshold', array( $this, 'set_threshold_to_2' ) );
     68
     69                $this->assertSame( 2, $found );
     70        }
     71
     72        public function test_comments_per_page_should_be_respected_when_page_comments_is_disabled_but_comment_count_is_below_threshold() {
     73                update_option( 'page_comments', 0 );
     74                update_option( 'comments_per_page', 2 );
     75
     76                $comments = $this->factory->comment->create_many( 3, array(
     77                        'comment_post_ID' => self::$post_id,
     78                ) );
     79
     80                add_filter( 'comment_paging_threshold', array( $this, 'set_threshold_to_20' ) );
     81                $found = wp_get_comment_pages_count( self::$post_id, 20 );
     82                remove_filter( 'comment_paging_threshold', array( $this, 'set_threshold_to_20' ) );
     83
     84                $this->assertSame( 2, $found );
     85        }
     86
     87        public static function set_threshold_to_2( $threshold ) {
     88                return 2;
     89        }
     90
     91        public static function set_threshold_to_20( $threshold ) {
     92                return 20;
     93        }
     94}
  • new file tests/phpunit/tests/comment/wpGetTopLevelCommentsCount.php

    diff --git tests/phpunit/tests/comment/wpGetTopLevelCommentsCount.php tests/phpunit/tests/comment/wpGetTopLevelCommentsCount.php
    new file mode 100644
    index 0000000..62c5f58
    - +  
     1<?php
     2
     3/**
     4 * @group comment
     5 * @ticket 8071
     6 */
     7class Tests_Comment_WpGetTopLevelCommentsCount extends WP_UnitTestCase {
     8        public function test_should_return_0_for_nonexistent_post() {
     9                $this->assertSame( 0, wp_get_top_level_comments_count( 12345 ) );
     10        }
     11
     12        public function test_should_return_0_for_post_with_no_comments() {
     13                $p = $this->factory->post->create();
     14                $this->assertSame( 0, wp_get_top_level_comments_count( $p ) );
     15        }
     16
     17        public function test_with_post_that_has_no_child_comments() {
     18                $p = $this->factory->post->create();
     19                $comments = $this->factory->comment->create_many( 2, array(
     20                        'comment_post_ID' => $p,
     21                ) );
     22
     23                $this->assertSame( 2, wp_get_top_level_comments_count( $p ) );
     24        }
     25
     26        public function test_non_approved_comments_should_be_excluded_from_count() {
     27                $p = $this->factory->post->create();
     28
     29                $this->factory->comment->create( array(
     30                        'comment_post_ID' => $p,
     31                        'comment_approved' => '1',
     32                ) );
     33
     34                $this->factory->comment->create( array(
     35                        'comment_post_ID' => $p,
     36                        'comment_approved' => '0',
     37                ) );
     38
     39                $this->factory->comment->create( array(
     40                        'comment_post_ID' => $p,
     41                        'comment_approved' => 'spam',
     42                ) );
     43
     44                $this->assertSame( 1, wp_get_top_level_comments_count( $p ) );
     45        }
     46
     47        public function test_child_comments_should_not_be_counted() {
     48                $p = $this->factory->post->create();
     49
     50                $c1 = $this->factory->comment->create( array(
     51                        'comment_post_ID' => $p,
     52                ) );
     53
     54                $c2 = $this->factory->comment->create( array(
     55                        'comment_post_ID' => $p,
     56                        'comment_parent' => $c1,
     57                ) );
     58
     59                $c3 = $this->factory->comment->create( array(
     60                        'comment_post_ID' => $p,
     61                        'comment_parent' => $c2,
     62                ) );
     63
     64                $c4 = $this->factory->comment->create( array(
     65                        'comment_post_ID' => $p,
     66                ) );
     67
     68                $this->assertSame( 2, wp_get_top_level_comments_count( $p ) );
     69        }
     70}
  • tests/phpunit/tests/link/getNextCommentsLink.php

    diff --git tests/phpunit/tests/link/getNextCommentsLink.php tests/phpunit/tests/link/getNextCommentsLink.php
    index 4459fa6..03851b3 100644
     
    88class Tests_Link_GetNextCommentsLink extends WP_UnitTestCase {
    99
    1010        public function test_page_should_respect_value_of_cpage_query_var() {
     11                update_option( 'page_comments', '1' );
    1112                $p = $this->factory->post->create();
    1213                $this->go_to( get_permalink( $p ) );
    1314
    class Tests_Link_GetNextCommentsLink extends WP_UnitTestCase { 
    2526         * @ticket 20319
    2627         */
    2728        public function test_page_should_default_to_1_when_no_cpage_query_var_is_found() {
     29                update_option( 'page_comments', '1' );
    2830                $p = $this->factory->post->create();
    2931                $this->go_to( get_permalink( $p ) );
    3032
  • tests/phpunit/tests/link/getPreviousCommentsLink.php

    diff --git tests/phpunit/tests/link/getPreviousCommentsLink.php tests/phpunit/tests/link/getPreviousCommentsLink.php
    index 4aa336d..187b874 100644
     
    88class Tests_Link_GetPreviousCommentsLink extends WP_UnitTestCase {
    99
    1010        public function test_page_should_respect_value_of_cpage_query_var() {
     11                update_option( 'page_comments', '1' );
    1112                $p = $this->factory->post->create();
    1213                $this->go_to( get_permalink( $p ) );
    1314
    class Tests_Link_GetPreviousCommentsLink extends WP_UnitTestCase { 
    2223        }
    2324
    2425        public function test_page_should_default_to_1_when_no_cpage_query_var_is_found() {
     26                update_option( 'page_comments', '1' );
    2527                $p = $this->factory->post->create();
    2628                $this->go_to( get_permalink( $p ) );
    2729