| | 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 | */ |
| | 2799 | function 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 | */ |
| | 2814 | function 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 | */ |
| | 2854 | function 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 | */ |
| | 2895 | function 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 | */ |
| | 2929 | function add_prefetch_attr( $host ) { |
| | 2930 | $host = array( 'dns-prefetch', $host ); |
| | 2931 | return $host; |
| | 2932 | } |
| | 2933 | |
| | 2934 | /** |