Make WordPress Core

Ticket #34292: 34292.5.diff

File 34292.5.diff, 7.3 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_browser_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_browser_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

     
    27902790}
    27912791
    27922792/**
     2793 * Makes sure browser hint array has unique values.
     2794 *
     2795 * @since 4.6.0
     2796 *
     2797 * @param array $urls Array of browser hints.
     2798 */
     2799function unique_browser_hints( $urls ) {
     2800        $seen = array();
     2801        foreach( $urls as $url ) {
     2802                if ( ! in_array( $url, $seen ) ) {
     2803                        array_push( $seen, $url );
     2804                }
     2805        }
     2806        return $seen;
     2807}
     2808
     2809/**
     2810 * Prints out brower hints for dns-prefetch, prerender, preconnect, etc.
     2811 *
     2812 * @since 4.6.0
     2813 */
     2814function wp_browser_hints() {
     2815
     2816        $urls = array(
     2817                array( 'preconnect', 's.w.org' ),
     2818        );
     2819
     2820        $enqueued_hosts = wp_hint_scripts_styles();
     2821        $urls           = array_merge( $urls, $enqueued_hosts );
     2822
     2823        if ( is_admin() ) {
     2824                $admin_urls = wp_admin_hints();
     2825                $urls       = array_merge( $urls, $admin_urls );
     2826        }
     2827
     2828        /**
     2829         * Filters domains and urls for browser hints.
     2830         *
     2831         * @since 4.6.0
     2832         *
     2833         * @param array $urls URL's to print for browser hints.
     2834         */
     2835        $urls = apply_filters( 'browser_hints', $urls );
     2836
     2837        $urls = unique_browser_hints( $urls );
     2838
     2839        foreach ( $urls as $url ) {
     2840                if ( $url[0] === 'dns-prefetch' || $url[0] === 'preconnect' ) {
     2841                        $url[1] = esc_url( untrailingslashit( $url[1] ), array( 'http', 'https' ) );
     2842                } else {
     2843                        $url[1] = esc_url( $url[1], array( 'http', 'https' ) );
     2844                }
     2845                printf( "<link rel='%s' href='%s'>\r\n", $url[0], $url[1] );
     2846        }
     2847}
     2848
     2849/**
     2850 * Adds browsers hints for common wp-admin scripts and styles.
     2851 *
     2852 * @since 4.6.0
     2853 */
     2854function wp_admin_hints() {
     2855
     2856        $hints = array();
     2857
     2858        $screen = get_current_screen();
     2859
     2860        if ( $screen->id === 'edit-post' ) {
     2861
     2862                $edit_hints = array(
     2863                        array( 'prefetch', admin_url( 'css/edit.css' ) ),
     2864                        array( 'prefetch', includes_url( 'js/tinymce/tinymce.min.js' ) ),
     2865                );
     2866
     2867                $hints = array_merge( $hints, $edit_hints );
     2868
     2869        } else if ( $screen->id === 'users' ) {
     2870
     2871                $users_hints = array(
     2872                        array( 'prefetch', admin_url( 'js/user-profile.js' ) ),
     2873                );
     2874
     2875                $hints = array_merge( $hints, $users_hints );
     2876        }
     2877
     2878        /**
     2879         * Filters which admin styles and scripts will be hinted.
     2880         *
     2881         * @since 4.6.0
     2882         *
     2883         * @param array $hints Admin URL's to hint.
     2884         */
     2885        $hints = apply_filters( 'admin_browser_hints', $hints );
     2886
     2887        return $hints;
     2888}
     2889
     2890/**
     2891 * Adds dns-prefetch for all scripts and styles enqueued from external hosts.
     2892 *
     2893 * @since 4.6.0
     2894 */
     2895function wp_hint_scripts_styles() {
     2896        global $wp_scripts, $wp_styles;
     2897
     2898        $unique_hosts = array();
     2899
     2900        if ( is_object( $wp_scripts ) && ! empty( $wp_scripts->registered ) ) {
     2901                foreach ( $wp_scripts->registered as $registered_script ) {
     2902                        $this_host = parse_url( $registered_script->src, PHP_URL_HOST );
     2903                        if ( ! in_array( $this_host, $unique_hosts ) && ! empty( $this_host ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
     2904                                $unique_hosts[] = $this_host;
     2905                        }
     2906                }
     2907        }
     2908
     2909        if ( is_object( $wp_styles ) && ! empty( $wp_styles->registered ) ) {
     2910                foreach ( $wp_styles->registered as $registered_style ) {
     2911                        $this_host = parse_url( $registered_style->src, PHP_URL_HOST );
     2912                        if ( ! in_array( $this_host, $unique_hosts ) && ! empty( $this_host ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
     2913                                $unique_hosts[] = $this_host;
     2914                        }
     2915                }
     2916        }
     2917
     2918        $unique_hosts = array_map( 'add_prefetch_attr', $unique_hosts );
     2919        return $unique_hosts;
     2920}
     2921
     2922/**
     2923 * Adds dns-prefetch to hosts of scripts and styles.
     2924 *
     2925 * @since 4.6.0
     2926 *
     2927 * @param array $hosts A hostname.
     2928 */
     2929function add_prefetch_attr( $host ) {
     2930        $host = array( 'dns-prefetch', $host );
     2931        return $host;
     2932}
     2933
     2934/**
    27932935 * Whether the user should have a WYSIWIG editor.
    27942936 *
    27952937 * Checks that the user requires a WYSIWIG editor and that the editor is
  • tests/phpunit/tests/general/template.php

     
    387387                $actual = get_the_modified_time( $d, $post_id );
    388388                $this->assertEquals( $expected, $actual );
    389389        }
     390
     391        /**
     392         * @ticket 34292
     393         */
     394        function test_wp_dns_prefetch() {
     395                $preexpected = "<link rel='preconnect' href='http://s.w.org'>\r\n";
     396
     397                $this->assertEquals( $preexpected, get_echo( 'wp_browser_hints' ) );
     398
     399                add_filter( 'browser_hints', array( $this, '_add_dns_prefetch_domains' ) );
     400
     401                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     402                            "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" .
     403                            "<link rel='dns-prefetch' href='https://google.com'>\r\n" .
     404                            "<link rel='dns-prefetch' href='//make.wordpress.org'>\r\n";
     405
     406                $this->assertEquals( $expected, get_echo( 'wp_browser_hints' ) );
     407        }
     408
     409        function _add_dns_prefetch_domains( $hints ) {
     410                $hints[] = array( 'dns-prefetch', 'http://wordpress.org' );
     411                $hints[] = array( 'dns-prefetch', 'https://google.com' );
     412                $hints[] = array( 'dns-prefetch', '//make.wordpress.org' );
     413
     414                return $hints;
     415        }
     416
     417        /**
     418         * @ticket 34292
     419         */
     420        function test_wp_prerender() {
     421                $preexpected = "<link rel='preconnect' href='http://s.w.org'>\r\n";
     422
     423                $this->assertEquals( $preexpected, get_echo( 'wp_browser_hints' ) );
     424
     425                add_filter( 'browser_hints', array( $this, '_add_prerender_urls' ) );
     426
     427                $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
     428                            "<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" .
     429                            "<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" .
     430                            "<link rel='prerender' href='//core.trac.wordpress.org'>\r\n";
     431
     432                $this->assertEquals( $expected, get_echo( 'wp_browser_hints' ) );
     433        }
     434
     435        function _add_prerender_urls( $hints ) {
     436                $hints[] = array( 'prerender', 'https://make.wordpress.org/great-again' );
     437                $hints[] = array( 'prerender', 'http://jobs.wordpress.net' );
     438                $hints[] = array( 'prerender', '//core.trac.wordpress.org' );
     439
     440                return $hints;
     441        }
    390442}