| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 3 |
|
| 4 |
add_action( 'admin_init', '_imagify_upgrader' ); |
| 5 |
/** |
| 6 |
* Tell WP what to do when admin is loaded aka upgrader. |
| 7 |
* |
| 8 |
* @since 1.0 |
| 9 |
*/ |
| 10 |
function _imagify_upgrader() { |
| 11 |
// Back-compat' with previous version of the upgrader. |
| 12 |
imagify_upgrader_upgrade(); |
| 13 |
|
| 14 |
// Version stored on the network. |
| 15 |
$network_version = Imagify_Options::get_instance()->get( 'version' ); |
| 16 |
// Version stored at the site level. |
| 17 |
$site_version = Imagify_Data::get_instance()->get( 'version' ); |
| 18 |
|
| 19 |
if ( ! $network_version ) { |
| 20 |
// First install (network). |
| 21 |
|
| 22 |
/** |
| 23 |
* Triggered on Imagify first install (network). |
| 24 |
* |
| 25 |
* @since 1.7 |
| 26 |
* @author Grégory Viguier |
| 27 |
*/ |
| 28 |
do_action( 'imagify_first_network_install' ); |
| 29 |
} elseif ( IMAGIFY_VERSION !== $network_version ) { |
| 30 |
// Already installed but got updated (network). |
| 31 |
|
| 32 |
/** |
| 33 |
* Triggered on Imagify upgrade (network). |
| 34 |
* |
| 35 |
* @since 1.7 |
| 36 |
* @author Grégory Viguier |
| 37 |
* |
| 38 |
* @param string $network_version Previous version stored on the network. |
| 39 |
* @param string $site_version Previous version stored on site level. |
| 40 |
*/ |
| 41 |
do_action( 'imagify_network_upgrade', $network_version, $site_version ); |
| 42 |
} |
| 43 |
|
| 44 |
// If any upgrade has been done, we flush and update version. |
| 45 |
if ( did_action( 'imagify_first_network_install' ) || did_action( 'imagify_network_upgrade' ) ) { |
| 46 |
Imagify_Options::get_instance()->set( 'version', IMAGIFY_VERSION ); |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! $site_version ) { |
| 50 |
// First install (site level). |
| 51 |
|
| 52 |
/** |
| 53 |
* Triggered on Imagify first install (site level). |
| 54 |
* |
| 55 |
* @since 1.0 |
| 56 |
*/ |
| 57 |
do_action( 'imagify_first_install' ); |
| 58 |
} elseif ( IMAGIFY_VERSION !== $site_version ) { |
| 59 |
// Already installed but got updated (site level). |
| 60 |
|
| 61 |
/** |
| 62 |
* Triggered on Imagify upgrade (site level). |
| 63 |
* |
| 64 |
* @since 1.0 |
| 65 |
* @since 1.7 $network_version replaces the "new version" (which can easily be grabbed with the constant). |
| 66 |
* |
| 67 |
* @param string $network_version Previous version stored on the network. |
| 68 |
* @param string $site_version Previous version stored on site level. |
| 69 |
*/ |
| 70 |
do_action( 'imagify_upgrade', $network_version, $site_version ); |
| 71 |
} |
| 72 |
|
| 73 |
// If any upgrade has been done, we flush and update version. |
| 74 |
if ( did_action( 'imagify_first_install' ) || did_action( 'imagify_upgrade' ) ) { |
| 75 |
Imagify_Data::get_instance()->set( 'version', IMAGIFY_VERSION ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 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; |
| 88 |
|
| 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; |
| 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 ); |
| 161 |
} |
| 162 |
|
| 163 |
add_action( 'imagify_first_network_install', '_imagify_first_install' ); |
| 164 |
/** |
| 165 |
* Keeps this function up to date at each version. |
| 166 |
* |
| 167 |
* @since 1.0 |
| 168 |
*/ |
| 169 |
function _imagify_first_install() { |
| 170 |
// Set a transient to know when we will have to display a notice to ask the user to rate the plugin. |
| 171 |
set_site_transient( 'imagify_seen_rating_notice', true, DAY_IN_SECONDS * 3 ); |
| 172 |
} |
| 173 |
|
| 174 |
add_action( 'imagify_upgrade', '_imagify_new_upgrade', 10, 2 ); |
| 175 |
/** |
| 176 |
* What to do when Imagify is updated, depending on versions. |
| 177 |
* |
| 178 |
* @since 1.0 |
| 179 |
* @since 1.7 $network_version replaces the "new version" (which can easily be grabbed with the constant). |
| 180 |
* |
| 181 |
* @param string $network_version Previous version stored on the network. |
| 182 |
* @param string $site_version Previous version stored on site level. |
| 183 |
*/ |
| 184 |
function _imagify_new_upgrade( $network_version, $site_version ) { |
| 185 |
global $wpdb; |
| 186 |
|
| 187 |
$options = Imagify_Options::get_instance(); |
| 188 |
|
| 189 |
// 1.2 |
| 190 |
if ( version_compare( $site_version, '1.2' ) < 0 ) { |
| 191 |
// 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 |
) ); |
| 204 |
|
| 205 |
if ( $query->posts ) { |
| 206 |
foreach ( (array) $query->posts as $id ) { |
| 207 |
$data = get_post_meta( $id, '_imagify_data', true ); |
| 208 |
$error = ! empty( $data['sizes']['full']['error'] ) ? $data['sizes']['full']['error'] : ''; |
| 209 |
|
| 210 |
if ( false !== strpos( $error, 'This image is already compressed' ) ) { |
| 211 |
update_post_meta( $id, '_imagify_status', 'already_optimized' ); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
// Auto-activate the Admin Bar option. |
| 217 |
$options->set( 'admin_bar_menu', 1 ); |
| 218 |
} |
| 219 |
|
| 220 |
// 1.3.2 |
| 221 |
if ( version_compare( $site_version, '1.3.2' ) < 0 ) { |
| 222 |
// 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 |
) ); |
| 244 |
|
| 245 |
if ( $query->posts ) { |
| 246 |
foreach ( (array) $query->posts as $id ) { |
| 247 |
$data = get_post_meta( $id, '_imagify_data', true ); |
| 248 |
$stats = isset( $data['stats'] ) ? $data['stats'] : []; |
| 249 |
|
| 250 |
if ( isset( $stats['aggressive'] ) ) { |
| 251 |
update_post_meta( $id, '_imagify_optimization_level', (int) $stats['aggressive'] ); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
// 1.4.5 |
| 258 |
if ( version_compare( $site_version, '1.4.5' ) < 0 ) { |
| 259 |
// Delete all transients used for async optimization. |
| 260 |
$wpdb->query( $wpdb->prepare( "DELETE from $wpdb->options WHERE option_name LIKE %s", Imagify_DB::esc_like( '_transient_imagify-async-in-progress-' ) . '%' ) ); |
| 261 |
} |
| 262 |
|
| 263 |
// 1.7 |
| 264 |
if ( version_compare( $site_version, '1.7' ) < 0 ) { |
| 265 |
// Migrate data. |
| 266 |
Imagify_Cron_Library_Size::get_instance()->do_event(); |
| 267 |
|
| 268 |
if ( ! imagify_is_active_for_network() ) { |
| 269 |
// Make sure the settings are autoloaded. |
| 270 |
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET `autoload` = 'yes' WHERE `autoload` != 'yes' AND option_name = %s", $options->get_option_name() ) ); |
| 271 |
} |
| 272 |
|
| 273 |
// Rename the option that stores the NGG table version. Since the table is also updated in 1.7, let's simply delete the option. |
| 274 |
delete_option( $wpdb->prefix . 'ngg_imagify_data_db_version' ); |
| 275 |
} |
| 276 |
|
| 277 |
// 1.8.1 |
| 278 |
if ( version_compare( $site_version, '1.8.1' ) < 0 ) { |
| 279 |
// Custom folders: replace `{{ABSPATH}}/` by `{{ROOT}}/`. |
| 280 |
$filesystem = imagify_get_filesystem(); |
| 281 |
$replacement = '{{ROOT}}/'; |
| 282 |
|
| 283 |
if ( $filesystem->has_wp_its_own_directory() ) { |
| 284 |
$replacement .= preg_replace( '@^' . preg_quote( $filesystem->get_site_root(), '@' ) . '@', '', $filesystem->get_abspath() ); |
| 285 |
} |
| 286 |
|
| 287 |
$replacement = Imagify_DB::esc_like( $replacement ); |
| 288 |
|
| 289 |
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->base_prefix}imagify_files SET path = REPLACE( path, '{{ABSPATH}}/', %s ) WHERE path LIKE %s", $replacement, '{{ABSPATH}}/%' ) ); |
| 290 |
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->base_prefix}imagify_folders SET path = REPLACE( path, '{{ABSPATH}}/', %s ) WHERE path LIKE %s", $replacement, '{{ABSPATH}}/%' ) ); |
| 291 |
} |
| 292 |
|
| 293 |
// 1.8.2 |
| 294 |
if ( version_compare( $site_version, '1.8.2' ) < 0 ) { |
| 295 |
Imagify_Options::get_instance()->set( 'partner_links', 1 ); |
| 296 |
} |
| 297 |
|
| 298 |
// 1.9.6 |
| 299 |
if ( version_compare( $site_version, '1.9.6' ) < 0 ) { |
| 300 |
\Imagify\Stats\OptimizedMediaWithoutWebp::get_instance()->clear_cache(); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
add_action( 'upgrader_process_complete', 'imagify_maybe_reset_opcache', 20, 2 ); |
| 305 |
/** |
| 306 |
* Maybe reset opcache after Imagify update. |
| 307 |
* |
| 308 |
* @since 1.7.1.2 |
| 309 |
* @author Grégory Viguier |
| 310 |
* |
| 311 |
* @param object $wp_upgrader Plugin_Upgrader instance. |
| 312 |
* @param array $hook_extra { |
| 313 |
* Array of bulk item update data. |
| 314 |
* |
| 315 |
* @type string $action Type of action. Default 'update'. |
| 316 |
* @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'. |
| 317 |
* @type bool $bulk Whether the update process is a bulk update. Default true. |
| 318 |
* @type array $plugins Array of the basename paths of the plugins' main files. |
| 319 |
* } |
| 320 |
*/ |
| 321 |
function imagify_maybe_reset_opcache( $wp_upgrader, $hook_extra ) { |
| 322 |
static $imagify_path; |
| 323 |
|
| 324 |
if ( ! isset( $hook_extra['action'], $hook_extra['type'], $hook_extra['plugins'] ) ) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
if ( 'update' !== $hook_extra['action'] || 'plugin' !== $hook_extra['type'] || ! is_array( $hook_extra['plugins'] ) ) { |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
$plugins = array_flip( $hook_extra['plugins'] ); |
| 333 |
|
| 334 |
if ( ! isset( $imagify_path ) ) { |
| 335 |
$imagify_path = plugin_basename( IMAGIFY_FILE ); |
| 336 |
} |
| 337 |
|
| 338 |
if ( ! isset( $plugins[ $imagify_path ] ) ) { |
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
imagify_reset_opcache(); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Reset PHP opcache. |
| 347 |
* |
| 348 |
* @since 1.8.1 |
| 349 |
* @author Grégory Viguier |
| 350 |
*/ |
| 351 |
function imagify_reset_opcache() { |
| 352 |
static $can_reset; |
| 353 |
|
| 354 |
if ( ! isset( $can_reset ) ) { |
| 355 |
if ( ! function_exists( 'opcache_reset' ) ) { |
| 356 |
$can_reset = false; |
| 357 |
return; |
| 358 |
} |
| 359 |
|
| 360 |
$restrict_api = ini_get( 'opcache.restrict_api' ); |
| 361 |
|
| 362 |
if ( $restrict_api && strpos( __FILE__, $restrict_api ) !== 0 ) { |
| 363 |
$can_reset = false; |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
$can_reset = true; |
| 368 |
} |
| 369 |
|
| 370 |
if ( ! $can_reset ) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
opcache_reset(); |
| 375 |
} |
| 376 |
|