PluginProbe
Gutenberg / 10.6.1
Gutenberg v10.6.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 / block-editor.php

block-editor.php in Gutenberg 10.6.1, at lib/block-editor.php

294 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block Editor API.
4 *
5 * @package Gutenberg
6 * @subpackage Editor
7 * @since 10.5.0
8 */
9
10 /**
11 * Returns the list of default categories for block types.
12 *
13 * This is a temporary solution until the Gutenberg plugin sets
14 * the required WordPress version to 5.8.
15 *
16 * @see https://core.trac.wordpress.org/ticket/52920
17 *
18 * @since 10.5.0.
19 *
20 * @return array[] Array of categories for block types.
21 */
22 function gutenberg_get_default_block_categories() {
23 return array(
24 array(
25 'slug' => 'text',
26 'title' => _x( 'Text', 'block category', 'gutenberg' ),
27 'icon' => null,
28 ),
29 array(
30 'slug' => 'media',
31 'title' => _x( 'Media', 'block category', 'gutenberg' ),
32 'icon' => null,
33 ),
34 array(
35 'slug' => 'design',
36 'title' => _x( 'Design', 'block category', 'gutenberg' ),
37 'icon' => null,
38 ),
39 array(
40 'slug' => 'widgets',
41 'title' => _x( 'Widgets', 'block category', 'gutenberg' ),
42 'icon' => null,
43 ),
44 array(
45 'slug' => 'theme',
46 'title' => _x( 'Theme', 'block category', 'gutenberg' ),
47 'icon' => null,
48 ),
49 array(
50 'slug' => 'embed',
51 'title' => _x( 'Embeds', 'block category', 'gutenberg' ),
52 'icon' => null,
53 ),
54 array(
55 'slug' => 'reusable',
56 'title' => _x( 'Reusable Blocks', 'block category', 'gutenberg' ),
57 'icon' => null,
58 ),
59 );
60 }
61
62 /**
63 * Returns all the categories for block types that will be shown in the block editor.
64 *
65 * This is a temporary solution until the Gutenberg plugin sets
66 * the required WordPress version to 5.8.
67 *
68 * @see https://core.trac.wordpress.org/ticket/52920
69 *
70 * @since 10.5.0
71 *
72 * @param string|WP_Post $editor_name_or_post The name of the editor (e.g. 'post-editor')
73 * or the post object.
74 *
75 * @return array[] Array of categories for block types.
76 */
77 function gutenberg_get_block_categories( $editor_name_or_post ) {
78 // Assume the post editor when the WP_Post object passed.
79 $editor_name = is_object( $editor_name_or_post ) ? 'post-editor' : $editor_name_or_post;
80 $default_categories = gutenberg_get_default_block_categories();
81
82 /**
83 * Filters the default array of categories for block types.
84 *
85 * @since 5.8.0
86 *
87 * @param array[] $default_categories Array of categories for block types.
88 */
89 $block_categories = apply_filters( 'block_categories_all', $default_categories );
90 if ( 'post-editor' === $editor_name ) {
91 $post = is_object( $editor_name_or_post ) ? $editor_name_or_post : get_post();
92
93 /**
94 * Filters the default array of categories for block types.
95 *
96 * @since 5.0.0
97 * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain the $post instance.
98 *
99 * @param array[] $block_categories Array of categories for block types.
100 * @param WP_Post $post Post being loaded.
101 */
102 $block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );
103 }
104
105 return $block_categories;
106 }
107
108 /**
109 * Gets the list of allowed block types to use in the block editor.
110 *
111 * This is a temporary solution until the Gutenberg plugin sets
112 * the required WordPress version to 5.8.
113 *
114 * @see https://core.trac.wordpress.org/ticket/52920
115 *
116 * @since 10.5.0
117 *
118 * @param string $editor_name The name of the editor (e.g. 'post-editor').
119 *
120 * @return bool|array Array of block type slugs, or boolean to enable/disable all.
121 */
122 function gutenberg_get_allowed_block_types( $editor_name ) {
123 $allowed_block_types = true;
124
125 /**
126 * Filters the allowed block types for the given editor, defaulting to true (all
127 * registered block types supported).
128 *
129 * @since 5.8.0
130 *
131 * @param bool|array $allowed_block_types Array of block type slugs, or
132 * boolean to enable/disable all.
133 */
134 $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types );
135 if ( 'post-editor' === $editor_name ) {
136 $post = get_post();
137
138 /**
139 * Filters the allowed block types for the editor, defaulting to true (all
140 * block types supported).
141 *
142 * @since 5.0.0
143 * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
144 *
145 * @param bool|array $allowed_block_types Array of block type slugs, or
146 * boolean to enable/disable all.
147 * @param WP_Post $post The post resource data.
148 */
149 $allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );
150 }
151
152 return $allowed_block_types;
153 }
154
155 /**
156 * Returns the default block editor settings.
157 *
158 * This is a temporary solution until the Gutenberg plugin sets
159 * the required WordPress version to 5.8.
160 *
161 * @see https://core.trac.wordpress.org/ticket/52920
162 *
163 * @since 10.5.0
164 *
165 * @return array The default block editor settings.
166 */
167 function gutenberg_get_default_block_editor_settings() {
168 // Media settings.
169 $max_upload_size = wp_max_upload_size();
170 if ( ! $max_upload_size ) {
171 $max_upload_size = 0;
172 }
173
174 /** This filter is documented in wp-admin/includes/media.php */
175 $image_size_names = apply_filters(
176 'image_size_names_choose',
177 array(
178 'thumbnail' => __( 'Thumbnail', 'gutenberg' ),
179 'medium' => __( 'Medium', 'gutenberg' ),
180 'large' => __( 'Large', 'gutenberg' ),
181 'full' => __( 'Full Size', 'gutenberg' ),
182 )
183 );
184
185 $available_image_sizes = array();
186 foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
187 $available_image_sizes[] = array(
188 'slug' => $image_size_slug,
189 'name' => $image_size_name,
190 );
191 }
192
193 $default_size = get_option( 'image_default_size', 'large' );
194 $image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $default_size : 'large';
195
196 $image_dimensions = array();
197 $all_sizes = wp_get_registered_image_subsizes();
198 foreach ( $available_image_sizes as $size ) {
199 $key = $size['slug'];
200 if ( isset( $all_sizes[ $key ] ) ) {
201 $image_dimensions[ $key ] = $all_sizes[ $key ];
202 }
203 }
204
205 $editor_settings = array(
206 '__unstableEnableFullSiteEditingBlocks' => gutenberg_supports_block_templates(),
207 'alignWide' => get_theme_support( 'align-wide' ),
208 'allowedBlockTypes' => true,
209 'allowedMimeTypes' => get_allowed_mime_types(),
210 'blockCategories' => gutenberg_get_default_block_categories(),
211 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
212 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
213 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
214 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
215 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ),
216 'enableCustomUnits' => get_theme_support( 'custom-units' ),
217 'isRTL' => is_rtl(),
218 'imageDefaultSize' => $image_default_size,
219 'imageDimensions' => $image_dimensions,
220 'imageEditing' => true,
221 'imageSizes' => $available_image_sizes,
222 'maxUploadFileSize' => $max_upload_size,
223 );
224
225 // Theme settings.
226 $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
227 if ( false !== $color_palette ) {
228 $editor_settings['colors'] = $color_palette;
229 }
230
231 $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
232 if ( false !== $font_sizes ) {
233 $editor_settings['fontSizes'] = $font_sizes;
234 }
235
236 $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
237 if ( false !== $gradient_presets ) {
238 $editor_settings['gradients'] = $gradient_presets;
239 }
240
241 return $editor_settings;
242 }
243
244 /**
245 * Returns the contextualized block editor settings settings for a selected editor type.
246 *
247 * This is a temporary solution until the Gutenberg plugin sets
248 * the required WordPress version to 5.8.
249 *
250 * @see https://core.trac.wordpress.org/ticket/52920
251 *
252 * @since 10.5.0
253 *
254 * @param string $editor_name The name of the editor (e.g. 'post-editor').
255 * @param array $custom_settings Optional custom settings to use with the editor type.
256 *
257 * @return array The contextualized block editor settings.
258 */
259 function gutenberg_get_block_editor_settings( $editor_name, $custom_settings = array() ) {
260 $editor_settings = array_merge(
261 gutenberg_get_default_block_editor_settings( $editor_name ),
262 array(
263 'allowedBlockTypes' => gutenberg_get_allowed_block_types( $editor_name ),
264 'blockCategories' => gutenberg_get_block_categories( $editor_name ),
265 ),
266 $custom_settings
267 );
268
269 /**
270 * Filters the settings to pass to the block editor for all editor types.
271 *
272 * @since 5.8.0
273 *
274 * @param array $editor_settings Default editor settings.
275 */
276 $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings );
277 if ( 'post-editor' === $editor_name ) {
278 $post = get_post();
279
280 /**
281 * Filters the settings to pass to the block editor.
282 *
283 * @since 5.0.0
284 * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
285 *
286 * @param array $editor_settings Default editor settings.
287 * @param WP_Post $post Post being edited.
288 */
289 $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );
290 }
291
292 return $editor_settings;
293 }
294