| 1 |
<?php |
| 2 |
/** |
| 3 |
* @see Gutenberg_REST_Templates_Controller_7_0 |
| 4 |
*/ |
| 5 |
class Gutenberg_REST_Static_Templates_Controller extends Gutenberg_REST_Templates_Controller_7_0 { |
| 6 |
public function __construct() { |
| 7 |
$this->rest_base = 'registered-templates'; |
| 8 |
$this->namespace = 'wp/v2'; |
| 9 |
} |
| 10 |
|
| 11 |
public function register_routes() { |
| 12 |
// Lists all templates. |
| 13 |
register_rest_route( |
| 14 |
$this->namespace, |
| 15 |
'/' . $this->rest_base, |
| 16 |
array( |
| 17 |
array( |
| 18 |
'methods' => WP_REST_Server::READABLE, |
| 19 |
'callback' => array( $this, 'get_items' ), |
| 20 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 21 |
'args' => $this->get_collection_params(), |
| 22 |
), |
| 23 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 24 |
) |
| 25 |
); |
| 26 |
|
| 27 |
// Lists/updates a single template based on the given id. |
| 28 |
register_rest_route( |
| 29 |
$this->namespace, |
| 30 |
// The route. |
| 31 |
sprintf( |
| 32 |
'/%s/(?P<id>%s%s)', |
| 33 |
$this->rest_base, |
| 34 |
/* |
| 35 |
* Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`. |
| 36 |
* Excludes invalid directory name characters: `/:<>*?"|`. |
| 37 |
*/ |
| 38 |
'([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', |
| 39 |
// Matches the template name. |
| 40 |
'[\/\w%-]+' |
| 41 |
), |
| 42 |
array( |
| 43 |
'args' => array( |
| 44 |
'id' => array( |
| 45 |
'description' => __( 'The id of a template' ), |
| 46 |
'type' => 'string', |
| 47 |
'sanitize_callback' => array( $this, '_sanitize_template_id' ), |
| 48 |
), |
| 49 |
), |
| 50 |
array( |
| 51 |
'methods' => WP_REST_Server::READABLE, |
| 52 |
'callback' => array( $this, 'get_item' ), |
| 53 |
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 54 |
'args' => array( |
| 55 |
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 56 |
), |
| 57 |
), |
| 58 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 59 |
) |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
public function get_item_schema() { |
| 64 |
$schema = parent::get_item_schema(); |
| 65 |
$schema['properties']['is_custom'] = array( |
| 66 |
'description' => __( 'Whether a template is a custom template.' ), |
| 67 |
'type' => 'bool', |
| 68 |
'context' => array( 'embed', 'view', 'edit' ), |
| 69 |
'readonly' => true, |
| 70 |
); |
| 71 |
$schema['properties']['plugin'] = array( |
| 72 |
'type' => 'string', |
| 73 |
'description' => __( 'Plugin that registered the template.' ), |
| 74 |
'readonly' => true, |
| 75 |
'context' => array( 'view', 'edit', 'embed' ), |
| 76 |
); |
| 77 |
return $schema; |
| 78 |
} |
| 79 |
|
| 80 |
public function get_items( $request ) { |
| 81 |
$query = array(); |
| 82 |
if ( isset( $request['area'] ) ) { |
| 83 |
$query['area'] = $request['area']; |
| 84 |
} |
| 85 |
if ( isset( $request['post_type'] ) ) { |
| 86 |
$query['post_type'] = $request['post_type']; |
| 87 |
} |
| 88 |
$query_result = gutenberg_get_registered_block_templates( $query ); |
| 89 |
$templates = array(); |
| 90 |
foreach ( $query_result as $template ) { |
| 91 |
$item = $this->prepare_item_for_response( $template, $request ); |
| 92 |
$templates[] = $this->prepare_response_for_collection( $item ); |
| 93 |
} |
| 94 |
|
| 95 |
return rest_ensure_response( $templates ); |
| 96 |
} |
| 97 |
|
| 98 |
public function get_item( $request ) { |
| 99 |
$template = get_block_file_template( $request['id'], 'wp_template' ); |
| 100 |
|
| 101 |
if ( ! $template ) { |
| 102 |
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) ); |
| 103 |
} |
| 104 |
|
| 105 |
$item = $this->prepare_item_for_response( $template, $request ); |
| 106 |
return rest_ensure_response( $item ); |
| 107 |
} |
| 108 |
} |
| 109 |
|