Skip to content

Commit c9d244a

Browse files
committed
Sync Options: Rewrote mock_option function to be part of Jetpack-Sync class now
1 parent c90edb9 commit c9d244a

4 files changed

+43
-33
lines changed

class.jetpack-sync.php

+27
Original file line numberDiff line numberDiff line change
@@ -870,4 +870,31 @@ private function _get_post_count_cloud() {
870870
return (int) $results['results']['total'];
871871
}
872872

873+
/**
874+
* Sometimes we need to fake options to be able to sync data with .com
875+
* This is a helper function. That will make it easier to do just that.
876+
*
877+
* It will make sure that the options are synced when do_action( 'jetpack_sync_all_registered_options' );
878+
*
879+
* Which should happen everytime we update Jetpack to a new version or daily by Jetpack_Heartbeat.
880+
*
881+
* $callback is a function that is passed into a filter that returns the value of the option.
882+
* This value should never be false. Since we want to short circuit the get_option function
883+
* to return the value of the our callback.
884+
*
885+
* You can also trigger an update when a something else changes by calling the
886+
* do_action( 'add_option_jetpack_' . $option, 'jetpack_'.$option, $callback_function );
887+
* on the action that should that would trigger the update.
888+
*
889+
*
890+
* @param string $option Option will always be prefixed with Jetpack and be saved on .com side
891+
* @param string or array $callback
892+
*/
893+
function mock_option( $option , $callback ) {
894+
895+
add_filter( 'pre_option_jetpack_'. $option , $callback );
896+
// Instead of passing a file we just pass in a string.
897+
$this->options( 'mock-option' , 'jetpack_' . $option );
898+
899+
}
873900
}

class.jetpack.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,19 @@ private function Jetpack() {
381381
'timezone_string'
382382
);
383383

384+
/**
385+
* Sometimes you want to sync data to .com without adding options to .org sites.
386+
* The mock option allows you to do just that.
387+
*/
388+
$this->sync->mock_option( 'is_main_network', array( $this, 'is_main_network_option' ) );
389+
$this->sync->mock_option( 'main_network_site', array( $this, 'jetpack_main_network_site_option' ) );
390+
391+
/**
392+
* Trigger an update to the main_network_site when we update the blogname of a site.
393+
*
394+
*/
395+
add_action( 'update_option_blogname', array( $this, 'update_jetpack_main_network_site_option' ) );
396+
384397
add_action( 'update_option', array( $this, 'log_settings_change' ), 10, 3 );
385398

386399
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) {
@@ -636,7 +649,7 @@ function require_lib_dir( $lib_dir ) {
636649
* @param bool $option
637650
* @return string
638651
*/
639-
public static function jetpack_main_network_site_option( $option ) {
652+
public function jetpack_main_network_site_option( $option ) {
640653
return network_site_url();
641654
}
642655

@@ -650,7 +663,7 @@ public static function jetpack_main_network_site_option( $option ) {
650663
*
651664
* @return boolean
652665
*/
653-
public static function is_main_network_option( $option ) {
666+
public function is_main_network_option( $option ) {
654667
// return '1' or ''
655668
return (string) (bool) Jetpack::is_multi_network();
656669
}
@@ -683,7 +696,7 @@ public static function is_multi_network() {
683696
* @return null
684697
*/
685698
function update_jetpack_main_network_site_option() {
686-
do_action( 'add_option_jetpack_main_network_site', 'main_network_site', network_site_url() );
699+
do_action( 'add_option_jetpack_main_network_site', 'jetpack_main_network_site', network_site_url() );
687700
do_action( 'add_option_jetpack_is_main_network', 'jetpack_is_main_network', (string) (bool) Jetpack::is_multi_network() );
688701
}
689702

functions.jetpack_mock_option.php

-29
This file was deleted.

jetpack.php

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
require_once( JETPACK__PLUGIN_DIR . 'functions.photon.php' );
4646
require_once( JETPACK__PLUGIN_DIR . 'functions.compat.php' );
4747
require_once( JETPACK__PLUGIN_DIR . 'functions.gallery.php' );
48-
require_once( JETPACK__PLUGIN_DIR . 'functions.jetpack_mock_option.php' );
4948
require_once( JETPACK__PLUGIN_DIR . 'require-lib.php' );
5049
require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-autoupdate.php' );
5150

0 commit comments

Comments
 (0)