| @@ -1,8 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 2 | +defined( 'ABSPATH' ) || exit; | |
| 3 | 3 | |
| 4 | -add_action( 'admin_init', '_imagify_upgrader' ); | |
| 5 | 4 | /** |
| 6 | 5 | * Tell WP what to do when admin is loaded aka upgrader. |
| 7 | 6 | * |
| 8 | 7 | * @since 1.0 |
| @@ -13,9 +12,9 @@ | ||
| 13 | 12 | |
| 14 | 13 | // Version stored on the network. |
| 15 | 14 | $network_version = Imagify_Options::get_instance()->get( 'version' ); |
| 16 | 15 | // Version stored at the site level. |
| 17 | - $site_version = Imagify_Data::get_instance()->get( 'version' ); | |
| 16 | + $site_version = Imagify_Data::get_instance()->get( 'version' ); | |
| 18 | 17 | |
| 19 | 18 | if ( ! $network_version ) { |
| 20 | 19 | // First install (network). |
| 21 | 20 | |
| @@ -74,8 +73,9 @@ | ||
| 74 | 73 | if ( did_action( 'imagify_first_install' ) || did_action( 'imagify_upgrade' ) ) { |
| 75 | 74 | Imagify_Data::get_instance()->set( 'version', IMAGIFY_VERSION ); |
| 76 | 75 | } |
| 77 | 76 | } |
| 77 | +add_action( 'admin_init', '_imagify_upgrader' ); | |
| 78 | 78 | |
| 79 | 79 | /** |
| 80 | 80 | * Upgrade the upgrader: |
| 81 | 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". |
| @@ -151,9 +151,9 @@ | ||
| 151 | 151 | // We're done, all the sites have been upgraded. |
| 152 | 152 | delete_site_option( 'imagify_old_version' ); |
| 153 | 153 | } else { |
| 154 | 154 | // Some sites still need to be upgraded. |
| 155 | - $sites = array_flip( $sites ); | |
| 155 | + $sites = array_flip( $sites ); | |
| 156 | 156 | $sites['version'] = $network_version; |
| 157 | 157 | update_site_option( 'imagify_old_version', $sites ); |
| 158 | 158 | } |
| 159 | 159 | |
| @@ -159,9 +159,8 @@ | ||
| 159 | 159 | |
| 160 | 160 | Imagify_Data::get_instance()->set( 'version', $network_version ); |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | -add_action( 'imagify_first_network_install', '_imagify_first_install' ); | |
| 164 | 163 | /** |
| 165 | 164 | * Keeps this function up to date at each version. |
| 166 | 165 | * |
| 167 | 166 | * @since 1.0 |
| @@ -169,10 +168,10 @@ | ||
| 169 | 168 | function _imagify_first_install() { |
| 170 | 169 | // Set a transient to know when we will have to display a notice to ask the user to rate the plugin. |
| 171 | 170 | set_site_transient( 'imagify_seen_rating_notice', true, DAY_IN_SECONDS * 3 ); |
| 172 | 171 | } |
| 172 | +add_action( 'imagify_first_network_install', '_imagify_first_install' ); | |
| 173 | 173 | |
| 174 | -add_action( 'imagify_upgrade', '_imagify_new_upgrade', 10, 2 ); | |
| 175 | 174 | /** |
| 176 | 175 | * What to do when Imagify is updated, depending on versions. |
| 177 | 176 | * |
| 178 | 177 | * @since 1.0 |
| @@ -188,20 +187,22 @@ | ||
| 188 | 187 | |
| 189 | 188 | // 1.2 |
| 190 | 189 | if ( version_compare( $site_version, '1.2' ) < 0 ) { |
| 191 | 190 | // Update all already optimized images status from 'error' to 'already_optimized'. |
| 192 | - $query = new WP_Query( array( | |
| 193 | - 'is_imagify' => true, | |
| 194 | - 'post_type' => 'attachment', | |
| 195 | - 'post_status' => imagify_get_post_statuses(), | |
| 196 | - 'post_mime_type' => imagify_get_mime_types(), | |
| 197 | - 'meta_key' => '_imagify_status', | |
| 198 | - 'meta_value' => 'error', | |
| 199 | - 'posts_per_page' => -1, | |
| 200 | - 'update_post_term_cache' => false, | |
| 201 | - 'no_found_rows' => true, | |
| 202 | - 'fields' => 'ids', | |
| 203 | - ) ); | |
| 191 | + $query = new WP_Query( | |
| 192 | + [ | |
| 193 | + 'is_imagify' => true, | |
| 194 | + 'post_type' => 'attachment', | |
| 195 | + 'post_status' => imagify_get_post_statuses(), | |
| 196 | + 'post_mime_type' => imagify_get_mime_types(), | |
| 197 | + 'meta_key' => '_imagify_status', | |
| 198 | + 'meta_value' => 'error', | |
| 199 | + 'posts_per_page' => -1, | |
| 200 | + 'update_post_term_cache' => false, | |
| 201 | + 'no_found_rows' => true, | |
| 202 | + 'fields' => 'ids', | |
| 203 | + ] | |
| 204 | + ); | |
| 204 | 205 | |
| 205 | 206 | if ( $query->posts ) { |
| 206 | 207 | foreach ( (array) $query->posts as $id ) { |
| 207 | 208 | $data = get_post_meta( $id, '_imagify_data', true ); |
| @@ -219,29 +220,31 @@ | ||
| 219 | 220 | |
| 220 | 221 | // 1.3.2 |
| 221 | 222 | if ( version_compare( $site_version, '1.3.2' ) < 0 ) { |
| 222 | 223 | // Update all already optimized images status from 'error' to 'already_optimized'. |
| 223 | - $query = new WP_Query( array( | |
| 224 | - 'is_imagify' => true, | |
| 225 | - 'post_type' => 'attachment', | |
| 226 | - 'post_status' => imagify_get_post_statuses(), | |
| 227 | - 'post_mime_type' => imagify_get_mime_types(), | |
| 228 | - 'meta_query' => array( | |
| 229 | - 'relation' => 'AND', | |
| 230 | - array( | |
| 231 | - 'key' => '_imagify_data', | |
| 232 | - 'compare' => 'EXISTS', | |
| 233 | - ), | |
| 234 | - array( | |
| 235 | - 'key' => '_imagify_optimization_level', | |
| 236 | - 'compare' => 'NOT EXISTS', | |
| 237 | - ), | |
| 238 | - ), | |
| 239 | - 'posts_per_page' => -1, | |
| 240 | - 'update_post_term_cache' => false, | |
| 241 | - 'no_found_rows' => true, | |
| 242 | - 'fields' => 'ids', | |
| 243 | - ) ); | |
| 224 | + $query = new WP_Query( | |
| 225 | + [ | |
| 226 | + 'is_imagify' => true, | |
| 227 | + 'post_type' => 'attachment', | |
| 228 | + 'post_status' => imagify_get_post_statuses(), | |
| 229 | + 'post_mime_type' => imagify_get_mime_types(), | |
| 230 | + 'meta_query' => [ | |
| 231 | + 'relation' => 'AND', | |
| 232 | + [ | |
| 233 | + 'key' => '_imagify_data', | |
| 234 | + 'compare' => 'EXISTS', | |
| 235 | + ], | |
| 236 | + [ | |
| 237 | + 'key' => '_imagify_optimization_level', | |
| 238 | + 'compare' => 'NOT EXISTS', | |
| 239 | + ], | |
| 240 | + ], | |
| 241 | + 'posts_per_page' => -1, | |
| 242 | + 'update_post_term_cache' => false, | |
| 243 | + 'no_found_rows' => true, | |
| 244 | + 'fields' => 'ids', | |
| 245 | + ] | |
| 246 | + ); | |
| 244 | 247 | |
| 245 | 248 | if ( $query->posts ) { |
| 246 | 249 | foreach ( (array) $query->posts as $id ) { |
| 247 | 250 | $data = get_post_meta( $id, '_imagify_data', true ); |
| @@ -291,14 +294,14 @@ | ||
| 291 | 294 | } |
| 292 | 295 | |
| 293 | 296 | // 1.8.2 |
| 294 | 297 | if ( version_compare( $site_version, '1.8.2' ) < 0 ) { |
| 295 | - Imagify_Options::get_instance()->set( 'partner_links', 1 ); | |
| 298 | + $options->set( 'partner_links', 1 ); | |
| 296 | 299 | } |
| 297 | 300 | |
| 298 | 301 | // 1.9.6 |
| 299 | 302 | if ( version_compare( $site_version, '1.9.6' ) < 0 ) { |
| 300 | - \Imagify\Stats\OptimizedMediaWithoutWebp::get_instance()->clear_cache(); | |
| 303 | + \Imagify\Stats\OptimizedMediaWithoutNextGen::get_instance()->clear_cache(); | |
| 301 | 304 | } |
| 302 | 305 | |
| 303 | 306 | // 1.9.11 |
| 304 | 307 | if ( version_compare( $site_version, '1.9.11' ) < 0 ) { |
| @@ -303,95 +306,28 @@ | ||
| 303 | 306 | // 1.9.11 |
| 304 | 307 | if ( version_compare( $site_version, '1.9.11' ) < 0 ) { |
| 305 | 308 | imagify_secure_custom_directories(); |
| 306 | 309 | } |
| 307 | -} | |
| 308 | 310 | |
| 309 | -add_action( 'upgrader_process_complete', 'imagify_maybe_reset_opcache', 20, 2 ); | |
| 310 | -/** | |
| 311 | - * Maybe reset opcache after Imagify update. | |
| 312 | - * | |
| 313 | - * @since 1.7.1.2 | |
| 314 | - * @author Grégory Viguier | |
| 315 | - * | |
| 316 | - * @param object $wp_upgrader Plugin_Upgrader instance. | |
| 317 | - * @param array $hook_extra { | |
| 318 | - * Array of bulk item update data. | |
| 319 | - * | |
| 320 | - * @type string $action Type of action. Default 'update'. | |
| 321 | - * @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'. | |
| 322 | - * @type bool $bulk Whether the update process is a bulk update. Default true. | |
| 323 | - * @type array $plugins Array of the basename paths of the plugins' main files. | |
| 324 | - * } | |
| 325 | - */ | |
| 326 | -function imagify_maybe_reset_opcache( $wp_upgrader, $hook_extra ) { | |
| 327 | - static $imagify_path; | |
| 328 | - | |
| 329 | - if ( ! isset( $hook_extra['action'], $hook_extra['type'], $hook_extra['plugins'] ) ) { | |
| 330 | - return; | |
| 311 | + if ( version_compare( $site_version, '2.0' ) < 0 ) { | |
| 312 | + $options->set( 'optimization_level', 2 ); | |
| 331 | 313 | } |
| 332 | 314 | |
| 333 | - if ( 'update' !== $hook_extra['action'] || 'plugin' !== $hook_extra['type'] || ! is_array( $hook_extra['plugins'] ) ) { | |
| 334 | - return; | |
| 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' ) ); | |
| 335 | 318 | } |
| 336 | 319 | |
| 337 | - $plugins = array_flip( $hook_extra['plugins'] ); | |
| 338 | - | |
| 339 | - if ( ! isset( $imagify_path ) ) { | |
| 340 | - $imagify_path = plugin_basename( IMAGIFY_FILE ); | |
| 341 | - } | |
| 342 | - | |
| 343 | - if ( ! isset( $plugins[ $imagify_path ] ) ) { | |
| 344 | - return; | |
| 345 | - } | |
| 346 | - | |
| 347 | - imagify_reset_opcache(); | |
| 348 | -} | |
| 349 | - | |
| 350 | -/** | |
| 351 | - * Reset PHP opcache. | |
| 352 | - * | |
| 353 | - * @since 1.8.1 | |
| 354 | - * @since 1.9.9 Added $reset_function_cache parameter and return boolean. | |
| 355 | - * @author Grégory Viguier | |
| 356 | - * | |
| 357 | - * @param bool $reset_function_cache Set to true to bypass the cache. | |
| 358 | - * @return bool Return true if the opcode cache was reset (or reset in a previous call), or false if the opcode cache is disabled. | |
| 359 | - */ | |
| 360 | -function imagify_reset_opcache( $reset_function_cache = false ) { | |
| 361 | - static $can_reset; | |
| 362 | - | |
| 363 | - if ( $reset_function_cache || ! isset( $can_reset ) ) { | |
| 364 | - if ( ! function_exists( 'opcache_reset' ) ) { | |
| 365 | - $can_reset = false; | |
| 366 | - return false; | |
| 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' ); | |
| 367 | 325 | } |
| 368 | - | |
| 369 | - $opcache_enabled = filter_var( ini_get( 'opcache.enable' ), FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore PHPCompatibility.IniDirectives.NewIniDirectives.opcache_enableFound | |
| 370 | - | |
| 371 | - if ( ! $opcache_enabled ) { | |
| 372 | - $can_reset = false; | |
| 373 | - return false; | |
| 374 | - } | |
| 375 | - | |
| 376 | - $restrict_api = ini_get( 'opcache.restrict_api' ); // phpcs:ignore PHPCompatibility.IniDirectives.NewIniDirectives.opcache_restrict_apiFound | |
| 377 | - | |
| 378 | - if ( $restrict_api && strpos( __FILE__, $restrict_api ) !== 0 ) { | |
| 379 | - $can_reset = false; | |
| 380 | - return false; | |
| 381 | - } | |
| 382 | - | |
| 383 | - $can_reset = true; | |
| 384 | 326 | } |
| 385 | - | |
| 386 | - if ( ! $can_reset ) { | |
| 387 | - return false; | |
| 388 | - } | |
| 389 | - | |
| 390 | - return opcache_reset(); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.opcache_resetFound | |
| 391 | 327 | } |
| 328 | +add_action( 'imagify_upgrade', '_imagify_new_upgrade', 10, 2 ); | |
| 392 | 329 | |
| 393 | -add_action( 'imagify_activation', 'imagify_secure_custom_directories' ); | |
| 394 | 330 | /** |
| 395 | 331 | * Scan imagify directories and add `index.php` files where missing. |
| 396 | 332 | * |
| 397 | 333 | * @since 1.9.11 |
| @@ -408,4 +344,5 @@ | ||
| 408 | 344 | |
| 409 | 345 | $backup_dir = get_imagify_backup_dir_path(); |
| 410 | 346 | Imagify_Custom_Folders::add_indexes( $backup_dir ); |
| 411 | 347 | } |
| 348 | +add_action( 'imagify_activation', 'imagify_secure_custom_directories' ); | |