Jetpack_Sync_Module_Callables

The WordPress Core Jetpack Sync Module Callables class.

Defined (1)

The class is defined in the following location(s).

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