-
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
Add author support to templates and template parts #1937
Conversation
@@ -516,6 +516,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; |
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.
Should we set $template->post_author
to something in _build_block_template_result_from_file
? as well Or is 0 correct in that case?
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.
It's a good question. File templates won't have an author, but I wasn't sure what a good default value was here. I leaned on convention by using the same value posts do.
But maybe it'd be better to return no author property in the response for file templates, which could also be reflected in the WP_Block_Template class by not giving it a default of 0
.
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.
I think 0
as no author makes sense for file templates.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
What's this check for? Because wp_insert_post()
will set post_author
to get_current_user_id()
if we don't specify?
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.
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 comment
The 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.
edit: Never mind, I see you already did that in WordPress/gutenberg#36763 😀 |
2bb7197
to
8888852
Compare
@@ -69,6 +69,7 @@ class WP_Block_Template { | |||
*/ | |||
public $description = ''; | |||
|
|||
|
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.
This was merged into #1967 and committed. |
Related - WordPress/gutenberg#36763
Adds support for two new REST API fields for the templates controller to meet the requirements here - WordPress/gutenberg#36597 (comment)
It adds:
author
. When a user creates a complete new template (or template part) the author is now stored. The author can then be shown in the list of templates.origin
. When a file template is customized and a post template created, this stores what the original 'source' of that template was (usually either 'theme' or 'plugin').I'll leave a disclaimer that making these kind of changes is not my strength, so this could do with some critique.
To test the full feature here you need both WordPress and Gutenberg dev environments, with this checked out for the WordPress dev environment and WordPress/gutenberg#36763 checked for the gutenberg environment.
Testing steps
wp-admin/themes.php?page=gutenberg-edit-site&postType=wp_template_part
wp-admin/themes.php?page=gutenberg-edit-site&postType=wp_template_part
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.