From b175fb545586a97ae9eb4145f85e3bd9af85e54f Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Tue, 26 Nov 2024 14:38:44 +0100 Subject: [PATCH 1/4] add default_template_part_areas and default_template_types to site index endpoint --- .../rest-api/class-wp-rest-server.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) 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..f0cb74ffbc556 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,15 @@ 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 +1490,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 From d38449f624bd1efa08bf12220ceda6f5883f123c Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Tue, 26 Nov 2024 14:38:59 +0100 Subject: [PATCH 2/4] remove default_template_part_areas and default_template_types from setting object --- src/wp-admin/edit-form-blocks.php | 4 ---- src/wp-admin/site-editor.php | 2 -- 2 files changed, 6 deletions(-) diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php index 5dbb9f33e4970..db3179f4b0cbf 100644 --- a/src/wp-admin/edit-form-blocks.php +++ b/src/wp-admin/edit-form-blocks.php @@ -269,10 +269,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 3207bccdf2632..11f331731df9a 100644 --- a/src/wp-admin/site-editor.php +++ b/src/wp-admin/site-editor.php @@ -61,8 +61,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' ), ); From 687e5f50629715ca19f2a385bc4a4d2c1700c2b0 Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Tue, 26 Nov 2024 14:48:09 +0100 Subject: [PATCH 3/4] format php code --- src/wp-includes/rest-api/class-wp-rest-server.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 f0cb74ffbc556..88572744d1680 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -1410,7 +1410,6 @@ public function get_index( $request ) { $this->add_default_template_types_to_index( $response ); } - /** * Filters the REST API root index data. * @@ -1497,7 +1496,7 @@ protected function add_site_icon_to_index( WP_REST_Response $response ) { * * @param WP_REST_Response $response REST API response. */ - protected function add_default_template_part_areas_to_index(WP_REST_Response $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(); } @@ -1508,7 +1507,7 @@ protected function add_default_template_part_areas_to_index(WP_REST_Response $re * * @param WP_REST_Response $response REST API response. */ - protected function add_default_template_types_to_index(WP_REST_Response $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; From 60827f8d2cc880f14da018eaf84ffdc51f1506c2 Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Wed, 27 Nov 2024 13:03:50 +0100 Subject: [PATCH 4/4] add unit test --- tests/phpunit/tests/rest-api/rest-server.php | 76 ++++++++++++++++++++ 1 file changed, 76 insertions(+) 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(