Make WordPress Core

Changeset 52312


Ignore:
Timestamp:
12/03/2021 03:28:21 PM (5 years ago)
Author:
audrasjb
Message:

Editor: Avoid undefined index notices in the Template Parts Editor.

This changes adds a leading slash when needed in the ?context=edit path to avoid an undefined index notice in the Template Parts Editor.

Follow-up to [52275].

Props kafleg, costdev, mukesh27, Boniu91.
Fixes #54558.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-editor.php

    r52275 r52312  
    468468         */
    469469        $backup_global_post = ! empty( $post ) ? clone $post : $post;
     470
     471        foreach ( $preload_paths as &$path ) {
     472                if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) {
     473                        $path = '/' . $path;
     474                        continue;
     475                }
     476
     477                if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) {
     478                                $path[0] = '/' . $path[0];
     479                }
     480        }
    470481
    471482        $preload_data = array_reduce(
  • trunk/tests/phpunit/tests/blocks/editor.php

    r52042 r52312  
    494494                $this->assertStringContainsString( '"\/wp\/v2\/types"', $after );
    495495        }
     496
     497        /**
     498         * @ticket 54558
     499         * @dataProvider data_block_editor_rest_api_preload_adds_missing_leading_slash
     500         *
     501         * @covers ::block_editor_rest_api_preload
     502         *
     503         * @param array  $preload_paths The paths to preload.
     504         * @param string $expected      The expected substring.
     505         */
     506        public function test_block_editor_rest_api_preload_adds_missing_leading_slash( array $preload_paths, $expected ) {
     507                block_editor_rest_api_preload( $preload_paths, new WP_Block_Editor_Context() );
     508                $haystack = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
     509                $this->assertStringContainsString( $expected, $haystack );
     510        }
     511
     512        /**
     513         * Data provider.
     514         *
     515         * @return array
     516         */
     517        public function data_block_editor_rest_api_preload_adds_missing_leading_slash() {
     518                return array(
     519                        'a string without a slash' => array(
     520                                'preload_paths' => array( 'wp/v2/blocks' ),
     521                                'expected'      => '\/wp\/v2\/blocks',
     522                        ),
     523                        'a string with a slash' => array(
     524                                'preload_paths' => array( '/wp/v2/blocks' ),
     525                                'expected'      => '\/wp\/v2\/blocks',
     526                        ),
     527                        'a string starting with a question mark' => array(
     528                                'preload_paths' => array( '?context=edit' ),
     529                                'expected'      => '/?context=edit',
     530                        ),
     531                        'an array with a string without a slash' => array(
     532                                'preload_paths' => array( array( 'wp/v2/blocks', 'OPTIONS' ) ),
     533                                'expected'      => '\/wp\/v2\/blocks',
     534                        ),
     535                        'an array with a string with a slash' => array(
     536                                'preload_paths' => array( array( '/wp/v2/blocks', 'OPTIONS' ) ),
     537                                'expected'      => '\/wp\/v2\/blocks',
     538                        ),
     539                        'an array with a string starting with a question mark' => array(
     540                                'preload_paths' => array( array( '?context=edit', 'OPTIONS' ) ),
     541                                'expected'      => '\/?context=edit',
     542                        ),
     543                );
     544        }
    496545}
Note: See TracChangeset for help on using the changeset viewer.