Make WordPress Core


Ignore:
Timestamp:
04/17/2019 01:55:21 AM (6 years ago)
Author:
desrosj
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-screen.php

    r44574 r45224  
    203203        }
    204204
    205         $post_type = $taxonomy = null;
    206         $in_admin  = false;
    207         $action    = '';
     205        $post_type       = $taxonomy = null;
     206        $in_admin        = false;
     207        $action          = '';
     208        $is_block_editor = false;
    208209
    209210        if ( $hook_name ) {
     
    295296                        if ( $post ) {
    296297                            $post_type = $post->post_type;
     298
     299                            /** This filter is documented in wp-admin/post.php */
     300                            $replace_editor = apply_filters( 'replace_editor', false, $post );
     301
     302                            if ( ! $replace_editor ) {
     303                                $is_block_editor = use_block_editor_for_post( $post );
     304                            }
    297305                        }
    298306                    }
     
    315323                    $post_type = 'post';
    316324                }
     325
     326                // When creating a new post, use the default block editor support value for the post type.
     327                if ( empty( $post_id ) ) {
     328                    $is_block_editor = use_block_editor_for_post_type( $post_type );
     329                }
     330
    317331                $id = $post_type;
    318332                break;
     
    358372        }
    359373
    360         $screen->base       = $base;
    361         $screen->action     = $action;
    362         $screen->post_type  = (string) $post_type;
    363         $screen->taxonomy   = (string) $taxonomy;
    364         $screen->is_user    = ( 'user' == $in_admin );
    365         $screen->is_network = ( 'network' == $in_admin );
    366         $screen->in_admin   = $in_admin;
     374        $screen->base            = $base;
     375        $screen->action          = $action;
     376        $screen->post_type       = (string) $post_type;
     377        $screen->taxonomy        = (string) $taxonomy;
     378        $screen->is_user         = ( 'user' == $in_admin );
     379        $screen->is_network      = ( 'network' == $in_admin );
     380        $screen->in_admin        = $in_admin;
     381        $screen->is_block_editor = $is_block_editor;
    367382
    368383        self::$_registry[ $id ] = $screen;
Note: See TracChangeset for help on using the changeset viewer.