PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
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 2.3.0, at inc/admin/upgrader.php

349 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || exit;
3
4 /**
5 * Tell WP what to do when admin is loaded aka upgrader.
6 *
7 * @since 1.0
8 */
9 function _imagify_upgrader() {
10 // Back-compat' with previous version of the upgrader.
11 imagify_upgrader_upgrade();
12
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 */
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 );
70 }
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 );
75 }
76 }
77 add_action( 'admin_init', '_imagify_upgrader' );
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 /**
164 * Keeps this function up to date at each version.
165 *
166 * @since 1.0
167 */
168 function _imagify_first_install() {
169 // Set a transient to know when we will have to display a notice to ask the user to rate the plugin.
170 set_site_transient( 'imagify_seen_rating_notice', true, DAY_IN_SECONDS * 3 );
171 }
172 add_action( 'imagify_first_network_install', '_imagify_first_install' );
173
174 /**
175 * What to do when Imagify is updated, depending on versions.
176 *
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.
182 */
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'.
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 );
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 }
214 }
215 }
216
217 // Auto-activate the Admin Bar option.
218 $options->set( 'admin_bar_menu', 1 );
219 }
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'.
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 );
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 }
257 }
258 }
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-' ) . '%' ) );
264 }
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' );
349