REST API: Use `wp_get_lastest_revision_id_and_total_count` function in `WP_REST_Posts_Controller` class.
Add new function called `wp_get_lastest_revision_id_and_total_count`, that performs an optimized query to get the last revision and total and use it in `WP_REST_Posts_Controller` class. Props Spacedmonkey, timothyblynjacobs, furi3r, peterwilsoncc. Fixes #55857. Built from https://develop.svn.wordpress.org/trunk@53759 git-svn-id: http://core.svn.wordpress.org/trunk@53318 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
acbd857a4c
commit
ee12e8acc0
|
@ -2024,8 +2024,8 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
|
if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
|
||||||
$revisions = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
|
$revision = wp_get_lastest_revision_id_and_total_count( $post->ID );
|
||||||
$revisions_count = count( $revisions );
|
$revisions_count = ! is_wp_error( $revision ) ? $revision['count'] : 0;
|
||||||
|
|
||||||
$links['version-history'] = array(
|
$links['version-history'] = array(
|
||||||
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
|
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
|
||||||
|
@ -2033,8 +2033,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( $revisions_count > 0 ) {
|
if ( $revisions_count > 0 ) {
|
||||||
$last_revision = array_shift( $revisions );
|
$last_revision = $revision['revision'];
|
||||||
|
|
||||||
$links['predecessor-version'] = array(
|
$links['predecessor-version'] = array(
|
||||||
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
|
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
|
||||||
'id' => $last_revision,
|
'id' => $last_revision,
|
||||||
|
|
|
@ -527,6 +527,57 @@ function wp_get_post_revisions( $post = 0, $args = null ) {
|
||||||
return $revisions;
|
return $revisions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get latest revision and count of revisions for a post.
|
||||||
|
*
|
||||||
|
* @since 6.1.0
|
||||||
|
*
|
||||||
|
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
|
||||||
|
* @return WP_Error|array {
|
||||||
|
* Returns associative array with last revision and total count.
|
||||||
|
*
|
||||||
|
* @type int $revision The last revision post id or 0 if non existing.
|
||||||
|
* @type int $count The total count of revisions for $post_id.
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
function wp_get_lastest_revision_id_and_total_count( $post = null ) {
|
||||||
|
$post = get_post( $post );
|
||||||
|
|
||||||
|
if ( ! $post ) {
|
||||||
|
return new WP_Error( 'revision_error', __( 'Invalid post.' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! wp_revisions_enabled( $post ) ) {
|
||||||
|
return new WP_Error( 'revision_error', __( 'Revisions not enabled.' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$args = array(
|
||||||
|
'post_parent' => $post->ID,
|
||||||
|
'fields' => 'ids',
|
||||||
|
'post_type' => 'revision',
|
||||||
|
'post_status' => 'inherit',
|
||||||
|
'order' => 'DESC',
|
||||||
|
'orderby' => 'date ID',
|
||||||
|
'posts_per_page' => 1,
|
||||||
|
'ignore_sticky_posts' => true,
|
||||||
|
);
|
||||||
|
|
||||||
|
$revision_query = new WP_Query();
|
||||||
|
$revisions = $revision_query->query( $args );
|
||||||
|
|
||||||
|
if ( ! $revisions ) {
|
||||||
|
return array(
|
||||||
|
'revision' => 0,
|
||||||
|
'count' => 0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'revision' => $revisions[0],
|
||||||
|
'count' => $revision_query->found_posts,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the url for viewing and potentially restoring revisions of a given post.
|
* Returns the url for viewing and potentially restoring revisions of a given post.
|
||||||
*
|
*
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '6.1-alpha-53758';
|
$wp_version = '6.1-alpha-53759';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue