Skip to content

Commit

Permalink
Global styles revisions: remove human time diff and custom author fie…
Browse files Browse the repository at this point in the history
…lds from the API response (#50234)

* This commit:
- removes the human friendly time diff to make the response appear a little closer to the regular post revisions object
- the assumption is that the consumer can format the raw dates as they wish
- updates tests

* This commit:
- removes the specific author/user properties from the rest controller
- updates tests

* Il LINTERTORE, SEI INCREDIBILE!
  • Loading branch information
ramonjd authored May 2, 2023
1 parent 86bdf73 commit f9df0da
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,6 @@ public function prepare_item_for_response( $item, $request ) {
$raw_revision_config = json_decode( $item->post_content, true );
$config = ( new WP_Theme_JSON_Gutenberg( $raw_revision_config, 'custom' ) )->get_raw_data();

// Builds human-friendly date.
$now_gmt = time();
$modified = strtotime( $item->post_modified );
$modified_gmt = strtotime( $item->post_modified_gmt . ' +0000' );
/* translators: %s: Human-readable time difference. */
$time_ago = sprintf( __( '%s ago', 'gutenberg' ), human_time_diff( $modified_gmt, $now_gmt ) );
$date_short = date_i18n( _x( 'j M @ H:i', 'revision date short format', 'gutenberg' ), $modified );

// Prepares item data.
$data = array();
$fields = $this->get_fields_for_response( $request );
Expand All @@ -138,28 +130,10 @@ public function prepare_item_for_response( $item, $request ) {
$data['author'] = (int) $item->post_author;
}

if ( rest_is_field_included( 'author_avatar_url', $fields ) ) {
$data['author_avatar_url'] = get_avatar_url(
$item->post_author,
array(
'size' => 24,
)
);
}

if ( rest_is_field_included( 'author_display_name', $fields ) ) {
$data['author_display_name'] = get_the_author_meta( 'display_name', $item->post_author );
}

if ( rest_is_field_included( 'date', $fields ) ) {
$data['date'] = $item->post_date;
}

if ( rest_is_field_included( 'date_display', $fields ) ) {
/* translators: 1: Human-readable time difference, 2: short date combined to show rendered revision date. */
$data['date_display'] = sprintf( __( '%1$s (%2$s)', 'gutenberg' ), $time_ago, $date_short );
}

if ( rest_is_field_included( 'date_gmt', $fields ) ) {
$data['date_gmt'] = $item->post_date_gmt;
}
Expand Down Expand Up @@ -218,71 +192,53 @@ public function get_item_schema() {
* Adds settings and styles from the WP_REST_Revisions_Controller item fields.
* Leaves out GUID as global styles shouldn't be accessible via URL.
*/
'author' => array(
'author' => array(
'description' => __( 'The ID for the author of the revision.', 'gutenberg' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
'date' => array(
'date' => array(
'description' => __( "The date the revision was published, in the site's timezone.", 'gutenberg' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit', 'embed' ),
),
'date_gmt' => array(
'date_gmt' => array(
'description' => __( 'The date the revision was published, as GMT.', 'gutenberg' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'id' => array(
'id' => array(
'description' => __( 'Unique identifier for the revision.', 'gutenberg' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
'modified' => array(
'modified' => array(
'description' => __( "The date the revision was last modified, in the site's timezone.", 'gutenberg' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'modified_gmt' => array(
'modified_gmt' => array(
'description' => __( 'The date the revision was last modified, as GMT.', 'gutenberg' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'parent' => array(
'parent' => array(
'description' => __( 'The ID for the parent of the revision.', 'gutenberg' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),

// Adds custom global styles revisions schema.
'author_display_name' => array(
'description' => __( 'The display name of the author.', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),

'author_avatar_url' => array(
'description' => __( 'A URL to the avatar image of the author', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),

'date_display' => array(
'description' => __( 'A human-friendly rendering of the date', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
// Adds settings and styles from the WP_REST_Global_Styles_Controller parent schema.
'styles' => array(
'styles' => array(
'description' => __( 'Global styles.', 'gutenberg' ),
'type' => array( 'object' ),
'context' => array( 'view', 'edit' ),
),
'settings' => array(
'settings' => array(
'description' => __( 'Global settings.', 'gutenberg' ),
'type' => array( 'object' ),
'context' => array( 'view', 'edit' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,12 @@ public function test_get_items() {
// Dates.
$this->assertArrayHasKey( 'date', $data[0], 'Check that an date key exists' );
$this->assertArrayHasKey( 'date_gmt', $data[0], 'Check that an date_gmt key exists' );
$this->assertArrayHasKey( 'date_display', $data[0], 'Check that an date_display key exists' );
$this->assertArrayHasKey( 'modified', $data[0], 'Check that an modified key exists' );
$this->assertArrayHasKey( 'modified_gmt', $data[0], 'Check that an modified_gmt key exists' );
$this->assertArrayHasKey( 'modified_gmt', $data[0], 'Check that an modified_gmt key exists' );

// Author information.
$this->assertEquals( self::$admin_id, $data[0]['author'], 'Check that author id returns expected value' );
$this->assertEquals( get_the_author_meta( 'display_name', self::$admin_id ), $data[0]['author_display_name'], 'Check that author display_name returns expected value' );
$this->assertIsString(
$data[0]['author_avatar_url'],
'Check that author avatar_url returns expected value type'
);

// Global styles.
$this->assertEquals(
Expand Down Expand Up @@ -143,6 +137,7 @@ public function test_get_items() {

$this->assertCount( 2, $data, 'Check that two revisions exists' );
$this->assertEquals( self::$second_admin_id, $data[0]['author'], 'Check that second author id returns expected value' );
$this->assertEquals( self::$admin_id, $data[1]['author'], 'Check that second author id returns expected value' );
}

/**
Expand All @@ -153,17 +148,14 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 12, $properties, 'Schema properties array does not have exactly 4 elements' );
$this->assertCount( 9, $properties, 'Schema properties array does not have exactly 9 elements' );
$this->assertArrayHasKey( 'id', $properties, 'Schema properties array does not have "id" key' );
$this->assertArrayHasKey( 'styles', $properties, 'Schema properties array does not have "styles" key' );
$this->assertArrayHasKey( 'settings', $properties, 'Schema properties array does not have "settings" key' );
$this->assertArrayHasKey( 'parent', $properties, 'Schema properties array does not have "parent" key' );
$this->assertArrayHasKey( 'author', $properties, 'Schema properties array does not have "author" key' );
$this->assertArrayHasKey( 'author_display_name', $properties, 'Schema properties array does not have "author_display_name" key' );
$this->assertArrayHasKey( 'author_avatar_url', $properties, 'Schema properties array does not have "author_avatar_url" key' );
$this->assertArrayHasKey( 'date', $properties, 'Schema properties array does not have "date" key' );
$this->assertArrayHasKey( 'date_gmt', $properties, 'Schema properties array does not have "date_gmt" key' );
$this->assertArrayHasKey( 'date_display', $properties, 'Schema properties array does not have "date_display" key' );
$this->assertArrayHasKey( 'modified', $properties, 'Schema properties array does not have "modified" key' );
$this->assertArrayHasKey( 'modified_gmt', $properties, 'Schema properties array does not have "modified_gmt" key' );
}
Expand Down

0 comments on commit f9df0da

Please sign in to comment.