Make WordPress Core

Changeset 45224


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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/edit-form-advanced.php

    r45140 r45224  
    1818 */
    1919global $post_type, $post_type_object, $post;
     20
     21// Flag that we're not loading the block editor.
     22$current_screen = get_current_screen();
     23$current_screen->is_block_editor( false );
    2024
    2125if ( is_multisite() ) {
  • 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;
  • trunk/src/wp-admin/post-new.php

    r44165 r45224  
    7575        include( ABSPATH . 'wp-admin/edit-form-advanced.php' );
    7676    }
     77} else {
     78    // Flag that we're not loading the block editor.
     79    $current_screen = get_current_screen();
     80    $current_screen->is_block_editor( false );
    7781}
    7882
  • trunk/tests/phpunit/tests/admin/includesScreen.php

    r44577 r45224  
    88    var $core_screens = array(
    99        'index.php'                            => array(
    10             'base' => 'dashboard',
    11             'id'   => 'dashboard',
     10            'base'            => 'dashboard',
     11            'id'              => 'dashboard',
     12            'is_block_editor' => false,
    1213        ),
    1314        'edit.php'                             => array(
    14             'base'      => 'edit',
    15             'id'        => 'edit-post',
    16             'post_type' => 'post',
     15            'base'            => 'edit',
     16            'id'              => 'edit-post',
     17            'post_type'       => 'post',
     18            'is_block_editor' => false,
    1719        ),
    1820        'post-new.php'                         => array(
    19             'action'    => 'add',
    20             'base'      => 'post',
    21             'id'        => 'post',
    22             'post_type' => 'post',
     21            'action'          => 'add',
     22            'base'            => 'post',
     23            'id'              => 'post',
     24            'post_type'       => 'post',
     25            'is_block_editor' => true,
     26        ),
     27        'post.php'                             => array(
     28            'base'            => 'post',
     29            'id'              => 'post',
     30            'post_type'       => 'post',
     31            'is_block_editor' => true,
    2332        ),
    2433        'edit-tags.php'                        => array(
    25             'base'      => 'edit-tags',
    26             'id'        => 'edit-post_tag',
    27             'post_type' => 'post',
    28             'taxonomy'  => 'post_tag',
     34            'base'            => 'edit-tags',
     35            'id'              => 'edit-post_tag',
     36            'post_type'       => 'post',
     37            'taxonomy'        => 'post_tag',
     38            'is_block_editor' => false,
    2939        ),
    3040        'edit-tags.php?taxonomy=post_tag'      => array(
    31             'base'      => 'edit-tags',
    32             'id'        => 'edit-post_tag',
    33             'post_type' => 'post',
    34             'taxonomy'  => 'post_tag',
     41            'base'            => 'edit-tags',
     42            'id'              => 'edit-post_tag',
     43            'post_type'       => 'post',
     44            'taxonomy'        => 'post_tag',
     45            'is_block_editor' => false,
    3546        ),
    3647        'edit-tags.php?taxonomy=category'      => array(
    37             'base'      => 'edit-tags',
    38             'id'        => 'edit-category',
    39             'post_type' => 'post',
    40             'taxonomy'  => 'category',
     48            'base'            => 'edit-tags',
     49            'id'              => 'edit-category',
     50            'post_type'       => 'post',
     51            'taxonomy'        => 'category',
     52            'is_block_editor' => false,
    4153        ),
    4254        'upload.php'                           => array(
    43             'base'      => 'upload',
    44             'id'        => 'upload',
    45             'post_type' => 'attachment',
     55            'base'            => 'upload',
     56            'id'              => 'upload',
     57            'post_type'       => 'attachment',
     58            'is_block_editor' => false,
    4659        ),
    4760        'media-new.php'                        => array(
    48             'action' => 'add',
    49             'base'   => 'media',
    50             'id'     => 'media',
     61            'action'          => 'add',
     62            'base'            => 'media',
     63            'id'              => 'media',
     64            'is_block_editor' => false,
    5165        ),
    5266        'edit.php?post_type=page'              => array(
    53             'base'      => 'edit',
    54             'id'        => 'edit-page',
    55             'post_type' => 'page',
     67            'base'            => 'edit',
     68            'id'              => 'edit-page',
     69            'post_type'       => 'page',
     70            'is_block_editor' => false,
    5671        ),
    5772        'link-manager.php'                     => array(
    58             'base' => 'link-manager',
    59             'id'   => 'link-manager',
     73            'base'            => 'link-manager',
     74            'id'              => 'link-manager',
     75            'is_block_editor' => false,
    6076        ),
    6177        'link-add.php'                         => array(
    62             'action' => 'add',
    63             'base'   => 'link',
    64             'id'     => 'link',
     78            'action'          => 'add',
     79            'base'            => 'link',
     80            'id'              => 'link',
     81            'is_block_editor' => false,
    6582        ),
    6683        'edit-tags.php?taxonomy=link_category' => array(
    67             'base'      => 'edit-tags',
    68             'id'        => 'edit-link_category',
    69             'taxonomy'  => 'link_category',
    70             'post_type' => '',
     84            'base'            => 'edit-tags',
     85            'id'              => 'edit-link_category',
     86            'taxonomy'        => 'link_category',
     87            'post_type'       => '',
     88            'is_block_editor' => false,
    7189        ),
    7290        'edit-comments.php'                    => array(
    73             'base' => 'edit-comments',
    74             'id'   => 'edit-comments',
     91            'base'            => 'edit-comments',
     92            'id'              => 'edit-comments',
     93            'is_block_editor' => false,
    7594        ),
    7695        'themes.php'                           => array(
    77             'base' => 'themes',
    78             'id'   => 'themes',
     96            'base'            => 'themes',
     97            'id'              => 'themes',
     98            'is_block_editor' => false,
    7999        ),
    80100        'widgets.php'                          => array(
    81             'base' => 'widgets',
    82             'id'   => 'widgets',
     101            'base'            => 'widgets',
     102            'id'              => 'widgets',
     103            'is_block_editor' => false,
    83104        ),
    84105        'nav-menus.php'                        => array(
    85             'base' => 'nav-menus',
    86             'id'   => 'nav-menus',
     106            'base'            => 'nav-menus',
     107            'id'              => 'nav-menus',
     108            'is_block_editor' => false,
    87109        ),
    88110        'plugins.php'                          => array(
    89             'base' => 'plugins',
    90             'id'   => 'plugins',
     111            'base'            => 'plugins',
     112            'id'              => 'plugins',
     113            'is_block_editor' => false,
    91114        ),
    92115        'users.php'                            => array(
    93             'base' => 'users',
    94             'id'   => 'users',
     116            'base'            => 'users',
     117            'id'              => 'users',
     118            'is_block_editor' => false,
    95119        ),
    96120        'user-new.php'                         => array(
    97             'action' => 'add',
    98             'base'   => 'user',
    99             'id'     => 'user',
     121            'action'          => 'add',
     122            'base'            => 'user',
     123            'id'              => 'user',
     124            'is_block_editor' => false,
    100125        ),
    101126        'profile.php'                          => array(
    102             'base' => 'profile',
    103             'id'   => 'profile',
     127            'base'            => 'profile',
     128            'id'              => 'profile',
     129            'is_block_editor' => false,
    104130        ),
    105131        'tools.php'                            => array(
    106             'base' => 'tools',
    107             'id'   => 'tools',
     132            'base'            => 'tools',
     133            'id'              => 'tools',
     134            'is_block_editor' => false,
    108135        ),
    109136        'import.php'                           => array(
    110             'base' => 'import',
    111             'id'   => 'import',
     137            'base'            => 'import',
     138            'id'              => 'import',
     139            'is_block_editor' => false,
    112140        ),
    113141        'export.php'                           => array(
    114             'base' => 'export',
    115             'id'   => 'export',
     142            'base'            => 'export',
     143            'id'              => 'export',
     144            'is_block_editor' => false,
    116145        ),
    117146        'options-general.php'                  => array(
    118             'base' => 'options-general',
    119             'id'   => 'options-general',
     147            'base'            => 'options-general',
     148            'id'              => 'options-general',
     149            'is_block_editor' => false,
    120150        ),
    121151        'options-writing.php'                  => array(
    122             'base' => 'options-writing',
    123             'id'   => 'options-writing',
     152            'base'            => 'options-writing',
     153            'id'              => 'options-writing',
     154            'is_block_editor' => false,
    124155        ),
    125156    );
     
    133164        unset( $GLOBALS['wp_taxonomies']['old-or-new'] );
    134165        unset( $GLOBALS['screen'] );
     166        unset( $GLOBALS['current_screen'] );
    135167        parent::tearDown();
    136168    }
     
    179211            $this->assertFalse( $current_screen->in_admin( 'user' ) );
    180212            $this->assertFalse( $current_screen->in_admin( 'garbage' ) );
     213            $this->assertSame( $screen->is_block_editor, $current_screen->is_block_editor );
    181214
    182215            // With convert_to_screen(), the same ID should return the exact $current_screen.
     
    196229        $this->assertEquals( $screen->base, 'post' );
    197230        $this->assertEquals( $screen->id, 'page' );
     231        $this->assertTrue( $screen->is_block_editor );
    198232    }
    199233
     
    204238        $this->assertEquals( $screen->base, 'post' );
    205239        $this->assertEquals( $screen->id, 'value-add' );
     240        $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default.
    206241
    207242        $screen = convert_to_screen( 'edit-value-add' ); // the -add part is key.
     
    209244        $this->assertEquals( $screen->base, 'edit' );
    210245        $this->assertEquals( $screen->id, 'edit-value-add' );
     246        $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default.
    211247    }
    212248
     
    217253        $this->assertEquals( $screen->base, 'edit-tags' );
    218254        $this->assertEquals( $screen->id, 'edit-old-or-new' );
     255        $this->assertFalse( $screen->is_block_editor );
    219256    }
    220257
     
    225262        $this->assertEquals( $screen->base, 'post' );
    226263        $this->assertEquals( $screen->id, 'edit-some-thing' );
     264        $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default.
    227265
    228266        $screen = convert_to_screen( 'edit-edit-some-thing' );
     
    230268        $this->assertEquals( $screen->base, 'edit' );
    231269        $this->assertEquals( $screen->id, 'edit-edit-some-thing' );
     270        $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default.
    232271    }
    233272
     
    434473        $GLOBALS['current_screen'] = $screen;
    435474    }
     475
     476    /**
     477     * Sets up a method for testing is_block_editor for a custom post type.
     478     *
     479     * @since 5.2.0
     480     *
     481     * @param string $hook Admin page hook.
     482     * @return WP_Screen Screen object.
     483     */
     484    public function setup_block_editor_test( $hook = 'post.php' ) {
     485        register_post_type( 'type_shows_in_rest', array( 'show_in_rest' => true ) );
     486
     487        $GLOBALS['typenow']     = $_GET['post_type'] = $_POST['post_type'] = $_REQUEST['post_type'] = 'type_shows_in_rest';
     488        $GLOBALS['hook_suffix'] = $hook;
     489
     490        if ( 'post.php' === $hook ) {
     491            $post_id      = $this->factory->post->create(
     492                array(
     493                    'post_type' => 'type_shows_in_rest',
     494                )
     495            );
     496            $_GET['post'] = $post_id;
     497        }
     498
     499        set_current_screen();
     500
     501        return get_current_screen();
     502    }
     503
     504    /**
     505     * Data provider for testing is_block_editor.
     506     */
     507    public function data_is_block_editor() {
     508        return array(
     509            array(
     510                // Edit post: Post type supports `show_in_rest`, no filters.
     511                'hook'     => 'post.php',
     512                'filter'   => array(),
     513                'expected' => true,
     514            ),
     515            array(
     516                // Edit post: Support is disabled using post specific filter.
     517                'hook'     => 'post.php',
     518                'filter'   => array(
     519                    'name'     => 'use_block_editor_for_post',
     520                    'function' => '__return_false',
     521                ),
     522                'expected' => false,
     523            ),
     524            array(
     525                // Edit post: Support is disabled using post type specific filter.
     526                'hook'     => 'post.php',
     527                'filter'   => array(
     528                    'name'     => 'use_block_editor_for_post_type',
     529                    'function' => '__return_false',
     530                ),
     531                'expected' => false,
     532            ),
     533            array(
     534                // Edit post: Support is disabled using global replace filter.
     535                'hook'     => 'post.php',
     536                'filter'   => array(
     537                    'name'     => 'replace_editor',
     538                    'function' => '__return_true',
     539                ),
     540                'expected' => false,
     541            ),
     542            array(
     543                // Create post: Post type supports `show_in_rest`, no filters.
     544                'hook'     => 'post-new.php',
     545                'filter'   => array(),
     546                'expected' => true,
     547            ),
     548            array(
     549                // Create post: Support is disabled using post type specific filter.
     550                'hook'     => 'post-new.php',
     551                'filter'   => array(
     552                    'name'     => 'use_block_editor_for_post_type',
     553                    'function' => '__return_false',
     554                ),
     555                'expected' => false,
     556            ),
     557
     558            array(
     559                // Create post: Support is not immediately disabled using post specific filter.
     560                'hook'     => 'post-new.php',
     561                'filter'   => array(
     562                    'name'     => 'use_block_editor_for_post',
     563                    'function' => '__return_false',
     564                ),
     565                'expected' => true,
     566            ),
     567
     568            array(
     569                // Create post: Support is not immediately disabled using global replace filter.
     570                'hook'     => 'post-new.php',
     571                'filter'   => array(
     572                    'name'     => 'replace_editor',
     573                    'function' => '__return_true',
     574                ),
     575                'expected' => true,
     576            ),
     577        );
     578    }
     579
     580    /**
     581     * When editing a post type with `show_in_rest` support, the is_block_editor should indicate support.
     582     *
     583     * @ticket 46195
     584     * @dataProvider data_is_block_editor
     585     *
     586     * @param string $hook Admin hook.
     587     * @param array  $filter {
     588     *     Optional. Filter name and function to hook.
     589     *
     590     *     $name     string Filter name to hook a function.
     591     *     $function string Function name to hook to the filter.
     592     * }
     593     * @param bool   $expected The expected `is_block_editor` value.
     594     */
     595    public function test_is_block_editor( $hook, $filter, $expected ) {
     596        if ( ! empty( $filter['name'] ) && ! empty( $filter['function'] ) ) {
     597            add_filter( $filter['name'], $filter['function'] );
     598        }
     599
     600        $screen = $this->setup_block_editor_test( $hook );
     601
     602        $this->assertSame( 'post', $screen->base );
     603        $this->assertSame( 'type_shows_in_rest', $screen->post_type );
     604
     605        if ( 'post.php' === $hook ) {
     606            $this->assertEmpty( $screen->action );
     607        } else {
     608            $this->assertSame( 'add', $screen->action );
     609        }
     610
     611        $this->assertSame( $expected, $screen->is_block_editor );
     612    }
    436613}
Note: See TracChangeset for help on using the changeset viewer.