-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhyperdrive.php
More file actions
334 lines (314 loc) · 10.2 KB
/
Copy pathhyperdrive.php
File metadata and controls
334 lines (314 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Putting WordPress into Hyperdrive.
*
* @package hyperdrive
* @author Josh Habdas and contributors
* @link https://wordpress.stackexchange.com/a/263733/117731
* @license GPL-3.0 or later
*
* @wordpress-plugin
* Plugin Name: Hyperdrive
* Plugin URI: https://github.com/wp-id/hyperdrive
* Author URI: https://wp-id.org/
* Description: The fastest way to load pages in WordPress.
* Version: 1.0.0-beta
* Author: WordCamp 2017 Plugin Team
* License: GPL-3.0 or later
*
* Hyperdrive. The fastest way to load pages in WordPress.
* Copyright (C) 2017 Josh Habdas and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <https://opensource.org/licenses/GPL-3.0>.
*
*/
namespace hyperdrive;
if ( !defined('ABSPATH') ) { exit(); }
// Basic configuration
define('HYPERDRIVE_VERSION', '1.0.0-beta.3');
add_action('wp_head', __NAMESPACE__ .'\engage');
/**
* Prepare structured data for Fetch Injection.
* Also dequeues enqueued scripts so WordPress doesn't load them.
*
* @uses get_dependency_data
* @uses get_enqueued_scripts
* @uses get_src_for_handle
* @since Hyperdrive 1.0.0-beta.0
* @return Associative array containing structured data. Data
* structure is assumed by functions using and used by this
* method and must be udpated if data structure changes.
*
* Example structured data ("Calibration data"):
*
* array(
* string "jquery-scrollto"
* string "/assets/js/jquery.scrollTo.js?ver=2.1.2"
* array(
* string "jquery"
* string ""
* array(
* array(
* string "jquery-core"
* string "/wp-includes/js/jquery/jquery.js?ver=1.12.4"
* array(0)
* )
* array(
* string "jquery-migrate"
* string "/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1"
* array(0)
* )
* )
* )
* )
*
*/
function calibrate_thrusters() {
$calibration_data = [];
$scripts = get_enqueued_scripts();
foreach ( $scripts as $script ) {
if ( empty($script->extra[ 'conditional' ]) ) {
// exclude scripts using conditional comments
// Internet Explorer will never support fetch
$calibration_data[] = array(
$script->handle,
get_src_for_handle( $script->handle ),
get_dependency_data( $script->deps )
);
// we'll take it from here
wp_dequeue_script( $script->handle );
}
}
return $calibration_data;
}
/**
* Prepares "Calibration data" for Fetch Injection.
* Dedupes associative array and respect sort order.
*
* @since Hyperdrive 1.0.0-beta.0
* @param array(array(...)) $calibration_data Destination coordinates
* @param boolean [$recursing=false] True when generating subparticles
* @return A list of scripts for use in Fetch Injection
*/
function generate_antimatter( $calibration_data, $recursing = false ) {
$particle_array = [];
foreach ( $calibration_data as $idx => $data ) {
$handle = $data[0];
$url = $data[1];
$particle_array[] = "{$url}";
$subparticles = $data[2];
if ( $subparticles ) {
$particle_array[] = generate_antimatter( $subparticles, true );
}
}
array_multisort( $particle_array ); // remove numeric array keys
// remove duplicate values
$particle_array = array_map(
'unserialize', array_unique(
array_map( 'serialize', $particle_array )
)
);
return $particle_array;
}
/**
* Converts antimatter particles into dark matter.
*
* @since Hyperdrive 1.0.0-beta.0
* @link https://github.com/jhabdas/fetch-inject
* @param array $antimatter_particles Partical array
* @return A string containing a fully-assembled inline script
* for Fetch Injection.
*/
function fold_spacetime( $antimatter_particles ) {
$injectors = $particle_array = [];
$fetch_inject_string = '';
// create ordered array of JSON encoded strings for Fetch Injection
function walk_recursive( $array, $accumulator, &$injectors, &$particle_array, &$injection_json = '') {
$accumulator = [];
array_walk( $array, function( $item ) use( &$accumulator, &$injectors, &$particle_array, &$injection_json ) {
if ( !empty($item) ) {
if ( is_array($item) ) {
walk_recursive( $item, $accumulator, $injectors, $particle_array, $injection_json );
} else {
if ( !in_multi_array($item, $particle_array) ) {
$accumulator[] = $particle_array[] = $item;
}
}
}
});
if ( !empty($accumulator) ) {
$injection_json = json_encode($accumulator, JSON_UNESCAPED_SLASHES);
$injectors[] = $injection_json;
}
}
walk_recursive( $antimatter_particles, FALSE, $injectors, $particle_array );
// assemble Fetch Inject string using ordered array
$first_element = reset($injectors);
$last_element = end($injectors);
foreach ($injectors as $idx => $injector) {
if ( $injector === $first_element ) {
$fetch_inject_string = "fetchInject($injector)";
} else if ( $injector === $last_element ) {
$fetch_inject_string = "fetchInject($injector, $fetch_inject_string)";
} else {
$array_with_empty_string = array(''); // like WordPress core jquery handle
if ( !(json_decode($injector) === $array_with_empty_string) ) {
$fetch_inject_string = "fetchInject($injector, $fetch_inject_string)";
}
}
}
$hyperdrive_ver = HYPERDRIVE_VERSION;
return <<<EOD
/**
* Hyperdrive v$hyperdrive_ver
*/
(function () {
if (!window.fetch) return;
/**
* Fetch Inject v1.6.11
* Copyright (c) 2017 Josh Habdas
* @licence ISC
*/
var fetchInject=function(){"use strict";const e=function(e,t,n,r,o,i,c){i=t.createElement(n),c=t.getElementsByTagName(n)[0],i.appendChild(t.createTextNode(r.text)),i.onload=o(r),c?c.parentNode.insertBefore(i,c):t.head.appendChild(i)},t=function(t,n){if(!t||!Array.isArray(t))return Promise.reject(new Error("`inputs` must be an array"));if(n&&!(n instanceof Promise))return Promise.reject(new Error("`promise` must be a promise"));const r=[],o=n?[].concat(n):[],i=[];return t.forEach(e=>o.push(window.fetch(e).then(e=>{return[e.clone().text(),e.blob()]}).then(e=>{return Promise.all(e).then(e=>{r.push({text:e[0],blob:e[1]})})}))),Promise.all(o).then(()=>{return r.forEach(t=>{i.push({then:n=>{"text/css"===t.blob.type?e(window,document,"style",t,n):e(window,document,"script",t,n)}})}),Promise.all(i)})};return t}();
$fetch_inject_string;
})();
EOD;
}
/**
* Echos an inline script into the document.
*
* @since Hyperdrive 1.0.0-beta.0
* @param string $dark_energy An inline script to asynchronously
* fetch previously enqueued page resources.
*/
function enter_hyperspace( $dark_energy ) {
echo "<script>{$dark_energy}</script>";
}
/**
* Main function engages the hyperdrive.
* Hook into `wp_head` for optimum performance.
*
* @uses calibrate_thrusters
* @uses generate_antimatter
* @uses fold_spacetime
* @uses enter_hyperspace
* @since Hyperdrive 1.0.0-beta.0
*/
function engage() {
$calibration_data = calibrate_thrusters();
$antimatter_particles = generate_antimatter( $calibration_data );
$dark_energy = fold_spacetime( $antimatter_particles );
enter_hyperspace( $dark_energy );
}
// UTILITY FUNCTIONS
// -----------------
/**
* Gets dependency data recursively.
*
* @uses get_deps_for_handle
* @uses get_src_for_handle
* @since Hyperdrive 1.0.0-beta.0
* @param array(string) $handles An array of handles
* @return array(array) Dependency data matching expected structure
*/
function get_dependency_data( $handles ) {
$dependency_data = [];
foreach ( $handles as $idx => $handle ) {
$source_url = get_src_for_handle( $handle );
if ( $source_url ) {
$dependency_data[] = array(
$handle,
$source_url,
array() // maintain consistency
);
}
$deps = get_deps_for_handle( $handle );
if ( count($deps) > 0 ) {
$dependency_data[] = array(
$handle,
'', // maintain consistency
get_dependency_data( $deps )
);
}
}
return $dependency_data;
}
/**
* Gets scripts registered and enqueued.
*
* @since Hyperdrive 1.0.0-beta.0
* @return array(_WP_Dependency) A list of enqueued dependencies
*/
function get_enqueued_scripts() {
$wp_scripts = wp_scripts();
foreach ( $wp_scripts->queue as $handle ) {
$enqueued_scripts[] = $wp_scripts->registered[ $handle ];
}
return $enqueued_scripts;
}
/**
* Gets a script dependency for a handle
*
* @since Hyperdrive 1.0.0-beta.0
* @param string $handle The handle
* @return _WP_Dependency associated with input handle
*/
function get_dep_for_handle( $handle ) {
$wp_scripts = wp_scripts();
return $wp_scripts->registered[ $handle ];
}
/**
* Gets the source URL given a script handle.
*
* @since Hyperdrive 1.0.0-beta.0
* @param string $handle The handle
* @return URL associated with handle, or empty string
*/
function get_src_for_handle( $handle ) {
$dep = get_dep_for_handle( $handle );
$suffix = ( $dep->src && $dep->ver )
? "?ver={$dep->ver}"
: '';
return "{$dep->src}{$suffix}";
}
/**
* Gets all dependencies for a given handle.
*
* @since Hyperdrive 1.0.0-beta.0
* @param string $handle The handle
* @return array(string) List of handles for dependencies of $handle
*/
function get_deps_for_handle( $handle ) {
$dep = get_dep_for_handle( $handle );
return $dep->deps;
}
/**
* Checks if a value exists in a multidimensional array.
*
* @since Hyperdrive 1.0.0-beta.3
* @param string/array $needle The value(s) to search for.
* @param array $haystack The array to search.
* @return boolean True if found, false otherwise.
*/
function in_multi_array($needle, $haystack) {
foreach ( $haystack as $item ) {
if ( is_array($item) && in_multi_array($needle, $item) ) {
return true;
} else if ( $item == $needle ) {
return true;
}
}
return false;
}