Make WordPress Core

Changeset 62571


Ignore:
Timestamp:
06/29/2026 12:21:10 PM (6 weeks ago)
Author:
ntsekouras
Message:

Editor: Add a date field to templates and template parts.

Templates and template parts previously exposed only a modified date.
This adds a date property to the WP_Block_Template class, populated
from the post's publish date (post_date), and exposes it as a
read-only date field through the templates REST API controller.

Having the publish date available alongside modified simplifies
revisions handling for these post types, removing workarounds that
previously relied on the modified date alone.

Props ntsekouras, audrasjb, wildworks, mamaduka.
Fixes #65049.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r62550 r62571  
    596596 * @since 5.9.0
    597597 * @since 6.3.0 Added `modified` property to template objects.
     598 * @since 7.1.0 Added `date` property to template objects.
    598599 * @access private
    599600 *
     
    618619        $template->is_custom      = true;
    619620        $template->modified       = null;
     621        $template->date           = null;
    620622
    621623        if ( 'wp_template' === $template_type ) {
     
    868870        $template->author         = $post->post_author;
    869871        $template->modified       = $post->post_modified;
     872        $template->date           = $post->post_date;
    870873
    871874        if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
  • trunk/src/wp-includes/class-wp-block-template.php

    r59073 r62571  
    163163         */
    164164        public $modified;
     165
     166        /**
     167         * Date.
     168         *
     169         * @since 7.1.0
     170         * @var string|null
     171         */
     172        public $date;
    165173}
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r61572 r62571  
    668668         * @since 5.9.0 Renamed `$template` to `$item` to match parent class for PHP 8 named parameter support.
    669669         * @since 6.3.0 Added `modified` property to the response.
     670         * @since 7.1.0 Added `date` property to the response.
    670671         *
    671672         * @param WP_Block_Template $item    Template instance.
     
    777778                if ( rest_is_field_included( 'modified', $fields ) ) {
    778779                        $data['modified'] = mysql_to_rfc3339( $template->modified );
     780                }
     781
     782                if ( rest_is_field_included( 'date', $fields ) ) {
     783                        $data['date'] = mysql_to_rfc3339( $template->date );
    779784                }
    780785
     
    11731178                                        ),
    11741179                                ),
     1180                                'date'        => array(
     1181                                        'description' => __( "The date the template was published, in the site's timezone." ),
     1182                                        'type'        => array( 'string', 'null' ),
     1183                                        'format'      => 'date-time',
     1184                                        'context'     => array( 'view', 'edit' ),
     1185                                        'readonly'    => true,
     1186                                ),
    11751187                        ),
    11761188                );
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php

    r61178 r62571  
    683683                $this->assertArrayHasKey( 'author', $properties, 'author key should exist in properties.' );
    684684                $this->assertArrayHasKey( 'modified', $properties, 'modified key should exist in properties.' );
     685                $this->assertArrayHasKey( 'date', $properties, 'date key should exist in properties.' );
    685686                $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
    686687                $this->assertArrayHasKey( 'author_text', $properties, 'author_text key should exist in properties.' );
     
    701702                                'templates',
    702703                                self::TEST_THEME . '//' . self::TEMPLATE_NAME,
    703                                 19,
     704                                20,
    704705                                array( 'is_custom', 'plugin' ),
    705706                        ),
     
    707708                                'template-parts',
    708709                                self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME,
    709                                 18,
     710                                19,
    710711                                array( 'area' ),
    711712                        ),
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php

    r61178 r62571  
    929929                $this->assertArrayHasKey( 'author', $properties, 'author key should exist in properties.' );
    930930                $this->assertArrayHasKey( 'modified', $properties, 'modified key should exist in properties.' );
     931                $this->assertArrayHasKey( 'date', $properties, 'date key should exist in properties.' );
    931932                $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
    932933                $this->assertArrayHasKey( 'author_text', $properties, 'author_text key should exist in properties.' );
     
    948949                                'templates',
    949950                                self::TEST_THEME . '//' . self::TEMPLATE_NAME,
    950                                 19,
     951                                20,
    951952                                array( 'is_custom', 'plugin' ),
    952953                        ),
     
    954955                                'template-parts',
    955956                                self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME,
    956                                 18,
     957                                19,
    957958                                array( 'area' ),
    958959                        ),
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r60729 r62571  
    169169                                'author'          => 0,
    170170                                'modified'        => mysql_to_rfc3339( self::$template_post->post_modified ),
     171                                'date'            => mysql_to_rfc3339( self::$template_post->post_date ),
    171172                                'author_text'     => 'Test Blog',
    172173                                'original_source' => 'site',
     
    248249                                'author'          => 0,
    249250                                'modified'        => mysql_to_rfc3339( self::$template_post->post_modified ),
     251                                'date'            => mysql_to_rfc3339( self::$template_post->post_date ),
    250252                                'author_text'     => 'Test Blog',
    251253                                'original_source' => 'site',
     
    305307                                'author'          => 0,
    306308                                'modified'        => mysql_to_rfc3339( self::$template_post->post_modified ),
     309                                'date'            => mysql_to_rfc3339( self::$template_post->post_date ),
    307310                                'author_text'     => 'Test Blog',
    308311                                'original_source' => 'site',
     
    356359                                'author'          => 0,
    357360                                'modified'        => mysql_to_rfc3339( self::$template_post->post_modified ),
     361                                'date'            => mysql_to_rfc3339( self::$template_post->post_date ),
    358362                                'author_text'     => 'Test Blog',
    359363                                'original_source' => 'site',
     
    405409                                'author'          => 0,
    406410                                'modified'        => mysql_to_rfc3339( self::$template_post->post_modified ),
     411                                'date'            => mysql_to_rfc3339( self::$template_post->post_date ),
    407412                                'author_text'     => 'Test Blog',
    408413                                'original_source' => 'site',
     
    470475                                'author_text'     => $author_name,
    471476                                'original_source' => 'user',
     477                                'date'            => mysql_to_rfc3339( $post->post_date ),
    472478                        ),
    473479                        $data
     
    674680                $data     = $response->get_data();
    675681                $modified = get_post( $data['wp_id'] )->post_modified;
     682                $date     = get_post( $data['wp_id'] )->post_date;
    676683                unset( $data['_links'] );
    677684                unset( $data['wp_id'] );
     
    700707                                'author'          => self::$admin_id,
    701708                                'modified'        => mysql_to_rfc3339( $modified ),
     709                                'date'            => mysql_to_rfc3339( $date ),
    702710                                'author_text'     => $author_name,
    703711                                'original_source' => 'user',
     
    726734                $data     = $response->get_data();
    727735                $modified = get_post( $data['wp_id'] )->post_modified;
     736                $date     = get_post( $data['wp_id'] )->post_date;
    728737                unset( $data['_links'] );
    729738                unset( $data['wp_id'] );
     
    752761                                'author'          => self::$admin_id,
    753762                                'modified'        => mysql_to_rfc3339( $modified ),
     763                                'date'            => mysql_to_rfc3339( $date ),
    754764                                'author_text'     => $author_name,
    755765                                'original_source' => 'user',
     
    782792                $data     = $response->get_data();
    783793                $modified = get_post( $data['wp_id'] )->post_modified;
     794                $date     = get_post( $data['wp_id'] )->post_date;
    784795                unset( $data['_links'] );
    785796                unset( $data['wp_id'] );
     
    808819                                'author'          => self::$admin_id,
    809820                                'modified'        => mysql_to_rfc3339( $modified ),
     821                                'date'            => mysql_to_rfc3339( $date ),
    810822                                'author_text'     => $author_name,
    811823                                'original_source' => 'user',
     
    968980                $data       = $response->get_data();
    969981                $properties = $data['schema']['properties'];
    970                 $this->assertCount( 18, $properties );
     982                $this->assertCount( 19, $properties );
    971983                $this->assertArrayHasKey( 'id', $properties );
    972984                $this->assertArrayHasKey( 'description', $properties );
     
    985997                $this->assertArrayHasKey( 'author', $properties );
    986998                $this->assertArrayHasKey( 'modified', $properties );
     999                $this->assertArrayHasKey( 'date', $properties );
    9871000                $this->assertArrayHasKey( 'author_text', $properties );
    9881001                $this->assertArrayHasKey( 'original_source', $properties );
     
    10211034                $data                        = $response->get_data();
    10221035                $modified                    = get_post( $data['wp_id'] )->post_modified;
     1036                $date                        = get_post( $data['wp_id'] )->post_date;
    10231037                $expected['modified']        = mysql_to_rfc3339( $modified );
     1038                $expected['date']            = mysql_to_rfc3339( $date );
    10241039                $expected['author_text']     = get_user_by( 'id', self::$admin_id )->get( 'display_name' );
    10251040                $expected['original_source'] = 'user';
Note: See TracChangeset for help on using the changeset viewer.