Skip to content

Commit

Permalink
Augment Templates REST endpoint with post_types
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed May 28, 2024
1 parent 4cea711 commit f7b953e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,43 @@ function gutenberg_register_global_styles_revisions_endpoints() {
}

add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' );

/**
* Registers a new post_type field for the Templates REST API route.
*/
function gutenberg_register_template_post_type_field() {
register_rest_field(
'wp_template',
'post_types',
array(
'get_callback' => 'gutenberg_rest_template_post_type_callback',
'schema' => array(
'description' => __( 'The post types the template is intended for.', 'gutenberg' ),
'type' => 'array',
'readonly' => true,
),
)
);
}
add_action( 'rest_api_init', 'gutenberg_register_template_post_type_field' );

/**
* Callback for the post_type field in the Templates REST API route.
*
* @param array $template The template object.
*
* @return array The post type the template is intended for.
*/
function gutenberg_rest_template_post_type_callback( $item ) {

$template_metadata = _get_block_template_file( 'wp_template', $item['slug'] );
if ( null === $template_metadata ) {
return array();
}

if ( isset( $template_metadata['postTypes'] ) ) {
return $template_metadata['postTypes'];
}

return array();
}

0 comments on commit f7b953e

Please sign in to comment.