| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/** |
| 5 |
* The class responsible for handling the migration. |
| 6 |
* |
| 7 |
* @link https://webdeclic.com |
| 8 |
* @since 3.4.0 |
| 9 |
* |
| 10 |
* @package Quickwebp |
| 11 |
* @subpackage Quickwebp/admin |
| 12 |
*/ |
| 13 |
class Quickwebp_Migration { |
| 14 |
|
| 15 |
/** |
| 16 |
* Init the migration. |
| 17 |
*/ |
| 18 |
public function init_migration() { |
| 19 |
$this->maybe_migrate_optimization_stats(); |
| 20 |
|
| 21 |
$last_version = get_option( 'quickwebp_plugin_version', '1.0.0' ); |
| 22 |
|
| 23 |
if ( version_compare( $last_version, QUICKWEBP_VERSION, '<' ) ) { |
| 24 |
$this->run_migration_tasks( $last_version ); |
| 25 |
update_option( 'quickwebp_plugin_version', QUICKWEBP_VERSION ); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Backfill installation age and optimization statistics for existing sites. |
| 31 |
*/ |
| 32 |
private function maybe_migrate_optimization_stats() { |
| 33 |
if ( get_option( 'quickwebp_optimization_stats_migrated', false ) ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$stats = array( |
| 38 |
'images_optimized' => 0, |
| 39 |
'bytes_before' => 0, |
| 40 |
'bytes_after' => 0, |
| 41 |
'bytes_saved' => 0, |
| 42 |
); |
| 43 |
$page = 1; |
| 44 |
|
| 45 |
do { |
| 46 |
$query = new WP_Query( |
| 47 |
array( |
| 48 |
'post_type' => 'attachment', |
| 49 |
'post_status' => 'any', |
| 50 |
'posts_per_page' => 200, |
| 51 |
'paged' => $page, |
| 52 |
'fields' => 'ids', |
| 53 |
'orderby' => 'ID', |
| 54 |
'order' => 'ASC', |
| 55 |
'no_found_rows' => false, |
| 56 |
'update_post_meta_cache' => false, |
| 57 |
'update_post_term_cache' => false, |
| 58 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 59 |
'meta_query' => array( |
| 60 |
'relation' => 'OR', |
| 61 |
array( |
| 62 |
'key' => 'quickwebp_already_optimized', |
| 63 |
'compare' => 'EXISTS', |
| 64 |
), |
| 65 |
array( |
| 66 |
'key' => 'quickwebp_data', |
| 67 |
'compare' => 'EXISTS', |
| 68 |
), |
| 69 |
), |
| 70 |
) |
| 71 |
); |
| 72 |
|
| 73 |
foreach ( $query->posts as $attachment_id ) { |
| 74 |
$data = get_post_meta( $attachment_id, 'quickwebp_data', true ); |
| 75 |
|
| 76 |
$stats['images_optimized']++; |
| 77 |
|
| 78 |
if ( ! is_array( $data ) ) { |
| 79 |
continue; |
| 80 |
} |
| 81 |
|
| 82 |
foreach ( $data as $size_data ) { |
| 83 |
$stats['bytes_before'] += absint( $size_data['original_size'] ?? 0 ); |
| 84 |
$stats['bytes_after'] += absint( $size_data['optimized_size'] ?? 0 ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
$max_pages = (int) $query->max_num_pages; |
| 89 |
$page++; |
| 90 |
} while ( $page <= $max_pages ); |
| 91 |
|
| 92 |
$stats['bytes_saved'] = max( 0, $stats['bytes_before'] - $stats['bytes_after'] ); |
| 93 |
|
| 94 |
if ( $stats['images_optimized'] > 0 ) { |
| 95 |
update_option( 'quickwebp_optimization_stats', $stats, false ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! get_option( 'quickwebp_installed_at', false ) ) { |
| 99 |
$oldest_optimized = get_posts( |
| 100 |
array( |
| 101 |
'post_type' => 'attachment', |
| 102 |
'post_status' => 'any', |
| 103 |
'posts_per_page' => 1, |
| 104 |
'orderby' => 'date', |
| 105 |
'order' => 'ASC', |
| 106 |
'fields' => 'ids', |
| 107 |
'update_post_meta_cache' => false, |
| 108 |
'update_post_term_cache' => false, |
| 109 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 110 |
'meta_query' => array( |
| 111 |
array( |
| 112 |
'key' => 'quickwebp_already_optimized', |
| 113 |
'compare' => 'EXISTS', |
| 114 |
), |
| 115 |
), |
| 116 |
) |
| 117 |
); |
| 118 |
|
| 119 |
if ( ! empty( $oldest_optimized ) ) { |
| 120 |
$installed_at = get_post_time( 'U', true, $oldest_optimized[0] ); |
| 121 |
} else { |
| 122 |
$installed_at = time() - ( 8 * DAY_IN_SECONDS ); |
| 123 |
} |
| 124 |
|
| 125 |
add_option( 'quickwebp_installed_at', $installed_at, '', false ); |
| 126 |
} |
| 127 |
|
| 128 |
update_option( 'quickwebp_optimization_stats_migrated', 1, false ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Run migration tasks based on the last version. |
| 133 |
*/ |
| 134 |
private function run_migration_tasks( $last_version ) { |
| 135 |
|
| 136 |
if ( version_compare( $last_version, '3.4.0', '<' ) ) { |
| 137 |
|
| 138 |
$quality = get_option( 'quickwebp_settings_conversion_quality', '' ); |
| 139 |
if ( ! empty( $quality ) && ! in_array( $quality, array( 'low', 'medium', 'high', 'extra_high' ) ) ) { |
| 140 |
|
| 141 |
$quality = (int) $quality; |
| 142 |
|
| 143 |
if ( $quality < 50 ) { |
| 144 |
$quality = 'low'; |
| 145 |
} elseif ( $quality < 60 ) { |
| 146 |
$quality = 'medium'; |
| 147 |
} elseif ( $quality < 80 ) { |
| 148 |
$quality = 'high'; |
| 149 |
} else { |
| 150 |
$quality = 'extra_high'; |
| 151 |
} |
| 152 |
|
| 153 |
update_option( 'quickwebp_settings_conversion_quality', $quality ); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|