| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /** |
| 3 | 4 | * API Handler |
| 4 | 5 | * |
| 5 | 6 | * |
| @@ -5,12 +6,17 @@ | ||
| 5 | 6 | * |
| 6 | 7 | * |
| 7 | 8 | * @since 1.0.0 |
| 8 | 9 | */ |
| 10 | + | |
| 9 | 11 | namespace wpCloud\StatelessMedia { |
| 10 | 12 | |
| 11 | - if( !class_exists( 'wpCloud\StatelessMedia\API' ) ) { | |
| 13 | + use wpCloud\StatelessMedia\Sync\FileSync; | |
| 14 | + use wpCloud\StatelessMedia\Sync\ImageSync; | |
| 15 | + use wpCloud\StatelessMedia\Sync\NonLibrarySync; | |
| 12 | 16 | |
| 17 | + if (!class_exists('wpCloud\StatelessMedia\API')) { | |
| 18 | + | |
| 13 | 19 | /** |
| 14 | 20 | * Class API |
| 15 | 21 | * |
| 16 | 22 | * @package wpCloud\StatelessMedia |
| @@ -29,16 +35,20 @@ | ||
| 29 | 35 | * |
| 30 | 36 | * @param \WP_REST_Request $request |
| 31 | 37 | * @return bool|\WP_Error |
| 32 | 38 | */ |
| 33 | - static public function authCheck( \WP_REST_Request $request ) { | |
| 39 | + static public function authCheck(\WP_REST_Request $request) { | |
| 34 | 40 | $auth_token = $request->get_header('authorization'); |
| 35 | - if ( !$auth_token ) return false; | |
| 41 | + // Allow using custom `x-wps-auth` header if authorization hedaer is disabled | |
| 42 | + if (!$auth_token) $auth_token = $request->get_header('x-wps-auth'); | |
| 43 | + | |
| 44 | + if (!$auth_token) return false; | |
| 45 | + | |
| 36 | 46 | try { |
| 37 | - self::$tokenData = Utility::verify_jwt_token( $auth_token ); | |
| 38 | - } catch ( \Exception $e ) { | |
| 47 | + self::$tokenData = Utility::verify_jwt_token($auth_token); | |
| 48 | + } catch (\Exception $e) { | |
| 39 | 49 | self::$tokenData = null; |
| 40 | - return new \WP_Error( 'auth_failed', $e->getMessage(), ['status' => 401] ); | |
| 50 | + return new \WP_Error('auth_failed', $e->getMessage(), ['status' => 401]); | |
| 41 | 51 | } |
| 42 | 52 | return true; |
| 43 | 53 | } |
| 44 | 54 | |
| @@ -47,12 +57,12 @@ | ||
| 47 | 57 | * |
| 48 | 58 | * @return \WP_REST_Response |
| 49 | 59 | */ |
| 50 | 60 | static public function status() { |
| 51 | - return new \WP_REST_Response( array( | |
| 61 | + return new \WP_REST_Response(array( | |
| 52 | 62 | "ok" => true, |
| 53 | 63 | "message" => "API up." |
| 54 | - ), 200 ); | |
| 64 | + ), 200); | |
| 55 | 65 | } |
| 56 | 66 | |
| 57 | 67 | /** |
| 58 | 68 | * Get settings |
| @@ -60,9 +70,9 @@ | ||
| 60 | 70 | * @todo Implement this if needed |
| 61 | 71 | * @param \WP_REST_Request $request |
| 62 | 72 | * @return \WP_REST_Response|\WP_Error |
| 63 | 73 | */ |
| 64 | - static public function getSettings( \WP_REST_Request $request ) { | |
| 74 | + static public function getSettings(\WP_REST_Request $request) { | |
| 65 | 75 | return new \WP_Error('not_implemented', 'Method not implemented', ['status' => 501]); |
| 66 | 76 | } |
| 67 | 77 | |
| 68 | 78 | /** |
| @@ -70,24 +80,24 @@ | ||
| 70 | 80 | * |
| 71 | 81 | * @param \WP_REST_Request $request |
| 72 | 82 | * @return \WP_REST_Response|\WP_Error |
| 73 | 83 | */ |
| 74 | - static public function updateSettings( \WP_REST_Request $request ) { | |
| 75 | - if ( self::$tokenData === null || empty( self::$tokenData->user_id ) ) { | |
| 76 | - return new \WP_Error( 'unauthorized', 'Auth token looks incorrect', ['status' => 401] ); | |
| 84 | + static public function updateSettings(\WP_REST_Request $request) { | |
| 85 | + if (self::$tokenData === null || empty(self::$tokenData->user_id)) { | |
| 86 | + return new \WP_Error('unauthorized', 'Auth token looks incorrect', ['status' => 401]); | |
| 77 | 87 | } |
| 78 | - $is_gae = isset($_SERVER["GAE_VERSION"]) ? true : false; | |
| 88 | + $is_gae = apply_filters('wp_stateless_is_app_engine', false); | |
| 79 | 89 | $upload_dir = wp_upload_dir(); |
| 80 | - $is_upload_dir_writable = is_writable( $upload_dir['basedir'] ); | |
| 90 | + $is_upload_dir_writable = is_writable($upload_dir['basedir']); | |
| 81 | 91 | |
| 82 | 92 | try { |
| 83 | 93 | $queryParams = $request->get_json_params(); |
| 84 | - if ( empty( $queryParams ) ) throw new \Exception('Query is empty'); | |
| 94 | + if (empty($queryParams)) throw new \Exception('Query is empty'); | |
| 85 | 95 | |
| 86 | - $bucketName = isset($queryParams['bucket_name'])?$queryParams['bucket_name']:null; | |
| 87 | - $privateKeyData = isset($queryParams['private_key_data'])?$queryParams['private_key_data']:null; | |
| 96 | + $bucketName = isset($queryParams['bucket_name']) ? $queryParams['bucket_name'] : null; | |
| 97 | + $privateKeyData = isset($queryParams['private_key_data']) ? $queryParams['private_key_data'] : null; | |
| 88 | 98 | |
| 89 | - if ( !$bucketName || !$privateKeyData ) { | |
| 99 | + if (!$bucketName || !$privateKeyData) { | |
| 90 | 100 | throw new \Exception('bucket_name and private_key_data are required'); |
| 91 | 101 | } |
| 92 | 102 | |
| 93 | 103 | if ($privateKeyData) { |
| @@ -93,55 +103,53 @@ | ||
| 93 | 103 | if ($privateKeyData) { |
| 94 | 104 | $privateKeyData = base64_decode($privateKeyData); |
| 95 | 105 | } |
| 96 | 106 | |
| 97 | - switch ( self::$tokenData->is_network ) { | |
| 107 | + switch (self::$tokenData->is_network) { | |
| 98 | 108 | case true: |
| 99 | - if ( !user_can( self::$tokenData->user_id, 'manage_network_options' ) ) { | |
| 100 | - return new \WP_Error( 'not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403] ); | |
| 109 | + if (!user_can(self::$tokenData->user_id, 'manage_network_options')) { | |
| 110 | + return new \WP_Error('not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403]); | |
| 101 | 111 | } |
| 102 | 112 | /** |
| 103 | 113 | * If Google App Engine detected - set Stateless mode |
| 104 | 114 | * and Google App Engine compatibility by default |
| 105 | 115 | */ |
| 106 | - if ( $is_gae || !$is_upload_dir_writable ) { | |
| 107 | - update_site_option( 'sm_mode', 'stateless' ); | |
| 116 | + if ($is_gae || !$is_upload_dir_writable) { | |
| 117 | + update_site_option('sm_mode', 'stateless'); | |
| 108 | 118 | |
| 109 | 119 | $modules = get_site_option('stateless-modules', array()); |
| 110 | - if ( $is_gae && empty($modules['google-app-engine']) || $modules['google-app-engine'] != 'true') { | |
| 120 | + if ($is_gae && empty($modules['google-app-engine']) || $modules['google-app-engine'] != 'true') { | |
| 111 | 121 | $modules['google-app-engine'] = 'true'; |
| 112 | - update_site_option('stateless-modules', $modules ); | |
| 122 | + update_site_option('stateless-modules', $modules); | |
| 113 | 123 | } |
| 124 | + } elseif (get_site_option('sm_mode', 'disabled') == 'disabled') { | |
| 125 | + update_site_option('sm_mode', 'ephemeral'); | |
| 114 | 126 | } |
| 115 | - elseif ( get_site_option('sm_mode', 'disabled') == 'disabled' ) { | |
| 116 | - update_site_option( 'sm_mode', 'cdn'); | |
| 117 | - } | |
| 118 | - update_site_option( 'sm_bucket', $bucketName); | |
| 119 | - update_site_option( 'sm_key_json', $privateKeyData); | |
| 127 | + update_site_option('sm_bucket', $bucketName); | |
| 128 | + update_site_option('sm_key_json', $privateKeyData); | |
| 120 | 129 | break; |
| 121 | 130 | |
| 122 | 131 | case false: |
| 123 | - if ( !user_can( self::$tokenData->user_id, 'manage_options' ) ) { | |
| 124 | - return new \WP_Error( 'not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403] ); | |
| 132 | + if (!user_can(self::$tokenData->user_id, 'manage_options')) { | |
| 133 | + return new \WP_Error('not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403]); | |
| 125 | 134 | } |
| 126 | 135 | /** |
| 127 | 136 | * If Google App Engine detected - set Stateless mode |
| 128 | 137 | * and Google App Engine compatibility by default |
| 129 | 138 | */ |
| 130 | - if ( $is_gae || !$is_upload_dir_writable ) { | |
| 131 | - update_option( 'sm_mode', 'stateless' ); | |
| 139 | + if ($is_gae || !$is_upload_dir_writable) { | |
| 140 | + update_option('sm_mode', 'stateless'); | |
| 132 | 141 | |
| 133 | 142 | $modules = get_option('stateless-modules', array()); |
| 134 | - if ( $is_gae && empty($modules['google-app-engine']) || $modules['google-app-engine'] != 'true') { | |
| 143 | + if ($is_gae && empty($modules['google-app-engine']) || $modules['google-app-engine'] != 'true') { | |
| 135 | 144 | $modules['google-app-engine'] = 'true'; |
| 136 | - update_option('stateless-modules', $modules ); | |
| 145 | + update_option('stateless-modules', $modules); | |
| 137 | 146 | } |
| 147 | + } elseif (get_option('sm_mode', 'disabled') == 'disabled') { | |
| 148 | + update_option('sm_mode', 'ephemeral'); | |
| 138 | 149 | } |
| 139 | - elseif ( get_option('sm_mode', 'disabled') == 'disabled') { | |
| 140 | - update_option('sm_mode', 'cdn'); | |
| 141 | - } | |
| 142 | - update_option( 'sm_bucket', $bucketName); | |
| 143 | - update_option( 'sm_key_json', $privateKeyData); | |
| 150 | + update_option('sm_bucket', $bucketName); | |
| 151 | + update_option('sm_key_json', $privateKeyData); | |
| 144 | 152 | break; |
| 145 | 153 | } |
| 146 | 154 | |
| 147 | 155 | return new \WP_REST_Response(array( |
| @@ -148,13 +156,180 @@ | ||
| 148 | 156 | 'ok' => true, |
| 149 | 157 | 'message' => 'Settings updated successfully' |
| 150 | 158 | )); |
| 151 | 159 | } catch (\Throwable $e) { |
| 152 | - return new \WP_Error( 'internal_server_error', $e->getMessage(), ['status' => 500] ); | |
| 160 | + return new \WP_Error('internal_server_error', $e->getMessage(), ['status' => 500]); | |
| 153 | 161 | } |
| 154 | 162 | } |
| 155 | 163 | |
| 164 | + /** | |
| 165 | + * Get all available processes with their states | |
| 166 | + * | |
| 167 | + * @return \WP_REST_Response|\WP_Error | |
| 168 | + */ | |
| 169 | + static public function syncGetProcesses() { | |
| 170 | + try { | |
| 171 | + if (!user_can(self::$tokenData->user_id, 'manage_options')) { | |
| 172 | + return new \WP_Error('not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403]); | |
| 173 | + } | |
| 174 | + | |
| 175 | + return new \WP_REST_Response(array( | |
| 176 | + 'ok' => true, | |
| 177 | + 'data' => Utility::get_available_sync_classes() | |
| 178 | + )); | |
| 179 | + } catch (\Throwable $e) { | |
| 180 | + return new \WP_Error('internal_server_error', $e->getMessage(), ['status' => 500]); | |
| 181 | + } | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Get a single process by id (base64 encoded class name) | |
| 186 | + * | |
| 187 | + * @param \WP_REST_Request $request | |
| 188 | + * @return \WP_Error|\WP_REST_Response | |
| 189 | + */ | |
| 190 | + static public function syncGetProcess(\WP_REST_Request $request) { | |
| 191 | + try { | |
| 192 | + if (!user_can(self::$tokenData->user_id, 'manage_options')) { | |
| 193 | + return new \WP_Error('not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403]); | |
| 194 | + } | |
| 195 | + | |
| 196 | + $id = base64_decode($request->get_param('id')); | |
| 197 | + if (!class_exists($id)) { | |
| 198 | + throw new \Exception(sprintf('Could not get process by id %s', $id)); | |
| 199 | + } | |
| 200 | + | |
| 201 | + $syncClasses = Utility::get_available_sync_classes(); | |
| 202 | + | |
| 203 | + if (!array_key_exists($id, $syncClasses)) { | |
| 204 | + throw new \Exception(sprintf('Could not get process by id %s', $id)); | |
| 205 | + } | |
| 206 | + | |
| 207 | + return new \WP_REST_Response(array( | |
| 208 | + 'ok' => true, | |
| 209 | + 'data' => $syncClasses[$id] | |
| 210 | + )); | |
| 211 | + } catch (\Throwable $e) { | |
| 212 | + return new \WP_Error('internal_server_error', $e->getMessage(), ['status' => 500]); | |
| 213 | + } | |
| 214 | + } | |
| 215 | + | |
| 216 | + /** | |
| 217 | + * Run sync by processing class id | |
| 218 | + * | |
| 219 | + * @param \WP_REST_Request $request | |
| 220 | + * @return \WP_Error|\WP_REST_Response | |
| 221 | + */ | |
| 222 | + static public function syncRun(\WP_REST_Request $request) { | |
| 223 | + try { | |
| 224 | + if (!user_can(self::$tokenData->user_id, 'manage_options')) { | |
| 225 | + return new \WP_Error('not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403]); | |
| 226 | + } | |
| 227 | + | |
| 228 | + $params = wp_parse_args($request->get_params(), [ | |
| 229 | + 'id' => null, | |
| 230 | + 'limit' => null, | |
| 231 | + 'order' => null, | |
| 232 | + ]); | |
| 233 | + | |
| 234 | + if (empty($params['id']) || !class_exists($params['id'])) { | |
| 235 | + throw new \Exception(sprintf('Processing class not found: %s', $params['id'])); | |
| 236 | + } | |
| 237 | + | |
| 238 | + $processingClass = $params['id']; | |
| 239 | + | |
| 240 | + return new \WP_REST_Response(array( | |
| 241 | + 'ok' => $processingClass::instance()->start($params) | |
| 242 | + )); | |
| 243 | + } catch (\Throwable $e) { | |
| 244 | + return new \WP_Error('internal_server_error', $e->getMessage(), ['status' => 500]); | |
| 245 | + } | |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Stop sync by processing class id | |
| 250 | + * | |
| 251 | + * @param \WP_REST_Request $request | |
| 252 | + * @return \WP_Error|\WP_REST_Response | |
| 253 | + */ | |
| 254 | + static public function syncStop(\WP_REST_Request $request) { | |
| 255 | + try { | |
| 256 | + if (!user_can(self::$tokenData->user_id, 'manage_options')) { | |
| 257 | + return new \WP_Error('not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403]); | |
| 258 | + } | |
| 259 | + | |
| 260 | + $params = wp_parse_args($request->get_params(), [ | |
| 261 | + 'id' => null | |
| 262 | + ]); | |
| 263 | + | |
| 264 | + if (empty($params['id']) || !class_exists($params['id'])) { | |
| 265 | + throw new \Exception(sprintf('Processing class not found: %s', $params['id'])); | |
| 266 | + } | |
| 267 | + | |
| 268 | + $processingClass = $params['id']; | |
| 269 | + | |
| 270 | + return new \WP_REST_Response(array( | |
| 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, | |
| 328 | + )); | |
| 329 | + } catch (\Throwable $e) { | |
| 330 | + return new \WP_Error('internal_server_error', $e->getMessage(), ['status' => 500]); | |
| 331 | + } | |
| 332 | + } | |
| 156 | 333 | } |
| 157 | - | |
| 158 | 334 | } |
| 159 | - | |
| 160 | 335 | } |