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