/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
lib/compat/wordpress-7.1/rest-api.php
108 строк
4 KB
Nik Tsekouras
Fix template `modified` and `date` return value for file templates (#80733)
30 июл 2026, 12:22
Не верифицирован
30 июл 2026, 12:22
ba4d03e
Код
Авторство
О чём код?
<?php /** * WordPress 7.1 compatibility functions for the Gutenberg * editor plugin changes related to REST API. * * @package gutenberg */ /** * Registers the View Config REST API routes. */ function gutenberg_register_view_config_controller_endpoints() { $view_config_controller = new Gutenberg_REST_View_Config_Controller_7_1(); $view_config_controller->register_routes(); } add_action( 'rest_api_init', 'gutenberg_register_view_config_controller_endpoints', PHP_INT_MAX ); /** * Add the `date` value to the `wp_template` schema. * * @since 7.1.0 Added 'date' property and response value. */ function gutenberg_add_date_wp_template_schema() { register_rest_field( array( 'wp_template', 'wp_template_part' ), 'date', array( 'schema' => array( 'description' => __( "The date the template was published, in the site's timezone.", 'gutenberg' ), 'type' => array( 'string', 'null' ), 'format' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'get_callback' => function ( $item ) { if ( ! empty( $item['wp_id'] ) ) { $post = get_post( $item['wp_id'] ); if ( $post && isset( $post->post_date ) ) { return mysql_to_rfc3339( $post->post_date ); } } return null; }, ) ); } 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. */ function gutenberg_register_icon_collections_controller_endpoints() { $icon_collections_controller = new WP_REST_Icon_Collections_Controller(); $icon_collections_controller->register_routes(); } add_action( 'rest_api_init', 'gutenberg_register_icon_collections_controller_endpoints' ); /** * Overrides the REST controller for the attachment post type to add support * for filtering by multiple media types. * * Only applies if the experimental media processing feature is not enabled, * as that feature includes this functionality and more. * * @param array $args Array of arguments for registering a post type. * @param string $post_type Post type key. * @return array Modified array of arguments. */ function gutenberg_override_attachments_rest_controller( $args, $post_type ) { if ( 'attachment' === $post_type && ! gutenberg_is_client_side_media_processing_enabled() ) { $args['rest_controller_class'] = 'Gutenberg_REST_Attachments_Controller_7_1'; } return $args; } add_filter( 'register_post_type_args', 'gutenberg_override_attachments_rest_controller', 10, 2 );