Skip to content

Commit

Permalink
props @jmdodd - filter out set_object_terms actions that don't perfor…
Browse files Browse the repository at this point in the history
…m any update. Includes unit tests. (#17171)
  • Loading branch information
mdbitz authored Sep 23, 2020
1 parent ff46122 commit e9558f0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/sync/src/modules/class-terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function init_listeners( $callable ) {
add_action( 'delete_term', $callable, 10, 4 );
add_action( 'set_object_terms', $callable, 10, 6 );
add_action( 'deleted_term_relationships', $callable, 10, 2 );
add_filter( 'jetpack_sync_before_enqueue_set_object_terms', array( $this, 'filter_set_object_terms_no_update' ) );
add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_term', array( $this, 'filter_blacklisted_taxonomies' ) );
add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_add_term', array( $this, 'filter_blacklisted_taxonomies' ) );
}
Expand Down Expand Up @@ -255,6 +256,23 @@ public function filter_blacklisted_taxonomies( $args ) {
return $args;
}

/**
* Filter out set_object_terms actions where the terms have not changed.
*
* @param array $args Hook args.
* @return array|boolean False if no change in terms, the original hook args otherwise.
*/
public function filter_set_object_terms_no_update( $args ) {
// There is potential for other plugins to modify args, therefore lets validate # of and types.
// $args[2] is $tt_ids, $args[5] is $old_tt_ids see wp-includes/taxonomy.php L2740.
if ( 6 === count( $args ) && is_array( $args[2] ) && is_array( $args[5] ) ) {
if ( empty( array_diff( $args[2], $args[5] ) ) && empty( array_diff( $args[5], $args[2] ) ) ) {
return false;
}
}
return $args;
}

/**
* Expand the term taxonomy IDs to terms within a hook before they are serialized and sent to the server.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/php/sync/test_class.jetpack-sync-terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ public function test_delete_object_term_relationships() {
$this->assertEquals( $object_terms, $server_object_terms );
}

/**
* Validate filter_set_object_terms_no_update filters out set_object_terms without changes.
*/
public function test_filter_set_object_terms_no_update() {
$anther_term = wp_insert_term( 'mouse', $this->taxonomy );
wp_set_post_terms( $this->post_id, array( $anther_term['term_id'] ), $this->taxonomy, false );

// Sync actions.
$this->sender->do_sync();
// Verify set_object_terms was triggered.
$this->assertNotFalse( $this->server_event_storage->get_most_recent_event( 'set_object_terms' ) );
// Reset event (actions) storage.
$this->server_event_storage->reset();

// Re-trigger and Sync actions.
wp_set_post_terms( $this->post_id, array( $anther_term['term_id'] ), $this->taxonomy, false );
$this->sender->do_sync();
// Verify set_object_terms was *not* triggered.
$this->assertFalse( $this->server_event_storage->get_most_recent_event( 'set_object_terms' ) );
}

public function test_filters_out_blacklisted_taxonomies() {
register_taxonomy( 'bloginfo_rss', 'post' );

Expand Down

0 comments on commit e9558f0

Please sign in to comment.