PluginProbe
Gutenberg / 11.8.1
Gutenberg v11.8.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 / editor-settings.php

editor-settings.php in Gutenberg 11.8.1, at lib/editor-settings.php

118 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Utilities to manage editor settings.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Extends the block editor with settings that are only in the plugin.
10 *
11 * This is a temporary solution until the Gutenberg plugin sets
12 * the required WordPress version to 5.8.
13 *
14 * @see https://core.trac.wordpress.org/ticket/52920
15 *
16 * @param array $settings Existing editor settings.
17 *
18 * @return array Filtered settings.
19 */
20 function gutenberg_extend_post_editor_settings( $settings ) {
21 $image_default_size = get_option( 'image_default_size', 'large' );
22 $image_sizes = wp_list_pluck( $settings['imageSizes'], 'slug' );
23
24 $settings['imageDefaultSize'] = in_array( $image_default_size, $image_sizes, true ) ? $image_default_size : 'large';
25 $settings['__unstableEnableFullSiteEditingBlocks'] = gutenberg_supports_block_templates();
26
27 if ( gutenberg_is_fse_theme() ) {
28 $settings['defaultTemplatePartAreas'] = gutenberg_get_allowed_template_part_areas();
29 }
30
31 return $settings;
32 }
33 // This can be removed when plugin support requires WordPress 5.8.0+.
34 if ( function_exists( 'get_block_editor_settings' ) ) {
35 add_filter( 'block_editor_settings_all', 'gutenberg_extend_post_editor_settings' );
36 } else {
37 add_filter( 'block_editor_settings', 'gutenberg_extend_post_editor_settings' );
38 }
39
40 /**
41 * Initialize a block-based editor.
42 *
43 * @param string $editor_name Editor name.
44 * @param string $editor_script_handle Editor script handle.
45 * @param array $settings {
46 * Elements to initialize a block-based editor.
47 *
48 * @type array $preload_paths Array of paths to preload.
49 * @type string $initializer_name Editor initialization function name.
50 * @type array $editor_settings Editor settings.
51 * }
52 * @return void
53 */
54 function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $settings ) {
55
56 $defaults = array(
57 'preload_paths' => array(),
58 'initializer_name' => 'initialize',
59 'editor_settings' => array(),
60 );
61
62 $settings = wp_parse_args( $settings, $defaults );
63
64 /**
65 * Preload common data by specifying an array of REST API paths that will be preloaded.
66 *
67 * Filters the array of paths that will be preloaded.
68 *
69 * @param string[] $preload_paths Array of paths to preload.
70 */
71 $preload_paths = apply_filters( "{$editor_name}_preload_paths", $settings['preload_paths'] );
72
73 $preload_data = array_reduce(
74 $preload_paths,
75 'rest_preload_api_request',
76 array()
77 );
78
79 /**
80 * Filters the array of data that has been preloaded.
81 *
82 * The dynamic portion of the hook name, `$editor_name`, refers to the type of block editor.
83 *
84 * @param array $preload_data Array containing the preloaded data.
85 * @param string $editor_name Current editor name.
86 * @param array Array containing the filtered preloaded data.
87 */
88 $preload_data = apply_filters( 'block_editor_preload_data', $preload_data, $editor_name );
89
90 wp_add_inline_script(
91 'wp-api-fetch',
92 sprintf(
93 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
94 wp_json_encode( $preload_data )
95 ),
96 'after'
97 );
98 wp_add_inline_script(
99 "wp-{$editor_script_handle}",
100 sprintf(
101 'wp.domReady( function() {
102 wp.%s.%s( "%s", %s );
103 } );',
104 lcfirst( str_replace( '-', '', ucwords( $editor_script_handle, '-' ) ) ),
105 $settings['initializer_name'],
106 str_replace( '_', '-', $editor_name ),
107 wp_json_encode( $settings['editor_settings'] )
108 )
109 );
110
111 // Preload server-registered block schemas.
112 wp_add_inline_script(
113 'wp-blocks',
114 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
115 );
116
117 }
118