Ticket #34292: 34292.6.diff
File 34292.6.diff, 7.5 KB (added by , 9 years ago) |
---|
-
src/wp-admin/includes/admin-filters.php
diff --git src/wp-admin/includes/admin-filters.php src/wp-admin/includes/admin-filters.php index c864374..06a973b 100644
add_action( 'admin_head', 'wp_color_scheme_settings' ); 43 43 add_action( 'admin_head', 'wp_site_icon' ); 44 44 add_action( 'admin_head', '_ipad_meta' ); 45 45 46 // Prerendering. 47 add_filter( 'admin_head', 'wp_resource_hints' ); 48 46 49 add_action( 'admin_print_scripts-post.php', 'wp_page_reload_on_back_button_js' ); 47 50 add_action( 'admin_print_scripts-post-new.php', 'wp_page_reload_on_back_button_js' ); 48 51 -
src/wp-includes/default-filters.php
diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php index ddaa5ef..538f3f0 100644
add_action( 'wp_head', 'wp_print_styles', 8 ); 235 235 add_action( 'wp_head', 'wp_print_head_scripts', 9 ); 236 236 add_action( 'wp_head', 'wp_generator' ); 237 237 add_action( 'wp_head', 'rel_canonical' ); 238 add_action( 'wp_head', 'wp_resource_hints' ); 238 239 add_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); 239 240 add_action( 'wp_head', 'wp_site_icon', 99 ); 240 241 add_action( 'wp_footer', 'wp_print_footer_scripts', 20 ); -
src/wp-includes/general-template.php
diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php index db0dfaf..a434113 100644
function wp_site_icon() { 2788 2788 } 2789 2789 2790 2790 /** 2791 * Prints resource hints to browsers for pre-fetching, pre-rendering and pre-connecting to web sites. 2792 * 2793 * Gives hints to browsers to prefetch specific pages or render them in the background, 2794 * to perform DNS lookups or to begin the connection handshake (DNS, TCP, TLS) in the background. 2795 * 2796 * These performance improving indicators work by using `<link rel"…">`. 2797 * 2798 * @since 4.6.0 2799 */ 2800 function wp_resource_hints() { 2801 $hints = array( 2802 'preconnect' => array( 's.w.org' ), 2803 'prerender' => array(), 2804 'prefetch' => array(), 2805 'dns-prefetch' => wp_resource_hints_scripts_styles(), 2806 ); 2807 2808 if ( is_admin() ) { 2809 $screen = get_current_screen(); 2810 2811 if ( 'edit-post' === $screen->id ) { 2812 $hints['prefetch'][] = admin_url( 'css/edit.css' ); 2813 $hints['prefetch'][] = includes_url( 'js/tinymce/tinymce.min.js' ); 2814 } else if ( 'users' === $screen->id ) { 2815 $hints['prefetch'][] = admin_url( 'js/user-profile.js' ); 2816 } 2817 } 2818 2819 foreach ( $hints as $method => $urls ) { 2820 /** 2821 * Filters domains and URLs for resource hints. 2822 * 2823 * @since 4.6.0 2824 * 2825 * @param array $urls URLs to print for resource hints. 2826 * @param string $method The method the URLs are printed for, e.g. 'preconnect' or 'prerender'. 2827 */ 2828 $urls = array_unique( apply_filters( 'wp_resource_hints', $urls, $method ) ); 2829 2830 foreach ( $urls as $url ) { 2831 $url = esc_url( $url, array( 'http', 'https' ) ); 2832 2833 if ( in_array( $url, array( 'preconnect', 'dns-prefetch' ) ) ) { 2834 $url = parse_url( $url, PHP_URL_HOST ); 2835 } 2836 2837 printf( "<link rel='%s' href='%s'>\r\n", $method, $url ); 2838 } 2839 } 2840 } 2841 2842 /** 2843 * Adds dns-prefetch for all scripts and styles enqueued from external hosts. 2844 * 2845 * @since 4.6.0 2846 */ 2847 function wp_resource_hints_scripts_styles() { 2848 global $wp_scripts, $wp_styles; 2849 2850 $unique_hosts = array(); 2851 2852 if ( is_object( $wp_scripts ) && ! empty( $wp_scripts->registered ) ) { 2853 foreach ( $wp_scripts->registered as $registered_script ) { 2854 $this_host = parse_url( $registered_script->src, PHP_URL_HOST ); 2855 if ( ! in_array( $this_host, $unique_hosts ) && ! empty( $this_host ) && $this_host !== $_SERVER['SERVER_NAME'] ) { 2856 $unique_hosts[] = $this_host; 2857 } 2858 } 2859 } 2860 2861 if ( is_object( $wp_styles ) && ! empty( $wp_styles->registered ) ) { 2862 foreach ( $wp_styles->registered as $registered_style ) { 2863 $this_host = parse_url( $registered_style->src, PHP_URL_HOST ); 2864 if ( ! in_array( $this_host, $unique_hosts ) && ! empty( $this_host ) && $this_host !== $_SERVER['SERVER_NAME'] ) { 2865 $unique_hosts[] = $this_host; 2866 } 2867 } 2868 } 2869 2870 return $unique_hosts; 2871 } 2872 2873 /** 2791 2874 * Whether the user should have a WYSIWIG editor. 2792 2875 * 2793 2876 * Checks that the user requires a WYSIWIG editor and that the editor is -
new file tests/phpunit/tests/general/resourceHints.php
diff --git tests/phpunit/tests/general/resourceHints.php tests/phpunit/tests/general/resourceHints.php new file mode 100644 index 0000000..aea8171
- + 1 <?php 2 3 /** 4 * @group template 5 * @ticket 34292 6 */ 7 class Tests_WP_Resource_Hints extends WP_UnitTestCase { 8 function test_should_have_defaults_on_frontend() { 9 $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n"; 10 11 $this->expectOutputString( $expected ); 12 13 wp_resource_hints(); 14 } 15 16 function test_edit_post_screen() { 17 $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . 18 "<link rel='prefetch' href='" . admin_url( 'css/edit.css' ) . "'>\r\n" . 19 "<link rel='prefetch' href='" . includes_url( 'js/tinymce/tinymce.min.js' ) . "'>\r\n"; 20 21 set_current_screen( 'edit.php' ); 22 23 $actual = get_echo( 'wp_resource_hints' ); 24 25 set_current_screen( 'front' ); 26 27 $this->assertEquals( $expected, $actual ); 28 } 29 30 function test_users_screen() { 31 $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . 32 "<link rel='prefetch' href='" . admin_url( 'js/user-profile.js' ) . "'>\r\n"; 33 34 set_current_screen( 'users.php' ); 35 36 $actual = get_echo( 'wp_resource_hints' ); 37 38 set_current_screen( 'front' ); 39 40 $this->assertEquals( $expected, $actual ); 41 } 42 43 function test_dns_prefetching() { 44 $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . 45 "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" . 46 "<link rel='dns-prefetch' href='https://google.com'>\r\n" . 47 "<link rel='dns-prefetch' href='//make.wordpress.org'>\r\n"; 48 49 add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 ); 50 51 $actual = get_echo( 'wp_resource_hints' ); 52 53 remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ) ); 54 55 $this->assertEquals( $expected, $actual ); 56 } 57 58 function _add_dns_prefetch_domains( $hints, $method ) { 59 if ( 'dns-prefetch' === $method ) { 60 $hints[] = 'http://wordpress.org'; 61 $hints[] = 'https://google.com'; 62 $hints[] = '//make.wordpress.org'; 63 } 64 65 return $hints; 66 } 67 68 /** 69 * @ticket 34292 70 */ 71 function test_prerender() { 72 $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . 73 "<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" . 74 "<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" . 75 "<link rel='prerender' href='//core.trac.wordpress.org'>\r\n"; 76 77 add_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ), 10, 2 ); 78 79 $actual = get_echo( 'wp_resource_hints' ); 80 81 remove_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ) ); 82 83 $this->assertEquals( $expected, $actual ); 84 } 85 86 function _add_prerender_urls( $hints, $method ) { 87 if ( 'prerender' === $method ) { 88 $hints[] = 'https://make.wordpress.org/great-again'; 89 $hints[] = 'http://jobs.wordpress.net'; 90 $hints[] = '//core.trac.wordpress.org'; 91 } 92 93 return $hints; 94 } 95 }