1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WCCOM Site Installer REST API Controller |
4
|
|
|
* |
5
|
|
|
* Handles requests to /installer. |
6
|
|
|
* |
7
|
|
|
* @package WooCommerce\WooCommerce_Site\Rest_Api |
8
|
|
|
* @since 3.7.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
defined( 'ABSPATH' ) || exit; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* REST API WCCOM Site Installer Controller Class. |
15
|
|
|
* |
16
|
|
|
* @package WooCommerce/WCCOM_Site/REST_API |
17
|
|
|
* @extends WC_REST_Controller |
18
|
|
|
*/ |
19
|
|
|
class WC_REST_WCCOM_Site_Installer_Controller extends WC_REST_Controller { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Endpoint namespace. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $namespace = 'wccom-site/v1'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Route base. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $rest_base = 'installer'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Register the routes for product reviews. |
37
|
|
|
* |
38
|
|
|
* @since 3.7.0 |
39
|
|
|
*/ |
40
|
1 |
|
public function register_routes() { |
41
|
1 |
|
register_rest_route( |
42
|
1 |
|
$this->namespace, |
43
|
1 |
|
'/' . $this->rest_base, |
44
|
|
|
array( |
45
|
|
|
array( |
46
|
1 |
|
'methods' => WP_REST_Server::READABLE, |
47
|
1 |
|
'callback' => array( $this, 'get_install_state' ), |
48
|
1 |
|
'permission_callback' => array( $this, 'check_permission' ), |
49
|
|
|
), |
50
|
|
|
array( |
51
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
52
|
1 |
|
'callback' => array( $this, 'install' ), |
53
|
1 |
|
'permission_callback' => array( $this, 'check_permission' ), |
54
|
|
|
), |
55
|
|
|
array( |
56
|
|
|
'methods' => WP_REST_Server::DELETABLE, |
57
|
1 |
|
'callback' => array( $this, 'reset_install' ), |
58
|
1 |
|
'permission_callback' => array( $this, 'check_permission' ), |
59
|
|
|
), |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check permissions. |
66
|
|
|
* |
67
|
|
|
* @since 3.7.0 |
68
|
|
|
* @param WP_REST_Request $request Full details about the request. |
69
|
|
|
* @return bool|WP_Error |
70
|
|
|
*/ |
71
|
|
|
public function check_permission( $request ) { |
72
|
|
|
if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'install_themes' ) ) { |
73
|
|
|
return new WP_Error( 'woocommerce_rest_cannot_install_product', __( 'You do not have permission to install plugin or theme', 'woocommerce' ), array( 'status' => 401 ) ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get installation state. |
81
|
|
|
* |
82
|
|
|
* @since 3.7.0 |
83
|
|
|
* @param WP_REST_Request $request Full details about the request. |
84
|
|
|
* @return bool|WP_Error |
85
|
|
|
*/ |
86
|
|
|
public function get_install_state( $request ) { |
87
|
|
|
return rest_ensure_response( WC_WCCOM_Site_Installer::get_state() ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Install WooCommerce.com products. |
92
|
|
|
* |
93
|
|
|
* @since 3.7.0 |
94
|
|
|
* @param WP_REST_Request $request Full details about the request. |
95
|
|
|
* @return bool|WP_Error |
96
|
|
|
*/ |
97
|
|
|
public function install( $request ) { |
98
|
|
|
if ( empty( $request['products'] ) ) { |
99
|
|
|
return new WP_Error( 'missing_products', __( 'Missing products in request body.', 'woocommerce' ), array( 'status' => 400 ) ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$validation_result = $this->validate_products( $request['products'] ); |
103
|
|
|
if ( is_wp_error( $validation_result ) ) { |
104
|
|
|
return $validation_result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return rest_ensure_response( WC_WCCOM_Site_Installer::schedule_install( $request['products'] ) ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Reset installation state. |
112
|
|
|
* |
113
|
|
|
* @since 3.7.0 |
114
|
|
|
* @param WP_REST_Request $request Full details about the request. |
115
|
|
|
* @return bool|WP_Error |
116
|
|
|
*/ |
117
|
|
|
public function reset_install( $request ) { |
118
|
|
|
$resp = rest_ensure_response( WC_WCCOM_Site_Installer::reset_state() ); |
119
|
|
|
$resp->set_status( 204 ); |
120
|
|
|
|
121
|
|
|
return $resp; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Validate products from request body. |
126
|
|
|
* |
127
|
|
|
* @since 3.7.0 |
128
|
|
|
* @param array $products Array of products where key is product ID and |
129
|
|
|
* element is install args. |
130
|
|
|
* @return bool|WP_Error |
131
|
|
|
*/ |
132
|
|
|
protected function validate_products( $products ) { |
133
|
|
|
$err = new WP_Error( 'invalid_products', __( 'Invalid products in request body.', 'woocommerce' ), array( 'status' => 400 ) ); |
134
|
|
|
|
135
|
|
|
if ( ! is_array( $products ) ) { |
136
|
|
|
return $err; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
foreach ( $products as $product_id => $install_args ) { |
140
|
|
|
if ( ! absint( $product_id ) ) { |
141
|
|
|
return $err; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ( empty( $install_args ) || ! is_array( $install_args ) ) { |
145
|
|
|
return $err; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|