Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default_template_part_areas and default_template_types to site index endpoint #7895

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/wp-admin/edit-form-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 0 additions & 2 deletions src/wp-admin/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that because of these two changes to site editor and editor initialization, we'll need to wait for the next package update to be able to commit this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads-up that Gutenberg 19.8 has been released.

Expand Down
36 changes: 36 additions & 0 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions tests/phpunit/tests/rest-api/rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading