From 718eb0aebe3b6923b95532f2ce45c5a9df15582a Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 14 Jun 2023 17:29:51 +1000 Subject: [PATCH 1/4] Adding 'modified' field and value (if available) to template rest API controller --- src/wp-includes/block-template-utils.php | 2 ++ src/wp-includes/class-wp-block-template.php | 8 +++++++ .../class-wp-rest-templates-controller.php | 21 +++++++++++++++++++ tests/phpunit/tests/block-template-utils.php | 4 ++++ .../rest-api/wpRestTemplatesController.php | 19 ++++++++++++++--- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index f3eaf7b95a56d..dc6ca4ff0ed80 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -743,6 +743,7 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * Builds a unified template object based a post Object. * * @since 5.9.0 + * @since 6.3.0 Added `modified` property for saved templates. * @access private * * @param WP_Post $post Template post. @@ -782,6 +783,7 @@ function _build_block_template_result_from_post( $post ) { $template->has_theme_file = $has_theme_file; $template->is_custom = empty( $is_wp_suggestion ); $template->author = $post->post_author; + $template->modified = $post->post_modified; if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { $template->post_types = $template_file['postTypes']; diff --git a/src/wp-includes/class-wp-block-template.php b/src/wp-includes/class-wp-block-template.php index 75bfd4747d356..8149935203220 100644 --- a/src/wp-includes/class-wp-block-template.php +++ b/src/wp-includes/class-wp-block-template.php @@ -146,4 +146,12 @@ class WP_Block_Template { * @var string|null */ public $area; + + /** + * Modified. + * + * @since 6.3.0 + * @var string|null + */ + public $modified; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index df8a3dcd6d933..a7d788a224bc3 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -614,6 +614,7 @@ protected function prepare_item_for_database( $request ) { * * @since 5.8.0 * @since 5.9.0 Renamed `$template` to `$item` to match parent class for PHP 8 named parameter support. + * @since 6.3.0 Added `modified` property to the response. * * @param WP_Block_Template $item Template instance. * @param WP_REST_Request $request Request object. @@ -708,6 +709,19 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V $data['area'] = $template->area; } + if ( rest_is_field_included( 'author', $fields ) ) { + $data['author'] = (int) $template->author; + } + + /* + * Only add the template's modified date if it is available. + * This is because the modified date is not available for template files, + * i.e., templates that have not been saved as posts. + */ + if ( rest_is_field_included( 'modified', $fields ) && ! empty( $template->modified ) ) { + $data['modified'] = mysql_to_rfc3339( $template->modified ); + } + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, $context ); @@ -926,6 +940,13 @@ public function get_item_schema() { 'type' => 'integer', 'context' => array( 'view', 'edit', 'embed' ), ), + 'modified' => array( + 'description' => __( "The date the post was last modified, in the site's timezone." ), + 'type' => 'string', + 'format' => 'date-time', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), ), ); diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 5d42207aa7325..85588d2fd9e68 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -101,6 +101,7 @@ public function test_build_block_template_result_from_post() { $this->assertSame( 'My Template', $template->title ); $this->assertSame( 'Description of my template', $template->description ); $this->assertSame( 'wp_template', $template->type ); + $this->assertSame( self::$template_post->post_modified, $template->modified ); // Test template parts. $template_part = _build_block_template_result_from_post( @@ -117,6 +118,7 @@ public function test_build_block_template_result_from_post() { $this->assertSame( 'Description of my template part', $template_part->description ); $this->assertSame( 'wp_template_part', $template_part->type ); $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area ); + $this->assertSame( self::$template_part_post->post_modified, $template->modified ); } public function test_build_block_template_result_from_file() { @@ -136,6 +138,7 @@ public function test_build_block_template_result_from_file() { $this->assertSame( 'Single', $template->title ); $this->assertSame( 'Displays single posts on your website unless a custom template has been applied to that post or a dedicated template exists.', $template->description ); $this->assertSame( 'wp_template', $template->type ); + $this->assertEmpty( $template->modified ); // Test template parts. $template_part = _build_block_template_result_from_file( @@ -155,6 +158,7 @@ public function test_build_block_template_result_from_file() { $this->assertSame( '', $template_part->description ); $this->assertSame( 'wp_template_part', $template_part->type ); $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area ); + $this->assertEmpty( $template_part->modified ); } public function test_inject_theme_attribute_in_block_template_content() { diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php index 811767444d3ae..f6310a5e42765 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -118,6 +118,7 @@ public function test_get_items() { 'has_theme_file' => false, 'is_custom' => true, 'author' => 0, + 'modified' => mysql_to_rfc3339( self::$post->post_modified ), ), $this->find_and_normalize_template_by_id( $data, 'default//my_template' ) ); @@ -162,6 +163,7 @@ public function test_get_item() { 'has_theme_file' => false, 'is_custom' => true, 'author' => 0, + 'modified' => mysql_to_rfc3339( self::$post->post_modified ), ), $data ); @@ -198,6 +200,7 @@ public function test_get_item_works_with_a_single_slash( $endpoint_url ) { 'has_theme_file' => false, 'is_custom' => true, 'author' => 0, + 'modified' => mysql_to_rfc3339( self::$post->post_modified ), ), $data ); @@ -257,6 +260,7 @@ public function test_get_item_with_valid_theme_dirname( $theme_dir, $template, a 'has_theme_file' => false, 'is_custom' => true, 'author' => self::$admin_id, + 'modified' => mysql_to_rfc3339( $post->post_modified ), ), $data ); @@ -413,6 +417,7 @@ public function test_create_item() { ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + $modified = get_post( $data['wp_id'] )->post_modified; unset( $data['_links'] ); unset( $data['wp_id'] ); @@ -436,6 +441,7 @@ public function test_create_item() { 'has_theme_file' => false, 'is_custom' => true, 'author' => self::$admin_id, + 'modified' => mysql_to_rfc3339( $modified ), ), $data ); @@ -459,6 +465,7 @@ public function test_create_item_with_numeric_slug() { ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + $modified = get_post( $data['wp_id'] )->post_modified; unset( $data['_links'] ); unset( $data['wp_id'] ); @@ -482,6 +489,7 @@ public function test_create_item_with_numeric_slug() { 'has_theme_file' => false, 'is_custom' => false, 'author' => self::$admin_id, + 'modified' => mysql_to_rfc3339( $modified ), ), $data ); @@ -509,6 +517,7 @@ public function test_create_item_raw() { ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + $modified = get_post( $data['wp_id'] )->post_modified; unset( $data['_links'] ); unset( $data['wp_id'] ); @@ -532,6 +541,7 @@ public function test_create_item_raw() { 'has_theme_file' => false, 'is_custom' => true, 'author' => self::$admin_id, + 'modified' => mysql_to_rfc3339( $modified ), ), $data ); @@ -690,7 +700,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 14, $properties ); + $this->assertCount( 15, $properties ); $this->assertArrayHasKey( 'id', $properties ); $this->assertArrayHasKey( 'description', $properties ); $this->assertArrayHasKey( 'slug', $properties ); @@ -706,6 +716,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'has_theme_file', $properties ); $this->assertArrayHasKey( 'is_custom', $properties ); $this->assertArrayHasKey( 'author', $properties ); + $this->assertArrayHasKey( 'modified', $properties ); } protected function find_and_normalize_template_by_id( $templates, $id ) { @@ -736,8 +747,10 @@ public function test_create_item_with_is_wp_suggestion( array $body_params, arra $request = new WP_REST_Request( 'POST', '/wp/v2/templates' ); $request->set_body_params( $body_params ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $modified = get_post( $data['wp_id'] )->post_modified; + $expected['modified'] = mysql_to_rfc3339( $modified ); unset( $data['_links'] ); unset( $data['wp_id'] ); From b8ff1dd99bb8aac06492e12abc6de2950899a006 Mon Sep 17 00:00:00 2001 From: Ramon Date: Thu, 15 Jun 2023 15:59:29 +1000 Subject: [PATCH 2/4] Update src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com> --- .../rest-api/endpoints/class-wp-rest-templates-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index a7d788a224bc3..bde8de4a1eb83 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -941,7 +941,7 @@ public function get_item_schema() { 'context' => array( 'view', 'edit', 'embed' ), ), 'modified' => array( - 'description' => __( "The date the post was last modified, in the site's timezone." ), + 'description' => __( "The date the template was last modified, in the site's timezone." ), 'type' => 'string', 'format' => 'date-time', 'context' => array( 'view', 'edit' ), From 1a3415bf76ed3cc5f73c3276788fa9a862a8099a Mon Sep 17 00:00:00 2001 From: ramon Date: Thu, 15 Jun 2023 16:26:30 +1000 Subject: [PATCH 3/4] Ensuring that `modified` property is always returned on the response even for file-based templates. --- src/wp-includes/block-template-utils.php | 4 +++- .../endpoints/class-wp-rest-templates-controller.php | 7 +------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index dc6ca4ff0ed80..3aba7f381d73b 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -542,6 +542,7 @@ function _remove_theme_attribute_in_block_template_content( $template_content ) * Builds a unified template object based on a theme file. * * @since 5.9.0 + * @since 6.3.0 Added `modified` property to template objects. * @access private * * @param array $template_file Theme file. @@ -564,6 +565,7 @@ function _build_block_template_result_from_file( $template_file, $template_type $template->status = 'publish'; $template->has_theme_file = true; $template->is_custom = true; + $template->modified = null; if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) { $template->description = $default_template_types[ $template_file['slug'] ]['description']; @@ -743,7 +745,7 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * Builds a unified template object based a post Object. * * @since 5.9.0 - * @since 6.3.0 Added `modified` property for saved templates. + * @since 6.3.0 Added `modified` property to template objects. * @access private * * @param WP_Post $post Template post. diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index bde8de4a1eb83..f001285402c89 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -713,12 +713,7 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V $data['author'] = (int) $template->author; } - /* - * Only add the template's modified date if it is available. - * This is because the modified date is not available for template files, - * i.e., templates that have not been saved as posts. - */ - if ( rest_is_field_included( 'modified', $fields ) && ! empty( $template->modified ) ) { + if ( rest_is_field_included( 'modified', $fields ) ) { $data['modified'] = mysql_to_rfc3339( $template->modified ); } From a52dcd0083b34e0dcb8af3366e48b343e33a329e Mon Sep 17 00:00:00 2001 From: ramon Date: Thu, 15 Jun 2023 16:33:15 +1000 Subject: [PATCH 4/4] Removed mistakenly inserted code block --- .../rest-api/endpoints/class-wp-rest-templates-controller.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index f001285402c89..d1854c5073e69 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -709,10 +709,6 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V $data['area'] = $template->area; } - if ( rest_is_field_included( 'author', $fields ) ) { - $data['author'] = (int) $template->author; - } - if ( rest_is_field_included( 'modified', $fields ) ) { $data['modified'] = mysql_to_rfc3339( $template->modified ); }