WordPress.org

Make WordPress Core

Ticket #39157: 39157.3.patch

File 39157.3.patch, 4.6 KB (added by stevenkword, 3 years ago)

Adds WP_Query::is_custom_feed()

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

     
    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
     
    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;
     
    925945                                $this->is_archive = true;
    926946                }
    927947
    928                 if ( '' != $qv['feed'] )
     948                if ( '' != $qv['feed'] ) {
    929949                        $this->is_feed = true;
     950                        if ( ! in_array( $qv['feed'], array( 'feed', 'rss', 'rss2', 'atom', 'rdf' ) ) ) {
     951                                $this->is_custom_feed = true;
     952                        }
     953                }
    930954
    931955                if ( '' != $qv['embed'] ) {
    932956                        $this->is_embed = true;
     
    955979                if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
    956980                        $this->is_comment_feed = true;
    957981
    958                 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 ) )
    959                         $this->is_home = true;
    960982
     983
     984                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 ) ) {
     985                        if ( $this->is_feed ) {
     986                                $this->is_home_feed = true;
     987                        } else {
     988                                $this->is_home = true;
     989                        }
     990
     991                }
     992
    961993                // Correct is_* for page_on_front and page_for_posts
    962994                if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
    963995                        $_query = wp_parse_args($this->query);
     
    36663698        }
    36673699
    36683700        /**
     3701         * Is the query for a hompage feed?
     3702         *
     3703         * @since 4.7.1
     3704         *
     3705         * @return bool
     3706         */
     3707        public function is_home_feed() {
     3708                return (bool) $this->is_home_feed;
     3709        }
     3710
     3711        /**
     3712         * Is the query for a custom feed?
     3713         *
     3714         * @since 4.7.1
     3715         *
     3716         * @return bool
     3717         */
     3718        public function is_custom_feed() {
     3719                return (bool) $this->is_custom_feed;
     3720        }
     3721
     3722        /**
    36693723         * Is the query for the front page of the site?
    36703724         *
    36713725         * This is for what is displayed at your site's main URL.
  • src/wp-includes/functions.php

     
    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

     
    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.