-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement core style of including revisions data on Post response #7495
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -338,6 +338,48 @@ function gutenberg_add_target_schema_to_links( $response, $post, $request ) { | |||
return $response; | ||||
} | ||||
|
||||
/** | ||||
* Include revisions data on post response links. | ||||
* | ||||
* @see https://core.trac.wordpress.org/ticket/44321 | ||||
* | ||||
* @param WP_REST_Response $response WP REST API response of a post. | ||||
* @param WP_Post $post The post being returned. | ||||
* @param WP_REST_Request $request WP REST API request. | ||||
* @return WP_REST_Response Response containing the new links. | ||||
*/ | ||||
function gutenberg_add_revisions_data_to_links( $response, $post, $request ) { | ||||
|
||||
$new_links = array(); | ||||
$orig_links = $response->get_links(); | ||||
|
||||
if ( ! empty( $orig_links['version-history'] ) ) { | ||||
$version_history_link = array_shift( $orig_links['version-history'] ); | ||||
// 'version-history' already exists and we don't want to duplicate it. | ||||
$response->remove_link( 'version-history' ); | ||||
|
||||
$revisions = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) ); | ||||
$revisions_count = count( $revisions ); | ||||
|
||||
$new_links['version-history'] = array( | ||||
'href' => $version_history_link['href'], | ||||
'count' => $revisions_count, | ||||
); | ||||
|
||||
if ( $revisions_count > 0 ) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it be greater than 1? I think we don't render the link if there is only 1 revision. Yep, confirmed:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's a client specific detail. Seems to me the REST API should provide the raw value (maybe even if it were There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming it is still a correct url, is fine as is. |
||||
$last_revision = array_shift( $revisions ); | ||||
|
||||
$new_links['predecessor-version'] = array( | ||||
'href' => $version_history_link['href'] . '/' . $last_revision, | ||||
'id' => $last_revision, | ||||
); | ||||
} | ||||
} | ||||
|
||||
$response->add_links( $new_links ); | ||||
return $response; | ||||
} | ||||
|
||||
/** | ||||
* Whenever a post type is registered, ensure we're hooked into it's WP REST API response. | ||||
* | ||||
|
@@ -348,6 +390,7 @@ function gutenberg_register_post_prepare_functions( $post_type ) { | |||
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_permalink_template_to_posts', 10, 3 ); | ||||
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_block_format_to_post_content', 10, 3 ); | ||||
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_target_schema_to_links', 10, 3 ); | ||||
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_revisions_data_to_links', 10, 3 ); | ||||
add_filter( "rest_{$post_type}_collection_params", 'gutenberg_filter_post_collection_parameters', 10, 2 ); | ||||
add_filter( "rest_{$post_type}_query", 'gutenberg_filter_post_query_arguments', 10, 2 ); | ||||
return $post_type; | ||||
|
@@ -365,46 +408,6 @@ function gutenberg_register_taxonomy_prepare_functions( $taxonomy ) { | |||
} | ||||
add_filter( 'registered_taxonomy', 'gutenberg_register_taxonomy_prepare_functions' ); | ||||
|
||||
/** | ||||
* Gets revisions details for the selected post. | ||||
* | ||||
* @since 1.6.0 | ||||
* | ||||
* @param array $post The post object from the response. | ||||
* @return array|null Revisions details or null when no revisions present. | ||||
*/ | ||||
function gutenberg_get_post_revisions( $post ) { | ||||
$revisions = wp_get_post_revisions( $post['id'] ); | ||||
$revisions_count = count( $revisions ); | ||||
if ( 0 === $revisions_count ) { | ||||
return null; | ||||
} | ||||
|
||||
$last_revision = array_shift( $revisions ); | ||||
|
||||
return array( | ||||
'count' => $revisions_count, | ||||
'last_id' => $last_revision->ID, | ||||
); | ||||
} | ||||
|
||||
/** | ||||
* Adds the custom field `revisions` to the REST API response of post. | ||||
* | ||||
* TODO: This is a temporary solution. Next step would be to find a solution that is limited to the editor. | ||||
* | ||||
* @since 1.6.0 | ||||
*/ | ||||
function gutenberg_register_rest_api_post_revisions() { | ||||
register_rest_field( get_post_types( '', 'names' ), | ||||
'revisions', | ||||
array( | ||||
'get_callback' => 'gutenberg_get_post_revisions', | ||||
) | ||||
); | ||||
} | ||||
add_action( 'rest_api_init', 'gutenberg_register_rest_api_post_revisions' ); | ||||
|
||||
/** | ||||
* Ensure that the wp-json index contains the 'theme-supports' setting as | ||||
* part of its site info elements. | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: If intent is to merge in the
count
attribute, may be more clear if usedarray_merge
to indicate as such.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$orig_links['version-history']
has a different format and can't be used directly.