WordPress.org

Make WordPress Core

Changeset 47018


Ignore:
Timestamp:
12/28/2019 09:18:03 PM (4 weeks ago)
Author:
SergeyBiryukov
Message:

Bootstrap/Load: Make handling the /favicon.ico requests more flexible.

Previously, wp_favicon_request() was introduced in [13205] to avoid a performance hit of serving a full 404 page on every favicon request.

While working as intended, that implementation did not provide a way for theme or plugin authors to manage the behavior of favicon requests.

This changeset implements the following logic (only applied if WordPress is installed in the root directory):

  • If there is a Site Icon set in Customizer, redirect /favicon.ico requests to that icon.
  • Otherwise, use the WordPress logo as a default icon.
  • If a physical /favicon.ico file exists, do nothing, let the server handle the request.

Handling /favicon.ico is now more consistent with handling /robots.txt requests.

New functions and hooks:

  • Introduce is_favicon() conditional tag to complement is_robots().
  • Introduce do_favicon action to complement do_robots and use it in template loader.
  • Introduce do_favicon() function, hooked to the above action by default, to complement do_robots().
  • Introduce do_faviconico action to complement do_robotstxt, for plugins to override the default behavior.
  • Mark wp_favicon_request() as deprecated in favor of do_favicon().

Props jonoaldersonwp, birgire, joostdevalk, mukesh27, SergeyBiryukov.
Fixes #47398.

Location:
trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/canonical.php

    r45739 r47018  
    2020 * Prevents redirection for feeds, trackbacks, searches, and
    2121 * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
    22  * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST
    23  * requests.
     22 * page/post previews, WP admin, Trackbacks, robots.txt, favicon.ico, searches,
     23 * or on POST requests.
    2424 *
    2525 * Will also attempt to find the correct link when a user enters a URL that does
     
    5757    }
    5858
    59     if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && ! iis7_supports_permalinks() ) ) {
     59    if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || is_favicon() || ( $is_IIS && ! iis7_supports_permalinks() ) ) {
    6060        return;
    6161    }
  • trunk/src/wp-includes/class-wp-query.php

    r46661 r47018  
    390390     */
    391391    public $is_robots = false;
     392
     393    /**
     394     * Signifies whether the current query is for the favicon.ico file.
     395     *
     396     * @since 5.4.0
     397     * @var bool
     398     */
     399    public $is_favicon = false;
    392400
    393401    /**
     
    479487        $this->is_singular          = false;
    480488        $this->is_robots            = false;
     489        $this->is_favicon           = false;
    481490        $this->is_posts_page        = false;
    482491        $this->is_post_type_archive = false;
     
    745754        if ( ! empty( $qv['robots'] ) ) {
    746755            $this->is_robots = true;
     756        } elseif ( ! empty( $qv['favicon'] ) ) {
     757            $this->is_favicon = true;
    747758        }
    748759
     
    958969        }
    959970
    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 ) ) {
     971        if ( ! ( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed
     972                || ( defined( 'REST_REQUEST' ) && REST_REQUEST )
     973                || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon ) ) {
    961974            $this->is_home = true;
    962975        }
     
    40084021
    40094022    /**
    4010      * Is the query for the robots file?
     4023     * Is the query for the robots.txt file?
    40114024     *
    40124025     * @since 3.1.0
     
    40164029    public function is_robots() {
    40174030        return (bool) $this->is_robots;
     4031    }
     4032
     4033    /**
     4034     * Is the query for the favicon.ico file?
     4035     *
     4036     * @since 5.4.0
     4037     *
     4038     * @return bool
     4039     */
     4040    public function is_favicon() {
     4041        return (bool) $this->is_favicon;
    40184042    }
    40194043
  • trunk/src/wp-includes/class-wp-rewrite.php

    r46685 r47018  
    12601260        }
    12611261
    1262         // robots.txt -only if installed at the root
     1262        // robots.txt -- only if installed at the root
    12631263        $home_path      = parse_url( home_url() );
    12641264        $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array();
     1265
     1266        // favicon.ico -- only if installed at the root
     1267        $favicon_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'favicon\.ico$' => $this->index . '?favicon=1' ) : array();
    12651268
    12661269        // Old feed and service files.
     
    14201423        // Put them together.
    14211424        if ( $this->use_verbose_page_rules ) {
    1422             $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules );
     1425            $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules );
    14231426        } else {
    1424             $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules );
     1427            $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules );
    14251428        }
    14261429
  • trunk/src/wp-includes/class-wp.php

    r46685 r47018  
    1515     * @var string[]
    1616     */
    17     public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage', 'post_type', 'embed' );
     17    public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'favicon', 'taxonomy', 'term', 'cpage', 'post_type', 'embed' );
    1818
    1919    /**
     
    660660        }
    661661
    662         // Never 404 for the admin, robots, or if we found posts.
    663         if ( is_admin() || is_robots() || $wp_query->posts ) {
     662        // Never 404 for the admin, robots, favicon, or if we found posts.
     663        if ( is_admin() || is_robots() || is_favicon() || $wp_query->posts ) {
    664664
    665665            $success = true;
  • trunk/src/wp-includes/default-filters.php

    r46896 r47018  
    339339add_action( 'do_pings', 'do_all_pings', 10, 0 );
    340340add_action( 'do_robots', 'do_robots' );
     341add_action( 'do_favicon', 'do_favicon' );
    341342add_action( 'set_comment_cookies', 'wp_set_comment_cookies', 10, 3 );
    342343add_action( 'sanitize_comment_cookies', 'sanitize_comment_cookies' );
  • trunk/src/wp-includes/functions.php

    r47002 r47018  
    16351635     */
    16361636    echo apply_filters( 'robots_txt', $output, $public );
     1637}
     1638
     1639/**
     1640 * Display the favicon.ico file content.
     1641 *
     1642 * @since 5.4.0
     1643 */
     1644function do_favicon() {
     1645    /**
     1646     * Fires when serving the favicon.ico file.
     1647     *
     1648     * @since 5.4.0
     1649     */
     1650    do_action( 'do_faviconico' );
     1651
     1652    wp_redirect( get_site_icon_url( 32, admin_url( 'images/w-logo-blue.png' ) ) );
     1653    exit;
    16371654}
    16381655
  • trunk/src/wp-includes/load.php

    r46760 r47018  
    161161 *
    162162 * @since 3.0.0
     163 * @deprecated 5.4.0 Deprecated in favor of do_favicon().
    163164 */
    164165function wp_favicon_request() {
  • trunk/src/wp-includes/query.php

    r46660 r47018  
    624624
    625625/**
    626  * Is the query for the robots file?
     626 * Is the query for the robots.txt file?
    627627 *
    628628 * @since 2.1.0
     
    641641
    642642    return $wp_query->is_robots();
     643}
     644
     645/**
     646 * Is the query for the favicon.ico file?
     647 *
     648 * @since 5.4.0
     649 *
     650 * @global WP_Query $wp_query WordPress Query object.
     651 *
     652 * @return bool
     653 */
     654function is_favicon() {
     655    global $wp_query;
     656
     657    if ( ! isset( $wp_query ) ) {
     658        _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
     659        return false;
     660    }
     661
     662    return $wp_query->is_favicon();
    643663}
    644664
  • trunk/src/wp-includes/template-loader.php

    r45590 r47018  
    3636     */
    3737    do_action( 'do_robots' );
     38    return;
     39} elseif ( is_favicon() ) {
     40    /**
     41     * Fired when the template loader determines a favicon.ico request.
     42     *
     43     * @since 5.4.0
     44     */
     45    do_action( 'do_favicon' );
    3846    return;
    3947} elseif ( is_feed() ) {
  • trunk/src/wp-includes/version.php

    r46582 r47018  
    2121 * @global int $wp_db_version
    2222 */
    23 $wp_db_version = 45805;
     23$wp_db_version = 47018;
    2424
    2525/**
  • trunk/src/wp-settings.php

    r46543 r47018  
    6363// Standardize $_SERVER variables across setups.
    6464wp_fix_server_vars();
    65 
    66 // Check if we have received a request due to missing favicon.ico
    67 wp_favicon_request();
    6865
    6966// Check if we're in maintenance mode.
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r47001 r47018  
    963963            'is_preview',
    964964            'is_robots',
     965            'is_favicon',
    965966            'is_search',
    966967            'is_single',
  • trunk/tests/phpunit/tests/query/vars.php

    r46586 r47018  
    6161                'preview',
    6262                'robots',
     63                'favicon',
    6364                'taxonomy',
    6465                'term',
Note: See TracChangeset for help on using the changeset viewer.