-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Templates API: return post modified datetime in response #51362
Conversation
…ate and template part objects in the rest response for WP_REST_Templates_Controller For theme templates, it will add the modified value when we have a post id (which is once the template is first save after user change)
7032cff
to
be9285f
Compare
Nice work on this one @ramonjd! Since this PR is quite large, you might have already explored this, but I was wondering if it'd be possible to add the Just thought I'd mention it in case it could avoid having to copy / paste more code from core to Gutenberg. No worries if that won't work, though, and we need to have the duplication! |
Thanks @andrewserong Oh, no I didn't try that route! Just for context: all the API methods I copied across call either So we need to have both the rest field + schema as well as the updated Worth a shot though. I can try to confirm. |
Flaky tests detected in fd88de2. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5275344341
|
…nd _wp_build_title_and_description_for_taxonomy_block_template have to be ported across too because they expect WP_Block_Template, not Gutenberg_Block_Template
🤣 This will do it 😄 diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php
index cbe9b5242a..08518c35a3 100644
--- a/lib/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php
+++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php
@@ -80,6 +80,13 @@ class Gutenberg_REST_Templates_Controller_6_3 extends WP_REST_Templates_Controll
$response = parent::prepare_item_for_response( $item, $request );
+ if ( rest_is_field_included( 'modified', $fields ) && ! empty( $response->data['wp_id'] ) ) {
+ $post = get_post( $response->data['wp_id'] );
+ if ( $post ) {
+ $response->data['modified'] = $post->post_modified;
+ }
+ }
+
if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$links = $this->prepare_revision_links( $template );
$response->add_links( $links );
diff --git a/lib/compat/wordpress-6.3/rest-api.php b/lib/compat/wordpress-6.3/rest-api.php
index 757bd317dc..992d24f187 100644
--- a/lib/compat/wordpress-6.3/rest-api.php
+++ b/lib/compat/wordpress-6.3/rest-api.php
@@ -76,3 +76,25 @@ function gutenberg_update_global_styles_rest_controller( $args, $post_type ) {
return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_update_global_styles_rest_controller', 10, 2 );
+
+/**
+ * Add the `modified` value to the `wp_template` schema.
+ *
+ * @since 6.3.0 Added 'block_types' property.
+ */
+function add_modified_wp_template_schema() {
+ register_rest_field(
+ 'wp_template',
+ 'modified',
+ array(
+ 'schema' => array(
+ 'description' => __( "The date the post was last modified, in the site's timezone.", 'gutenberg' ),
+ 'type' => 'string',
+ 'format' => 'date-time',
+ 'context' => array( 'view', 'edit' ),
+ 'readonly' => true,
+ ),
+ )
+ );
+}
+add_filter( 'rest_api_init', 'add_modified_wp_template_schema' ); I'll update this branch. The contents of the previous commit will be instructive when porting to Core at least 😄 Thanks for the nudge @andrewserong 🙇 |
… to add the field to the response.
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.
Thanks for the PR Ramon! This looks good.
lib/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php
Outdated
Show resolved
Hide resolved
…back method on the register_rest_field filter, which is the way I should have done it in the first place :)
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.
LGTM, this is working nicely for me!
Just one note of difference in testing between the PR description and the current state: it appears that when accessing the endpoint for a template that hasn't been modified, the modified
attribute is still returned by the API, but it has a null
value:
That seems good to me (I think it's more predictable if the key is consistent, but the value is null
), but just thought I'd mention it in case it wasn't intentional.
Thanks again for all the follow-up! ✨
Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com>
Thanks @andrewserong ! I'd forgotten to update the description 😢 |
* Initial commit: - refactoring page details so that the styles and components can be used in templates - getting home template details in order * - displaying template areas - refactoring footer to show last modified in template and page details * - bare bones rest controller changes to return modified for `get_item` - linking up template areas * - passing footer class to row * - hooking into settings. * Refactoring input controls layout Tweaking CSS accordingly * Reverted prefix change to file that was not copied to GB * Removing last modified changes until the templates API supports it. We can reinstate these changes once it's merged (also adding the property to core-data/src/entity-types/wp-template-part.ts) * Showing the details pages for the index template Updating translations for entity types when saving * Fixed new unlock path * updating design of area buttons switching over site title editing to posts page title editing * Updated hover states of area buttons * This commit: - adds a last updated footer (if the modified property is available) - removes all controls and addes details * Don't need these * Reinstate post title and posts per page controls Reinstate allow comments control Abstract last modified footer * Updated copy * Update help text * SidebarNavigationItem instead of button for links to template parts from the home template * Wrap areas in ItemGroup * Remove bottom margin on last detail panel * Spacing * Large inputs * Leave border radius on inputs as 2px for now * Use NumberControl * Remove debounce Use spin custom controls on the number control component Update changelog * Restore since annotation change made in #51362 --------- Co-authored-by: James Koster <james@jameskoster.co.uk>
…1362) * All these file changes are so that we can add `modified` to the template and template part objects in the rest response for WP_REST_Templates_Controller For theme templates, it will add the modified value when we have a post id (which is once the template is first save after user change) * Modified property is returned when updated. * Modified property is returned when created. * _wp_build_title_and_description_for_single_post_type_block_template and _wp_build_title_and_description_for_taxonomy_block_template have to be ported across too because they expect WP_Block_Template, not Gutenberg_Block_Template * Revert changes that required pulling all files across and using hooks to add the field to the response. * Now adding the value of `modified` to the response using the get_callback method on the register_rest_field filter, which is the way I should have done it in the first place :) * Formatting date according post_modified rules found in https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/prepare_item_for_response/ * Whoops, forgot to wrap date expectations in mysql_to_rfc3339 * Update lib/compat/wordpress-6.3/rest-api.php Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com> --------- Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com>
* Initial commit: - refactoring page details so that the styles and components can be used in templates - getting home template details in order * - displaying template areas - refactoring footer to show last modified in template and page details * - bare bones rest controller changes to return modified for `get_item` - linking up template areas * - passing footer class to row * - hooking into settings. * Refactoring input controls layout Tweaking CSS accordingly * Reverted prefix change to file that was not copied to GB * Removing last modified changes until the templates API supports it. We can reinstate these changes once it's merged (also adding the property to core-data/src/entity-types/wp-template-part.ts) * Showing the details pages for the index template Updating translations for entity types when saving * Fixed new unlock path * updating design of area buttons switching over site title editing to posts page title editing * Updated hover states of area buttons * This commit: - adds a last updated footer (if the modified property is available) - removes all controls and addes details * Don't need these * Reinstate post title and posts per page controls Reinstate allow comments control Abstract last modified footer * Updated copy * Update help text * SidebarNavigationItem instead of button for links to template parts from the home template * Wrap areas in ItemGroup * Remove bottom margin on last detail panel * Spacing * Large inputs * Leave border radius on inputs as 2px for now * Use NumberControl * Remove debounce Use spin custom controls on the number control component Update changelog * Restore since annotation change made in WordPress#51362 --------- Co-authored-by: James Koster <james@jameskoster.co.uk>
What?
All these file changes are so that we can add
modified
to the template and template part objects in the rest response for WP_REST_Templates_Controller 😄For theme templates, it will add the modified value when we have a post id (which is once the template is first save after user change)
The
modified
property will be returned when a template is created, updated and fetched.Why?
So that we can display the modified date in the UI. See: #49597
How?
Grabbing the value of
post_modified
from the post object and adding it to the response objectTesting Instructions
In the site editor, open a theme template and check the API response. There should be no modified field (because the template post record hasn't been created in the database yet.
Now make a few changes to the open theme template, and save. This will create a database record. Now refresh the page and inspect the API response (filter by
template
)When creating a new custom templates, WordPress adds a database record immediately, and therefore the
modified
property is available.