Skip to content

Commit

Permalink
Prepare rest endpoint to be extended.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jan 3, 2023
1 parent e71ff47 commit d95917c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
16 changes: 0 additions & 16 deletions lib/compat/wordpress-6.1/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@
* @package gutenberg
*/

/**
* Update `wp_template` and `wp_template-part` post types to use
* Gutenberg's REST controller.
*
* @param array $args Array of arguments for registering a post type.
* @param string $post_type Post type key.
*/
function gutenberg_update_templates_template_parts_rest_controller( $args, $post_type ) {
if ( in_array( $post_type, array( 'wp_template', 'wp_template-part' ), true ) ) {
$args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller';
}
return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_update_templates_template_parts_rest_controller', 10, 2 );


/**
* Add the post type's `icon`(menu_icon) in the response.
* When we backport this change we will need to add the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* REST API: Gutenberg_REST_Templates_Controller_6_2 class
*
* @package Gutenberg
* @subpackage REST_API
*/

/**
* Base Templates REST API Controller.
*/
class Gutenberg_REST_Templates_Controller_6_2 extends Gutenberg_REST_Templates_Controller {

/**
* Registers the controllers routes.
*
* @return void
*/
public function register_routes() {
parent::register_routes();
// Get fallback template content.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/lookup',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_template_fallback' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'slug' => array(
'description' => __( 'The slug of the template to get the fallback for', 'gutenberg' ),
'type' => 'string',
'required' => true,
),
'is_custom' => array(
'description' => __( 'Indicates if a template is custom or part of the template hierarchy', 'gutenberg' ),
'type' => 'boolean',
),
'template_prefix' => array(
'description' => __( 'The template prefix for the created template. This is used to extract the main template type ex. in `taxonomy-books` we extract the `taxonomy`', 'gutenberg' ),
'type' => 'string',
),
),
),
)
);
}

/**
* Returns the fallback template for a given slug.
*
* @param WP_REST_Request $request The request instance.
*
* @return WP_REST_Response|WP_Error
*/
public function get_template_fallback( $request ) {
$hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] );
$fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' );
$response = $this->prepare_item_for_response( $fallback_template, $request );
return rest_ensure_response( $response );
}
}
15 changes: 15 additions & 0 deletions lib/compat/wordpress-6.2/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
* @package gutenberg
*/

/**
* Update `wp_template` and `wp_template-part` post types to use
* Gutenberg's REST controller.
*
* @param array $args Array of arguments for registering a post type.
* @param string $post_type Post type key.
*/
function gutenberg_update_templates_template_parts_rest_controller( $args, $post_type ) {
if ( in_array( $post_type, array( 'wp_template', 'wp_template-part' ), true ) ) {
$args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_2';
}
return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_update_templates_template_parts_rest_controller', 10, 2 );

/**
* Registers the block pattern categories REST API routes.
*/
Expand Down
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/compat/wordpress-6.2/class-gutenberg-rest-block-patterns-controller-6-2.php';
require_once __DIR__ . '/compat/wordpress-6.2/class-gutenberg-rest-block-pattern-categories-controller.php';
require_once __DIR__ . '/compat/wordpress-6.2/class-gutenberg-rest-pattern-directory-controller-6-2.php';
require_once __DIR__ . '/compat/wordpress-6.2/class-gutenberg-rest-templates-controller-6-2.php';
require_once __DIR__ . '/compat/wordpress-6.2/rest-api.php';
require_once __DIR__ . '/compat/wordpress-6.2/block-patterns.php';
require_once __DIR__ . '/compat/wordpress-6.2/class-gutenberg-rest-global-styles-controller-6-2.php';
Expand Down

0 comments on commit d95917c

Please sign in to comment.