|
2 | 2 |
|
3 | 3 | namespace BetterResourceHints;
|
4 | 4 |
|
5 |
| -class Prefetcher { |
6 |
| - |
7 |
| - public function __construct() { |
8 |
| - add_action('wp_head', array($this, 'prefetch_javascript'), 1); |
9 |
| - add_action('wp_head', array($this, 'prefetch_styles'), 1); |
10 |
| - } |
11 |
| - |
12 |
| - public function prefetch_javascript() { |
13 |
| - $this->generate_prefetch_markup('scripts'); |
14 |
| - } |
15 |
| - |
16 |
| - public function prefetch_styles() { |
17 |
| - $this->generate_prefetch_markup('styles'); |
18 |
| - } |
19 |
| - |
20 |
| - /** |
21 |
| - * Only allow users to prefetch by specific handle, |
22 |
| - * because we don't want to prefetch all admin scripts |
23 |
| - * if that option is selected. |
24 |
| - * |
25 |
| - * @param string $type |
26 |
| - * @return void |
27 |
| - */ |
28 |
| - private function generate_prefetch_markup($type = 'scripts') { |
29 |
| - |
30 |
| - global ${'wp_' . $type}; |
31 |
| - $singularType = substr_replace($type, "", -1); |
32 |
| - $handlesToPrefetch = []; |
33 |
| - |
34 |
| - $noSpaces = Utilities::strip_spaces(Utilities::get_option('prefetch_'. $type. '_handles')); |
35 |
| - $specificHandlesToPrefetch = explode(',', $noSpaces ?: ''); |
36 |
| - |
37 |
| - //-- @todo In the future, check if user would like to preload $specificHandlesToPrefetch's dependencies as well. |
38 |
| - // foreach($specificHandlesToPrefetch as $handle) { |
39 |
| - // $specificHandlesToPrefetch = array_merge($specificHandlesToPrefetch, Utilities::collect_all_deps($handle)); |
40 |
| - // } |
41 |
| - |
42 |
| - //-- Loop through and print preload tags. |
43 |
| - foreach(array_unique($specificHandlesToPrefetch) as $handle) { |
44 |
| - $resource = isset(${'wp_' . $type}->registered[$handle]) |
45 |
| - ? ${'wp_' . $type}->registered[$handle] |
46 |
| - : false; |
47 |
| - |
48 |
| - if($resource === false) continue; |
49 |
| - if(empty($resource->src)) continue; |
50 |
| - |
51 |
| - $methodToCheckIfAssetIsEnqueued = 'wp_' . $singularType . '_is'; |
52 |
| - if($methodToCheckIfAssetIsEnqueued($handle)) continue; |
53 |
| - |
54 |
| - $source = $resource->src . ($resource->ver ? "?ver={$resource->ver}" : ""); |
55 |
| - |
56 |
| - if(Utilities::get_option("prefetch_assets_enable_server_push")) { |
57 |
| - Utilities::construct_server_push_headers('prefetch', $source); |
58 |
| - } |
59 |
| - |
60 |
| - echo apply_filters('better_resource_hints_prefetch_tag', "<link rel='prefetch' href='{$source}'/>\n", $handle, $type); |
61 |
| - } |
62 |
| - } |
| 5 | +class Prefetcher |
| 6 | +{ |
| 7 | + public function __construct() |
| 8 | + { |
| 9 | + add_action('wp_head', array($this, 'prefetch_javascript'), 1); |
| 10 | + add_action('wp_head', array($this, 'prefetch_styles'), 1); |
| 11 | + } |
| 12 | + |
| 13 | + public function prefetch_javascript() |
| 14 | + { |
| 15 | + $this->generate_prefetch_markup('scripts'); |
| 16 | + } |
| 17 | + |
| 18 | + public function prefetch_styles() |
| 19 | + { |
| 20 | + $this->generate_prefetch_markup('styles'); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Only allow users to prefetch by specific handle, |
| 25 | + * because we don't want to prefetch all admin scripts |
| 26 | + * if that option is selected. |
| 27 | + * |
| 28 | + * @param string $type |
| 29 | + * @return void |
| 30 | + */ |
| 31 | + private function generate_prefetch_markup($type = 'scripts') |
| 32 | + { |
| 33 | + global ${'wp_' . $type}; |
| 34 | + $singularType = substr_replace($type, "", -1); |
| 35 | + $handlesToPrefetch = []; |
| 36 | + |
| 37 | + $noSpaces = Utilities::strip_spaces(Utilities::get_option('prefetch_'. $type. '_handles')); |
| 38 | + $specificHandlesToPrefetch = explode(',', $noSpaces ?: ''); |
| 39 | + |
| 40 | + //-- @todo In the future, check if user would like to preload $specificHandlesToPrefetch's dependencies as well. |
| 41 | + // foreach($specificHandlesToPrefetch as $handle) { |
| 42 | + // $specificHandlesToPrefetch = array_merge($specificHandlesToPrefetch, Utilities::collect_all_deps($handle)); |
| 43 | + // } |
| 44 | + |
| 45 | + //-- Loop through and print preload tags. |
| 46 | + foreach (array_unique($specificHandlesToPrefetch) as $handle) { |
| 47 | + $resource = isset(${'wp_' . $type}->registered[$handle]) |
| 48 | + ? ${'wp_' . $type}->registered[$handle] |
| 49 | + : false; |
| 50 | + |
| 51 | + if ($resource === false) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + if (empty($resource->src)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + $methodToCheckIfAssetIsEnqueued = 'wp_' . $singularType . '_is'; |
| 59 | + if ($methodToCheckIfAssetIsEnqueued($handle)) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + $source = $resource->src . ($resource->ver ? "?ver={$resource->ver}" : ""); |
| 64 | + |
| 65 | + if (Utilities::get_option("prefetch_assets_enable_server_push")) { |
| 66 | + Utilities::construct_server_push_headers('prefetch', $source); |
| 67 | + } |
| 68 | + |
| 69 | + echo apply_filters('better_resource_hints_prefetch_tag', "<link rel='prefetch' href='{$source}'/>\n", $handle, $type); |
| 70 | + } |
| 71 | + } |
63 | 72 | }
|
0 commit comments