PluginProbe
Gutenberg / 10.3.1
Gutenberg v10.3.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 10.3.1, at lib/editor-settings.php

156 lines 4.8 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 * Returns editor settings that are common to all editors:
10 * post, site, widgets, and navigation.
11 *
12 * All these settings are already part of core,
13 * see edit-form-blocks.php
14 *
15 * @return array Common editor settings.
16 */
17 function gutenberg_get_common_block_editor_settings() {
18 $max_upload_size = wp_max_upload_size();
19 if ( ! $max_upload_size ) {
20 $max_upload_size = 0;
21 }
22
23 $available_image_sizes = array();
24 // This filter is documented in wp-admin/includes/media.php.
25 $image_size_names = apply_filters(
26 'image_size_names_choose',
27 array(
28 'thumbnail' => __( 'Thumbnail', 'gutenberg' ),
29 'medium' => __( 'Medium', 'gutenberg' ),
30 'large' => __( 'Large', 'gutenberg' ),
31 'full' => __( 'Full Size', 'gutenberg' ),
32 )
33 );
34 foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
35 $available_image_sizes[] = array(
36 'slug' => $image_size_slug,
37 'name' => $image_size_name,
38 );
39 };
40
41 $settings = array(
42 '__unstableEnableFullSiteEditingBlocks' => gutenberg_is_fse_theme(),
43 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
44 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
45 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
46 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
47 'enableCustomUnits' => get_theme_support( 'custom-units' ),
48 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ),
49 'imageSizes' => $available_image_sizes,
50 'isRTL' => is_rtl(),
51 'maxUploadFileSize' => $max_upload_size,
52 );
53
54 $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
55 if ( false !== $color_palette ) {
56 $settings['colors'] = $color_palette;
57 }
58
59 $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
60 if ( false !== $font_sizes ) {
61 $settings['fontSizes'] = $font_sizes;
62 }
63
64 $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
65 if ( false !== $gradient_presets ) {
66 $settings['gradients'] = $gradient_presets;
67 }
68
69 return $settings;
70 }
71
72 /**
73 * Extends the block editor with settings that are only in the plugin.
74 *
75 * @param array $settings Existing editor settings.
76 *
77 * @return array Filtered settings.
78 */
79 function gutenberg_extend_post_editor_settings( $settings ) {
80 $image_default_size = get_option( 'image_default_size', 'large' );
81 $image_sizes = wp_list_pluck( $settings['imageSizes'], 'slug' );
82
83 $settings['imageDefaultSize'] = in_array( $image_default_size, $image_sizes, true ) ? $image_default_size : 'large';
84 $settings['__unstableEnableFullSiteEditingBlocks'] = gutenberg_is_fse_theme();
85
86 return $settings;
87 }
88 add_filter( 'block_editor_settings', 'gutenberg_extend_post_editor_settings' );
89
90 /**
91 * Initialize a block-based editor.
92 *
93 * @param string $editor_name Editor name.
94 * @param string $editor_script_handle Editor script handle.
95 * @param array $settings {
96 * Elements to initialize a block-based editor.
97 *
98 * @type array $preload_paths Array of paths to preload.
99 * @type string $initializer_name Editor initialization function name.
100 * @type array $editor_settings Editor settings.
101 * }
102 * @return void
103 */
104 function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $settings ) {
105
106 $defaults = array(
107 'preload_paths' => array(),
108 'initializer_name' => 'initialize',
109 'editor_settings' => array(),
110 );
111
112 $settings = wp_parse_args( $settings, $defaults );
113
114 /**
115 * Preload common data by specifying an array of REST API paths that will be preloaded.
116 *
117 * Filters the array of paths that will be preloaded.
118 *
119 * @param string[] $preload_paths Array of paths to preload.
120 */
121 $preload_paths = apply_filters( "{$editor_name}_preload_paths", $settings['preload_paths'] );
122
123 $preload_data = array_reduce(
124 $preload_paths,
125 'rest_preload_api_request',
126 array()
127 );
128 wp_add_inline_script(
129 'wp-api-fetch',
130 sprintf(
131 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
132 wp_json_encode( $preload_data )
133 ),
134 'after'
135 );
136 wp_add_inline_script(
137 "wp-{$editor_script_handle}",
138 sprintf(
139 'wp.domReady( function() {
140 wp.%s.%s( "%s", %s );
141 } );',
142 lcfirst( str_replace( '-', '', ucwords( $editor_script_handle, '-' ) ) ),
143 $settings['initializer_name'],
144 str_replace( '_', '-', $editor_name ),
145 wp_json_encode( $settings['editor_settings'] )
146 )
147 );
148
149 // Preload server-registered block schemas.
150 wp_add_inline_script(
151 'wp-blocks',
152 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
153 );
154
155 }
156