Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_JSON_API_Install_Backup_Helper_Script_Endpoint
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 validate_input
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 install
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 result
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * API endpoint /sites/%s/install-backup-helper-script
4 * This API endpoint installs a Helper Script to assist Jetpack Backup fetch data
5 *
6 * @package automattic/jetpack
7 */
8
9use Automattic\Jetpack\Backup\V0005\Helper_Script_Manager;
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15/**
16 * API endpoint /sites/%s/install-backup-helper-script
17 * This API endpoint installs a Helper Script to assist Jetpack Backup fetch data
18 *
19 * @phan-constructor-used-for-side-effects
20 */
21class Jetpack_JSON_API_Install_Backup_Helper_Script_Endpoint extends Jetpack_JSON_API_Endpoint {
22    /**
23     * An empty capability set means this endpoint is only accessible using a site token: it is
24     * reachable with a Jetpack site (blog) token and denied for every user token, however
25     * privileged. See Jetpack_JSON_API_Endpoint::check_capability().
26     *
27     * @var array
28     */
29    protected $needed_capabilities = array();
30
31    /**
32     * Method to call when running this endpoint (install)
33     *
34     * @var string
35     */
36    protected $action = 'install';
37
38    /**
39     * Contents of the Helper Script to install
40     *
41     * @var string|null
42     */
43    protected $helper_script = null;
44
45    /**
46     * Contains the result of installing the Helper Script.
47     *
48     * @var null|WP_Error|array
49     */
50    protected $result = null;
51
52    /**
53     * Checks that the input args look like a valid Helper Script.
54     *
55     * @param  null $object  Unused.
56     * @return bool|WP_Error a WP_Error object or true if the input seems ok.
57     */
58    protected function validate_input( $object ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
59        $args = $this->input();
60
61        if ( ! isset( $args['helper'] ) ) {
62            return new WP_Error( 'invalid_args', __( 'You must specify a helper script body', 'jetpack' ), 400 );
63        }
64
65        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
66        $this->helper_script = base64_decode( $args['helper'] );
67        if ( ! $this->helper_script ) {
68            return new WP_Error( 'invalid_args', __( 'Helper script body must be base64 encoded', 'jetpack' ), 400 );
69        }
70
71        return true;
72    }
73
74    /**
75     * Installs the uploaded Helper Script.
76     */
77    protected function install() {
78        $this->result = Helper_Script_Manager::install_helper_script( $this->helper_script );
79        Helper_Script_Manager::cleanup_expired_helper_scripts();
80    }
81
82    /**
83     * Return the success or failure of the backup helper script installation operation.
84     *
85     * @return array|WP_Error An array with installation info on success:
86     *
87     *   'path'    (string) Helper script installation path on the filesystem.
88     *   'url'     (string) URL to the helper script.
89     *   'abspath' (string) WordPress root.
90     *
91     *   or an instance of WP_Error on failure.
92     */
93    protected function result() {
94        return $this->result;
95    }
96}