Jetpack_Sync_Actions

The role of this class is to hook the Sync subsystem into WordPress - when to listen for actions, when to send, when to perform a full sync, etc.

Defined (1)

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

/sync/class.jetpack-sync-actions.php  
  1. class Jetpack_Sync_Actions { 
  2. static $sender = null; 
  3. static $listener = null; 
  4. const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; 
  5. const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; 
  6.  
  7. static function init() { 
  8.  
  9. // everything below this point should only happen if we're a valid sync site 
  10. if ( ! self::sync_allowed() ) { 
  11. return; 
  12.  
  13. if ( self::sync_via_cron_allowed() ) { 
  14. self::init_sync_cron_jobs(); 
  15. } else if ( wp_next_scheduled( 'jetpack_sync_cron' ) ) { 
  16. self::clear_sync_cron_jobs(); 
  17.  
  18. // On jetpack authorization, schedule a full sync 
  19. add_action( 'jetpack_client_authorized', array( __CLASS__, 'do_full_sync' ), 10, 0 ); 
  20.  
  21. // When importing via cron, do not sync 
  22. add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); 
  23.  
  24. // Sync connected user role changes to .com 
  25. require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php'; 
  26.  
  27. // publicize filter to prevent publicizing blacklisted post types 
  28. add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); 
  29.  
  30. /** 
  31. * Fires on every request before default loading sync listener code. 
  32. * Return false to not load sync listener code that monitors common 
  33. * WP actions to be serialized. 
  34. * By default this returns true for cron jobs, non-GET-requests, or requests where the 
  35. * user is logged-in. 
  36. * @since 4.2.0 
  37. * @param bool should we load sync listener code for this request 
  38. */ 
  39. if ( apply_filters( 'jetpack_sync_listener_should_load', true ) ) { 
  40. self::initialize_listener(); 
  41.  
  42. add_action( 'init', array( __CLASS__, 'add_sender_shutdown' ), 90 ); 
  43.  
  44.  
  45. static function add_sender_shutdown() { 
  46. /** 
  47. * Fires on every request before default loading sync sender code. 
  48. * Return false to not load sync sender code that serializes pending 
  49. * data and sends it to WPCOM for processing. 
  50. * By default this returns true for cron jobs, POST requests, admin requests, or requests 
  51. * by users who can manage_options. 
  52. * @since 4.2.0 
  53. * @param bool should we load sync sender code for this request 
  54. */ 
  55. if ( apply_filters( 'jetpack_sync_sender_should_load',  
  56. ( isset( $_SERVER["REQUEST_METHOD"] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) 
  57. || 
  58. current_user_can( 'manage_options' ) 
  59. || 
  60. is_admin() 
  61. || 
  62. defined( 'PHPUNIT_JETPACK_TESTSUITE' ) 
  63. ) ) { 
  64. self::initialize_sender(); 
  65. add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); 
  66. add_action( 'shutdown', array( self::$sender, 'do_full_sync' ) ); 
  67.  
  68. static function sync_allowed() { 
  69. require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; 
  70. return ( ! Jetpack_Sync_Settings::get_setting( 'disable' ) && Jetpack::is_active() && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) ) 
  71. || defined( 'PHPUNIT_JETPACK_TESTSUITE' ); 
  72.  
  73. static function sync_via_cron_allowed() { 
  74. require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; 
  75. return ( Jetpack_Sync_Settings::get_setting( 'sync_via_cron' ) ); 
  76.  
  77. static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { 
  78. require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; 
  79. if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { 
  80. return false; 
  81.  
  82. return $should_publicize; 
  83.  
  84. static function set_is_importing_true() { 
  85. require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; 
  86. Jetpack_Sync_Settings::set_importing( true ); 
  87.  
  88. static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration ) { 
  89. Jetpack::load_xml_rpc_client(); 
  90.  
  91. $query_args = array( 
  92. 'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action 
  93. 'codec' => $codec_name, // send the name of the codec used to encode the data 
  94. 'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences 
  95. 'queue' => $queue_id, // sync or full_sync 
  96. 'home' => get_home_url(), // Send home url option to check for Identity Crisis server-side 
  97. 'siteurl' => get_site_url(), // Send siteurl option to check for Identity Crisis server-side 
  98. 'cd' => sprintf( '%.4f', $checkout_duration), // Time spent retrieving queue items from the DB 
  99. 'pd' => sprintf( '%.4f', $preprocess_duration), // Time spent converting queue items into data to send 
  100. ); 
  101.  
  102. // Has the site opted in to IDC mitigation? 
  103. if ( Jetpack::sync_idc_optin() ) { 
  104. $query_args['idc'] = true; 
  105.  
  106. if ( Jetpack_Options::get_option( 'migrate_for_idc', false ) ) { 
  107. $query_args['migrate_for_idc'] = true; 
  108.  
  109. $query_args['timeout'] = Jetpack_Sync_Settings::is_doing_cron() ? 30 : 15; 
  110.  
  111. /** 
  112. * Filters query parameters appended to the Sync request URL sent to WordPress.com. 
  113. * @since 4.7.0 
  114. * @param array $query_args associative array of query parameters. 
  115. */ 
  116. $query_args = apply_filters( 'jetpack_sync_send_data_query_args', $query_args ); 
  117.  
  118. $url = add_query_arg( $query_args, Jetpack::xmlrpc_api_url() ); 
  119.  
  120. $rpc = new Jetpack_IXR_Client( array( 
  121. 'url' => $url,  
  122. 'user_id' => JETPACK_MASTER_USER,  
  123. 'timeout' => $query_args['timeout'],  
  124. ) ); 
  125.  
  126. $result = $rpc->query( 'jetpack.syncActions', $data ); 
  127.  
  128. if ( ! $result ) { 
  129. return $rpc->get_jetpack_error(); 
  130.  
  131. $response = $rpc->getResponse(); 
  132.  
  133. // Check if WordPress.com IDC mitigation blocked the sync request 
  134. if ( is_array( $response ) && isset( $response['error_code'] ) ) { 
  135. $error_code = $response['error_code']; 
  136. $allowed_idc_error_codes = array( 
  137. 'jetpack_url_mismatch',  
  138. 'jetpack_home_url_mismatch',  
  139. 'jetpack_site_url_mismatch' 
  140. ); 
  141.  
  142. if ( in_array( $error_code, $allowed_idc_error_codes ) ) { 
  143. Jetpack_Options::update_option( 
  144. 'sync_error_idc',  
  145. Jetpack::get_sync_error_idc_option( $response ) 
  146. ); 
  147.  
  148. return new WP_Error( 
  149. 'sync_error_idc',  
  150. esc_html__( 'Sync has been blocked from WordPress.com because it would cause an identity crisis', 'jetpack' ) 
  151. ); 
  152.  
  153. return $response; 
  154.  
  155. static function do_initial_sync( $new_version = null, $old_version = null ) { 
  156. if ( ! empty( $old_version ) && version_compare( $old_version, '4.2', '>=' ) ) { 
  157. return; 
  158.  
  159. $initial_sync_config = array( 
  160. 'options' => true,  
  161. 'network_options' => true,  
  162. 'functions' => true,  
  163. 'constants' => true,  
  164. 'users' => 'initial',  
  165. ); 
  166.  
  167. self::do_full_sync( $initial_sync_config ); 
  168.  
  169. static function do_full_sync( $modules = null ) { 
  170. if ( ! self::sync_allowed() ) { 
  171. return false; 
  172.  
  173. self::initialize_listener(); 
  174. Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules ); 
  175.  
  176. return true; 
  177.  
  178. static function jetpack_cron_schedule( $schedules ) { 
  179. if ( ! isset( $schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] ) ) { 
  180. $schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] = array( 
  181. 'interval' => self::DEFAULT_SYNC_CRON_INTERVAL_VALUE,  
  182. 'display' => sprintf( 
  183. esc_html( _n( 'Every minute', 'Every %d minutes', intval( self::DEFAULT_SYNC_CRON_INTERVAL_VALUE / 60 ), 'jetpack' ) ),  
  184. intval( self::DEFAULT_SYNC_CRON_INTERVAL_VALUE / 60 ) 
  185. ); 
  186. return $schedules; 
  187.  
  188. static function do_cron_sync() { 
  189. self::do_cron_sync_by_type( 'sync' ); 
  190.  
  191. static function do_cron_full_sync() { 
  192. self::do_cron_sync_by_type( 'full_sync' ); 
  193.  
  194. /** 
  195. * Try to send actions until we run out of things to send,  
  196. * or have to wait more than 15s before sending again,  
  197. * or we hit a lock or some other sending issue 
  198. * @param string $type Sync type. Can be `sync` or `full_sync`. 
  199. */ 
  200. static function do_cron_sync_by_type( $type ) { 
  201. if ( ! self::sync_allowed() || ( 'sync' !== $type && 'full_sync' !== $type ) ) { 
  202. return; 
  203.  
  204. self::initialize_sender(); 
  205.  
  206. $time_limit = Jetpack_Sync_Settings::get_setting( 'cron_sync_time_limit' ); 
  207. $start_time = time(); 
  208.  
  209. do { 
  210. $next_sync_time = self::$sender->get_next_sync_time( $type ); 
  211.  
  212. if ( $next_sync_time ) { 
  213. $delay = $next_sync_time - time() + 1; 
  214. if ( $delay > 15 ) { 
  215. break; 
  216. } elseif ( $delay > 0 ) { 
  217. sleep( $delay ); 
  218.  
  219. $result = 'full_sync' === $type ? self::$sender->do_full_sync() : self::$sender->do_sync(); 
  220. } while ( $result && ( $start_time + $time_limit ) > time() ); 
  221.  
  222. static function initialize_listener() { 
  223. require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; 
  224. self::$listener = Jetpack_Sync_Listener::get_instance(); 
  225.  
  226. static function initialize_sender() { 
  227. require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php'; 
  228. self::$sender = Jetpack_Sync_Sender::get_instance(); 
  229.  
  230. // bind the sending process 
  231. add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 6 ); 
  232.  
  233. static function initialize_woocommerce() { 
  234. if ( false === class_exists( 'WooCommerce' ) ) { 
  235. return; 
  236. add_filter( 'jetpack_sync_modules', array( 'Jetpack_Sync_Actions', 'add_woocommerce_sync_module' ) ); 
  237.  
  238. static function add_woocommerce_sync_module( $sync_modules ) { 
  239. require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-woocommerce.php'; 
  240. $sync_modules[] = 'Jetpack_Sync_Module_WooCommerce'; 
  241. return $sync_modules; 
  242.  
  243. static function initialize_wp_super_cache() { 
  244. if ( false === function_exists( 'wp_cache_is_enabled' ) ) { 
  245. return; 
  246. add_filter( 'jetpack_sync_modules', array( 'Jetpack_Sync_Actions', 'add_wp_super_cache_sync_module' ) ); 
  247.  
  248. static function add_wp_super_cache_sync_module( $sync_modules ) { 
  249. require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-wp-super-cache.php'; 
  250. $sync_modules[] = 'Jetpack_Sync_Module_WP_Super_Cache'; 
  251. return $sync_modules; 
  252.  
  253. static function sanitize_filtered_sync_cron_schedule( $schedule ) { 
  254. $schedule = sanitize_key( $schedule ); 
  255. $schedules = wp_get_schedules(); 
  256.  
  257. // Make sure that the schedule has actually been registered using the `cron_intervals` filter. 
  258. if ( isset( $schedules[ $schedule ] ) ) { 
  259. return $schedule; 
  260.  
  261. return self::DEFAULT_SYNC_CRON_INTERVAL_NAME; 
  262.  
  263. static function get_start_time_offset( $schedule = '', $hook = '' ) { 
  264. $start_time_offset = is_multisite() 
  265. ? mt_rand( 0, ( 2 * self::DEFAULT_SYNC_CRON_INTERVAL_VALUE ) ) 
  266. : 0; 
  267.  
  268. /** 
  269. * Allows overriding the offset that the sync cron jobs will first run. This can be useful when scheduling 
  270. * cron jobs across multiple sites in a network. 
  271. * @since 4.5 
  272. * @param int $start_time_offset 
  273. * @param string $hook 
  274. * @param string $schedule 
  275. */ 
  276. return intval( apply_filters( 
  277. 'jetpack_sync_cron_start_time_offset',  
  278. $start_time_offset,  
  279. $hook,  
  280. $schedule 
  281. ) ); 
  282.  
  283. static function maybe_schedule_sync_cron( $schedule, $hook ) { 
  284. if ( ! $hook ) { 
  285. return; 
  286. $schedule = self::sanitize_filtered_sync_cron_schedule( $schedule ); 
  287.  
  288. $start_time = time() + self::get_start_time_offset( $schedule, $hook ); 
  289. if ( ! wp_next_scheduled( $hook ) ) { 
  290. // Schedule a job to send pending queue items once a minute 
  291. wp_schedule_event( $start_time, $schedule, $hook ); 
  292. } else if ( $schedule != wp_get_schedule( $hook ) ) { 
  293. // If the schedule has changed, update the schedule 
  294. wp_clear_scheduled_hook( $hook ); 
  295. wp_schedule_event( $start_time, $schedule, $hook ); 
  296.  
  297. static function clear_sync_cron_jobs() { 
  298. wp_clear_scheduled_hook( 'jetpack_sync_cron' ); 
  299. wp_clear_scheduled_hook( 'jetpack_sync_full_cron' ); 
  300.  
  301. static function init_sync_cron_jobs() { 
  302. add_filter( 'cron_schedules', array( __CLASS__, 'jetpack_cron_schedule' ) ); 
  303.  
  304. add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) ); 
  305. add_action( 'jetpack_sync_full_cron', array( __CLASS__, 'do_cron_full_sync' ) ); 
  306.  
  307. /** 
  308. * Allows overriding of the default incremental sync cron schedule which defaults to once every 5 minutes. 
  309. * @since 4.3.2 
  310. * @param string self::DEFAULT_SYNC_CRON_INTERVAL_NAME 
  311. */ 
  312. $incremental_sync_cron_schedule = apply_filters( 'jetpack_sync_incremental_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL_NAME ); 
  313. self::maybe_schedule_sync_cron( $incremental_sync_cron_schedule, 'jetpack_sync_cron' ); 
  314.  
  315. /** 
  316. * Allows overriding of the full sync cron schedule which defaults to once every 5 minutes. 
  317. * @since 4.3.2 
  318. * @param string self::DEFAULT_SYNC_CRON_INTERVAL_NAME 
  319. */ 
  320. $full_sync_cron_schedule = apply_filters( 'jetpack_sync_full_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL_NAME ); 
  321. self::maybe_schedule_sync_cron( $full_sync_cron_schedule, 'jetpack_sync_full_cron' ); 
  322.  
  323. static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { 
  324. if ( wp_next_scheduled( 'jetpack_sync_send_db_checksum' ) ) { 
  325. wp_clear_scheduled_hook( 'jetpack_sync_send_db_checksum' ); 
  326.  
  327. $is_new_sync_upgrade = version_compare( $old_version, '4.2', '>=' ); 
  328. if ( ! empty( $old_version ) && $is_new_sync_upgrade && version_compare( $old_version, '4.5', '<' ) ) { 
  329. require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; 
  330. self::clear_sync_cron_jobs(); 
  331. Jetpack_Sync_Settings::update_settings( array( 
  332. 'render_filtered_content' => Jetpack_Sync_Defaults::$default_render_filtered_content 
  333. ) ); 
  334.  
  335. static function get_sync_status() { 
  336. self::initialize_sender(); 
  337.  
  338. $sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' ); 
  339. $queue = self::$sender->get_sync_queue(); 
  340. $full_queue = self::$sender->get_full_sync_queue(); 
  341. $cron_timestamps = array_keys( _get_cron_array() ); 
  342. $next_cron = $cron_timestamps[0] - time(); 
  343.  
  344. return array_merge( 
  345. $sync_module->get_status(),  
  346. array( 
  347. 'cron_size' => count( $cron_timestamps ),  
  348. 'next_cron' => $next_cron,  
  349. 'queue_size' => $queue->size(),  
  350. 'queue_lag' => $queue->lag(),  
  351. 'queue_next_sync' => ( self::$sender->get_next_sync_time( 'sync' ) - microtime( true ) ),  
  352. 'full_queue_size' => $full_queue->size(),  
  353. 'full_queue_lag' => $full_queue->lag(),  
  354. 'full_queue_next_sync' => ( self::$sender->get_next_sync_time( 'full_sync' ) - microtime( true ) ),  
  355. );