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

Properly capture post revision_id using save_post callback #593

Merged
merged 8 commits into from
Jul 18, 2014
77 changes: 57 additions & 20 deletions connectors/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ class WP_Stream_Connector_Posts extends WP_Stream_Connector {
*/
public static $actions = array(
'transition_post_status',
'save_post',
'deleted_post',
);

/**
* Holds the post transition data to be called in callback_save_post
*
* @var array
*/
protected static $transition_post_status = array();

/**
* Return translated connector label
*
Expand Down Expand Up @@ -50,6 +58,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_ignored_post_types() ) );

Expand Down Expand Up @@ -140,48 +149,48 @@ public static function callback_transition_post_status( $new, $old, $post ) {

if ( in_array( $new, array( 'auto-draft', 'inherit' ) ) ) {
return;
} elseif ( $old == 'auto-draft' && $new == 'draft' ) {
$message = _x(
} elseif ( 'auto-draft' === $old && 'draft' === $new ) {
$summary = _x(
'"%1$s" %2$s drafted',
'1: Post title, 2: Post type singular name',
'stream'
);
$action = 'created';
} elseif ( $old == 'auto-draft' && ( in_array( $new, array( 'publish', 'private' ) ) ) ) {
$message = _x(
} elseif ( 'auto-draft' === $old && ( in_array( $new, array( 'publish', 'private' ) ) ) ) {
$summary = _x(
'"%1$s" %2$s published',
'1: Post title, 2: Post type singular name',
'stream'
);
$action = 'created';
} elseif ( $old == 'draft' && ( in_array( $new, array( 'publish', 'private' ) ) ) ) {
$message = _x(
} elseif ( 'draft' === $old && ( in_array( $new, array( 'publish', 'private' ) ) ) ) {
$summary = _x(
'"%1$s" %2$s published',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( $old == 'publish' && ( in_array( $new, array( 'draft' ) ) ) ) {
$message = _x(
} elseif ( 'publish' === $old && ( in_array( $new, array( 'draft' ) ) ) ) {
$summary = _x(
'"%1$s" %2$s unpublished',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( $new == 'trash' ) {
$message = _x(
} elseif ( 'trash' === $new ) {
$summary = _x(
'"%1$s" %2$s trashed',
'1: Post title, 2: Post type singular name',
'stream'
);
$action = 'trashed';
} elseif ( $old == 'trash' && $new != 'trash' ) {
$message = _x(
} elseif ( 'trash' === $old && 'trash' !== $new ) {
$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 @@ -192,6 +201,32 @@ public static function callback_transition_post_status( $new, $old, $post ) {
$action = 'updated';
}

// Hold the post transition data for use in callback_save_post
self::$transition_post_status = array(
'new_status' => $new,
'old_status' => $old,
'summary' => $summary,
'action' => $action,
);
}

/**
* Log all post saves
*
* We don't log a record until after the post is saved in order to
* properly capture the post revision ID (if one exists).
*
* New post status, old post status, summary and action pulled from
* the $transition_post_status property which is defined in during
* the transition_post_status action which happens before save_post.
*
* @action save_post
*/
public static function callback_save_post( $post_ID, $post, $update ) {
if ( in_array( $post->post_type, self::get_ignored_post_types() ) ) {
return;
}

$revision_id = null;

if ( wp_revisions_enabled( $post ) ) {
Expand All @@ -201,28 +236,30 @@ 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',
'order' => 'DESC',
'orderby' => 'post_date',
)
);

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,
self::$transition_post_status['summary'],
array(
'post_title' => $post->post_title,
'singular_name' => $post_type_name,
'new_status' => $new,
'old_status' => $old,
'new_status' => self::$transition_post_status['new_status'],
'old_status' => self::$transition_post_status['old_status'],
'revision_id' => $revision_id,
),
$post->ID,
array( $post->post_type => $action )
array( $post->post_type => self::$transition_post_status['action'] )
);
}

Expand Down