Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@ protected function prepare_item_for_database( $request ) {
* @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.
* @since 7.1.0 Added `date` property to the response.
* @since 7.1.0 The `modified` property is `null` for templates that have no
* modification date.
*
* @param WP_Block_Template $item Template instance.
* @param WP_REST_Request $request Request object.
Expand Down Expand Up @@ -776,11 +778,23 @@ public function prepare_item_for_response( $item, $request ) {
}

if ( rest_is_field_included( 'modified', $fields ) ) {
$data['modified'] = mysql_to_rfc3339( $template->modified );
/*
* File-backed templates have no modification date, and `mysql_to_rfc3339()`
* returns `false` for an empty or malformed value, which the schema does
* not allow. Return `null` in that case.
*/
$modified = mysql_to_rfc3339( $template->modified );
$data['modified'] = false !== $modified ? $modified : null;
}

if ( rest_is_field_included( 'date', $fields ) ) {
$data['date'] = mysql_to_rfc3339( $template->date );
/*
* File-backed templates have no date, and `mysql_to_rfc3339()` returns
* `false` for an empty or malformed value, which the schema does not
* allow. Return `null` in that case.
*/
$date = mysql_to_rfc3339( $template->date );
$data['date'] = false !== $date ? $date : null;
}

if ( rest_is_field_included( 'author_text', $fields ) ) {
Expand Down Expand Up @@ -1154,7 +1168,7 @@ public function get_item_schema() {
),
'modified' => array(
'description' => __( "The date the template was last modified, in the site's timezone." ),
'type' => 'string',
'type' => array( 'string', 'null' ),
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
Expand All @@ -1177,7 +1191,7 @@ public function get_item_schema() {
'user',
),
),
'date' => array(
'date' => array(
'description' => __( "The date the template was published, in the site's timezone." ),
'type' => array( 'string', 'null' ),
'format' => 'date-time',
Expand Down
20 changes: 20 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,26 @@ public function test_get_item_from_registry() {
$this->assertSame( 404, $response->get_status(), 'Fetching an unregistered template should return 404.' );
}

/**
* A file-backed template has no publication or modification date, which should
* be exposed as `null` rather than the `false` returned by `mysql_to_rfc3339()`.
*
* @ticket 65728
* @covers WP_REST_Templates_Controller::prepare_item_for_response
*/
public function test_get_item_dates_are_null_for_file_backed_template() {
wp_set_current_user( self::$admin_id );
switch_theme( 'block-theme' );

$request = new WP_REST_Request( 'GET', '/wp/v2/templates/block-theme//page-home' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 200, $response->get_status(), 'Fetching a file-backed template should return 200.' );
$this->assertNull( $data['date'], 'The date should be null for a file-backed template.' );
$this->assertNull( $data['modified'], 'The modified date should be null for a file-backed template.' );
}

/**
* @ticket 54507
* @dataProvider data_sanitize_template_id
Expand Down
Loading