PluginProbe
Gutenberg / 7.7.2
Gutenberg v7.7.2
24.1.0 24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 All 404 releases
← All changes | lib/client-assets.php +204 -220 12.6.0 → 7.7.2 View file →
@@ -17,9 +17,9 @@
17 17 *
18 18 * @since 0.1.0
19 19 */
20 20 function gutenberg_dir_path() {
21 - return plugin_dir_path( __DIR__ );
21 + return plugin_dir_path( dirname( __FILE__ ) );
22 22 }
23 23
24 24 /**
25 25 * Retrieves a URL to a file in the gutenberg plugin.
@@ -30,9 +30,9 @@
30 30 *
31 31 * @since 0.1.0
32 32 */
33 33 function gutenberg_url( $path ) {
34 - return plugins_url( $path, __DIR__ );
34 + return plugins_url( $path, dirname( __FILE__ ) );
35 35 }
36 36
37 37 /**
38 38 * Registers a script according to `wp_register_script`. Honors this request by
@@ -41,9 +41,9 @@
41 41 * avoid losing inline scripts which may have been attached.
42 42 *
43 43 * @since 4.1.0
44 44 *
45 - * @param WP_Scripts $scripts WP_Scripts instance.
45 + * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
46 46 * @param string $handle Name of the script. Should be unique.
47 47 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
48 48 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
49 49 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
@@ -52,9 +52,9 @@
52 52 * If set to null, no version is added.
53 53 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
54 54 * Default 'false'.
55 55 */
56 -function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
56 +function gutenberg_override_script( &$scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
57 57 $script = $scripts->query( $handle, 'registered' );
58 58 if ( $script ) {
59 59 /*
60 60 * In many ways, this is a reimplementation of `wp_register_script` but
@@ -87,29 +87,15 @@
87 87 /*
88 88 * `WP_Dependencies::set_translations` will fall over on itself if setting
89 89 * translations on the `wp-i18n` handle, since it internally adds `wp-i18n`
90 90 * as a dependency of itself, exhausting memory. The same applies for the
91 - * polyfill and hooks scripts, which are dependencies _of_ `wp-i18n`.
91 + * polyfill script, which is a dependency _of_ `wp-i18n`.
92 92 *
93 93 * See: https://core.trac.wordpress.org/ticket/46089
94 94 */
95 - if ( ! in_array( $handle, array( 'wp-i18n', 'wp-polyfill', 'wp-hooks' ), true ) ) {
95 + if ( 'wp-i18n' !== $handle && 'wp-polyfill' !== $handle ) {
96 96 $scripts->set_translations( $handle, 'default' );
97 97 }
98 -
99 - /*
100 - * Wp-editor module is exposed as window.wp.editor.
101 - * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor.
102 - * Solution: fuse the two objects together to maintain backward compatibility.
103 - * For more context, see https://github.com/WordPress/gutenberg/issues/33203
104 - */
105 - if ( 'wp-editor' === $handle ) {
106 - $scripts->add_inline_script(
107 - 'wp-editor',
108 - 'Object.assign( window.wp.editor, window.wp.oldEditor );',
109 - 'after'
110 - );
111 - }
112 98 }
113 99
114 100 /**
115 101 * Filters the default translation file load behavior to load the Gutenberg
@@ -133,9 +119,9 @@
133 119 return $file;
134 120 }
135 121
136 122 // Ignore scripts that are not found in the expected `build/` location.
137 - $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.min.js';
123 + $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.js';
138 124 if ( ! file_exists( $script_path ) ) {
139 125 return $file;
140 126 }
141 127
@@ -163,14 +149,33 @@
163 149 }
164 150 add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 );
165 151
166 152 /**
153 + * Filters the default labels for common post types to change the case style
154 + * from capitalized (e.g. "Featured Image") to sentence-style (e.g. "Featured
155 + * image").
156 + *
157 + * See: https://github.com/WordPress/gutenberg/pull/18758
158 + *
159 + * @param object $labels Object with all the labels as member variables.
160 + *
161 + * @return object Object with all the labels, including overridden ones.
162 + */
163 +function gutenberg_override_posttype_labels( $labels ) {
164 + $labels->featured_image = __( 'Featured image', 'gutenberg' );
165 + return $labels;
166 +}
167 +foreach ( array( 'post', 'page' ) as $post_type ) {
168 + add_filter( "post_type_labels_{$post_type}", 'gutenberg_override_posttype_labels' );
169 +}
170 +
171 +/**
167 172 * Registers a style according to `wp_register_style`. Honors this request by
168 173 * deregistering any style by the same handler before registration.
169 174 *
170 175 * @since 4.1.0
171 176 *
172 - * @param WP_Styles $styles WP_Styles instance.
177 + * @param WP_Styles $styles WP_Styles instance (passed by reference).
173 178 * @param string $handle Name of the stylesheet. Should be unique.
174 179 * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
175 180 * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
176 181 * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
@@ -180,9 +185,9 @@
180 185 * @param string $media Optional. The media for which this stylesheet has been defined.
181 186 * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
182 187 * '(orientation: portrait)' and '(max-width: 640px)'.
183 188 */
184 -function gutenberg_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
189 +function gutenberg_override_style( &$styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
185 190 $style = $styles->query( $handle, 'registered' );
186 191 if ( $style ) {
187 192 $styles->remove( $handle );
188 193 }
@@ -197,27 +202,21 @@
197 202 * should not call any WordPress PHP functions.
198 203 *
199 204 * @since 0.1.0
200 205 *
201 - * @param WP_Scripts $scripts WP_Scripts instance.
206 + * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
202 207 */
203 -function gutenberg_register_vendor_scripts( $scripts ) {
204 - $suffix = SCRIPT_DEBUG ? '' : '.min';
205 -
206 - $react_suffix = ( SCRIPT_DEBUG ? '.development' : '.production' ) . $suffix;
207 - gutenberg_register_vendor_script(
208 - $scripts,
209 - 'react',
210 - 'https://unpkg.com/[email protected]/umd/react' . $react_suffix . '.js',
211 - // See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
212 - SCRIPT_DEBUG ? array( 'wp-react-refresh-entry', 'wp-polyfill' ) : array( 'wp-polyfill' )
213 - );
214 - gutenberg_register_vendor_script(
215 - $scripts,
216 - 'react-dom',
217 - 'https://unpkg.com/[email protected]/umd/react-dom' . $react_suffix . '.js',
218 - array( 'react' )
219 - );
208 +function gutenberg_register_vendor_scripts( &$scripts ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
209 + // This function is intentionally left empty.
210 + //
211 + // Scripts such as react and react-dom are expected to be overridden soon,
212 + // and it is preferred to keep this function in place so as not to disturb
213 + // tooling related to the plugin build process.
214 + //
215 + // TODO: Remove phpcs exception in function signature once this function
216 + // regains its use.
217 + //
218 + // See https://github.com/WordPress/gutenberg/pull/20628.
220 219 }
221 220 add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
222 221
223 222 /**
@@ -225,27 +224,23 @@
225 224 * `build/` location.
226 225 *
227 226 * @since 4.5.0
228 227 *
229 - * @param WP_Scripts $scripts WP_Scripts instance.
228 + * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
230 229 */
231 -function gutenberg_register_packages_scripts( $scripts ) {
232 - // When in production, use the plugin's version as the default asset version;
233 - // else (for development or test) default to use the current time.
234 - $default_version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();
235 -
236 - foreach ( glob( gutenberg_dir_path() . 'build/*/index.min.js' ) as $path ) {
230 +function gutenberg_register_packages_scripts( &$scripts ) {
231 + foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) {
237 232 // Prefix `wp-` to package directory to get script handle.
238 - // For example, `…/build/a11y/index.min.js` becomes `wp-a11y`.
233 + // For example, `…/build/a11y/index.js` becomes `wp-a11y`.
239 234 $handle = 'wp-' . basename( dirname( $path ) );
240 235
241 - // Replace extension with `.asset.php` to find the generated dependencies file.
242 - $asset_file = substr( $path, 0, -( strlen( '.js' ) ) ) . '.asset.php';
236 + // Replace `.js` extension with `.asset.php` to find the generated dependencies file.
237 + $asset_file = substr( $path, 0, -3 ) . '.asset.php';
243 238 $asset = file_exists( $asset_file )
244 239 ? require( $asset_file )
245 240 : null;
246 241 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
247 - $version = isset( $asset['version'] ) ? $asset['version'] : $default_version;
242 + $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
248 243
249 244 // Add dependencies that cannot be detected and generated by build tools.
250 245 switch ( $handle ) {
251 246 case 'wp-block-library':
@@ -281,22 +276,18 @@
281 276 * `build/` location.
282 277 *
283 278 * @since 6.7.0
284 279
285 - * @param WP_Styles $styles WP_Styles instance.
280 + * @param WP_Styles $styles WP_Styles instance (passed by reference).
286 281 */
287 -function gutenberg_register_packages_styles( $styles ) {
288 - // When in production, use the plugin's version as the asset version;
289 - // else (for development or test) default to use the current time.
290 - $version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();
291 -
282 +function gutenberg_register_packages_styles( &$styles ) {
292 283 // Editor Styles.
293 284 gutenberg_override_style(
294 285 $styles,
295 286 'wp-block-editor',
296 287 gutenberg_url( 'build/block-editor/style.css' ),
297 - array( 'wp-components' ),
298 - $version
288 + array( 'wp-components', 'wp-editor-font' ),
289 + filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
299 290 );
300 291 $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
301 292
302 293 gutenberg_override_style(
@@ -302,10 +293,10 @@
302 293 gutenberg_override_style(
303 294 $styles,
304 295 'wp-editor',
305 296 gutenberg_url( 'build/editor/style.css' ),
306 - array( 'wp-components', 'wp-block-editor', 'wp-nux', 'wp-reusable-blocks' ),
307 - $version
297 + array( 'wp-components', 'wp-block-editor', 'wp-nux' ),
298 + filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
308 299 );
309 300 $styles->add_data( 'wp-editor', 'rtl', 'replace' );
310 301
311 302 gutenberg_override_style(
@@ -312,9 +303,9 @@
312 303 $styles,
313 304 'wp-edit-post',
314 305 gutenberg_url( 'build/edit-post/style.css' ),
315 306 array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ),
316 - $version
307 + filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' )
317 308 );
318 309 $styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
319 310
320 311 gutenberg_override_style(
@@ -320,23 +311,21 @@
320 311 gutenberg_override_style(
321 312 $styles,
322 313 'wp-components',
323 314 gutenberg_url( 'build/components/style.css' ),
324 - array( 'dashicons' ),
325 - $version
315 + array(),
316 + filemtime( gutenberg_dir_path() . 'build/components/style.css' )
326 317 );
327 318 $styles->add_data( 'wp-components', 'rtl', 'replace' );
328 319
329 - $block_library_filename = wp_should_load_separate_core_block_assets() ? 'common' : 'style';
330 320 gutenberg_override_style(
331 321 $styles,
332 322 'wp-block-library',
333 - gutenberg_url( 'build/block-library/' . $block_library_filename . '.css' ),
323 + gutenberg_url( 'build/block-library/style.css' ),
334 324 array(),
335 - $version
325 + filemtime( gutenberg_dir_path() . 'build/block-library/style.css' )
336 326 );
337 327 $styles->add_data( 'wp-block-library', 'rtl', 'replace' );
338 - $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' );
339 328
340 329 gutenberg_override_style(
341 330 $styles,
342 331 'wp-format-library',
@@ -341,57 +330,24 @@
341 330 $styles,
342 331 'wp-format-library',
343 332 gutenberg_url( 'build/format-library/style.css' ),
344 333 array( 'wp-block-editor', 'wp-components' ),
345 - $version
334 + filemtime( gutenberg_dir_path() . 'build/format-library/style.css' )
346 335 );
347 336 $styles->add_data( 'wp-format-library', 'rtl', 'replace' );
348 337
349 - $wp_edit_blocks_dependencies = array(
350 - 'wp-components',
351 - 'wp-editor',
352 - // This need to be added before the block library styles,
353 - // The block library styles override the "reset" styles.
354 - 'wp-reset-editor-styles',
355 - 'wp-block-library',
356 - 'wp-reusable-blocks',
357 - );
358 -
359 - // Only load the default layout and margin styles for themes without theme.json file.
360 - if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
361 - $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
362 - }
363 -
364 - global $editor_styles;
365 - if ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) {
366 - // Include opinionated block styles if no $editor_styles are declared, so the editor never appears broken.
367 - $wp_edit_blocks_dependencies[] = 'wp-block-library-theme';
368 - }
369 -
370 338 gutenberg_override_style(
371 339 $styles,
372 - 'wp-reset-editor-styles',
373 - gutenberg_url( 'build/block-library/reset.css' ),
374 - array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Admin styles.
375 - $version
376 - );
377 - $styles->add_data( 'wp-reset-editor-styles', 'rtl', 'replace' );
378 -
379 - gutenberg_override_style(
380 - $styles,
381 - 'wp-editor-classic-layout-styles',
382 - gutenberg_url( 'build/edit-post/classic.css' ),
383 - array(),
384 - $version
385 - );
386 - $styles->add_data( 'wp-editor-classic-layout-styles', 'rtl', 'replace' );
387 -
388 - gutenberg_override_style(
389 - $styles,
390 340 'wp-edit-blocks',
391 341 gutenberg_url( 'build/block-library/editor.css' ),
392 - $wp_edit_blocks_dependencies,
393 - $version
342 + array(
343 + 'wp-components',
344 + 'wp-editor',
345 + 'wp-block-library',
346 + // Always include visual styles so the editor never appears broken.
347 + 'wp-block-library-theme',
348 + ),
349 + filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' )
394 350 );
395 351 $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
396 352
397 353 gutenberg_override_style(
@@ -398,9 +354,9 @@
398 354 $styles,
399 355 'wp-nux',
400 356 gutenberg_url( 'build/nux/style.css' ),
401 357 array( 'wp-components' ),
402 - $version
358 + filemtime( gutenberg_dir_path() . 'build/nux/style.css' )
403 359 );
404 360 $styles->add_data( 'wp-nux', 'rtl', 'replace' );
405 361
406 362 gutenberg_override_style(
@@ -407,9 +363,9 @@
407 363 $styles,
408 364 'wp-block-library-theme',
409 365 gutenberg_url( 'build/block-library/theme.css' ),
410 366 array(),
411 - $version
367 + filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' )
412 368 );
413 369 $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
414 370
415 371 gutenberg_override_style(
@@ -416,27 +372,18 @@
416 372 $styles,
417 373 'wp-list-reusable-blocks',
418 374 gutenberg_url( 'build/list-reusable-blocks/style.css' ),
419 375 array( 'wp-components' ),
420 - $version
376 + filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' )
421 377 );
422 378 $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
423 379
424 380 gutenberg_override_style(
425 381 $styles,
426 - 'wp-edit-navigation',
427 - gutenberg_url( 'build/edit-navigation/style.css' ),
428 - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
429 - $version
430 - );
431 - $styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' );
432 -
433 - gutenberg_override_style(
434 - $styles,
435 382 'wp-edit-site',
436 383 gutenberg_url( 'build/edit-site/style.css' ),
437 384 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
438 - $version
385 + filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' )
439 386 );
440 387 $styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
441 388
442 389 gutenberg_override_style(
@@ -442,10 +389,10 @@
442 389 gutenberg_override_style(
443 390 $styles,
444 391 'wp-edit-widgets',
445 392 gutenberg_url( 'build/edit-widgets/style.css' ),
446 - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks', 'wp-widgets' ),
447 - $version
393 + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
394 + filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' )
448 395 );
449 396 $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
450 397
451 398 gutenberg_override_style(
@@ -452,39 +399,44 @@
452 399 $styles,
453 400 'wp-block-directory',
454 401 gutenberg_url( 'build/block-directory/style.css' ),
455 402 array( 'wp-block-editor', 'wp-components' ),
456 - $version
403 + filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' )
457 404 );
458 405 $styles->add_data( 'wp-block-directory', 'rtl', 'replace' );
406 +}
407 +add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
459 408
460 - gutenberg_override_style(
461 - $styles,
462 - 'wp-customize-widgets',
463 - gutenberg_url( 'build/customize-widgets/style.css' ),
464 - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-widgets' ),
465 - $version
409 +/**
410 + * Registers common scripts and styles to be used as dependencies of the editor
411 + * and plugins.
412 + *
413 + * @since 0.1.0
414 + */
415 +function gutenberg_enqueue_block_editor_assets() {
416 + wp_add_inline_script(
417 + 'wp-api-fetch',
418 + sprintf(
419 + 'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );' .
420 + 'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );' .
421 + 'wp.apiFetch.nonceEndpoint = "%s";' .
422 + 'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
423 + ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
424 + admin_url( 'admin-ajax.php?action=gutenberg_rest_nonce' )
425 + ),
426 + 'after'
466 427 );
467 - $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' );
468 428
469 - gutenberg_override_style(
470 - $styles,
471 - 'wp-reusable-blocks',
472 - gutenberg_url( 'build/reusable-blocks/style.css' ),
473 - array( 'wp-components' ),
474 - $version
475 - );
476 - $styles->add_data( 'wp-reusable-block', 'rtl', 'replace' );
429 + if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
430 + $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;
477 431
478 - gutenberg_override_style(
479 - $styles,
480 - 'wp-widgets',
481 - gutenberg_url( 'build/widgets/style.css' ),
482 - array( 'wp-components' )
483 - );
484 - $styles->add_data( 'wp-widgets', 'rtl', 'replace' );
432 + wp_enqueue_script(
433 + 'gutenberg-live-reload',
434 + $live_reload_url
435 + );
436 + }
485 437 }
486 -add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
438 +add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets' );
487 439
488 440 /**
489 441 * Retrieves a unique and reasonably short and human-friendly filename for a
490 442 * vendor script based on a URL and the script handle.
@@ -520,9 +472,9 @@
520 472 * Registers a vendor script from a URL, preferring a locally cached version if
521 473 * possible, or downloading it if the cached version is unavailable or
522 474 * outdated.
523 475 *
524 - * @param WP_Scripts $scripts WP_Scripts instance.
476 + * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
525 477 * @param string $handle Name of the script.
526 478 * @param string $src Full URL of the external script.
527 479 * @param array $deps Optional. An array of registered script handles this
528 480 * script depends on.
@@ -534,9 +486,9 @@
534 486 * Default 'false'.
535 487 *
536 488 * @since 0.1.0
537 489 */
538 -function gutenberg_register_vendor_script( $scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
490 +function gutenberg_register_vendor_script( &$scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
539 491 if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
540 492 return;
541 493 }
542 494
@@ -559,27 +511,17 @@
559 511 if ( $needs_fetch ) {
560 512 // Determine whether we can write to this file. If not, don't waste
561 513 // time doing a network request.
562 514 // @codingStandardsIgnoreStart
563 -
564 - $is_writable = is_writable( $full_path );
565 - if ( $is_writable ) {
566 - $f = @fopen( $full_path, 'a' );
567 - if ( ! $f ) {
568 - $is_writable = false;
569 - } else {
570 - fclose( $f );
571 - }
572 - }
573 -
515 + $f = @fopen( $full_path, 'a' );
574 516 // @codingStandardsIgnoreEnd
575 - if ( ! $is_writable ) {
517 + if ( ! $f ) {
576 518 // Failed to open the file for writing, probably due to server
577 519 // permissions. Enqueue the script directly from the URL instead.
578 520 gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
579 521 return;
580 522 }
581 -
523 + fclose( $f );
582 524 $response = wp_remote_get( $src );
583 525 if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
584 526 $f = fopen( $full_path, 'w' );
585 527 fwrite( $f, wp_remote_retrieve_body( $response ) );
@@ -603,75 +545,117 @@
603 545 );
604 546 }
605 547
606 548 /**
607 - * Sets the editor styles to be consumed by JS.
549 + * Extends block editor settings to include Gutenberg's `editor-styles.css` as
550 + * taking precedent those styles shipped with core.
551 + *
552 + * @param array $settings Default editor settings.
553 + *
554 + * @return array Filtered editor settings.
608 555 */
609 -function gutenberg_resolve_assets() {
610 - global $pagenow;
556 +function gutenberg_extend_block_editor_styles( $settings ) {
557 + $editor_styles_file = gutenberg_dir_path() . 'build/editor/editor-styles.css';
611 558
612 - $script_handles = array();
613 - $style_handles = array(
614 - 'wp-block-editor',
615 - 'wp-block-library',
616 - 'wp-block-library-theme',
617 - 'wp-edit-blocks',
618 - );
619 -
620 - if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) {
621 - $style_handles[] = 'wp-widgets';
622 - $style_handles[] = 'wp-edit-widgets';
559 + /*
560 + * If, for whatever reason, the built editor styles do not exist, avoid
561 + * override and fall back to the default.
562 + */
563 + if ( ! file_exists( $editor_styles_file ) ) {
564 + return $settings;
623 565 }
624 566
625 - $block_registry = WP_Block_Type_Registry::get_instance();
567 + if ( empty( $settings['styles'] ) ) {
568 + $settings['styles'] = array();
569 + } else {
570 + /*
571 + * The styles setting is an array of CSS strings, so there is no direct
572 + * way to find the default styles. To maximize stability, load (again)
573 + * the default styles from disk and find its place in the array.
574 + *
575 + * See: https://github.com/WordPress/wordpress-develop/blob/5.0.3/src/wp-admin/edit-form-blocks.php#L168-L175
576 + */
626 577
627 - foreach ( $block_registry->get_all_registered() as $block_type ) {
628 - if ( ! empty( $block_type->style ) ) {
629 - $style_handles[] = $block_type->style;
630 - }
578 + $default_styles = file_get_contents(
579 + ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
580 + );
631 581
632 - if ( ! empty( $block_type->editor_style ) ) {
633 - $style_handles[] = $block_type->editor_style;
582 + /*
583 + * Iterate backwards from the end of the array since the preferred
584 + * insertion point in case not found is prepended as first entry.
585 + */
586 + for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) {
587 + if ( isset( $settings['styles'][ $i ]['css'] ) &&
588 + $default_styles === $settings['styles'][ $i ]['css'] ) {
589 + break;
590 + }
634 591 }
635 -
636 - if ( ! empty( $block_type->script ) ) {
637 - $script_handles[] = $block_type->script;
638 - }
639 592 }
640 593
641 - $style_handles = array_unique( $style_handles );
642 - $done = wp_styles()->done;
594 + $editor_styles = array(
595 + 'css' => file_get_contents( $editor_styles_file ),
596 + );
643 597
644 - ob_start();
598 + // Substitute default styles if found. Otherwise, prepend to setting array.
599 + if ( isset( $i ) && $i >= 0 ) {
600 + $settings['styles'][ $i ] = $editor_styles;
601 + } else {
602 + array_unshift( $settings['styles'], $editor_styles );
603 + }
645 604
646 - // We do not need reset styles for the iframed editor.
647 - wp_styles()->done = array( 'wp-reset-editor-styles' );
648 - wp_styles()->do_items( $style_handles );
649 - wp_styles()->done = $done;
605 + return $settings;
606 +}
607 +add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
650 608
651 - $styles = ob_get_clean();
609 +/**
610 + * Extends block editor settings to include a list of image dimensions per size.
611 + *
612 + * @param array $settings Default editor settings.
613 + *
614 + * @return array Filtered editor settings.
615 + */
616 +function gutenberg_extend_settings_image_dimensions( $settings ) {
617 + $image_dimensions = array();
618 + $all_sizes = wp_get_registered_image_subsizes();
619 + foreach ( $settings['imageSizes'] as $size ) {
620 + $key = $size['slug'];
621 + if ( isset( $all_sizes[ $key ] ) ) {
622 + $image_dimensions[ $key ] = $all_sizes[ $key ];
623 + }
624 + }
625 + $settings['imageDimensions'] = $image_dimensions;
626 + return $settings;
627 +}
628 +add_filter( 'block_editor_settings', 'gutenberg_extend_settings_image_dimensions' );
652 629
653 - $script_handles = array_unique( $script_handles );
654 - $done = wp_scripts()->done;
655 -
656 - ob_start();
657 -
658 - wp_scripts()->done = array();
659 - wp_scripts()->do_items( $script_handles );
660 - wp_scripts()->done = $done;
661 -
662 - $scripts = ob_get_clean();
663 -
664 - return array(
665 - 'styles' => $styles,
666 - 'scripts' => $scripts,
630 +/**
631 + * Load a block pattern by name.
632 + *
633 + * @param string $name Block Pattern File name.
634 + *
635 + * @return array Block Pattern Array.
636 + */
637 +function gutenberg_load_block_pattern( $name ) {
638 + return json_decode(
639 + file_get_contents( __DIR__ . '/patterns/' . $name . '.json' ),
640 + true
667 641 );
668 642 }
669 643
670 -add_filter(
671 - 'block_editor_settings_all',
672 - function( $settings ) {
673 - // In the future we can allow WP Dependency handles to be passed.
674 - $settings['__unstableResolvedAssets'] = gutenberg_resolve_assets();
675 - return $settings;
676 - }
677 -);
644 +/**
645 + * Extends block editor settings to include a list of default block patterns.
646 + *
647 + * @param array $settings Default editor settings.
648 + *
649 + * @return array Filtered editor settings.
650 + */
651 +function gutenberg_extend_settings_block_patterns( $settings ) {
652 + $block_patterns = [
653 + gutenberg_load_block_pattern( 'text-two-columns' ),
654 + gutenberg_load_block_pattern( 'two-buttons' ),
655 + gutenberg_load_block_pattern( 'cover-abc' ),
656 + gutenberg_load_block_pattern( 'two-images' ),
657 + ];
658 + $settings['__experimentalBlockPatterns'] = $block_patterns;
659 + return $settings;
660 +}
661 +add_filter( 'block_editor_settings', 'gutenberg_extend_settings_block_patterns' );