Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add origin and author to template rest api #36896

Merged
merged 8 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/compat/wordpress-5.9/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ function _build_block_template_result_from_post( $post ) {
return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'gutenberg' ) );
}

$origin = get_post_meta( $post->ID, 'origin', true );

$theme = $terms[0]->name;
$has_theme_file = wp_get_theme()->get_stylesheet() === $theme &&
null !== _get_block_template_file( $post->post_type, $post->post_name );
Expand All @@ -561,12 +563,14 @@ function _build_block_template_result_from_post( $post ) {
$template->content = $post->post_content;
$template->slug = $post->post_name;
$template->source = 'custom';
$template->origin = ! empty( $origin ) ? $origin : null;
$template->type = $post->post_type;
$template->description = $post->post_excerpt;
$template->title = $post->post_title;
$template->status = $post->post_status;
$template->has_theme_file = $has_theme_file;
$template->is_custom = true;
$template->author = $post->post_author;

if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
$template->is_custom = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ public function update_item( $request ) {

$changes = $this->prepare_item_for_database( $request );

if ( is_wp_error( $changes ) ) {
return $changes;
}

if ( 'custom' === $template->source ) {
$result = wp_update_post( wp_slash( (array) $changes ), true );
} else {
Expand Down Expand Up @@ -283,7 +287,12 @@ public function create_item_permissions_check( $request ) {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function create_item( $request ) {
$changes = $this->prepare_item_for_database( $request );
$changes = $this->prepare_item_for_database( $request );

if ( is_wp_error( $changes ) ) {
return $changes;
}

$changes->post_name = $request['slug'];
$result = wp_insert_post( wp_slash( (array) $changes ), true );
if ( is_wp_error( $result ) ) {
Expand Down Expand Up @@ -385,6 +394,9 @@ protected function prepare_item_for_database( $request ) {
$changes->tax_input = array(
'wp_theme' => $template->theme,
);
$changes->meta_input = array(
'origin' => $template->source,
);
} else {
$changes->post_name = $template->slug;
$changes->ID = $template->wp_id;
Expand Down Expand Up @@ -416,6 +428,24 @@ protected function prepare_item_for_database( $request ) {
}
}

if ( ! empty( $request['author'] ) ) {
$post_author = (int) $request['author'];

if ( get_current_user_id() !== $post_author ) {
$user_obj = get_userdata( $post_author );

if ( ! $user_obj ) {
return new WP_Error(
'rest_invalid_author',
__( 'Invalid author ID.', 'gutenberg' ),
array( 'status' => 400 )
);
}
}

$changes->post_author = $post_author;
}

return $changes;
}

Expand All @@ -434,6 +464,7 @@ public function prepare_item_for_response( $template, $request ) { // phpcs:igno
'content' => array( 'raw' => $template->content ),
'slug' => $template->slug,
'source' => $template->source,
'origin' => $template->origin,
'type' => $template->type,
'description' => $template->description,
'title' => array(
Expand All @@ -443,6 +474,7 @@ public function prepare_item_for_response( $template, $request ) { // phpcs:igno
'status' => $template->status,
'wp_id' => $template->wp_id,
'has_theme_file' => $template->has_theme_file,
'author' => (int) $template->author,
);

if ( 'wp_template' === $template->type ) {
Expand Down Expand Up @@ -578,6 +610,12 @@ public function get_item_schema() {
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'origin' => array(
'description' => __( 'Source of customized template', 'gutenberg' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'content' => array(
'description' => __( 'Content of template.', 'gutenberg' ),
'type' => array( 'object', 'string' ),
Expand Down Expand Up @@ -614,6 +652,11 @@ public function get_item_schema() {
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'author' => array(
'description' => __( 'The ID for the author of the template.', 'gutenberg' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
),
);

Expand Down
18 changes: 18 additions & 0 deletions lib/full-site-editing/class-wp-block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ class WP_Block_Template {
*/
public $source = 'theme';

/**
* Origin of the content when the content has been customized.
* When customized, origin takes on the value of source and source becomes
* 'custom'.
*
* @var string
*/
public $origin;

/**
* Post Id.
*
Expand Down Expand Up @@ -94,4 +103,13 @@ class WP_Block_Template {
* @var bool
*/
public $is_custom = true;

/**
* Author.
*
* A value of 0 means no author.
*
* @var int
*/
public $author = 0;
}
1 change: 1 addition & 0 deletions lib/full-site-editing/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function gutenberg_register_template_part_post_type() {
'excerpt',
'editor',
'revisions',
'author',
),
);

Expand Down
1 change: 1 addition & 0 deletions lib/full-site-editing/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function gutenberg_register_template_post_type() {
'excerpt',
'editor',
'revisions',
'author',
),
);

Expand Down
14 changes: 14 additions & 0 deletions phpunit/class-gutenberg-rest-template-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ function find_and_normalize_template_by_id( $templates, $id ) {
'wp_id' => null,
'has_theme_file' => true,
'is_custom' => false,
'origin' => null,
'author' => 0,
),
find_and_normalize_template_by_id( $data, 'tt1-blocks//index' )
);
Expand All @@ -102,6 +104,8 @@ function find_and_normalize_template_by_id( $templates, $id ) {
'wp_id' => null,
'area' => WP_TEMPLATE_PART_AREA_HEADER,
'has_theme_file' => true,
'origin' => null,
'author' => 0,
),
find_and_normalize_template_by_id( $data, 'tt1-blocks//header' )
);
Expand Down Expand Up @@ -131,6 +135,8 @@ public function test_get_item() {
'wp_id' => null,
'has_theme_file' => true,
'is_custom' => false,
'origin' => null,
'author' => 0,
),
$data
);
Expand All @@ -157,6 +163,8 @@ public function test_get_item() {
'wp_id' => null,
'area' => WP_TEMPLATE_PART_AREA_HEADER,
'has_theme_file' => true,
'origin' => null,
'author' => 0,
),
$data
);
Expand Down Expand Up @@ -192,6 +200,8 @@ public function test_get_item_works_with_a_single_slash( $endpoint_url ) {
'wp_id' => null,
'has_theme_file' => true,
'is_custom' => false,
'origin' => null,
'author' => 0,
),
$data
);
Expand Down Expand Up @@ -267,6 +277,8 @@ public function test_create_item() {
),
'has_theme_file' => false,
'is_custom' => true,
'origin' => null,
'author' => 0,
),
$data
);
Expand Down Expand Up @@ -305,6 +317,8 @@ public function test_create_item() {
),
'area' => 'header',
'has_theme_file' => false,
'origin' => null,
'author' => 0,
),
$data
);
Expand Down