Make WordPress Core

Ticket #30210: 30210.8.patch

File 30210.8.patch, 8.5 KB (added by stevenkword, 7 years ago)

No more global $wp_query

  • src/wp-includes/functions.php

     
    12131213function do_feed() {
    12141214        global $wp_query;
    12151215
     1216        // Determine if we are looking at the main comment feed
     1217        $is_main_comments_feed = ( $wp_query->is_comment_feed() && ! $wp_query->is_singular() ) ? true : false;
     1218
     1219        /*
     1220         * Check the queried object for the existence of posts if it is not a feed for an archive,
     1221         * search result, or main comments. By checking for the absense of posts we can prevent rendering the feed
     1222         * templates at invalid endpoints. e.g.) /wp-content/plugins/feed/
     1223         *
     1224         */
     1225        if ( ! $wp_query->have_posts() && ! ( $wp_query->is_archive() || $wp_query->is_post_type_archive() || $wp_query->is_search() || $is_main_comments_feed ) ) {
     1226                wp_die( __( 'ERROR: This is not a valid feed.' ), '', array( 'response' => 404 ) );
     1227        }
     1228
    12161229        $feed = get_query_var( 'feed' );
    12171230
    12181231        // Remove the pad, if present.
  • tests/phpunit/tests/feed/rss2.php

     
    1212        static $user_id;
    1313        static $posts;
    1414        static $category;
     15        static $post_date;
    1516
    1617        /**
    1718         * Setup a new user and attribute some posts.
     
    2526                ) );
    2627
    2728                // Create a taxonomy
    28                 self::$category = self::factory()->category->create_and_get( array(
    29                         'name' => 'Test Category',
    30                         'slug' => 'test-cat',
     29                self::$category = $factory->category->create_and_get( array(
     30                        'name' => 'Foo Category',
     31                        'slug' => 'foo',
    3132                ) );
    3233
     34                // Set a predictable time for testing date archives.
     35                self::$post_date = '2003-05-27 10:07:53';
     36
    3337                $count = get_option( 'posts_per_rss' ) + 1;
    3438
    3539                // Create a few posts
    3640                self::$posts = $factory->post->create_many( $count, array(
    3741                        'post_author'  => self::$user_id,
     42                        'post_date'    => self::$post_date,
    3843                        'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec velit massa, ultrices eu est suscipit, mattis posuere est. Donec vitae purus lacus. Cras vitae odio odio.',
    3944                        'post_excerpt' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
    4045                ) );
     
    5560                $this->excerpt_only = get_option( 'rss_use_excerpt' );
    5661                // this seems to break something
    5762                update_option( 'use_smilies', false );
     63
     64                $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     65                create_initial_taxonomies();
    5866        }
    5967
    6068        /**
     
    250258                remove_filter( 'comments_open', '__return_false' );
    251259        }
    252260
     261        /*
     262         * Check to make sure we are rendering feed templates for the home feed.
     263         * e.g.) https://example.com/feed/
     264         *
     265         * @ticket 30210
     266         */
     267        function test_valid_home_feed_endpoint() {
     268                // An example of a valid home feed endpoint.
     269                $this->go_to( 'feed/' );
     270
     271                // Verify the query object is a feed.
     272                $this->assertQueryTrue( 'is_feed' );
     273
     274                // Queries performed on valid feed endpoints should contain posts.
     275                $this->assertTrue( have_posts() );
     276
     277                // Check to see if we have the expected XML output from the feed template.
     278                $feed = $this->do_rss2();
     279
     280                $xml = xml_to_array( $feed );
     281
     282                // Get the <rss> child element of <xml>.
     283                $rss = xml_find( $xml, 'rss' );
     284
     285                // There should only be one <rss> child element.
     286                $this->assertEquals( 1, count( $rss ) );
     287        }
     288
     289        /*
     290         * Check to make sure we are rendering feed templates for the taxonomy feeds.
     291         * e.g.) https://example.com/category/foo/feed/
     292         *
     293         * @ticket 30210
     294         */
     295        function test_valid_taxonomy_feed_endpoint() {
     296                // An example of an valid taxonomy feed endpoint.
     297                $this->go_to( 'category/foo/feed/' );
     298
     299                // Verify the query object is a feed.
     300                $this->assertQueryTrue( 'is_feed', 'is_archive', 'is_category' );
     301
     302                // Queries performed on valid feed endpoints should contain posts.
     303                $this->assertTrue( have_posts() );
     304
     305                // Check to see if we have the expected XML output from the feed template.
     306                $feed = $this->do_rss2();
     307
     308                // Check to see if we have the expected XML output from the feed template.
     309                $xml = xml_to_array( $feed );
     310
     311                // Get the <rss> child element of <xml>.
     312                $rss = xml_find( $xml, 'rss' );
     313
     314                // There should only be one <rss> child element.
     315                $this->assertEquals( 1, count( $rss ) );
     316        }
     317
     318        /*
     319         * Check to make sure we are rendering feed templates for the main comment feed.
     320         * e.g.) https://example.com/comments/feed/
     321         *
     322         * @ticket 30210
     323         */
     324        function test_valid_main_comment_feed_endpoint() {
     325                // Assign a category to those posts
     326                foreach ( self::$posts as $post ) {
     327                        self::factory()->comment->create_post_comments( $post, 3 );
     328                }
     329
     330                // An example of an valid main comment feed endpoint.
     331                $this->go_to( 'comments/feed/' );
     332
     333                // Verify the query object is a feed.
     334                $this->assertQueryTrue( 'is_feed', 'is_comment_feed' );
     335
     336                // Queries performed on valid feed endpoints should contain comments.
     337                $this->assertTrue( have_comments() );
     338
     339                // Check to see if we have the expected XML output from the feed template.
     340                $feed = $this->do_rss2();
     341
     342                // Check to see if we have the expected XML output from the feed template.
     343                $xml = xml_to_array( $feed );
     344
     345                // Get the <rss> child element of <xml>.
     346                $rss = xml_find( $xml, 'rss' );
     347
     348                // There should only be one <rss> child element.
     349                $this->assertEquals( 1, count( $rss ) );
     350        }
     351
     352        /*
     353         * Check to make sure we are rendering feed templates for the date archive feeds.
     354         * e.g.) https://example.com/2003/05/27/feed/
     355         *
     356         * @ticket 30210
     357         */
     358        function test_valid_archive_feed_endpoint() {
     359                // An example of an valid date archive feed endpoint.
     360                $this->go_to( '2003/05/27/feed/' );
     361
     362                // Verify the query object is a feed.
     363                $this->assertQueryTrue( 'is_feed', 'is_archive', 'is_day', 'is_date' );
     364
     365                // Queries performed on valid feed endpoints should contain posts.
     366                $this->assertTrue( have_posts() );
     367
     368                // Check to see if we have the expected XML output from the feed template.
     369                $feed = $this->do_rss2();
     370
     371                // Check to see if we have the expected XML output from the feed template.
     372                $xml = xml_to_array( $feed );
     373
     374                // Get the <rss> child element of <xml>.
     375                $rss = xml_find( $xml, 'rss' );
     376
     377                // There should only be one <rss> child element.
     378                $this->assertEquals( 1, count( $rss ) );
     379        }
     380
     381
     382        /*
     383         * Check to make sure we are rendering feed templates for the search archive feeds.
     384         * e.g.) https://example.com/?s=Lorem&feed=rss
     385         *
     386         * @ticket 30210
     387         */
     388        function test_valid_search_feed_endpoint() {
     389                // An example of an valid search feed endpoint
     390                $this->go_to( '?s=Lorem&feed=rss' );
     391
     392                // Verify the query object is a feed.
     393                $this->assertQueryTrue( 'is_feed', 'is_search' );
     394
     395                // Queries performed on valid feed endpoints should contain posts.
     396                $this->assertTrue( have_posts() );
     397
     398                // Check to see if we have the expected XML output from the feed template.
     399                $feed = $this->do_rss2();
     400
     401                // Check to see if we have the expected XML output from the feed template.
     402                $xml = xml_to_array( $feed );
     403
     404                // Get the <rss> child element of <xml>.
     405                $rss = xml_find( $xml, 'rss' );
     406
     407                // There should only be one <rss> child element.
     408                $this->assertEquals( 1, count( $rss ) );
     409        }
     410
     411        /*
     412         * Check to make sure we are not rendering feed templates for invalid feed endpoints.
     413         * e.g.) https://example.com/wp-content/feed/
     414         *
     415         * @ticket 30210
     416         */
     417        function test_invalid_feed_endpoint() {
     418                // An example of an invalid feed endpoint
     419                $this->go_to( 'wp-content/feed/' );
     420
     421                // Queries performed on invalid feed endpoints should never contain posts.
     422                $this->assertFalse( have_posts() );
     423
     424                // This is the assertion.  Once the exception is thrown in do_feed, execution stops, preventing futher assertions.
     425                $this->setExpectedException( 'WPDieException', 'ERROR: This is not a valid feed.' );
     426                do_feed();
     427        }
     428
     429        /*
     430         * Make sure the requested feed is registered before rendering the requested template.
     431         *
     432         * @ticket 30210
     433         */
     434        function test_nonexistent_feeds() {
     435                global $wp_rewrite;
     436                $feedname = 'badfeed';
     437
     438                // Unregister the feed if it exists
     439                if ( in_array( $feedname, $wp_rewrite->feeds ) ) {
     440                        unset( $wp_rewrite->feeds->feedname );
     441                        $hook = 'do_feed_' . $feedname;
     442                        // Remove default function hook
     443                        remove_action( $hook, $hook );
     444                }
     445
     446                $this->go_to( '/?feed=' . $feedname );
     447
     448                // This is the assertion.  Once the exception is thrown in do_feed, execution stops, preventing futher assertions.
     449                $this->setExpectedException( 'WPDieException', 'ERROR: This is not a valid feed template.' );
     450                do_feed();
     451        }
     452
    253453}