|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
|
4
|
|
|
|
|
5
|
|
|
class 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
|
|
|
add_action( 'current_screen', array( $this, 'set_plugin_action_links' ), 9999 ); // Should happen very late |
|
26
|
|
|
|
|
27
|
|
|
// For some options, we should always send the change right away! |
|
28
|
|
|
$always_send_updates_to_these_options = array( |
|
29
|
|
|
'jetpack_active_modules', |
|
30
|
|
|
'home', |
|
31
|
|
|
'siteurl', |
|
32
|
|
|
'jetpack_sync_error_idc', |
|
33
|
|
|
'paused_plugins', |
|
34
|
|
|
'paused_themes', |
|
35
|
|
|
); |
|
36
|
|
|
foreach ( $always_send_updates_to_these_options as $option ) { |
|
37
|
|
|
add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) ); |
|
38
|
|
|
add_action( "delete_option_{$option}", array( $this, 'unlock_sync_callable' ) ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Provide a hook so that hosts can send changes to certain callables right away. |
|
42
|
|
|
// Especially useful when a host uses constants to change home and siteurl. |
|
43
|
|
|
add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) ); |
|
44
|
|
|
|
|
45
|
|
|
// get_plugins and wp_version |
|
46
|
|
|
// gets fired when new code gets installed, updates etc. |
|
47
|
|
|
add_action( 'upgrader_process_complete', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
|
48
|
|
|
add_action( 'update_option_active_plugins', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function init_full_sync_listeners( $callable ) { |
|
52
|
|
|
add_action( 'jetpack_full_sync_callables', $callable ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function init_before_send() { |
|
56
|
|
|
add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) ); |
|
57
|
|
|
|
|
58
|
|
|
// full sync |
|
59
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function reset_data() { |
|
63
|
|
|
delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME ); |
|
64
|
|
|
delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
|
65
|
|
|
|
|
66
|
|
|
$url_callables = array( 'home_url', 'site_url', 'main_network_site_url' ); |
|
67
|
|
|
foreach ( $url_callables as $callable ) { |
|
68
|
|
|
delete_option( \Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable ); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
function set_callable_whitelist( $callables ) { |
|
73
|
|
|
$this->callable_whitelist = $callables; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
function get_callable_whitelist() { |
|
77
|
|
|
return $this->callable_whitelist; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function get_all_callables() { |
|
81
|
|
|
// get_all_callables should run as the master user always. |
|
82
|
|
|
$current_user_id = get_current_user_id(); |
|
83
|
|
|
wp_set_current_user( \Jetpack_Options::get_option( 'master_user' ) ); |
|
84
|
|
|
$callables = array_combine( |
|
85
|
|
|
array_keys( $this->get_callable_whitelist() ), |
|
86
|
|
|
array_map( array( $this, 'get_callable' ), array_values( $this->get_callable_whitelist() ) ) |
|
87
|
|
|
); |
|
88
|
|
|
wp_set_current_user( $current_user_id ); |
|
89
|
|
|
return $callables; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function get_callable( $callable ) { |
|
93
|
|
|
return call_user_func( $callable ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
|
97
|
|
|
/** |
|
98
|
|
|
* Tells the client to sync all callables to the server |
|
99
|
|
|
* |
|
100
|
|
|
* @since 4.2.0 |
|
101
|
|
|
* |
|
102
|
|
|
* @param boolean Whether to expand callables (should always be true) |
|
103
|
|
|
*/ |
|
104
|
|
|
do_action( 'jetpack_full_sync_callables', true ); |
|
105
|
|
|
|
|
106
|
|
|
// The number of actions enqueued, and next module state (true == done) |
|
107
|
|
|
return array( 1, true ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function estimate_full_sync_actions( $config ) { |
|
111
|
|
|
return 1; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function get_full_sync_actions() { |
|
115
|
|
|
return array( 'jetpack_full_sync_callables' ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function unlock_sync_callable() { |
|
119
|
|
|
delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function unlock_plugin_action_link_and_callables() { |
|
123
|
|
|
delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
|
124
|
|
|
delete_transient( 'jetpack_plugin_api_action_links_refresh' ); |
|
125
|
|
|
add_filter( 'jetpack_check_and_send_callables', '__return_true' ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function set_plugin_action_links() { |
|
129
|
|
|
if ( |
|
130
|
|
|
! class_exists( '\DOMDocument' ) || |
|
131
|
|
|
! function_exists( 'libxml_use_internal_errors' ) || |
|
132
|
|
|
! function_exists( 'mb_convert_encoding' ) |
|
133
|
|
|
) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$current_screeen = get_current_screen(); |
|
138
|
|
|
|
|
139
|
|
|
$plugins_action_links = array(); |
|
140
|
|
|
// Is the transient lock in place? |
|
141
|
|
|
$plugins_lock = get_transient( 'jetpack_plugin_api_action_links_refresh', false ); |
|
142
|
|
|
if ( ! empty( $plugins_lock ) && ( isset( $current_screeen->id ) && $current_screeen->id !== 'plugins' ) ) { |
|
143
|
|
|
return; |
|
144
|
|
|
} |
|
145
|
|
|
$plugins = array_keys( \Jetpack_Sync_Functions::get_plugins() ); |
|
146
|
|
|
foreach ( $plugins as $plugin_file ) { |
|
147
|
|
|
/** |
|
148
|
|
|
* Plugins often like to unset things but things break if they are not able to. |
|
149
|
|
|
*/ |
|
150
|
|
|
$action_links = array( |
|
151
|
|
|
'deactivate' => '', |
|
152
|
|
|
'activate' => '', |
|
153
|
|
|
'details' => '', |
|
154
|
|
|
'delete' => '', |
|
155
|
|
|
'edit' => '', |
|
156
|
|
|
); |
|
157
|
|
|
/** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */ |
|
158
|
|
|
$action_links = apply_filters( 'plugin_action_links', $action_links, $plugin_file, null, 'all' ); |
|
159
|
|
|
/** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */ |
|
160
|
|
|
$action_links = apply_filters( "plugin_action_links_{$plugin_file}", $action_links, $plugin_file, null, 'all' ); |
|
161
|
|
|
$action_links = array_filter( $action_links ); |
|
162
|
|
|
$formatted_action_links = null; |
|
163
|
|
|
if ( ! empty( $action_links ) && count( $action_links ) > 0 ) { |
|
164
|
|
|
$dom_doc = new \DOMDocument(); |
|
165
|
|
|
foreach ( $action_links as $action_link ) { |
|
166
|
|
|
// The @ is not enough to suppress errors when dealing with libxml, |
|
167
|
|
|
// we have to tell it directly how we want to handle errors. |
|
168
|
|
|
libxml_use_internal_errors( true ); |
|
169
|
|
|
$dom_doc->loadHTML( mb_convert_encoding( $action_link, 'HTML-ENTITIES', 'UTF-8' ) ); |
|
170
|
|
|
libxml_use_internal_errors( false ); |
|
171
|
|
|
|
|
172
|
|
|
$link_elements = $dom_doc->getElementsByTagName( 'a' ); |
|
173
|
|
|
if ( $link_elements->length == 0 ) { |
|
174
|
|
|
continue; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$link_element = $link_elements->item( 0 ); |
|
178
|
|
|
if ( $link_element->hasAttribute( 'href' ) && $link_element->nodeValue ) { |
|
|
|
|
|
|
179
|
|
|
$link_url = trim( $link_element->getAttribute( 'href' ) ); |
|
180
|
|
|
|
|
181
|
|
|
// Add the full admin path to the url if the plugin did not provide it |
|
182
|
|
|
$link_url_scheme = wp_parse_url( $link_url, PHP_URL_SCHEME ); |
|
183
|
|
|
if ( empty( $link_url_scheme ) ) { |
|
184
|
|
|
$link_url = admin_url( $link_url ); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$formatted_action_links[ $link_element->nodeValue ] = $link_url; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
if ( $formatted_action_links ) { |
|
192
|
|
|
$plugins_action_links[ $plugin_file ] = $formatted_action_links; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
// Cache things for a long time |
|
196
|
|
|
set_transient( 'jetpack_plugin_api_action_links_refresh', time(), DAY_IN_SECONDS ); |
|
197
|
|
|
update_option( 'jetpack_plugin_api_action_links', $plugins_action_links ); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function should_send_callable( $callable_checksums, $name, $checksum ) { |
|
201
|
|
|
$idc_override_callables = array( |
|
202
|
|
|
'main_network_site', |
|
203
|
|
|
'home_url', |
|
204
|
|
|
'site_url', |
|
205
|
|
|
); |
|
206
|
|
|
if ( in_array( $name, $idc_override_callables ) && \Jetpack_Options::get_option( 'migrate_for_idc' ) ) { |
|
207
|
|
|
return true; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function maybe_sync_callables() { |
|
214
|
|
|
if ( ! apply_filters( 'jetpack_check_and_send_callables', false ) ) { |
|
215
|
|
|
if ( ! is_admin() || \Jetpack_Sync_Settings::is_doing_cron() ) { |
|
216
|
|
|
return; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) { |
|
220
|
|
|
return; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), \Jetpack_Sync_Defaults::$default_sync_callables_wait_time ); |
|
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
$callables = $this->get_all_callables(); |
|
227
|
|
|
|
|
228
|
|
|
if ( empty( $callables ) ) { |
|
229
|
|
|
return; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
$callable_checksums = (array) \Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() ); |
|
233
|
|
|
$has_changed = false; |
|
234
|
|
|
// only send the callables that have changed |
|
235
|
|
View Code Duplication |
foreach ( $callables as $name => $value ) { |
|
236
|
|
|
$checksum = $this->get_check_sum( $value ); |
|
237
|
|
|
// explicitly not using Identical comparison as get_option returns a string |
|
238
|
|
|
if ( ! is_null( $value ) && $this->should_send_callable( $callable_checksums, $name, $checksum ) ) { |
|
239
|
|
|
/** |
|
240
|
|
|
* Tells the client to sync a callable (aka function) to the server |
|
241
|
|
|
* |
|
242
|
|
|
* @since 4.2.0 |
|
243
|
|
|
* |
|
244
|
|
|
* @param string The name of the callable |
|
245
|
|
|
* @param mixed The value of the callable |
|
246
|
|
|
*/ |
|
247
|
|
|
do_action( 'jetpack_sync_callable', $name, $value ); |
|
248
|
|
|
$callable_checksums[ $name ] = $checksum; |
|
249
|
|
|
$has_changed = true; |
|
250
|
|
|
} else { |
|
251
|
|
|
$callable_checksums[ $name ] = $checksum; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
if ( $has_changed ) { |
|
255
|
|
|
\Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums ); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
public function expand_callables( $args ) { |
|
261
|
|
|
if ( $args[0] ) { |
|
262
|
|
|
$callables = $this->get_all_callables(); |
|
263
|
|
|
$callables_checksums = array(); |
|
264
|
|
|
foreach ( $callables as $name => $value ) { |
|
265
|
|
|
$callables_checksums[ $name ] = $this->get_check_sum( $value ); |
|
266
|
|
|
} |
|
267
|
|
|
\Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callables_checksums ); |
|
268
|
|
|
return $callables; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
return $args; |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.