summaryrefslogtreecommitdiff
blob: da71873f87d9cf697cfd815c9679a394382c5673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
 * IXR_ClientMulticall
 *
 * @package automattic/jetpack-connection
 *
 * @since 1.5
 * @since 7.7 Moved to the jetpack-connection package.
 */

/**
 * A Jetpack implementation of the WordPress core IXR client, capable of multiple calls in a single request.
 */
class Jetpack_IXR_ClientMulticall extends Jetpack_IXR_Client {
	/**
	 * Storage for the IXR calls.
	 *
	 * @var array
	 */
	public $calls = array();

	/**
	 * Add a IXR call to the client.
	 * First argument is the method name.
	 * The rest of the arguments are the params specified to the method.
	 */
	public function addCall() {
		$args          = func_get_args();
		$method_name   = array_shift( $args );
		$struct        = array(
			'methodName' => $method_name,
			'params'     => $args,
		);
		$this->calls[] = $struct;
	}

	/**
	 * Perform the IXR multicall request.
	 *
	 * @return bool True if request succeeded, false otherwise.
	 */
	public function query() {
		usort( $this->calls, array( $this, 'sort_calls' ) );

		// Prepare multicall, then call the parent::query() method.
		return parent::query( 'system.multicall', $this->calls );
	}

	/**
	 * Sort the IXR calls.
	 * Make sure syncs are always done first.
	 *
	 * @param array $a First call in the sorting iteration.
	 * @param array $b Second call in the sorting iteration.
	 * @return int Result of the sorting iteration.
	 */
	public function sort_calls( $a, $b ) {
		if ( 'jetpack.syncContent' === $a['methodName'] ) {
			return -1;
		}

		if ( 'jetpack.syncContent' === $b['methodName'] ) {
			return 1;
		}

		return 0;
	}
}