| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin’ uh?' ); |
| 3 |
|
| 4 |
/* |
| 5 |
* Tell WP what to do when admin is loaded aka upgrader |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
*/ |
| 9 |
add_action( 'admin_init', '_imagify_upgrader' ); |
| 10 |
function _imagify_upgrader() { |
| 11 |
$current_version = get_imagify_option( 'version' ); |
| 12 |
|
| 13 |
// You can hook the upgrader to trigger any action when Imagify is upgraded |
| 14 |
// first install |
| 15 |
if ( ! $current_version ) { |
| 16 |
do_action( 'imagify_first_install' ); |
| 17 |
} |
| 18 |
// already installed but got updated |
| 19 |
elseif ( IMAGIFY_VERSION != $current_version ) { |
| 20 |
do_action( 'imagify_upgrade', IMAGIFY_VERSION, $current_version ); |
| 21 |
} |
| 22 |
|
| 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; |
| 27 |
|
| 28 |
update_site_option( IMAGIFY_SETTINGS_SLUG, $options ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Keeps this function up to date at each version |
| 34 |
* |
| 35 |
* @since 1.0 |
| 36 |
*/ |
| 37 |
add_action( 'imagify_first_install', '_imagify_first_install' ); |
| 38 |
function _imagify_first_install() { |
| 39 |
// 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 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* What to do when Imagify is updated, depending on versions |
| 60 |
* |
| 61 |
* @since 1.0 |
| 62 |
*/ |
| 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' |
| 67 |
$query = new WP_Query( |
| 68 |
array( |
| 69 |
'post_type' => 'attachment', |
| 70 |
'post_status' => 'inherit', |
| 71 |
'post_mime_type' => 'image', |
| 72 |
'meta_key' => '_imagify_status', |
| 73 |
'meta_value' => 'error', |
| 74 |
'posts_per_page' => -1, |
| 75 |
'update_post_term_cache' => false, |
| 76 |
'no_found_rows' => true, |
| 77 |
'fields' => 'ids' |
| 78 |
) |
| 79 |
); |
| 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' ); |
| 91 |
} |
| 92 |
} |
| 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 ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( version_compare( $current_version, '1.3.2', '<' ) ) { |
| 101 |
// Update all already optimized images status from 'error' to 'already_optimized' |
| 102 |
$query = new WP_Query( |
| 103 |
array( |
| 104 |
'post_type' => 'attachment', |
| 105 |
'post_status' => 'inherit', |
| 106 |
'post_mime_type' => 'image', |
| 107 |
'meta_query' => array( |
| 108 |
'relation' => 'AND', |
| 109 |
array( |
| 110 |
'key' => '_imagify_data', |
| 111 |
'compare' => 'EXISTS' |
| 112 |
), |
| 113 |
array( |
| 114 |
'key' => '_imagify_optimization_level', |
| 115 |
'compare' => 'NOT EXISTS' |
| 116 |
), |
| 117 |
), |
| 118 |
'posts_per_page' => -1, |
| 119 |
'update_post_term_cache' => false, |
| 120 |
'no_found_rows' => true, |
| 121 |
'fields' => 'ids' |
| 122 |
) |
| 123 |
); |
| 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 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |