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.
Built from https://develop.svn.wordpress.org/trunk@47018


git-svn-id: http://core.svn.wordpress.org/trunk@46818 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov
2019-12-28 21:20:04 +00:00
parent c546aa36ec
commit d858656023
11 changed files with 88 additions and 17 deletions

View File

@@ -390,6 +390,14 @@ class WP_Query {
*/
public $is_robots = false;
/**
* Signifies whether the current query is for the favicon.ico file.
*
* @since 5.4.0
* @var bool
*/
public $is_favicon = false;
/**
* Signifies whether the current query is for the page_for_posts page.
*
@@ -478,6 +486,7 @@ class WP_Query {
$this->is_attachment = false;
$this->is_singular = false;
$this->is_robots = false;
$this->is_favicon = false;
$this->is_posts_page = false;
$this->is_post_type_archive = false;
}
@@ -744,6 +753,8 @@ class WP_Query {
if ( ! empty( $qv['robots'] ) ) {
$this->is_robots = true;
} elseif ( ! empty( $qv['favicon'] ) ) {
$this->is_favicon = true;
}
if ( ! is_scalar( $qv['p'] ) || $qv['p'] < 0 ) {
@@ -957,7 +968,9 @@ class WP_Query {
$this->is_comment_feed = true;
}
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 ) ) {
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 || $this->is_favicon ) ) {
$this->is_home = true;
}
@@ -4007,7 +4020,7 @@ class WP_Query {
}
/**
* Is the query for the robots file?
* Is the query for the robots.txt file?
*
* @since 3.1.0
*
@@ -4017,6 +4030,17 @@ class WP_Query {
return (bool) $this->is_robots;
}
/**
* Is the query for the favicon.ico file?
*
* @since 5.4.0
*
* @return bool
*/
public function is_favicon() {
return (bool) $this->is_favicon;
}
/**
* Is the query for a search?
*