1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php'; |
4
|
|
|
|
5
|
|
|
class Jetpack_Sync_Module_Callables extends Jetpack_Sync_Module { |
6
|
|
|
const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum'; |
7
|
|
|
const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await'; |
8
|
|
|
|
9
|
|
|
private $callable_whitelist; |
10
|
|
|
|
11
|
|
|
public function name() { |
12
|
|
|
return 'functions'; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function set_defaults() { |
16
|
|
|
if ( is_multisite() ) { |
17
|
|
|
$this->callable_whitelist = array_merge( Jetpack_Sync_Defaults::get_callable_whitelist(), Jetpack_Sync_Defaults::get_multisite_callable_whitelist() ); |
18
|
|
|
} else { |
19
|
|
|
$this->callable_whitelist = Jetpack_Sync_Defaults::get_callable_whitelist(); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function init_listeners( $callable ) { |
24
|
|
|
add_action( 'jetpack_sync_callable', $callable, 10, 2 ); |
25
|
|
|
|
26
|
|
|
// For some options, we should always send the change right away! |
27
|
|
|
$always_send_updates_to_these_options = array( |
28
|
|
|
'jetpack_active_modules', |
29
|
|
|
'home', |
30
|
|
|
'siteurl', |
31
|
|
|
'jetpack_sync_error_idc' |
32
|
|
|
); |
33
|
|
|
foreach( $always_send_updates_to_these_options as $option ) { |
34
|
|
|
add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Provide a hook so that hosts can send changes to certain callables right away. |
38
|
|
|
// Especially useful when a host uses constants to change home and siteurl. |
39
|
|
|
add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) ); |
40
|
|
|
|
41
|
|
|
// get_plugins and wp_version |
42
|
|
|
// gets fired when new code gets installed, updates etc. |
43
|
|
|
add_action( 'upgrader_process_complete', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
44
|
|
|
add_action( 'update_option_active_plugins', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function init_full_sync_listeners( $callable ) { |
48
|
|
|
add_action( 'jetpack_full_sync_callables', $callable ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function init_before_send() { |
52
|
|
|
add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) ); |
53
|
|
|
|
54
|
|
|
// full sync |
55
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function reset_data() { |
59
|
|
|
delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME ); |
60
|
|
|
delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
61
|
|
|
|
62
|
|
|
$url_callables = array( 'home_url', 'site_url', 'main_network_site_url' ); |
63
|
|
|
foreach( $url_callables as $callable ) { |
64
|
|
|
delete_option( Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable ); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function set_callable_whitelist( $callables ) { |
69
|
|
|
$this->callable_whitelist = $callables; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
function get_callable_whitelist() { |
73
|
|
|
return $this->callable_whitelist; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function get_all_callables() { |
77
|
|
|
// get_all_callables should run as the master user always. |
78
|
|
|
$current_user_id = get_current_user_id(); |
79
|
|
|
wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) ); |
80
|
|
|
$callables = array_combine( |
81
|
|
|
array_keys( $this->get_callable_whitelist() ), |
82
|
|
|
array_map( array( $this, 'get_callable' ), array_values( $this->get_callable_whitelist() ) ) |
83
|
|
|
); |
84
|
|
|
wp_set_current_user( $current_user_id ); |
85
|
|
|
|
86
|
|
|
return $callables; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function get_callable( $callable ) { |
90
|
|
|
return call_user_func( $callable ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
94
|
|
|
/** |
95
|
|
|
* Tells the client to sync all callables to the server |
96
|
|
|
* |
97
|
|
|
* @since 4.2.0 |
98
|
|
|
* |
99
|
|
|
* @param boolean Whether to expand callables (should always be true) |
100
|
|
|
*/ |
101
|
|
|
do_action( 'jetpack_full_sync_callables', true ); |
102
|
|
|
|
103
|
|
|
// The number of actions enqueued, and next module state (true == done) |
104
|
|
|
return array( 1, true ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function estimate_full_sync_actions( $config ) { |
108
|
|
|
return 1; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function get_full_sync_actions() { |
112
|
|
|
return array( 'jetpack_full_sync_callables' ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function unlock_sync_callable() { |
116
|
|
|
delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function unlock_plugin_action_link_and_callables() { |
120
|
|
|
delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
121
|
|
|
delete_transient( 'jetpack_plugin_api_action_links' ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function should_send_callable( $callable_checksums, $name, $checksum ) { |
125
|
|
|
$idc_override_callables = array( |
126
|
|
|
'main_network_site', |
127
|
|
|
'home_url', |
128
|
|
|
'site_url', |
129
|
|
|
); |
130
|
|
|
if ( in_array( $name, $idc_override_callables ) && Jetpack_Options::get_option( 'migrate_for_idc' ) ) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function maybe_sync_callables() { |
138
|
|
|
if ( ! is_admin() || Jetpack_Sync_Settings::is_doing_cron() ) { |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) { |
143
|
|
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time ); |
|
|
|
|
147
|
|
|
|
148
|
|
|
$callables = $this->get_all_callables(); |
149
|
|
|
|
150
|
|
|
if ( empty( $callables ) ) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$callable_checksums = (array) Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() ); |
155
|
|
|
|
156
|
|
|
// only send the callables that have changed |
157
|
|
|
foreach ( $callables as $name => $value ) { |
158
|
|
|
$checksum = $this->get_check_sum( $value ); |
159
|
|
|
// explicitly not using Identical comparison as get_option returns a string |
160
|
|
|
if ( ! is_null( $value ) && $this->should_send_callable( $callable_checksums, $name, $checksum ) ) { |
161
|
|
|
/** |
162
|
|
|
* Tells the client to sync a callable (aka function) to the server |
163
|
|
|
* |
164
|
|
|
* @since 4.2.0 |
165
|
|
|
* |
166
|
|
|
* @param string The name of the callable |
167
|
|
|
* @param mixed The value of the callable |
168
|
|
|
*/ |
169
|
|
|
do_action( 'jetpack_sync_callable', $name, $value ); |
170
|
|
|
$callable_checksums[ $name ] = $checksum; |
171
|
|
|
} else { |
172
|
|
|
$callable_checksums[ $name ] = $checksum; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function expand_callables( $args ) { |
179
|
|
|
if ( $args[0] ) { |
180
|
|
|
$callables = $this->get_all_callables(); |
181
|
|
|
$callables_checksums = array(); |
182
|
|
|
foreach ( $callables as $name => $value ) { |
183
|
|
|
$callables_checksums[ $name ] = $this->get_check_sum( $value ); |
184
|
|
|
} |
185
|
|
|
Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callables_checksums ); |
186
|
|
|
return $callables; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $args; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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.