diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
index 5487c8fbe4..c3b07e487b 100644
|
|
add_filter( 'rest_authentication_errors', 'rest_cookie_check_errors', 100 ); |
268 | 268 | // Actions |
269 | 269 | add_action( 'wp_head', '_wp_render_title_tag', 1 ); |
270 | 270 | add_action( 'wp_head', 'wp_enqueue_scripts', 1 ); |
| 271 | add_action( 'template_redirect', 'wp_dns_prefetch_control_header', 11, 0 ); |
271 | 272 | add_action( 'wp_head', 'wp_resource_hints', 2 ); |
272 | 273 | add_action( 'wp_head', 'feed_links', 2 ); |
273 | 274 | add_action( 'wp_head', 'feed_links_extra', 3 ); |
diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php
index 57a7b675dc..92d082b08f 100644
|
|
function wp_site_icon() { |
2913 | 2913 | } |
2914 | 2914 | |
2915 | 2915 | /** |
2916 | | * Prints resource hints to browsers for pre-fetching, pre-rendering |
2917 | | * and pre-connecting to web sites. |
| 2916 | * Returns the unique list of URLs and hosts that can be used for resource hints. |
2918 | 2917 | * |
2919 | | * Gives hints to browsers to prefetch specific pages or render them |
| 2918 | * Resource hints suggest browsers to prefetch specific pages or render them |
2920 | 2919 | * in the background, to perform DNS lookups or to begin the connection |
2921 | 2920 | * handshake (DNS, TCP, TLS) in the background. |
2922 | 2921 | * |
2923 | | * These performance improving indicators work by using `<link rel"…">`. |
2924 | | * |
2925 | | * @since 4.6.0 |
| 2922 | * @since 5.0.0 |
2926 | 2923 | */ |
2927 | | function wp_resource_hints() { |
| 2924 | function wp_get_resource_hints() { |
2928 | 2925 | $hints = array( |
2929 | 2926 | 'dns-prefetch' => wp_dependencies_unique_hosts(), |
2930 | 2927 | 'preconnect' => array(), |
… |
… |
function wp_resource_hints() { |
2995 | 2992 | $unique_urls[ $url ] = $atts; |
2996 | 2993 | } |
2997 | 2994 | |
2998 | | foreach ( $unique_urls as $atts ) { |
2999 | | $html = ''; |
| 2995 | return $unique_urls; |
| 2996 | } |
| 2997 | } |
3000 | 2998 | |
3001 | | foreach ( $atts as $attr => $value ) { |
3002 | | if ( ! is_scalar( $value ) || |
3003 | | ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr ) ) ) { |
| 2999 | /** |
| 3000 | * Sends an X-DNS-Prefetch-Control response header. |
| 3001 | * |
| 3002 | * By default, the header is only sent if dns-prefetch resource hints are added. |
| 3003 | * |
| 3004 | * @since 5.0.0 |
| 3005 | */ |
| 3006 | function wp_dns_prefetch_control_header() { |
| 3007 | $unique_urls = wp_get_resource_hints(); |
3004 | 3008 | |
3005 | | continue; |
3006 | | } |
| 3009 | /** |
| 3010 | * Filters whether WordPress should send an X-DNS-Prefetch-Control header. |
| 3011 | * |
| 3012 | * @since 5.0.0 |
| 3013 | * |
| 3014 | * @param bool $send_header Whether the header should be sent or not. |
| 3015 | * True if dns-prefetch resource hints are added, false otherwise. |
| 3016 | */ |
| 3017 | $send_header = apply_filters( 'dns_prefetch_control_header', ! empty( $unique_urls['dns-prefetch'] ) ); |
3007 | 3018 | |
3008 | | $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
| 3019 | if (!$send_header) { |
| 3020 | return; |
| 3021 | } |
3009 | 3022 | |
3010 | | if ( ! is_string( $attr ) ) { |
3011 | | $html .= " $value"; |
3012 | | } else { |
3013 | | $html .= " $attr='$value'"; |
3014 | | } |
| 3023 | header( 'X-DNS-Prefetch-Control: on', false ); |
| 3024 | } |
| 3025 | |
| 3026 | /** |
| 3027 | * Prints resource hints to browsers for pre-fetching, pre-rendering |
| 3028 | * and pre-connecting to web sites. |
| 3029 | * |
| 3030 | * Gives hints to browsers to prefetch specific pages or render them |
| 3031 | * in the background, to perform DNS lookups or to begin the connection |
| 3032 | * handshake (DNS, TCP, TLS) in the background. |
| 3033 | * |
| 3034 | * These performance improving indicators work by using `<link rel"…">`. |
| 3035 | * |
| 3036 | * @since 4.6.0 |
| 3037 | */ |
| 3038 | function wp_resource_hints() { |
| 3039 | $unique_urls = wp_get_resource_hints(); |
| 3040 | |
| 3041 | foreach ( $unique_urls as $atts ) { |
| 3042 | $html = ''; |
| 3043 | |
| 3044 | foreach ( $atts as $attr => $value ) { |
| 3045 | if ( ! is_scalar( $value ) || |
| 3046 | ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr ) ) ) { |
| 3047 | |
| 3048 | continue; |
3015 | 3049 | } |
3016 | 3050 | |
3017 | | $html = trim( $html ); |
| 3051 | $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
3018 | 3052 | |
3019 | | echo "<link $html />\n"; |
| 3053 | if ( ! is_string( $attr ) ) { |
| 3054 | $html .= " $value"; |
| 3055 | } else { |
| 3056 | $html .= " $attr='$value'"; |
| 3057 | } |
3020 | 3058 | } |
| 3059 | |
| 3060 | $html = trim( $html ); |
| 3061 | |
| 3062 | echo "<link $html />\n"; |
3021 | 3063 | } |
3022 | 3064 | } |
3023 | 3065 | |