Make WordPress Core

Changeset 55992


Ignore:
Timestamp:
06/23/2023 06:27:45 AM (3 years ago)
Author:
isabel_brison
Message:

REST API: return post modified datetime for Templates.

Adds a modified field to the template and template part objects in the rest response for WP_REST_Templates_Controller.

Props ramonopoly, andrewserong, mukesh27, timothyblynjacobs.
Fixes #58540.

Location:
trunk
Files:
5 edited

Legend:

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

    r55881 r55992  
    543543 *
    544544 * @since 5.9.0
     545 * @since 6.3.0 Added `modified` property to template objects.
    545546 * @access private
    546547 *
    … …  
    565566        $template->has_theme_file = true;
    566567        $template->is_custom      = true;
     568        $template->modified       = null;
    567569
    568570        if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
    … …  
    744746 *
    745747 * @since 5.9.0
     748 * @since 6.3.0 Added `modified` property to template objects.
    746749 * @access private
    747750 *
    … …  
    783786        $template->is_custom      = empty( $is_wp_suggestion );
    784787        $template->author         = $post->post_author;
     788        $template->modified       = $post->post_modified;
    785789
    786790        if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
  • trunk/src/wp-includes/class-wp-block-template.php

    r55693 r55992  
    147147         */
    148148        public $area;
     149
     150        /**
     151         * Modified.
     152         *
     153         * @since 6.3.0
     154         * @var string|null
     155         */
     156        public $modified;
    149157}
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r55294 r55992  
    615615         * @since 5.8.0
    616616         * @since 5.9.0 Renamed `$template` to `$item` to match parent class for PHP 8 named parameter support.
     617         * @since 6.3.0 Added `modified` property to the response.
    617618         *
    618619         * @param WP_Block_Template $item    Template instance.
    … …  
    707708                if ( rest_is_field_included( 'area', $fields ) && 'wp_template_part' === $template->type ) {
    708709                        $data['area'] = $template->area;
     710                }
     711
     712                if ( rest_is_field_included( 'modified', $fields ) ) {
     713                        $data['modified'] = mysql_to_rfc3339( $template->modified );
    709714                }
    710715
    … …  
    927932                                        'context'     => array( 'view', 'edit', 'embed' ),
    928933                                ),
     934                                'modified'       => array(
     935                                        'description' => __( "The date the template was last modified, in the site's timezone." ),
     936                                        'type'        => 'string',
     937                                        'format'      => 'date-time',
     938                                        'context'     => array( 'view', 'edit' ),
     939                                        'readonly'    => true,
     940                                ),
    929941                        ),
    930942                );
  • trunk/tests/phpunit/tests/block-template-utils.php

    r55500 r55992  
    102102                $this->assertSame( 'Description of my template', $template->description );
    103103                $this->assertSame( 'wp_template', $template->type );
     104                $this->assertSame( self::$template_post->post_modified, $template->modified );
    104105
    105106                // Test template parts.
    … …  
    118119                $this->assertSame( 'wp_template_part', $template_part->type );
    119120                $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
     121                $this->assertSame( self::$template_part_post->post_modified, $template->modified );
    120122        }
    121123
    … …  
    137139                $this->assertSame( 'Displays single posts on your website unless a custom template has been applied to that post or a dedicated template exists.', $template->description );
    138140                $this->assertSame( 'wp_template', $template->type );
     141                $this->assertEmpty( $template->modified );
    139142
    140143                // Test template parts.
    … …  
    156159                $this->assertSame( 'wp_template_part', $template_part->type );
    157160                $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
     161                $this->assertEmpty( $template_part->modified );
    158162        }
    159163
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r55562 r55992  
    119119                                'is_custom'      => true,
    120120                                'author'         => 0,
     121                                'modified'       => mysql_to_rfc3339( self::$post->post_modified ),
    121122                        ),
    122123                        $this->find_and_normalize_template_by_id( $data, 'default//my_template' )
    … …  
    163164                                'is_custom'      => true,
    164165                                'author'         => 0,
     166                                'modified'       => mysql_to_rfc3339( self::$post->post_modified ),
    165167                        ),
    166168                        $data
    … …  
    199201                                'is_custom'      => true,
    200202                                'author'         => 0,
     203                                'modified'       => mysql_to_rfc3339( self::$post->post_modified ),
    201204                        ),
    202205                        $data
    … …  
    258261                                'is_custom'      => true,
    259262                                'author'         => self::$admin_id,
     263                                'modified'       => mysql_to_rfc3339( $post->post_modified ),
    260264                        ),
    261265                        $data
    … …  
    414418                $response = rest_get_server()->dispatch( $request );
    415419                $data     = $response->get_data();
     420                $modified = get_post( $data['wp_id'] )->post_modified;
    416421                unset( $data['_links'] );
    417422                unset( $data['wp_id'] );
    … …  
    437442                                'is_custom'      => true,
    438443                                'author'         => self::$admin_id,
     444                                'modified'       => mysql_to_rfc3339( $modified ),
    439445                        ),
    440446                        $data
    … …  
    460466                $response = rest_get_server()->dispatch( $request );
    461467                $data     = $response->get_data();
     468                $modified = get_post( $data['wp_id'] )->post_modified;
    462469                unset( $data['_links'] );
    463470                unset( $data['wp_id'] );
    … …  
    483490                                'is_custom'      => false,
    484491                                'author'         => self::$admin_id,
     492                                'modified'       => mysql_to_rfc3339( $modified ),
    485493                        ),
    486494                        $data
    … …  
    510518                $response = rest_get_server()->dispatch( $request );
    511519                $data     = $response->get_data();
     520                $modified = get_post( $data['wp_id'] )->post_modified;
    512521                unset( $data['_links'] );
    513522                unset( $data['wp_id'] );
    … …  
    533542                                'is_custom'      => true,
    534543                                'author'         => self::$admin_id,
     544                                'modified'       => mysql_to_rfc3339( $modified ),
    535545                        ),
    536546                        $data
    … …  
    691701                $data       = $response->get_data();
    692702                $properties = $data['schema']['properties'];
    693                 $this->assertCount( 14, $properties );
     703                $this->assertCount( 15, $properties );
    694704                $this->assertArrayHasKey( 'id', $properties );
    695705                $this->assertArrayHasKey( 'description', $properties );
    … …  
    707717                $this->assertArrayHasKey( 'is_custom', $properties );
    708718                $this->assertArrayHasKey( 'author', $properties );
     719                $this->assertArrayHasKey( 'modified', $properties );
    709720        }
    710721
    … …  
    737748                $request = new WP_REST_Request( 'POST', '/wp/v2/templates' );
    738749                $request->set_body_params( $body_params );
    739                 $response = rest_get_server()->dispatch( $request );
    740                 $data     = $response->get_data();
     750                $response             = rest_get_server()->dispatch( $request );
     751                $data                 = $response->get_data();
     752                $modified             = get_post( $data['wp_id'] )->post_modified;
     753                $expected['modified'] = mysql_to_rfc3339( $modified );
    741754                unset( $data['_links'] );
    742755                unset( $data['wp_id'] );
Note: See TracChangeset for help on using the changeset viewer.