|
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
|
|
|
|
|
13
|
|
|
// this is necessary because you can't use "new" when you declare instance properties >:( |
|
14
|
|
|
function __construct() { |
|
15
|
|
|
$this->codec = new Jetpack_Sync_Deflate_Codec(); |
|
16
|
|
|
$this->events_processed = array(); |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
function set_codec( iJetpack_Sync_Codec $codec ) { |
|
20
|
|
|
$this->codec = $codec; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
function receive( $data, $token = null ) { |
|
24
|
|
|
$start_time = microtime( true ); |
|
25
|
|
|
if ( ! is_array( $data ) ) { |
|
26
|
|
|
return new WP_Error( 'action_decoder_error', 'Events must be an array' ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$events = wp_unslash( array_map( array( $this->codec, 'decode' ), $data ) ); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Fires when an array of actions are received from a remote Jetpack site |
|
33
|
|
|
* |
|
34
|
|
|
* @since 4.1 |
|
35
|
|
|
* |
|
36
|
|
|
* @param array Array of actions received from the remote site |
|
37
|
|
|
*/ |
|
38
|
|
|
do_action( "jetpack_sync_remote_actions", $events, $token ); |
|
39
|
|
|
|
|
40
|
|
|
foreach ( $events as $key => $event ) { |
|
41
|
|
|
list( $action_name, $args, $user_id, $timestamp ) = $event; |
|
42
|
|
|
/** |
|
43
|
|
|
* Fires when an action is received from a remote Jetpack site |
|
44
|
|
|
* |
|
45
|
|
|
* @since 4.1 |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $action_name The name of the action executed on the remote site |
|
48
|
|
|
* @param array $args The arguments passed to the action |
|
49
|
|
|
* @param int $user_id The external_user_id who did the action |
|
50
|
|
|
* @param double $timestamp Timestamp (in seconds) when the action occurred |
|
51
|
|
|
* @param array $token The auth token used to invoke the API |
|
52
|
|
|
*/ |
|
53
|
|
|
do_action( 'jetpack_sync_remote_action', $action_name, $args, $user_id, $timestamp, $token ); |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Fires when an action is received from a remote Jetpack site |
|
57
|
|
|
* |
|
58
|
|
|
* @since 4.1 |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $args The arguments passed to the action |
|
61
|
|
|
* @param int $user_id The external_user_id who did the action |
|
62
|
|
|
* @param double $timestamp Timestamp (in seconds) when the action occurred |
|
63
|
|
|
* @param array $token The auth token used to invoke the API |
|
64
|
|
|
*/ |
|
65
|
|
|
do_action( 'jetpack_sync_' . $action_name, $args, $user_id, $timestamp, $token ); |
|
66
|
|
|
|
|
67
|
|
|
$this->events_processed[] = $key; |
|
68
|
|
|
|
|
69
|
|
|
// TODO this can be improved to be more intelligent |
|
|
|
|
|
|
70
|
|
|
if ( microtime( true ) - $start_time > self::MAX_TIME_PER_REQUEST_IN_SECONDS ) { |
|
71
|
|
|
break; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->events_processed; |
|
76
|
|
|
} |
|
77
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: