PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-connection/src/class-xmlrpc-async-call.php +106 -0 16.2-beta → 16.3-beta View file →
@@ -1,0 +1,106 @@
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 + // https://plugins.trac.wordpress.org/ticket/2041
64 + if ( function_exists( 'ignore_user_abort' ) ) {
65 + ignore_user_abort( true );
66 + }
67 +
68 + array_unshift( $args, $method );
69 +
70 + call_user_func_array( array( self::$clients[ $client_blog_id ][ $user_id ], 'addCall' ), $args );
71 +
72 + if ( false === has_action( 'shutdown', array( 'Automattic\Jetpack\Connection\XMLRPC_Async_Call', 'do_calls' ) ) ) {
73 + add_action( 'shutdown', array( 'Automattic\Jetpack\Connection\XMLRPC_Async_Call', 'do_calls' ) );
74 + }
75 + }
76 +
77 + /**
78 + * Trigger the calls at shutdown
79 + *
80 + * @return void
81 + */
82 + public static function do_calls() {
83 + foreach ( self::$clients as $client_blog_id => $blog_clients ) {
84 + if ( $client_blog_id > 0 ) {
85 + $switch_success = switch_to_blog( $client_blog_id );
86 +
87 + if ( ! $switch_success ) {
88 + continue;
89 + }
90 + }
91 +
92 + foreach ( $blog_clients as $client ) {
93 + if ( empty( $client->calls ) ) {
94 + continue;
95 + }
96 +
97 + flush();
98 + $client->query();
99 + }
100 +
101 + if ( $client_blog_id > 0 ) {
102 + restore_current_blog();
103 + }
104 + }
105 + }
106 +}