Make WordPress Core


Ignore:
Timestamp:
05/24/2021 07:31:49 AM (3 years ago)
Author:
gziolo
Message:

Editor: Extract block_editor_rest_api_preload method for use with different editor screens

It is going to be used on the new widgets editor screen. This patch also introduced a new class WP_Block_Editor_Context that is going to be used with revised block editor filters to let extenders to keep their existing behavior. It should also allow to provide more settings through the context class as new screens get introduced like the navigation editor.

Props azaozz, chrisvanpatten, timothyblynjacobs, youknowriad.
Fixes #52920.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/block-editor.php

    r50920 r50956  
    6666    /**
    6767     * @ticket 52920
     68     */
     69    function test_block_editor_context_no_settings() {
     70        $context = new WP_Block_Editor_Context();
     71
     72        $this->assertNull( $context->post );
     73    }
     74
     75    /**
     76     * @ticket 52920
     77     */
     78    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 );
     82    }
     83
     84    /**
     85     * @ticket 52920
    6886     * @expectedDeprecated block_categories
    6987     */
     
    300318        );
    301319    }
     320
     321    /**
     322     * @ticket 52920
     323     */
     324    function test_block_editor_rest_api_preload_no_paths() {
     325        $context = new WP_Block_Editor_Context();
     326        block_editor_rest_api_preload( array(), $context );
     327
     328        $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
     329        $this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
     330    }
     331
     332    /**
     333     * @ticket 52920
     334     * @expectedDeprecated block_editor_preload_paths
     335     */
     336    function test_block_editor_rest_api_preload_deprecated_filter_post_editor() {
     337        function filter_remove_preload_paths( $preload_paths, $post ) {
     338            if ( empty( $post ) ) {
     339                return $preload_paths;
     340            }
     341            return array();
     342        }
     343        add_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths', 10, 2 );
     344
     345        $context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
     346        block_editor_rest_api_preload(
     347            array(
     348                array( '/wp/v2/blocks', 'OPTIONS' ),
     349            ),
     350            $context
     351        );
     352
     353        remove_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths' );
     354
     355        $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
     356        $this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
     357    }
     358
     359    /**
     360     * @ticket 52920
     361     */
     362    function test_block_editor_rest_api_preload_filter_all() {
     363        function filter_add_preload_paths( $preload_paths, WP_Block_Editor_Context $context ) {
     364            if ( empty( $context->post ) ) {
     365                array_push( $preload_paths, array( '/wp/v2/types', 'OPTIONS' ) );
     366            }
     367
     368            return $preload_paths;
     369        }
     370        add_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths', 10, 2 );
     371
     372        $context = new WP_Block_Editor_Context();
     373        block_editor_rest_api_preload(
     374            array(
     375                array( '/wp/v2/blocks', 'OPTIONS' ),
     376            ),
     377            $context
     378        );
     379
     380        remove_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths' );
     381
     382        $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
     383        $this->assertContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
     384        $this->assertContains( '"\/wp\/v2\/blocks"', $after );
     385        $this->assertContains( '"\/wp\/v2\/types"', $after );
     386    }
    302387}
Note: See TracChangeset for help on using the changeset viewer.