|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Jetpack_Sync_Module_Options extends Jetpack_Sync_Module { |
|
4
|
|
|
private $options_whitelist; |
|
5
|
|
|
|
|
6
|
|
|
public function name() { |
|
7
|
|
|
return 'options'; |
|
8
|
|
|
} |
|
9
|
|
|
|
|
10
|
|
|
public function init_listeners( $callable ) { |
|
11
|
|
|
// options |
|
12
|
|
|
add_action( 'added_option', $callable, 10, 2 ); |
|
13
|
|
|
add_action( 'updated_option', $callable, 10, 3 ); |
|
14
|
|
|
add_action( 'deleted_option', $callable, 10, 1 ); |
|
15
|
|
|
|
|
16
|
|
|
// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable. |
|
17
|
|
|
add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
|
18
|
|
|
add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
|
19
|
|
|
add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
|
20
|
|
|
|
|
21
|
|
|
$whitelist_option_handler = array( $this, 'whitelist_options' ); |
|
22
|
|
|
add_filter( 'jetpack_sync_before_enqueue_deleted_option', $whitelist_option_handler ); |
|
23
|
|
|
add_filter( 'jetpack_sync_before_enqueue_added_option', $whitelist_option_handler ); |
|
24
|
|
|
add_filter( 'jetpack_sync_before_enqueue_updated_option', $whitelist_option_handler ); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function init_full_sync_listeners( $callable ) { |
|
28
|
|
|
add_action( 'jetpack_full_sync_options', $callable ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function init_before_send() { |
|
32
|
|
|
// full sync |
|
33
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_options', array( $this, 'expand_options' ) ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function set_defaults() { |
|
37
|
|
|
$this->update_options_whitelist(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function set_late_default() { |
|
41
|
|
|
|
|
42
|
|
|
/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */ |
|
43
|
|
|
$late_options = apply_filters( 'jetpack_options_whitelist', array() ); |
|
44
|
|
|
if ( ! empty( $late_options ) && is_array( $late_options ) ) { |
|
45
|
|
|
$this->options_whitelist = array_merge( $this->options_whitelist, $late_options ); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
|
50
|
|
|
/** |
|
51
|
|
|
* Tells the client to sync all options to the server |
|
52
|
|
|
* |
|
53
|
|
|
* @since 4.2.0 |
|
54
|
|
|
* |
|
55
|
|
|
* @param boolean Whether to expand options (should always be true) |
|
56
|
|
|
*/ |
|
57
|
|
|
do_action( 'jetpack_full_sync_options', true ); |
|
58
|
|
|
|
|
59
|
|
|
// The number of actions enqueued, and next module state (true == done) |
|
60
|
|
|
return array( 1, true ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function estimate_full_sync_actions( $config ) { |
|
64
|
|
|
return 1; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
function get_full_sync_actions() { |
|
68
|
|
|
return array( 'jetpack_full_sync_options' ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Is public so that we don't have to store so much data all the options twice. |
|
72
|
|
|
function get_all_options() { |
|
73
|
|
|
$options = array(); |
|
74
|
|
|
foreach ( $this->options_whitelist as $option ) { |
|
75
|
|
|
$options[ $option ] = get_option( $option ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// add theme mods |
|
79
|
|
|
$theme_mods_option = 'theme_mods_'.get_option( 'stylesheet' ); |
|
80
|
|
|
$theme_mods_value = get_option( $theme_mods_option ); |
|
81
|
|
|
$this->filter_theme_mods( $theme_mods_value ); |
|
82
|
|
|
$options[ $theme_mods_option ] = $theme_mods_value; |
|
83
|
|
|
|
|
84
|
|
|
return $options; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
function update_options_whitelist() { |
|
88
|
|
|
/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */ |
|
89
|
|
|
$this->options_whitelist = apply_filters( 'jetpack_options_whitelist', Jetpack_Sync_Defaults::$default_options_whitelist ); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
function set_options_whitelist( $options ) { |
|
93
|
|
|
$this->options_whitelist = $options; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
function get_options_whitelist() { |
|
97
|
|
|
return $this->options_whitelist; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// reject non-whitelisted options |
|
101
|
|
|
function whitelist_options( $args ) { |
|
102
|
|
|
if ( ! $this->is_whitelisted_option( $args[0] ) ) { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// filter our weird array( false ) value for theme_mods_* |
|
107
|
|
|
if ( 'theme_mods_' === substr( $args[0], 0, 11 ) ) { |
|
108
|
|
|
$this->filter_theme_mods( $args[1] ); |
|
109
|
|
|
if ( isset( $args[2] ) ) { |
|
110
|
|
|
$this->filter_theme_mods( $args[2] ); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $args; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
function is_whitelisted_option( $option ) { |
|
118
|
|
|
return in_array( $option, $this->options_whitelist ) || 'theme_mods_' === substr( $option, 0, 11 ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function filter_theme_mods( &$value ) { |
|
122
|
|
|
if ( is_array( $value ) && isset( $value[0] ) ) { |
|
123
|
|
|
unset( $value[0] ); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
function jetpack_sync_core_icon() { |
|
128
|
|
|
if ( function_exists( 'get_site_icon_url' ) ) { |
|
129
|
|
|
$url = get_site_icon_url(); |
|
130
|
|
|
} else { |
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' ); |
|
135
|
|
|
// If there's a core icon, maybe update the option. If not, fall back to Jetpack's. |
|
136
|
|
|
if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) { |
|
137
|
|
|
// This is the option that is synced with dotcom |
|
138
|
|
|
Jetpack_Options::update_option( 'site_icon_url', $url ); |
|
139
|
|
|
} else if ( empty( $url ) ) { |
|
140
|
|
|
Jetpack_Options::delete_option( 'site_icon_url' ); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function expand_options( $args ) { |
|
145
|
|
|
if ( $args[0] ) { |
|
146
|
|
|
return $this->get_all_options(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $args; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.