Skip to content

Commit

Permalink
Sync: Add Jetpack Post Trashed event (#6885)
Browse files Browse the repository at this point in the history
* Sync: Add Jetpack Post Trashed event

We current don't have a good way to track when the post is marked as
trashed.

Adding the jetpack_post_trashed event will let us know when we have
post is marked as trashed.

* Minor fixes
  • Loading branch information
enejb committed Apr 6, 2017
1 parent 01ea0e4 commit a8c8e34
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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

0 comments on commit a8c8e34

Please sign in to comment.