Skip to content

Commit 7309aa2

Browse files
committed
Added Mock Jetpack Option to Sync options that don't have to live in the Database
If you want to sync options that don’t live in the database. Before you had to: Add the option to be synced to .com by calling the sync_options method. Add a filter to pre_option_ to fake retrieve it from the db. Trigger the change unless you want the synced by when ‘sync_all_registered_options’ method is called. Which is daily by heartbeat and on plugin upgrade. Now you can use the jetpack_mock_option function that lets you define the jetpack option and the callback that would return the output of it.
1 parent b93672b commit 7309aa2

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

class.jetpack.php

+3-11
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,7 @@ private function Jetpack() {
351351
* here, before we potentially fail out.
352352
*/
353353
add_filter( 'jetpack_require_lib_dir', array( $this, 'require_lib_dir' ) );
354-
/**
355-
* Update the main_network_site on .com
356-
*/
357-
add_filter( 'pre_option_jetpack_main_network_site', array( $this, 'jetpack_main_network_site_option' ) );
358-
add_action( 'update_option_siteurl', array( $this, 'update_jetpack_main_network_site_option' ) );
359-
// Update jetpack_is_main_network on .com
360-
add_filter( 'pre_option_jetpack_is_main_network', array( $this, 'is_main_network_option' ) );
354+
361355
/*
362356
* Load things that should only be in Network Admin.
363357
*
@@ -384,9 +378,7 @@ private function Jetpack() {
384378
'siteurl',
385379
'blogname',
386380
'gmt_offset',
387-
'timezone_string',
388-
'jetpack_main_network_site',
389-
'jetpack_is_main_network'
381+
'timezone_string'
390382
);
391383

392384
add_action( 'update_option', array( $this, 'log_settings_change' ), 10, 3 );
@@ -644,7 +636,7 @@ function require_lib_dir( $lib_dir ) {
644636
* @param bool $option
645637
* @return string
646638
*/
647-
function jetpack_main_network_site_option( $option ) {
639+
public static function jetpack_main_network_site_option( $option ) {
648640
return network_site_url();
649641
}
650642

functions.jetpack_mock_option.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Sometimes we need to fake options to be able to sync data with .com
4+
* This is a helper function. That will make it easier to do just that.
5+
*
6+
* It will make sure that the options are synced when
7+
* @param string $option Option will always be prefixed with Jetpack and be saved on .com side
8+
* @param string or array $callback should never return false. Best to stay way from bool type at all.
9+
* @return null
10+
*/
11+
function jetpack_mock_option( $option , $callback ) {
12+
13+
add_filter( 'pre_option_jetpack_'. $option , $callback );
14+
15+
Jetpack_Sync::sync_options( JETPACK__PLUGIN_DIR . 'functions.jetpack_mock_option.php', 'jetpack_' . $option );
16+
17+
}
18+
19+
20+
// Update jetpack_is_main_network on .com
21+
jetpack_mock_option( 'is_main_network', array( Jetpack::init(), 'is_main_network_option' ) );
22+
jetpack_mock_option( 'main_network_site', array( Jetpack::init(), 'jetpack_main_network_site_option' ) );
23+
24+
/**
25+
* Trigger an update to the main_network_site when we update the blogname of a site.
26+
*
27+
* @return null
28+
*/
29+
add_action( 'update_option_blogname', array( Jetpack::init(), 'update_jetpack_main_network_site_option' ) );
30+

jetpack.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
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' );
4849
require_once( JETPACK__PLUGIN_DIR . 'require-lib.php' );
4950
require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-autoupdate.php' );
5051

0 commit comments

Comments
 (0)