| | 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 | */ |
| | 2764 | function 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 | */ |
| | 2779 | function 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 | */ |
| | 2819 | function 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 | */ |
| | 2865 | function 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 | */ |
| | 2888 | function 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 | */ |
| | 2918 | function add_prefetch_attr( $host ) { |
| | 2919 | $host = array( 'dns-prefetch', $host ); |
| | 2920 | return $host; |
| | 2921 | } |
| | 2922 | |
| | 2923 | /** |