Make WordPress Core

Ticket #24142: 24142.2.diff

File 24142.2.diff, 5.0 KB (added by Howdy_McGee, 4 years ago)

Patch refreshed to only include updates to posts_per_page (not comments_per_page or posts_per_rss). Includes test for posts_per_page based on the table from reply #10. Progresses WP_Query tests in #49149. Ultimately makes posts_per_page more predictable.

  • src/wp-includes/class-wp-query.php

     
    19641964                        }
    19651965                }
    19661966                $post_type = $q['post_type'];
    1967                 if ( empty( $q['posts_per_page'] ) ) {
    1968                         $q['posts_per_page'] = get_option( 'posts_per_page' );
     1967                if( isset( $q['posts_per_page'] ) && ( is_numeric( $q['posts_per_page'] ) || is_bool( $q['posts_per_page'] ) ) ) {
     1968                        $q['posts_per_page'] = $q['posts_per_page'];
     1969                } else {
     1970                        $q['posts_per_page'] = get_option( 'posts_per_page', 10 );
    19691971                }
    1970                 if ( isset( $q['showposts'] ) && $q['showposts'] ) {
     1972                if( isset( $q['showposts'] ) && $q['showposts'] ) {
    19711973                        $q['showposts']      = (int) $q['showposts'];
    19721974                        $q['posts_per_page'] = $q['showposts'];
    19731975                }
    1974                 if ( ( isset( $q['posts_per_archive_page'] ) && 0 != $q['posts_per_archive_page'] ) && ( $this->is_archive || $this->is_search ) ) {
     1976                if ( isset( $q['posts_per_archive_page'] ) && ( is_numeric( $q['posts_per_archive_page'] ) || is_bool( $q['posts_per_archive_page'] ) ) && ( $this->is_archive || $this->is_search ) ) {
    19751977                        $q['posts_per_page'] = $q['posts_per_archive_page'];
    19761978                }
    19771979                if ( ! isset( $q['nopaging'] ) ) {
    1978                         if ( -1 == $q['posts_per_page'] ) {
     1980                        if ( -1 == (int) $q['posts_per_page'] ) {
    19791981                                $q['nopaging'] = true;
    19801982                        } else {
    19811983                                $q['nopaging'] = false;
     
    19941996                $q['posts_per_page'] = (int) $q['posts_per_page'];
    19951997                if ( $q['posts_per_page'] < -1 ) {
    19961998                        $q['posts_per_page'] = abs( $q['posts_per_page'] );
    1997                 } elseif ( 0 == $q['posts_per_page'] ) {
    1998                         $q['posts_per_page'] = 1;
    19991999                }
    20002000
    20012001                if ( ! isset( $q['comments_per_page'] ) || 0 == $q['comments_per_page'] ) {
     
    35893589                 */
    35903590                $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    35913591
    3592                 if ( ! empty( $limits ) ) {
     3592                if ( ! empty( $limits ) && 0 != $q['posts_per_page'] ) {
    35933593                        $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
    35943594                }
    35953595        }
  • tests/phpunit/tests/query/postsPerPage.php

     
     1<?php
     2
     3/**
     4 * @group query
     5 */
     6class Tests_Query_PostsPerPage extends WP_UnitTestCase {
     7        public $q;
     8
     9        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     10                // More than posts_per_page default of 10
     11                // This number is verified in the 'test_posts_per_page_all' method.
     12                self::factory()->post->create_many( 11 );
     13        }
     14
     15        public function set_up() {
     16                parent::set_up();
     17                unset( $this->q );
     18                $this->q = new WP_Query();
     19        }
     20
     21        public function _get_post_count( $args = array() ) {
     22                $args = wp_parse_args(
     23                        $args,
     24                        array(
     25                                'fields' => 'ids',
     26                        )
     27                );
     28
     29                return count( $this->q->query( $args ) );
     30        }
     31
     32        public function test_posts_per_page_integer_positive() {
     33                $count = $this->_get_post_count( array(
     34                        'posts_per_page' => 2,
     35                ) );
     36
     37                $this->assertSame( 2, $count );
     38        }
     39
     40        /**
     41         * @ticket 24142
     42         */
     43        public function test_posts_per_page_integer_zero() {
     44                $count = $this->_get_post_count( array(
     45                        'posts_per_page' => 0,
     46                ) );
     47
     48                $this->assertSame( 0, $count );
     49        }
     50
     51        public function test_posts_per_page_string_numeric() {
     52                $count = $this->_get_post_count( array(
     53                        'posts_per_page' => '2',
     54                ) );
     55
     56                $this->assertSame( 2, $count );
     57        }
     58
     59        public function test_posts_per_page_string_non_numeric() {
     60                $count = $this->_get_post_count( array(
     61                        'posts_per_page' => 'foo',
     62                ) );
     63
     64                $this->assertSame( 10, $count );
     65        }
     66
     67        public function test_posts_per_page_boolean_true() {
     68                $count = $this->_get_post_count( array(
     69                        'posts_per_page' => true,
     70                ) );
     71
     72                $this->assertSame( 1, $count );
     73        }
     74
     75        /**
     76         * @ticket 24142
     77         */
     78        public function test_posts_per_page_boolean_false() {
     79                $count = $this->_get_post_count( array(
     80                        'posts_per_page' => false,
     81                ) );
     82
     83                $this->assertSame( 0, $count );
     84        }
     85
     86        public function test_posts_per_page_null() {
     87                $count = $this->_get_post_count( array(
     88                        'posts_per_page' => null,
     89                ) );
     90
     91                $this->assertSame( 10, $count );
     92        }
     93
     94        public function test_posts_per_page_empty_string() {
     95                $count = $this->_get_post_count( array(
     96                        'posts_per_page' => null,
     97                ) );
     98
     99                $this->assertSame( 10, $count );
     100        }
     101
     102        public function test_posts_per_page_array() {
     103                $count = $this->_get_post_count( array(
     104                        'posts_per_page' => array(),
     105                ) );
     106
     107                $this->assertSame( 10, $count );
     108        }
     109
     110        public function test_posts_per_page_negative() {
     111                $count = $this->_get_post_count( array(
     112                        'posts_per_page' => -2,
     113                ) );
     114
     115                $this->assertSame( 2, $count );
     116        }
     117
     118        public function test_posts_per_page_all() {
     119                $count = $this->_get_post_count( array(
     120                        'posts_per_page' => -1,
     121                ) );
     122
     123                $this->assertSame( 11, $count );
     124        }
     125}
     126 No newline at end of file