From f9b30d542592e80fccd91c9ccaf368fea8ff9614 Mon Sep 17 00:00:00 2001 From: Frankie Jarrett Date: Thu, 13 Nov 2014 21:17:04 -0600 Subject: [PATCH 1/2] Fix post revision action link by getting the adjacent revision --- .../class-wp-stream-connector-posts.php | 73 ++++++++++++++++--- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/connectors/class-wp-stream-connector-posts.php b/connectors/class-wp-stream-connector-posts.php index cb909a397..471c4499e 100644 --- a/connectors/class-wp-stream-connector-posts.php +++ b/connectors/class-wp-stream-connector-posts.php @@ -50,6 +50,7 @@ public static function get_action_labels() { */ public static function get_context_labels() { global $wp_post_types; + $post_types = wp_filter_object_list( $wp_post_types, array(), null, 'label' ); $post_types = array_diff_key( $post_types, array_flip( self::get_excluded_post_types() ) ); @@ -106,7 +107,10 @@ public static function action_links( $links, $record ) { $links[ esc_html__( 'View', 'stream' ) ] = $view_link; } - if ( $revision_id = wp_stream_get_meta( $record, 'revision_id', true ) ) { + $revision_id = absint( wp_stream_get_meta( $record, 'revision_id', true ) ); + $revision_id = self::get_adjacent_post_revision( $revision_id, false ); + + if ( wp_is_post_revision( $revision_id ) ) { $links[ esc_html__( 'Revision', 'stream' ) ] = get_edit_post_link( $revision_id ); } } @@ -143,47 +147,47 @@ public static function callback_transition_post_status( $new, $old, $post ) { if ( in_array( $new, array( 'auto-draft', 'inherit' ) ) ) { return; } elseif ( 'auto-draft' === $old && 'draft' === $new ) { - $message = _x( + $summary = _x( '"%1$s" %2$s drafted', '1: Post title, 2: Post type singular name', 'stream' ); $action = 'created'; } elseif ( 'auto-draft' === $old && ( in_array( $new, array( 'publish', 'private' ) ) ) ) { - $message = _x( + $summary = _x( '"%1$s" %2$s published', '1: Post title, 2: Post type singular name', 'stream' ); $action = 'created'; } elseif ( 'draft' === $old && ( in_array( $new, array( 'publish', 'private' ) ) ) ) { - $message = _x( + $summary = _x( '"%1$s" %2$s published', '1: Post title, 2: Post type singular name', 'stream' ); } elseif ( 'publish' === $old && ( in_array( $new, array( 'draft' ) ) ) ) { - $message = _x( + $summary = _x( '"%1$s" %2$s unpublished', '1: Post title, 2: Post type singular name', 'stream' ); } elseif ( 'trash' === $new ) { - $message = _x( + $summary = _x( '"%1$s" %2$s trashed', '1: Post title, 2: Post type singular name', 'stream' ); $action = 'trashed'; } elseif ( 'trash' === $old && 'trash' !== $new ) { - $message = _x( + $summary = _x( '"%1$s" %2$s restored from trash', '1: Post title, 2: Post type singular name', 'stream' ); $action = 'untrashed'; } else { - $message = _x( + $summary = _x( '"%1$s" %2$s updated', '1: Post title, 2: Post type singular name', 'stream' @@ -203,19 +207,21 @@ public static function callback_transition_post_status( $new, $old, $post ) { 'post_status' => 'inherit', 'post_parent' => $post->ID, 'posts_per_page' => 1, - 'order' => 'desc', - 'fields' => 'ids', + 'orderby' => 'post_date', + 'order' => 'DESC', ) ); + if ( $revision ) { - $revision_id = $revision[0]; + $revision = array_values( $revision ); + $revision_id = $revision[0]->ID; } } $post_type_name = strtolower( self::get_post_type_name( $post->post_type ) ); self::log( - $message, + $summary, array( 'post_title' => $post->post_title, 'singular_name' => $post_type_name, @@ -298,4 +304,47 @@ public static function get_post_type_name( $post_type_slug ) { return $name; } + /** + * Get an adjacent post revision ID + * + * @param int $revision_id + * @param bool $previous + * + * @return int $revision_id + */ + public static function get_adjacent_post_revision( $revision_id, $previous = true ) { + if ( ! wp_is_post_revision( $revision_id ) ) { + return false; + } + + $revision = wp_get_post_revision( $revision_id ); + $operator = ( $previous ) ? '<' : '>'; + $order = ( $previous ) ? 'DESC' : 'ASC'; + + global $wpdb; + + $revision_id = $wpdb->get_var( + $wpdb->prepare( " + SELECT p.ID + FROM $wpdb->posts AS p + WHERE p.post_date {$operator} %s + AND p.post_type = 'revision' + AND p.post_parent = %d + ORDER BY p.post_date {$order} + LIMIT 1 + ", + $revision->post_date, + $revision->post_parent + ) + ); + + $revision_id = absint( $revision_id ); + + if ( ! wp_is_post_revision( $revision_id ) ) { + return false; + } + + return $revision_id; + } + } From a0e42d2631aabd15e6e3edbe816b3656fe4d3a7e Mon Sep 17 00:00:00 2001 From: Frankie Jarrett Date: Tue, 18 Nov 2014 15:33:26 -0600 Subject: [PATCH 2/2] Remove duplicate function call, bail early if revision_id is empty --- connectors/class-wp-stream-connector-posts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/connectors/class-wp-stream-connector-posts.php b/connectors/class-wp-stream-connector-posts.php index 471c4499e..9fda54116 100644 --- a/connectors/class-wp-stream-connector-posts.php +++ b/connectors/class-wp-stream-connector-posts.php @@ -110,7 +110,7 @@ public static function action_links( $links, $record ) { $revision_id = absint( wp_stream_get_meta( $record, 'revision_id', true ) ); $revision_id = self::get_adjacent_post_revision( $revision_id, false ); - if ( wp_is_post_revision( $revision_id ) ) { + if ( $revision_id ) { $links[ esc_html__( 'Revision', 'stream' ) ] = get_edit_post_link( $revision_id ); } } @@ -313,7 +313,7 @@ public static function get_post_type_name( $post_type_slug ) { * @return int $revision_id */ public static function get_adjacent_post_revision( $revision_id, $previous = true ) { - if ( ! wp_is_post_revision( $revision_id ) ) { + if ( empty( $revision_id ) || ! wp_is_post_revision( $revision_id ) ) { return false; }