-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
client-assets.php
353 lines (326 loc) · 9.59 KB
/
client-assets.php
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* Functions to register client-side assets (scripts and stylesheets) for the
* Gutenberg editor plugin.
*
* @package gutenberg
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}
/**
* Retrieves the root plugin path.
*
* @return string Root path to the gutenberg plugin.
*
* @since 0.1.0
*/
function gutenberg_dir_path() {
return plugin_dir_path( dirname( __FILE__ ) );
}
/**
* Retrieves a URL to a file in the gutenberg plugin.
*
* @param string $path Relative path of the desired file.
*
* @return string Fully qualified URL pointing to the desired file.
*
* @since 0.1.0
*/
function gutenberg_url( $path ) {
return plugins_url( $path, dirname( __FILE__ ) );
}
/**
* Registers common scripts to be used as dependencies of the editor and plugins.
*
* @since 0.1.0
*/
function gutenberg_register_scripts() {
$suffix = SCRIPT_DEBUG ? '' : '.min';
// Vendor Scripts.
$react_suffix = ( SCRIPT_DEBUG ? '.development' : '.production' ) . $suffix;
gutenberg_register_vendor_script(
'react',
'https://unpkg.com/react@next/umd/react' . $react_suffix . '.js'
);
gutenberg_register_vendor_script(
'react-dom',
'https://unpkg.com/react-dom@next/umd/react-dom' . $react_suffix . '.js',
array( 'react' )
);
gutenberg_register_vendor_script(
'react-dom-server',
'https://unpkg.com/react-dom@next/umd/react-dom-server' . $react_suffix . '.js',
array( 'react' )
);
$moment_script = SCRIPT_DEBUG ? 'moment.js' : 'min/moment.min.js';
gutenberg_register_vendor_script(
'moment',
'https://unpkg.com/[email protected]/' . $moment_script,
array( 'react' )
);
gutenberg_register_vendor_script(
'tinymce-nightly',
'https://fiddle.azurewebsites.net/tinymce/nightly/tinymce' . $suffix . '.js'
);
gutenberg_register_vendor_script(
'tinymce-nightly-lists',
'https://fiddle.azurewebsites.net/tinymce/nightly/plugins/lists/plugin' . $suffix . '.js',
array( 'tinymce-nightly' )
);
// Editor Scripts.
wp_register_script(
'wp-utils',
gutenberg_url( 'utils/build/index.js' ),
array(),
filemtime( gutenberg_dir_path() . 'utils/build/index.js' )
);
wp_register_script(
'wp-date',
gutenberg_url( 'date/build/index.js' ),
array( 'moment' ),
filemtime( gutenberg_dir_path() . 'date/build/index.js' )
);
global $wp_locale;
wp_add_inline_script( 'wp-date', 'window._wpDateSettings = ' . wp_json_encode( array(
'l10n' => array(
'locale' => get_locale(),
'months' => array_values( $wp_locale->month ),
'monthsShort' => array_values( $wp_locale->month_abbrev ),
'weekdays' => array_values( $wp_locale->weekday ),
'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ),
'meridiem' => (object) $wp_locale->meridiem,
'relative' => array(
/* translators: %s: duration */
'future' => __( '%s from now' ),
/* translators: %s: duration */
'past' => __( '%s ago' ),
),
),
'formats' => array(
'time' => get_option( 'time_format', __( 'g:i a' ) ),
'date' => get_option( 'date_format', __( 'F j, Y' ) ),
'datetime' => __( 'F j, Y g:i a' ),
),
'timezone' => array(
'offset' => get_option( 'gmt_offset', 0 ),
'string' => get_option( 'timezone_string', 'UTC' ),
),
) ), 'before' );
wp_register_script(
'wp-i18n',
gutenberg_url( 'i18n/build/index.js' ),
array(),
filemtime( gutenberg_dir_path() . 'i18n/build/index.js' )
);
wp_register_script(
'wp-element',
gutenberg_url( 'element/build/index.js' ),
array( 'react', 'react-dom', 'react-dom-server' ),
filemtime( gutenberg_dir_path() . 'element/build/index.js' )
);
wp_register_script(
'wp-components',
gutenberg_url( 'components/build/index.js' ),
array( 'wp-element' ),
filemtime( gutenberg_dir_path() . 'components/build/index.js' )
);
wp_register_script(
'wp-blocks',
gutenberg_url( 'blocks/build/index.js' ),
array( 'wp-element', 'wp-components', 'wp-utils', 'tinymce-nightly', 'tinymce-nightly-lists' ),
filemtime( gutenberg_dir_path() . 'blocks/build/index.js' )
);
// Editor Styles.
wp_register_style(
'wp-components',
gutenberg_url( 'components/build/style.css' ),
array(),
filemtime( gutenberg_dir_path() . 'components/build/style.css' )
);
wp_register_style(
'wp-blocks',
gutenberg_url( 'blocks/build/style.css' ),
array(),
filemtime( gutenberg_dir_path() . 'blocks/build/style.css' )
);
}
add_action( 'init', 'gutenberg_register_scripts' );
/**
* Retrieves a unique and reasonably short and human-friendly filename for a
* vendor script based on a URL.
*
* @param string $src Full URL of the external script.
*
* @return string Script filename suitable for local caching.
*
* @since 0.1.0
*/
function gutenberg_vendor_script_filename( $src ) {
$filename = basename( $src );
$hash = substr( md5( $src ), 0, 8 );
$match = preg_match(
'/^'
. '(?P<prefix>.*?)'
. '(?P<ignore>\.development|\.production)?'
. '(?P<suffix>\.min)?'
. '(?P<extension>\.js)'
. '(?P<extra>.*)'
. '$/',
$filename,
$filename_pieces
);
if ( ! $match ) {
return "$filename.$hash.js";
}
$match = preg_match(
'@tinymce.*/plugins/([^/]+)/plugin(\.min)?\.js$@',
$src,
$tinymce_plugin_pieces
);
if ( $match ) {
$filename_pieces['prefix'] = 'tinymce-plugin-' . $tinymce_plugin_pieces[1];
}
return $filename_pieces['prefix'] . $filename_pieces['suffix']
. '.' . $hash
. $filename_pieces['extension'];
}
/**
* Registers a vendor script from a URL, preferring a locally cached version if
* possible, or downloading it if the cached version is unavailable or
* outdated.
*
* @param string $handle Name of the script.
* @param string $src Full URL of the external script.
* @param array $deps Optional. An array of registered script handles this
* script depends on.
*
* @since 0.1.0
*/
function gutenberg_register_vendor_script( $handle, $src, $deps = array() ) {
if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
return;
}
$filename = gutenberg_vendor_script_filename( $src );
$full_path = gutenberg_dir_path() . 'vendor/' . $filename;
$needs_fetch = (
! file_exists( $full_path ) ||
time() - filemtime( $full_path ) >= DAY_IN_SECONDS
);
if ( $needs_fetch ) {
// Determine whether we can write to this file. If not, don't waste
// time doing a network request.
// @codingStandardsIgnoreStart
$f = @fopen( $full_path, 'a' );
// @codingStandardsIgnoreEnd
if ( ! $f ) {
// Failed to open the file for writing, probably due to server
// permissions. Enqueue the script directly from the URL instead.
wp_register_script( $handle, $src, $deps );
return;
}
fclose( $f );
$response = wp_remote_get( $src );
if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
// The request failed; just enqueue the script directly from the
// URL. This will probably fail too, but surfacing the error to
// the browser is probably the best we can do.
wp_register_script( $handle, $src, $deps );
// If our file was newly created above, it will have a size of
// zero, and we need to delete it so that we don't think it's
// already cached on the next request.
if ( ! filesize( $full_path ) ) {
unlink( $full_path );
}
return;
}
$f = fopen( $full_path, 'w' );
fwrite( $f, wp_remote_retrieve_body( $response ) );
fclose( $f );
}
wp_register_script(
$handle,
gutenberg_url( 'vendor/' . $filename ),
$deps
);
}
/**
* Scripts & Styles.
*
* Enqueues the needed scripts and styles when visiting the top-level page of
* the Gutenberg editor.
*
* @since 0.1.0
*
* @param string $hook Screen name.
*/
function gutenberg_scripts_and_styles( $hook ) {
if ( ! preg_match( '/(toplevel|gutenberg)_page_gutenberg(-demo)?/', $hook, $page_match ) ) {
return;
}
$is_demo = isset( $page_match[ 2 ] );
/**
* Scripts
*/
wp_enqueue_media();
// The editor code itself.
wp_enqueue_script(
'wp-editor',
gutenberg_url( 'editor/build/index.js' ),
array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils' ),
filemtime( gutenberg_dir_path() . 'editor/build/index.js' ),
true // enqueue in the footer.
);
// Load an actual post if an ID is specified.
$post_to_edit = null;
if ( isset( $_GET['post_id'] ) && (int) $_GET['post_id'] > 0 ) {
$request = new WP_REST_Request(
'GET',
sprintf( '/wp/v2/posts/%d', (int) $_GET['post_id'] )
);
$request->set_param( 'context', 'edit' );
$response = rest_do_request( $request );
if ( 200 === $response->get_status() ) {
$post_to_edit = $response->get_data();
}
}
// Initialize the post data...
if ( $post_to_edit ) {
// ...with a real post
wp_add_inline_script(
'wp-editor',
'window._wpGutenbergPost = ' . wp_json_encode( $post_to_edit ) . ';'
);
} else if ( $is_demo ) {
// ...with some test content
wp_add_inline_script(
'wp-editor',
file_get_contents( gutenberg_dir_path() . 'post-content.js' )
);
} else {
// TODO: Error handling
}
// Prepare Jed locale data.
$locale_data = gutenberg_get_jed_locale_data( 'gutenberg' );
wp_add_inline_script(
'wp-editor',
'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );',
'before'
);
// Initialize the editor.
wp_add_inline_script( 'wp-editor', 'wp.editor.createEditorInstance( \'editor\', window._wpGutenbergPost );' );
/**
* Styles
*/
wp_enqueue_style(
'wp-editor-font',
'https://fonts.googleapis.com/css?family=Noto+Serif:400,400i,700,700i'
);
wp_enqueue_style(
'wp-editor',
gutenberg_url( 'editor/build/style.css' ),
array( 'wp-components', 'wp-blocks' ),
filemtime( gutenberg_dir_path() . 'editor/build/style.css' )
);
}
add_action( 'admin_enqueue_scripts', 'gutenberg_scripts_and_styles' );