Make WordPress Core

Ticket #34292: 34292.4.diff

File 34292.4.diff, 7.4 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( 'post_edit_form_tag', 'post_form_autocomplete_off' );
    4750
    4851add_action( 'update_option_home',          'update_home_siteurl', 10, 2 );
  • 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

     
    27552755}
    27562756
    27572757/**
     2758 * Makes sure browser hint array has unique values.
     2759 *
     2760 * @since 4.6.0
     2761 *
     2762 * @param array $urls Array of browser hints.
     2763 */
     2764function unique_browser_hints( $urls ) {
     2765        $seen = array();
     2766        foreach( $urls as $url ) {
     2767                if ( ! in_array( $url, $seen ) ) {
     2768                        array_push( $seen, $url );
     2769                }
     2770        }
     2771        return $seen;
     2772}
     2773
     2774/**
     2775 * Prints out brower hints for dns-prefetch, prerender, preconnect, etc.
     2776 *
     2777 * @since 4.6.0
     2778 */
     2779function wp_browser_hints() {
     2780
     2781        $default_urls = array(
     2782                array( 'preconnect', 's.w.org' ),
     2783        );
     2784
     2785        /**
     2786         * Filters domains and urls for browser hints.
     2787         *
     2788         * @since 4.6.0
     2789         *
     2790         * @param array $urls URL's to print for browser hints.
     2791         */
     2792        $urls           = apply_filters( 'browser_hints', $default_urls );
     2793        $prerender_urls = wp_prerender_posts();
     2794        $enqueued_hosts = wp_hint_scripts_styles();
     2795        $urls           = array_merge( $urls, $prerender_urls, $enqueued_hosts );
     2796
     2797        if ( is_admin() ) {
     2798                $admin_urls = prerender_admin_urls();
     2799                $urls       = array_merge( $urls, $admin_urls );
     2800        }
     2801
     2802        $urls = unique_browser_hints( $urls );
     2803
     2804        foreach ( $urls as $url ) {
     2805                if ( $url[0] === 'prerender' ) {
     2806                        $url[1] = esc_url( $url[1], array( 'http', 'https' ) );
     2807                } elseif ( $url[0] === 'dns-prefetch' || $url[0] === 'preconnect' ) {
     2808                        $url[1] = esc_url( untrailingslashit( $url[1] ), array( 'http', 'https' ) );
     2809                }
     2810                printf( "<link rel='%s' href='%s'>\r\n", $url[0], $url[1] );
     2811        }
     2812}
     2813
     2814/**
     2815 * Adds posts to be prerendered.
     2816 *
     2817 * @since 4.6.0
     2818 */
     2819function wp_prerender_posts() {
     2820        /**
     2821         * Filters the number of posts to prerender.
     2822         *
     2823         * @since 4.6.0
     2824         *
     2825         * @param int $prerender_count Number of posts to prerender.
     2826         */
     2827        $num_posts = apply_filters( 'prerender_count', 1 );
     2828
     2829        $posts_to_prerender = array();
     2830
     2831        if ( is_home() ) {
     2832
     2833                $count = 1;
     2834                while( have_posts() && $count <= $num_posts ) {
     2835                        the_post();
     2836                        array_push( $posts_to_prerender, array( 'prerender', get_the_permalink() ) );
     2837                        $count++;
     2838                }
     2839
     2840                rewind_posts();
     2841
     2842        } elseif ( is_single() ) {
     2843
     2844                $next_post = get_next_post();
     2845                $previous_post = get_previous_post();
     2846
     2847                if ( ! empty( $next_post ) ) {
     2848                        array_push( $posts_to_prerender, array( 'prerender', get_permalink( $next_post->ID ) ) );
     2849                }
     2850
     2851                if ( ! empty( $previous_post ) ) {
     2852                        array_push( $posts_to_prerender, array( 'prerender', get_permalink( $previous_post->ID ) ) );
     2853                }
     2854
     2855        }
     2856
     2857        return $posts_to_prerender;
     2858}
     2859
     2860/**
     2861 * Prerender pages in the admin that users are most likely to visit.
     2862 *
     2863 * @since 4.6.0
     2864 */
     2865function prerender_admin_urls() {
     2866
     2867        $default_urls = array(
     2868                array( 'prerender', admin_url( 'post-new.php' ) ),
     2869        );
     2870
     2871        /**
     2872         * Filters which admin pages will be prerendered.
     2873         *
     2874         * @since 4.6.0
     2875         *
     2876         * @param array $urls Admin URL's to prerender.
     2877         */
     2878        $urls = apply_filters( 'prerender_admin_pages', $default_urls );
     2879
     2880        return $urls;
     2881}
     2882
     2883/**
     2884 * Adds dns-prefetch for all scripts and styles enqueued from external hosts.
     2885 *
     2886 * @since 4.6.0
     2887 */
     2888function wp_hint_scripts_styles() {
     2889        global $wp_scripts, $wp_styles;
     2890
     2891        $unique_hosts = array();
     2892
     2893        foreach ( $wp_scripts->registered as $registered_script ) {
     2894                $this_host = parse_url( $registered_script->src, PHP_URL_HOST );
     2895                if ( ! in_array( $this_host, $unique_hosts ) && ! empty( $this_host ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
     2896                        $unique_hosts[] = $this_host;
     2897                }
     2898        }
     2899
     2900        foreach ( $wp_styles->registered as $registered_style ) {
     2901                $this_host = parse_url( $registered_style->src, PHP_URL_HOST );
     2902                if ( ! in_array( $this_host, $unique_hosts ) && ! empty( $this_host ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
     2903                        $unique_hosts[] = $this_host;
     2904                }
     2905        }
     2906
     2907        $unique_hosts = array_map( 'add_prefetch_attr', $unique_hosts );
     2908        return $unique_hosts;
     2909}
     2910
     2911/**
     2912 * Adds dns-prefetch to hosts of scripts and styles.
     2913 *
     2914 * @since 4.6.0
     2915 *
     2916 * @param array $hosts A hostname.
     2917 */
     2918function add_prefetch_attr( $host ) {
     2919        $host = array( 'dns-prefetch', $host );
     2920        return $host;
     2921}
     2922
     2923/**
    27582924 * Whether the user should have a WYSIWIG editor.
    27592925 *
    27602926 * Checks that the user requires a WYSIWIG editor and that the editor is
  • tests/phpunit/tests/general/template.php

     
    345345                $this->custom_logo_id  = $this->_make_attachment( $upload );
    346346                return $this->custom_logo_id;
    347347        }
     348
     349        /**
     350         * @ticket 34292
     351         */
     352        function test_wp_dns_prefetch() {
     353                $this->assertEmpty( get_echo( 'wp_dns_prefetch' ) );
     354
     355                add_filter( 'dns_prefetch_domains', array( $this, '_add_dns_prefetch_domains' ) );
     356
     357                $expected = "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" .
     358                            "<link rel='dns-prefetch' href='https://google.com'>\r\n" .
     359                            "<link rel='dns-prefetch' href='//make.wordpress.org'>\r\n";
     360
     361                $this->assertEquals( $expected, get_echo( 'wp_dns_prefetch' ) );
     362        }
     363
     364        function _add_dns_prefetch_domains( $domains ) {
     365                $domains[] = 'http://wordpress.org';
     366                $domains[] = 'https://google.com';
     367                $domains[] = '//make.wordpress.org';
     368
     369                return $domains;
     370        }
     371
     372        /**
     373         * @ticket 34292
     374         */
     375        function test_wp_prerender() {
     376                $this->assertEmpty( get_echo( 'wp_prerender' ) );
     377
     378                add_filter( 'prerender_urls', array( $this, '_add_prerender_urls' ) );
     379
     380                $expected = "<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" .
     381                            "<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" .
     382                            "<link rel='prerender' href='//core.trac.wordpress.org'>\r\n";
     383
     384                $this->assertEquals( $expected, get_echo( 'wp_prerender' ) );
     385        }
     386
     387        function _add_prerender_urls( $urls ) {
     388                $urls[] = 'https://make.wordpress.org/great-again';
     389                $urls[] = 'http://jobs.wordpress.net';
     390                $urls[] = '//core.trac.wordpress.org';
     391
     392                return $urls;
     393        }
    348394}