Skip to content

Commit

Permalink
Sync: Allow unlocking callables via action (#5413)
Browse files Browse the repository at this point in the history
* Sync: Allow unlocking callables via action

* Sync: Add docblock for jetpack_sync_unlock_sync_callable in test file
  • Loading branch information
ebinnion authored and bwilczek committed Nov 1, 2016
1 parent 22684bb commit 64468f4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sync/class.jetpack-sync-module-callables.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function init_listeners( $callable ) {
add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) );
}

// Provide a hook so that hosts can send changes to certain callables right away.
// Especially useful when a host uses constants to change home and siteurl.
add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) );

// get_plugins and wp_version
// gets fired when new code gets installed, updates etc.
add_action( 'upgrader_process_complete', array( $this, 'unlock_sync_callable' ) );
Expand Down
45 changes: 45 additions & 0 deletions tests/php/sync/test_class.jetpack-sync-callables.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,51 @@ function test_sync_always_sync_changes_to_home_siteurl_right_away() {
update_option( 'siteurl', $original_siteurl_option );
}

function test_sync_jetpack_sync_unlock_sync_callable_action_allows_syncing_siteurl_changes() {
delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME );
delete_option( Jetpack_Sync_Module_Callables::CALLABLES_CHECKSUM_OPTION_NAME );
$this->setSyncClientDefaults();

$original_home_option = get_option( 'home' );
$original_siteurl_option = get_option( 'siteurl' );

// Let's see if the original values get synced. This will also set the await transient.
$this->sender->do_sync();
$synced_home_url = $synced_value = $this->server_replica_storage->get_callable( 'home_url' );
$synced_site_url = $synced_value = $this->server_replica_storage->get_callable( 'site_url' );

$this->assertEquals( $original_home_option, $synced_home_url );
$this->assertEquals( $original_siteurl_option, $synced_site_url );

$this->server_replica_storage->reset();

// We set the filters here to simulate how setting the WP_HOME and WP_SITEURL constant works.
add_filter( 'option_home', array( $this, 'return_https_site_com_blog' ) );
add_filter( 'option_siteurl', array( $this, 'return_https_site_com_blog' ) );

// By calling this action, we simulate wp_schedule_single_event()

/**
* Used to signal that the callables await transient should be cleared. Clearing the await transient is useful
* in cases where we need to sync values to WordPress.com sooner than the default wait time.
*
* @since 4.4.0
*/
do_action( 'jetpack_sync_unlock_sync_callable' );

$this->sender->do_sync();

$synced_home_url = $synced_value = $this->server_replica_storage->get_callable( 'home_url' );
$synced_site_url = $synced_value = $this->server_replica_storage->get_callable( 'site_url' );

$this->assertEquals( $this->return_https_site_com_blog(), $synced_home_url );
$this->assertEquals( $this->return_https_site_com_blog(), $synced_site_url );

// Cleanup
remove_filter( 'option_home', array( $this, 'return_https_site_com_blog' ) );
remove_filter( 'option_siteurl', array( $this, 'return_https_site_com_blog' ) );
}

function test_scheme_switching_does_not_cause_sync() {
$this->setSyncClientDefaults();
delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME );
Expand Down

0 comments on commit 64468f4

Please sign in to comment.