From f7b953e418dc127fc024b7cdda028e1669b6987f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Tue, 28 May 2024 12:28:19 +0200 Subject: [PATCH] Augment Templates REST endpoint with post_types --- lib/compat/wordpress-6.6/rest-api.php | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index 54796685f45ab8..f23f9df3771386 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -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(); +}