Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix post revision action link by getting the adjacent revision #659

Merged
merged 2 commits into from
Nov 19, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 61 additions & 12 deletions connectors/class-wp-stream-connector-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) );

Expand Down Expand Up @@ -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 ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fjarrett get_adjacent_post_revision already does that check internally. Also i think we should first check if $revision_id is empty or not, so as not to make the extra function call if it was empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shadyvb Nice suggestions. How's this? a0e42d2

$links[ esc_html__( 'Revision', 'stream' ) ] = get_edit_post_link( $revision_id );
}
}
Expand Down Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}

}