| @@ -3,100 +3,21 @@ | ||
| 3 | 3 | /** |
| 4 | 4 | * Class Optml_Settings. |
| 5 | 5 | */ |
| 6 | 6 | class Optml_Settings { |
| 7 | - use Optml_Normalizer; | |
| 8 | 7 | |
| 9 | - const FILTER_EXT = 'extension'; | |
| 10 | - const FILTER_URL = 'page_url'; | |
| 11 | - const FILTER_URL_MATCH = 'page_url_match'; | |
| 12 | - const FILTER_FILENAME = 'filename'; | |
| 13 | - const FILTER_CLASS = 'class'; | |
| 14 | - const FILTER_TYPE_LAZYLOAD = 'lazyload'; | |
| 15 | - const FILTER_TYPE_OPTIMIZE = 'optimize'; | |
| 16 | - const OPTML_USER_EMAIL = 'optml_user_email'; | |
| 17 | 8 | /** |
| 18 | - * Holds an array of possible settings to alter via wp cli or wp-config constants. | |
| 19 | - * | |
| 20 | - * @var array Whitelisted settings. | |
| 21 | - */ | |
| 22 | - public static $whitelisted_settings = [ | |
| 23 | - 'image_replacer' => 'bool', | |
| 24 | - 'quality' => 'int', | |
| 25 | - 'lazyload' => 'bool', | |
| 26 | - 'lazyload_placeholder' => 'bool', | |
| 27 | - 'network_optimization' => 'bool', | |
| 28 | - 'autoquality' => 'bool', | |
| 29 | - 'img_to_video' => 'bool', | |
| 30 | - 'resize_smart' => 'bool', | |
| 31 | - 'retina_images' => 'bool', | |
| 32 | - 'native_lazyload' => 'bool', | |
| 33 | - 'video_lazyload' => 'bool', | |
| 34 | - 'bg_replacer' => 'bool', | |
| 35 | - 'scale' => 'bool', | |
| 36 | - 'cdn' => 'bool', | |
| 37 | - ]; | |
| 38 | - /** | |
| 39 | - * Holds the status of the auto connect hook. | |
| 40 | - * | |
| 41 | - * @var boolean Whether or not the auto connect action is hooked. | |
| 42 | - */ | |
| 43 | - private static $auto_connect_hooked = false; | |
| 44 | - | |
| 45 | - /** | |
| 46 | 9 | * Default settings schema. |
| 47 | 10 | * |
| 48 | 11 | * @var array Settings schema. |
| 49 | 12 | */ |
| 50 | - private $default_schema = [ | |
| 51 | - 'api_key' => '', | |
| 52 | - 'service_data' => '', | |
| 53 | - 'cache_buster' => '', | |
| 54 | - 'cache_buster_assets' => '', | |
| 55 | - 'cache_buster_images' => '', | |
| 56 | - 'cdn' => 'disabled', | |
| 57 | - 'admin_bar_item' => 'enabled', | |
| 58 | - 'lazyload' => 'disabled', | |
| 59 | - 'scale' => 'disabled', | |
| 60 | - 'network_optimization' => 'disabled', | |
| 61 | - 'lazyload_placeholder' => 'enabled', | |
| 62 | - 'bg_replacer' => 'enabled', | |
| 63 | - 'video_lazyload' => 'enabled', | |
| 64 | - 'retina_images' => 'disabled', | |
| 65 | - 'limit_dimensions' => 'enabled', | |
| 66 | - 'limit_height' => 1080, | |
| 67 | - 'limit_width' => 1920, | |
| 68 | - 'resize_smart' => 'disabled', | |
| 69 | - 'no_script' => 'disabled', | |
| 70 | - 'filters' => [], | |
| 71 | - 'cloud_sites' => [ 'all' => 'true' ], | |
| 72 | - 'watchers' => '', | |
| 73 | - 'quality' => 'auto', | |
| 74 | - 'wm_id' => - 1, | |
| 75 | - 'wm_opacity' => 1, | |
| 76 | - 'wm_position' => Optml_Resize::GRAVITY_SOUTH_EAST, | |
| 77 | - 'wm_x' => 0, | |
| 78 | - 'wm_y' => 0, | |
| 79 | - 'wm_scale' => 0, | |
| 80 | - 'image_replacer' => 'enabled', | |
| 81 | - 'img_to_video' => 'disabled', | |
| 82 | - 'css_minify' => 'enabled', | |
| 83 | - 'js_minify' => 'disabled', | |
| 84 | - 'report_script' => 'disabled', | |
| 85 | - 'avif' => 'enabled', | |
| 86 | - 'autoquality' => 'enabled', | |
| 87 | - 'native_lazyload' => 'disabled', | |
| 88 | - 'offload_media' => 'disabled', | |
| 89 | - 'cloud_images' => 'disabled', | |
| 90 | - 'strip_metadata' => 'enabled', | |
| 91 | - 'skip_lazyload_images' => 3, | |
| 92 | - 'defined_image_sizes' => [ ], | |
| 93 | - 'banner_frontend' => 'disabled', | |
| 94 | - 'offloading_status' => 'disabled', | |
| 95 | - 'rollback_status' => 'disabled', | |
| 96 | - 'best_format' => 'enabled', | |
| 97 | - 'placeholder_color' => '', | |
| 98 | - ]; | |
| 13 | + private $default_schema = array( | |
| 14 | + 'api_key' => '', | |
| 15 | + 'service_data' => '', | |
| 16 | + 'admin_bar_item' => 'enabled', | |
| 17 | + 'quality' => 'auto', | |
| 18 | + 'image_replacer' => 'enabled', | |
| 19 | + ); | |
| 99 | 20 | /** |
| 100 | 21 | * Option key. |
| 101 | 22 | * |
| 102 | 23 | * @var string Option name. |
| @@ -112,58 +33,15 @@ | ||
| 112 | 33 | /** |
| 113 | 34 | * Optml_Settings constructor. |
| 114 | 35 | */ |
| 115 | 36 | public function __construct() { |
| 116 | - | |
| 117 | - $this->namespace = OPTML_NAMESPACE . '_settings'; | |
| 118 | - $this->default_schema = apply_filters( 'optml_default_settings', $this->default_schema ); | |
| 119 | - $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema ); | |
| 120 | - | |
| 121 | - if ( defined( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && $this->to_boolean( constant( 'OPTIML_ENABLED_MU' ) ) && constant( 'OPTIML_MU_SITE_ID' ) ) { | |
| 122 | - switch_to_blog( constant( 'OPTIML_MU_SITE_ID' ) ); | |
| 37 | + $this->namespace = OPTML_NAMESPACE . '_settings'; | |
| 38 | + $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema ); | |
| 39 | + if ( defined( 'OPTIML_ENABLED_MU' ) && OPTIML_ENABLED_MU && defined( 'OPTIML_MU_SITE_ID' ) && ! empty( OPTIML_MU_SITE_ID ) ) { | |
| 40 | + switch_to_blog( OPTIML_MU_SITE_ID ); | |
| 123 | 41 | $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema ); |
| 124 | 42 | restore_current_blog(); |
| 125 | 43 | } |
| 126 | - | |
| 127 | - if ( defined( 'OPTIML_USE_ENV' ) && constant( 'OPTIML_USE_ENV' ) && $this->to_boolean( constant( 'OPTIML_USE_ENV' ) ) ) { | |
| 128 | - | |
| 129 | - if ( defined( 'OPTIML_API_KEY' ) | |
| 130 | - && constant( 'OPTIML_API_KEY' ) !== '' | |
| 131 | - ) { | |
| 132 | - if ( ! $this->is_connected() && ! self::$auto_connect_hooked ) { | |
| 133 | - self::$auto_connect_hooked = true; | |
| 134 | - add_action( | |
| 135 | - 'plugins_loaded', | |
| 136 | - [ $this, 'auto_connect' ] | |
| 137 | - ); | |
| 138 | - } | |
| 139 | - } | |
| 140 | - | |
| 141 | - foreach ( self::$whitelisted_settings as $key => $type ) { | |
| 142 | - $env_key = 'OPTIML_' . strtoupper( $key ); | |
| 143 | - if ( defined( $env_key ) && constant( $env_key ) ) { | |
| 144 | - $value = constant( $env_key ); | |
| 145 | - if ( $type === 'bool' && ( (string) $value === '' || ! in_array( | |
| 146 | - $value, | |
| 147 | - [ | |
| 148 | - 'on', | |
| 149 | - 'off', | |
| 150 | - ], | |
| 151 | - true | |
| 152 | - ) ) ) { | |
| 153 | - continue; | |
| 154 | - } | |
| 155 | - | |
| 156 | - if ( $type === 'int' && ( (string) $value === '' || (int) $value > 100 || (int) $value < 0 ) ) { | |
| 157 | - continue; | |
| 158 | - } | |
| 159 | - $sanitized_value = ( $type === 'bool' ) ? ( $value === 'on' ? 'enabled' : 'disabled' ) : (int) $value; | |
| 160 | - $this->options[ $key ] = $sanitized_value; | |
| 161 | - } | |
| 162 | - } | |
| 163 | - } | |
| 164 | - | |
| 165 | - add_action( 'init', [ $this, 'register_settings' ] ); | |
| 166 | 44 | } |
| 167 | 45 | |
| 168 | 46 | /** |
| 169 | 47 | * Check if the user is connected to Optimole. |
| @@ -193,15 +71,15 @@ | ||
| 193 | 71 | if ( ! $this->is_allowed( $key ) ) { |
| 194 | 72 | return null; |
| 195 | 73 | } |
| 196 | 74 | |
| 197 | - return isset( $this->options[ $key ] ) ? $this->options[ $key ] : ''; | |
| 75 | + return isset ( $this->options[ $key ] ) ? $this->options[ $key ] : ''; | |
| 198 | 76 | } |
| 199 | 77 | |
| 200 | 78 | /** |
| 201 | 79 | * Check if key is allowed. |
| 202 | 80 | * |
| 203 | - * @param string $key Is key allowed or not. | |
| 81 | + * @param string $key Is key allowed or not. | |
| 204 | 82 | * |
| 205 | 83 | * @return bool Is key allowed or not. |
| 206 | 84 | */ |
| 207 | 85 | private function is_allowed( $key ) { |
| @@ -208,436 +86,25 @@ | ||
| 208 | 86 | return isset( $this->default_schema[ $key ] ); |
| 209 | 87 | } |
| 210 | 88 | |
| 211 | 89 | /** |
| 212 | - * Auto connect action. | |
| 213 | - */ | |
| 214 | - public function auto_connect() { | |
| 215 | - $request = new WP_REST_Request( 'POST' ); | |
| 216 | - $request->set_param( 'api_key', constant( 'OPTIML_API_KEY' ) ); | |
| 217 | - Optml_Main::instance()->rest->connect( $request ); | |
| 218 | - | |
| 219 | - remove_action( 'plugins_loaded', [ $this, 'auto_connect' ] ); | |
| 220 | - self::$auto_connect_hooked = false; | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * Process settings. | |
| 225 | - * | |
| 226 | - * @param array $new_settings List of settings. | |
| 227 | - * | |
| 228 | - * @return array | |
| 229 | - */ | |
| 230 | - public function parse_settings( $new_settings ) { | |
| 231 | - $sanitized = []; | |
| 232 | - foreach ( $new_settings as $key => $value ) { | |
| 233 | - switch ( $key ) { | |
| 234 | - case 'admin_bar_item': | |
| 235 | - case 'lazyload': | |
| 236 | - case 'scale': | |
| 237 | - case 'image_replacer': | |
| 238 | - case 'cdn': | |
| 239 | - case 'network_optimization': | |
| 240 | - case 'lazyload_placeholder': | |
| 241 | - case 'retina_images': | |
| 242 | - case 'limit_dimensions': | |
| 243 | - case 'resize_smart': | |
| 244 | - case 'bg_replacer': | |
| 245 | - case 'video_lazyload': | |
| 246 | - case 'report_script': | |
| 247 | - case 'avif': | |
| 248 | - case 'offload_media': | |
| 249 | - case 'cloud_images': | |
| 250 | - case 'autoquality': | |
| 251 | - case 'img_to_video': | |
| 252 | - case 'css_minify': | |
| 253 | - case 'js_minify': | |
| 254 | - case 'native_lazyload': | |
| 255 | - case 'strip_metadata': | |
| 256 | - case 'no_script': | |
| 257 | - case 'banner_frontend': | |
| 258 | - case 'offloading_status': | |
| 259 | - case 'rollback_status': | |
| 260 | - case 'best_format': | |
| 261 | - $sanitized_value = $this->to_map_values( $value, [ 'enabled', 'disabled' ], 'enabled' ); | |
| 262 | - break; | |
| 263 | - case 'limit_height': | |
| 264 | - case 'limit_width': | |
| 265 | - $sanitized_value = $this->to_bound_integer( $value, 100, 5000 ); | |
| 266 | - break; | |
| 267 | - case 'quality': | |
| 268 | - $sanitized_value = $this->to_bound_integer( $value, 1, 100 ); | |
| 269 | - break; | |
| 270 | - case 'wm_id': | |
| 271 | - $sanitized_value = intval( $value ); | |
| 272 | - break; | |
| 273 | - case 'cache_buster_assets': | |
| 274 | - case 'cache_buster_images': | |
| 275 | - case 'cache_buster': | |
| 276 | - $sanitized_value = is_string( $value ) ? sanitize_text_field( $value ) : ''; | |
| 277 | - break; | |
| 278 | - case 'cloud_sites': | |
| 279 | - $current_sites = $this->get( 'cloud_sites' ); | |
| 280 | - $sanitized_value = array_replace_recursive( $current_sites, $value ); | |
| 281 | - if ( isset( $value['all'] ) && $value['all'] === 'true' ) { | |
| 282 | - $sanitized_value = [ 'all' => 'true' ]; | |
| 283 | - } | |
| 284 | - break; | |
| 285 | - case 'defined_image_sizes': | |
| 286 | - $current_sizes = $this->get( 'defined_image_sizes' ); | |
| 287 | - foreach ( $value as $size_name => $size_value ) { | |
| 288 | - if ( $size_value === 'remove' ) { | |
| 289 | - unset( $current_sizes[ $size_name ] ); | |
| 290 | - unset( $value[ $size_name ] ); | |
| 291 | - } | |
| 292 | - } | |
| 293 | - $sanitized_value = array_replace_recursive( $current_sizes, $value ); | |
| 294 | - break; | |
| 295 | - case 'filters': | |
| 296 | - $current_filters = $this->get_filters(); | |
| 297 | - $sanitized_value = array_replace_recursive( $current_filters, $value ); | |
| 298 | - // Remove falsy vars. | |
| 299 | - foreach ( $sanitized_value as $filter_type => $filter_values ) { | |
| 300 | - foreach ( $filter_values as $filter_rule_type => $filter_rules_value ) { | |
| 301 | - $sanitized_value[ $filter_type ][ $filter_rule_type ] = array_filter( | |
| 302 | - $filter_rules_value, | |
| 303 | - function ( $value ) { | |
| 304 | - return ( $value !== 'false' && $value !== false ); | |
| 305 | - } | |
| 306 | - ); | |
| 307 | - } | |
| 308 | - } | |
| 309 | - break; | |
| 310 | - case 'watchers': | |
| 311 | - case 'placeholder_color': | |
| 312 | - $sanitized_value = sanitize_text_field( $value ); | |
| 313 | - break; | |
| 314 | - case 'skip_lazyload_images': | |
| 315 | - $sanitized_value = $this->to_bound_integer( $value, 0, 100 ); | |
| 316 | - break; | |
| 317 | - case 'wm_opacity': | |
| 318 | - case 'wm_scale': | |
| 319 | - case 'wm_x': | |
| 320 | - case 'wm_y': | |
| 321 | - $sanitized_value = floatval( $value ); | |
| 322 | - break; | |
| 323 | - case 'wm_position': | |
| 324 | - $sanitized_value = $this->to_map_values( | |
| 325 | - $value, | |
| 326 | - [ | |
| 327 | - Optml_Resize::GRAVITY_NORTH, | |
| 328 | - Optml_Resize::GRAVITY_NORTH_EAST, | |
| 329 | - Optml_Resize::GRAVITY_NORTH_WEST, | |
| 330 | - Optml_Resize::GRAVITY_CENTER, | |
| 331 | - Optml_Resize::GRAVITY_EAST, | |
| 332 | - Optml_Resize::GRAVITY_WEST, | |
| 333 | - Optml_Resize::GRAVITY_SOUTH_EAST, | |
| 334 | - Optml_Resize::GRAVITY_SOUTH, | |
| 335 | - Optml_Resize::GRAVITY_SOUTH_WEST, | |
| 336 | - ], | |
| 337 | - Optml_Resize::GRAVITY_SOUTH_EAST | |
| 338 | - ); | |
| 339 | - break; | |
| 340 | - default: | |
| 341 | - $sanitized_value = ''; | |
| 342 | - break; | |
| 343 | - | |
| 344 | - } | |
| 345 | - | |
| 346 | - $sanitized[ $key ] = $sanitized_value; | |
| 347 | - $this->update( $key, $sanitized_value ); | |
| 348 | - } | |
| 349 | - | |
| 350 | - return $sanitized; | |
| 351 | - } | |
| 352 | - | |
| 353 | - /** | |
| 354 | - * Return filter definitions. | |
| 355 | - * | |
| 356 | - * @return mixed|null Filter values. | |
| 357 | - */ | |
| 358 | - public function get_filters() { | |
| 359 | - | |
| 360 | - $filters = $this->get( 'filters' ); | |
| 361 | - if ( ! isset( $filters[ self::FILTER_TYPE_LAZYLOAD ] ) ) { | |
| 362 | - $filters[ self::FILTER_TYPE_LAZYLOAD ] = []; | |
| 363 | - } | |
| 364 | - if ( ! isset( $filters[ self::FILTER_TYPE_OPTIMIZE ] ) ) { | |
| 365 | - $filters[ self::FILTER_TYPE_OPTIMIZE ] = []; | |
| 366 | - } | |
| 367 | - foreach ( $filters as $filter_key => $filter_rules ) { | |
| 368 | - if ( ! isset( $filter_rules[ self::FILTER_EXT ] ) ) { | |
| 369 | - $filters[ $filter_key ][ self::FILTER_EXT ] = []; | |
| 370 | - } | |
| 371 | - if ( ! isset( $filter_rules[ self::FILTER_FILENAME ] ) ) { | |
| 372 | - $filters[ $filter_key ][ self::FILTER_FILENAME ] = []; | |
| 373 | - } | |
| 374 | - if ( ! isset( $filter_rules[ self::FILTER_URL ] ) ) { | |
| 375 | - $filters[ $filter_key ][ self::FILTER_URL ] = []; | |
| 376 | - } | |
| 377 | - if ( ! isset( $filter_rules[ self::FILTER_URL_MATCH ] ) ) { | |
| 378 | - $filters[ $filter_key ][ self::FILTER_URL_MATCH ] = []; | |
| 379 | - } | |
| 380 | - if ( ! isset( $filter_rules[ self::FILTER_CLASS ] ) ) { | |
| 381 | - $filters[ $filter_key ][ self::FILTER_CLASS ] = []; | |
| 382 | - } | |
| 383 | - } | |
| 384 | - | |
| 385 | - return $filters; | |
| 386 | - } | |
| 387 | - | |
| 388 | - /** | |
| 389 | - * Update frontend banner setting from remote. | |
| 390 | - * | |
| 391 | - * @param bool $value Value. | |
| 392 | - * | |
| 393 | - * @return bool | |
| 394 | - */ | |
| 395 | - public function update_frontend_banner_from_remote( $value ) { | |
| 396 | - if ( ! $this->is_main_mu_site() ) { | |
| 397 | - return false; | |
| 398 | - } | |
| 399 | - | |
| 400 | - $opts = $this->options; | |
| 401 | - $opts['banner_frontend'] = $value ? 'enabled' : 'disabled'; | |
| 402 | - | |
| 403 | - $update = update_option( $this->namespace, $opts, false ); | |
| 404 | - | |
| 405 | - if ( $update ) { | |
| 406 | - $this->options = $opts; | |
| 407 | - } | |
| 408 | - | |
| 409 | - return $update; | |
| 410 | - } | |
| 411 | - | |
| 412 | - /** | |
| 413 | - * Update settings. | |
| 414 | - * | |
| 415 | - * @param string $key Settings key. | |
| 416 | - * @param mixed $value Settings value. | |
| 417 | - * | |
| 418 | - * @return bool Update result. | |
| 419 | - */ | |
| 420 | - public function update( $key, $value ) { | |
| 421 | - if ( ! $this->is_allowed( $key ) ) { | |
| 422 | - return false; | |
| 423 | - } | |
| 424 | - | |
| 425 | - if ( ! $this->is_main_mu_site() ) { | |
| 426 | - return false; | |
| 427 | - } | |
| 428 | - $opt = $this->options; | |
| 429 | - | |
| 430 | - if ( $key === 'banner_frontend' ) { | |
| 431 | - $api = new Optml_Api(); | |
| 432 | - $service_data = $this->get( 'service_data' ); | |
| 433 | - $application = isset( $service_data['cdn_key'] ) ? $service_data['cdn_key'] : ''; | |
| 434 | - $response = $api->update_extra_visits( $opt['api_key'], $value, $application ); | |
| 435 | - } | |
| 436 | - | |
| 437 | - $opt[ $key ] = $value; | |
| 438 | - $update = update_option( $this->namespace, $opt, false ); | |
| 439 | - if ( $update ) { | |
| 440 | - $this->options = $opt; | |
| 441 | - } | |
| 442 | - if ( apply_filters( 'optml_dont_trigger_settings_updated', false ) === false ) { | |
| 443 | - do_action( 'optml_settings_updated' ); | |
| 444 | - } | |
| 445 | - return $update; | |
| 446 | - } | |
| 447 | - | |
| 448 | - /** | |
| 449 | - * Check that we're on the main OPTML blog. | |
| 450 | - * | |
| 451 | - * @return bool | |
| 452 | - */ | |
| 453 | - private function is_main_mu_site() { | |
| 454 | - // If we try to update from a website which is not the main OPTML blog, bail. | |
| 455 | - if ( defined( 'OPTIML_ENABLED_MU' ) && constant( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && constant( 'OPTIML_MU_SITE_ID' ) && | |
| 456 | - intval( constant( 'OPTIML_MU_SITE_ID' ) ) !== get_current_blog_id() | |
| 457 | - ) { | |
| 458 | - return false; | |
| 459 | - } | |
| 460 | - | |
| 461 | - return true; | |
| 462 | - } | |
| 463 | - | |
| 464 | - /** | |
| 465 | - * Return site settings. | |
| 466 | - * | |
| 467 | - * @return array Site settings. | |
| 468 | - */ | |
| 469 | - public function get_site_settings() { | |
| 470 | - $service_data = $this->get( 'service_data' ); | |
| 471 | - $whitelist = []; | |
| 472 | - if ( isset( $service_data['whitelist'] ) ) { | |
| 473 | - $whitelist = $service_data['whitelist']; | |
| 474 | - } | |
| 475 | - | |
| 476 | - return [ | |
| 477 | - 'quality' => $this->get_quality(), | |
| 478 | - 'admin_bar_item' => $this->get( 'admin_bar_item' ), | |
| 479 | - 'lazyload' => $this->get( 'lazyload' ), | |
| 480 | - 'network_optimization' => $this->get( 'network_optimization' ), | |
| 481 | - 'retina_images' => $this->get( 'retina_images' ), | |
| 482 | - 'limit_dimensions' => $this->get( 'limit_dimensions' ), | |
| 483 | - 'limit_height' => $this->get( 'limit_height' ), | |
| 484 | - 'limit_width' => $this->get( 'limit_width' ), | |
| 485 | - 'lazyload_placeholder' => $this->get( 'lazyload_placeholder' ), | |
| 486 | - 'skip_lazyload_images' => $this->get( 'skip_lazyload_images' ), | |
| 487 | - 'bg_replacer' => $this->get( 'bg_replacer' ), | |
| 488 | - 'video_lazyload' => $this->get( 'video_lazyload' ), | |
| 489 | - 'resize_smart' => $this->get( 'resize_smart' ), | |
| 490 | - 'no_script' => $this->get( 'no_script' ), | |
| 491 | - 'image_replacer' => $this->get( 'image_replacer' ), | |
| 492 | - 'cdn' => $this->get( 'cdn' ), | |
| 493 | - 'filters' => $this->get_filters(), | |
| 494 | - 'cloud_sites' => $this->get( 'cloud_sites' ), | |
| 495 | - 'defined_image_sizes' => $this->get( 'defined_image_sizes' ), | |
| 496 | - 'watchers' => $this->get_watchers(), | |
| 497 | - 'watermark' => $this->get_watermark(), | |
| 498 | - 'img_to_video' => $this->get( 'img_to_video' ), | |
| 499 | - 'scale' => $this->get( 'scale' ), | |
| 500 | - 'css_minify' => $this->get( 'css_minify' ), | |
| 501 | - 'js_minify' => $this->get( 'js_minify' ), | |
| 502 | - 'native_lazyload' => $this->get( 'native_lazyload' ), | |
| 503 | - 'report_script' => $this->get( 'report_script' ), | |
| 504 | - 'avif' => $this->get( 'avif' ), | |
| 505 | - 'autoquality' => $this->get( 'autoquality' ), | |
| 506 | - 'offload_media' => $this->get( 'offload_media' ), | |
| 507 | - 'cloud_images' => $this->get( 'cloud_images' ), | |
| 508 | - 'strip_metadata' => $this->get( 'strip_metadata' ), | |
| 509 | - 'whitelist_domains' => $whitelist, | |
| 510 | - 'banner_frontend' => $this->get( 'banner_frontend' ), | |
| 511 | - 'offloading_status' => $this->get( 'offloading_status' ), | |
| 512 | - 'rollback_status' => $this->get( 'rollback_status' ), | |
| 513 | - 'best_format' => $this->get( 'best_format' ), | |
| 514 | - 'placeholder_color' => $this->get( 'placeholder_color' ), | |
| 515 | - ]; | |
| 516 | - } | |
| 517 | - | |
| 518 | - /** | |
| 519 | - * Return quality factor. | |
| 520 | - * | |
| 521 | - * @return string Quality factor. | |
| 522 | - */ | |
| 523 | - public function get_quality() { | |
| 524 | - $quality = $this->get( 'quality' ); | |
| 525 | - if ( $this->get( 'autoquality' ) === 'enabled' ) { | |
| 526 | - return 'mauto'; | |
| 527 | - } | |
| 528 | - // Legacy compat. | |
| 529 | - if ( $quality === 'low' ) { | |
| 530 | - return 'high_c'; | |
| 531 | - } | |
| 532 | - if ( $quality === 'medium' ) { | |
| 533 | - return 'medium_c'; | |
| 534 | - } | |
| 535 | - | |
| 536 | - if ( $quality === 'high' ) { | |
| 537 | - return 'low_c'; | |
| 538 | - } | |
| 539 | - | |
| 540 | - return $quality; | |
| 541 | - } | |
| 542 | - | |
| 543 | - /** | |
| 544 | - * Return filter definitions. | |
| 545 | - * | |
| 546 | - * @return mixed|null Filter values. | |
| 547 | - */ | |
| 548 | - public function get_watchers() { | |
| 549 | - | |
| 550 | - return $this->get( 'watchers' ); | |
| 551 | - | |
| 552 | - } | |
| 553 | - | |
| 554 | - /** | |
| 555 | - * Return an watermark array. | |
| 556 | - * | |
| 557 | - * @return array | |
| 558 | - */ | |
| 559 | - public function get_watermark() { | |
| 560 | - return [ | |
| 561 | - 'id' => $this->get( 'wm_id' ), | |
| 562 | - 'opacity' => $this->get( 'wm_opacity' ), | |
| 563 | - 'position' => $this->get( 'wm_position' ), | |
| 564 | - 'x_offset' => $this->get( 'wm_x' ), | |
| 565 | - 'y_offset' => $this->get( 'wm_y' ), | |
| 566 | - 'scale' => $this->get( 'wm_scale' ), | |
| 567 | - ]; | |
| 568 | - } | |
| 569 | - | |
| 570 | - /** | |
| 571 | - * Check if smart cropping is enabled. | |
| 572 | - * | |
| 573 | - * @return bool Is smart cropping enabled. | |
| 574 | - */ | |
| 575 | - public function is_smart_cropping() { | |
| 576 | - return $this->get( 'resize_smart' ) === 'enabled'; | |
| 577 | - } | |
| 578 | - | |
| 579 | - /** | |
| 580 | - * Check if best format is enabled. | |
| 581 | - * | |
| 582 | - * @return bool | |
| 583 | - */ | |
| 584 | - public function is_best_format() { | |
| 585 | - return $this->get( 'best_format' ) === 'enabled'; | |
| 586 | - } | |
| 587 | - | |
| 588 | - /** | |
| 589 | - * Get numeric quality used by the service. | |
| 590 | - * | |
| 591 | - * @return int Numeric quality. | |
| 592 | - */ | |
| 593 | - public function get_numeric_quality() { | |
| 594 | - $value = $this->get_quality(); | |
| 595 | - | |
| 596 | - return (int) $this->to_accepted_quality( $value ); | |
| 597 | - } | |
| 598 | - | |
| 599 | - /** | |
| 600 | 90 | * Check if replacer is enabled. |
| 601 | 91 | * |
| 602 | 92 | * @return bool Replacer enabled |
| 603 | 93 | */ |
| 604 | 94 | public function is_enabled() { |
| 605 | - $status = $this->get( 'image_replacer' ); | |
| 606 | - $service_data = $this->get( 'service_data' ); | |
| 607 | - if ( empty( $service_data ) ) { | |
| 95 | + $status = $this->get( 'image_replacer' ); | |
| 96 | + if ( $status === 'disabled' ) { | |
| 608 | 97 | return false; |
| 609 | 98 | } |
| 610 | - if ( isset( $service_data['status'] ) && $service_data['status'] === 'inactive' ) { | |
| 99 | + if ( empty( $status ) ) { | |
| 611 | 100 | return false; |
| 612 | 101 | } |
| 613 | 102 | |
| 614 | - return $this->to_boolean( $status ); | |
| 103 | + return true; | |
| 615 | 104 | } |
| 616 | 105 | |
| 617 | 106 | /** |
| 618 | - * Check if lazyload is enabled. | |
| 619 | - * | |
| 620 | - * @return bool Lazyload enabled | |
| 621 | - */ | |
| 622 | - public function use_lazyload() { | |
| 623 | - $status = $this->get( 'lazyload' ); | |
| 624 | - | |
| 625 | - return $this->to_boolean( $status ); | |
| 626 | - } | |
| 627 | - | |
| 628 | - /** | |
| 629 | - * Check if replacer is enabled. | |
| 630 | - * | |
| 631 | - * @return bool Replacer enabled | |
| 632 | - */ | |
| 633 | - public function use_cdn() { | |
| 634 | - $status = $this->get( 'cdn' ); | |
| 635 | - | |
| 636 | - return $this->to_boolean( $status ); | |
| 637 | - } | |
| 638 | - | |
| 639 | - /** | |
| 640 | 107 | * Return cdn url. |
| 641 | 108 | * |
| 642 | 109 | * @return string CDN url. |
| 643 | 110 | */ |
| @@ -646,20 +113,12 @@ | ||
| 646 | 113 | if ( ! isset( $service_data['cdn_key'] ) ) { |
| 647 | 114 | return ''; |
| 648 | 115 | } |
| 649 | 116 | |
| 650 | - if ( isset( $service_data['is_cname_assigned'] ) && $service_data['is_cname_assigned'] === 'yes' && ! empty( $service_data['domain'] ) ) { | |
| 651 | - return strtolower( $service_data['domain'] ); | |
| 652 | - } | |
| 653 | - | |
| 654 | - if ( defined( 'OPTML_CUSTOM_DOMAIN' ) && constant( 'OPTML_CUSTOM_DOMAIN' ) ) { | |
| 655 | - return parse_url( strtolower( OPTML_CUSTOM_DOMAIN ), PHP_URL_HOST ); | |
| 656 | - } | |
| 657 | - | |
| 658 | 117 | return sprintf( |
| 659 | 118 | '%s.%s', |
| 660 | 119 | strtolower( $service_data['cdn_key'] ), |
| 661 | - Optml_Config::$base_domain | |
| 120 | + 'i.optimole.com' | |
| 662 | 121 | ); |
| 663 | 122 | } |
| 664 | 123 | |
| 665 | 124 | /** |
| @@ -667,98 +126,42 @@ | ||
| 667 | 126 | * |
| 668 | 127 | * @return bool Reset action status. |
| 669 | 128 | */ |
| 670 | 129 | public function reset() { |
| 671 | - $reset_schema = $this->default_schema; | |
| 672 | - $reset_schema['filters'] = $this->options['filters']; | |
| 673 | 130 | |
| 674 | - $update = update_option( $this->namespace, $reset_schema ); | |
| 131 | + $update = update_option( $this->namespace, $this->default_schema ); | |
| 675 | 132 | if ( $update ) { |
| 676 | - $this->options = $reset_schema; | |
| 133 | + $this->options = $this->default_schema; | |
| 677 | 134 | } |
| 678 | 135 | |
| 679 | 136 | return $update; |
| 680 | 137 | } |
| 681 | - /** | |
| 682 | - * Get raw settings value. | |
| 683 | - * | |
| 684 | - * @return array | |
| 685 | - */ | |
| 686 | - public function get_raw_settings() { | |
| 687 | - return get_option( $this->namespace, false ); | |
| 688 | - } | |
| 689 | 138 | |
| 690 | 139 | /** |
| 691 | - * Get settings for CSAT. | |
| 140 | + * Update settings. | |
| 692 | 141 | * |
| 693 | - * @return void | |
| 694 | - */ | |
| 695 | - public function register_settings() { | |
| 696 | - register_setting( | |
| 697 | - 'optml_settings', | |
| 698 | - 'optml_csat', | |
| 699 | - [ | |
| 700 | - 'type' => 'string', | |
| 701 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 702 | - 'show_in_rest' => true, | |
| 703 | - 'default' => '{}', | |
| 704 | - ] | |
| 705 | - ); | |
| 706 | - } | |
| 707 | - | |
| 708 | - /** | |
| 709 | - * Clear cache. | |
| 142 | + * @param string $key Settings key. | |
| 143 | + * @param mixed $value Settings value. | |
| 710 | 144 | * |
| 711 | - * @param string $type Cache type. | |
| 712 | - * | |
| 713 | - * @return string|WP_Error | |
| 145 | + * @return bool Update result. | |
| 714 | 146 | */ |
| 715 | - public function clear_cache( $type = '' ) { | |
| 716 | - $token = $this->get( 'cache_buster' ); | |
| 717 | - $token_images = $this->get( 'cache_buster_images' ); | |
| 718 | - | |
| 719 | - if ( ! empty( $token_images ) ) { | |
| 720 | - $token = $token_images; | |
| 147 | + public function update( $key, $value ) { | |
| 148 | + if ( ! $this->is_allowed( $key ) ) { | |
| 149 | + return false; | |
| 721 | 150 | } |
| 722 | - | |
| 723 | - if ( ! empty( $type ) && $type === 'assets' ) { | |
| 724 | - $token = $this->get( 'cache_buster_assets' ); | |
| 725 | - } | |
| 726 | - | |
| 727 | - $request = new Optml_Api(); | |
| 728 | - $data = $request->get_cache_token( $token, $type ); | |
| 729 | - | |
| 730 | - if ( $data === false || is_wp_error( $data ) || empty( $data ) || ! isset( $data['token'] ) ) { | |
| 731 | - $extra = ''; | |
| 732 | - | |
| 733 | - if ( is_wp_error( $data ) ) { | |
| 734 | - /** | |
| 735 | - * Error from api. | |
| 736 | - * | |
| 737 | - * @var WP_Error $data Error object. | |
| 738 | - */ | |
| 739 | - $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() ); | |
| 151 | + // If we try to update from a website which is not the main OPTML blog, bail. | |
| 152 | + if ( defined( 'OPTIML_ENABLED_MU' ) && OPTIML_ENABLED_MU && defined( 'OPTIML_MU_SITE_ID' ) && ! empty( OPTIML_MU_SITE_ID ) ) { | |
| 153 | + if ( intval( OPTIML_MU_SITE_ID ) !== get_current_blog_id() ) { | |
| 154 | + return $this->options; | |
| 740 | 155 | } |
| 741 | - | |
| 742 | - return new WP_Error( 'optimole_cache_buster_error', __( 'Can not get new token from Optimole service', 'optimole-wp' ) . $extra ); | |
| 743 | 156 | } |
| 744 | - | |
| 745 | - if ( ! empty( $type ) && $type === 'assets' ) { | |
| 746 | - set_transient( 'optml_cache_lock_assets', 'yes', 5 * MINUTE_IN_SECONDS ); | |
| 747 | - $this->update( 'cache_buster_assets', $data['token'] ); | |
| 748 | - } else { | |
| 749 | - set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS ); | |
| 750 | - $this->update( 'cache_buster_images', $data['token'] ); | |
| 157 | + $options = $this->options; | |
| 158 | + $options[ $key ] = $value; | |
| 159 | + $update = update_option( $this->namespace, $options ); | |
| 160 | + if ( $update ) { | |
| 161 | + $this->options = $options; | |
| 751 | 162 | } |
| 752 | 163 | |
| 753 | - return $data['token']; | |
| 164 | + return $update; | |
| 754 | 165 | } |
| 755 | 166 | |
| 756 | - /** | |
| 757 | - * Utility to check if offload is enabled. | |
| 758 | - * | |
| 759 | - * @return bool | |
| 760 | - */ | |
| 761 | - public function is_offload_enabled() { | |
| 762 | - return $this->get( 'offload_media' ) === 'enabled' || $this->get( 'rollback_status' ) !== 'disabled'; | |
| 763 | - } | |
| 764 | 167 | } |