Ticket #38121: 38121.2.diff
| File 38121.2.diff, 5.6 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 75a4bdf..1ace5ef 100644
function wp_resource_hints() { 2812 2812 'preconnect' => array(), 2813 2813 'prefetch' => array(), 2814 2814 'prerender' => array(), 2815 'preload' => array(), 2815 2816 ); 2816 2817 2817 2818 /* … … function wp_resource_hints() { 2822 2823 $hints['dns-prefetch'][] = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2.2.1/svg/' ); 2823 2824 2824 2825 foreach ( $hints as $relation_type => $urls ) { 2826 $unique_urls = array(); 2827 2825 2828 /** 2826 2829 * Filters domains and URLs for resource hints of relation type. 2827 2830 * … … function wp_resource_hints() { 2833 2836 $urls = apply_filters( 'wp_resource_hints', $urls, $relation_type ); 2834 2837 2835 2838 foreach ( $urls as $key => $url ) { 2839 $atts = array(); 2840 2841 if ( is_array( $url ) ) { 2842 $atts = $url[ key( $url ) ]; 2843 $url = key( $url ); 2844 } 2845 2836 2846 $url = esc_url( $url, array( 'http', 'https' ) ); 2847 2837 2848 if ( ! $url ) { 2838 unset( $urls[ $key ] ); 2849 continue; 2850 } 2851 2852 if ( isset( $unique_urls[ $url ] ) ) { 2839 2853 continue; 2840 2854 } 2841 2855 2842 2856 if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ) ) ) { 2843 2857 $parsed = wp_parse_url( $url ); 2858 2844 2859 if ( empty( $parsed['host'] ) ) { 2845 unset( $urls[ $key ] );2846 2860 continue; 2847 2861 } 2848 2862 … … function wp_resource_hints() { 2854 2868 } 2855 2869 } 2856 2870 2857 $urls[ $key ] = $url; 2871 $atts['rel'] = $relation_type; 2872 $atts['href'] = $url; 2873 2874 $unique_urls[ $url ] = $atts; 2858 2875 } 2859 2876 2860 $urls = array_unique( $urls ); 2877 foreach ( $unique_urls as $atts ) { 2878 $html = ''; 2879 2880 foreach ( $atts as $attr => $value ) { 2881 if ( is_array( $value ) || ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) ) { 2882 continue; 2883 } 2884 2885 $attr = esc_attr( $attr ); 2886 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); 2887 2888 if ( ! is_string( $attr ) ) { 2889 $html .= " $value"; 2890 } else { 2891 $html .= " $attr='$value'"; 2892 } 2893 } 2894 2895 $html = trim( $html ); 2861 2896 2862 foreach ( $urls as $url ) { 2863 printf( "<link rel='%s' href='%s' />\n", $relation_type, $url ); 2897 echo "<link $html />\n"; 2864 2898 } 2865 2899 } 2866 2900 } -
tests/phpunit/tests/general/resourceHints.php
diff --git tests/phpunit/tests/general/resourceHints.php tests/phpunit/tests/general/resourceHints.php index a25d8bb..84ece5c 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 function test_custom_attributes() { 276 $expected = "<link rel='dns-prefetch' href='//s.w.org' />\n" . 277 "<link rel='preconnect' href='https://make.wordpress.org' />\n" . 278 "<link pr='0.5' rel='prefetch' href='http://wordpress.org' />\n" . 279 "<link rel='prerender' href='http://wordpress.org' />\n" . 280 "<link as='font' type='font/woff2' rel='preload' href='https://example.com/font.woff2' />\n"; 281 282 add_filter( 'wp_resource_hints', array( $this, '_add_url_with_attributes' ), 10, 2 ); 283 284 $actual = get_echo( 'wp_resource_hints' ); 285 286 remove_filter( 'wp_resource_hints', array( $this, '_add_url_with_attributes' ) ); 287 288 $this->assertEquals( $expected, $actual ); 289 } 290 291 function _add_url_with_attributes( $hints, $method ) { 292 293 if ( 'preconnect' === $method ) { 294 // Should ignore rel and href attributes. 295 $hints[] = array( 296 'https://make.wordpress.org/great-again' => array( 297 'rel' => 'foo', 298 'href' => 'bar', 299 ), 300 ); 301 } elseif ( 'preload' === $method ) { 302 // Should support typical preload argument 303 $hints[] = array( 304 'https://example.com/font.woff2' => array( 305 'crossorigin', 306 'as' => 'font', 307 'type' => 'font/woff2', 308 ), 309 ); 310 } elseif ( 'preload' === $method ) { 311 // Should support typical preload argument 312 $hints[] = array( 313 'https://example.com/font.woff2' => array( 314 'crossorigin', 315 'as' => 'font', 316 'type' => 'font/woff2', 317 ), 318 ); 319 $hints[] = array( 320 'https://example.com/foo.css' => array( 321 'crossorigin' => 'use-credentials', 322 'as' => 'style', 323 ), 324 ); 325 } elseif ( 'prefetch' === $method ) { 326 $hints[] = array( 327 'http://wordpress.org' => array( 328 'pr' => 0.5, 329 ), 330 ); 331 } elseif ( 'prerender' === $method ) { 332 $hints[] = array( 333 'http://wordpress.org' => array( 334 'foo' => 'bar', 335 'bar' => 'baz', 336 ), 337 ); 338 } 339 340 return $hints; 341 } 245 342 }