Skip to content

Commit

Permalink
Replicate API changes for template author support from WP core
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Nov 24, 2021
1 parent 60f8a87 commit a815bbc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/compat/wordpress-5.9/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ function _build_block_template_result_from_post( $post ) {
$template->status = $post->post_status;
$template->has_theme_file = $has_theme_file;
$template->is_custom = true;
$template->post_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 @@ -385,6 +385,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.' ),
array( 'status' => 400 )
);
}
}

$changes->post_author = $post_author;
}

return $changes;
}

Expand Down Expand Up @@ -412,6 +430,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->post_author,
);

if ( 'wp_template_part' === $template->type ) {
Expand Down Expand Up @@ -579,6 +598,11 @@ public function get_item_schema() {
'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' ),
),
),
);

Expand Down
7 changes: 7 additions & 0 deletions lib/full-site-editing/class-wp-block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,11 @@ class WP_Block_Template {
* @var bool
*/
public $is_custom = true;

/**
* Author.
*
* @var int
*/
public $post_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

0 comments on commit a815bbc

Please sign in to comment.