diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php index bd66ac60ba60b..0b2d1179691b5 100644 --- a/src/wp-admin/edit-form-blocks.php +++ b/src/wp-admin/edit-form-blocks.php @@ -276,10 +276,6 @@ static function ( $classes ) { } } -if ( wp_is_block_theme() && $editor_settings['supportsTemplateMode'] ) { - $editor_settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas(); -} - /** * Scripts */ diff --git a/src/wp-admin/site-editor.php b/src/wp-admin/site-editor.php index a4a3cca276e2e..334f632ff1b42 100644 --- a/src/wp-admin/site-editor.php +++ b/src/wp-admin/site-editor.php @@ -144,8 +144,6 @@ static function ( $classes ) { 'siteUrl' => site_url(), 'postsPerPage' => get_option( 'posts_per_page' ), 'styles' => get_block_editor_theme_styles(), - 'defaultTemplateTypes' => $indexed_template_types, - 'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(), 'supportsLayout' => wp_theme_has_theme_json(), 'supportsTemplatePartsMode' => ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ), ); diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index 17d620f6fb6e0..88572744d1680 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -1402,6 +1402,14 @@ public function get_index( $request ) { } } + if ( rest_is_field_included( 'default_template_part_areas', $fields ) ) { + $this->add_default_template_part_areas_to_index( $response ); + } + + if ( rest_is_field_included( 'default_template_types', $fields ) ) { + $this->add_default_template_types_to_index( $response ); + } + /** * Filters the REST API root index data. * @@ -1481,6 +1489,34 @@ protected function add_site_icon_to_index( WP_REST_Response $response ) { $response->data['site_icon_url'] = get_site_icon_url(); } + /** + * Exposes the default template part areas through the WordPress REST API. + * + * @since 6.8.0 + * + * @param WP_REST_Response $response REST API response. + */ + protected function add_default_template_part_areas_to_index( WP_REST_Response $response ) { + $response->data['default_template_part_areas'] = get_allowed_block_template_part_areas(); + } + + /** + * Exposes the default template types through the WordPress REST API. + * + * @since 6.8.0 + * + * @param WP_REST_Response $response REST API response. + */ + protected function add_default_template_types_to_index( WP_REST_Response $response ) { + $indexed_template_types = array(); + foreach ( get_default_block_template_types() as $slug => $template_type ) { + $template_type['slug'] = (string) $slug; + $indexed_template_types[] = $template_type; + } + + $response->data['default_template_types'] = $indexed_template_types; + } + /** * Exposes an image through the WordPress REST API. * This is used for fetching this information when user has no rights diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 378b51f606cc9..496c4ba178fb3 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -1358,6 +1358,82 @@ public function data_get_index_should_return_site_icon_and_site_logo_fields() { ); } + /** + * Test that the "get_index" method doesn't include the default_template_part_areas + * and default_template_types fields by default. + * @ticket 62574 + * + * @covers WP_REST_Server::get_index + */ + public function test_get_index_should_not_include_default_template_part_areas_and_default_template_types() { + $server = new WP_REST_Server(); + $request = new WP_REST_Request( 'GET', '/' ); + $index = $server->dispatch( $request ); + $data = $index->get_data(); + + $this->assertArrayNotHasKey( 'default_template_types', $data, 'The "default_template_types" field is missing in the response.' ); + $this->assertArrayNotHasKey( 'default_template_part_areas', $data, 'The "default_template_part_areas" field is missing in the response.' ); + } + + /** + * Test that the "get_index" method returns the expected default_template_part_areas + * and default_template_types fields based on the specified request parameters. + * + * @ticket 62574 + * + * @covers WP_REST_Server::get_index + * + * @dataProvider data_get_index_should_return_default_template_part_areas_and_default_template_types + * + * @param string $fields List of fields to use in the request. + * @param array $expected_fields Expected fields. + * @param array $unexpected_fields Optional. Fields that should not be in the results. Default array(). + */ + public function test_get_index_should_return_default_template_part_areas_and_default_template_types( $fields, $expected_fields, $unexpected_fields = array() ) { + $server = new WP_REST_Server(); + $request = new WP_REST_Request( 'GET', '/', array() ); + $request->set_param( '_fields', $fields ); + $response = $server->get_index( $request )->get_data(); + + foreach ( $expected_fields as $expected_field ) { + $this->assertArrayHasKey( $expected_field, $response, "Expected \"{$expected_field}\" field is missing in the response." ); + } + + foreach ( $unexpected_fields as $unexpected_field ) { + $this->assertArrayNotHasKey( $unexpected_field, $response, "Response must not contain the \"{$unexpected_field}\" field." ); + } + } + + /** + * Data provider. + * + * @return array + */ + public function data_get_index_should_return_default_template_part_areas_and_default_template_types() { + return array( + 'no default_template_types or default_template_part_areas fields' => array( + 'fields' => 'name', + 'expected_fields' => array(), + 'unexpected_fields' => array( 'default_template_types', 'default_template_part_areas' ), + ), + 'default_template_types field' => array( + 'fields' => 'default_template_types', + 'expected_fields' => array( 'default_template_types' ), + 'unexpected_fields' => array( 'default_template_part_areas' ), + ), + 'default_template_part_areas field' => array( + 'fields' => 'default_template_part_areas', + 'expected_fields' => array( 'default_template_part_areas' ), + 'unexpected_fields' => array( 'default_template_types' ), + ), + 'default_template_part_areas and default_template_types fields' => array( + 'fields' => 'default_template_part_areas,default_template_types', + 'expected_fields' => array( 'default_template_types', 'default_template_part_areas' ), + 'unexpected_fields' => array( '' ), + ), + ); + } + public function test_get_namespace_index() { $server = new WP_REST_Server(); $server->register_route(