Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.35% covered (warning)
82.35%
14 / 17
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_IXR_ClientMulticall
86.67% covered (warning)
86.67%
13 / 15
66.67% covered (warning)
66.67%
2 / 3
5.06
0.00% covered (danger)
0.00%
0 / 1
 addCall
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 query
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 sort_calls
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * IXR_ClientMulticall
4 *
5 * @package automattic/jetpack-connection
6 *
7 * @since 1.7.0
8 * @since-jetpack 1.5
9 * @since-jetpack 7.7 Moved to the jetpack-connection package.
10 */
11
12if ( ! defined( 'ABSPATH' ) ) {
13    exit( 0 );
14}
15
16/**
17 * A Jetpack implementation of the WordPress core IXR client, capable of multiple calls in a single request.
18 */
19class Jetpack_IXR_ClientMulticall extends Jetpack_IXR_Client {
20    /**
21     * Storage for the IXR calls.
22     *
23     * @var array
24     */
25    public $calls = array();
26
27    /**
28     * Add a IXR call to the client.
29     * First argument is the method name.
30     * The rest of the arguments are the params specified to the method.
31     *
32     * @param string[] ...$args IXR args.
33     */
34    public function addCall( ...$args ) {
35        $method_name   = array_shift( $args );
36        $struct        = array(
37            'methodName' => $method_name,
38            'params'     => $args,
39        );
40        $this->calls[] = $struct;
41    }
42
43    /**
44     * Perform the IXR multicall request.
45     *
46     * @param string[] ...$args IXR args.
47     *
48     * @return bool True if request succeeded, false otherwise.
49     */
50    public function query( ...$args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
51        $this->calls = $this->sort_calls( $this->calls );
52
53        // Prepare multicall, then call the parent::query() method.
54        return parent::query( 'system.multicall', $this->calls );
55    }
56
57    /**
58     * Sort the IXR calls.
59     * Make sure syncs are always done first preserving relative order.
60     *
61     * @param array $calls Calls to sort.
62     * @return array Sorted calls.
63     */
64    public function sort_calls( $calls ) {
65        $sync_calls  = array();
66        $other_calls = array();
67
68        foreach ( $calls as $call ) {
69            if ( 'jetpack.syncContent' === $call['methodName'] ) {
70                $sync_calls[] = $call;
71            } else {
72                $other_calls[] = $call;
73            }
74        }
75
76        return array_merge( $sync_calls, $other_calls );
77    }
78}