Make WordPress Core


Ignore:
Timestamp:
10/19/2016 09:28:22 AM (8 years ago)
Author:
swissspidy
Message:

Resource Hints: Allow passing custom attributes to resource hints.

[37920] introduced resource hints that allow browsers to prefetch specific pages or render them in the background. With this change, the as, crossorigin, pr, and type attributes can be passed in addition to the URLs/hosts.

Props peterwilsoncc, swissspidy.
Fixes #38121.

File:
1 edited

Legend:

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

    r38786 r38826  
    28312831
    28322832    foreach ( $hints as $relation_type => $urls ) {
     2833        $unique_urls = array();
     2834
    28332835        /**
    28342836         * Filters domains and URLs for resource hints of relation type.
     
    28422844
    28432845        foreach ( $urls as $key => $url ) {
     2846            $atts = array();
     2847
     2848            if ( is_array( $url ) ) {
     2849                if ( isset( $url['href'] ) ) {
     2850                    $atts = $url;
     2851                    $url  = $url['href'];
     2852                } else {
     2853                    continue;
     2854                }
     2855            }
     2856
    28442857            $url = esc_url( $url, array( 'http', 'https' ) );
     2858
    28452859            if ( ! $url ) {
    2846                 unset( $urls[ $key ] );
    28472860                continue;
    28482861            }
    28492862
     2863            if ( isset( $unique_urls[ $url ] ) ) {
     2864                continue;
     2865            }
     2866
    28502867            if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ) ) ) {
    28512868                $parsed = wp_parse_url( $url );
     2869
    28522870                if ( empty( $parsed['host'] ) ) {
    2853                     unset( $urls[ $key ] );
    28542871                    continue;
    28552872                }
     
    28632880            }
    28642881
    2865             $urls[ $key ] = $url;
     2882            $atts['rel'] = $relation_type;
     2883            $atts['href'] = $url;
     2884
     2885            $unique_urls[ $url ] = $atts;
    28662886        }
    28672887
    2868         $urls = array_unique( $urls );
    2869 
    2870         foreach ( $urls as $url ) {
    2871             printf( "<link rel='%s' href='%s' />\n", $relation_type, $url );
     2888        foreach ( $unique_urls as $atts ) {
     2889            $html = '';
     2890
     2891            foreach ( $atts as $attr => $value ) {
     2892                if ( ! is_scalar( $value ) ||
     2893                     ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr ))
     2894                ) {
     2895                    continue;
     2896                }
     2897
     2898                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
     2899
     2900                if ( ! is_string( $attr ) ) {
     2901                    $html .= " $value";
     2902                } else {
     2903                    $html .= " $attr='$value'";
     2904                }
     2905            }
     2906
     2907            $html = trim( $html );
     2908
     2909            echo "<link $html />\n";
    28722910        }
    28732911    }
Note: See TracChangeset for help on using the changeset viewer.