| @@ -46,15 +46,39 @@ | ||
| 46 | 46 | } |
| 47 | 47 | add_filter( 'rest_api_init', 'gutenberg_add_date_wp_template_schema' ); |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | - * Registers the Icon Collections Registry REST API routes. | |
| 50 | + * Reports a `null` `modified` date for templates that have never been saved. | |
| 51 | + * | |
| 52 | + * `WP_REST_Templates_Controller::prepare_item_for_response()` passes the template's | |
| 53 | + * `modified` property straight to `mysql_to_rfc3339()`, which returns `false` for | |
| 54 | + * file-backed templates because they have no modification date. That value matches | |
| 55 | + * neither the documented `string` type nor anything a client can format, so it is | |
| 56 | + * replaced with `null` and the schema widened to allow it. | |
| 57 | + * | |
| 58 | + * @since 7.1.0 Allowed 'modified' to be null. | |
| 51 | 59 | */ |
| 52 | -function gutenberg_register_icon_collections_controller_endpoints() { | |
| 53 | - $icon_collections_controller = new WP_REST_Icon_Collections_Controller(); | |
| 54 | - $icon_collections_controller->register_routes(); | |
| 60 | +function gutenberg_allow_null_modified_wp_template_field() { | |
| 61 | + register_rest_field( | |
| 62 | + array( 'wp_template', 'wp_template_part' ), | |
| 63 | + 'modified', | |
| 64 | + array( | |
| 65 | + 'schema' => array( | |
| 66 | + 'description' => __( "The date the template was last modified, in the site's timezone.", 'gutenberg' ), | |
| 67 | + 'type' => array( 'string', 'null' ), | |
| 68 | + 'format' => 'date-time', | |
| 69 | + 'context' => array( 'view', 'edit' ), | |
| 70 | + 'readonly' => true, | |
| 71 | + ), | |
| 72 | + // The controller has already converted the date by the time this runs, so | |
| 73 | + // the prepared value is reused and only the `false` case is replaced. | |
| 74 | + 'get_callback' => function ( $item ) { | |
| 75 | + return isset( $item['modified'] ) && false !== $item['modified'] ? $item['modified'] : null; | |
| 76 | + }, | |
| 77 | + ) | |
| 78 | + ); | |
| 55 | 79 | } |
| 56 | -add_action( 'rest_api_init', 'gutenberg_register_icon_collections_controller_endpoints' ); | |
| 80 | +add_action( 'rest_api_init', 'gutenberg_allow_null_modified_wp_template_field' ); | |
| 57 | 81 | |
| 58 | 82 | /** |
| 59 | 83 | * Overrides the REST controller for the attachment post type to add support |
| 60 | 84 | * for filtering by multiple media types. |