Completed
Push — dna-stats-module ( 8da522...2880ce )
by
unknown
382:09 queued 374:09
created

Jetpack_Sync_Server::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Simple version of a Jetpack Sync Server - just receives arrays of events and
5
 * issues them locally with the 'jetpack_sync_remote_action' action.
6
 */
7
class Jetpack_Sync_Server {
8
	private $codec;
9
	const MAX_TIME_PER_REQUEST_IN_SECONDS = 15;
10
	const BLOG_LOCK_TRANSIENT_PREFIX      = 'jp_sync_req_lock_';
11
	const BLOG_LOCK_TRANSIENT_EXPIRY      = 60; // seconds
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_JSON_Deflate_Array_Codec();
16
	}
17
18
	function set_codec( iJetpack_Sync_Codec $codec ) {
19
		$this->codec = $codec;
20
	}
21
22
	function attempt_request_lock( $blog_id, $expiry = self::BLOG_LOCK_TRANSIENT_EXPIRY ) {
23
		$transient_name = $this->get_concurrent_request_transient_name( $blog_id );
24
		$locked_time    = get_site_transient( $transient_name );
25
		if ( $locked_time ) {
26
			return false;
27
		}
28
		set_site_transient( $transient_name, microtime( true ), $expiry );
29
30
		return true;
31
	}
32
33
	private function get_concurrent_request_transient_name( $blog_id ) {
34
		return self::BLOG_LOCK_TRANSIENT_PREFIX . $blog_id;
35
	}
36
37
	function remove_request_lock( $blog_id ) {
38
		delete_site_transient( $this->get_concurrent_request_transient_name( $blog_id ) );
39
	}
40
41
	function receive( $data, $token = null, $sent_timestamp = null, $queue_id = null ) {
42
		$start_time = microtime( true );
43
		if ( ! is_array( $data ) ) {
44
			return new WP_Error( 'action_decoder_error', 'Events must be an array' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'action_decoder_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
		}
46
47
		if ( $token && ! $this->attempt_request_lock( $token->blog_id ) ) {
48
			/**
49
			 * Fires when the server receives two concurrent requests from the same blog
50
			 *
51
			 * @since 4.2.0
52
			 *
53
			 * @param token The token object of the misbehaving site
54
			 */
55
			do_action( 'jetpack_sync_multi_request_fail', $token );
56
57
			return new WP_Error( 'concurrent_request_error', 'There is another request running for the same blog ID' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'concurrent_request_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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.2.0
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, $silent ) = $event;
74
75
			/**
76
			 * Fires when an action is received from a remote Jetpack site
77
			 *
78
			 * @since 4.2.0
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 bool $silent Whether the item was created via import
84
			 * @param double $timestamp Timestamp (in seconds) when the action occurred
85
			 * @param double $sent_timestamp Timestamp (in seconds) when the action was transmitted
86
			 * @param string $queue_id ID of the queue from which the event was sent (sync or full_sync)
87
			 * @param array $token The auth token used to invoke the API
88
			 */
89
			do_action( 'jetpack_sync_remote_action', $action_name, $args, $user_id, $silent, $timestamp, $sent_timestamp, $queue_id, $token );
90
91
			$events_processed[] = $key;
92
93
			if ( microtime( true ) - $start_time > self::MAX_TIME_PER_REQUEST_IN_SECONDS ) {
94
				break;
95
			}
96
		}
97
98
		if ( $token ) {
99
			$this->remove_request_lock( $token->blog_id );
100
		}
101
102
		return $events_processed;
103
	}
104
}
105