WordPress.org

Make WordPress Core

Ticket #39157: 39157.diff

File 39157.diff, 8.7 KB (added by peterwilsoncc, 3 years ago)
  • src/wp-includes/class-wp-query.php

    diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php
    index eb68f21..3b56ee5 100644
    class WP_Query { 
    346346        public $is_comment_feed = false;
    347347
    348348        /**
     349         * Set if query is home feed display.
     350         *
     351         * @since 4.7.1
     352         * @access public
     353         * @var bool
     354         */
     355        public $is_home_feed = false;
     356
     357        /**
     358         * Set if query is custom feed display.
     359         *
     360         * @since 4.7.1
     361         * @access public
     362         * @var bool
     363         */
     364        public $is_custom_feed = false;
     365
     366        /**
    349367         * Set if query is trackback.
    350368         *
    351369         * @since 1.5.0
    class WP_Query { 
    511529                $this->is_search = false;
    512530                $this->is_feed = false;
    513531                $this->is_comment_feed = false;
     532                $this->is_home_feed = false;
     533                $this->is_custom_feed = false;
    514534                $this->is_trackback = false;
    515535                $this->is_home = false;
    516536                $this->is_404 = false;
    class WP_Query { 
    927947                                $this->is_archive = true;
    928948                }
    929949
    930                 if ( '' != $qv['feed'] )
     950                if ( '' != $qv['feed'] ) {
    931951                        $this->is_feed = true;
     952                        if ( ! in_array( $qv['feed'], array( 'feed', 'rss', 'rss2', 'atom', 'rdf' ) ) ) {
     953                                $this->is_custom_feed = true;
     954                        }
     955                }
    932956
    933957                if ( '' != $qv['embed'] ) {
    934958                        $this->is_embed = true;
    class WP_Query { 
    957981                if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
    958982                        $this->is_comment_feed = true;
    959983
    960                 if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots ) )
    961                         $this->is_home = true;
     984
     985
     986                if ( !( $this->is_singular || $this->is_archive || $this->is_search || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_custom_feed ) ) {
     987                        if ( $this->is_feed ) {
     988                                if ( ! $this->is_comment_feed ) {
     989                                        $this->is_home_feed = true;
     990                                }
     991                        } else {
     992                                $this->is_home = true;
     993                        }
     994
     995                }
    962996
    963997                // Correct is_* for page_on_front and page_for_posts
    964998                if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
    class WP_Query { 
    36683702        }
    36693703
    36703704        /**
     3705         * Is the query for a hompage feed?
     3706         *
     3707         * @since 4.7.1
     3708         *
     3709         * @return bool
     3710         */
     3711        public function is_home_feed() {
     3712                return (bool) $this->is_home_feed;
     3713        }
     3714
     3715        /**
     3716         * Is the query for a custom feed?
     3717         *
     3718         * @since 4.7.1
     3719         *
     3720         * @return bool
     3721         */
     3722        public function is_custom_feed() {
     3723                return (bool) $this->is_custom_feed;
     3724        }
     3725
     3726        /**
    36713727         * Is the query for the front page of the site?
    36723728         *
    36733729         * This is for what is displayed at your site's main URL.
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 914cbb9..733a718 100644
    function do_feed() { 
    12211221         * search result, or main comments. By checking for the absense of posts we can prevent rendering the feed
    12221222         * templates at invalid endpoints. e.g.) /wp-content/plugins/feed/
    12231223         */
    1224         if ( ! $wp_query->have_posts() && ! ( $wp_query->is_archive() || $wp_query->is_search() || $is_main_comments_feed ) ) {
     1224        if ( ! $wp_query->have_posts() && ! ( $wp_query->is_archive() || $wp_query->is_search() || $is_main_comments_feed || $wp_query->is_home_feed() || $wp_query->is_custom_feed() ) ) {
    12251225                wp_die( __( 'ERROR: This is not a valid feed.' ), '', array( 'response' => 404 ) );
    12261226        }
    12271227
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index 744610f..db399f1 100644
    function is_comment_feed() { 
    383383}
    384384
    385385/**
     386 * Is the query for the blog homepage feed?
     387 *
     388 * @since 4.7.1
     389 *
     390 * @global WP_Query $wp_query Global WP_Query instance.
     391 *
     392 * @return bool
     393 */
     394function is_home_feed() {
     395        global $wp_query;
     396
     397        if ( ! isset( $wp_query ) ) {
     398                _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
     399                return false;
     400        }
     401
     402        return $wp_query->is_home_feed();
     403}
     404
     405/**
     406 * Is the query for the custom feed?
     407 *
     408 * @since 4.7.1
     409 *
     410 * @global WP_Query $wp_query Global WP_Query instance.
     411 *
     412 * @return bool
     413 */
     414function is_custom_feed() {
     415        global $wp_query;
     416
     417        if ( ! isset( $wp_query ) ) {
     418                _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
     419                return false;
     420        }
     421
     422        return $wp_query->is_custom_feed();
     423}
     424
     425/**
    386426 * Is the query for the front page of the site?
    387427 *
    388428 * This is for what is displayed at your site's main URL.
  • tests/phpunit/includes/testcase.php

    diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
    index 8990504..681bcd0 100644
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    597597                        'is_day',
    598598                        'is_embed',
    599599                        'is_feed',
     600                        'is_home_feed',
     601                        'is_custom_feed',
    600602                        'is_front_page',
    601603                        'is_home',
    602604                        'is_month',
  • tests/phpunit/tests/feed/rss2.php

    diff --git tests/phpunit/tests/feed/rss2.php tests/phpunit/tests/feed/rss2.php
    index fab3726..dd13839 100644
    class Tests_Feeds_RSS2 extends WP_UnitTestCase { 
    6363
    6464                $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    6565                create_initial_taxonomies();
     66
     67                // Add a custom feed
     68                add_feed( 'custom', 'do_rss2' );
     69
    6670        }
    6771
    6872        /**
    class Tests_Feeds_RSS2 extends WP_UnitTestCase { 
    296300                $this->go_to( 'feed/' );
    297301
    298302                // Verify the query object is a feed.
    299                 $this->assertQueryTrue( 'is_feed' );
     303                $this->assertQueryTrue( 'is_feed', 'is_home_feed' );
    300304
    301305                // Queries performed on valid feed endpoints should contain posts.
    302306                $this->assertTrue( have_posts() );
    class Tests_Feeds_RSS2 extends WP_UnitTestCase { 
    313317                $this->assertEquals( 1, count( $rss ) );
    314318        }
    315319
     320        function test_valid_custom_feed_endpoint() {
     321                // An example of a valid home feed endpoint.
     322                $this->go_to( 'feed/custom' );
     323
     324                // Verify the query object is a feed.
     325                $this->assertQueryTrue( 'is_feed', 'is_custom_feed' );
     326
     327                // Check to see if we have the expected XML output from the feed template.
     328                $feed = $this->do_rss2();
     329
     330                $xml = xml_to_array( $feed );
     331
     332                // Get the <rss> child element of <xml>.
     333                $rss = xml_find( $xml, 'rss' );
     334
     335                // There should only be one <rss> child element.
     336                $this->assertEquals( 1, count( $rss ) );
     337        }
     338
    316339        /*
    317340         * Check to make sure we are rendering feed templates for the taxonomy feeds.
    318341         * e.g. https://example.com/category/foo/feed/
    class Tests_Feeds_RSS2 extends WP_UnitTestCase { 
    408431         *
    409432         * @ticket 30210
    410433         */
    411         function test_valid_single_post_comment_feed_endpoint() {
     434        function test_valid_singular_comment_feed_endpoint() {
    412435                // An example of an valid date archive feed endpoint.
    413436                $this->go_to( get_post_comments_feed_link( self::$posts[0] ) );
    414437
  • tests/phpunit/tests/query/conditionals.php

    diff --git tests/phpunit/tests/query/conditionals.php tests/phpunit/tests/query/conditionals.php
    index 8cceaa5..323ba67 100644
    class Tests_Query_Conditionals extends WP_UnitTestCase { 
    253253                // long version
    254254                foreach ($feeds as $feed) {
    255255                        $this->go_to("/feed/{$feed}/");
    256                         $this->assertQueryTrue('is_feed');
     256                        $this->assertQueryTrue('is_feed', 'is_home_feed');
    257257                }
    258258
    259259                // short version
    260260                foreach ($feeds as $feed) {
    261261                        $this->go_to("/{$feed}/");
    262                         $this->assertQueryTrue('is_feed');
     262                        $this->assertQueryTrue('is_feed', 'is_home_feed');
    263263                }
    264264
    265265        }
    class Tests_Query_Conditionals extends WP_UnitTestCase { 
    269269                $types = array('rss2', 'rss', 'atom');
    270270                foreach ($types as $type) {
    271271                        $this->go_to(get_feed_link($type));
    272                         $this->assertQueryTrue('is_feed');
     272                        $this->assertQueryTrue('is_feed', 'is_home_feed');
    273273                }
    274274        }
    275275
    class Tests_Query_Conditionals extends WP_UnitTestCase { 
    369369                }
    370370        }
    371371
     372        function test_invalid_category_feed() {
     373                // check the long form
     374                $types = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
     375                foreach ( $types as $type ) {
     376                        $this->go_to( "/category/four-oh-four-cat/feed/{$type}/" );
     377                        $this->assertQueryTrue( 'is_404' );
     378                }
     379
     380                // check the short form
     381                $types = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
     382                foreach ( $types as $type ) {
     383                        $this->go_to( "/category/four-oh-four-cat/{$type}/" );
     384                        $this->assertQueryTrue( 'is_404' );
     385                }
     386        }
     387
    372388        // 'category/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?category_name=$matches[1]&paged=$matches[2]',
    373389        function test_category_paged() {
    374390                update_option( 'posts_per_page', 2 );