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

Templates API: return post modified datetime in response #51362

Merged
merged 10 commits into from
Jun 15, 2023

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Jun 9, 2023

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 object

Testing Instructions

npm run test:unit:php -- --filter Gutenberg_REST_Templates_Controller_Test

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.
Screenshot 2023-06-09 at 1 23 50 pm

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)

Screenshot 2023-06-09 at 1 22 11 pm

When creating a new custom templates, WordPress adds a database record immediately, and therefore the modified property is available.

@ramonjd ramonjd added [Type] Enhancement A suggestion for improvement. [Feature] Templates API Related to API powering block template functionality in the Site Editor [Block] Template Part Affects the Template Parts Block labels Jun 9, 2023
@ramonjd ramonjd requested a review from spacedmonkey as a code owner June 9, 2023 04:00
@ramonjd ramonjd self-assigned this Jun 9, 2023
@ramonjd ramonjd removed the [Block] Template Part Affects the Template Parts Block label Jun 9, 2023
ramonjd added 3 commits June 9, 2023 14:34
…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)
@ramonjd ramonjd force-pushed the update/templates-rest-controller-properties branch from 7032cff to be9285f Compare June 9, 2023 04:34
@andrewserong
Copy link
Contributor

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 modified field via a register_rest_field call in a rest_api_init action, using wp_template as the object type? From a quick skim of WP_REST_Templates_Controller it looks like it should (in theory) support additional fields being added via a hook, and I think it winds up being registered with the wp_template type?

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!

@ramonjd
Copy link
Member Author

ramonjd commented Jun 9, 2023

Since this PR is quite large, you might have already explored this, but I was wondering if it'd be possible to add the modified field via a register_rest_field call in a rest_api_init action, using wp_template as the object type?

Thanks @andrewserong

Oh, no I didn't try that route!

Just for context: all the API methods I copied across call either get_block_template or get_block_templates or both, which, in turn call _gutenberg_build_block_template_result_from_post. It's the latter function that constructs the object from WP_Block_Template and adds the modified property.

So we need to have both the rest field + schema as well as the updated WP_Block_Template class.

Worth a shot though. I can try to confirm.

@github-actions
Copy link

github-actions bot commented Jun 9, 2023

Flaky tests detected in fd88de2.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5275344341
📝 Reported issues:

…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
@ramonjd
Copy link
Member Author

ramonjd commented Jun 9, 2023

🤣

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 🙇

Copy link
Contributor

@ntsekouras ntsekouras left a 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.

Copy link
Contributor

@andrewserong andrewserong left a 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:

image

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! ✨

lib/compat/wordpress-6.3/rest-api.php Outdated Show resolved Hide resolved
Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com>
@ramonjd
Copy link
Member Author

ramonjd commented Jun 15, 2023

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 @andrewserong !

I'd forgotten to update the description 😢

@ramonjd ramonjd enabled auto-merge (squash) June 15, 2023 06:14
@ramonjd ramonjd merged commit cd234e8 into trunk Jun 15, 2023
@ramonjd ramonjd deleted the update/templates-rest-controller-properties branch June 15, 2023 06:41
@github-actions github-actions bot added this to the Gutenberg 16.1 milestone Jun 15, 2023
ramonjd added a commit that referenced this pull request Jun 20, 2023
ramonjd added a commit that referenced this pull request Jun 20, 2023
* 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>
sethrubenstein pushed a commit to pewresearch/gutenberg that referenced this pull request Jul 13, 2023
…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>
sethrubenstein pushed a commit to pewresearch/gutenberg that referenced this pull request Jul 13, 2023
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants