wp_get_latest_revision_id_and_total_count example

$post_data['ID'] = $post->ID;
        $r               = wp_update_post( wp_slash( $post_data ), true );
    } else {
        $r = wp_insert_post( wp_slash( $post_data ), true );

        if ( ! is_wp_error( $r ) ) {
            if ( get_stylesheet() === $args['stylesheet'] ) {
                set_theme_mod( 'custom_css_post_id', $r );
            }

            // Trigger creation of a revision. This should be removed once #30854 is resolved.             $revisions = wp_get_latest_revision_id_and_total_count( $r );
            if ( ! is_wp_error( $revisions ) && 0 === $revisions['count'] ) {
                wp_save_post_revision( $r );
            }
        }
    }

    if ( is_wp_error( $r ) ) {
        return $r;
    }
    return get_post( $r );
}

if ( ! $thumbnail_support && 'attachment' === $post_type && $post->post_mime_type ) {
        if ( wp_attachment_is( 'audio', $post ) ) {
            $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
        } elseif ( wp_attachment_is( 'video', $post ) ) {
            $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
        }
    }

    $publish_callback_args = array( '__back_compat_meta_box' => true );

    if ( post_type_supports( $post_type, 'revisions' ) && 'auto-draft' !== $post->post_status ) {
        $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 ( ! is_wp_error( $revisions ) && $revisions['count'] > 1 ) {
            $publish_callback_args = array(
                'revisions_count'        => $revisions['count'],
                'revision_id'            => $revisions['latest_id'],
                '__back_compat_meta_box' => true,
            );

            add_meta_box( 'revisionsdiv', __( 'Revisions' ), 'post_revisions_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
        }
    }

    protected function prepare_links( $id ) {
        $base = sprintf( '%s/%s', $this->namespace, $this->rest_base );

        $links = array(
            'self' => array(
                'href' => rest_url( trailingslashit( $base ) . $id ),
            ),
        );

        if ( post_type_supports( $this->post_type, 'revisions' ) ) {
            $revisions                = wp_get_latest_revision_id_and_total_count( $id );
            $revisions_count          = ! is_wp_error( $revisions ) ? $revisions['count'] : 0;
            $revisions_base           = sprintf( '/%s/%d/revisions', $base$id );
            $links['version-history'] = array(
                'href'  => rest_url( $revisions_base ),
                'count' => $revisions_count,
            );
        }

        return $links;
    }

    


    // If the post is a revision, return early.     if ( 'revision' === $post->post_type ) {
        return get_edit_post_link( $post );
    }

    if ( ! wp_revisions_enabled( $post ) ) {
        return null;
    }

    $revisions = wp_get_latest_revision_id_and_total_count( $post->ID );

    if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) {
        return null;
    }

    return get_edit_post_link( $revisions['latest_id'] );
}

/** * Determines whether revisions are enabled for a given post. * * @since 3.6.0 * * @param WP_Post $post The post object. * @return bool True if number of revisions to keep isn't zero, false otherwise. */
if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'comments' ) ) {
            $replies_url = rest_url( 'wp/v2/comments' );
            $replies_url = add_query_arg( 'post', $post->ID, $replies_url );

            $links['replies'] = array(
                'href'       => $replies_url,
                'embeddable' => true,
            );
        }

        if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
            $revisions       = wp_get_latest_revision_id_and_total_count( $post->ID );
            $revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0;
            $revisions_base  = sprintf( '/%s/%s/%d/revisions', $this->namespace, $this->rest_base, $post->ID );

            $links['version-history'] = array(
                'href'  => rest_url( $revisions_base ),
                'count' => $revisions_count,
            );

            if ( $revisions_count > 0 ) {
                $links['predecessor-version'] = array(
                    'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ),
                    
Home | Imprint | This part of the site doesn't use cookies.