-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 author support to templates and template parts #1937
Changes from 9 commits
8f87db5
77a5e68
bdb04b3
0095e94
39e5f91
11e1a10
8888852
63d008a
51c768e
89ea62c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,6 +244,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 { | ||
|
@@ -294,6 +298,11 @@ public function create_item_permissions_check( $request ) { | |
*/ | ||
public function create_item( $request ) { | ||
$prepared_post = $this->prepare_item_for_database( $request ); | ||
|
||
if ( is_wp_error( $prepared_post ) ) { | ||
return $prepared_post; | ||
} | ||
|
||
$prepared_post->post_name = $request['slug']; | ||
$post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true ); | ||
if ( is_wp_error( $post_id ) ) { | ||
|
@@ -422,6 +431,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; | ||
|
@@ -461,6 +473,24 @@ protected function prepare_item_for_database( $request ) { | |
} | ||
} | ||
|
||
if ( ! empty( $request['author'] ) ) { | ||
$post_author = (int) $request['author']; | ||
|
||
if ( get_current_user_id() !== $post_author ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this check for? Because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I lifted it directly from the rest posts controller. 😄 My understanding is that it's a quick way to check if the provided author value is valid. If the id matches the current user it bypasses the more expensive check a few lines below. Seems to be based on the assumption that the current user is valid, which I think is fair enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine as it is a copy and paste from the post controller. |
||
$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; | ||
} | ||
|
||
|
@@ -510,6 +540,10 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V | |
$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; | ||
} | ||
|
@@ -547,6 +581,10 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V | |
$data['has_theme_file'] = (bool) $template->has_theme_file; | ||
} | ||
|
||
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; | ||
} | ||
|
@@ -695,6 +733,12 @@ public function get_item_schema() { | |
'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' ), | ||
|
@@ -758,6 +802,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' ), | ||
), | ||
), | ||
); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this line.