diff --git a/backport-changelog/7.1/12709.md b/backport-changelog/7.1/12709.md new file mode 100644 index 00000000000000..41368a9c69f476 --- /dev/null +++ b/backport-changelog/7.1/12709.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/12709 + +* https://github.com/WordPress/gutenberg/pull/80733 diff --git a/lib/compat/wordpress-7.1/rest-api.php b/lib/compat/wordpress-7.1/rest-api.php index 2362b086e06ec7..3b9b0671fe12b9 100644 --- a/lib/compat/wordpress-7.1/rest-api.php +++ b/lib/compat/wordpress-7.1/rest-api.php @@ -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. */ diff --git a/phpunit/class-gutenberg-rest-templates-controller-test.php b/phpunit/class-gutenberg-rest-templates-controller-test.php index 52e1f6f5fc03fa..8bb84c7c7d38c7 100644 --- a/phpunit/class-gutenberg-rest-templates-controller-test.php +++ b/phpunit/class-gutenberg-rest-templates-controller-test.php @@ -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 */