Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_JSON_API_Delete_Backup_Helper_Script_Endpoint
0.00% covered (danger)
0.00%
0 / 11
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 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 delete
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 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/delete-backup-helper-script
4 * This API endpoint deletes a Jetpack Backup Helper Script
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/delete-backup-helper-script
17 * This API endpoint deletes a Jetpack Backup Helper Script
18 *
19 * @phan-constructor-used-for-side-effects
20 */
21class Jetpack_JSON_API_Delete_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 (delete)
33     *
34     * @var string
35     */
36    protected $action = 'delete';
37
38    /**
39     * Local path to the Helper Script to delete.
40     *
41     * @var string|null
42     */
43    protected $script_path = null;
44
45    /**
46     * An array with 'success' => true if the specified file has been successfully deleted, or an instance of WP_Error.
47     *
48     * @var array|WP_Error
49     */
50    protected $result;
51
52    /**
53     * Checks that the input args look like a valid Helper Script path.
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['path'] ) ) {
62            return new WP_Error( 'invalid_args', __( 'You must specify a helper script path', 'jetpack' ), 400 );
63        }
64
65        $this->script_path = $args['path'];
66        return true;
67    }
68
69    /**
70     * Deletes the specified Helper Script.
71     */
72    protected function delete() {
73        $delete_result = Helper_Script_Manager::delete_helper_script( $this->script_path );
74        Helper_Script_Manager::cleanup_expired_helper_scripts();
75
76        if ( is_wp_error( $delete_result ) ) {
77            $this->result = $delete_result;
78        } else {
79            $this->result = array( 'success' => true );
80        }
81    }
82
83    /**
84     * Returns the success or failure of the deletion operation
85     *
86     * @return array An array containing one key; 'success', which specifies whether the operation was successful.
87     */
88    protected function result() {
89        return $this->result;
90    }
91}