| 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 |
} |
| 107 |
|