Skip to content

Commit

Permalink
Editor: Abstract block editor configuration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gziolo committed Apr 21, 2021
1 parent bf63735 commit a19f589
Show file tree
Hide file tree
Showing 5 changed files with 584 additions and 158 deletions.
104 changes: 4 additions & 100 deletions src/wp-admin/edit-form-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;

$editor_name = 'post-editor';

// Flag that we're loading the block editor.
$current_screen = get_current_screen();
$current_screen->is_block_editor( true );
Expand Down Expand Up @@ -56,6 +58,7 @@
sprintf( '/wp/v2/%s/%d/autosaves?context=edit', $rest_base, $post->ID ),
);


/**
* Preload common data by specifying an array of REST API paths that will be preloaded.
*
Expand Down Expand Up @@ -135,31 +138,6 @@
'before'
);


/*
* Initialize the editor.
*/

$align_wide = get_theme_support( 'align-wide' );
$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
$font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
$gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
$custom_line_height = get_theme_support( 'custom-line-height' );
$custom_units = get_theme_support( 'custom-units' );
$custom_spacing = get_theme_support( 'custom-spacing' );

/**
* Filters the allowed block types for the editor, defaulting to true (all
* block types supported).
*
* @since 5.0.0
*
* @param bool|array $allowed_block_types Array of block type slugs, or
* boolean to enable/disable all.
* @param WP_Post $post The post resource data.
*/
$allowed_block_types = apply_filters( 'allowed_block_types', true, $post );

/*
* Get all available templates for the post/page attributes meta-box.
* The "Default template" array element should only be added if the array is
Expand All @@ -175,12 +153,6 @@
$available_templates
) : $available_templates;

// Media settings.
$max_upload_size = wp_max_upload_size();
if ( ! $max_upload_size ) {
$max_upload_size = 0;
}

// Editor Styles.
$styles = array(
array(
Expand Down Expand Up @@ -208,39 +180,6 @@
}
}

// Image sizes.

/** This filter is documented in wp-admin/includes/media.php */
$image_size_names = apply_filters(
'image_size_names_choose',
array(
'thumbnail' => __( 'Thumbnail' ),
'medium' => __( 'Medium' ),
'large' => __( 'Large' ),
'full' => __( 'Full Size' ),
)
);

$available_image_sizes = array();
foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
$available_image_sizes[] = array(
'slug' => $image_size_slug,
'name' => $image_size_name,
);
}

$default_size = get_option( 'image_default_size', 'large' );
$image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $image_default_size : 'large';

$image_dimensions = array();
$all_sizes = wp_get_registered_image_subsizes();
foreach ( $available_image_sizes as $size ) {
$key = $size['slug'];
if ( isset( $all_sizes[ $key ] ) ) {
$image_dimensions[ $key ] = $all_sizes[ $key ];
}
}

// Lock settings.
$user_id = wp_check_post_lock( $post->ID );
if ( $user_id ) {
Expand Down Expand Up @@ -289,24 +228,13 @@
$body_placeholder = apply_filters( 'write_your_story', __( 'Type / to choose a block' ), $post );

$editor_settings = array(
'alignWide' => $align_wide,
'availableTemplates' => $available_templates,
'allowedBlockTypes' => $allowed_block_types,
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
'disablePostFormats' => ! current_theme_supports( 'post-formats' ),
/** This filter is documented in wp-admin/edit-form-advanced.php */
'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
'bodyPlaceholder' => $body_placeholder,
'isRTL' => is_rtl(),
'autosaveInterval' => AUTOSAVE_INTERVAL,
'maxUploadFileSize' => $max_upload_size,
'allowedMimeTypes' => get_allowed_mime_types(),
'styles' => $styles,
'imageSizes' => $available_image_sizes,
'imageDefaultSize' => $image_default_size,
'imageDimensions' => $image_dimensions,
'richEditingEnabled' => user_can_richedit(),
'postLock' => $lock_details,
'postLockUtils' => array(
Expand All @@ -320,9 +248,6 @@
// Whether or not to load the 'postcustom' meta box is stored as a user meta
// field so that we're not always loading its assets.
'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
'enableCustomLineHeight' => $custom_line_height,
'enableCustomUnits' => $custom_units,
'enableCustomSpacing' => $custom_spacing,
);

$autosave = wp_get_post_autosave( $post->ID );
Expand All @@ -336,18 +261,6 @@
}
}

if ( false !== $color_palette ) {
$editor_settings['colors'] = $color_palette;
}

if ( false !== $font_sizes ) {
$editor_settings['fontSizes'] = $font_sizes;
}

if ( false !== $gradient_presets ) {
$editor_settings['gradients'] = $gradient_presets;
}

if ( ! empty( $post_type_object->template ) ) {
$editor_settings['template'] = $post_type_object->template;
$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
Expand All @@ -372,7 +285,6 @@
wp_tinymce_inline_scripts();
wp_enqueue_editor();


/**
* Styles
*/
Expand Down Expand Up @@ -400,15 +312,7 @@
unset( $editor_settings['enableCustomFields'] );
}

/**
* Filters the settings to pass to the block editor.
*
* @since 5.0.0
*
* @param array $editor_settings Default editor settings.
* @param WP_Post $post Post being edited.
*/
$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
$editor_settings = get_block_editor_settings( $editor_name, $editor_settings );

$init_script = <<<JS
( function() {
Expand Down
58 changes: 0 additions & 58 deletions src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2180,64 +2180,6 @@ function use_block_editor_for_post_type( $post_type ) {
return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
}

/**
* Returns all the block categories that will be shown in the block editor.
*
* @since 5.0.0
*
* @param WP_Post $post Post object.
* @return array[] Array of block categories.
*/
function get_block_categories( $post ) {
$default_categories = array(
array(
'slug' => 'text',
'title' => _x( 'Text', 'block category' ),
'icon' => null,
),
array(
'slug' => 'media',
'title' => _x( 'Media', 'block category' ),
'icon' => null,
),
array(
'slug' => 'design',
'title' => _x( 'Design', 'block category' ),
'icon' => null,
),
array(
'slug' => 'widgets',
'title' => _x( 'Widgets', 'block category' ),
'icon' => null,
),
array(
'slug' => 'theme',
'title' => _x( 'Theme', 'block category' ),
'icon' => null,
),
array(
'slug' => 'embed',
'title' => _x( 'Embeds', 'block category' ),
'icon' => null,
),
array(
'slug' => 'reusable',
'title' => _x( 'Reusable Blocks', 'block category' ),
'icon' => null,
),
);

/**
* Filters the default array of block categories.
*
* @since 5.0.0
*
* @param array[] $default_categories Array of block categories.
* @param WP_Post $post Post being loaded.
*/
return apply_filters( 'block_categories', $default_categories, $post );
}

/**
* Prepares server-registered blocks for the block editor.
*
Expand Down
Loading

0 comments on commit a19f589

Please sign in to comment.