Make WordPress Core

Ticket #34292: 34292.9.diff

File 34292.9.diff, 8.7 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        include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
     2802
     2803        $hints = array(
     2804                'preconnect'   => array( 's.w.org' ),
     2805                'prerender'    => array(),
     2806                'prefetch'     => array(),
     2807                'dns-prefetch' => wp_resource_hints_scripts_styles(),
     2808        );
     2809
     2810        foreach ( $hints as $method => $urls ) {
     2811                /**
     2812                 * Filters domains and URLs for resource hints.
     2813                 *
     2814                 * @since 4.6.0
     2815                 *
     2816                 * @param array  $urls   URLs to print for resource hints.
     2817                 * @param string $method The method the URLs are printed for, e.g. 'preconnect' or 'prerender'.
     2818                 */
     2819                $urls = array_unique( apply_filters( 'wp_resource_hints', $urls, $method ) );
     2820
     2821                foreach ( $urls as $url ) {
     2822                        $url = esc_url( $url, array( 'http', 'https' ) );
     2823
     2824
     2825                        if ( in_array( $method, array( 'preconnect', 'dns-prefetch' ) ) ) {
     2826                                $parsed = parse_url( $url );
     2827
     2828                                if ( ! empty( $parsed['scheme'] ) ) {
     2829                                        $url = $parsed['scheme'] . '://' . $parsed['host'];
     2830                                } else {
     2831                                        $url = $parsed['host'];
     2832                                }
     2833                        }
     2834
     2835                        printf( "<link rel='%s' href='%s'>\r\n", $method, $url );
     2836                }
     2837        }
     2838}
     2839
     2840/**
     2841 * Adds dns-prefetch for all scripts and styles enqueued from external hosts.
     2842 *
     2843 * @since 4.6.0
     2844 */
     2845function wp_resource_hints_scripts_styles() {
     2846        global $wp_scripts, $wp_styles;
     2847
     2848        $unique_hosts = array();
     2849
     2850        if ( is_object( $wp_scripts ) && ! empty( $wp_scripts->registered ) ) {
     2851                foreach ( $wp_scripts->registered as $registered_script ) {
     2852                        $this_host = parse_url( $registered_script->src, PHP_URL_HOST );
     2853                        if ( ! in_array( $this_host, $unique_hosts ) && ! empty( $this_host ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
     2854                                $unique_hosts[] = $this_host;
     2855                        }
     2856                }
     2857        }
     2858
     2859        if ( is_object( $wp_styles ) && ! empty( $wp_styles->registered ) ) {
     2860                foreach ( $wp_styles->registered as $registered_style ) {
     2861                        $this_host = parse_url( $registered_style->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        return $unique_hosts;
     2869}
     2870
     2871/**
    27912872 * Whether the user should have a WYSIWIG editor.
    27922873 *
    27932874 * 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_dns_prefetching() {
     33                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     34                            "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" .
     35                            "<link rel='dns-prefetch' href='https://google.com'>\r\n" .
     36                            "<link rel='dns-prefetch' href='make.wordpress.org'>\r\n";
     37
     38                add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 );
     39
     40                $actual = get_echo( 'wp_resource_hints' );
     41
     42                remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ) );
     43
     44                $this->assertEquals( $expected, $actual );
     45        }
     46
     47        function _add_dns_prefetch_domains( $hints, $method ) {
     48                if ( 'dns-prefetch' === $method ) {
     49                        $hints[] = 'http://wordpress.org';
     50                        $hints[] = 'https://google.com';
     51                        $hints[] = '//make.wordpress.org';
     52                }
     53
     54                return $hints;
     55        }
     56
     57        function test_prerender() {
     58                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     59                            "<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" .
     60                            "<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" .
     61                            "<link rel='prerender' href='//core.trac.wordpress.org'>\r\n";
     62
     63                add_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ), 10, 2 );
     64
     65                $actual = get_echo( 'wp_resource_hints' );
     66
     67                remove_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ) );
     68
     69                $this->assertEquals( $expected, $actual );
     70        }
     71
     72        function _add_prerender_urls( $hints, $method ) {
     73                if ( 'prerender' === $method ) {
     74                        $hints[] = 'https://make.wordpress.org/great-again';
     75                        $hints[] = 'http://jobs.wordpress.net';
     76                        $hints[] = '//core.trac.wordpress.org';
     77                }
     78
     79                return $hints;
     80        }
     81
     82        function test_parse_url_dns_prefetch() {
     83                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     84                            "<link rel='dns-prefetch' href='http://make.wordpress.org'>\r\n";
     85
     86                add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ), 10, 2 );
     87
     88                $actual = get_echo( 'wp_resource_hints' );
     89
     90                remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ) );
     91
     92                $this->assertEquals( $expected, $actual );
     93        }
     94
     95        function _add_dns_prefetch_long_urls( $hints, $method ) {
     96                if ( 'dns-prefetch' === $method ) {
     97                        $hints[] = 'http://make.wordpress.org/wp-includes/css/editor.css';
     98                }
     99
     100                return $hints;
     101        }
     102
     103        function test_dns_prefetch_styles() {
     104                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     105                            "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n";
     106
     107                $args = array(
     108                        'family' => 'Open+Sans:400',
     109                        'subset' => 'latin',
     110                );
     111
     112                wp_enqueue_style( 'googlefonts', add_query_arg( $args, "//fonts.googleapis.com/css" ) );
     113
     114                $actual = get_echo( 'wp_resource_hints' );
     115
     116                $this->assertEquals( $expected, $actual );
     117
     118        }
     119
     120        function test_dns_prefetch_scripts() {
     121                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     122                            "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n";
     123
     124                $args = array(
     125                        'family' => 'Open+Sans:400',
     126                        'subset' => 'latin',
     127                );
     128
     129                wp_enqueue_script( 'googlefonts', add_query_arg( $args, "//fonts.googleapis.com/css" ) );
     130
     131                $actual = get_echo( 'wp_resource_hints' );
     132
     133                $this->assertEquals( $expected, $actual );
     134        }
     135
     136}