Jetpack_Sync_Server

Simple version of a Jetpack Sync Server - just receives arrays of events and issues them locally with the 'jetpack_sync_remote_action' action.

Defined (1)

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

/sync/class.jetpack-sync-server.php  
  1. class Jetpack_Sync_Server { 
  2. private $codec; 
  3. const MAX_TIME_PER_REQUEST_IN_SECONDS = 15; 
  4. const BLOG_LOCK_TRANSIENT_PREFIX = 'jp_sync_req_lock_'; 
  5. const BLOG_LOCK_TRANSIENT_EXPIRY = 60; // seconds 
  6.  
  7. // this is necessary because you can't use "new" when you declare instance properties >:( 
  8. function __construct() { 
  9. $this->codec = new Jetpack_Sync_JSON_Deflate_Array_Codec(); 
  10.  
  11. function set_codec( iJetpack_Sync_Codec $codec ) { 
  12. $this->codec = $codec; 
  13.  
  14. function attempt_request_lock( $blog_id, $expiry = self::BLOG_LOCK_TRANSIENT_EXPIRY ) { 
  15. $transient_name = $this->get_concurrent_request_transient_name( $blog_id ); 
  16. $locked_time = get_site_transient( $transient_name ); 
  17. if ( $locked_time ) { 
  18. return false; 
  19. set_site_transient( $transient_name, microtime( true ), $expiry ); 
  20.  
  21. return true; 
  22.  
  23. private function get_concurrent_request_transient_name( $blog_id ) { 
  24. return self::BLOG_LOCK_TRANSIENT_PREFIX . $blog_id; 
  25.  
  26. function remove_request_lock( $blog_id ) { 
  27. delete_site_transient( $this->get_concurrent_request_transient_name( $blog_id ) ); 
  28.  
  29. function receive( $data, $token = null, $sent_timestamp = null, $queue_id = null ) { 
  30. $start_time = microtime( true ); 
  31. if ( ! is_array( $data ) ) { 
  32. return new WP_Error( 'action_decoder_error', 'Events must be an array' ); 
  33.  
  34. if ( $token && ! $this->attempt_request_lock( $token->blog_id ) ) { 
  35. /** 
  36. * Fires when the server receives two concurrent requests from the same blog 
  37. * @since 4.2.0 
  38. * @param token The token object of the misbehaving site 
  39. */ 
  40. do_action( 'jetpack_sync_multi_request_fail', $token ); 
  41.  
  42. return new WP_Error( 'concurrent_request_error', 'There is another request running for the same blog ID' ); 
  43.  
  44. $events = wp_unslash( array_map( array( $this->codec, 'decode' ), $data ) ); 
  45. $events_processed = array(); 
  46.  
  47. /** 
  48. * Fires when an array of actions are received from a remote Jetpack site 
  49. * @since 4.2.0 
  50. * @param array Array of actions received from the remote site 
  51. */ 
  52. do_action( 'jetpack_sync_remote_actions', $events, $token ); 
  53.  
  54. foreach ( $events as $key => $event ) { 
  55. list( $action_name, $args, $user_id, $timestamp, $silent ) = $event; 
  56.  
  57. /** 
  58. * Fires when an action is received from a remote Jetpack site 
  59. * @since 4.2.0 
  60. * @param string $action_name The name of the action executed on the remote site 
  61. * @param array $args The arguments passed to the action 
  62. * @param int $user_id The external_user_id who did the action 
  63. * @param bool $silent Whether the item was created via import 
  64. * @param double $timestamp Timestamp (in seconds) when the action occurred 
  65. * @param double $sent_timestamp Timestamp (in seconds) when the action was transmitted 
  66. * @param string $queue_id ID of the queue from which the event was sent (sync or full_sync) 
  67. * @param array $token The auth token used to invoke the API 
  68. */ 
  69. do_action( 'jetpack_sync_remote_action', $action_name, $args, $user_id, $silent, $timestamp, $sent_timestamp, $queue_id, $token ); 
  70.  
  71. $events_processed[] = $key; 
  72.  
  73. if ( microtime( true ) - $start_time > self::MAX_TIME_PER_REQUEST_IN_SECONDS ) { 
  74. break; 
  75.  
  76. if ( $token ) { 
  77. $this->remove_request_lock( $token->blog_id ); 
  78.  
  79. return $events_processed;