| @@ -44,4 +44,56 @@ | ||
| 44 | 44 | ) |
| 45 | 45 | ); |
| 46 | 46 | } |
| 47 | 47 | add_filter( 'rest_api_init', 'gutenberg_add_date_wp_template_schema' ); |
| 48 | + | |
| 49 | +/** | |
| 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. | |
| 59 | + */ | |
| 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 | + ); | |
| 79 | +} | |
| 80 | +add_action( 'rest_api_init', 'gutenberg_allow_null_modified_wp_template_field' ); | |
| 81 | + | |
| 82 | +/** | |
| 83 | + * Overrides the REST controller for the attachment post type to add support | |
| 84 | + * for filtering by multiple media types. | |
| 85 | + * | |
| 86 | + * Only applies if the experimental media processing feature is not enabled, | |
| 87 | + * as that feature includes this functionality and more. | |
| 88 | + * | |
| 89 | + * @param array $args Array of arguments for registering a post type. | |
| 90 | + * @param string $post_type Post type key. | |
| 91 | + * @return array Modified array of arguments. | |
| 92 | + */ | |
| 93 | +function gutenberg_override_attachments_rest_controller( $args, $post_type ) { | |
| 94 | + if ( 'attachment' === $post_type && ! gutenberg_is_client_side_media_processing_enabled() ) { | |
| 95 | + $args['rest_controller_class'] = 'Gutenberg_REST_Attachments_Controller_7_1'; | |
| 96 | + } | |
| 97 | + return $args; | |
| 98 | +} | |
| 99 | +add_filter( 'register_post_type_args', 'gutenberg_override_attachments_rest_controller', 10, 2 ); | |