Skip to content

Commit

Permalink
Adds prepare_date_response to parse and format the date in the respon…
Browse files Browse the repository at this point in the history
…se object
  • Loading branch information
ramonjd committed Jun 14, 2023
1 parent 85db775 commit ef8ab8a
Showing 1 changed file with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,47 +107,69 @@ public function get_items( $request ) {
return rest_ensure_response( $response );
}

/**
* A direct copy of WP_REST_Revisions_Controller->prepare_date_response().
* Checks the post_date_gmt or modified_gmt and prepare any post or
* modified date for single post output.
*
* @since 6.3.0
*
* @param string $date_gmt GMT publication time.
* @param string|null $date Optional. Local publication time. Default null.
* @return string|null ISO8601/RFC3339 formatted datetime, otherwise null.
*/
protected function prepare_date_response( $date_gmt, $date = null ) {
if ( '0000-00-00 00:00:00' === $date_gmt ) {
return null;
}

if ( isset( $date ) ) {
return mysql_to_rfc3339( $date );
}

return mysql_to_rfc3339( $date_gmt );
}
/**
* Prepares the revision for the REST response.
*
* @since 6.3.0
*
* @param WP_Post $item Post revision object.
* @param WP_Post $post Post revision object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response Response object.
*/
public function prepare_item_for_response( $item, $request ) {
public function prepare_item_for_response( $post, $request ) {
$parent = $this->get_parent( $request['parent'] );
// Retrieves global styles config as JSON.
$raw_revision_config = json_decode( $item->post_content, true );
$raw_revision_config = json_decode( $post->post_content, true );
$config = ( new WP_Theme_JSON( $raw_revision_config, 'custom' ) )->get_raw_data();

// Prepares item data.
$data = array();
$fields = $this->get_fields_for_response( $request );

if ( rest_is_field_included( 'author', $fields ) ) {
$data['author'] = (int) $item->post_author;
$data['author'] = (int) $post->post_author;
}

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

if ( rest_is_field_included( 'date_gmt', $fields ) ) {
$data['date_gmt'] = $item->post_date_gmt;
$data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
}

if ( rest_is_field_included( 'id', $fields ) ) {
$data['id'] = (int) $item->ID;
$data['id'] = (int) $post->ID;
}

if ( rest_is_field_included( 'modified', $fields ) ) {
$data['modified'] = $item->post_modified;
$data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
}

if ( rest_is_field_included( 'modified_gmt', $fields ) ) {
$data['modified_gmt'] = $item->post_modified_gmt;
$data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
}

if ( rest_is_field_included( 'parent', $fields ) ) {
Expand Down

0 comments on commit ef8ab8a

Please sign in to comment.