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     * This endpoint is only accessible from Jetpack Backup; it requires no further capabilities.
24     *
25     * @var array
26     */
27    protected $needed_capabilities = array();
28
29    /**
30     * Method to call when running this endpoint (install)
31     *
32     * @var string
33     */
34    protected $action = 'install';
35
36    /**
37     * Contents of the Helper Script to install
38     *
39     * @var string|null
40     */
41    protected $helper_script = null;
42
43    /**
44     * Contains the result of installing the Helper Script.
45     *
46     * @var null|WP_Error|array
47     */
48    protected $result = null;
49
50    /**
51     * Checks that the input args look like a valid Helper Script.
52     *
53     * @param  null $object  Unused.
54     * @return bool|WP_Error a WP_Error object or true if the input seems ok.
55     */
56    protected function validate_input( $object ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
57        $args = $this->input();
58
59        if ( ! isset( $args['helper'] ) ) {
60            return new WP_Error( 'invalid_args', __( 'You must specify a helper script body', 'jetpack' ), 400 );
61        }
62
63        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
64        $this->helper_script = base64_decode( $args['helper'] );
65        if ( ! $this->helper_script ) {
66            return new WP_Error( 'invalid_args', __( 'Helper script body must be base64 encoded', 'jetpack' ), 400 );
67        }
68
69        return true;
70    }
71
72    /**
73     * Installs the uploaded Helper Script.
74     */
75    protected function install() {
76        $this->result = Helper_Script_Manager::install_helper_script( $this->helper_script );
77        Helper_Script_Manager::cleanup_expired_helper_scripts();
78    }
79
80    /**
81     * Return the success or failure of the backup helper script installation operation.
82     *
83     * @return array|WP_Error An array with installation info on success:
84     *
85     *   'path'    (string) Helper script installation path on the filesystem.
86     *   'url'     (string) URL to the helper script.
87     *   'abspath' (string) WordPress root.
88     *
89     *   or an instance of WP_Error on failure.
90     */
91    protected function result() {
92        return $this->result;
93    }
94}