WordPress.org

Make WordPress Core

Changeset 50983


Ignore:
Timestamp:
05/24/2021 09:20:31 PM (3 weeks ago)
Author:
gziolo
Message:

Editor: Use the block editor context in filters that used the editor name

Follow-up for [50956].
Props azaozz, chrisvanpatten, timothyblynjacobs, youknowriad.
Fixes #52920.

Location:
trunk
Files:
3 edited

Legend:

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

    r50956 r50983  
    2424global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
    2525
    26 $editor_name          = 'post-editor';
    2726$block_editor_context = new WP_Block_Editor_Context( array( 'post' => $post ) );
    2827
     
    281280}
    282281
    283 $editor_settings = get_block_editor_settings( $editor_name, $editor_settings );
     282$editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );
    284283
    285284$init_script = <<<JS
  • trunk/src/wp-includes/block-editor.php

    r50973 r50983  
    5959 *
    6060 * @since 5.0.0
    61  *
    62  * @param string|WP_Post $editor_name_or_post The name of the editor (e.g. 'post-editor')
    63  *                                            or the post object.
     61 * @since 5.8.0 It is possible to pass the block editor context as param.
     62 *
     63 * @param WP_Post|WP_Block_Editor_Context $post_or_block_editor_context The current post object or
     64 *                                                                      the block editor context.
    6465 *
    6566 * @return array[] Array of categories for block types.
    6667 */
    67 function get_block_categories( $editor_name_or_post ) {
    68     // Assume the post editor when the WP_Post object passed.
    69     $editor_name      = is_object( $editor_name_or_post ) ? 'post-editor' : $editor_name_or_post;
    70     $block_categories = get_default_block_categories();
     68function get_block_categories( $post_or_block_editor_context ) {
     69    $block_categories     = get_default_block_categories();
     70    $block_editor_context = $post_or_block_editor_context instanceof WP_Post ?
     71        new WP_Block_Editor_Context(
     72            array(
     73                'post' => $post_or_block_editor_context,
     74            )
     75        ) : $post_or_block_editor_context;
    7176
    7277    /**
     
    7580     * @since 5.8.0
    7681     *
    77      * @param array[] $block_categories Array of categories for block types.
    78      * @param string  $editor_name      The name of the editor, e.g. 'post-editor'.
     82     * @param array[]                 $block_categories    Array of categories for block types.
     83     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    7984     */
    80     $block_categories = apply_filters( 'block_categories_all', $block_categories, $editor_name );
    81     if ( 'post-editor' === $editor_name ) {
    82         $post = is_object( $editor_name_or_post ) ? $editor_name_or_post : get_post();
     85    $block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
     86    if ( ! empty( $block_editor_context->post ) ) {
     87        $post = $block_editor_context->post;
    8388
    8489        /**
     
    102107 * @since 5.8.0
    103108 *
    104  * @param string $editor_name The name of the editor (e.g. 'post-editor').
     109 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    105110 *
    106111 * @return bool|array Array of block type slugs, or boolean to enable/disable all.
    107112 */
    108 function get_allowed_block_types( $editor_name ) {
     113function get_allowed_block_types( $block_editor_context ) {
    109114    $allowed_block_types = true;
    110115
     
    116121     * @since 5.8.0
    117122     *
    118      * @param bool|array $allowed_block_types Array of block type slugs, or
    119      *                                        boolean to enable/disable all.
    120      * @param string     $editor_name         The name of the editor, e.g. 'post-editor'.
     123     * @param bool|array              $allowed_block_types Array of block type slugs, or
     124     *                                                      boolean to enable/disable all.
     125     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    121126     */
    122     $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $editor_name );
    123     if ( 'post-editor' === $editor_name ) {
    124         $post = get_post();
     127    $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
     128    if ( ! empty( $block_editor_context->post ) ) {
     129        $post = $block_editor_context->post;
    125130
    126131        /**
     
    225230
    226231/**
    227  * Returns the contextualized block editor settings settings for a selected editor type.
     232 * Returns the contextualized block editor settings settings for a selected editor context.
    228233 *
    229234 * @since 5.8.0
    230235 *
    231  * @param string $editor_name     The name of the editor (e.g. 'post-editor').
    232  * @param array  $custom_settings Optional custom settings to use with the editor type.
     236 * @param array                   $custom_settings      Custom settings to use with the given editor type.
     237 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    233238 *
    234239 * @return array The contextualized block editor settings.
    235240 */
    236 function get_block_editor_settings( $editor_name, $custom_settings = array() ) {
     241function get_block_editor_settings( array $custom_settings, $block_editor_context ) {
    237242    $editor_settings = array_merge(
    238243        get_default_block_editor_settings(),
    239244        array(
    240             'allowedBlockTypes' => get_allowed_block_types( $editor_name ),
    241             'blockCategories'   => get_block_categories( $editor_name ),
     245            'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ),
     246            'blockCategories'   => get_block_categories( $block_editor_context ),
    242247        ),
    243248        $custom_settings
     
    302307     * @since 5.8.0
    303308     *
    304      * @param array  $editor_settings Default editor settings.
    305      * @param string $editor_name     The name of the editor, e.g. 'post-editor'.
     309     * @param array                   $editor_settings      Default editor settings.
     310     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    306311     */
    307     $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $editor_name );
    308     if ( 'post-editor' === $editor_name ) {
    309         $post = get_post();
     312    $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
     313    if ( ! empty( $block_editor_context->post ) ) {
     314        $post = $block_editor_context->post;
    310315
    311316        /**
  • trunk/tests/phpunit/tests/blocks/block-editor.php

    r50956 r50983  
    7777     */
    7878    function test_block_editor_context_post() {
    79         $context = new WP_Block_Editor_Context( array( 'post' => $this->post ) );
    80 
    81         $this->assertSame( $this->post, $context->post );
     79        $context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
     80
     81        $this->assertSame( get_post(), $context->post );
    8282    }
    8383
     
    112112        add_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ), 10, 2 );
    113113
    114         $block_categories = get_block_categories( 'post-editor' );
     114        $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
     115        $block_categories    = get_block_categories( $post_editor_context );
    115116
    116117        remove_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ) );
     
    132133     */
    133134    function test_get_allowed_block_types_default() {
    134         $allowed_block_types = get_allowed_block_types( 'post-editor' );
     135        $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
     136        $allowed_block_types = get_allowed_block_types( $post_editor_context );
    135137
    136138        $this->assertTrue( $allowed_block_types );
     
    144146        add_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ), 10, 2 );
    145147
    146         $allowed_block_types = get_allowed_block_types( 'post-editor' );
     148        $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
     149        $allowed_block_types = get_allowed_block_types( $post_editor_context );
    147150
    148151        remove_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ) );
     
    280283        add_filter( 'block_editor_settings_all', 'filter_block_editor_settings_my_editor', 10, 1 );
    281284
    282         $settings = get_block_editor_settings( 'my-editor' );
     285        $my_editor_context = new WP_Block_Editor_Context();
     286        $settings          = get_block_editor_settings( array(), $my_editor_context );
    283287
    284288        remove_filter( 'allowed_block_types_all', 'filter_allowed_block_types_my_editor' );
     
    307311        add_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ), 10, 2 );
    308312
    309         $settings = get_block_editor_settings( 'post-editor' );
     313        $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
     314        $settings            = get_block_editor_settings( array(), $post_editor_context );
    310315
    311316        remove_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ) );
     
    323328     */
    324329    function test_block_editor_rest_api_preload_no_paths() {
    325         $context = new WP_Block_Editor_Context();
    326         block_editor_rest_api_preload( array(), $context );
     330        $editor_context = new WP_Block_Editor_Context();
     331        block_editor_rest_api_preload( array(), $editor_context );
    327332
    328333        $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
     
    343348        add_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths', 10, 2 );
    344349
    345         $context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
     350        $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
    346351        block_editor_rest_api_preload(
    347352            array(
    348353                array( '/wp/v2/blocks', 'OPTIONS' ),
    349354            ),
    350             $context
     355            $post_editor_context
    351356        );
    352357
     
    370375        add_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths', 10, 2 );
    371376
    372         $context = new WP_Block_Editor_Context();
     377        $editor_context = new WP_Block_Editor_Context();
    373378        block_editor_rest_api_preload(
    374379            array(
    375380                array( '/wp/v2/blocks', 'OPTIONS' ),
    376381            ),
    377             $context
     382            $editor_context
    378383        );
    379384
Note: See TracChangeset for help on using the changeset viewer.