| @@ -15,8 +15,9 @@ | ||
| 15 | 15 | /** |
| 16 | 16 | * Init the migration. |
| 17 | 17 | */ |
| 18 | 18 | public function init_migration() { |
| 19 | + $this->maybe_migrate_optimization_stats(); | |
| 19 | 20 | |
| 20 | 21 | $last_version = get_option( 'quickwebp_plugin_version', '1.0.0' ); |
| 21 | 22 | |
| 22 | 23 | if ( version_compare( $last_version, QUICKWEBP_VERSION, '<' ) ) { |
| @@ -22,8 +23,110 @@ | ||
| 22 | 23 | if ( version_compare( $last_version, QUICKWEBP_VERSION, '<' ) ) { |
| 23 | 24 | $this->run_migration_tasks( $last_version ); |
| 24 | 25 | update_option( 'quickwebp_plugin_version', QUICKWEBP_VERSION ); |
| 25 | 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 ); | |
| 26 | 129 | } |
| 27 | 130 | |
| 28 | 131 | /** |
| 29 | 132 | * Run migration tasks based on the last version. |