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

Sync: Add Jetpack Post Trashed event #6885

Merged
merged 2 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions sync/class.jetpack-sync-module-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module {

private $just_published = array();
private $just_trashed = array();
private $action_handler;

public function name() {
Expand Down Expand Up @@ -33,6 +34,8 @@ public function init_listeners( $callable ) {

add_action( 'deleted_post', $callable, 10 );
add_action( 'jetpack_published_post', $callable, 10, 2 );
add_action( 'jetpack_trashed_post', $callable, 10, 2 );

add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) );

Expand Down Expand Up @@ -241,11 +244,16 @@ public function save_published( $new_status, $old_status, $post ) {
if ( 'publish' === $new_status && 'publish' !== $old_status ) {
$this->just_published[] = $post->ID;
}

if ( 'trash' === $new_status && 'trash' !== $old_status ) {
$this->just_trashed[] = $post->ID;
}
}

public function wp_insert_post( $post_ID, $post, $update ) {
call_user_func( $this->action_handler, $post_ID, $post, $update );
$this->send_published( $post_ID, $post );
$this->send_trashed( $post_ID, $post );
}

public function send_published( $post_ID, $post ) {
Expand Down Expand Up @@ -281,6 +289,28 @@ public function send_published( $post_ID, $post ) {
$this->just_published = array_diff( $this->just_published, array( $post_ID ) );
}

public function send_trashed( $post_ID, $post ) {
if ( ! in_array( $post_ID, $this->just_trashed ) ) {
return;
}

// Post revisions cause race conditions where this send_published add the action before the actual post gets synced
if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
return;
}

/**
* Action that gets synced when a post type gets trashed.
*
* @since 4.9.0
*
* @param int $post_ID
*/
do_action( 'jetpack_trashed_post', $post_ID );

$this->just_trashed = array_diff( $this->just_trashed, array( $post_ID ) );
}

public function expand_post_ids( $args ) {
$post_ids = $args[0];

Expand Down
11 changes: 11 additions & 0 deletions tests/php/sync/test_class.jetpack-sync-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ public function test_trash_post_trashes_data() {
wp_delete_post( $this->post->ID );

$this->sender->do_sync();
$event = $this->server_event_storage->get_most_recent_event( 'jetpack_trashed_post' );

$this->assertTrue( (bool) $event );
$this->server_event_storage->reset();

$this->assertEquals( 0, $this->server_replica_storage->post_count( 'publish' ) );
$this->assertEquals( 1, $this->server_replica_storage->post_count( 'trash' ) );

wp_delete_post( $this->post->ID );
$this->sender->do_sync();

// Since the post status is not changing here we don't expect the post to be trashed again.
$event = $this->server_event_storage->get_most_recent_event( 'jetpack_trashed_post' );
$this->assertFalse( (bool) $event );
}

public function test_delete_post_deletes_data() {
Expand Down