Skip to content
Merged
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
3 changes: 3 additions & 0 deletions backport-changelog/7.1/12709.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/12709

* https://github.com/WordPress/gutenberg/pull/80733
33 changes: 33 additions & 0 deletions lib/compat/wordpress-7.1/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,39 @@ function gutenberg_add_date_wp_template_schema() {
}
add_filter( 'rest_api_init', 'gutenberg_add_date_wp_template_schema' );

/**
* Reports a `null` `modified` date for templates that have never been saved.
*
* `WP_REST_Templates_Controller::prepare_item_for_response()` passes the template's
* `modified` property straight to `mysql_to_rfc3339()`, which returns `false` for
* file-backed templates because they have no modification date. That value matches
* neither the documented `string` type nor anything a client can format, so it is
* replaced with `null` and the schema widened to allow it.
*
* @since 7.1.0 Allowed 'modified' to be null.
*/
function gutenberg_allow_null_modified_wp_template_field() {
register_rest_field(
array( 'wp_template', 'wp_template_part' ),
'modified',
array(
'schema' => array(
'description' => __( "The date the template was last modified, in the site's timezone.", 'gutenberg' ),
'type' => array( 'string', 'null' ),
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
// The controller has already converted the date by the time this runs, so
// the prepared value is reused and only the `false` case is replaced.
'get_callback' => function ( $item ) {
return isset( $item['modified'] ) && false !== $item['modified'] ? $item['modified'] : null;
},
)
);
}
add_action( 'rest_api_init', 'gutenberg_allow_null_modified_wp_template_field' );

/**
* Registers the Icon Collections Registry REST API routes.
*/
Expand Down
19 changes: 19 additions & 0 deletions phpunit/class-gutenberg-rest-templates-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ public function test_get_items() {
);
}

/**
* A file-backed template has no 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_modified_is_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['modified'], 'The modified date should be null for a file-backed template.' );
}

/**
* @doesNotPerformAssertions
*/
Expand Down
Loading