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

REST API: Limit default templates and template part areas data access via index #69346

Closed
Closed
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
3 changes: 3 additions & 0 deletions backport-changelog/6.8/8422.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/8422

* https://github.com/WordPress/gutenberg/pull/69346
38 changes: 13 additions & 25 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,28 @@ function ( $taxonomy ) {
);

/**
* Adds the default template part areas to the REST API index.
*
* This function exposes the default template part areas through the WordPress REST API.
* Note: This function backports into the wp-includes/rest-api/class-wp-rest-server.php file.
* Adds the default template types and template part areas to the REST API index.
*
* @param WP_REST_Response $response REST API response.
* @return WP_REST_Response Modified REST API response with default template part areas.
* @return WP_REST_Response Modified REST API response.
*/
function gutenberg_add_default_template_part_areas_to_index( WP_REST_Response $response ) {
$response->data['default_template_part_areas'] = get_allowed_block_template_part_areas();
return $response;
}
add_filter( 'rest_index', 'gutenberg_add_default_template_part_areas_to_index' );
function gutenberg_add_templates_default_data_to_index( WP_REST_Response $response ) {
if ( ! is_user_logged_in() ) {
return $response;
}

/**
* Adds the default template types to the REST API index.
*
* This function exposes the default template types through the WordPress REST API.
* Note: This function backports into the wp-includes/rest-api/class-wp-rest-server.php file.
*
* @param WP_REST_Response $response REST API response.
* @return WP_REST_Response Modified REST API response with default template part areas.
*/
function gutenberg_add_default_template_types_to_index( WP_REST_Response $response ) {
$indexed_template_types = array();
foreach ( get_default_block_template_types() as $slug => $template_type ) {
$default_template_types = array();
foreach ( (array) get_default_block_template_types() as $slug => $template_type ) {
$template_type['slug'] = (string) $slug;
$indexed_template_types[] = $template_type;
$default_template_types[] = $template_type;
}

$response->data['default_template_types'] = $indexed_template_types;
$response->data['default_template_part_areas'] = get_allowed_block_template_part_areas();
$response->data['default_template_types'] = $default_template_types;

return $response;
}
add_filter( 'rest_index', 'gutenberg_add_default_template_types_to_index' );
add_filter( 'rest_index', 'gutenberg_add_templates_default_data_to_index' );

/**
* Adds the site reading options to the REST API index.
Expand Down
Loading