ID ); } ); return self::latest( $post->ID ); } /** * The id of the post's latest revision, autosaves aside. * * @param int $post_id Post id. * @return int|null */ public static function latest( $post_id ) { foreach ( self::all( $post_id ) as $revision ) { return $revision->ID; } return null; } /** * The first revision a change created on the post. * * @param int $post_id Post id. * @param string $change_id Blaze's change id. * @return int|null */ public static function labelled( $post_id, $change_id ) { $ids = get_posts( array( 'post_type' => 'revision', 'post_status' => 'inherit', 'post_parent' => (int) $post_id, 'meta_key' => RevisionLabeler::META_KEY, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Only used to undo a change. 'meta_value' => (string) $change_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'orderby' => 'ID', 'order' => 'ASC', 'posts_per_page' => 1, 'fields' => 'ids', 'no_found_rows' => true, ) ); return $ids ? (int) $ids[0] : null; } /** * The revision just before another one: the post as it was before the * change that created that revision. * * @param int $post_id Post id. * @param int $revision_id A revision of the post. * @return int|null */ public static function before( $post_id, $revision_id ) { foreach ( self::all( $post_id ) as $revision ) { if ( $revision->ID < $revision_id ) { return $revision->ID; } } return null; } /** * The post's revisions, newest first, without autosaves. * * @param int $post_id Post id. * @return \WP_Post[] */ private static function all( $post_id ) { $revisions = wp_get_post_revisions( $post_id, array( 'orderby' => 'ID', 'order' => 'DESC', ) ); return array_values( array_filter( $revisions, static function ( $revision ) { return ! wp_is_post_autosave( $revision ); } ) ); } }