PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.7
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / admin / upgrader.php

upgrader.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.7, at inc/admin/upgrader.php

273 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin&#8217; 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 // First install (network).
20 if ( ! $network_version ) {
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 }
29 // Already installed but got updated (network).
30 elseif ( IMAGIFY_VERSION !== $network_version ) {
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 // First install (site level).
49 if ( ! $site_version ) {
50 /**
51 * Triggered on Imagify first install (site level).
52 *
53 * @since 1.0
54 */
55 do_action( 'imagify_first_install' );
56 }
57 // Already installed but got updated (site level).
58 elseif ( IMAGIFY_VERSION !== $site_version ) {
59 /**
60 * Triggered on Imagify upgrade (site level).
61 *
62 * @since 1.0
63 * @since 1.7 $network_version replaces the "new version" (which can easily be grabbed with the constant).
64 *
65 * @param string $network_version Previous version stored on the network.
66 * @param string $site_version Previous version stored on site level.
67 */
68 do_action( 'imagify_upgrade', $network_version, $site_version );
69 }
70
71 // If any upgrade has been done, we flush and update version.
72 if ( did_action( 'imagify_first_install' ) || did_action( 'imagify_upgrade' ) ) {
73 Imagify_Data::get_instance()->set( 'version', IMAGIFY_VERSION );
74 }
75 }
76
77 /**
78 * Upgrade the upgrader:
79 * 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".
80 *
81 * @since 1.7
82 * @author Grégory Viguier
83 */
84 function imagify_upgrader_upgrade() {
85 global $wpdb;
86
87 // Version stored on the network.
88 $network_version = Imagify_Options::get_instance()->get( 'version' );
89
90 if ( ! $network_version ) {
91 // Really first install.
92 return;
93 }
94
95 // Version stored at the site level.
96 $site_version = Imagify_Data::get_instance()->get( 'version' );
97
98 if ( $site_version ) {
99 // This site's upgrader is already upgraded.
100 return;
101 }
102
103 if ( ! is_multisite() ) {
104 // Not a multisite, so both versions must have the same value.
105 Imagify_Data::get_instance()->set( 'version', $network_version );
106 return;
107 }
108
109 $sites = get_site_option( 'imagify_old_version' );
110
111 if ( IMAGIFY_VERSION !== $network_version && ! $sites ) {
112 // The network is not up-to-date yet: store the site IDs that must be updated.
113 $network_id = function_exists( 'get_current_network_id' ) ? get_current_network_id() : $wpdb->siteid;
114 $sites = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d AND archived = 0 AND deleted = 0", $network_id ) );
115 $sites = array_map( 'absint', $sites );
116 $sites = array_filter( $sites );
117
118 if ( ! $sites ) {
119 // Uh?
120 return;
121 }
122
123 // We store the old network version and the site Ids: those sites will need to be upgraded from this version.
124 $sites['version'] = $network_version;
125
126 add_site_option( 'imagify_old_version', $sites );
127 }
128
129 if ( empty( $sites['version'] ) ) {
130 // WTF.
131 delete_site_option( 'imagify_old_version' );
132 return;
133 }
134
135 $network_version = $sites['version'];
136 unset( $sites['version'] );
137
138 $sites = array_flip( $sites );
139 $site_id = get_current_blog_id();
140
141 if ( ! isset( $sites[ $site_id ] ) ) {
142 // This site is already upgraded.
143 return;
144 }
145
146 unset( $sites[ $site_id ] );
147
148 if ( ! $sites ) {
149 // We're done, all the sites have been upgraded.
150 delete_site_option( 'imagify_old_version' );
151 } else {
152 // Some sites still need to be upgraded.
153 $sites = array_flip( $sites );
154 $sites['version'] = $network_version;
155 update_site_option( 'imagify_old_version', $sites );
156 }
157
158 Imagify_Data::get_instance()->set( 'version', $network_version );
159 }
160
161 add_action( 'imagify_first_network_install', '_imagify_first_install' );
162 /**
163 * Keeps this function up to date at each version.
164 *
165 * @since 1.0
166 */
167 function _imagify_first_install() {
168 // Set a transient to know when we will have to display a notice to ask the user to rate the plugin.
169 set_site_transient( 'imagify_seen_rating_notice', true, DAY_IN_SECONDS * 3 );
170 }
171
172 add_action( 'imagify_upgrade', '_imagify_new_upgrade', 10, 2 );
173 /**
174 * What to do when Imagify is updated, depending on versions.
175 *
176 * @since 1.0
177 * @since 1.7 $network_version replaces the "new version" (which can easily be grabbed with the constant).
178 *
179 * @param string $network_version Previous version stored on the network.
180 * @param string $site_version Previous version stored on site level.
181 */
182 function _imagify_new_upgrade( $network_version, $site_version ) {
183 global $wpdb;
184
185 $options = Imagify_Options::get_instance();
186
187 // 1.2
188 if ( version_compare( $site_version, '1.2' ) < 0 ) {
189 // Update all already optimized images status from 'error' to 'already_optimized'.
190 $query = new WP_Query( array(
191 'is_imagify' => true,
192 'post_type' => 'attachment',
193 'post_status' => imagify_get_post_statuses(),
194 'post_mime_type' => imagify_get_mime_types(),
195 'meta_key' => '_imagify_status',
196 'meta_value' => 'error',
197 'posts_per_page' => -1,
198 'update_post_term_cache' => false,
199 'no_found_rows' => true,
200 'fields' => 'ids',
201 ) );
202
203 if ( $query->posts ) {
204 foreach ( (array) $query->posts as $id ) {
205 $attachment_error = get_imagify_attachment( 'wp', $id, 'imagify_upgrade' )->get_optimized_error();
206
207 if ( false !== strpos( $attachment_error, 'This image is already compressed' ) ) {
208 update_post_meta( $id, '_imagify_status', 'already_optimized' );
209 }
210 }
211 }
212
213 // Auto-activate the Admin Bar option.
214 $options->set( 'admin_bar_menu', 1 );
215 }
216
217 // 1.3.2
218 if ( version_compare( $site_version, '1.3.2' ) < 0 ) {
219 // Update all already optimized images status from 'error' to 'already_optimized'.
220 $query = new WP_Query( array(
221 'is_imagify' => true,
222 'post_type' => 'attachment',
223 'post_status' => imagify_get_post_statuses(),
224 'post_mime_type' => imagify_get_mime_types(),
225 'meta_query' => array(
226 'relation' => 'AND',
227 array(
228 'key' => '_imagify_data',
229 'compare' => 'EXISTS',
230 ),
231 array(
232 'key' => '_imagify_optimization_level',
233 'compare' => 'NOT EXISTS',
234 ),
235 ),
236 'posts_per_page' => -1,
237 'update_post_term_cache' => false,
238 'no_found_rows' => true,
239 'fields' => 'ids',
240 ) );
241
242 if ( $query->posts ) {
243 foreach ( (array) $query->posts as $id ) {
244 $attachment_stats = get_imagify_attachment( 'wp', $id, 'imagify_upgrade' )->get_stats_data();
245
246 if ( isset( $attachment_stats['aggressive'] ) ) {
247 update_post_meta( $id, '_imagify_optimization_level', (int) $attachment_stats['aggressive'] );
248 }
249 }
250 }
251 }
252
253 // 1.4.5
254 if ( version_compare( $site_version, '1.4.5' ) < 0 ) {
255 // Delete all transients used for async optimization.
256 $wpdb->query( 'DELETE from ' . $wpdb->options . ' WHERE option_name LIKE "_transient_imagify-async-in-progress-%"' );
257 }
258
259 // 1.7
260 if ( version_compare( $site_version, '1.7' ) < 0 ) {
261 // Migrate data.
262 _do_imagify_update_library_size_calculations();
263
264 if ( ! imagify_is_active_for_network() ) {
265 // Make sure the settings are autoloaded.
266 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET `autoload` = 'yes' WHERE `autoload` != 'yes' AND option_name = %s", $options->get_option_name() ) );
267 }
268
269 // Rename the option that stores the NGG table version. Since the table is also updated in 1.7, let's simply delete the option.
270 delete_option( $wpdb->prefix . 'ngg_imagify_data_db_version' );
271 }
272 }
273