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 #4613

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'];
Expand Down Expand Up @@ -743,6 +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 to template objects.
* @access private
*
* @param WP_Post $post Template post.
Expand Down Expand Up @@ -782,6 +785,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'];
Expand Down
8 changes: 8 additions & 0 deletions src/wp-includes/class-wp-block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,12 @@ class WP_Block_Template {
* @var string|null
*/
public $area;

/**
* Modified.
*
* @since 6.3.0
* @var string|null
*/
public $modified;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -708,6 +709,10 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V
$data['area'] = $template->area;
}

if ( rest_is_field_included( 'modified', $fields ) ) {
$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 );
Expand Down Expand Up @@ -926,6 +931,13 @@ public function get_item_schema() {
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
'modified' => array(
'description' => __( "The date the template was last modified, in the site's timezone." ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'description' => __( "The date the template was last modified, in the site's timezone." ),
'description' => __( 'The date the template was last modified, in the site's timezone.' ),

Copy link
Member Author

@ramonjd ramonjd Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double quote are there because of the single quotes in site's. I can update the language to avoid the apostrophe if you think it's better.

I copied it from WP_REST_Posts_Controller

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh leave it then or you can update like below.

__( 'The date the template was last modified, in the site\'s timezone.' )

'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);

Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/tests/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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() {
Expand All @@ -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(
Expand All @@ -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() {
Expand Down
19 changes: 16 additions & 3 deletions tests/phpunit/tests/rest-api/wpRestTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
);
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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'] );

Expand All @@ -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
);
Expand All @@ -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'] );

Expand All @@ -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
);
Expand Down Expand Up @@ -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'] );

Expand All @@ -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
);
Expand Down Expand Up @@ -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 );
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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'] );

Expand Down