Administration: Improve the accuracy of is_block_editor().

Currently, there are a number of scenarios where `is_block_editor()` (and `WP_Screen::is_block_editor`) would incorrectly indicate block editor support at different points of the loading process. Most notably, checking `is_block_editor` when hooking into the `current_screen` action will always result in `false`, even when the block editor is being loaded. This is because `is_block_editor` is not set to `true` until `edit-form-blocks.php` is included.

This change adds logic to `WP_Screen` to ensure the accuracy of `is_block_editor` on block editor pages earlier in the load process.

While edit screens will now be accurate 100% of the time from `current_screen` on, there are still a few edge cases where `is_block_editor` could contain an incorrect value when creating a new post.

Because a `WP_Post` object is a required parameter for the `replace_editor` filter and `use_block_editor_for_post()` function, `WP_Screen` will fall back to the value returned by `use_block_editor_for_post_type()` for the post being created. To eliminate these edge cases, the `use_block_editor_for_post_type` filter can be used to return the appropriate boolean value to indicate support.

Props Chouby, desrosj, aduth, johnbillion.
Fixes #46195.
Built from https://develop.svn.wordpress.org/trunk@45224


git-svn-id: http://core.svn.wordpress.org/trunk@45033 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
desrosj 2019-04-17 01:56:52 +00:00
parent 893be2cf11
commit f807a098b1
4 changed files with 34 additions and 11 deletions

View File

@ -18,6 +18,10 @@ if ( ! defined( 'ABSPATH' ) ) {
*/ */
global $post_type, $post_type_object, $post; global $post_type, $post_type_object, $post;
// Flag that we're not loading the block editor.
$current_screen = get_current_screen();
$current_screen->is_block_editor( false );
if ( is_multisite() ) { if ( is_multisite() ) {
add_action( 'admin_footer', '_admin_notice_post_locked' ); add_action( 'admin_footer', '_admin_notice_post_locked' );
} else { } else {

View File

@ -202,9 +202,10 @@ final class WP_Screen {
return $hook_name; return $hook_name;
} }
$post_type = $taxonomy = null; $post_type = $taxonomy = null;
$in_admin = false; $in_admin = false;
$action = ''; $action = '';
$is_block_editor = false;
if ( $hook_name ) { if ( $hook_name ) {
$id = $hook_name; $id = $hook_name;
@ -294,6 +295,13 @@ final class WP_Screen {
$post = get_post( $post_id ); $post = get_post( $post_id );
if ( $post ) { if ( $post ) {
$post_type = $post->post_type; $post_type = $post->post_type;
/** This filter is documented in wp-admin/post.php */
$replace_editor = apply_filters( 'replace_editor', false, $post );
if ( ! $replace_editor ) {
$is_block_editor = use_block_editor_for_post( $post );
}
} }
} }
break; break;
@ -314,6 +322,12 @@ final class WP_Screen {
if ( null === $post_type ) { if ( null === $post_type ) {
$post_type = 'post'; $post_type = 'post';
} }
// When creating a new post, use the default block editor support value for the post type.
if ( empty( $post_id ) ) {
$is_block_editor = use_block_editor_for_post_type( $post_type );
}
$id = $post_type; $id = $post_type;
break; break;
case 'edit': case 'edit':
@ -357,13 +371,14 @@ final class WP_Screen {
$screen->id = $id; $screen->id = $id;
} }
$screen->base = $base; $screen->base = $base;
$screen->action = $action; $screen->action = $action;
$screen->post_type = (string) $post_type; $screen->post_type = (string) $post_type;
$screen->taxonomy = (string) $taxonomy; $screen->taxonomy = (string) $taxonomy;
$screen->is_user = ( 'user' == $in_admin ); $screen->is_user = ( 'user' == $in_admin );
$screen->is_network = ( 'network' == $in_admin ); $screen->is_network = ( 'network' == $in_admin );
$screen->in_admin = $in_admin; $screen->in_admin = $in_admin;
$screen->is_block_editor = $is_block_editor;
self::$_registry[ $id ] = $screen; self::$_registry[ $id ] = $screen;

View File

@ -74,6 +74,10 @@ if ( apply_filters( 'replace_editor', false, $post ) !== true ) {
wp_enqueue_script( 'autosave' ); wp_enqueue_script( 'autosave' );
include( ABSPATH . 'wp-admin/edit-form-advanced.php' ); include( ABSPATH . 'wp-admin/edit-form-advanced.php' );
} }
} else {
// Flag that we're not loading the block editor.
$current_screen = get_current_screen();
$current_screen->is_block_editor( false );
} }
include( ABSPATH . 'wp-admin/admin-footer.php' ); include( ABSPATH . 'wp-admin/admin-footer.php' );

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.2-beta3-45223'; $wp_version = '5.2-beta3-45224';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.