| @@ -1,142 +1,348 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) or die( 'Cheatin’ uh?' ); | |
| 2 | +defined( 'ABSPATH' ) || exit; | |
| 3 | 3 | |
| 4 | -/* | |
| 5 | - * Tell WP what to do when admin is loaded aka upgrader | |
| 4 | +/** | |
| 5 | + * Tell WP what to do when admin is loaded aka upgrader. | |
| 6 | 6 | * |
| 7 | 7 | * @since 1.0 |
| 8 | 8 | */ |
| 9 | -add_action( 'admin_init', '_imagify_upgrader' ); | |
| 10 | 9 | function _imagify_upgrader() { |
| 11 | - $current_version = get_imagify_option( 'version' ); | |
| 10 | + // Back-compat' with previous version of the upgrader. | |
| 11 | + imagify_upgrader_upgrade(); | |
| 12 | 12 | |
| 13 | - // You can hook the upgrader to trigger any action when Imagify is upgraded | |
| 14 | - // first install | |
| 15 | - if ( ! $current_version ) { | |
| 13 | + // Version stored on the network. | |
| 14 | + $network_version = Imagify_Options::get_instance()->get( 'version' ); | |
| 15 | + // Version stored at the site level. | |
| 16 | + $site_version = Imagify_Data::get_instance()->get( 'version' ); | |
| 17 | + | |
| 18 | + if ( ! $network_version ) { | |
| 19 | + // First install (network). | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Triggered on Imagify first install (network). | |
| 23 | + * | |
| 24 | + * @since 1.7 | |
| 25 | + * @author Grégory Viguier | |
| 26 | + */ | |
| 27 | + do_action( 'imagify_first_network_install' ); | |
| 28 | + } elseif ( IMAGIFY_VERSION !== $network_version ) { | |
| 29 | + // Already installed but got updated (network). | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Triggered on Imagify upgrade (network). | |
| 33 | + * | |
| 34 | + * @since 1.7 | |
| 35 | + * @author Grégory Viguier | |
| 36 | + * | |
| 37 | + * @param string $network_version Previous version stored on the network. | |
| 38 | + * @param string $site_version Previous version stored on site level. | |
| 39 | + */ | |
| 40 | + do_action( 'imagify_network_upgrade', $network_version, $site_version ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + // If any upgrade has been done, we flush and update version. | |
| 44 | + if ( did_action( 'imagify_first_network_install' ) || did_action( 'imagify_network_upgrade' ) ) { | |
| 45 | + Imagify_Options::get_instance()->set( 'version', IMAGIFY_VERSION ); | |
| 46 | + } | |
| 47 | + | |
| 48 | + if ( ! $site_version ) { | |
| 49 | + // First install (site level). | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Triggered on Imagify first install (site level). | |
| 53 | + * | |
| 54 | + * @since 1.0 | |
| 55 | + */ | |
| 16 | 56 | do_action( 'imagify_first_install' ); |
| 57 | + } elseif ( IMAGIFY_VERSION !== $site_version ) { | |
| 58 | + // Already installed but got updated (site level). | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Triggered on Imagify upgrade (site level). | |
| 62 | + * | |
| 63 | + * @since 1.0 | |
| 64 | + * @since 1.7 $network_version replaces the "new version" (which can easily be grabbed with the constant). | |
| 65 | + * | |
| 66 | + * @param string $network_version Previous version stored on the network. | |
| 67 | + * @param string $site_version Previous version stored on site level. | |
| 68 | + */ | |
| 69 | + do_action( 'imagify_upgrade', $network_version, $site_version ); | |
| 17 | 70 | } |
| 18 | - // already installed but got updated | |
| 19 | - elseif ( IMAGIFY_VERSION != $current_version ) { | |
| 20 | - do_action( 'imagify_upgrade', IMAGIFY_VERSION, $current_version ); | |
| 71 | + | |
| 72 | + // If any upgrade has been done, we flush and update version. | |
| 73 | + if ( did_action( 'imagify_first_install' ) || did_action( 'imagify_upgrade' ) ) { | |
| 74 | + Imagify_Data::get_instance()->set( 'version', IMAGIFY_VERSION ); | |
| 21 | 75 | } |
| 76 | +} | |
| 77 | +add_action( 'admin_init', '_imagify_upgrader' ); | |
| 22 | 78 | |
| 23 | - // If any upgrade has been done, we flush and update version # | |
| 24 | - if ( did_action( 'imagify_first_install' ) || did_action( 'imagify_upgrade' ) ) { | |
| 25 | - $options = get_site_option( IMAGIFY_SETTINGS_SLUG ); // do not use get_imagify_option() here | |
| 26 | - $options['version'] = IMAGIFY_VERSION; | |
| 79 | +/** | |
| 80 | + * Upgrade the upgrader: | |
| 81 | + * Imagify 1.7 splits "network version" and "site version". Since the "site version" didn't exist before 1.7, we need to provide a version based on the "network version". | |
| 82 | + * | |
| 83 | + * @since 1.7 | |
| 84 | + * @author Grégory Viguier | |
| 85 | + */ | |
| 86 | +function imagify_upgrader_upgrade() { | |
| 87 | + global $wpdb; | |
| 27 | 88 | |
| 28 | - update_site_option( IMAGIFY_SETTINGS_SLUG, $options ); | |
| 89 | + // Version stored on the network. | |
| 90 | + $network_version = Imagify_Options::get_instance()->get( 'version' ); | |
| 91 | + | |
| 92 | + if ( ! $network_version ) { | |
| 93 | + // Really first install. | |
| 94 | + return; | |
| 29 | 95 | } |
| 96 | + | |
| 97 | + // Version stored at the site level. | |
| 98 | + $site_version = Imagify_Data::get_instance()->get( 'version' ); | |
| 99 | + | |
| 100 | + if ( $site_version ) { | |
| 101 | + // This site's upgrader is already upgraded. | |
| 102 | + return; | |
| 103 | + } | |
| 104 | + | |
| 105 | + if ( ! is_multisite() ) { | |
| 106 | + // Not a multisite, so both versions must have the same value. | |
| 107 | + Imagify_Data::get_instance()->set( 'version', $network_version ); | |
| 108 | + return; | |
| 109 | + } | |
| 110 | + | |
| 111 | + $sites = get_site_option( 'imagify_old_version' ); | |
| 112 | + | |
| 113 | + if ( IMAGIFY_VERSION !== $network_version && ! $sites ) { | |
| 114 | + // The network is not up-to-date yet: store the site IDs that must be updated. | |
| 115 | + $network_id = function_exists( 'get_current_network_id' ) ? get_current_network_id() : $wpdb->siteid; | |
| 116 | + $sites = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d AND archived = 0 AND deleted = 0", $network_id ) ); | |
| 117 | + $sites = array_map( 'absint', $sites ); | |
| 118 | + $sites = array_filter( $sites ); | |
| 119 | + | |
| 120 | + if ( ! $sites ) { | |
| 121 | + // Uh? | |
| 122 | + return; | |
| 123 | + } | |
| 124 | + | |
| 125 | + // We store the old network version and the site Ids: those sites will need to be upgraded from this version. | |
| 126 | + $sites['version'] = $network_version; | |
| 127 | + | |
| 128 | + add_site_option( 'imagify_old_version', $sites ); | |
| 129 | + } | |
| 130 | + | |
| 131 | + if ( empty( $sites['version'] ) ) { | |
| 132 | + // WTF. | |
| 133 | + delete_site_option( 'imagify_old_version' ); | |
| 134 | + return; | |
| 135 | + } | |
| 136 | + | |
| 137 | + $network_version = $sites['version']; | |
| 138 | + unset( $sites['version'] ); | |
| 139 | + | |
| 140 | + $sites = array_flip( $sites ); | |
| 141 | + $site_id = get_current_blog_id(); | |
| 142 | + | |
| 143 | + if ( ! isset( $sites[ $site_id ] ) ) { | |
| 144 | + // This site is already upgraded. | |
| 145 | + return; | |
| 146 | + } | |
| 147 | + | |
| 148 | + unset( $sites[ $site_id ] ); | |
| 149 | + | |
| 150 | + if ( ! $sites ) { | |
| 151 | + // We're done, all the sites have been upgraded. | |
| 152 | + delete_site_option( 'imagify_old_version' ); | |
| 153 | + } else { | |
| 154 | + // Some sites still need to be upgraded. | |
| 155 | + $sites = array_flip( $sites ); | |
| 156 | + $sites['version'] = $network_version; | |
| 157 | + update_site_option( 'imagify_old_version', $sites ); | |
| 158 | + } | |
| 159 | + | |
| 160 | + Imagify_Data::get_instance()->set( 'version', $network_version ); | |
| 30 | 161 | } |
| 31 | 162 | |
| 32 | 163 | /** |
| 33 | - * Keeps this function up to date at each version | |
| 164 | + * Keeps this function up to date at each version. | |
| 34 | 165 | * |
| 35 | 166 | * @since 1.0 |
| 36 | 167 | */ |
| 37 | -add_action( 'imagify_first_install', '_imagify_first_install' ); | |
| 38 | -function _imagify_first_install() { | |
| 168 | +function _imagify_first_install() { | |
| 39 | 169 | // Set a transient to know when we will have to display a notice to ask the user to rate the plugin. |
| 40 | - set_site_transient( 'imagify_seen_rating_notice', true, DAY_IN_SECONDS * 7 ); | |
| 41 | - | |
| 42 | - // Create Options | |
| 43 | - add_site_option( IMAGIFY_SETTINGS_SLUG, | |
| 44 | - array( | |
| 45 | - 'api_key' => '', | |
| 46 | - 'optimization_level' => 1, | |
| 47 | - 'auto_optimize' => 1, | |
| 48 | - 'backup' => 1, | |
| 49 | - 'resize_larger' => '', | |
| 50 | - 'resize_larger_w' => '', | |
| 51 | - 'exif' => 0, | |
| 52 | - 'disallowed-sizes' => array(), | |
| 53 | - 'admin_bar_menu' => 1 | |
| 54 | - ) | |
| 55 | - ); | |
| 170 | + set_site_transient( 'imagify_seen_rating_notice', true, DAY_IN_SECONDS * 3 ); | |
| 56 | 171 | } |
| 172 | +add_action( 'imagify_first_network_install', '_imagify_first_install' ); | |
| 57 | 173 | |
| 58 | 174 | /** |
| 59 | - * What to do when Imagify is updated, depending on versions | |
| 175 | + * What to do when Imagify is updated, depending on versions. | |
| 60 | 176 | * |
| 61 | 177 | * @since 1.0 |
| 178 | + * @since 1.7 $network_version replaces the "new version" (which can easily be grabbed with the constant). | |
| 179 | + * | |
| 180 | + * @param string $network_version Previous version stored on the network. | |
| 181 | + * @param string $site_version Previous version stored on site level. | |
| 62 | 182 | */ |
| 63 | -add_action( 'imagify_upgrade', '_imagify_new_upgrade', 10, 2 ); | |
| 64 | -function _imagify_new_upgrade( $imagify_version, $current_version ) { | |
| 65 | - if ( version_compare( $current_version, '1.2', '<' ) ) { | |
| 66 | - // Update all already optimized images status from 'error' to 'already_optimized' | |
| 183 | +function _imagify_new_upgrade( $network_version, $site_version ) { | |
| 184 | + global $wpdb; | |
| 185 | + | |
| 186 | + $options = Imagify_Options::get_instance(); | |
| 187 | + | |
| 188 | + // 1.2 | |
| 189 | + if ( version_compare( $site_version, '1.2' ) < 0 ) { | |
| 190 | + // Update all already optimized images status from 'error' to 'already_optimized'. | |
| 67 | 191 | $query = new WP_Query( |
| 68 | - array( | |
| 192 | + [ | |
| 193 | + 'is_imagify' => true, | |
| 69 | 194 | 'post_type' => 'attachment', |
| 70 | - 'post_status' => 'inherit', | |
| 71 | - 'post_mime_type' => 'image', | |
| 72 | - 'meta_key' => '_imagify_status', | |
| 73 | - 'meta_value' => 'error', | |
| 195 | + 'post_status' => imagify_get_post_statuses(), | |
| 196 | + 'post_mime_type' => imagify_get_mime_types(), | |
| 197 | + 'meta_key' => '_imagify_status', | |
| 198 | + 'meta_value' => 'error', | |
| 74 | 199 | 'posts_per_page' => -1, |
| 75 | 200 | 'update_post_term_cache' => false, |
| 76 | 201 | 'no_found_rows' => true, |
| 77 | - 'fields' => 'ids' | |
| 78 | - ) | |
| 202 | + 'fields' => 'ids', | |
| 203 | + ] | |
| 79 | 204 | ); |
| 80 | - | |
| 81 | - $ids = (array) $query->posts; | |
| 82 | - | |
| 83 | - foreach ( $ids as $id ) { | |
| 84 | - $attachment = new Imagify_Attachment( $id ); | |
| 85 | - $attachment_error = $attachment->get_optimized_error(); | |
| 86 | - $attachment_error = trim( $attachment_error ); | |
| 87 | - $attachment_status = get_post_meta( $id, '_imagify_status', true ); | |
| 88 | - | |
| 89 | - if ( false !== strpos( $attachment_error, 'This image is already compressed' ) ) { | |
| 90 | - update_post_meta( $id, '_imagify_status', 'already_optimized' ); | |
| 205 | + | |
| 206 | + if ( $query->posts ) { | |
| 207 | + foreach ( (array) $query->posts as $id ) { | |
| 208 | + $data = get_post_meta( $id, '_imagify_data', true ); | |
| 209 | + $error = ! empty( $data['sizes']['full']['error'] ) ? $data['sizes']['full']['error'] : ''; | |
| 210 | + | |
| 211 | + if ( false !== strpos( $error, 'This image is already compressed' ) ) { | |
| 212 | + update_post_meta( $id, '_imagify_status', 'already_optimized' ); | |
| 213 | + } | |
| 91 | 214 | } |
| 92 | 215 | } |
| 93 | - | |
| 94 | - // Auto-activate the Admin Bar option | |
| 95 | - $options = get_site_option( IMAGIFY_SETTINGS_SLUG ); | |
| 96 | - $options['admin_bar_menu'] = 1; | |
| 97 | - update_site_option( IMAGIFY_SETTINGS_SLUG, $options ); | |
| 216 | + | |
| 217 | + // Auto-activate the Admin Bar option. | |
| 218 | + $options->set( 'admin_bar_menu', 1 ); | |
| 98 | 219 | } |
| 99 | - | |
| 100 | - if ( version_compare( $current_version, '1.3.2', '<' ) ) { | |
| 101 | - // Update all already optimized images status from 'error' to 'already_optimized' | |
| 220 | + | |
| 221 | + // 1.3.2 | |
| 222 | + if ( version_compare( $site_version, '1.3.2' ) < 0 ) { | |
| 223 | + // Update all already optimized images status from 'error' to 'already_optimized'. | |
| 102 | 224 | $query = new WP_Query( |
| 103 | - array( | |
| 225 | + [ | |
| 226 | + 'is_imagify' => true, | |
| 104 | 227 | 'post_type' => 'attachment', |
| 105 | - 'post_status' => 'inherit', | |
| 106 | - 'post_mime_type' => 'image', | |
| 107 | - 'meta_query' => array( | |
| 228 | + 'post_status' => imagify_get_post_statuses(), | |
| 229 | + 'post_mime_type' => imagify_get_mime_types(), | |
| 230 | + 'meta_query' => [ | |
| 108 | 231 | 'relation' => 'AND', |
| 109 | - array( | |
| 232 | + [ | |
| 110 | 233 | 'key' => '_imagify_data', |
| 111 | - 'compare' => 'EXISTS' | |
| 112 | - ), | |
| 113 | - array( | |
| 234 | + 'compare' => 'EXISTS', | |
| 235 | + ], | |
| 236 | + [ | |
| 114 | 237 | 'key' => '_imagify_optimization_level', |
| 115 | - 'compare' => 'NOT EXISTS' | |
| 116 | - ), | |
| 117 | - ), | |
| 238 | + 'compare' => 'NOT EXISTS', | |
| 239 | + ], | |
| 240 | + ], | |
| 118 | 241 | 'posts_per_page' => -1, |
| 119 | 242 | 'update_post_term_cache' => false, |
| 120 | 243 | 'no_found_rows' => true, |
| 121 | - 'fields' => 'ids' | |
| 122 | - ) | |
| 244 | + 'fields' => 'ids', | |
| 245 | + ] | |
| 123 | 246 | ); |
| 124 | - | |
| 125 | - $ids = (array) $query->posts; | |
| 126 | - | |
| 127 | - foreach ( $ids as $id ) { | |
| 128 | - $attachment = new Imagify_Attachment( $id ); | |
| 129 | - $attachment_stats = $attachment->get_stats_data(); | |
| 130 | - | |
| 131 | - if ( isset( $attachment_stats['aggressive'] ) ) { | |
| 132 | - update_post_meta( $id, '_imagify_optimization_level', (int) $attachment_stats['aggressive'] ); | |
| 133 | - } | |
| 247 | + | |
| 248 | + if ( $query->posts ) { | |
| 249 | + foreach ( (array) $query->posts as $id ) { | |
| 250 | + $data = get_post_meta( $id, '_imagify_data', true ); | |
| 251 | + $stats = isset( $data['stats'] ) ? $data['stats'] : []; | |
| 252 | + | |
| 253 | + if ( isset( $stats['aggressive'] ) ) { | |
| 254 | + update_post_meta( $id, '_imagify_optimization_level', (int) $stats['aggressive'] ); | |
| 255 | + } | |
| 256 | + } | |
| 134 | 257 | } |
| 135 | 258 | } |
| 136 | - | |
| 137 | - if ( version_compare( $current_version, '1.4.2', '<' ) ) { | |
| 138 | - // Delete all transients used for async optimization | |
| 139 | - global $wpdb; | |
| 140 | - $wpdb->query( 'DELETE from ' . $wpdb->options . ' WHERE option_name LIKE "_transient_imagify-async-in-progress-%"' ); | |
| 259 | + | |
| 260 | + // 1.4.5 | |
| 261 | + if ( version_compare( $site_version, '1.4.5' ) < 0 ) { | |
| 262 | + // Delete all transients used for async optimization. | |
| 263 | + $wpdb->query( $wpdb->prepare( "DELETE from $wpdb->options WHERE option_name LIKE %s", Imagify_DB::esc_like( '_transient_imagify-async-in-progress-' ) . '%' ) ); | |
| 141 | 264 | } |
| 142 | -} | |
| 265 | + | |
| 266 | + // 1.7 | |
| 267 | + if ( version_compare( $site_version, '1.7' ) < 0 ) { | |
| 268 | + // Migrate data. | |
| 269 | + Imagify_Cron_Library_Size::get_instance()->do_event(); | |
| 270 | + | |
| 271 | + if ( ! imagify_is_active_for_network() ) { | |
| 272 | + // Make sure the settings are autoloaded. | |
| 273 | + $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET `autoload` = 'yes' WHERE `autoload` != 'yes' AND option_name = %s", $options->get_option_name() ) ); | |
| 274 | + } | |
| 275 | + | |
| 276 | + // Rename the option that stores the NGG table version. Since the table is also updated in 1.7, let's simply delete the option. | |
| 277 | + delete_option( $wpdb->prefix . 'ngg_imagify_data_db_version' ); | |
| 278 | + } | |
| 279 | + | |
| 280 | + // 1.8.1 | |
| 281 | + if ( version_compare( $site_version, '1.8.1' ) < 0 ) { | |
| 282 | + // Custom folders: replace `{{ABSPATH}}/` by `{{ROOT}}/`. | |
| 283 | + $filesystem = imagify_get_filesystem(); | |
| 284 | + $replacement = '{{ROOT}}/'; | |
| 285 | + | |
| 286 | + if ( $filesystem->has_wp_its_own_directory() ) { | |
| 287 | + $replacement .= preg_replace( '@^' . preg_quote( $filesystem->get_site_root(), '@' ) . '@', '', $filesystem->get_abspath() ); | |
| 288 | + } | |
| 289 | + | |
| 290 | + $replacement = Imagify_DB::esc_like( $replacement ); | |
| 291 | + | |
| 292 | + $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->base_prefix}imagify_files SET path = REPLACE( path, '{{ABSPATH}}/', %s ) WHERE path LIKE %s", $replacement, '{{ABSPATH}}/%' ) ); | |
| 293 | + $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->base_prefix}imagify_folders SET path = REPLACE( path, '{{ABSPATH}}/', %s ) WHERE path LIKE %s", $replacement, '{{ABSPATH}}/%' ) ); | |
| 294 | + } | |
| 295 | + | |
| 296 | + // 1.8.2 | |
| 297 | + if ( version_compare( $site_version, '1.8.2' ) < 0 ) { | |
| 298 | + $options->set( 'partner_links', 1 ); | |
| 299 | + } | |
| 300 | + | |
| 301 | + // 1.9.6 | |
| 302 | + if ( version_compare( $site_version, '1.9.6' ) < 0 ) { | |
| 303 | + \Imagify\Stats\OptimizedMediaWithoutNextGen::get_instance()->clear_cache(); | |
| 304 | + } | |
| 305 | + | |
| 306 | + // 1.9.11 | |
| 307 | + if ( version_compare( $site_version, '1.9.11' ) < 0 ) { | |
| 308 | + imagify_secure_custom_directories(); | |
| 309 | + } | |
| 310 | + | |
| 311 | + if ( version_compare( $site_version, '2.0' ) < 0 ) { | |
| 312 | + $options->set( 'optimization_level', 2 ); | |
| 313 | + } | |
| 314 | + | |
| 315 | + if ( version_compare( $site_version, '2.2' ) < 0 ) { | |
| 316 | + $options->set( 'display_nextgen', $options->get( 'display_webp', 0 ) ); | |
| 317 | + $options->set( 'display_nextgen_method', $options->get( 'display_webp_method' ) ); | |
| 318 | + } | |
| 319 | + | |
| 320 | + if ( version_compare( $site_version, '2.2.2', '<' ) ) { | |
| 321 | + if ( $options->get( 'convert_to_avif' ) ) { | |
| 322 | + $options->set( 'optimization_format', 'avif' ); | |
| 323 | + } else { | |
| 324 | + $options->set( 'optimization_format', 'webp' ); | |
| 325 | + } | |
| 326 | + } | |
| 327 | +} | |
| 328 | +add_action( 'imagify_upgrade', '_imagify_new_upgrade', 10, 2 ); | |
| 329 | + | |
| 330 | +/** | |
| 331 | + * Scan imagify directories and add `index.php` files where missing. | |
| 332 | + * | |
| 333 | + * @since 1.9.11 | |
| 334 | + * | |
| 335 | + * @return void | |
| 336 | + */ | |
| 337 | +function imagify_secure_custom_directories() { | |
| 338 | + $filesystem = imagify_get_filesystem(); | |
| 339 | + | |
| 340 | + Imagify_Custom_Folders::add_indexes(); | |
| 341 | + | |
| 342 | + $conf_dir = $filesystem->get_site_root() . 'conf'; | |
| 343 | + Imagify_Custom_Folders::add_indexes( $conf_dir ); | |
| 344 | + | |
| 345 | + $backup_dir = get_imagify_backup_dir_path(); | |
| 346 | + Imagify_Custom_Folders::add_indexes( $backup_dir ); | |
| 347 | +} | |
| 348 | +add_action( 'imagify_activation', 'imagify_secure_custom_directories' ); | |