Ticket #38121: 38121.4.diff
| File 38121.4.diff, 5.7 KB (added by , 9 years ago) |
|---|
-
src/wp-includes/general-template.php
diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php index 23b2f1c..a5fa3ff 100644
function wp_resource_hints() { 2820 2820 'preconnect' => array(), 2821 2821 'prefetch' => array(), 2822 2822 'prerender' => array(), 2823 'preload' => array(), 2823 2824 ); 2824 2825 2825 2826 /* … … function wp_resource_hints() { 2830 2831 $hints['dns-prefetch'][] = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2.2.1/svg/' ); 2831 2832 2832 2833 foreach ( $hints as $relation_type => $urls ) { 2834 $unique_urls = array(); 2835 2833 2836 /** 2834 2837 * Filters domains and URLs for resource hints of relation type. 2835 2838 * … … function wp_resource_hints() { 2841 2844 $urls = apply_filters( 'wp_resource_hints', $urls, $relation_type ); 2842 2845 2843 2846 foreach ( $urls as $key => $url ) { 2847 $atts = array(); 2848 2849 if ( is_array( $url ) ) { 2850 if ( isset( $url['href'] ) ) { 2851 $atts = $url; 2852 $url = $url['href']; 2853 } else { 2854 continue; 2855 } 2856 } 2857 2844 2858 $url = esc_url( $url, array( 'http', 'https' ) ); 2859 2845 2860 if ( ! $url ) { 2846 unset( $urls[ $key ] ); 2861 continue; 2862 } 2863 2864 if ( isset( $unique_urls[ $url ] ) ) { 2847 2865 continue; 2848 2866 } 2849 2867 2850 2868 if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ) ) ) { 2851 2869 $parsed = wp_parse_url( $url ); 2870 2852 2871 if ( empty( $parsed['host'] ) ) { 2853 unset( $urls[ $key ] );2854 2872 continue; 2855 2873 } 2856 2874 … … function wp_resource_hints() { 2862 2880 } 2863 2881 } 2864 2882 2865 $urls[ $key ] = $url; 2883 $atts['rel'] = $relation_type; 2884 $atts['href'] = $url; 2885 2886 $unique_urls[ $url ] = $atts; 2866 2887 } 2867 2888 2868 $urls = array_unique( $urls ); 2889 foreach ( $unique_urls as $atts ) { 2890 $html = ''; 2891 2892 foreach ( $atts as $attr => $value ) { 2893 if ( ! is_scalar( $value ) || 2894 ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr )) 2895 ) { 2896 continue; 2897 } 2898 2899 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); 2900 2901 if ( ! is_string( $attr ) ) { 2902 $html .= " $value"; 2903 } else { 2904 $html .= " $attr='$value'"; 2905 } 2906 } 2907 2908 $html = trim( $html ); 2869 2909 2870 foreach ( $urls as $url ) { 2871 printf( "<link rel='%s' href='%s' />\n", $relation_type, $url ); 2910 echo "<link $html />\n"; 2872 2911 } 2873 2912 } 2874 2913 } -
tests/phpunit/tests/general/resourceHints.php
diff --git tests/phpunit/tests/general/resourceHints.php tests/phpunit/tests/general/resourceHints.php index a25d8bb..71b0d69 100644
1 1 <?php 2 2 3 3 /** 4 * @group template4 * @group template 5 5 * @ticket 34292 6 6 */ 7 7 class Tests_WP_Resource_Hints extends WP_UnitTestCase { … … class Tests_WP_Resource_Hints extends WP_UnitTestCase { 242 242 243 243 return $hints; 244 244 } 245 246 /** 247 * @group 38121 248 */ 249 function test_preload() { 250 $expected = "<link rel='dns-prefetch' href='//s.w.org' />\n" . 251 "<link rel='preload' href='https://make.wordpress.org/great-again' />\n" . 252 "<link rel='preload' href='http://jobs.wordpress.net' />\n" . 253 "<link rel='preload' href='//core.trac.wordpress.org' />\n"; 254 255 add_filter( 'wp_resource_hints', array( $this, '_add_preload_urls' ), 10, 2 ); 256 257 $actual = get_echo( 'wp_resource_hints' ); 258 259 remove_filter( 'wp_resource_hints', array( $this, '_add_preload_urls' ) ); 260 261 $this->assertEquals( $expected, $actual ); 262 } 263 264 function _add_preload_urls( $hints, $method ) { 265 if ( 'preload' === $method ) { 266 $hints[] = 'https://make.wordpress.org/great-again'; 267 $hints[] = 'http://jobs.wordpress.net'; 268 $hints[] = '//core.trac.wordpress.org'; 269 $hints[] = 'htps://wordpress.org'; // Invalid URLs should be skipped. 270 } 271 272 return $hints; 273 } 274 275 /** 276 * @group 38121 277 */ 278 function test_custom_attributes() { 279 $expected = "<link rel='dns-prefetch' href='//s.w.org' />\n" . 280 "<link rel='preconnect' href='https://make.wordpress.org' />\n" . 281 "<link pr='0.5' href='http://wordpress.org' rel='prefetch' />\n" . 282 "<link href='http://wordpress.org' rel='prerender' />\n" . 283 "<link crossorigin as='font' type='font/woff2' href='https://example.com/font.woff2' rel='preload' />\n" . 284 "<link crossorigin='use-credentials' as='style' href='https://example.com/foo.css' rel='preload' />\n"; 285 286 add_filter( 'wp_resource_hints', array( $this, '_add_url_with_attributes' ), 10, 2 ); 287 288 $actual = get_echo( 'wp_resource_hints' ); 289 290 remove_filter( 'wp_resource_hints', array( $this, '_add_url_with_attributes' ) ); 291 292 $this->assertEquals( $expected, $actual ); 293 } 294 295 function _add_url_with_attributes( $hints, $method ) { 296 // Ignore hints with missing href attributes. 297 $hints[] = array( 298 'rel' => 'foo', 299 ); 300 301 if ( 'preconnect' === $method ) { 302 // Should ignore rel attributes. 303 $hints[] = array( 304 'rel' => 'foo', 305 'href' => 'https://make.wordpress.org/great-again', 306 ); 307 } elseif ( 'preload' === $method ) { 308 // Should support typical preload argument. 309 $hints[] = array( 310 'crossorigin', 311 'as' => 'font', 312 'type' => 'font/woff2', 313 'href' => 'https://example.com/font.woff2', 314 ); 315 $hints[] = array( 316 'crossorigin' => 'use-credentials', 317 'as' => 'style', 318 'href' => 'https://example.com/foo.css', 319 ); 320 } elseif ( 'prefetch' === $method ) { 321 $hints[] = array( 322 'pr' => 0.5, 323 'href' => 'http://wordpress.org', 324 ); 325 } elseif ( 'prerender' === $method ) { 326 // Ignore invalid attributes. 327 $hints[] = array( 328 'foo' => 'bar', 329 'bar' => 'baz', 330 'href' => 'http://wordpress.org', 331 ); 332 } 333 334 return $hints; 335 } 245 336 }