Jetpack_Sync_Listener

This class monitors actions and logs them to the queue to be sent.

Defined (1)

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

/sync/class.jetpack-sync-listener.php  
  1. class Jetpack_Sync_Listener { 
  2. const QUEUE_STATE_CHECK_TRANSIENT = 'jetpack_sync_last_checked_queue_state'; 
  3. const QUEUE_STATE_CHECK_TIMEOUT = 300; // 5 minutes 
  4.  
  5. private $sync_queue; 
  6. private $full_sync_queue; 
  7. private $sync_queue_size_limit; 
  8. private $sync_queue_lag_limit; 
  9.  
  10. // singleton functions 
  11. private static $instance; 
  12.  
  13. public static function get_instance() { 
  14. if ( null === self::$instance ) { 
  15. self::$instance = new self(); 
  16.  
  17. return self::$instance; 
  18.  
  19. // this is necessary because you can't use "new" when you declare instance properties >:( 
  20. protected function __construct() { 
  21. $this->set_defaults(); 
  22. $this->init(); 
  23.  
  24. private function init() { 
  25. $handler = array( $this, 'action_handler' ); 
  26. $full_sync_handler = array( $this, 'full_sync_action_handler' ); 
  27.  
  28. foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { 
  29. $module->init_listeners( $handler ); 
  30. $module->init_full_sync_listeners( $full_sync_handler ); 
  31.  
  32. // Module Activation 
  33. add_action( 'jetpack_activate_module', $handler ); 
  34. add_action( 'jetpack_deactivate_module', $handler ); 
  35.  
  36. // Jetpack Upgrade 
  37. add_action( 'updating_jetpack_version', $handler, 10, 2 ); 
  38.  
  39. // Send periodic checksum 
  40. add_action( 'jetpack_sync_checksum', $handler ); 
  41.  
  42. function get_sync_queue() { 
  43. return $this->sync_queue; 
  44.  
  45. function get_full_sync_queue() { 
  46. return $this->full_sync_queue; 
  47.  
  48. function set_queue_size_limit( $limit ) { 
  49. $this->sync_queue_size_limit = $limit; 
  50.  
  51. function get_queue_size_limit() { 
  52. return $this->sync_queue_size_limit; 
  53.  
  54. function set_queue_lag_limit( $age ) { 
  55. $this->sync_queue_lag_limit = $age; 
  56.  
  57. function get_queue_lag_limit() { 
  58. return $this->sync_queue_lag_limit; 
  59.  
  60. function force_recheck_queue_limit() { 
  61. delete_transient( self::QUEUE_STATE_CHECK_TRANSIENT . '_' . $this->sync_queue->id ); 
  62. delete_transient( self::QUEUE_STATE_CHECK_TRANSIENT . '_' . $this->full_sync_queue->id ); 
  63.  
  64. // prevent adding items to the queue if it hasn't sent an item for 15 mins 
  65. // AND the queue is over 1000 items long (by default) 
  66. function can_add_to_queue( $queue ) { 
  67. if ( Jetpack_Sync_Settings::get_setting( 'disable' ) ) { 
  68. return false; 
  69.  
  70. $state_transient_name = self::QUEUE_STATE_CHECK_TRANSIENT . '_' . $queue->id; 
  71.  
  72. $queue_state = get_transient( $state_transient_name ); 
  73.  
  74. if ( false === $queue_state ) { 
  75. $queue_state = array( $queue->size(), $queue->lag() ); 
  76. set_transient( $state_transient_name, $queue_state, self::QUEUE_STATE_CHECK_TIMEOUT ); 
  77.  
  78. list( $queue_size, $queue_age ) = $queue_state; 
  79.  
  80. return ( $queue_age < $this->sync_queue_lag_limit ) 
  81. || 
  82. ( ( $queue_size + 1 ) < $this->sync_queue_size_limit ); 
  83.  
  84. function full_sync_action_handler() { 
  85. $args = func_get_args(); 
  86. $this->enqueue_action( current_filter(), $args, $this->full_sync_queue ); 
  87.  
  88. function action_handler() { 
  89. $args = func_get_args(); 
  90. $this->enqueue_action( current_filter(), $args, $this->sync_queue ); 
  91.  
  92. // add many actions to the queue directly, without invoking them 
  93. function bulk_enqueue_full_sync_actions( $action_name, $args_array ) { 
  94. $queue = $this->get_full_sync_queue(); 
  95.  
  96. // periodically check the size of the queue, and disable adding to it if 
  97. // it exceeds some limit AND the oldest item exceeds the age limit (i.e. sending has stopped) 
  98. if ( ! $this->can_add_to_queue( $queue ) ) { 
  99. return; 
  100.  
  101. // if we add any items to the queue, we should try to ensure that our script  
  102. // can't be killed before they are sent 
  103. if ( function_exists( 'ignore_user_abort' ) ) { 
  104. ignore_user_abort( true ); 
  105.  
  106. $data_to_enqueue = array(); 
  107. $user_id = get_current_user_id(); 
  108. $currtime = microtime( true ); 
  109. $is_importing = Jetpack_Sync_Settings::is_importing(); 
  110.  
  111. foreach( $args_array as $args ) { 
  112.  
  113. /** 
  114. * Modify or reject the data within an action before it is enqueued locally. 
  115. * @since 4.2.0 
  116. * @param array The action parameters 
  117. */ 
  118. $args = apply_filters( "jetpack_sync_before_enqueue_$action_name", $args ); 
  119.  
  120. // allow listeners to abort 
  121. if ( $args === false ) { 
  122. continue; 
  123.  
  124. $data_to_enqueue[] = array( 
  125. $action_name,  
  126. array( $args ),  
  127. $user_id,  
  128. $currtime,  
  129. $is_importing,  
  130. ); 
  131.  
  132. $queue->add_all( $data_to_enqueue ); 
  133.  
  134. function enqueue_action( $current_filter, $args, $queue ) { 
  135. // don't enqueue an action during the outbound http request - this prevents recursion 
  136. if ( Jetpack_Sync_Settings::is_sending() ) { 
  137. return; 
  138.  
  139. /** 
  140. * Modify or reject the data within an action before it is enqueued locally. 
  141. * @since 4.2.0 
  142. * @param array The action parameters 
  143. */ 
  144. $args = apply_filters( "jetpack_sync_before_enqueue_$current_filter", $args ); 
  145.  
  146. // allow listeners to abort 
  147. if ( $args === false ) { 
  148. return; 
  149.  
  150. // periodically check the size of the queue, and disable adding to it if 
  151. // it exceeds some limit AND the oldest item exceeds the age limit (i.e. sending has stopped) 
  152. if ( ! $this->can_add_to_queue( $queue ) ) { 
  153. return; 
  154.  
  155. // if we add any items to the queue, we should try to ensure that our script  
  156. // can't be killed before they are sent 
  157. if ( function_exists( 'ignore_user_abort' ) ) { 
  158. ignore_user_abort( true ); 
  159.  
  160. if ( 
  161. 'sync' === $queue->id || 
  162. in_array( 
  163. $current_filter,  
  164. array( 
  165. 'jetpack_full_sync_start',  
  166. 'jetpack_full_sync_end',  
  167. 'jetpack_full_sync_cancel' 
  168. ) { 
  169. $queue->add( array( 
  170. $current_filter,  
  171. $args,  
  172. get_current_user_id(),  
  173. microtime( true ),  
  174. Jetpack_Sync_Settings::is_importing(),  
  175. $this->get_actor(),  
  176. ) ); 
  177. } else { 
  178. $queue->add( array( 
  179. $current_filter,  
  180. $args,  
  181. get_current_user_id(),  
  182. microtime( true ),  
  183. Jetpack_Sync_Settings::is_importing() 
  184. ) ); 
  185.  
  186. // since we've added some items, let's try to load the sender so we can send them as quickly as possible 
  187. if ( ! Jetpack_Sync_Actions::$sender ) { 
  188. add_filter( 'jetpack_sync_sender_should_load', '__return_true' ); 
  189. if ( did_action( 'init' ) ) { 
  190. Jetpack_Sync_Actions::add_sender_shutdown(); 
  191. function get_actor() { 
  192. $current_user = wp_get_current_user(); 
  193. $actor = array(); 
  194. if ( $current_user ) { 
  195. $actor[ 'display_name' ] = $current_user->display_name; 
  196. $actor[ 'user_email' ] = $current_user->user_email; 
  197.  
  198. if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { 
  199. $actor[ 'ip' ] = $_SERVER['REMOTE_ADDR']; 
  200.  
  201. $actor['is_cron'] = defined( 'DOING_CRON' ) ? DOING_CRON : false; 
  202. $actor['is_wp_admin'] = is_admin(); 
  203. $actor['is_rest'] = defined( 'REST_API_REQUEST' ) ? REST_API_REQUEST : false; 
  204. $actor['is_xmlrpc'] = defined( 'XMLRPC_REQUEST' ) ? XMLRPC_REQUEST : false; 
  205. $actor['is_wp_rest'] = defined( 'REST_REQUEST' ) ? REST_REQUEST : false; 
  206. $actor['is_ajax'] = defined( 'DOING_AJAX' ) ? DOING_AJAX : false; 
  207.  
  208. return $actor; 
  209.  
  210. function set_defaults() { 
  211. $this->sync_queue = new Jetpack_Sync_Queue( 'sync' ); 
  212. $this->full_sync_queue = new Jetpack_Sync_Queue( 'full_sync' ); 
  213. $this->set_queue_size_limit( Jetpack_Sync_Settings::get_setting( 'max_queue_size' ) ); 
  214. $this->set_queue_lag_limit( Jetpack_Sync_Settings::get_setting( 'max_queue_lag' ) );