Changeset 38826
- Timestamp:
- 10/19/2016 09:28:22 AM (8 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/general-template.php
r38786 r38826 2831 2831 2832 2832 foreach ( $hints as $relation_type => $urls ) { 2833 $unique_urls = array(); 2834 2833 2835 /** 2834 2836 * Filters domains and URLs for resource hints of relation type. … … 2842 2844 2843 2845 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 2844 2857 $url = esc_url( $url, array( 'http', 'https' ) ); 2858 2845 2859 if ( ! $url ) { 2846 unset( $urls[ $key ] );2847 2860 continue; 2848 2861 } 2849 2862 2863 if ( isset( $unique_urls[ $url ] ) ) { 2864 continue; 2865 } 2866 2850 2867 if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ) ) ) { 2851 2868 $parsed = wp_parse_url( $url ); 2869 2852 2870 if ( empty( $parsed['host'] ) ) { 2853 unset( $urls[ $key ] );2854 2871 continue; 2855 2872 } … … 2863 2880 } 2864 2881 2865 $urls[ $key ] = $url; 2882 $atts['rel'] = $relation_type; 2883 $atts['href'] = $url; 2884 2885 $unique_urls[ $url ] = $atts; 2866 2886 } 2867 2887 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"; 2872 2910 } 2873 2911 } -
trunk/tests/phpunit/tests/general/resourceHints.php
r38447 r38826 2 2 3 3 /** 4 * @group 4 * @group template 5 5 * @ticket 34292 6 6 */ … … 243 243 return $hints; 244 244 } 245 246 /** 247 * @group 38121 248 */ 249 function test_custom_attributes() { 250 $expected = "<link rel='dns-prefetch' href='//s.w.org' />\n" . 251 "<link rel='preconnect' href='https://make.wordpress.org' />\n" . 252 "<link crossorigin as='image' pr='0.5' href='https://example.com/foo.jpeg' rel='prefetch' />\n" . 253 "<link crossorigin='use-credentials' as='style' href='https://example.com/foo.css' rel='prefetch' />\n" . 254 "<link href='http://wordpress.org' rel='prerender' />\n"; 255 256 add_filter( 'wp_resource_hints', array( $this, '_add_url_with_attributes' ), 10, 2 ); 257 258 $actual = get_echo( 'wp_resource_hints' ); 259 260 remove_filter( 'wp_resource_hints', array( $this, '_add_url_with_attributes' ) ); 261 262 $this->assertEquals( $expected, $actual ); 263 } 264 265 function _add_url_with_attributes( $hints, $method ) { 266 // Ignore hints with missing href attributes. 267 $hints[] = array( 268 'rel' => 'foo', 269 ); 270 271 if ( 'preconnect' === $method ) { 272 // Should ignore rel attributes. 273 $hints[] = array( 274 'rel' => 'foo', 275 'href' => 'https://make.wordpress.org/great-again', 276 ); 277 } elseif ( 'prefetch' === $method ) { 278 $hints[] = array( 279 'crossorigin', 280 'as' => 'image', 281 'pr' => 0.5, 282 'href' => 'https://example.com/foo.jpeg', 283 ); 284 $hints[] = array( 285 'crossorigin' => 'use-credentials', 286 'as' => 'style', 287 'href' => 'https://example.com/foo.css', 288 ); 289 } elseif ( 'prerender' === $method ) { 290 // Ignore invalid attributes. 291 $hints[] = array( 292 'foo' => 'bar', 293 'bar' => 'baz', 294 'href' => 'http://wordpress.org', 295 ); 296 } 297 298 return $hints; 299 } 245 300 }
Note: See TracChangeset
for help on using the changeset viewer.