| @@ -84,9 +84,9 @@ | ||
| 84 | 84 | static public function updateSettings(\WP_REST_Request $request) { |
| 85 | 85 | if (self::$tokenData === null || empty(self::$tokenData->user_id)) { |
| 86 | 86 | return new \WP_Error('unauthorized', 'Auth token looks incorrect', ['status' => 401]); |
| 87 | 87 | } |
| 88 | - $is_gae = isset($_SERVER["GAE_VERSION"]) ? true : false; | |
| 88 | + $is_gae = apply_filters('wp_stateless_is_app_engine', false); | |
| 89 | 89 | $upload_dir = wp_upload_dir(); |
| 90 | 90 | $is_upload_dir_writable = is_writable($upload_dir['basedir']); |
| 91 | 91 | |
| 92 | 92 | try { |
| @@ -268,8 +268,64 @@ | ||
| 268 | 268 | $processingClass = $params['id']; |
| 269 | 269 | |
| 270 | 270 | return new \WP_REST_Response(array( |
| 271 | 271 | 'ok' => $processingClass::instance()->stop() |
| 272 | + )); | |
| 273 | + } catch (\Throwable $e) { | |
| 274 | + return new \WP_Error('internal_server_error', $e->getMessage(), ['status' => 500]); | |
| 275 | + } | |
| 276 | + } | |
| 277 | + | |
| 278 | + /** | |
| 279 | + * Batch Status Endpoint. | |
| 280 | + * | |
| 281 | + * @param \WP_REST_Request $request | |
| 282 | + * @return \WP_REST_Response | |
| 283 | + */ | |
| 284 | + static public function batchState(\WP_REST_Request $request) { | |
| 285 | + try { | |
| 286 | + return new \WP_REST_Response(array( | |
| 287 | + 'ok' => true, | |
| 288 | + 'data' => apply_filters('wp_stateless_batch_state', [], $request->get_params()), | |
| 289 | + )); | |
| 290 | + } catch (\Throwable $e) { | |
| 291 | + return new \WP_Error('internal_server_error', $e->getMessage(), ['status' => 500]); | |
| 292 | + } | |
| 293 | + } | |
| 294 | + | |
| 295 | + /** | |
| 296 | + * Batch action (start/pause/resume) | |
| 297 | + * | |
| 298 | + * @param \WP_REST_Request $request | |
| 299 | + * @return \WP_Error|\WP_REST_Response | |
| 300 | + */ | |
| 301 | + static public function batchAction(\WP_REST_Request $request) { | |
| 302 | + try { | |
| 303 | + if (!user_can(self::$tokenData->user_id, 'manage_options')) { | |
| 304 | + return new \WP_Error('not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403]); | |
| 305 | + } | |
| 306 | + | |
| 307 | + wp_set_current_user(self::$tokenData->user_id); | |
| 308 | + | |
| 309 | + $params = wp_parse_args($request->get_params(), [ | |
| 310 | + 'action' => '', | |
| 311 | + ]); | |
| 312 | + | |
| 313 | + if ( empty($params['action']) ) { | |
| 314 | + throw new \Exception('Batch action not set'); | |
| 315 | + } | |
| 316 | + | |
| 317 | + $action = $params['action']; | |
| 318 | + | |
| 319 | + $data = apply_filters("wp_stateless_batch_action_$action", [], $params); | |
| 320 | + | |
| 321 | + $data = array_merge( | |
| 322 | + $data, | |
| 323 | + apply_filters("wp_stateless_batch_state", [], ['force_migrations' => true]), | |
| 324 | + ); | |
| 325 | + | |
| 326 | + return new \WP_REST_Response(array( | |
| 327 | + 'data' => $data, | |
| 272 | 328 | )); |
| 273 | 329 | } catch (\Throwable $e) { |
| 274 | 330 | return new \WP_Error('internal_server_error', $e->getMessage(), ['status' => 500]); |
| 275 | 331 | } |