Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
9.14% |
17 / 186 |
|
11.54% |
3 / 26 |
CRAP | |
0.00% |
0 / 1 |
| Callables | |
8.70% |
16 / 184 |
|
11.54% |
3 / 26 |
5454.73 | |
0.00% |
0 / 1 |
| name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set_defaults | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| set_late_default | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| init_listeners | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| init_full_sync_listeners | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| init_before_send | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| reset_data | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| set_callable_whitelist | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_callable_whitelist | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_all_callables | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| get_callable | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| enqueue_full_sync_actions | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| send_full_sync_actions | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| estimate_full_sync_actions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_full_sync_actions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| unlock_sync_callable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| unlock_sync_callable_next_tick | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| unlock_plugin_action_link_and_callables | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| set_plugin_action_links | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
342 | |||
| should_send_callable | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| maybe_sync_callables | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
272 | |||
| get_always_sent_callables | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| expand_callables | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_objects_by_id | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
| get_object_by_id | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Callables sync module. |
| 4 | * |
| 5 | * @package automattic/jetpack-sync |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Sync\Modules; |
| 9 | |
| 10 | use Automattic\Jetpack\Constants as Jetpack_Constants; |
| 11 | use Automattic\Jetpack\Sync\Dedicated_Sender; |
| 12 | use Automattic\Jetpack\Sync\Defaults; |
| 13 | use Automattic\Jetpack\Sync\Functions; |
| 14 | use Automattic\Jetpack\Sync\Settings; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit( 0 ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Class to handle sync for callables. |
| 22 | */ |
| 23 | class Callables extends Module { |
| 24 | /** |
| 25 | * Name of the callables checksum option. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum'; |
| 30 | |
| 31 | /** |
| 32 | * Name of the transient for locking callables. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await'; |
| 37 | |
| 38 | /** |
| 39 | * Whitelist for callables we want to sync. |
| 40 | * |
| 41 | * @access private |
| 42 | * |
| 43 | * @var array |
| 44 | */ |
| 45 | private $callable_whitelist; |
| 46 | |
| 47 | /** |
| 48 | * For some options, we should always send the change right away! |
| 49 | * |
| 50 | * @access public |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
| 54 | const ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS = array( |
| 55 | 'jetpack_active_modules', |
| 56 | 'home', // option is home, callable is home_url. |
| 57 | 'siteurl', |
| 58 | 'jetpack_sync_error_idc', |
| 59 | 'paused_plugins', |
| 60 | 'paused_themes', |
| 61 | |
| 62 | ); |
| 63 | |
| 64 | const ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS_NEXT_TICK = array( |
| 65 | 'stylesheet', |
| 66 | ); |
| 67 | /** |
| 68 | * Setting this value to true will make it so that the callables will not be unlocked |
| 69 | * but the lock will be removed after content is send so that callables will be |
| 70 | * sent in the next request. |
| 71 | * |
| 72 | * @var bool |
| 73 | */ |
| 74 | private $force_send_callables_on_next_tick = false; |
| 75 | |
| 76 | /** |
| 77 | * For some options, the callable key differs from the option name/key |
| 78 | * |
| 79 | * @access public |
| 80 | * |
| 81 | * @var array |
| 82 | */ |
| 83 | const OPTION_NAMES_TO_CALLABLE_NAMES = array( |
| 84 | // @TODO: Audit the other option names for differences between the option names and callable names. |
| 85 | 'home' => 'home_url', |
| 86 | 'siteurl' => 'site_url', |
| 87 | 'jetpack_active_modules' => 'active_modules', |
| 88 | ); |
| 89 | |
| 90 | /** |
| 91 | * Sync module name. |
| 92 | * |
| 93 | * @access public |
| 94 | * |
| 95 | * @return string |
| 96 | */ |
| 97 | public function name() { |
| 98 | return 'functions'; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Set module defaults. |
| 103 | * Define the callable whitelist based on whether this is a single site or a multisite installation. |
| 104 | * |
| 105 | * @access public |
| 106 | */ |
| 107 | public function set_defaults() { |
| 108 | if ( is_multisite() ) { |
| 109 | $this->callable_whitelist = array_merge( Defaults::get_callable_whitelist(), Defaults::get_multisite_callable_whitelist() ); |
| 110 | } else { |
| 111 | $this->callable_whitelist = Defaults::get_callable_whitelist(); |
| 112 | } |
| 113 | $this->force_send_callables_on_next_tick = false; // Resets here as well mostly for tests. |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set module defaults at a later time. |
| 118 | * Reset the callable whitelist if needed to account for plugins adding the 'jetpack_sync_callable_whitelist' |
| 119 | * and 'jetpack_sync_multisite_callable_whitelist' filters late. |
| 120 | * |
| 121 | * @see Automattic\Jetpack\Sync\Modules::set_module_defaults |
| 122 | * @access public |
| 123 | */ |
| 124 | public function set_late_default() { |
| 125 | if ( is_multisite() ) { |
| 126 | $late_callables = array_merge( |
| 127 | apply_filters( 'jetpack_sync_callable_whitelist', array() ), |
| 128 | apply_filters( 'jetpack_sync_multisite_callable_whitelist', array() ) |
| 129 | ); |
| 130 | } else { |
| 131 | $late_callables = apply_filters( 'jetpack_sync_callable_whitelist', array() ); |
| 132 | } |
| 133 | if ( ! empty( $late_callables ) && is_array( $late_callables ) ) { |
| 134 | $this->callable_whitelist = array_merge( $this->callable_whitelist, $late_callables ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Initialize callables action listeners. |
| 140 | * |
| 141 | * @access public |
| 142 | * |
| 143 | * @param callable $callable Action handler callable. |
| 144 | */ |
| 145 | public function init_listeners( $callable ) { |
| 146 | add_action( 'jetpack_sync_callable', $callable, 10, 2 ); |
| 147 | add_action( 'current_screen', array( $this, 'set_plugin_action_links' ), 9999 ); // Should happen very late. |
| 148 | |
| 149 | foreach ( self::ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS as $option ) { |
| 150 | add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) ); |
| 151 | add_action( "delete_option_{$option}", array( $this, 'unlock_sync_callable' ) ); |
| 152 | } |
| 153 | |
| 154 | foreach ( self::ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS_NEXT_TICK as $option ) { |
| 155 | add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable_next_tick' ) ); |
| 156 | add_action( "delete_option_{$option}", array( $this, 'unlock_sync_callable_next_tick' ) ); |
| 157 | } |
| 158 | |
| 159 | // Provide a hook so that hosts can send changes to certain callables right away. |
| 160 | // Especially useful when a host uses constants to change home and siteurl. |
| 161 | add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) ); |
| 162 | |
| 163 | // get_plugins and wp_version |
| 164 | // gets fired when new code gets installed, updates etc. |
| 165 | add_action( 'upgrader_process_complete', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
| 166 | add_action( 'update_option_active_plugins', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Initialize callables action listeners for full sync. |
| 171 | * |
| 172 | * @access public |
| 173 | * |
| 174 | * @param callable $callable Action handler callable. |
| 175 | */ |
| 176 | public function init_full_sync_listeners( $callable ) { |
| 177 | add_action( 'jetpack_full_sync_callables', $callable ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Initialize the module in the sender. |
| 182 | * |
| 183 | * @access public |
| 184 | */ |
| 185 | public function init_before_send() { |
| 186 | add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) ); |
| 187 | |
| 188 | // Full sync. |
| 189 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Perform module cleanup. |
| 194 | * Deletes any transients and options that this module uses. |
| 195 | * Usually triggered when uninstalling the plugin. |
| 196 | * |
| 197 | * @access public |
| 198 | */ |
| 199 | public function reset_data() { |
| 200 | delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME ); |
| 201 | delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
| 202 | |
| 203 | $url_callables = array( 'home_url', 'site_url', 'main_network_site_url' ); |
| 204 | foreach ( $url_callables as $callable ) { |
| 205 | delete_option( Functions::HTTPS_CHECK_OPTION_PREFIX . $callable ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Set the callable whitelist. |
| 211 | * |
| 212 | * @access public |
| 213 | * |
| 214 | * @param array $callables The new callables whitelist. |
| 215 | */ |
| 216 | public function set_callable_whitelist( $callables ) { |
| 217 | $this->callable_whitelist = $callables; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Get the callable whitelist. |
| 222 | * |
| 223 | * @access public |
| 224 | * |
| 225 | * @return array The callables whitelist. |
| 226 | */ |
| 227 | public function get_callable_whitelist() { |
| 228 | return $this->callable_whitelist; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Retrieve all callables as per the current callables whitelist. |
| 233 | * |
| 234 | * @access public |
| 235 | * |
| 236 | * @return array All callables. |
| 237 | */ |
| 238 | public function get_all_callables() { |
| 239 | // get_all_callables should run as the master user always. |
| 240 | $current_user_id = get_current_user_id(); |
| 241 | wp_set_current_user( \Jetpack_Options::get_option( 'master_user' ) ); |
| 242 | $callables = array_combine( |
| 243 | array_keys( $this->get_callable_whitelist() ), |
| 244 | array_map( array( $this, 'get_callable' ), array_values( $this->get_callable_whitelist() ) ) |
| 245 | ); |
| 246 | wp_set_current_user( $current_user_id ); |
| 247 | return $callables; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Invoke a particular callable. |
| 252 | * Used as a wrapper to standartize invocation. |
| 253 | * |
| 254 | * @access private |
| 255 | * |
| 256 | * @param callable $callable Callable to invoke. |
| 257 | * @return mixed Return value of the callable, null if not callable. |
| 258 | */ |
| 259 | private function get_callable( $callable ) { |
| 260 | if ( is_callable( $callable ) ) { |
| 261 | return call_user_func( $callable ); |
| 262 | } else { |
| 263 | return null; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Enqueue the callable actions for full sync. |
| 269 | * |
| 270 | * @access public |
| 271 | * |
| 272 | * @param array $config Full sync configuration for this sync module. |
| 273 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
| 274 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
| 275 | * @return array Number of actions enqueued, and next module state. |
| 276 | */ |
| 277 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 278 | /** |
| 279 | * Tells the client to sync all callables to the server |
| 280 | * |
| 281 | * @since 1.6.3 |
| 282 | * @since-jetpack 4.2.0 |
| 283 | * |
| 284 | * @param boolean Whether to expand callables (should always be true) |
| 285 | */ |
| 286 | do_action( 'jetpack_full_sync_callables', true ); |
| 287 | |
| 288 | // The number of actions enqueued, and next module state (true == done). |
| 289 | return array( 1, true ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Send the callable actions for full sync. |
| 294 | * |
| 295 | * @access public |
| 296 | * |
| 297 | * @param array $config Full sync configuration for this sync module. |
| 298 | * @param array $status This Module Full Sync Status. |
| 299 | * @param int $send_until The timestamp until the current request can send. |
| 300 | * @param int $started The timestamp when the full sync started. |
| 301 | * |
| 302 | * @return array This Module Full Sync Status. |
| 303 | */ |
| 304 | public function send_full_sync_actions( $config, $status, $send_until, $started ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 305 | // we call this instead of do_action when sending immediately. |
| 306 | $result = $this->send_action( 'jetpack_full_sync_callables', array( true ) ); |
| 307 | |
| 308 | if ( is_wp_error( $result ) ) { |
| 309 | $status['error'] = true; |
| 310 | return $status; |
| 311 | } |
| 312 | $status['finished'] = true; |
| 313 | $status['sent'] = $status['total']; |
| 314 | return $status; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Retrieve an estimated number of actions that will be enqueued. |
| 319 | * |
| 320 | * @access public |
| 321 | * |
| 322 | * @param array $config Full sync configuration for this sync module. |
| 323 | * @return int Number of items yet to be enqueued. |
| 324 | */ |
| 325 | public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 326 | return 1; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Retrieve the actions that will be sent for this module during a full sync. |
| 331 | * |
| 332 | * @access public |
| 333 | * |
| 334 | * @return array Full sync actions of this module. |
| 335 | */ |
| 336 | public function get_full_sync_actions() { |
| 337 | return array( 'jetpack_full_sync_callables' ); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Unlock callables so they would be available for syncing again. |
| 342 | * |
| 343 | * @access public |
| 344 | */ |
| 345 | public function unlock_sync_callable() { |
| 346 | delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Unlock callables on the next tick. |
| 351 | * Sometime the true callable values are only present on the next tick. |
| 352 | * When switching themes for example. |
| 353 | * |
| 354 | * @access public |
| 355 | */ |
| 356 | public function unlock_sync_callable_next_tick() { |
| 357 | $this->force_send_callables_on_next_tick = true; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Unlock callables and plugin action links. |
| 362 | * |
| 363 | * @access public |
| 364 | */ |
| 365 | public function unlock_plugin_action_link_and_callables() { |
| 366 | delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
| 367 | delete_transient( 'jetpack_plugin_api_action_links_refresh' ); |
| 368 | add_filter( 'jetpack_check_and_send_callables', '__return_true' ); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Parse and store the plugin action links if on the plugins page. |
| 373 | * |
| 374 | * @uses \DOMDocument |
| 375 | * @uses libxml_use_internal_errors |
| 376 | * @uses mb_convert_encoding |
| 377 | * |
| 378 | * @access public |
| 379 | */ |
| 380 | public function set_plugin_action_links() { |
| 381 | if ( |
| 382 | ! class_exists( '\DOMDocument' ) || |
| 383 | ! function_exists( 'mb_convert_encoding' ) |
| 384 | ) { |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | $current_screeen = get_current_screen(); |
| 389 | |
| 390 | $plugins_action_links = array(); |
| 391 | // Is the transient lock in place? |
| 392 | $plugins_lock = get_transient( 'jetpack_plugin_api_action_links_refresh' ); |
| 393 | if ( ! empty( $plugins_lock ) && ( isset( $current_screeen->id ) && 'plugins' !== $current_screeen->id ) ) { |
| 394 | return; |
| 395 | } |
| 396 | $plugins = Functions::get_plugins(); |
| 397 | if ( ! is_array( $plugins ) ) { |
| 398 | return; |
| 399 | } |
| 400 | foreach ( $plugins as $plugin_file => $plugin_data ) { |
| 401 | /** |
| 402 | * Plugins often like to unset things but things break if they are not able to. |
| 403 | */ |
| 404 | $action_links = array( |
| 405 | 'deactivate' => '', |
| 406 | 'activate' => '', |
| 407 | 'details' => '', |
| 408 | 'delete' => '', |
| 409 | 'edit' => '', |
| 410 | ); |
| 411 | /** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */ |
| 412 | $action_links = apply_filters( 'plugin_action_links', $action_links, $plugin_file, $plugin_data, 'all' ); |
| 413 | // Verify $action_links is still an array. |
| 414 | if ( ! is_array( $action_links ) ) { |
| 415 | $action_links = array(); |
| 416 | } |
| 417 | /** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */ |
| 418 | $action_links = apply_filters( "plugin_action_links_{$plugin_file}", $action_links, $plugin_file, $plugin_data, 'all' ); |
| 419 | // Verify $action_links is still an array to resolve warnings from filters not returning an array. |
| 420 | if ( is_array( $action_links ) ) { |
| 421 | $action_links = array_filter( $action_links ); |
| 422 | } else { |
| 423 | $action_links = array(); |
| 424 | } |
| 425 | $formatted_action_links = null; |
| 426 | if ( $action_links ) { |
| 427 | $dom_doc = new \DOMDocument(); |
| 428 | foreach ( $action_links as $action_link ) { |
| 429 | // The @ is not enough to suppress errors when dealing with libxml, |
| 430 | // we have to tell it directly how we want to handle errors. |
| 431 | libxml_use_internal_errors( true ); |
| 432 | $dom_doc->loadHTML( '<?xml encoding="utf-8" ?>' . $action_link ); |
| 433 | libxml_use_internal_errors( false ); |
| 434 | |
| 435 | $link_elements = $dom_doc->getElementsByTagName( 'a' ); |
| 436 | if ( 0 === $link_elements->length ) { |
| 437 | continue; |
| 438 | } |
| 439 | |
| 440 | $link_element = $link_elements->item( 0 ); |
| 441 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 442 | if ( $link_element instanceof \DOMElement && $link_element->hasAttribute( 'href' ) && $link_element->nodeValue ) { |
| 443 | $link_url = trim( $link_element->getAttribute( 'href' ) ); |
| 444 | |
| 445 | // Add the full admin path to the url if the plugin did not provide it. |
| 446 | $link_url_scheme = wp_parse_url( $link_url, PHP_URL_SCHEME ); |
| 447 | if ( empty( $link_url_scheme ) ) { |
| 448 | $link_url = admin_url( $link_url ); |
| 449 | } |
| 450 | |
| 451 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 452 | $formatted_action_links[ $link_element->nodeValue ] = $link_url; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | if ( $formatted_action_links ) { |
| 457 | $plugins_action_links[ $plugin_file ] = $formatted_action_links; |
| 458 | } |
| 459 | } |
| 460 | // Cache things for a long time. |
| 461 | set_transient( 'jetpack_plugin_api_action_links_refresh', time(), DAY_IN_SECONDS ); |
| 462 | update_option( 'jetpack_plugin_api_action_links', $plugins_action_links ); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Whether a certain callable should be sent. |
| 467 | * |
| 468 | * @access public |
| 469 | * |
| 470 | * @param array $callable_checksums Callable checksums. |
| 471 | * @param string $name Name of the callable. |
| 472 | * @param string $checksum A checksum of the callable. |
| 473 | * @return boolean Whether to send the callable. |
| 474 | */ |
| 475 | public function should_send_callable( $callable_checksums, $name, $checksum ) { |
| 476 | $idc_override_callables = array( |
| 477 | 'main_network_site', |
| 478 | 'home_url', |
| 479 | 'site_url', |
| 480 | ); |
| 481 | if ( in_array( $name, $idc_override_callables, true ) && \Jetpack_Options::get_option( 'migrate_for_idc' ) ) { |
| 482 | return true; |
| 483 | } |
| 484 | |
| 485 | return ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ); |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Sync the callables if we're supposed to. |
| 490 | * |
| 491 | * @access public |
| 492 | */ |
| 493 | public function maybe_sync_callables() { |
| 494 | $callables = $this->get_all_callables(); |
| 495 | if ( ! apply_filters( 'jetpack_check_and_send_callables', false ) ) { |
| 496 | /** |
| 497 | * Treating Dedicated Sync requests a bit differently from normal. We want to send callables |
| 498 | * normally with all Sync actions, no matter if they were with admin action origin or not, |
| 499 | * since Dedicated Sync runs out of bound and the requests are never coming from an admin. |
| 500 | */ |
| 501 | if ( ! is_admin() && ! Dedicated_Sender::is_dedicated_sync_request() ) { |
| 502 | // If we're not an admin and we're not doing cron and this isn't WP_CLI, don't sync anything. |
| 503 | if ( ! Settings::is_doing_cron() && ! Jetpack_Constants::get_constant( 'WP_CLI' ) ) { |
| 504 | return; |
| 505 | } |
| 506 | // If we're not an admin and we are doing cron, sync the Callables that are always supposed to sync ( See https://github.com/Automattic/jetpack/issues/12924 ). |
| 507 | $callables = $this->get_always_sent_callables(); |
| 508 | } |
| 509 | if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) { |
| 510 | if ( $this->force_send_callables_on_next_tick ) { |
| 511 | $this->unlock_sync_callable(); |
| 512 | } |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if ( empty( $callables ) ) { |
| 518 | return; |
| 519 | } |
| 520 | // No need to set the transiant we are trying to remove it anyways. |
| 521 | if ( ! $this->force_send_callables_on_next_tick ) { |
| 522 | set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Defaults::$default_sync_callables_wait_time ); |
| 523 | } |
| 524 | |
| 525 | $callable_checksums = (array) \Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() ); |
| 526 | $has_changed = false; |
| 527 | // Only send the callables that have changed. |
| 528 | foreach ( $callables as $name => $value ) { |
| 529 | $checksum = $this->get_check_sum( $value ); |
| 530 | |
| 531 | // Explicitly not using Identical comparison as get_option returns a string. |
| 532 | if ( $value !== null && $this->should_send_callable( $callable_checksums, $name, $checksum ) ) { |
| 533 | |
| 534 | // Only send callable if the non sorted checksum also does not match. |
| 535 | if ( $this->should_send_callable( $callable_checksums, $name, $this->get_check_sum( $value, false ) ) ) { |
| 536 | |
| 537 | /** |
| 538 | * Tells the client to sync a callable (aka function) to the server |
| 539 | * |
| 540 | * @param string The name of the callable |
| 541 | * @param mixed The value of the callable |
| 542 | * |
| 543 | * @since 1.6.3 |
| 544 | * @since-jetpack 4.2.0 |
| 545 | */ |
| 546 | do_action( 'jetpack_sync_callable', $name, $value ); |
| 547 | } |
| 548 | |
| 549 | $callable_checksums[ $name ] = $checksum; |
| 550 | $has_changed = true; |
| 551 | } else { |
| 552 | $callable_checksums[ $name ] = $checksum; |
| 553 | } |
| 554 | } |
| 555 | if ( $has_changed ) { |
| 556 | \Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums ); |
| 557 | } |
| 558 | |
| 559 | if ( $this->force_send_callables_on_next_tick ) { |
| 560 | $this->unlock_sync_callable(); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Get the callables that should always be sent, e.g. on cron. |
| 566 | * |
| 567 | * @return array Callables that should always be sent |
| 568 | */ |
| 569 | protected function get_always_sent_callables() { |
| 570 | $callables = $this->get_all_callables(); |
| 571 | $cron_callables = array(); |
| 572 | foreach ( self::ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS as $option_name ) { |
| 573 | if ( array_key_exists( $option_name, $callables ) ) { |
| 574 | $cron_callables[ $option_name ] = $callables[ $option_name ]; |
| 575 | continue; |
| 576 | } |
| 577 | |
| 578 | // Check for the Callable name/key for the option, if different from option name. |
| 579 | if ( array_key_exists( $option_name, self::OPTION_NAMES_TO_CALLABLE_NAMES ) ) { |
| 580 | $callable_name = self::OPTION_NAMES_TO_CALLABLE_NAMES[ $option_name ]; |
| 581 | if ( array_key_exists( $callable_name, $callables ) ) { |
| 582 | $cron_callables[ $callable_name ] = $callables[ $callable_name ]; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | return $cron_callables; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Expand the callables within a hook before they are serialized and sent to the server. |
| 591 | * |
| 592 | * @access public |
| 593 | * |
| 594 | * @param array $args The hook parameters. |
| 595 | * @return array $args The hook parameters. |
| 596 | */ |
| 597 | public function expand_callables( $args ) { |
| 598 | if ( $args[0] ) { |
| 599 | $callables = $this->get_all_callables(); |
| 600 | $callables_checksums = array(); |
| 601 | foreach ( $callables as $name => $value ) { |
| 602 | $callables_checksums[ $name ] = $this->get_check_sum( $value ); |
| 603 | } |
| 604 | \Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callables_checksums ); |
| 605 | return $callables; |
| 606 | } |
| 607 | |
| 608 | return $args; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Return Total number of objects. |
| 613 | * |
| 614 | * @param array $config Full Sync config. |
| 615 | * |
| 616 | * @return int total |
| 617 | */ |
| 618 | public function total( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 619 | return count( $this->get_callable_whitelist() ); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Retrieve a set of callables by their IDs. |
| 624 | * |
| 625 | * @access public |
| 626 | * |
| 627 | * @param string $object_type Object type. |
| 628 | * @param array $ids Object IDs. |
| 629 | * @return array Array of objects. |
| 630 | */ |
| 631 | public function get_objects_by_id( $object_type, $ids ) { |
| 632 | if ( empty( $ids ) || empty( $object_type ) || 'callable' !== $object_type ) { |
| 633 | return array(); |
| 634 | } |
| 635 | |
| 636 | $objects = array(); |
| 637 | foreach ( (array) $ids as $id ) { |
| 638 | $object = $this->get_object_by_id( $object_type, $id ); |
| 639 | |
| 640 | if ( 'CALLABLE-DOES-NOT-EXIST' !== $object ) { |
| 641 | if ( 'all' === $id ) { |
| 642 | // If all was requested it contains all options and can simply be returned. |
| 643 | return $object; |
| 644 | } |
| 645 | $objects[ $id ] = $object; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | return $objects; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Retrieve a callable by its name. |
| 654 | * |
| 655 | * @access public |
| 656 | * |
| 657 | * @param string $object_type Type of the sync object. |
| 658 | * @param string $id ID of the sync object. |
| 659 | * @return mixed Value of Callable. |
| 660 | */ |
| 661 | public function get_object_by_id( $object_type, $id ) { |
| 662 | if ( 'callable' === $object_type ) { |
| 663 | |
| 664 | // Only whitelisted options can be returned. |
| 665 | if ( array_key_exists( $id, $this->get_callable_whitelist() ) ) { |
| 666 | // requires master user to be in context. |
| 667 | $current_user_id = get_current_user_id(); |
| 668 | wp_set_current_user( \Jetpack_Options::get_option( 'master_user' ) ); |
| 669 | $callable = $this->get_callable( $this->callable_whitelist[ $id ] ); |
| 670 | wp_set_current_user( $current_user_id ); |
| 671 | return $callable; |
| 672 | } elseif ( 'all' === $id ) { |
| 673 | return $this->get_all_callables(); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | return 'CALLABLE-DOES-NOT-EXIST'; |
| 678 | } |
| 679 | } |