Skip to content

Commit a19f589

Browse files
committed
Editor: Abstract block editor configuration
There are several WordPress hooks defined on the server that depend on $post object that isn’t present on the new screens like edit site, edit widgets, or edit navigation. This patch deprecates existing filters and introduces replacements that are context-aware. Props azaozz, andraganescu, jeremyfelt, nosolosw, youknowriad. See #52920. git-svn-id: https://develop.svn.wordpress.org/trunk@50776 602fd350-edb4-49c9-b593-d223f7449a82
1 parent bf63735 commit a19f589

5 files changed

Lines changed: 584 additions & 158 deletions

File tree

src/wp-admin/edit-form-blocks.php

Lines changed: 4 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
2525

26+
$editor_name = 'post-editor';
27+
2628
// Flag that we're loading the block editor.
2729
$current_screen = get_current_screen();
2830
$current_screen->is_block_editor( true );
@@ -56,6 +58,7 @@
5658
sprintf( '/wp/v2/%s/%d/autosaves?context=edit', $rest_base, $post->ID ),
5759
);
5860

61+
5962
/**
6063
* Preload common data by specifying an array of REST API paths that will be preloaded.
6164
*
@@ -135,31 +138,6 @@
135138
'before'
136139
);
137140

138-
139-
/*
140-
* Initialize the editor.
141-
*/
142-
143-
$align_wide = get_theme_support( 'align-wide' );
144-
$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
145-
$font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
146-
$gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
147-
$custom_line_height = get_theme_support( 'custom-line-height' );
148-
$custom_units = get_theme_support( 'custom-units' );
149-
$custom_spacing = get_theme_support( 'custom-spacing' );
150-
151-
/**
152-
* Filters the allowed block types for the editor, defaulting to true (all
153-
* block types supported).
154-
*
155-
* @since 5.0.0
156-
*
157-
* @param bool|array $allowed_block_types Array of block type slugs, or
158-
* boolean to enable/disable all.
159-
* @param WP_Post $post The post resource data.
160-
*/
161-
$allowed_block_types = apply_filters( 'allowed_block_types', true, $post );
162-
163141
/*
164142
* Get all available templates for the post/page attributes meta-box.
165143
* The "Default template" array element should only be added if the array is
@@ -175,12 +153,6 @@
175153
$available_templates
176154
) : $available_templates;
177155

178-
// Media settings.
179-
$max_upload_size = wp_max_upload_size();
180-
if ( ! $max_upload_size ) {
181-
$max_upload_size = 0;
182-
}
183-
184156
// Editor Styles.
185157
$styles = array(
186158
array(
@@ -208,39 +180,6 @@
208180
}
209181
}
210182

211-
// Image sizes.
212-
213-
/** This filter is documented in wp-admin/includes/media.php */
214-
$image_size_names = apply_filters(
215-
'image_size_names_choose',
216-
array(
217-
'thumbnail' => __( 'Thumbnail' ),
218-
'medium' => __( 'Medium' ),
219-
'large' => __( 'Large' ),
220-
'full' => __( 'Full Size' ),
221-
)
222-
);
223-
224-
$available_image_sizes = array();
225-
foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
226-
$available_image_sizes[] = array(
227-
'slug' => $image_size_slug,
228-
'name' => $image_size_name,
229-
);
230-
}
231-
232-
$default_size = get_option( 'image_default_size', 'large' );
233-
$image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $image_default_size : 'large';
234-
235-
$image_dimensions = array();
236-
$all_sizes = wp_get_registered_image_subsizes();
237-
foreach ( $available_image_sizes as $size ) {
238-
$key = $size['slug'];
239-
if ( isset( $all_sizes[ $key ] ) ) {
240-
$image_dimensions[ $key ] = $all_sizes[ $key ];
241-
}
242-
}
243-
244183
// Lock settings.
245184
$user_id = wp_check_post_lock( $post->ID );
246185
if ( $user_id ) {
@@ -289,24 +228,13 @@
289228
$body_placeholder = apply_filters( 'write_your_story', __( 'Type / to choose a block' ), $post );
290229

291230
$editor_settings = array(
292-
'alignWide' => $align_wide,
293231
'availableTemplates' => $available_templates,
294-
'allowedBlockTypes' => $allowed_block_types,
295-
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
296-
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
297-
'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
298232
'disablePostFormats' => ! current_theme_supports( 'post-formats' ),
299233
/** This filter is documented in wp-admin/edit-form-advanced.php */
300234
'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
301235
'bodyPlaceholder' => $body_placeholder,
302-
'isRTL' => is_rtl(),
303236
'autosaveInterval' => AUTOSAVE_INTERVAL,
304-
'maxUploadFileSize' => $max_upload_size,
305-
'allowedMimeTypes' => get_allowed_mime_types(),
306237
'styles' => $styles,
307-
'imageSizes' => $available_image_sizes,
308-
'imageDefaultSize' => $image_default_size,
309-
'imageDimensions' => $image_dimensions,
310238
'richEditingEnabled' => user_can_richedit(),
311239
'postLock' => $lock_details,
312240
'postLockUtils' => array(
@@ -320,9 +248,6 @@
320248
// Whether or not to load the 'postcustom' meta box is stored as a user meta
321249
// field so that we're not always loading its assets.
322250
'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
323-
'enableCustomLineHeight' => $custom_line_height,
324-
'enableCustomUnits' => $custom_units,
325-
'enableCustomSpacing' => $custom_spacing,
326251
);
327252

328253
$autosave = wp_get_post_autosave( $post->ID );
@@ -336,18 +261,6 @@
336261
}
337262
}
338263

339-
if ( false !== $color_palette ) {
340-
$editor_settings['colors'] = $color_palette;
341-
}
342-
343-
if ( false !== $font_sizes ) {
344-
$editor_settings['fontSizes'] = $font_sizes;
345-
}
346-
347-
if ( false !== $gradient_presets ) {
348-
$editor_settings['gradients'] = $gradient_presets;
349-
}
350-
351264
if ( ! empty( $post_type_object->template ) ) {
352265
$editor_settings['template'] = $post_type_object->template;
353266
$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
@@ -372,7 +285,6 @@
372285
wp_tinymce_inline_scripts();
373286
wp_enqueue_editor();
374287

375-
376288
/**
377289
* Styles
378290
*/
@@ -400,15 +312,7 @@
400312
unset( $editor_settings['enableCustomFields'] );
401313
}
402314

403-
/**
404-
* Filters the settings to pass to the block editor.
405-
*
406-
* @since 5.0.0
407-
*
408-
* @param array $editor_settings Default editor settings.
409-
* @param WP_Post $post Post being edited.
410-
*/
411-
$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
315+
$editor_settings = get_block_editor_settings( $editor_name, $editor_settings );
412316

413317
$init_script = <<<JS
414318
( function() {

src/wp-admin/includes/post.php

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,64 +2180,6 @@ function use_block_editor_for_post_type( $post_type ) {
21802180
return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
21812181
}
21822182

2183-
/**
2184-
* Returns all the block categories that will be shown in the block editor.
2185-
*
2186-
* @since 5.0.0
2187-
*
2188-
* @param WP_Post $post Post object.
2189-
* @return array[] Array of block categories.
2190-
*/
2191-
function get_block_categories( $post ) {
2192-
$default_categories = array(
2193-
array(
2194-
'slug' => 'text',
2195-
'title' => _x( 'Text', 'block category' ),
2196-
'icon' => null,
2197-
),
2198-
array(
2199-
'slug' => 'media',
2200-
'title' => _x( 'Media', 'block category' ),
2201-
'icon' => null,
2202-
),
2203-
array(
2204-
'slug' => 'design',
2205-
'title' => _x( 'Design', 'block category' ),
2206-
'icon' => null,
2207-
),
2208-
array(
2209-
'slug' => 'widgets',
2210-
'title' => _x( 'Widgets', 'block category' ),
2211-
'icon' => null,
2212-
),
2213-
array(
2214-
'slug' => 'theme',
2215-
'title' => _x( 'Theme', 'block category' ),
2216-
'icon' => null,
2217-
),
2218-
array(
2219-
'slug' => 'embed',
2220-
'title' => _x( 'Embeds', 'block category' ),
2221-
'icon' => null,
2222-
),
2223-
array(
2224-
'slug' => 'reusable',
2225-
'title' => _x( 'Reusable Blocks', 'block category' ),
2226-
'icon' => null,
2227-
),
2228-
);
2229-
2230-
/**
2231-
* Filters the default array of block categories.
2232-
*
2233-
* @since 5.0.0
2234-
*
2235-
* @param array[] $default_categories Array of block categories.
2236-
* @param WP_Post $post Post being loaded.
2237-
*/
2238-
return apply_filters( 'block_categories', $default_categories, $post );
2239-
}
2240-
22412183
/**
22422184
* Prepares server-registered blocks for the block editor.
22432185
*

0 commit comments

Comments
 (0)