Make WordPress Core

Changeset 62941


Ignore:
Timestamp:
07/30/2026 10:08:06 AM (10 days ago)
Author:
ntsekouras
Message:

Editor: Fix template date and modified REST API values for file-based templates.

File-based templates that have never been customized have no underlying post, so their date and modified properties are empty. mysql_to_rfc3339() returns false for such values, which matches neither the documented string type nor anything a client can format. The templates controller now returns null for both fields in that case, and the modified schema is widened to allow it.

Ports the changes from the Gutenberg plugin. See https://github.com/WordPress/gutenberg/pull/80733.

Follow-up to [62571].
Props ntsekouras, mamaduka, tyxla.
Fixes #65728.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r62571 r62941  
    669669         * @since 6.3.0 Added `modified` property to the response.
    670670         * @since 7.1.0 Added `date` property to the response.
     671         * @since 7.1.0 The `modified` property is `null` for templates that have no
     672         *              modification date.
    671673         *
    672674         * @param WP_Block_Template $item    Template instance.
     
    777779
    778780                if ( rest_is_field_included( 'modified', $fields ) ) {
    779                         $data['modified'] = mysql_to_rfc3339( $template->modified );
     781                        /*
     782                         * File-backed templates have no modification date, and `mysql_to_rfc3339()`
     783                         * returns `false` for an empty or malformed value, which the schema does
     784                         * not allow. Return `null` in that case.
     785                         */
     786                        $modified         = mysql_to_rfc3339( $template->modified );
     787                        $data['modified'] = false !== $modified ? $modified : null;
    780788                }
    781789
    782790                if ( rest_is_field_included( 'date', $fields ) ) {
    783                         $data['date'] = mysql_to_rfc3339( $template->date );
     791                        /*
     792                         * File-backed templates have no date, and `mysql_to_rfc3339()` returns
     793                         * `false` for an empty or malformed value, which the schema does not
     794                         * allow. Return `null` in that case.
     795                         */
     796                        $date         = mysql_to_rfc3339( $template->date );
     797                        $data['date'] = false !== $date ? $date : null;
    784798                }
    785799
     
    11551169                                'modified'        => array(
    11561170                                        'description' => __( "The date the template was last modified, in the site's timezone." ),
    1157                                         'type'        => 'string',
     1171                                        'type'        => array( 'string', 'null' ),
    11581172                                        'format'      => 'date-time',
    11591173                                        'context'     => array( 'view', 'edit' ),
     
    11781192                                        ),
    11791193                                ),
    1180                                 'date'        => array(
     1194                                'date'            => array(
    11811195                                        'description' => __( "The date the template was published, in the site's timezone." ),
    11821196                                        'type'        => array( 'string', 'null' ),
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r62571 r62941  
    637637                $this->assertNotWPError( $response, "Fetching an unregistered template shouldn't cause an error." );
    638638                $this->assertSame( 404, $response->get_status(), 'Fetching an unregistered template should return 404.' );
     639        }
     640
     641        /**
     642         * A file-backed template has no publication or modification date, which should
     643         * be exposed as `null` rather than the `false` returned by `mysql_to_rfc3339()`.
     644         *
     645         * @ticket 65728
     646         * @covers WP_REST_Templates_Controller::prepare_item_for_response
     647         */
     648        public function test_get_item_dates_are_null_for_file_backed_template() {
     649                wp_set_current_user( self::$admin_id );
     650                switch_theme( 'block-theme' );
     651
     652                $request  = new WP_REST_Request( 'GET', '/wp/v2/templates/block-theme//page-home' );
     653                $response = rest_get_server()->dispatch( $request );
     654                $data     = $response->get_data();
     655
     656                $this->assertSame( 200, $response->get_status(), 'Fetching a file-backed template should return 200.' );
     657                $this->assertNull( $data['date'], 'The date should be null for a file-backed template.' );
     658                $this->assertNull( $data['modified'], 'The modified date should be null for a file-backed template.' );
    639659        }
    640660
Note: See TracChangeset for help on using the changeset viewer.