if ( rest_is_field_included( 'content', $fields ) ) {
$data['content'] = array();
}
if ( rest_is_field_included( 'content.raw', $fields ) ) {
$data['content']['raw'] = $template->content;
}
if ( rest_is_field_included( 'content.block_version', $fields ) ) {
$data['content']['block_version'] = block_version( $template->content );
}
if ( rest_is_field_included( 'slug', $fields ) ) {
$data['slug'] = $template->slug;
}
if ( rest_is_field_included( 'source', $fields ) ) {
$data['source'] = $template->source;
}
if ( rest_is_field_included( 'origin', $fields ) ) {
$data['origin'] = $template->origin;
}
if ( rest_is_field_included( 'type', $fields ) ) {
$data['type'] = $template->type;
}
if ( rest_is_field_included( 'description', $fields ) ) {
$data['description'] = $template->description;
}
if ( rest_is_field_included( 'title', $fields ) ) {
$data['title'] = array();
}
if ( rest_is_field_included( 'title.raw', $fields ) ) {
$data['title']['raw'] = $template->title;
}
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
if ( $template->wp_id ) {
/** This filter is documented in wp-includes/post-template.php */
$data['title']['rendered'] = apply_filters( 'the_title', $template->title, $template->wp_id );
} else {
$data['title']['rendered'] = $template->title;
}
}
if ( rest_is_field_included( 'status', $fields ) ) {
$data['status'] = $template->status;
}
if ( rest_is_field_included( 'wp_id', $fields ) ) {
$data['wp_id'] = (int) $template->wp_id;
}
if ( rest_is_field_included( 'has_theme_file', $fields ) ) {
$data['has_theme_file'] = (bool) $template->has_theme_file;
}
if ( rest_is_field_included( 'is_custom', $fields ) && 'wp_template' === $template->type ) {
$data['is_custom'] = $template->is_custom;
}
if ( rest_is_field_included( 'author', $fields ) ) {
$data['author'] = (int) $template->author;
}
if ( rest_is_field_included( 'area', $fields ) && 'wp_template_part' === $template->type ) {
$data['area'] = $template->area;
}
if ( rest_is_field_included( 'modified', $fields ) ) {
$data['modified'] = mysql_to_rfc3339( $template->modified );
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$links = $this->prepare_links( $template->id );
$response->add_links( $links );
if ( ! empty( $links['self']['href'] ) ) {
$actions = $this->get_available_actions();
$self = $links['self']['href'];
foreach ( $actions as $rel ) {
$response->add_link( $rel, $self );
}
}
}
return $response;
}
/**
* Prepares links for the request.
*
* @since 5.8.0
*
* @param integer $id ID.
* @return array Links for the given post.
*/
protected function prepare_links( $id ) {
$links = array(
'self' => array(
'href' => rest_url( rest_get_route_for_post( $id ) ),
),
'collection' => array(
'href' => rest_url( rest_get_route_for_post_type_items( $this->post_type ) ),
),
'about' => array(
'href' => rest_url( 'wp/v2/types/' . $this->post_type ),
),
);
return $links;
}
/**
* Get the link relations available for the post and current user.
*
* @since 5.8.0
*
* @return string[] List of link relations.
*/
protected function get_available_actions() {
$rels = array();
$post_type = get_post_type_object( $this->post_type );
if ( current_user_can( $post_type->cap->publish_posts ) ) {
$rels[] = 'https://api.w.org/action-publish';
}
if ( current_user_can( 'unfiltered_html' ) ) {
$rels[] = 'https://api.w.org/action-unfiltered-html';
}
return $rels;
}
/**
* Retrieves the query params for the posts collection.
*
* @since 5.8.0
* @since 5.9.0 Added `'area'` and `'post_type'`.
*
* @return array Collection parameters.
*/
public function get_collection_params() {
return array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
'wp_id' => array(
'description' => __( 'Limit to the specified post id.' ),
'type' => 'integer',
),
'area' => array(
'description' => __( 'Limit to the specified template part area.' ),
'type' => 'string',
),
'post_type' => array(
'description' => __( 'Post type to get the templates for.' ),
'type' => 'string',
),
);
}
/**
* Retrieves the block type' schema, conforming to JSON Schema.
*
* @since 5.8.0
* @since 5.9.0 Added `'area'`.
*
* @return array Item schema data.
*/
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => $this->post_type,
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'ID of template.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'slug' => array(
'description' => __( 'Unique slug identifying the template.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'required' => true,
'minLength' => 1,
'pattern' => '[a-zA-Z0-9_\%-]+',
),
'theme' => array(
'description' => __( 'Theme identifier for the template.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'type' => array(
'description' => __( 'Type of template.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'source' => array(
'description' => __( 'Source of template' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'origin' => array(
'description' => __( 'Source of a customized template' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'content' => array(
'description' => __( 'Content of template.' ),
'type' => array( 'object', 'string' ),
'default' => '',
'context' => array( 'embed', 'view', 'edit' ),
'properties' => array(
'raw' => array(
'description' => __( 'Content for the template, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'block_version' => array(
'description' => __( 'Version of the content block format used by the template.' ),
'type' => 'integer',
'context' => array( 'edit' ),
'readonly' => true,
),
),
),
'title' => array(
'description' => __( 'Title of template.' ),
'type' => array( 'object', 'string' ),
'default' => '',
'context' => array( 'embed', 'view', 'edit' ),
'properties' => array(
'raw' => array(
'description' => __( 'Title for the template, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
),
'rendered' => array(
'description' => __( 'HTML title for the template, transformed for display.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
),
'description' => array(
'description' => __( 'Description of template.' ),
'type' => 'string',
'default' => '',
'context' => array( 'embed', 'view', 'edit' ),
),
'status' => array(
'description' => __( 'Status of template.' ),
'type' => 'string',
'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ),
'default' => 'publish',
'context' => array( 'embed', 'view', 'edit' ),
),
'wp_id' => array(
'description' => __( 'Post ID.' ),
'type' => 'integer',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'has_theme_file' => array(
'description' => __( 'Theme file exists.' ),
'type' => 'bool',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'author' => array(
'description' => __( 'The ID for the author of the template.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
'modified' => array(
'description' => __( "The date the template was last modified, in the site's timezone." ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
if ( 'wp_template' === $this->post_type ) {
$schema['properties']['is_custom'] = array(
'description' => __( 'Whether a template is a custom template.' ),
'type' => 'bool',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
);
}
if ( 'wp_template_part' === $this->post_type ) {
$schema['properties']['area'] = array(
'description' => __( 'Where the template part is intended for use (header, footer, etc.)' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
);
}
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}
}