|
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
|
|
|
foreach ( self::$clients as $client_blog_id => $blog_clients ) { |
|
83
|
|
|
if ( $client_blog_id > 0 ) { |
|
84
|
|
|
$switch_success = switch_to_blog( $client_blog_id, true ); |
|
85
|
|
|
|
|
86
|
|
|
if ( ! $switch_success ) { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
foreach ( $blog_clients as $client ) { |
|
92
|
|
|
if ( empty( $client->calls ) ) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
flush(); |
|
97
|
|
|
$client->query(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ( $client_blog_id > 0 ) { |
|
101
|
|
|
restore_current_blog(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|