Revisions: Use wp_get_latest_revision_id_and_total_count() where appropriate.

The function executes an optimized query to get the last revision ID and total count. It was originally introduced for `WP_REST_Posts_Controller::prepare_links()`, and is now used in a few more places in core:

* `register_and_do_post_meta_boxes()`
* `wp_get_post_revisions_url()`
* `wp_update_custom_css_post()`

Follow-up to [53759], [53769], [53778], [53779], [53841].

Props peterwilsoncc, mukesh27, SergeyBiryukov.
Fixes #56279.

git-svn-id: https://develop.svn.wordpress.org/trunk@53842 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2022-08-05 13:37:47 +00:00
parent 81805856e2
commit cc4a86d8bf
3 changed files with 9 additions and 9 deletions

View File

@@ -1459,13 +1459,13 @@ function register_and_do_post_meta_boxes( $post ) {
$publish_callback_args = array( '__back_compat_meta_box' => true );
if ( post_type_supports( $post_type, 'revisions' ) && 'auto-draft' !== $post->post_status ) {
$revisions = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
$revisions = wp_get_latest_revision_id_and_total_count( $post->ID );
// We should aim to show the revisions meta box only when there are revisions.
if ( count( $revisions ) > 1 ) {
if ( ! is_wp_error( $revisions ) && $revisions['count'] > 1 ) {
$publish_callback_args = array(
'revisions_count' => count( $revisions ),
'revision_id' => reset( $revisions ),
'revisions_count' => $revisions['count'],
'revision_id' => $revisions['latest_id'],
'__back_compat_meta_box' => true,
);

View File

@@ -603,14 +603,13 @@ function wp_get_post_revisions_url( $post = 0 ) {
return null;
}
$revisions = wp_get_post_revisions( $post->ID, array( 'posts_per_page' => 1 ) );
$revisions = wp_get_latest_revision_id_and_total_count( $post->ID );
if ( 0 === count( $revisions ) ) {
if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) {
return null;
}
$revision = reset( $revisions );
return get_edit_post_link( $revision );
return get_edit_post_link( $revisions['latest_id'] );
}
/**

View File

@@ -2068,7 +2068,8 @@ function wp_update_custom_css_post( $css, $args = array() ) {
}
// Trigger creation of a revision. This should be removed once #30854 is resolved.
if ( 0 === count( wp_get_post_revisions( $r ) ) ) {
$revisions = wp_get_latest_revision_id_and_total_count( $r );
if ( ! is_wp_error( $revisions ) && 0 === $revisions['count'] ) {
wp_save_post_revision( $r );
}
}