PluginProbe ʕ •ᴥ•ʔ
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN / 4.1.0
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN v4.1.0
4.1.0 4.0.3 4.0.2 2.8.1 2.9.1 3.0.0 3.0.1 3.0.2 3.1.1 3.10.1 3.10.2 3.10.3 3.11.1 3.12.3 3.12.4 3.12.5 3.12.6 3.13.0 3.13.1 3.13.2 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.2 3.16.4 3.16.5 3.16.6 3.17.0 3.17.1 3.18.0 3.18.1 3.2.0.1 3.2.1 3.2.2.1 3.2.4 3.20.0 3.21.1 3.22.1 3.22.3 3.23.0 3.23.1 3.23.2 3.23.3 3.23.4 3.24.0 3.24.0-beta.2 3.3.0 3.3.1 3.3.2 3.4.1 3.4.2 3.6.1 3.6.3 3.7.0 3.7.1 3.7.2 3.7.3 3.8.2 3.8.3 3.8.4 3.8.5 3.8.7 3.8.8 3.9.0 3.9.1 3.9.11 3.9.2 3.9.4 3.9.5 3.9.8 3.9.9 trunk 1.0.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.2 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.5.3 1.6.5.4 1.7 1.7.1 1.7.1.1 2.0 2.0.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.6.2 2.0.6.3 2.0.6.5 2.0.7 2.0.7.1 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.2 2.3 2.3.1 2.4 2.4.2 2.4.3 2.4.4 2.4.5 2.5.2 2.5.3 2.6.1 2.6.2 2.6.3 2.7 2.7.1 2.7.4 2.7.4.1 2.7.5 2.7.6 2.7.8 2.7.8.1 2.7.9.1 2.8.0 2.8.0.1
wp-smushit / core / class-installer.php
wp-smushit / core Last commit date
api 2 days ago background 2 days ago backups 2 days ago bulk 2 days ago cache 2 days ago cli 2 days ago external 2 days ago frontend 2 days ago integrations 2 days ago lazy-load 2 days ago media 2 days ago media-library 2 days ago membership 2 days ago modules 2 days ago parser 2 days ago photon 2 days ago png2jpg 2 days ago product-analytics 2 days ago rating-notification 2 days ago resize 2 days ago security 2 days ago smush 2 days ago srcset 2 days ago stats 2 days ago threads 2 days ago transform 2 days ago class-abstract-settings-dto.php 2 days ago class-activity-log-controller.php 2 days ago class-animated-status-controller.php 2 days ago class-array-utils.php 2 days ago class-attachment-id-list.php 2 days ago class-backup-size.php 2 days ago class-configs.php 2 days ago class-controller.php 2 days ago class-core.php 2 days ago class-cron-controller.php 2 days ago class-deprecated-hooks.php 2 days ago class-error-handler.php 2 days ago class-file-system.php 2 days ago class-file-utils.php 2 days ago class-format-utils.php 2 days ago class-helper.php 2 days ago class-hub-connector.php 2 days ago class-installer.php 2 days ago class-keyword-exclusions.php 2 days ago class-modules.php 2 days ago class-multisite-utils.php 2 days ago class-optimization-controller.php 2 days ago class-optimizer.php 2 days ago class-plugin-settings-watcher.php 2 days ago class-rest.php 2 days ago class-server-utils.php 2 days ago class-settings-controller.php 2 days ago class-settings-dto.php 2 days ago class-settings-sanitizer.php 2 days ago class-settings.php 2 days ago class-shim.php 2 days ago class-smush-file.php 2 days ago class-stats.php 2 days ago class-string-utils.php 2 days ago class-time-utils.php 2 days ago class-timer.php 2 days ago class-upload-dir.php 2 days ago class-url-utils.php 2 days ago class-urls-exclusions.php 2 days ago class-wp-query-utils.php 2 days ago wp-compat.php 2 days ago
class-installer.php
553 lines
1 <?php
2 /**
3 * Smush installer (update/upgrade procedures): Installer class
4 *
5 * @package Smush\Core
6 * @since 2.8.0
7 *
8 * @author Anton Vanyukov <anton@incsub.com>
9 *
10 * @copyright (c) 2018, Incsub (http://incsub.com)
11 */
12
13 namespace Smush\Core;
14
15 use Smush\App\Abstract_Page;
16 use Smush\Core\CDN\CDN_Controller;
17 use Smush\Core\Smush\Smusher;
18 use Smush\Core\Smush\Smusher_Options_Provider;
19 use WP_Smush;
20
21 if ( ! defined( 'WPINC' ) ) {
22 die;
23 }
24
25 /**
26 * Class Installer for handling updates and upgrades of the plugin.
27 *
28 * @since 2.8.0
29 */
30 class Installer {
31
32 /**
33 * Triggered on Smush deactivation.
34 *
35 * @since 3.1.0
36 */
37 public static function smush_deactivated() {
38 if ( ! class_exists( '\\Smush\\Core\\Modules\\CDN_Controller' ) ) {
39 $cdn_controller_path = __DIR__ . '/cdn/class-cdn-controller.php';
40 if ( file_exists( $cdn_controller_path ) ) {
41 require_once $cdn_controller_path;
42 }
43 }
44
45 Cron_Controller::get_instance()->unschedule_cron();
46 Settings::get_instance()->delete_setting( 'wp-smush-cdn_status' );
47
48 delete_site_option( 'wp_smush_api_auth' );
49 }
50
51 /**
52 * Redirect to Smush page after plugin activation if onboarding wizard has not been completed.
53 *
54 * @since 3.17.0
55 *
56 * @param string $plugin Plugin basename.
57 */
58 public static function redirect_to_setup_page( $plugin ) {
59 // Check if this is the Smush plugin being activated.
60 if ( WP_SMUSH_BASENAME !== $plugin ) {
61 return;
62 }
63
64 // Check if onboarding wizard has been completed.
65 $skip_quick_setup = ! empty( get_option( 'skip-smush-setup' ) );
66 if ( $skip_quick_setup ) {
67 return;
68 }
69
70 // Don't redirect if activating multiple plugins.
71 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
72 if ( isset( $_GET['activate-multi'] ) ) {
73 return;
74 }
75
76 // Don't redirect on AJAX, CLI, or network admin.
77 if ( wp_doing_ajax() || ( defined( 'WP_CLI' ) && WP_CLI ) || is_network_admin() ) {
78 return;
79 }
80
81 // Redirect to Smush page.
82 wp_safe_redirect( admin_url( 'admin.php?page=smush' ) );
83 exit;
84 }
85
86 /**
87 * Check if an existing install or new.
88 *
89 * @since 2.8.0 Moved to this class from wp-smush.php file.
90 */
91 public static function smush_activated() {
92 if ( ! defined( 'WP_SMUSH_ACTIVATING' ) ) {
93 define( 'WP_SMUSH_ACTIVATING', true );
94 }
95
96 $version = get_site_option( 'wp-smush-version' );
97 self::maybe_mark_as_pre_3_22_site( $version );
98
99 // Cache activated date time.
100 $event_name = ! empty( $version ) ? 'plugin_activated' : 'plugin_installed';
101 self::cache_event_time( $event_name );
102
103 if ( ! class_exists( '\\Smush\\Core\\Settings' ) ) {
104 require_once __DIR__ . '/class-settings.php';
105 }
106
107 Settings::get_instance()->initial_default_site_settings();
108
109 // If the version is not saved or if the version is not same as the current version,.
110 if ( ! $version || WP_SMUSH_VERSION !== $version ) {
111 global $wpdb;
112 // Check if there are any existing smush stats.
113 $results = $wpdb->get_var(
114 $wpdb->prepare(
115 "SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key=%s LIMIT 1",
116 'wp-smpro-smush-data'
117 )
118 ); // db call ok; no-cache ok.
119
120 if ( $results || $version ) {
121 update_site_option( 'wp-smush-install-type', 'existing' );
122 }
123
124 // Create directory smush table.
125 self::directory_smush_table();
126
127 // Store the plugin version in db.
128 update_site_option( 'wp-smush-version', WP_SMUSH_VERSION );
129 }
130 }
131
132 /**
133 * Handle plugin upgrades.
134 *
135 * @since 2.8.0
136 */
137 public static function upgrade_settings() {
138 // Avoid executing this over an over in same thread.
139 if ( defined( 'WP_SMUSH_ACTIVATING' ) || ( defined( 'WP_SMUSH_UPGRADING' ) && WP_SMUSH_UPGRADING ) ) {
140 return;
141 }
142
143 if ( ! class_exists( '\\Smush\\Core\\Settings' ) ) {
144 require_once __DIR__ . '/class-settings.php';
145 }
146
147 $version = get_site_option( 'wp-smush-version' );
148
149 if ( false === $version ) {
150 self::smush_activated();
151 } else {
152 self::maybe_mark_as_pre_3_22_site( $version );
153 }
154
155 if ( false !== $version && WP_SMUSH_VERSION !== $version ) {
156 if ( ! defined( 'WP_SMUSH_UPGRADING' ) ) {
157 define( 'WP_SMUSH_UPGRADING', true );
158 }
159
160 // Cache last updated time.
161 self::cache_event_time( 'plugin_upgraded' );
162
163 if ( version_compare( $version, '3.7.0', '<' ) ) {
164 self::upgrade_3_7_0();
165 }
166
167 if ( version_compare( $version, '3.8.0', '<' ) ) {
168 // Delete the flag for hiding the BF modal because it was removed.
169 delete_site_option( 'wp-smush-hide_blackfriday_modal' );
170 }
171
172 if ( version_compare( $version, '3.8.3', '<' ) ) {
173 // Delete this unused setting, leftover from old smush.
174 delete_option( 'wp-smush-transparent_png' );
175 }
176
177 if ( version_compare( $version, '3.9.5', '<' ) ) {
178 delete_site_option( 'wp-smush-show-black-friday' );
179 }
180
181 if ( version_compare( $version, '3.9.10', '<' ) ) {
182 self::dir_smush_set_primary_key();
183 }
184
185 if ( version_compare( $version, '3.10.0', '<' ) ) {
186 self::upgrade_3_10_0();
187 }
188
189 if ( version_compare( $version, '3.10.3', '<' ) ) {
190 self::upgrade_3_10_3();
191 }
192
193 if ( version_compare( $version, '3.16.0', '<' ) ) {
194 self::regenerate_preset_configs_before_3_16_0();
195 } elseif ( version_compare( $version, '3.21.0', '<' ) ) {
196 self::regenerate_preset_configs();
197 }
198
199 if ( version_compare( $version, '3.21.0', '<' ) ) {
200 self::upgrade_3_21_0();
201 }
202
203 if ( version_compare( $version, '4.0', '<' ) ) {
204 self::upgrade_4_0_0();
205 }
206
207 if ( version_compare( $version, '4.0', '<' ) ) {
208 $hide_new_feature_highlight_modal = apply_filters( 'wpmudev_branding_hide_doc_link', false );
209 if ( ! $hide_new_feature_highlight_modal ) {
210 // Add the flag to display the new feature background process modal.
211 add_site_option( 'wp-smush-show_upgrade_modal', true );
212 }
213
214 // Show new feature hotspot.
215 // self::set_new_feature_hotspot_flag();
216 }
217
218 // Create/upgrade directory smush table.
219 self::directory_smush_table();
220
221 // Store the latest plugin version in db.
222 update_site_option( 'wp-smush-version', WP_SMUSH_VERSION );
223
224 self::reset_smusher_error_counts();
225 }
226 }
227
228 /**
229 * Create or upgrade custom table for directory Smush.
230 *
231 * After creating or upgrading the custom table, update the path_hash
232 * column value and structure if upgrading from old version.
233 *
234 * @since 2.9.0
235 */
236 public static function directory_smush_table() {
237 if ( ! class_exists( '\\Smush\\Core\\Modules\\Abstract_Module' ) ) {
238 require_once __DIR__ . '/modules/class-abstract-module.php';
239 }
240
241 if ( ! class_exists( '\\Smush\\Core\\Modules\\Dir' ) ) {
242 require_once __DIR__ . '/modules/class-dir.php';
243 }
244
245 // No need to continue on sub sites.
246 if ( ! Modules\Dir::should_continue() ) {
247 return;
248 }
249
250 // Create a class object, if doesn't exists.
251 if ( ! is_object( WP_Smush::get_instance()->core()->mod->dir ) ) {
252 WP_Smush::get_instance()->core()->mod->dir = new Modules\Dir();
253 }
254
255 // Create/upgrade directory smush table.
256 WP_Smush::get_instance()->core()->mod->dir->create_table();
257 }
258
259 /**
260 * Set primary key for directory smush table on upgrade to 3.9.10.
261 *
262 * @since 3.9.10
263 */
264 private static function dir_smush_set_primary_key() {
265 global $wpdb;
266
267 // Only call it after creating table smush_dir_images. If the table doesn't exist, returns.
268 if ( ! Modules\Dir::table_exist() ) {
269 return;
270 }
271
272 // If the table is already set the primary key, return.
273 if ( $wpdb->query( $wpdb->prepare( "SHOW INDEXES FROM {$wpdb->base_prefix}smush_dir_images WHERE Key_name = %s;", 'PRIMARY' ) ) ) {
274 return;
275 }
276
277 // Set column ID as a primary key.
278 $wpdb->query( "ALTER TABLE {$wpdb->base_prefix}smush_dir_images ADD PRIMARY KEY (id);" );
279 }
280
281 /**
282 * Check if table needs to be created and create if not exists.
283 *
284 * @since 3.8.6
285 */
286 public static function maybe_create_table() {
287 if ( ! function_exists( 'get_current_screen' ) ) {
288 return;
289 }
290
291 if ( isset( get_current_screen()->id ) && false === strpos( get_current_screen()->id, 'page_smush' ) ) {
292 return;
293 }
294
295 self::directory_smush_table();
296 }
297
298 /**
299 * Upgrade to 3.7.0
300 *
301 * @since 3.7.0
302 */
303 private static function upgrade_3_7_0() {
304 delete_site_option( 'wp-smush-run_recheck' );
305
306 // Fix the "None" animation in lazy-load options.
307 $lazy = Settings::get_instance()->get_setting( 'wp-smush-lazy_load' );
308
309 if ( ! $lazy || ! isset( $lazy['animation'] ) || ! isset( $lazy['animation']['selected'] ) ) {
310 return;
311 }
312
313 if ( '0' === $lazy['animation']['selected'] ) {
314 $lazy['animation']['selected'] = 'none';
315 Settings::get_instance()->set_setting( 'wp-smush-lazy_load', $lazy );
316 }
317 }
318
319 /**
320 * Upgrade to 3.10.0
321 *
322 * @return void
323 * @since 3.10.0
324 */
325 private static function upgrade_3_10_0() {
326 // Remove unused options.
327 delete_site_option( 'wp-smush-hide_pagespeed_suggestion' );
328 delete_site_option( 'wp-smush-hide_upgrade_notice' );
329
330 // Rename the default config.
331 $stored_configs = get_site_option( 'wp-smush-preset_configs', false );
332 if ( is_array( $stored_configs ) && isset( $stored_configs[0] ) && isset( $stored_configs[0]['name'] ) && 'Basic config' === $stored_configs[0]['name'] ) {
333 $stored_configs[0]['name'] = __( 'Smush', 'wp-smushit' );
334 update_site_option( 'wp-smush-preset_configs', $stored_configs );
335 }
336 }
337
338 /**
339 * Upgrade to 4.0.0
340 *
341 * @return void
342 * @since 4.0.0
343 */
344 private static function upgrade_4_0_0() {
345 $settings = Settings::get_instance();
346 $lazy_options = $settings->get_setting( 'wp-smush-lazy_load' );
347 if ( ! is_array( $lazy_options ) ) {
348 return;
349 }
350
351 $changed = self::migrate_lazy_load_placeholder_in_options( $lazy_options );
352 $changed = self::migrate_lazy_load_spinner_in_options( $lazy_options ) || $changed;
353
354 if ( $changed ) {
355 $settings->set_setting( 'wp-smush-lazy_load', $lazy_options );
356 }
357 }
358
359 /**
360 * Migrate lazy load placeholder settings.
361 *
362 * @param array $lazy_options Lazy load options.
363 *
364 * @return bool True when settings were changed.
365 */
366 private static function migrate_lazy_load_placeholder_in_options( &$lazy_options ) {
367 $selected_placeholder = isset( $lazy_options['animation']['placeholder']['selected'] ) ? (int) $lazy_options['animation']['placeholder']['selected'] : 1;
368 if ( empty( $selected_placeholder ) || 2 !== $selected_placeholder ) {
369 return false;
370 }
371
372 // Update lazy load settings.
373 $lazy_options['animation']['placeholder']['selected'] = 1;
374 return true;
375 }
376
377 /**
378 * Migrate lazy load spinner settings.
379 *
380 * @param array $lazy_options Lazy load options.
381 *
382 * @return bool True when settings were changed.
383 */
384 private static function migrate_lazy_load_spinner_in_options( &$lazy_options ) {
385 $selected_spinner = isset( $lazy_options['animation']['spinner']['selected'] ) ? (int) $lazy_options['animation']['spinner']['selected'] : 1;
386 if ( empty( $selected_spinner ) || $selected_spinner > 5 ) {
387 return false;
388 }
389
390 $default_spinner = 1;
391 $map_spinner = array(
392 1 => 2,
393 3 => 3,
394 );
395 // Map the selected spinner to the new value.
396 $selected_spinner = isset( $map_spinner[ $selected_spinner ] ) ? $map_spinner[ $selected_spinner ] : $default_spinner;
397
398 // Update lazy load settings.
399 $lazy_options['animation']['spinner']['selected'] = $selected_spinner;
400 return true;
401 }
402
403 /**
404 * Upgrade 3.10.3
405 *
406 * @return void
407 * @since 3.10.3
408 */
409 private static function upgrade_3_10_3() {
410 delete_site_option( 'wp-smush-hide_smush_welcome' );
411 // Logger options.
412 delete_site_option( 'wdev_logger_wp-smush-pro' );
413 delete_site_option( 'wdev_logger_wp-smushit' );
414 // Clean old cronjob (missing callback).
415 if ( wp_next_scheduled( 'wdev_logger_clear_logs' ) ) {
416 wp_clear_scheduled_hook( 'wdev_logger_clear_logs' );
417 }
418 }
419
420 private static function maybe_mark_as_pre_3_22_site( $version ) {
421 if ( ! $version || false !== get_site_option( 'wp_smush_pre_3_22_site' ) ) {
422 return;
423 }
424 if ( version_compare( $version, '3.21.1', '>' ) ) {
425 $version = 0;
426 }
427 update_site_option( 'wp_smush_pre_3_22_site', $version );
428 }
429
430 private static function regenerate_preset_configs_before_3_16_0() {
431 // Update Smush mode for display on Configs page.
432 $stored_configs = get_site_option( 'wp-smush-preset_configs', array() );
433 if ( empty( $stored_configs ) || ! is_array( $stored_configs ) ) {
434 return;
435 }
436
437 $configs_handler = Configs::get_instance();
438 $new_settings = array(
439 'background_email' => false,
440 );
441 foreach ( $stored_configs as $key => $preset_config ) {
442 if ( empty( $preset_config['config']['configs']['settings'] ) ) {
443 continue;
444 }
445
446 $preset_config ['config']['configs']['settings'] = array_merge( $new_settings, $preset_config['config']['configs']['settings'] );
447 $preset_config ['config'] = $configs_handler->sanitize_and_format_configs( $preset_config['config']['configs'] );
448 $stored_configs[ $key ] = $preset_config;
449 }
450 update_site_option( 'wp-smush-preset_configs', $stored_configs );
451 }
452
453 private static function regenerate_preset_configs() {
454 // Regenerate preset configs to update Next-Gen Formats.
455 $stored_configs = get_site_option( 'wp-smush-preset_configs', array() );
456 if ( empty( $stored_configs ) || ! is_array( $stored_configs ) ) {
457 return;
458 }
459
460 $configs_handler = Configs::get_instance();
461 foreach ( $stored_configs as $key => $preset_config ) {
462 if ( empty( $preset_config['config']['configs'] ) ) {
463 continue;
464 }
465
466 $preset_config ['config'] = $configs_handler->sanitize_and_format_configs( $preset_config['config']['configs'] );
467 $stored_configs[ $key ] = $preset_config;
468 }
469
470 update_site_option( 'wp-smush-preset_configs', $stored_configs );
471 }
472
473 private static function upgrade_3_21_0() {
474 self::migrate_auto_resize_to_new_settings();
475 self::migrate_auto_resize_to_new_settings_for_sub_sites();
476 }
477
478 private static function migrate_auto_resize_to_new_settings_for_sub_sites() {
479 if ( ! is_multisite() ) {
480 return;
481 }
482
483 self::for_each_public_site( function() {
484 self::migrate_auto_resize_to_new_settings();
485 } );
486 }
487
488 private static function migrate_auto_resize_to_new_settings() {
489 $settings = Settings::get_instance();
490 $is_auto_resizing_active = $settings->get( 'auto_resize' );
491 if ( ! $is_auto_resizing_active ) {
492 return;
493 }
494 $settings->set( 'auto_resizing', $is_auto_resizing_active );
495 $settings->set( 'cdn_dynamic_sizes', $is_auto_resizing_active );
496 $settings->delete( 'auto_resize' );
497 }
498
499 private static function cache_event_time( $event ) {
500 $option_key = 'wp_smush_event_times';
501 $event_times = get_site_option( $option_key, array() );
502 $event_times[ $event ] = time();
503 update_site_option( $option_key, $event_times );
504 }
505
506 /**
507 * @return void
508 */
509 private static function reset_smusher_error_counts() {
510 $smusher_options = ( new Smusher_Options_Provider() )->get_options();
511 ( new Smusher( $smusher_options ) )->reset_error_counts();
512 }
513
514 private static function set_new_feature_hotspot_flag() {
515 add_option( 'wp-smush-show-new-feature-hotspot', true );
516 self::set_new_feature_hotspot_flag_for_sub_sites();
517 }
518
519 private static function set_new_feature_hotspot_flag_for_sub_sites() {
520 if ( ! is_multisite() ) {
521 return;
522 }
523
524 self::for_each_public_site( function() {
525 add_option( 'wp-smush-show-new-feature-hotspot', true );
526 } );
527 }
528
529 private static function for_each_public_site( $callback ) {
530 if ( ! is_multisite() ) {
531 return;
532 }
533
534 $site_args = array(
535 'fields' => 'ids',
536 'public' => 1,
537 'number' => 250, // Limit to 250 sites to avoid performance issues.
538 );
539
540 $site_ids = get_sites( $site_args );
541 if ( empty( $site_ids ) ) {
542 return;
543 }
544
545 foreach ( $site_ids as $site_id ) {
546 switch_to_blog( $site_id );
547 call_user_func( $callback );
548 restore_current_blog();
549 }
550 }
551 }
552
553