Completed
Push — add/xmlrpc-async-call-lib ( 56c669...8afe88 )
by
unknown
153:15 queued 143:12
created

XMLRPC_Async_Call::do_calls_multisite()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 24

Duplication

Lines 16
Ratio 66.67 %

Importance

Changes 0
Metric Value
cc 6
nc 3
nop 0
dl 16
loc 24
rs 8.9137
c 0
b 0
f 0
1
<?php
2
/**
3
 * XMLRPC Async Call class.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
use Jetpack_IXR_ClientMulticall;
11
12
/**
13
 * Make XMLRPC async calls to WordPress.com
14
 *
15
 * This class allows you to enqueue XMLRPC calls that will be grouped and sent
16
 * at once in a multi-call request at shutdown.
17
 *
18
 * Usage:
19
 *
20
 * XMLRPC_Async_Call::add_call( 'methodName', get_current_user_id(), $arg1, $arg2, etc... )
21
 *
22
 * See XMLRPC_Async_Call::add_call for details
23
 */
24
class XMLRPC_Async_Call {
25
26
	/**
27
	 * Hold the IXR Clients that will be dispatched at shutdown
28
	 *
29
	 * Clients are stored in the following schema:
30
	 * [
31
	 *  $blog_id => [
32
	 *    $user_id => [
33
	 *      arrat of Jetpack_IXR_ClientMulticall
34
	 *    ]
35
	 *  ]
36
	 * ]
37
	 *
38
	 * @var array
39
	 */
40
	public static $clients = array();
41
42
	/**
43
	 * Adds a new XMLRPC call to the queue to be processed on shutdown
44
	 *
45
	 * @param string  $method The XML-RPC method.
46
	 * @param integer $user_id The user ID used to make the request (will use this user's token); Use 0 for the blog token.
47
	 * @param mixed   ...$args This function accepts any number of additional arguments, that will be passed to the call.
48
	 * @return void
49
	 */
50
	public static function add_call( $method, $user_id = 0, ...$args ) {
51
		global $blog_id;
52
53
		$client_blog_id = is_multisite() ? $blog_id : 0;
54
55
		if ( ! isset( self::$clients[ $client_blog_id ] ) ) {
56
			self::$clients[ $client_blog_id ] = array();
57
		}
58
59
		if ( ! isset( self::$clients[ $client_blog_id ][ $user_id ] ) ) {
60
			self::$clients[ $client_blog_id ][ $user_id ] = new Jetpack_IXR_ClientMulticall( array( 'user_id' => $user_id ) );
61
		}
62
63
		if ( function_exists( 'ignore_user_abort' ) ) {
64
			ignore_user_abort( true );
65
		}
66
67
		array_unshift( $args, $method );
68
69
		call_user_func_array( array( self::$clients[ $client_blog_id ][ $user_id ], 'addCall' ), $args );
70
71
		if ( false === has_action( 'shutdown', array( 'Automattic\Jetpack\Connection\XMLRPC_Async_Call', 'do_calls' ) ) ) {
72
			add_action( 'shutdown', array( 'Automattic\Jetpack\Connection\XMLRPC_Async_Call', 'do_calls' ) );
73
		}
74
	}
75
76
	/**
77
	 * Trigger the calls at shutdown
78
	 *
79
	 * @return void
80
	 */
81
	public static function do_calls() {
82
		if ( is_multisite() ) {
83
			self::do_calls_multisite();
84
		} else {
85
			self::do_calls_single_site();
86
		}
87
	}
88
89
	/**
90
	 * Trigger the calls when in a multi-site environment
91
	 *
92
	 * @return void
93
	 */
94
	private static function do_calls_multisite() {
95
		foreach ( self::$clients as $client_blog_id => $blog_clients ) {
96
			if ( 0 === $client_blog_id ) {
97
				continue;
98
			}
99
100 View Code Duplication
			foreach ( $blog_clients as $client ) {
101
				if ( empty( $client->calls ) ) {
102
					continue;
103
				}
104
105
				$switch_success = switch_to_blog( $client_blog_id, true );
106
107
				if ( ! $switch_success ) {
108
					continue;
109
				}
110
111
				flush();
112
				$client->query();
113
114
				restore_current_blog();
115
			}
116
		}
117
	}
118
119
	/**
120
	 * Trigger the calls in a single site environment
121
	 *
122
	 * @return void
123
	 */
124
	private static function do_calls_single_site() {
125
		if ( ! isset( self::$clients[0] ) ) {
126
			return;
127
		}
128
129
		foreach ( self::$clients[0] as $client ) {
130
			if ( empty( $client->calls ) ) {
131
				continue;
132
			}
133
134
			flush();
135
			$client->query();
136
137
		}
138
	}
139
}
140