Skip to content

Commit 0c2c181

Browse files
committed
Templates: Add date field
1 parent 502624f commit 0c2c181

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

src/wp-includes/block-template-utils.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ function _remove_theme_attribute_from_template_part_block( &$block ) {
595595
*
596596
* @since 5.9.0
597597
* @since 6.3.0 Added `modified` property to template objects.
598+
* @since 7.1.0 Added `date` property to template objects.
598599
* @access private
599600
*
600601
* @param array $template_file Theme file.
@@ -617,6 +618,7 @@ function _build_block_template_result_from_file( $template_file, $template_type
617618
$template->has_theme_file = true;
618619
$template->is_custom = true;
619620
$template->modified = null;
621+
$template->date = null;
620622

621623
if ( 'wp_template' === $template_type ) {
622624
$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_file['slug'] );
@@ -867,6 +869,7 @@ function _build_block_template_object_from_post_object( $post, $terms = array(),
867869
$template->is_custom = empty( $meta['is_wp_suggestion'] );
868870
$template->author = $post->post_author;
869871
$template->modified = $post->post_modified;
872+
$template->date = $post->post_date;
870873

871874
if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
872875
$template->post_types = $template_file['postTypes'];

src/wp-includes/class-wp-block-template.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,12 @@ class WP_Block_Template {
162162
* @var string|null
163163
*/
164164
public $modified;
165+
166+
/**
167+
* Date.
168+
*
169+
* @since 7.1.0
170+
* @var string|null
171+
*/
172+
public $date;
165173
}

src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ protected function prepare_item_for_database( $request ) {
667667
* @since 5.8.0
668668
* @since 5.9.0 Renamed `$template` to `$item` to match parent class for PHP 8 named parameter support.
669669
* @since 6.3.0 Added `modified` property to the response.
670+
* @since 7.1.0 Added `date` property to the response.
670671
*
671672
* @param WP_Block_Template $item Template instance.
672673
* @param WP_REST_Request $request Request object.
@@ -778,6 +779,10 @@ public function prepare_item_for_response( $item, $request ) {
778779
$data['modified'] = mysql_to_rfc3339( $template->modified );
779780
}
780781

782+
if ( rest_is_field_included( 'date', $fields ) ) {
783+
$data['date'] = mysql_to_rfc3339( $template->date );
784+
}
785+
781786
if ( rest_is_field_included( 'author_text', $fields ) ) {
782787
$data['author_text'] = self::get_wp_templates_author_text_field( $template );
783788
}
@@ -1172,6 +1177,13 @@ public function get_item_schema() {
11721177
'user',
11731178
),
11741179
),
1180+
'date' => array(
1181+
'description' => __( "The date the template was published, in the site's timezone." ),
1182+
'type' => array( 'string', 'null' ),
1183+
'format' => 'date-time',
1184+
'context' => array( 'view', 'edit' ),
1185+
'readonly' => true,
1186+
),
11751187
),
11761188
);
11771189

tests/phpunit/tests/rest-api/wpRestTemplatesController.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public function test_get_items() {
168168
'is_custom' => true,
169169
'author' => 0,
170170
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
171+
'date' => mysql_to_rfc3339( self::$template_post->post_date ),
171172
'author_text' => 'Test Blog',
172173
'original_source' => 'site',
173174
),
@@ -247,6 +248,7 @@ public function test_get_items_editor() {
247248
'is_custom' => true,
248249
'author' => 0,
249250
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
251+
'date' => mysql_to_rfc3339( self::$template_post->post_date ),
250252
'author_text' => 'Test Blog',
251253
'original_source' => 'site',
252254
),
@@ -304,6 +306,7 @@ public function test_get_item() {
304306
'is_custom' => true,
305307
'author' => 0,
306308
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
309+
'date' => mysql_to_rfc3339( self::$template_post->post_date ),
307310
'author_text' => 'Test Blog',
308311
'original_source' => 'site',
309312
),
@@ -355,6 +358,7 @@ public function test_get_item_editor() {
355358
'is_custom' => true,
356359
'author' => 0,
357360
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
361+
'date' => mysql_to_rfc3339( self::$template_post->post_date ),
358362
'author_text' => 'Test Blog',
359363
'original_source' => 'site',
360364
),
@@ -404,6 +408,7 @@ public function test_get_item_works_with_a_single_slash( $endpoint_url ) {
404408
'is_custom' => true,
405409
'author' => 0,
406410
'modified' => mysql_to_rfc3339( self::$template_post->post_modified ),
411+
'date' => mysql_to_rfc3339( self::$template_post->post_date ),
407412
'author_text' => 'Test Blog',
408413
'original_source' => 'site',
409414
),
@@ -469,6 +474,7 @@ public function test_get_item_with_valid_theme_dirname( $theme_dir, $template, a
469474
'modified' => mysql_to_rfc3339( $post->post_modified ),
470475
'author_text' => $author_name,
471476
'original_source' => 'user',
477+
'date' => mysql_to_rfc3339( $post->post_date ),
472478
),
473479
$data
474480
);
@@ -673,6 +679,7 @@ public function test_create_item() {
673679
$response = rest_get_server()->dispatch( $request );
674680
$data = $response->get_data();
675681
$modified = get_post( $data['wp_id'] )->post_modified;
682+
$date = get_post( $data['wp_id'] )->post_date;
676683
unset( $data['_links'] );
677684
unset( $data['wp_id'] );
678685

@@ -699,6 +706,7 @@ public function test_create_item() {
699706
'is_custom' => true,
700707
'author' => self::$admin_id,
701708
'modified' => mysql_to_rfc3339( $modified ),
709+
'date' => mysql_to_rfc3339( $date ),
702710
'author_text' => $author_name,
703711
'original_source' => 'user',
704712
),
@@ -725,6 +733,7 @@ public function test_create_item_with_numeric_slug() {
725733
$response = rest_get_server()->dispatch( $request );
726734
$data = $response->get_data();
727735
$modified = get_post( $data['wp_id'] )->post_modified;
736+
$date = get_post( $data['wp_id'] )->post_date;
728737
unset( $data['_links'] );
729738
unset( $data['wp_id'] );
730739

@@ -751,6 +760,7 @@ public function test_create_item_with_numeric_slug() {
751760
'is_custom' => false,
752761
'author' => self::$admin_id,
753762
'modified' => mysql_to_rfc3339( $modified ),
763+
'date' => mysql_to_rfc3339( $date ),
754764
'author_text' => $author_name,
755765
'original_source' => 'user',
756766
),
@@ -781,6 +791,7 @@ public function test_create_item_raw() {
781791
$response = rest_get_server()->dispatch( $request );
782792
$data = $response->get_data();
783793
$modified = get_post( $data['wp_id'] )->post_modified;
794+
$date = get_post( $data['wp_id'] )->post_date;
784795
unset( $data['_links'] );
785796
unset( $data['wp_id'] );
786797

@@ -807,6 +818,7 @@ public function test_create_item_raw() {
807818
'is_custom' => true,
808819
'author' => self::$admin_id,
809820
'modified' => mysql_to_rfc3339( $modified ),
821+
'date' => mysql_to_rfc3339( $date ),
810822
'author_text' => $author_name,
811823
'original_source' => 'user',
812824
),
@@ -967,7 +979,7 @@ public function test_get_item_schema() {
967979
$response = rest_get_server()->dispatch( $request );
968980
$data = $response->get_data();
969981
$properties = $data['schema']['properties'];
970-
$this->assertCount( 18, $properties );
982+
$this->assertCount( 19, $properties );
971983
$this->assertArrayHasKey( 'id', $properties );
972984
$this->assertArrayHasKey( 'description', $properties );
973985
$this->assertArrayHasKey( 'slug', $properties );
@@ -984,6 +996,7 @@ public function test_get_item_schema() {
984996
$this->assertArrayHasKey( 'is_custom', $properties );
985997
$this->assertArrayHasKey( 'author', $properties );
986998
$this->assertArrayHasKey( 'modified', $properties );
999+
$this->assertArrayHasKey( 'date', $properties );
9871000
$this->assertArrayHasKey( 'author_text', $properties );
9881001
$this->assertArrayHasKey( 'original_source', $properties );
9891002
$this->assertArrayHasKey( 'plugin', $properties );
@@ -1020,7 +1033,9 @@ public function test_create_item_with_is_wp_suggestion( array $body_params, arra
10201033
$response = rest_get_server()->dispatch( $request );
10211034
$data = $response->get_data();
10221035
$modified = get_post( $data['wp_id'] )->post_modified;
1036+
$date = get_post( $data['wp_id'] )->post_date;
10231037
$expected['modified'] = mysql_to_rfc3339( $modified );
1038+
$expected['date'] = mysql_to_rfc3339( $date );
10241039
$expected['author_text'] = get_user_by( 'id', self::$admin_id )->get( 'display_name' );
10251040
$expected['original_source'] = 'user';
10261041

0 commit comments

Comments
 (0)