PluginProbe
Gutenberg / 8.2.1
Gutenberg v8.2.1
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 12.6.0 7.4.0 All 402 releases
gutenberg / lib / client-assets.php

client-assets.php in Gutenberg 8.2.1, at lib/client-assets.php

721 lines 24.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions to register client-side assets (scripts and stylesheets) for the
4 * Gutenberg editor plugin.
5 *
6 * @package gutenberg
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 die( 'Silence is golden.' );
11 }
12
13 /**
14 * Retrieves the root plugin path.
15 *
16 * @return string Root path to the gutenberg plugin.
17 *
18 * @since 0.1.0
19 */
20 function gutenberg_dir_path() {
21 return plugin_dir_path( dirname( __FILE__ ) );
22 }
23
24 /**
25 * Retrieves a URL to a file in the gutenberg plugin.
26 *
27 * @param string $path Relative path of the desired file.
28 *
29 * @return string Fully qualified URL pointing to the desired file.
30 *
31 * @since 0.1.0
32 */
33 function gutenberg_url( $path ) {
34 return plugins_url( $path, dirname( __FILE__ ) );
35 }
36
37 /**
38 * Registers a script according to `wp_register_script`. Honors this request by
39 * reassigning internal dependency properties of any script handle already
40 * registered by that name. It does not deregister the original script, to
41 * avoid losing inline scripts which may have been attached.
42 *
43 * @since 4.1.0
44 *
45 * @param WP_Scripts $scripts WP_Scripts instance.
46 * @param string $handle Name of the script. Should be unique.
47 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
48 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
49 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
50 * as a query string for cache busting purposes. If version is set to false, a version
51 * number is automatically added equal to current installed WordPress version.
52 * If set to null, no version is added.
53 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
54 * Default 'false'.
55 */
56 function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
57 $script = $scripts->query( $handle, 'registered' );
58 if ( $script ) {
59 /*
60 * In many ways, this is a reimplementation of `wp_register_script` but
61 * bypassing consideration of whether a script by the given handle had
62 * already been registered.
63 */
64
65 // See: `_WP_Dependency::__construct` .
66 $script->src = $src;
67 $script->deps = $deps;
68 $script->ver = $ver;
69 $script->args = $in_footer;
70
71 /*
72 * The script's `group` designation is an indication of whether it is
73 * to be printed in the header or footer. The behavior here defers to
74 * the arguments as passed. Specifically, group data is not assigned
75 * for a script unless it is designated to be printed in the footer.
76 */
77
78 // See: `wp_register_script` .
79 unset( $script->extra['group'] );
80 if ( $in_footer ) {
81 $script->add_data( 'group', 1 );
82 }
83 } else {
84 $scripts->add( $handle, $src, $deps, $ver, $in_footer );
85 }
86
87 /*
88 * `WP_Dependencies::set_translations` will fall over on itself if setting
89 * translations on the `wp-i18n` handle, since it internally adds `wp-i18n`
90 * as a dependency of itself, exhausting memory. The same applies for the
91 * polyfill script, which is a dependency _of_ `wp-i18n`.
92 *
93 * See: https://core.trac.wordpress.org/ticket/46089
94 */
95 if ( 'wp-i18n' !== $handle && 'wp-polyfill' !== $handle ) {
96 $scripts->set_translations( $handle, 'default' );
97 }
98 }
99
100 /**
101 * Filters the default translation file load behavior to load the Gutenberg
102 * plugin translation file, if available.
103 *
104 * @param string|false $file Path to the translation file to load. False if
105 * there isn't one.
106 * @param string $handle Name of the script to register a translation
107 * domain to.
108 *
109 * @return string|false Filtered path to the Gutenberg translation file, if
110 * available.
111 */
112 function gutenberg_override_translation_file( $file, $handle ) {
113 if ( ! $file ) {
114 return $file;
115 }
116
117 // Ignore scripts whose handle does not have the "wp-" prefix.
118 if ( 'wp-' !== substr( $handle, 0, 3 ) ) {
119 return $file;
120 }
121
122 // Ignore scripts that are not found in the expected `build/` location.
123 $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.js';
124 if ( ! file_exists( $script_path ) ) {
125 return $file;
126 }
127
128 /*
129 * The default file will be in the plugins language directory, omitting the
130 * domain since Gutenberg assigns the script translations as the default.
131 *
132 * Example: /www/wp-content/languages/plugins/de_DE-07d88e6a803e01276b9bfcc1203e862e.json
133 *
134 * The logic of `load_script_textdomain` is such that it will assume to
135 * search in the plugins language directory, since the assigned source of
136 * the overridden Gutenberg script originates in the plugins directory.
137 *
138 * The plugin translation files each begin with the slug of the plugin, so
139 * it's a simple matter of prepending the Gutenberg plugin slug.
140 */
141 $path_parts = pathinfo( $file );
142 $plugin_translation_file = (
143 $path_parts['dirname'] .
144 '/gutenberg-' .
145 $path_parts['basename']
146 );
147
148 return $plugin_translation_file;
149 }
150 add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 );
151
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 /**
172 * Registers a style according to `wp_register_style`. Honors this request by
173 * deregistering any style by the same handler before registration.
174 *
175 * @since 4.1.0
176 *
177 * @param WP_Styles $styles WP_Styles instance.
178 * @param string $handle Name of the stylesheet. Should be unique.
179 * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
180 * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
181 * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
182 * as a query string for cache busting purposes. If version is set to false, a version
183 * number is automatically added equal to current installed WordPress version.
184 * If set to null, no version is added.
185 * @param string $media Optional. The media for which this stylesheet has been defined.
186 * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
187 * '(orientation: portrait)' and '(max-width: 640px)'.
188 */
189 function gutenberg_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
190 $style = $styles->query( $handle, 'registered' );
191 if ( $style ) {
192 $styles->remove( $handle );
193 }
194 $styles->add( $handle, $src, $deps, $ver, $media );
195 }
196
197 /**
198 * Registers vendor JavaScript files to be used as dependencies of the editor
199 * and plugins.
200 *
201 * This function is called from a script during the plugin build process, so it
202 * should not call any WordPress PHP functions.
203 *
204 * @since 0.1.0
205 *
206 * @param WP_Scripts $scripts WP_Scripts instance.
207 */
208 function gutenberg_register_vendor_scripts( $scripts ) {
209 $suffix = SCRIPT_DEBUG ? '' : '.min';
210
211 /*
212 * This script registration and the corresponding function should be removed
213 * once the plugin is updated to support WordPress 5.4.0 and newer.
214 *
215 * See: `gutenberg_add_url_polyfill`
216 */
217 gutenberg_register_vendor_script(
218 $scripts,
219 'wp-polyfill-url',
220 'https://unpkg.com/core-js-url-browser@3.6.4/url' . $suffix . '.js',
221 array(),
222 '3.6.4'
223 );
224
225 /*
226 * This script registration and the corresponding function should be removed
227 * removed once the plugin is updated to support WordPress 5.4.0 and newer.
228 *
229 * See: `gutenberg_add_dom_rect_polyfill`
230 */
231 gutenberg_register_vendor_script(
232 $scripts,
233 'wp-polyfill-dom-rect',
234 'https://unpkg.com/polyfill-library@3.42.0/polyfills/DOMRect/polyfill.js',
235 array(),
236 '3.42.0'
237 );
238 }
239 add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
240
241 /**
242 * Registers all the WordPress packages scripts that are in the standardized
243 * `build/` location.
244 *
245 * @since 4.5.0
246 *
247 * @param WP_Scripts $scripts WP_Scripts instance.
248 */
249 function gutenberg_register_packages_scripts( $scripts ) {
250 foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) {
251 // Prefix `wp-` to package directory to get script handle.
252 // For example, `…/build/a11y/index.js` becomes `wp-a11y`.
253 $handle = 'wp-' . basename( dirname( $path ) );
254
255 // Replace `.js` extension with `.asset.php` to find the generated dependencies file.
256 $asset_file = substr( $path, 0, -3 ) . '.asset.php';
257 $asset = file_exists( $asset_file )
258 ? require( $asset_file )
259 : null;
260 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
261 $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
262
263 // Add dependencies that cannot be detected and generated by build tools.
264 switch ( $handle ) {
265 case 'wp-block-library':
266 array_push( $dependencies, 'editor' );
267 break;
268
269 case 'wp-edit-post':
270 array_push( $dependencies, 'media-models', 'media-views', 'postbox' );
271 break;
272
273 case 'wp-edit-site':
274 array_push( $dependencies, 'wp-dom-ready' );
275 break;
276 }
277
278 // Get the path from Gutenberg directory as expected by `gutenberg_url`.
279 $gutenberg_path = substr( $path, strlen( gutenberg_dir_path() ) );
280
281 gutenberg_override_script(
282 $scripts,
283 $handle,
284 gutenberg_url( $gutenberg_path ),
285 $dependencies,
286 $version,
287 true
288 );
289 }
290 }
291 add_action( 'wp_default_scripts', 'gutenberg_register_packages_scripts' );
292
293 /**
294 * Registers all the WordPress packages styles that are in the standardized
295 * `build/` location.
296 *
297 * @since 6.7.0
298
299 * @param WP_Styles $styles WP_Styles instance.
300 */
301 function gutenberg_register_packages_styles( $styles ) {
302 // Editor Styles.
303 gutenberg_override_style(
304 $styles,
305 'wp-block-editor',
306 gutenberg_url( 'build/block-editor/style.css' ),
307 array( 'wp-components', 'wp-editor-font' ),
308 filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
309 );
310 $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
311
312 gutenberg_override_style(
313 $styles,
314 'wp-editor',
315 gutenberg_url( 'build/editor/style.css' ),
316 array( 'wp-components', 'wp-block-editor', 'wp-nux' ),
317 filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
318 );
319 $styles->add_data( 'wp-editor', 'rtl', 'replace' );
320
321 gutenberg_override_style(
322 $styles,
323 'wp-edit-post',
324 gutenberg_url( 'build/edit-post/style.css' ),
325 array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ),
326 filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' )
327 );
328 $styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
329
330 gutenberg_override_style(
331 $styles,
332 'wp-components',
333 gutenberg_url( 'build/components/style.css' ),
334 array(),
335 filemtime( gutenberg_dir_path() . 'build/components/style.css' )
336 );
337 $styles->add_data( 'wp-components', 'rtl', 'replace' );
338
339 gutenberg_override_style(
340 $styles,
341 'wp-block-library',
342 gutenberg_url( 'build/block-library/style.css' ),
343 array(),
344 filemtime( gutenberg_dir_path() . 'build/block-library/style.css' )
345 );
346 $styles->add_data( 'wp-block-library', 'rtl', 'replace' );
347
348 gutenberg_override_style(
349 $styles,
350 'wp-format-library',
351 gutenberg_url( 'build/format-library/style.css' ),
352 array( 'wp-block-editor', 'wp-components' ),
353 filemtime( gutenberg_dir_path() . 'build/format-library/style.css' )
354 );
355 $styles->add_data( 'wp-format-library', 'rtl', 'replace' );
356
357 gutenberg_override_style(
358 $styles,
359 'wp-edit-blocks',
360 gutenberg_url( 'build/block-library/editor.css' ),
361 array(
362 'wp-components',
363 'wp-editor',
364 'wp-block-library',
365 // Always include visual styles so the editor never appears broken.
366 'wp-block-library-theme',
367 ),
368 filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' )
369 );
370 $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
371
372 gutenberg_override_style(
373 $styles,
374 'wp-nux',
375 gutenberg_url( 'build/nux/style.css' ),
376 array( 'wp-components' ),
377 filemtime( gutenberg_dir_path() . 'build/nux/style.css' )
378 );
379 $styles->add_data( 'wp-nux', 'rtl', 'replace' );
380
381 gutenberg_override_style(
382 $styles,
383 'wp-block-library-theme',
384 gutenberg_url( 'build/block-library/theme.css' ),
385 array(),
386 filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' )
387 );
388 $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
389
390 gutenberg_override_style(
391 $styles,
392 'wp-list-reusable-blocks',
393 gutenberg_url( 'build/list-reusable-blocks/style.css' ),
394 array( 'wp-components' ),
395 filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' )
396 );
397 $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
398
399 gutenberg_override_style(
400 $styles,
401 'wp-edit-navigation',
402 gutenberg_url( 'build/edit-navigation/style.css' ),
403 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
404 filemtime( gutenberg_dir_path() . 'build/edit-navigation/style.css' )
405 );
406 $styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' );
407
408 gutenberg_override_style(
409 $styles,
410 'wp-edit-site',
411 gutenberg_url( 'build/edit-site/style.css' ),
412 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
413 filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' )
414 );
415 $styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
416
417 gutenberg_override_style(
418 $styles,
419 'wp-edit-widgets',
420 gutenberg_url( 'build/edit-widgets/style.css' ),
421 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
422 filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' )
423 );
424 $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
425
426 gutenberg_override_style(
427 $styles,
428 'wp-block-directory',
429 gutenberg_url( 'build/block-directory/style.css' ),
430 array( 'wp-block-editor', 'wp-components' ),
431 filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' )
432 );
433 $styles->add_data( 'wp-block-directory', 'rtl', 'replace' );
434 }
435 add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
436
437 /**
438 * Registers common scripts and styles to be used as dependencies of the editor
439 * and plugins.
440 *
441 * @since 0.1.0
442 */
443 function gutenberg_enqueue_block_editor_assets() {
444 if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
445 $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;
446
447 wp_enqueue_script(
448 'gutenberg-live-reload',
449 $live_reload_url
450 );
451 }
452 }
453 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets' );
454
455 /**
456 * Retrieves a unique and reasonably short and human-friendly filename for a
457 * vendor script based on a URL and the script handle.
458 *
459 * @param string $handle The name of the script.
460 * @param string $src Full URL of the external script.
461 *
462 * @return string Script filename suitable for local caching.
463 *
464 * @since 0.1.0
465 */
466 function gutenberg_vendor_script_filename( $handle, $src ) {
467 $filename = basename( $src );
468 $match = preg_match(
469 '/^'
470 . '(?P<ignore>.*?)'
471 . '(?P<suffix>\.min)?'
472 . '(?P<extension>\.js)'
473 . '(?P<extra>.*)'
474 . '$/',
475 $filename,
476 $filename_pieces
477 );
478
479 $prefix = $handle;
480 $suffix = $match ? $filename_pieces['suffix'] : '';
481 $hash = substr( md5( $src ), 0, 8 );
482
483 return "${prefix}${suffix}.${hash}.js";
484 }
485
486 /**
487 * Registers a vendor script from a URL, preferring a locally cached version if
488 * possible, or downloading it if the cached version is unavailable or
489 * outdated.
490 *
491 * @param WP_Scripts $scripts WP_Scripts instance.
492 * @param string $handle Name of the script.
493 * @param string $src Full URL of the external script.
494 * @param array $deps Optional. An array of registered script handles this
495 * script depends on.
496 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
497 * as a query string for cache busting purposes. If version is set to false, a version
498 * number is automatically added equal to current installed WordPress version.
499 * If set to null, no version is added.
500 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
501 * Default 'false'.
502 *
503 * @since 0.1.0
504 */
505 function gutenberg_register_vendor_script( $scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
506 if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
507 return;
508 }
509
510 $filename = gutenberg_vendor_script_filename( $handle, $src );
511
512 if ( defined( 'GUTENBERG_LIST_VENDOR_ASSETS' ) && GUTENBERG_LIST_VENDOR_ASSETS ) {
513 echo "$src|$filename\n";
514 return;
515 }
516
517 $full_path = gutenberg_dir_path() . 'vendor/' . $filename;
518
519 $needs_fetch = (
520 defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE && (
521 ! file_exists( $full_path ) ||
522 time() - filemtime( $full_path ) >= DAY_IN_SECONDS
523 )
524 );
525
526 if ( $needs_fetch ) {
527 // Determine whether we can write to this file. If not, don't waste
528 // time doing a network request.
529 // @codingStandardsIgnoreStart
530 $f = @fopen( $full_path, 'a' );
531 // @codingStandardsIgnoreEnd
532 if ( ! $f ) {
533 // Failed to open the file for writing, probably due to server
534 // permissions. Enqueue the script directly from the URL instead.
535 gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
536 return;
537 }
538 fclose( $f );
539 $response = wp_remote_get( $src );
540 if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
541 $f = fopen( $full_path, 'w' );
542 fwrite( $f, wp_remote_retrieve_body( $response ) );
543 fclose( $f );
544 } elseif ( ! filesize( $full_path ) ) {
545 // The request failed. If the file is already cached, continue to
546 // use this file. If not, then unlink the 0 byte file, and enqueue
547 // the script directly from the URL.
548 gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
549 unlink( $full_path );
550 return;
551 }
552 }
553 gutenberg_override_script(
554 $scripts,
555 $handle,
556 gutenberg_url( 'vendor/' . $filename ),
557 $deps,
558 $ver,
559 $in_footer
560 );
561 }
562
563 /**
564 * Extends block editor settings to include Gutenberg's `editor-styles.css` as
565 * taking precedent those styles shipped with core.
566 *
567 * @param array $settings Default editor settings.
568 *
569 * @return array Filtered editor settings.
570 */
571 function gutenberg_extend_block_editor_styles( $settings ) {
572 $editor_styles_file = gutenberg_dir_path() . 'build/editor/editor-styles.css';
573
574 /*
575 * If, for whatever reason, the built editor styles do not exist, avoid
576 * override and fall back to the default.
577 */
578 if ( ! file_exists( $editor_styles_file ) ) {
579 return $settings;
580 }
581
582 if ( empty( $settings['styles'] ) ) {
583 $settings['styles'] = array();
584 } else {
585 /*
586 * The styles setting is an array of CSS strings, so there is no direct
587 * way to find the default styles. To maximize stability, load (again)
588 * the default styles from disk and find its place in the array.
589 *
590 * See: https://github.com/WordPress/wordpress-develop/blob/5.0.3/src/wp-admin/edit-form-blocks.php#L168-L175
591 */
592
593 $default_styles = file_get_contents(
594 ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
595 );
596
597 /*
598 * Iterate backwards from the end of the array since the preferred
599 * insertion point in case not found is prepended as first entry.
600 */
601 for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) {
602 if ( isset( $settings['styles'][ $i ]['css'] ) &&
603 $default_styles === $settings['styles'][ $i ]['css'] ) {
604 break;
605 }
606 }
607 }
608
609 $editor_styles = array(
610 'css' => file_get_contents( $editor_styles_file ),
611 );
612
613 // Substitute default styles if found. Otherwise, prepend to setting array.
614 if ( isset( $i ) && $i >= 0 ) {
615 $settings['styles'][ $i ] = $editor_styles;
616 } else {
617 array_unshift( $settings['styles'], $editor_styles );
618 }
619
620 return $settings;
621 }
622 add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
623
624 /**
625 * Load a block pattern by name.
626 *
627 * @param string $name Block Pattern File name.
628 *
629 * @return array Block Pattern Array.
630 */
631 function gutenberg_load_block_pattern( $name ) {
632 return require( __DIR__ . '/patterns/' . $name . '.php' );
633 }
634
635 /**
636 * Extends block editor settings to include a list of default patterns.
637 *
638 * @param array $settings Default editor settings.
639 *
640 * @return array Filtered editor settings.
641 */
642 function gutenberg_extend_settings_block_patterns( $settings ) {
643 if ( empty( $settings['__experimentalBlockPatterns'] ) ) {
644 $settings['__experimentalBlockPatterns'] = array();
645 }
646
647 $settings['__experimentalBlockPatterns'] = array_merge(
648 WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
649 $settings['__experimentalBlockPatterns']
650 );
651
652 if ( empty( $settings['__experimentalBlockPatternCategories'] ) ) {
653 $settings['__experimentalBlockPatternCategories'] = array();
654 }
655
656 $settings['__experimentalBlockPatternCategories'] = array_merge(
657 WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
658 $settings['__experimentalBlockPatternCategories']
659 );
660
661 return $settings;
662 }
663 add_filter( 'block_editor_settings', 'gutenberg_extend_settings_block_patterns', 0 );
664
665 /**
666 * Extends block editor settings to determine whether to use custom line height controls.
667 *
668 * @param array $settings Default editor settings.
669 *
670 * @return array Filtered editor settings.
671 */
672 function gutenberg_extend_settings_custom_line_height( $settings ) {
673 $settings['__experimentalDisableCustomLineHeight'] = get_theme_support( 'disable-custom-line-height' );
674 return $settings;
675 }
676 add_filter( 'block_editor_settings', 'gutenberg_extend_settings_custom_line_height' );
677
678 /**
679 * Extends block editor settings to determine whether to use custom unit controls.
680 * Currently experimental.
681 *
682 * @param array $settings Default editor settings.
683 *
684 * @return array Filtered editor settings.
685 */
686 function gutenberg_extend_settings_custom_units( $settings ) {
687 $settings['__experimentalDisableCustomUnits'] = get_theme_support( 'experimental-custom-units' );
688 return $settings;
689 }
690 add_filter( 'block_editor_settings', 'gutenberg_extend_settings_custom_units' );
691
692 /*
693 * Register default patterns if not registered in Core already.
694 */
695
696 if ( class_exists( 'WP_Block_Patterns_Registry' ) && ! WP_Block_Patterns_Registry::get_instance()->is_registered( 'text-two-columns' ) ) {
697 register_block_pattern( 'core/text-two-columns', gutenberg_load_block_pattern( 'text-two-columns' ) );
698 register_block_pattern( 'core/two-buttons', gutenberg_load_block_pattern( 'two-buttons' ) );
699 register_block_pattern( 'core/cover-abc', gutenberg_load_block_pattern( 'cover-abc' ) );
700 register_block_pattern( 'core/two-images', gutenberg_load_block_pattern( 'two-images' ) );
701 register_block_pattern( 'core/hero-two-columns', gutenberg_load_block_pattern( 'hero-two-columns' ) );
702 register_block_pattern( 'core/numbered-features', gutenberg_load_block_pattern( 'numbered-features' ) );
703 register_block_pattern( 'core/its-time', gutenberg_load_block_pattern( 'its-time' ) );
704 register_block_pattern( 'core/hero-right-column', gutenberg_load_block_pattern( 'hero-right-column' ) );
705 register_block_pattern( 'core/testimonials', gutenberg_load_block_pattern( 'testimonials' ) );
706 register_block_pattern( 'core/features-services', gutenberg_load_block_pattern( 'features-services' ) );
707 }
708
709 /*
710 * Register default pattern categories if not registered in Core already.
711 */
712 if ( class_exists( 'WP_Block_Pattern_Categories_Registry' ) ) {
713 register_block_pattern_category( 'text', array( 'label' => _x( 'Text', 'Block pattern category', 'gutenberg' ) ) );
714 register_block_pattern_category( 'hero', array( 'label' => _x( 'Hero', 'Block pattern category', 'gutenberg' ) ) );
715 register_block_pattern_category( 'columns', array( 'label' => _x( 'Columns', 'Block pattern category', 'gutenberg' ) ) );
716 register_block_pattern_category( 'buttons', array( 'label' => _x( 'Buttons', 'Block pattern category', 'gutenberg' ) ) );
717 register_block_pattern_category( 'gallery', array( 'label' => _x( 'Gallery', 'Block pattern category', 'gutenberg' ) ) );
718 register_block_pattern_category( 'features', array( 'label' => _x( 'Features', 'Block pattern category', 'gutenberg' ) ) );
719 register_block_pattern_category( 'testimonials', array( 'label' => _x( 'Testimonials', 'Block pattern category', 'gutenberg' ) ) );
720 }
721