|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-deflate-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_Deflate_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
|
|
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function get_concurrent_request_transient_name( $blog_id ) { |
|
35
|
|
|
return self::BLOG_LOCK_TRANSIENT_PREFIX.$blog_id; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
function remove_request_lock( $blog_id ) { |
|
39
|
|
|
delete_site_transient( $this->get_concurrent_request_transient_name( $blog_id ) ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
function receive( $data, $token = null ) { |
|
43
|
|
|
$start_time = microtime( true ); |
|
44
|
|
|
if ( ! is_array( $data ) ) { |
|
45
|
|
|
return new WP_Error( 'action_decoder_error', 'Events must be an array' ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ( $token && ! $this->attempt_request_lock( $token->blog_id ) ) { |
|
49
|
|
|
/** |
|
50
|
|
|
* Fires when the server receives two concurrent requests from the same blog |
|
51
|
|
|
* |
|
52
|
|
|
* @since 4.1 |
|
53
|
|
|
* |
|
54
|
|
|
* @param token The token object of the misbehaving site |
|
55
|
|
|
*/ |
|
56
|
|
|
do_action( "jetpack_sync_multi_request_fail", $token ); |
|
57
|
|
|
return new WP_Error( 'concurrent_request_error', 'There is another request running for the same blog ID' ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$events = wp_unslash( array_map( array( $this->codec, 'decode' ), $data ) ); |
|
61
|
|
|
$events_processed = array(); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Fires when an array of actions are received from a remote Jetpack site |
|
65
|
|
|
* |
|
66
|
|
|
* @since 4.1 |
|
67
|
|
|
* |
|
68
|
|
|
* @param array Array of actions received from the remote site |
|
69
|
|
|
*/ |
|
70
|
|
|
do_action( "jetpack_sync_remote_actions", $events, $token ); |
|
71
|
|
|
|
|
72
|
|
|
foreach ( $events as $key => $event ) { |
|
73
|
|
|
list( $action_name, $args, $user_id, $timestamp ) = $event; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Fires when an action is received from a remote Jetpack site |
|
77
|
|
|
* |
|
78
|
|
|
* @since 4.1 |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $action_name The name of the action executed on the remote site |
|
81
|
|
|
* @param array $args The arguments passed to the action |
|
82
|
|
|
* @param int $user_id The external_user_id who did the action |
|
83
|
|
|
* @param double $timestamp Timestamp (in seconds) when the action occurred |
|
84
|
|
|
* @param array $token The auth token used to invoke the API |
|
85
|
|
|
*/ |
|
86
|
|
|
do_action( 'jetpack_sync_remote_action', $action_name, $args, $user_id, $timestamp, $token ); |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Fires when an action is received from a remote Jetpack site |
|
90
|
|
|
* |
|
91
|
|
|
* @since 4.1 |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $args The arguments passed to the action |
|
94
|
|
|
* @param int $user_id The external_user_id who did the action |
|
95
|
|
|
* @param double $timestamp Timestamp (in seconds) when the action occurred |
|
96
|
|
|
* @param array $token The auth token used to invoke the API |
|
97
|
|
|
*/ |
|
98
|
|
|
do_action( 'jetpack_sync_' . $action_name, $args, $user_id, $timestamp, $token ); |
|
99
|
|
|
|
|
100
|
|
|
$events_processed[] = $key; |
|
101
|
|
|
|
|
102
|
|
|
if ( microtime( true ) - $start_time > self::MAX_TIME_PER_REQUEST_IN_SECONDS ) { |
|
103
|
|
|
break; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ( $token ) { |
|
108
|
|
|
$this->remove_request_lock( $token->blog_id ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $events_processed; |
|
112
|
|
|
} |
|
113
|
|
|
} |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.