| @@ -8,13 +8,10 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * Class Optml_Rest |
| 12 | - * | |
| 13 | - * @codeCoverageIgnore | |
| 14 | 12 | */ |
| 15 | 13 | class Optml_Rest { |
| 16 | - | |
| 17 | 14 | /** |
| 18 | 15 | * Rest api namespace. |
| 19 | 16 | * |
| 20 | 17 | * @var string Namespace. |
| @@ -21,244 +18,101 @@ | ||
| 21 | 18 | */ |
| 22 | 19 | private $namespace; |
| 23 | 20 | |
| 24 | 21 | /** |
| 25 | - * Upload conflicts api. | |
| 26 | - * | |
| 27 | - * @var array upload_conflicts_api. | |
| 28 | - */ | |
| 29 | - public static $rest_routes = [ | |
| 30 | - 'service_routes' => ['update_option' => 'POST', 'request_update' => 'GET', 'check_redirects' => 'POST_PUT_PATCH', | |
| 31 | - 'connect' => [ 'POST', 'args' => [ | |
| 32 | - 'api_key' => [ | |
| 33 | - 'type' => 'string', | |
| 34 | - 'required' => true, | |
| 35 | - ], | |
| 36 | - ], | |
| 37 | - ], | |
| 38 | - 'select_application' => [ 'POST', 'args' => [ | |
| 39 | - 'api_key' => [ | |
| 40 | - 'type' => 'string', | |
| 41 | - 'required' => true, | |
| 42 | - ], | |
| 43 | - 'application' => [ | |
| 44 | - 'type' => 'string', | |
| 45 | - 'required' => true, | |
| 46 | - ], | |
| 47 | - ], | |
| 48 | - ], | |
| 49 | - 'register_service' => [ 'POST', 'args' => [ | |
| 50 | - 'email' => [ | |
| 51 | - 'type' => 'string', | |
| 52 | - 'required' => true, | |
| 53 | - ], | |
| 54 | - ], | |
| 55 | - | |
| 56 | - ], | |
| 57 | - 'disconnect' => 'GET', | |
| 58 | - ], | |
| 59 | - 'image_routes' => [ | |
| 60 | - 'poll_optimized_images' => 'GET', | |
| 61 | - 'get_sample_rate' => 'POST', | |
| 62 | - 'upload_onboard_images' => [ 'POST', 'args' => [ | |
| 63 | - 'offset' => [ | |
| 64 | - 'type' => 'number', | |
| 65 | - 'required' => false, | |
| 66 | - 'default' => 0, | |
| 67 | - ], | |
| 68 | - ], | |
| 69 | - ], | |
| 70 | - ], | |
| 71 | - 'media_cloud_routes' => [ | |
| 72 | - 'number_of_images_and_pages' => 'POST', | |
| 73 | - 'get_offload_conflicts' => 'GET', | |
| 74 | - ], | |
| 75 | - 'watermark_routes' => [ | |
| 76 | - 'poll_watermarks' => 'GET', | |
| 77 | - 'add_watermark' => 'POST', | |
| 78 | - 'remove_watermark' => 'POST', | |
| 79 | - ], | |
| 80 | - 'conflict_routes' => [ | |
| 81 | - 'poll_conflicts' => 'GET', | |
| 82 | - 'dismiss_conflict' => 'POST', | |
| 83 | - ], | |
| 84 | - 'cache_routes' => [ | |
| 85 | - 'clear_cache_request' => 'POST', | |
| 86 | - ], | |
| 87 | - 'dam_routes' => [ | |
| 88 | - 'insert_images' => [ | |
| 89 | - 'POST', | |
| 90 | - 'args' => [ | |
| 91 | - 'images' => [ | |
| 92 | - 'type' => 'array', | |
| 93 | - 'required' => true, | |
| 94 | - ], | |
| 95 | - ], | |
| 96 | - 'permission_callback' => 'upload_files', | |
| 97 | - ], | |
| 98 | - ], | |
| 99 | - ]; | |
| 100 | - | |
| 101 | - /** | |
| 102 | 22 | * Optml_Rest constructor. |
| 103 | 23 | */ |
| 104 | 24 | public function __construct() { |
| 105 | 25 | $this->namespace = OPTML_NAMESPACE . '/v1'; |
| 106 | - | |
| 107 | - add_action( 'rest_api_init', [ $this, 'register' ] ); | |
| 26 | + add_action( 'rest_api_init', array( $this, 'register' ) ); | |
| 108 | 27 | } |
| 109 | 28 | |
| 110 | 29 | /** |
| 111 | - * Method to register a specific rest route. | |
| 112 | - * | |
| 113 | - * @param string $route The route name. | |
| 114 | - * @param string $method The route access method GET, POST, POST_PUT_PATCH. | |
| 115 | - * @param array $args Optional argument to include required args. | |
| 116 | - * @param string $permission_callback Optional permission callback. | |
| 117 | - */ | |
| 118 | - private function reqister_route( $route, $method = 'GET', $args = [], $permission_callback = 'manage_options' ) { | |
| 119 | - $wp_method_constant = false; | |
| 120 | - if ( $method === 'GET' ) { | |
| 121 | - $wp_method_constant = \WP_REST_Server::READABLE; | |
| 122 | - } | |
| 123 | - if ( $method === 'POST' ) { | |
| 124 | - $wp_method_constant = \WP_REST_Server::CREATABLE; | |
| 125 | - } | |
| 126 | - if ( $method === 'POST_PUT_PATCH' ) { | |
| 127 | - $wp_method_constant = \WP_REST_Server::EDITABLE; | |
| 128 | - } | |
| 129 | - if ( $wp_method_constant !== false ) { | |
| 130 | - $params = [ | |
| 131 | - 'methods' => $wp_method_constant, | |
| 132 | - 'permission_callback' => function () use ( $permission_callback ) { | |
| 133 | - return current_user_can( $permission_callback ); | |
| 134 | - }, | |
| 135 | - 'callback' => [ $this, $route ], | |
| 136 | - ]; | |
| 137 | - if ( ! empty( $args ) ) { | |
| 138 | - $params['args'] = $args; | |
| 139 | - } | |
| 140 | - register_rest_route( | |
| 141 | - $this->namespace, | |
| 142 | - '/' . $route, | |
| 143 | - [ | |
| 144 | - $params, | |
| 145 | - ] | |
| 146 | - ); | |
| 147 | - } | |
| 148 | - | |
| 149 | - } | |
| 150 | - | |
| 151 | - /** | |
| 152 | 30 | * Register rest routes. |
| 153 | 31 | */ |
| 154 | 32 | public function register() { |
| 155 | 33 | |
| 156 | - $this->register_service_routes(); | |
| 157 | - | |
| 158 | - $this->register_image_routes(); | |
| 159 | - $this->register_watermark_routes(); | |
| 160 | - $this->register_conflict_routes(); | |
| 161 | - $this->register_cache_routes(); | |
| 162 | - $this->register_media_offload_routes(); | |
| 163 | - $this->register_dam_routes(); | |
| 34 | + register_rest_route( | |
| 35 | + $this->namespace, '/connect', array( | |
| 36 | + array( | |
| 37 | + 'methods' => \WP_REST_Server::CREATABLE, | |
| 38 | + 'permission_callback' => function () { | |
| 39 | + return current_user_can( 'manage_options' ); | |
| 40 | + }, | |
| 41 | + 'callback' => array( $this, 'connect' ), | |
| 42 | + 'args' => array( | |
| 43 | + 'api_key' => array( | |
| 44 | + 'type' => 'string', | |
| 45 | + 'required' => true, | |
| 46 | + ), | |
| 47 | + ), | |
| 48 | + ), | |
| 49 | + ) | |
| 50 | + ); | |
| 51 | + register_rest_route( | |
| 52 | + $this->namespace, '/register', array( | |
| 53 | + array( | |
| 54 | + 'methods' => \WP_REST_Server::CREATABLE, | |
| 55 | + 'permission_callback' => function () { | |
| 56 | + return current_user_can( 'manage_options' ); | |
| 57 | + }, | |
| 58 | + 'callback' => array( $this, 'register_service' ), | |
| 59 | + 'args' => array( | |
| 60 | + 'email' => array( | |
| 61 | + 'type' => 'string', | |
| 62 | + 'required' => true, | |
| 63 | + ), | |
| 64 | + ), | |
| 65 | + ), | |
| 66 | + ) | |
| 67 | + ); | |
| 68 | + register_rest_route( | |
| 69 | + $this->namespace, '/disconnect', array( | |
| 70 | + array( | |
| 71 | + 'methods' => \WP_REST_Server::READABLE, | |
| 72 | + 'permission_callback' => function () { | |
| 73 | + return current_user_can( 'manage_options' ); | |
| 74 | + }, | |
| 75 | + 'callback' => array( $this, 'disconnect' ), | |
| 76 | + ), | |
| 77 | + ) | |
| 78 | + ); | |
| 79 | + register_rest_route( | |
| 80 | + $this->namespace, '/update_option', array( | |
| 81 | + array( | |
| 82 | + 'methods' => \WP_REST_Server::CREATABLE, | |
| 83 | + 'permission_callback' => function () { | |
| 84 | + return current_user_can( 'manage_options' ); | |
| 85 | + }, | |
| 86 | + 'callback' => array( $this, 'update_option' ), | |
| 87 | + ), | |
| 88 | + ) | |
| 89 | + ); | |
| 90 | + register_rest_route( | |
| 91 | + $this->namespace, '/poll_optimized_images', array( | |
| 92 | + array( | |
| 93 | + 'methods' => \WP_REST_Server::READABLE, | |
| 94 | + 'permission_callback' => function () { | |
| 95 | + return current_user_can( 'manage_options' ); | |
| 96 | + }, | |
| 97 | + 'callback' => array( $this, 'poll_optimized_images' ), | |
| 98 | + ), | |
| 99 | + ) | |
| 100 | + ); | |
| 101 | + register_rest_route( | |
| 102 | + $this->namespace, '/images-sample-rate', array( | |
| 103 | + array( | |
| 104 | + 'methods' => \WP_REST_Server::CREATABLE, | |
| 105 | + 'permission_callback' => function () { | |
| 106 | + return current_user_can( 'manage_options' ); | |
| 107 | + }, | |
| 108 | + 'callback' => array( $this, 'get_sample_rate' ), | |
| 109 | + ), | |
| 110 | + ) | |
| 111 | + ); | |
| 164 | 112 | } |
| 165 | 113 | |
| 166 | 114 | /** |
| 167 | - * Method to register service specific routes. | |
| 168 | - */ | |
| 169 | - public function register_service_routes() { | |
| 170 | - foreach ( self::$rest_routes['service_routes'] as $route => $details ) { | |
| 171 | - if ( is_array( $details ) ) { | |
| 172 | - $this->reqister_route( $route, $details[0], $details['args'] ); | |
| 173 | - } else { | |
| 174 | - $this->reqister_route( $route, $details ); | |
| 175 | - } | |
| 176 | - } | |
| 177 | - } | |
| 178 | - | |
| 179 | - /** | |
| 180 | - * Method to register image specific routes. | |
| 181 | - */ | |
| 182 | - public function register_image_routes() { | |
| 183 | - foreach ( self::$rest_routes['image_routes'] as $route => $details ) { | |
| 184 | - if ( is_array( $details ) ) { | |
| 185 | - $this->reqister_route( $route, $details[0], $details['args'] ); | |
| 186 | - } else { | |
| 187 | - $this->reqister_route( $route, $details ); | |
| 188 | - } | |
| 189 | - } | |
| 190 | - | |
| 191 | - } | |
| 192 | - /** | |
| 193 | - * Method to register media offload specific routes. | |
| 194 | - */ | |
| 195 | - public function register_media_offload_routes() { | |
| 196 | - foreach ( self::$rest_routes['media_cloud_routes'] as $route => $details ) { | |
| 197 | - $this->reqister_route( $route, $details ); | |
| 198 | - } | |
| 199 | - } | |
| 200 | - | |
| 201 | - /** | |
| 202 | - * Method to register watermark specific routes. | |
| 203 | - */ | |
| 204 | - public function register_watermark_routes() { | |
| 205 | - foreach ( self::$rest_routes['watermark_routes'] as $route => $details ) { | |
| 206 | - $this->reqister_route( $route, $details ); | |
| 207 | - } | |
| 208 | - } | |
| 209 | - | |
| 210 | - /** | |
| 211 | - * Method to register conflicts specific routes. | |
| 212 | - */ | |
| 213 | - public function register_conflict_routes() { | |
| 214 | - foreach ( self::$rest_routes['conflict_routes'] as $route => $details ) { | |
| 215 | - $this->reqister_route( $route, $details ); | |
| 216 | - } | |
| 217 | - } | |
| 218 | - | |
| 219 | - /** | |
| 220 | - * Method to register cache specific routes. | |
| 221 | - */ | |
| 222 | - public function register_cache_routes() { | |
| 223 | - foreach ( self::$rest_routes['cache_routes'] as $route => $details ) { | |
| 224 | - $this->reqister_route( $route, $details ); | |
| 225 | - } | |
| 226 | - } | |
| 227 | - | |
| 228 | - /** | |
| 229 | - * Register DAM routes. | |
| 230 | - * | |
| 231 | - * @return void | |
| 232 | - */ | |
| 233 | - public function register_dam_routes() { | |
| 234 | - foreach ( self::$rest_routes['dam_routes'] as $route => $details ) { | |
| 235 | - $permission = isset( $details['permission_callback'] ) ? $details['permission_callback'] : 'manage_options'; | |
| 236 | - $args = isset( $details['args'] ) ? $details['args'] : []; | |
| 237 | - $this->reqister_route( $route, $details[0], $args, $permission ); | |
| 238 | - } | |
| 239 | - } | |
| 240 | - | |
| 241 | - /** | |
| 242 | - * Clear Cache request. | |
| 243 | - * | |
| 244 | - * @param WP_REST_Request $request clear cache rest request. | |
| 245 | - * | |
| 246 | - * @return WP_Error|WP_REST_Response | |
| 247 | - */ | |
| 248 | - public function clear_cache_request( WP_REST_Request $request ) { | |
| 249 | - $settings = new Optml_Settings(); | |
| 250 | - $type = $request->get_param( 'type' ); | |
| 251 | - $response = $settings->clear_cache( $type ); | |
| 252 | - | |
| 253 | - if ( is_wp_error( $response ) ) { | |
| 254 | - wp_send_json_error( $response->get_error_message() ); | |
| 255 | - } | |
| 256 | - | |
| 257 | - return $this->response( $response, '200' ); | |
| 258 | - } | |
| 259 | - | |
| 260 | - /** | |
| 261 | 115 | * Connect to optimole service. |
| 262 | 116 | * |
| 263 | 117 | * @param WP_REST_Request $request connect rest request. |
| 264 | 118 | * |
| @@ -265,75 +119,29 @@ | ||
| 265 | 119 | * @return WP_Error|WP_REST_Response |
| 266 | 120 | */ |
| 267 | 121 | public function connect( WP_REST_Request $request ) { |
| 268 | 122 | $api_key = $request->get_param( 'api_key' ); |
| 269 | - $original_request = $request; | |
| 270 | 123 | $request = new Optml_Api(); |
| 271 | - $data = $request->connect( $api_key ); | |
| 272 | - | |
| 273 | - if ( $data === false || is_wp_error( $data ) ) { | |
| 274 | - $extra = ''; | |
| 275 | - if ( is_wp_error( $data ) ) { | |
| 276 | - /** | |
| 277 | - * Error from api. | |
| 278 | - * | |
| 279 | - * @var WP_Error $data Error object. | |
| 280 | - */ | |
| 281 | - $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() ); | |
| 282 | - } | |
| 283 | - return $this->response( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra, 400 ); | |
| 124 | + $data = $request->get_user_data( $api_key ); | |
| 125 | + if ( $data === false ) { | |
| 126 | + wp_send_json_error( __( 'Can not connect to optimole service', 'optimole-wp' ) ); | |
| 284 | 127 | } |
| 285 | 128 | $settings = new Optml_Settings(); |
| 129 | + $settings->update( 'service_data', $data ); | |
| 286 | 130 | $settings->update( 'api_key', $api_key ); |
| 287 | 131 | |
| 288 | - if ( isset( $data['extra_visits'] ) ) { | |
| 289 | - $settings->update_frontend_banner_from_remote( $data['extra_visits'] ); | |
| 290 | - } | |
| 291 | - | |
| 292 | - if ( $data['app_count'] === 1 ) { | |
| 293 | - return $this->select_application( $original_request ); | |
| 294 | - } | |
| 295 | 132 | return $this->response( $data ); |
| 296 | 133 | } |
| 297 | 134 | |
| 298 | 135 | /** |
| 299 | - * Select application. | |
| 300 | - * | |
| 301 | - * @param WP_REST_Request $request Rest request. | |
| 302 | - * | |
| 303 | - * @return WP_REST_Response | |
| 304 | - */ | |
| 305 | - public function select_application( WP_REST_Request $request ) { | |
| 306 | - $api_key = $request->get_param( 'api_key' ); | |
| 307 | - $application = $request->get_param( 'application' ); | |
| 308 | - $request = new Optml_Api(); | |
| 309 | - $data = $request->get_user_data( $api_key, $application ); | |
| 310 | - if ( $data === false || is_wp_error( $data ) ) { | |
| 311 | - $extra = ''; | |
| 312 | - if ( is_wp_error( $data ) ) { | |
| 313 | - /** | |
| 314 | - * Error from api. | |
| 315 | - * | |
| 316 | - * @var WP_Error $data Error object. | |
| 317 | - */ | |
| 318 | - $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() ); | |
| 319 | - } | |
| 320 | - wp_send_json_error( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra ); | |
| 321 | - } | |
| 322 | - $settings = new Optml_Settings(); | |
| 323 | - $settings->update( 'service_data', $data ); | |
| 324 | - return $this->response( $data ); | |
| 325 | - } | |
| 326 | - | |
| 327 | - /** | |
| 328 | 136 | * Wrapper for api response. |
| 329 | 137 | * |
| 330 | - * @param mixed $data data from api. | |
| 138 | + * @param array $data data from api. | |
| 331 | 139 | * |
| 332 | 140 | * @return WP_REST_Response |
| 333 | 141 | */ |
| 334 | - private function response( $data, $code = 'success' ) { | |
| 335 | - return new WP_REST_Response( [ 'data' => $data, 'code' => $code ], 200 ); | |
| 142 | + private function response( $data ) { | |
| 143 | + return new WP_REST_Response( array( 'data' => $data, 'code' => 'success' ), 200 ); | |
| 336 | 144 | } |
| 337 | 145 | |
| 338 | 146 | /** |
| 339 | 147 | * Connect to optimole service. |
| @@ -344,64 +152,14 @@ | ||
| 344 | 152 | */ |
| 345 | 153 | public function register_service( WP_REST_Request $request ) { |
| 346 | 154 | $email = $request->get_param( 'email' ); |
| 347 | 155 | $api = new Optml_Api(); |
| 348 | - | |
| 349 | 156 | $user = $api->create_account( $email ); |
| 350 | - | |
| 351 | - $auto_connect = $request->get_param( 'auto_connect' ); | |
| 352 | - | |
| 353 | - if ( ! empty( $auto_connect ) && $auto_connect === 'true' ) { | |
| 354 | - delete_option( Optml_Settings::OPTML_USER_EMAIL ); | |
| 157 | + if ( $user === false ) { | |
| 158 | + return new WP_Error( 'error', 'Error creating account.' ); | |
| 355 | 159 | } |
| 356 | 160 | |
| 357 | - if ( $user === false || is_wp_error( $user ) ) { | |
| 358 | - return new WP_REST_Response( | |
| 359 | - [ | |
| 360 | - 'data' => null, | |
| 361 | - 'message' => __( 'Error creating account.', 'optimole-wp' ), | |
| 362 | - 'code' => 'error', | |
| 363 | - ], | |
| 364 | - 200 | |
| 365 | - ); | |
| 366 | - } | |
| 367 | - if ( $user === 'email_registered' ) { | |
| 368 | - return new WP_REST_Response( | |
| 369 | - [ | |
| 370 | - 'data' => null, | |
| 371 | - 'message' => __( 'Error: This email is already registered. Please choose another one.', 'optimole-wp' ), | |
| 372 | - 'code' => 'email_registered', | |
| 373 | - ], | |
| 374 | - 200 | |
| 375 | - ); | |
| 376 | - | |
| 377 | - } | |
| 378 | - | |
| 379 | - if ( $user === 'site_exists' ) { | |
| 380 | - return new WP_REST_Response( | |
| 381 | - [ | |
| 382 | - 'data' => null, | |
| 383 | - 'message' => sprintf( __( 'Error: This site has been previously registered. You can login to your account from %1$shere%2$s ', 'optimole-wp' ), '<a href="https://dashboard.optimole.com/login" target="_blank"> ', '</a>' ), | |
| 384 | - 'code' => 'site_exists', | |
| 385 | - ], | |
| 386 | - 200 | |
| 387 | - ); | |
| 388 | - } | |
| 389 | - | |
| 390 | - $user_data = $user['res']; | |
| 391 | - | |
| 392 | - $settings = new Optml_Settings(); | |
| 393 | - $settings->update( 'api_key', $user_data['api_key'] ); | |
| 394 | - | |
| 395 | - if ( $user_data['app_count'] === 1 ) { | |
| 396 | - $settings->update( 'service_data', $user_data ); | |
| 397 | - } | |
| 398 | - | |
| 399 | - if ( isset( $user_data['extra_visits'] ) ) { | |
| 400 | - $settings->update_frontend_banner_from_remote( $user_data['extra_visits'] ); | |
| 401 | - } | |
| 402 | - | |
| 403 | - return $this->response( $user_data ); | |
| 161 | + return $this->response( $user ); | |
| 404 | 162 | } |
| 405 | 163 | |
| 406 | 164 | /** |
| 407 | 165 | * Return image samples. |
| @@ -412,158 +170,42 @@ | ||
| 412 | 170 | */ |
| 413 | 171 | public function get_sample_rate( WP_REST_Request $request ) { |
| 414 | 172 | |
| 415 | 173 | add_filter( 'optml_dont_replace_url', '__return_true' ); |
| 416 | - $image_sample = get_transient( 'optimole_sample_image' ); | |
| 417 | - if ( $image_sample === false || $request->get_param( 'force' ) === 'yes' ) { | |
| 418 | - $image_sample = $this->fetch_sample_image(); | |
| 419 | - set_transient( 'optimole_sample_image', $image_sample ); | |
| 420 | - } | |
| 421 | - $image = [ 'id' => $image_sample['id'] ]; | |
| 422 | 174 | |
| 423 | - $image['original'] = $image_sample['url']; | |
| 424 | - | |
| 425 | - remove_filter( 'optml_dont_replace_url', '__return_true' ); | |
| 426 | - | |
| 427 | - $image['optimized'] = apply_filters( | |
| 428 | - 'optml_replace_image', | |
| 429 | - $image['original'], | |
| 430 | - [ | |
| 431 | - 'width' => $image_sample['width'], | |
| 432 | - 'height' => $image_sample['height'], | |
| 433 | - 'quality' => $request->get_param( 'quality' ), | |
| 434 | - ] | |
| 175 | + $accepted_mimes = array( 'image/jpeg', 'image/png' ); | |
| 176 | + $args = array( | |
| 177 | + 'post_type' => 'attachment', | |
| 178 | + 'post_status' => 'any', | |
| 179 | + 'number' => '5', | |
| 180 | + 'no_found_rows' => true, | |
| 181 | + 'fields' => 'ids', | |
| 182 | + 'post_mime_type' => $accepted_mimes, | |
| 435 | 183 | ); |
| 436 | - | |
| 437 | - $optimized = wp_remote_get( | |
| 438 | - $image['optimized'], | |
| 439 | - [ | |
| 440 | - 'timeout' => 10, | |
| 441 | - 'headers' => [ | |
| 442 | - 'Accept' => 'text/html,application/xhtml+xml,image/webp,image/apng ', | |
| 443 | - ], | |
| 444 | - ] | |
| 445 | - ); | |
| 446 | - | |
| 447 | - $original = wp_remote_get( $image['original'] ); | |
| 448 | - | |
| 449 | - $image['optimized_size'] = (int) wp_remote_retrieve_header( $optimized, 'content-length' ); | |
| 450 | - $image['original_size'] = (int) wp_remote_retrieve_header( $original, 'content-length' ); | |
| 451 | - | |
| 452 | - return $this->response( $image ); | |
| 453 | - } | |
| 454 | - | |
| 455 | - /** | |
| 456 | - * Crawl & upload initial load. | |
| 457 | - * | |
| 458 | - * @return WP_REST_Response If there are more posts left to receive. | |
| 459 | - */ | |
| 460 | - public function upload_onboard_images( WP_REST_Request $request ) { | |
| 461 | - $offset = absint( $request->get_param( 'offset' ) ); | |
| 462 | - | |
| 463 | - // Arguments for get_posts function | |
| 464 | - $args = [ | |
| 465 | - 'post_type' => 'attachment', | |
| 466 | - 'post_mime_type' => 'image', | |
| 467 | - 'post_status' => 'inherit', | |
| 468 | - 'posts_per_page' => 100, | |
| 469 | - 'offset' => $offset, | |
| 470 | - 'fields' => 'ids', | |
| 471 | - 'no_found_rows' => true, | |
| 472 | - 'update_post_meta_cache' => false, | |
| 473 | - 'update_post_term_cache' => false, | |
| 474 | - 'orderby' => [ | |
| 475 | - 'parent' => 'DESC', | |
| 476 | - ], | |
| 477 | - ]; | |
| 478 | - | |
| 479 | - // Initialize an array to store the image URLs | |
| 480 | - $image_urls = []; | |
| 481 | - | |
| 482 | - add_filter( 'optml_dont_replace_url', '__return_true' ); | |
| 483 | - | |
| 484 | - // If site has a logo, get the logo url if offset is 0, ie first request | |
| 485 | - if ( $offset === 0 ) { | |
| 486 | - $custom_logo_id = get_theme_mod( 'custom_logo' ); | |
| 487 | - | |
| 488 | - if ( $custom_logo_id ) { | |
| 489 | - $image_urls[] = wp_get_attachment_url( $custom_logo_id ); | |
| 490 | - } | |
| 491 | - } | |
| 492 | - | |
| 493 | - $query = new \WP_Query( $args ); | |
| 494 | - | |
| 495 | - if ( $query->have_posts() ) { | |
| 496 | - $image_ids = $query->get_posts(); | |
| 497 | - $image_urls = array_map( 'wp_get_attachment_url', $image_ids ); | |
| 498 | - } | |
| 499 | - remove_filter( 'optml_dont_replace_url', '__return_true' ); | |
| 500 | - | |
| 501 | - if ( count( $image_urls ) === 0 ) { | |
| 502 | - return $this->response( true ); | |
| 503 | - } | |
| 504 | - | |
| 505 | - $api = new Optml_Api(); | |
| 506 | - $api->call_onboard_api( $image_urls ); | |
| 507 | - | |
| 508 | - // Check if image_url array has at least 100 items, if not, we have reached the end of the images | |
| 509 | - return $this->response( count( $image_urls ) < 100 ? true : false ); | |
| 510 | - } | |
| 511 | - | |
| 512 | - /** | |
| 513 | - * Return sample image data. | |
| 514 | - * | |
| 515 | - * @return array Image data. | |
| 516 | - */ | |
| 517 | - private function fetch_sample_image() { | |
| 518 | - $accepted_mimes = [ 'image/jpeg' ]; | |
| 519 | - $args = [ | |
| 520 | - 'post_type' => 'attachment', | |
| 521 | - 'post_status' => 'any', | |
| 522 | - 'number' => '5', | |
| 523 | - 'no_found_rows' => true, | |
| 524 | - 'fields' => 'ids', | |
| 525 | - 'post_mime_type' => $accepted_mimes, | |
| 526 | - 'post_parent__not_in' => [ 0 ], | |
| 527 | - ]; | |
| 528 | 184 | $image_result = new WP_Query( $args ); |
| 529 | 185 | if ( empty( $image_result->posts ) ) { |
| 530 | - $rand_id = rand( 1, 3 ); | |
| 531 | - $original_image_url = OPTML_URL . 'assets/img/' . $rand_id . '.jpg'; | |
| 532 | - | |
| 533 | - return [ | |
| 534 | - 'url' => $original_image_url, | |
| 535 | - 'width' => '700', | |
| 536 | - 'height' => '465', | |
| 537 | - 'id' => - 1, | |
| 538 | - ]; | |
| 186 | + return $this->response( array() ); | |
| 539 | 187 | } |
| 540 | - $attachment_id = $image_result->posts[ array_rand( $image_result->posts, 1 ) ]; | |
| 541 | 188 | |
| 542 | - $original_image_url = wp_get_attachment_image_url( $attachment_id, 'full' ); | |
| 189 | + $image = array( | |
| 190 | + 'id' => $image_result->posts [ array_rand( $image_result->posts, 1 ) ], | |
| 191 | + ); | |
| 192 | + $image['original'] = wp_get_attachment_image_url( $image['id'], 'full' ); | |
| 543 | 193 | |
| 544 | - $metadata = wp_get_attachment_metadata( $attachment_id ); | |
| 194 | + remove_filter( 'optml_dont_replace_url', '__return_true' ); | |
| 545 | 195 | |
| 546 | - $width = 'auto'; | |
| 547 | - $height = 'auto'; | |
| 548 | - $size = 'full'; | |
| 549 | - if ( isset( $metadata['sizes'] ) && isset( $metadata['sizes'][ $size ] ) ) { | |
| 550 | - $width = $metadata['sizes'][ $size ]['width']; | |
| 551 | - $height = $metadata['sizes'][ $size ]['height']; | |
| 552 | - } | |
| 196 | + $image['optimized'] = apply_filters( 'optml_replace_image', $image['original'], array( | |
| 197 | + 'width' => 'auto', | |
| 198 | + 'height' => 'auto', | |
| 199 | + 'quality' => $request->get_param( 'quality' ) | |
| 200 | + ) ); | |
| 553 | 201 | |
| 554 | - return [ | |
| 555 | - 'url' => $original_image_url, | |
| 556 | - 'id' => $attachment_id, | |
| 557 | - 'width' => $width, | |
| 558 | - 'height' => $height, | |
| 559 | - ]; | |
| 202 | + return $this->response( $image ); | |
| 560 | 203 | } |
| 561 | 204 | |
| 562 | 205 | /** |
| 563 | 206 | * Disconnect from optimole service. |
| 564 | 207 | * |
| 565 | - * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
| 566 | 208 | * @param WP_REST_Request $request disconnect rest request. |
| 567 | 209 | */ |
| 568 | 210 | public function disconnect( WP_REST_Request $request ) { |
| 569 | 211 | $settings = new Optml_Settings(); |
| @@ -581,290 +223,58 @@ | ||
| 581 | 223 | public function poll_optimized_images( WP_REST_Request $request ) { |
| 582 | 224 | $api_key = $request->get_param( 'api_key' ); |
| 583 | 225 | $request = new Optml_Api(); |
| 584 | 226 | $images = $request->get_optimized_images( $api_key ); |
| 585 | - if ( ! isset( $images['list'] ) || empty( $images['list'] ) ) { | |
| 586 | - return $this->response( [] ); | |
| 227 | + if ( ! isset ( $images['list'] ) || empty( $images['list'] ) ) { | |
| 228 | + return $this->response( array() ); | |
| 587 | 229 | } |
| 588 | 230 | |
| 589 | 231 | $final_images = array_splice( $images['list'], 0, 10 ); |
| 590 | 232 | |
| 591 | - foreach ( $final_images as $index => $value ) { | |
| 592 | - $final_images[ $index ]['url'] = Optml_Media_Offload::instance()->get_media_optimized_url( | |
| 593 | - $value['url'], | |
| 594 | - $value['key'], | |
| 595 | - 140, | |
| 596 | - 140, | |
| 597 | - [ | |
| 598 | - 'type' => Optml_Resize::RESIZE_FILL, | |
| 599 | - 'enlarge' => false, | |
| 600 | - 'gravity' => Optml_Resize::GRAVITY_CENTER, | |
| 601 | - ] | |
| 602 | - ); | |
| 603 | - unset( $final_images[ $index ]['key'] ); | |
| 604 | - } | |
| 605 | - | |
| 606 | 233 | return $this->response( $final_images ); |
| 607 | 234 | } |
| 608 | 235 | |
| 609 | 236 | /** |
| 610 | - * Get watermarks from API. | |
| 611 | - * | |
| 612 | - * @param WP_REST_Request $request rest request. | |
| 613 | - * | |
| 614 | - * @return WP_REST_Response | |
| 615 | - */ | |
| 616 | - public function poll_watermarks( WP_REST_Request $request ) { | |
| 617 | - $api_key = $request->get_param( 'api_key' ); | |
| 618 | - $request = new Optml_Api(); | |
| 619 | - $watermarks = $request->get_watermarks( $api_key ); | |
| 620 | - if ( ! isset( $watermarks['watermarks'] ) || empty( $watermarks['watermarks'] ) ) { | |
| 621 | - return $this->response( [] ); | |
| 622 | - } | |
| 623 | - $final_images = array_splice( $watermarks['watermarks'], 0, 10 ); | |
| 624 | - | |
| 625 | - return $this->response( $final_images ); | |
| 626 | - } | |
| 627 | - | |
| 628 | - /** | |
| 629 | - * Add watermark. | |
| 630 | - * | |
| 631 | - * @param WP_REST_Request $request rest request. | |
| 632 | - * | |
| 633 | - * @return WP_REST_Response | |
| 634 | - */ | |
| 635 | - public function add_watermark( WP_REST_Request $request ) { | |
| 636 | - $file = $request->get_file_params(); | |
| 637 | - $request = new Optml_Api(); | |
| 638 | - $response = $request->add_watermark( $file ); | |
| 639 | - if ( $response === false ) { | |
| 640 | - return $this->response( __( 'Error uploading image. Please try again.', 'optimole-wp' ), 'error' ); | |
| 641 | - } | |
| 642 | - | |
| 643 | - return $this->response( __( 'Watermark image uploaded succesfully ! ', 'optimole-wp' ) ); | |
| 644 | - } | |
| 645 | - | |
| 646 | - /** | |
| 647 | - * Remove watermark. | |
| 648 | - * | |
| 649 | - * @param WP_REST_Request $request rest request. | |
| 650 | - * | |
| 651 | - * @return WP_REST_Response | |
| 652 | - */ | |
| 653 | - public function remove_watermark( WP_REST_Request $request ) { | |
| 654 | - $post_id = $request->get_param( 'postID' ); | |
| 655 | - $api_key = $request->get_param( 'api_key' ); | |
| 656 | - $request = new Optml_Api(); | |
| 657 | - | |
| 658 | - return $this->response( $request->remove_watermark( $post_id, $api_key ) ); | |
| 659 | - } | |
| 660 | - | |
| 661 | - /** | |
| 662 | - * Get conflicts from API. | |
| 663 | - * | |
| 664 | - * @param WP_REST_Request $request rest request. | |
| 665 | - * | |
| 666 | - * @return WP_REST_Response | |
| 667 | - */ | |
| 668 | - public function poll_conflicts( WP_REST_Request $request ) { | |
| 669 | - $conflicts_to_register = apply_filters( 'optml_register_conflicts', [] ); | |
| 670 | - $manager = new Optml_Conflict_Manager( $conflicts_to_register ); | |
| 671 | - | |
| 672 | - return $this->response( | |
| 673 | - [ | |
| 674 | - 'count' => $manager->get_conflict_count(), | |
| 675 | - 'conflicts' => $manager->get_conflict_list(), | |
| 676 | - ] | |
| 677 | - ); | |
| 678 | - } | |
| 679 | - | |
| 680 | - /** | |
| 681 | - * Dismiss conflict. | |
| 682 | - * | |
| 683 | - * @param WP_REST_Request $request rest request. | |
| 684 | - * | |
| 685 | - * @return WP_REST_Response | |
| 686 | - */ | |
| 687 | - public function dismiss_conflict( WP_REST_Request $request ) { | |
| 688 | - $conflict_id = $request->get_param( 'conflictID' ); | |
| 689 | - $conflicts_to_register = apply_filters( 'optml_register_conflicts', [] ); | |
| 690 | - $manager = new Optml_Conflict_Manager( $conflicts_to_register ); | |
| 691 | - $manager->dismiss_conflict( $conflict_id ); | |
| 692 | - | |
| 693 | - return $this->response( | |
| 694 | - [ | |
| 695 | - 'count' => $manager->get_conflict_count(), | |
| 696 | - 'conflicts' => $manager->get_conflict_list(), | |
| 697 | - ] | |
| 698 | - ); | |
| 699 | - } | |
| 700 | - | |
| 701 | - /** | |
| 702 | - * Request stats update for app. | |
| 703 | - * | |
| 704 | - * @param WP_REST_Request $request rest request. | |
| 705 | - * | |
| 706 | - * @return WP_REST_Response | |
| 707 | - */ | |
| 708 | - public function request_update( WP_REST_Request $request ) { | |
| 709 | - do_action( 'optml_daily_sync' ); | |
| 710 | - $settings = new Optml_Settings(); | |
| 711 | - $service_data = $settings->get( 'service_data' ); | |
| 712 | - if ( empty( $service_data ) ) { | |
| 713 | - return $this->response( '', 'disconnected' ); | |
| 714 | - } | |
| 715 | - return $this->response( $service_data ); | |
| 716 | - } | |
| 717 | - | |
| 718 | - /** | |
| 719 | 237 | * Update options method. |
| 720 | 238 | * |
| 721 | 239 | * @param WP_REST_Request $request option update rest request. |
| 722 | - * | |
| 723 | - * @return WP_REST_Response | |
| 724 | 240 | */ |
| 725 | 241 | public function update_option( WP_REST_Request $request ) { |
| 726 | - $new_settings = $request->get_param( 'settings' ); | |
| 727 | - | |
| 728 | - if ( empty( $new_settings ) ) { | |
| 242 | + $option_key = $request->get_param( 'option_key' ); | |
| 243 | + $option_type = $request->get_param( 'type' ); | |
| 244 | + $option_value = $request->get_param( 'option_value' ); | |
| 245 | + if ( empty( $option_key ) ) { | |
| 729 | 246 | wp_send_json_error( 'No option key set.' ); |
| 730 | 247 | } |
| 731 | - | |
| 732 | - $settings = new Optml_Settings(); | |
| 733 | - $sanitized = $settings->parse_settings( $new_settings ); | |
| 734 | - | |
| 735 | - return $this->response( $sanitized ); | |
| 736 | - } | |
| 737 | - | |
| 738 | - /** | |
| 739 | - * Update options method. | |
| 740 | - * | |
| 741 | - * @param WP_REST_Request $request option update rest request. | |
| 742 | - * | |
| 743 | - * @return WP_REST_Response | |
| 744 | - */ | |
| 745 | - public function check_redirects( WP_REST_Request $request ) { | |
| 746 | - if ( empty( $request->get_param( 'images' ) ) ) { | |
| 747 | - return $this->response( __( 'No images available on the current page.' ), 'noImagesFound' ); | |
| 248 | + if ( empty( $option_type ) ) { | |
| 249 | + wp_send_json_error( 'No option type set.' ); | |
| 748 | 250 | } |
| 749 | - // 'ok' if no issues found, 'log' is there are issues we need to notify, 'deactivated' if the user's account is disabled | |
| 750 | - $status = 'ok'; | |
| 751 | - $result = ''; | |
| 752 | - foreach ( $request->get_param( 'images' ) as $domain => $value ) { | |
| 753 | - $args = [ | |
| 754 | - 'method' => 'GET', | |
| 755 | - 'redirection' => 0, | |
| 756 | - ]; | |
| 757 | - $processed_images = 0; | |
| 758 | - if ( isset( $value['src'] ) ) { | |
| 759 | - $processed_images = count( $value['src'] ); | |
| 760 | - } | |
| 761 | - if ( isset( $value['ignoredUrls'] ) && $value['ignoredUrls'] > $processed_images ) { | |
| 762 | - $result .= '<li>❌ ' . sprintf( __( 'The images from: %1$s are not optimized by Optimole. If you would like to do so, you can follow this: %2$sWhy Optimole does not optimize all the images from my site?%3$s.', 'optimole-wp' ), $domain, '<a target="_blank" href="https://docs.optimole.com/article/1290-how-to-optimize-images-using-optimole-from-my-domain">', '</a>' ) . '</li>'; | |
| 763 | - $status = 'log'; | |
| 764 | - continue; | |
| 765 | - } | |
| 766 | 251 | |
| 767 | - if ( $processed_images > 0 ) { | |
| 768 | - $response = wp_remote_get( $value['src'][ rand( 0, $processed_images - 1 ) ], $args ); | |
| 769 | - if ( ! is_wp_error( $response ) && is_array( $response ) ) { | |
| 770 | - $headers = $response['headers']; // array of http header lines | |
| 771 | - $status_code = $response['response']['code']; | |
| 772 | - if ( $status_code === 301 ) { | |
| 773 | - $status = 'deactivated'; | |
| 774 | - $result = '<li>❌ ' . sprintf( __( 'Your account is currently disabled due to exceeding quota and Optimole is no longer able to optimize the images. In order to fix this you will need to %1$supgrade%2$s.', 'optimole-wp' ), '<a target="_blank" href="https://optimole.com/pricing">', '</a>' ) . '</li>'; | |
| 775 | - break; | |
| 776 | - } | |
| 777 | - if ( $status_code === 302 ) { | |
| 778 | - if ( isset( $headers['x-redirect-o'] ) ) { | |
| 779 | - $optimole_code = (int) $headers['x-redirect-o']; | |
| 780 | - if ( $optimole_code === 1 ) { | |
| 781 | - $status = 'log'; | |
| 782 | - $result .= '<li>❌ ' . sprintf( __( 'The domain: %1$s is not allowed to optimize images using your Optimole account. You can add this to the allowed list %2$shere%3$s.', 'optimole-wp' ), '<b>' . $domain . '</b>', '<a target="_blank" href="https://dashboard.optimole.com/whitelist">', '</a>' ) . '</li>'; | |
| 783 | - } | |
| 784 | - if ( $optimole_code === 4 ) { | |
| 785 | - $status = 'log'; | |
| 786 | - $result .= '<li>❌ ' . sprintf( __( 'We are not able to download the images from %1$s. Please check %2$sthis%3$s document for a more advanced guide on how to solve this. ', 'optimole-wp' ), '<b>' . $domain . '</b>', '<a target="_blank" href="https://docs.optimole.com/article/1291-why-optimole-is-not-able-to-download-the-images-from-my-site">', '</a>' ) . '<br />' . '</li>'; | |
| 787 | - } | |
| 788 | - } | |
| 789 | - } | |
| 790 | - } | |
| 791 | - } | |
| 792 | - } | |
| 793 | - if ( $result === '' ) { | |
| 794 | - $result = __( 'No issues detected, everything is running smoothly.', 'optimole-wp' ); | |
| 795 | - } | |
| 252 | + $accepted_types = array( 'toggle', 'enum' ); | |
| 796 | 253 | |
| 797 | - return $this->response( '<ul>' . $result . '</ul>', $status ); | |
| 798 | - } | |
| 799 | - | |
| 800 | - /** | |
| 801 | - * Get total number of images. | |
| 802 | - * | |
| 803 | - * @param WP_REST_Request $request rest request object. | |
| 804 | - * | |
| 805 | - * @return WP_REST_Response | |
| 806 | - */ | |
| 807 | - public function number_of_images_and_pages( WP_REST_Request $request ) { | |
| 808 | - $action = 'offload_images'; | |
| 809 | - $refresh = false; | |
| 810 | - $images = []; | |
| 811 | - | |
| 812 | - if ( ! empty( $request->get_param( 'action' ) ) ) { | |
| 813 | - $action = $request->get_param( 'action' ); | |
| 254 | + if ( ! in_array( $option_type, $accepted_types ) ) { | |
| 255 | + wp_send_json_error( 'Invalid option type.' ); | |
| 814 | 256 | } |
| 815 | 257 | |
| 816 | - if ( ! empty( $request->get_param( 'refresh' ) ) ) { | |
| 817 | - $refresh = $request->get_param( 'refresh' ); | |
| 818 | - } | |
| 258 | + $settings = new Optml_Settings(); | |
| 819 | 259 | |
| 820 | - if ( ! empty( $request->get_param( 'images' ) ) ) { | |
| 821 | - $images = $request->get_param( 'images' ); | |
| 822 | - } | |
| 823 | - | |
| 824 | - return $this->response( Optml_Media_Offload::get_image_count( $action, $refresh, $images ) ); | |
| 825 | - } | |
| 826 | - | |
| 827 | - /** | |
| 828 | - * Get conflicts list. | |
| 829 | - * | |
| 830 | - * @param WP_REST_Request $request rest request object. | |
| 831 | - * | |
| 832 | - * @return WP_REST_Response | |
| 833 | - */ | |
| 834 | - public function get_offload_conflicts( WP_REST_Request $request ) { | |
| 835 | - $request = new Optml_Api(); | |
| 836 | - $decoded_list = $request->get_offload_conflicts(); | |
| 837 | - $active_conflicts = []; | |
| 838 | - if ( isset( $decoded_list['plugins'] ) ) { | |
| 839 | - foreach ( $decoded_list['plugins'] as $slug ) { | |
| 840 | - if ( is_plugin_active( $slug ) ) { | |
| 841 | - $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $slug ); | |
| 842 | - if ( ! empty( $plugin_data['Name'] ) ) { | |
| 843 | - $active_conflicts[] = $plugin_data['Name']; | |
| 844 | - } | |
| 260 | + switch ( $option_type ) { | |
| 261 | + case 'toggle': | |
| 262 | + $status = $settings->get( $option_key ); | |
| 263 | + if ( $status === 'enabled' ) { | |
| 264 | + $settings->update( $option_key, 'disabled' ); | |
| 265 | + wp_send_json_success( $option_key . ' disabled.' ); | |
| 266 | + } else { | |
| 267 | + $settings->update( $option_key, 'enabled' ); | |
| 268 | + wp_send_json_success( $option_key . ' enabled.' ); | |
| 845 | 269 | } |
| 846 | - } | |
| 270 | + break; | |
| 271 | + case 'enum': | |
| 272 | + $settings->update( $option_key, $option_value ); | |
| 273 | + wp_send_json_success( $option_key . ' saved to ' . $option_value ); | |
| 274 | + break; | |
| 275 | + default: | |
| 276 | + break; | |
| 847 | 277 | } |
| 848 | - | |
| 849 | - return $this->response( $active_conflicts ); | |
| 850 | 278 | } |
| 851 | 279 | |
| 852 | - /** | |
| 853 | - * Insert images request. | |
| 854 | - * | |
| 855 | - * @param WP_REST_Request $request insert images rest request. | |
| 856 | - * | |
| 857 | - * @return WP_REST_Response | |
| 858 | - */ | |
| 859 | - public function insert_images( WP_REST_Request $request ) { | |
| 860 | - $images = $request->get_param( 'images' ); | |
| 861 | - | |
| 862 | - if ( ! is_array( $images ) || empty( $images ) ) { | |
| 863 | - return $this->response( [ 'error' => 'No images found' ] ); | |
| 864 | - } | |
| 865 | - | |
| 866 | - $insert = Optml_Main::instance()->dam->insert_attachments( $images ); | |
| 867 | - | |
| 868 | - return $this->response( $insert ); | |
| 869 | - } | |
| 870 | 280 | } |