Make WordPress Core

Ticket #33860: 33860.2.diff

File 33860.2.diff, 3.9 KB (added by swissspidy, 11 years ago)
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index 84eda07..9fae51a 100644
    function the_post() { 
    820820        $wp_query->the_post();
    821821}
    822822
     823/**
     824 * Get the current post's position in the current query.
     825 *
     826 * @since 4.4.0
     827 *
     828 * @global WP_Query $wp_query Global WP_Query instance.
     829 *
     830 * @return int The current post's position (zero-based).
     831 */
     832function query_post_index() {
     833        global $wp_query;
     834
     835        return $wp_query->current_post;
     836}
     837
     838/**
     839 * Whether the current post is the first one in the current query.
     840 *
     841 * @since 4.4.0
     842 *
     843 * @global WP_Query $wp_query Global WP_Query instance.
     844 *
     845 * @return bool True if it's the first post in the query, false otherwise.
     846 */
     847function query_is_first_post() {
     848        global $wp_query;
     849
     850        return $wp_query->is_first_post();
     851}
     852
     853/**
     854 * Whether the current post is the last one in the current query.
     855 *
     856 * @since 4.4.0
     857 *
     858 * @global WP_Query $wp_query Global WP_Query instance.
     859 *
     860 * @return bool True if it's the last post in the query, false otherwise.
     861 */
     862function query_is_last_post() {
     863        global $wp_query;
     864
     865        return $wp_query->is_last_post();
     866}
     867
    823868/*
    824869 * Comments loop.
    825870 */
    class WP_Query { 
    38743919        }
    38753920
    38763921        /**
     3922         * Whether the current post is the first one in the query.
     3923         *
     3924         * @since 4.4.0
     3925         *
     3926         * @return bool True if it's the first post in the query, false otherwise.
     3927         */
     3928        function is_first_post() {
     3929                return 0 === $this->current_post;
     3930        }
     3931
     3932        /**
     3933         * Whether the current post is the last one in the current query.
     3934         *
     3935         * @since 4.4.0
     3936         *
     3937         * @return bool True if it's the last post in the query, false otherwise.
     3938         */
     3939        function is_last_post() {
     3940                return $this->current_post === ( $this->post_count - 1 );
     3941        }
     3942
     3943        /**
    38773944         * Whether there are more posts available in the loop.
    38783945         *
    38793946         * Calls action 'loop_end', when the loop is complete.
  • tests/phpunit/tests/query.php

    diff --git tests/phpunit/tests/query.php tests/phpunit/tests/query.php
    index b5ab027..6e46eca 100644
    class Tests_Query extends WP_UnitTestCase { 
    492492
    493493                $this->assertContains( 'LIMIT 5, 5', $q->request );
    494494        }
     495
     496        /**
     497         * @ticket 33860
     498         */
     499        public function test_query_index() {
     500                self::factory()->post->create_many( 5 );
     501
     502                $q = new WP_Query( array(
     503                        'posts_per_page' => -1,
     504                ) );
     505
     506                $actual = array();
     507                $index = array();
     508
     509                while ( $q->have_posts() ) {
     510                        $q->the_post();
     511
     512                        $index[] = $q->current_post;
     513
     514                        if ( $q->is_first_post() ) {
     515                                $actual[] = 'first';
     516                        } else if ( $q->is_last_post() ) {
     517                                $actual[] = 'last';
     518                        } else {
     519                                $actual[] = 'middle';
     520                        }
     521                }
     522
     523                $this->assertSame( array( 'first', 'middle', 'middle', 'middle', 'last' ), $actual );
     524                $this->assertSame( array( 0, 1, 2, 3, 4 ), $index );
     525        }
     526
     527        /**
     528         * @ticket 33860
     529         */
     530        public function test_query_index_paginated() {
     531                self::factory()->post->create_many( 4 );
     532
     533                $q = new WP_Query( array(
     534                        'posts_per_page' => 2,
     535                ) );
     536
     537                $actual = array();
     538                $index = array();
     539
     540                while ( $q->have_posts() ) {
     541                        $q->the_post();
     542
     543                        $index[] = $q->current_post;
     544
     545                        if ( $q->is_first_post() ) {
     546                                $actual[] = 'first';
     547                        } else if ( $q->is_last_post() ) {
     548                                $actual[] = 'last';
     549                        } else {
     550                                $actual[] = 'middle';
     551                        }
     552                }
     553
     554                $this->assertSame( array( 'first', 'last' ), $actual );
     555                $this->assertSame( array( 0, 1 ), $index );
     556
     557                $q = new WP_Query( array(
     558                        'posts_per_page' => 2,
     559                        'paged'          => 2,
     560                ) );
     561
     562                $actual = array();
     563                $index = array();
     564
     565                while ( $q->have_posts() ) {
     566                        $q->the_post();
     567
     568                        $index[] = $q->current_post;
     569
     570                        if ( $q->is_first_post() ) {
     571                                $actual[] = 'first';
     572                        } else if ( $q->is_last_post() ) {
     573                                $actual[] = 'last';
     574                        } else {
     575                                $actual[] = 'middle';
     576                        }
     577                }
     578
     579                $this->assertSame( array( 'first', 'last' ), $actual );
     580                $this->assertSame( array( 0, 1 ), $index );
     581        }
    495582}