Changeset 62941
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php
r62571 r62941 669 669 * @since 6.3.0 Added `modified` property to the response. 670 670 * @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. 671 673 * 672 674 * @param WP_Block_Template $item Template instance. … … 777 779 778 780 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; 780 788 } 781 789 782 790 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; 784 798 } 785 799 … … 1155 1169 'modified' => array( 1156 1170 'description' => __( "The date the template was last modified, in the site's timezone." ), 1157 'type' => 'string',1171 'type' => array( 'string', 'null' ), 1158 1172 'format' => 'date-time', 1159 1173 'context' => array( 'view', 'edit' ), … … 1178 1192 ), 1179 1193 ), 1180 'date' => array(1194 'date' => array( 1181 1195 'description' => __( "The date the template was published, in the site's timezone." ), 1182 1196 'type' => array( 'string', 'null' ), -
trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php
r62571 r62941 637 637 $this->assertNotWPError( $response, "Fetching an unregistered template shouldn't cause an error." ); 638 638 $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.' ); 639 659 } 640 660
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)