Make WordPress Core

Ticket #34292: 34292.7.diff

File 34292.7.diff, 9.8 KB (added by voldemortensen, 9 years ago)
  • src/wp-admin/includes/admin-filters.php

     
    4343add_action( 'admin_head', 'wp_site_icon'             );
    4444add_action( 'admin_head', '_ipad_meta'               );
    4545
     46// Prerendering.
     47add_filter( 'admin_head', 'wp_resource_hints' );
     48
    4649add_action( 'admin_print_scripts-post.php',     'wp_page_reload_on_back_button_js' );
    4750add_action( 'admin_print_scripts-post-new.php', 'wp_page_reload_on_back_button_js' );
    4851
  • src/wp-includes/default-filters.php

     
    235235add_action( 'wp_head',             'wp_print_head_scripts',            9    );
    236236add_action( 'wp_head',             'wp_generator'                           );
    237237add_action( 'wp_head',             'rel_canonical'                          );
     238add_action( 'wp_head',             'wp_resource_hints'                      );
    238239add_action( 'wp_head',             'wp_shortlink_wp_head',            10, 0 );
    239240add_action( 'wp_head',             'wp_site_icon',                    99    );
    240241add_action( 'wp_footer',           'wp_print_footer_scripts',         20    );
  • src/wp-includes/general-template.php

     
    27882788}
    27892789
    27902790/**
     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 */
     2800function 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
     2834                        if ( in_array( $method, array( 'preconnect', 'dns-prefetch' ) ) ) {
     2835                                $parsed = parse_url( $url );
     2836
     2837                                if ( ! empty( $parsed['scheme'] ) ) {
     2838                                        $url = $parsed['scheme'] . '://' . $parsed['host'];
     2839                                } else {
     2840                                        $url = $parsed['host'];
     2841                                }
     2842                        }
     2843
     2844                        printf( "<link rel='%s' href='%s'>\r\n", $method, $url );
     2845                }
     2846        }
     2847}
     2848
     2849/**
     2850 * Adds dns-prefetch for all scripts and styles enqueued from external hosts.
     2851 *
     2852 * @since 4.6.0
     2853 */
     2854function wp_resource_hints_scripts_styles() {
     2855        global $wp_scripts, $wp_styles;
     2856
     2857        $unique_hosts = array();
     2858
     2859        if ( is_object( $wp_scripts ) && ! empty( $wp_scripts->registered ) ) {
     2860                foreach ( $wp_scripts->registered as $registered_script ) {
     2861                        $this_host = parse_url( $registered_script->src, PHP_URL_HOST );
     2862                        if ( ! in_array( $this_host, $unique_hosts ) && ! empty( $this_host ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
     2863                                $unique_hosts[] = $this_host;
     2864                        }
     2865                }
     2866        }
     2867
     2868        if ( is_object( $wp_styles ) && ! empty( $wp_styles->registered ) ) {
     2869                foreach ( $wp_styles->registered as $registered_style ) {
     2870                        $this_host = parse_url( $registered_style->src, PHP_URL_HOST );
     2871                        if ( ! in_array( $this_host, $unique_hosts ) && ! empty( $this_host ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
     2872                                $unique_hosts[] = $this_host;
     2873                        }
     2874                }
     2875        }
     2876
     2877        return $unique_hosts;
     2878}
     2879
     2880/**
    27912881 * Whether the user should have a WYSIWIG editor.
    27922882 *
    27932883 * Checks that the user requires a WYSIWIG editor and that the editor is
  • tests/phpunit/tests/general/resourceHints.php

     
     1<?php
     2
     3/**
     4 * @group  template
     5 * @ticket 34292
     6 */
     7class Tests_WP_Resource_Hints extends WP_UnitTestCase {
     8        private $old_wp_scripts;
     9
     10        function setUp() {
     11                parent::setUp();
     12                $this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null;
     13                remove_action( 'wp_default_scripts', 'wp_default_scripts' );
     14                $GLOBALS['wp_scripts'] = new WP_Scripts();
     15                $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );
     16        }
     17
     18        function tearDown() {
     19                $GLOBALS['wp_scripts'] = $this->old_wp_scripts;
     20                add_action( 'wp_default_scripts', 'wp_default_scripts' );
     21                parent::tearDown();
     22        }
     23
     24        function test_should_have_defaults_on_frontend() {
     25                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n";
     26
     27                $this->expectOutputString( $expected );
     28
     29                wp_resource_hints();
     30        }
     31
     32        function test_edit_post_screen() {
     33                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     34                            "<link rel='prefetch' href='" . admin_url( 'css/edit.css' ) . "'>\r\n" .
     35                            "<link rel='prefetch' href='" . includes_url( 'js/tinymce/tinymce.min.js' ) . "'>\r\n";
     36
     37                set_current_screen( 'edit.php' );
     38
     39                $actual = get_echo( 'wp_resource_hints' );
     40
     41                set_current_screen( 'front' );
     42
     43                $this->assertEquals( $expected, $actual );
     44        }
     45
     46        function test_users_screen() {
     47                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     48                            "<link rel='prefetch' href='" . admin_url( 'js/user-profile.js' ) . "'>\r\n";
     49
     50                set_current_screen( 'users.php' );
     51
     52                $actual = get_echo( 'wp_resource_hints' );
     53
     54                set_current_screen( 'front' );
     55
     56                $this->assertEquals( $expected, $actual );
     57        }
     58
     59        function test_dns_prefetching() {
     60                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     61                            "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" .
     62                            "<link rel='dns-prefetch' href='https://google.com'>\r\n" .
     63                            "<link rel='dns-prefetch' href='make.wordpress.org'>\r\n";
     64
     65                add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 );
     66
     67                $actual = get_echo( 'wp_resource_hints' );
     68
     69                remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ) );
     70
     71                $this->assertEquals( $expected, $actual );
     72        }
     73
     74        function _add_dns_prefetch_domains( $hints, $method ) {
     75                if ( 'dns-prefetch' === $method ) {
     76                        $hints[] = 'http://wordpress.org';
     77                        $hints[] = 'https://google.com';
     78                        $hints[] = '//make.wordpress.org';
     79                }
     80
     81                return $hints;
     82        }
     83
     84        function test_prerender() {
     85                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     86                            "<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" .
     87                            "<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" .
     88                            "<link rel='prerender' href='//core.trac.wordpress.org'>\r\n";
     89
     90                add_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ), 10, 2 );
     91
     92                $actual = get_echo( 'wp_resource_hints' );
     93
     94                remove_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ) );
     95
     96                $this->assertEquals( $expected, $actual );
     97        }
     98
     99        function _add_prerender_urls( $hints, $method ) {
     100                if ( 'prerender' === $method ) {
     101                        $hints[] = 'https://make.wordpress.org/great-again';
     102                        $hints[] = 'http://jobs.wordpress.net';
     103                        $hints[] = '//core.trac.wordpress.org';
     104                }
     105
     106                return $hints;
     107        }
     108
     109        function test_parse_url_dns_prefetch() {
     110                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     111                            "<link rel='dns-prefetch' href='http://make.wordpress.org'>\r\n";
     112
     113                add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ), 10, 2 );
     114
     115                $actual = get_echo( 'wp_resource_hints' );
     116
     117                remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ) );
     118
     119                $this->assertEquals( $expected, $actual );
     120        }
     121
     122        function _add_dns_prefetch_long_urls( $hints, $method ) {
     123                if ( 'dns-prefetch' === $method ) {
     124                        $hints[] = 'http://make.wordpress.org/wp-includes/css/editor.css';
     125                }
     126
     127                return $hints;
     128        }
     129
     130        function test_dns_prefetch_styles() {
     131                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     132                            "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n";
     133
     134                $args = array(
     135                        'family' => 'Open+Sans:400',
     136                        'subset' => 'latin',
     137                );
     138
     139                wp_enqueue_style( 'googlefonts', add_query_arg( $args, "//fonts.googleapis.com/css" ) );
     140
     141                $actual = get_echo( 'wp_resource_hints' );
     142
     143                $this->assertEquals( $expected, $actual );
     144
     145        }
     146
     147        function test_dns_prefetch_scripts() {
     148                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     149                            "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n";
     150
     151                $args = array(
     152                        'family' => 'Open+Sans:400',
     153                        'subset' => 'latin',
     154                );
     155
     156                wp_enqueue_script( 'googlefonts', add_query_arg( $args, "//fonts.googleapis.com/css" ) );
     157
     158                $actual = get_echo( 'wp_resource_hints' );
     159
     160                $this->assertEquals( $expected, $actual );
     161        }
     162
     163}