PluginProbe
Autoptimize / trunk
Autoptimize vtrunk
3.1.16 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 All 108 releases
autoptimize / classes / autoptimizeVersionUpdatesHandler.php

autoptimizeVersionUpdatesHandler.php in Autoptimize trunk, at classes/autoptimizeVersionUpdatesHandler.php

301 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles version updates and should only be instantiated in autoptimize.php if/when needed.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 class autoptimizeVersionUpdatesHandler
11 {
12 /**
13 * The current major version string.
14 *
15 * @var string
16 */
17 protected $current_major_version = null;
18
19 public function __construct( $current_version )
20 {
21 $this->current_major_version = substr( $current_version, 0, 3 );
22 }
23
24 /**
25 * Runs all needed upgrade procedures (depending on the
26 * current major version specified during class instantiation)
27 */
28 public function run_needed_major_upgrades()
29 {
30 $major_update = false;
31 switch ( $this->current_major_version ) {
32 case '1.6':
33 $this->upgrade_from_1_6();
34 $major_update = true;
35 // No break, intentionally, so all upgrades are ran during a single request...
36 case '1.7':
37 $this->upgrade_from_1_7();
38 $major_update = true;
39 // No break, intentionally, so all upgrades are ran during a single request...
40 case '1.9':
41 $this->upgrade_from_1_9();
42 $major_update = true;
43 // No break, intentionally, so all upgrades are ran during a single request...
44 case '2.2':
45 $this->upgrade_from_2_2();
46 $major_update = true;
47 // No break, intentionally, so all upgrades are ran during a single request...
48 case '2.4':
49 if ( autoptimizeOptionWrapper::get_option( 'autoptimize_version', 'none' ) == '2.4.2' ) {
50 $this->upgrade_from_2_4_2();
51 }
52 $this->upgrade_from_2_4();
53 $major_update = false;
54 // No break, intentionally, so all upgrades are ran during a single request...
55 case '2.7':
56 $this->upgrade_from_2_7();
57 $major_update = true;
58 // No break, intentionally, so all upgrades are ran during a single request...
59 case '2.8':
60 // nothing.
61 case '2.9':
62 if ( version_compare( autoptimizeOptionWrapper::get_option( 'autoptimize_version', 'none' ), '2.9.999', 'lt' ) ) {
63 $this->upgrade_from_2_9_before_compatibility();
64 }
65 $major_update = false;
66 // No break, intentionally, so all upgrades are ran during a single request...
67 case '3.0':
68 // nothing.
69 case '3.1':
70 $this->upgrade_from_3_1();
71 $major_update = false;
72 }
73
74 if ( true === $major_update ) {
75 $this->on_major_version_update();
76 }
77 }
78
79 /**
80 * Checks specified version against the one stored in the database under `autoptimize_version` and performs
81 * any major upgrade routines if needed.
82 * Updates the database version to the specified $target if it's different to the one currently stored there.
83 *
84 * @param string $target Target version to check against (ie., the currently running one).
85 */
86 public static function check_installed_and_update( $target )
87 {
88 $db_version = autoptimizeOptionWrapper::get_option( 'autoptimize_version', 'none' );
89 if ( $db_version !== $target ) {
90 if ( 'none' === $db_version ) {
91 add_action( 'admin_notices', 'autoptimizeMain::notice_installed' );
92 } else {
93 $updater = new self( $db_version );
94 $updater->run_needed_major_upgrades();
95 }
96
97 // Versions differed, upgrades happened if needed, store the new version.
98 autoptimizeOptionWrapper::update_option( 'autoptimize_version', $target );
99 }
100 }
101
102 /**
103 * Called after any major version update (and it's accompanying upgrade procedure)
104 * has happened. Clears cache and sets an admin notice.
105 */
106 protected function on_major_version_update()
107 {
108 // The transients guard here prevents stale object caches from busting the cache on every request.
109 if ( false == get_transient( 'autoptimize_stale_option_buster' ) ) {
110 set_transient( 'autoptimize_stale_option_buster', 'Mamsie & Liessie zehhe: ZWIJH!', HOUR_IN_SECONDS );
111 autoptimizeCache::clearall();
112 add_action( 'admin_notices', 'autoptimizeMain::notice_updated' );
113 }
114 }
115
116 /**
117 * From back in the days when I did not yet consider multisite.
118 */
119 private function upgrade_from_1_6()
120 {
121 // If user was on version 1.6.x, force advanced options to be shown by default.
122 autoptimizeOptionWrapper::update_option( 'autoptimize_show_adv', '1' );
123
124 // And remove old options.
125 $to_delete_options = array(
126 'autoptimize_cdn_css',
127 'autoptimize_cdn_css_url',
128 'autoptimize_cdn_js',
129 'autoptimize_cdn_js_url',
130 'autoptimize_cdn_img',
131 'autoptimize_cdn_img_url',
132 'autoptimize_css_yui',
133 );
134 foreach ( $to_delete_options as $del_opt ) {
135 delete_option( $del_opt );
136 }
137 }
138
139 /**
140 * Forces WP 3.8 dashicons in CSS exclude options when upgrading from 1.7 to 1.8
141 *
142 * @global $wpdb
143 */
144 private function upgrade_from_1_7()
145 {
146 if ( ! is_multisite() ) {
147 $css_exclude = autoptimizeOptionWrapper::get_option( 'autoptimize_css_exclude' );
148 if ( empty( $css_exclude ) ) {
149 $css_exclude = 'admin-bar.min.css, dashicons.min.css';
150 } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) {
151 $css_exclude .= ', dashicons.min.css';
152 }
153 autoptimizeOptionWrapper::update_option( 'autoptimize_css_exclude', $css_exclude );
154 } else {
155 global $wpdb;
156 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
157 $original_blog_id = get_current_blog_id();
158 foreach ( $blog_ids as $blog_id ) {
159 switch_to_blog( $blog_id );
160 $css_exclude = autoptimizeOptionWrapper::get_option( 'autoptimize_css_exclude' );
161 if ( empty( $css_exclude ) ) {
162 $css_exclude = 'admin-bar.min.css, dashicons.min.css';
163 } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) {
164 $css_exclude .= ', dashicons.min.css';
165 }
166 autoptimizeOptionWrapper::update_option( 'autoptimize_css_exclude', $css_exclude );
167 }
168 switch_to_blog( $original_blog_id );
169 }
170 }
171
172 /**
173 * 2.0 will not aggregate inline CSS/JS by default, but we want users
174 * upgrading from 1.9 to keep their inline code aggregated by default.
175 *
176 * @global $wpdb
177 */
178 private function upgrade_from_1_9()
179 {
180 if ( ! is_multisite() ) {
181 autoptimizeOptionWrapper::update_option( 'autoptimize_css_include_inline', 'on' );
182 autoptimizeOptionWrapper::update_option( 'autoptimize_js_include_inline', 'on' );
183 } else {
184 global $wpdb;
185 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
186 $original_blog_id = get_current_blog_id();
187 foreach ( $blog_ids as $blog_id ) {
188 switch_to_blog( $blog_id );
189 autoptimizeOptionWrapper::update_option( 'autoptimize_css_include_inline', 'on' );
190 autoptimizeOptionWrapper::update_option( 'autoptimize_js_include_inline', 'on' );
191 }
192 switch_to_blog( $original_blog_id );
193 }
194 }
195
196 /**
197 * 2.3 has no "remove google fonts" in main screen, moved to "extra"
198 *
199 * @global $wpdb
200 */
201 private function upgrade_from_2_2()
202 {
203 if ( ! is_multisite() ) {
204 $this->do_2_2_settings_update();
205 } else {
206 global $wpdb;
207 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
208 $original_blog_id = get_current_blog_id();
209 foreach ( $blog_ids as $blog_id ) {
210 switch_to_blog( $blog_id );
211 $this->do_2_2_settings_update();
212 }
213 switch_to_blog( $original_blog_id );
214 }
215 }
216
217 /**
218 * Helper for 2.2 autoptimize_extra_settings upgrade to avoid duplicate code
219 */
220 private function do_2_2_settings_update()
221 {
222 $nogooglefont = autoptimizeOptionWrapper::get_option( 'autoptimize_css_nogooglefont', '' );
223 $ao_extrasetting = autoptimizeOptionWrapper::get_option( 'autoptimize_extra_settings', '' );
224 if ( ( $nogooglefont ) && ( empty( $ao_extrasetting ) ) ) {
225 autoptimizeOptionWrapper::update_option( 'autoptimize_extra_settings', autoptimizeConfig::get_ao_extra_default_options() );
226 }
227 delete_option( 'autoptimize_css_nogooglefont' );
228 }
229
230 /**
231 * 2.4.2 introduced too many cronned ao_cachecheckers, make this right
232 */
233 private function upgrade_from_2_4_2() {
234 // below code by Thomas Sjolshagen (http://eighty20results.com/)
235 // as found on https://www.paidmembershipspro.com/deleting-oldextra-cron-events/.
236 $jobs = _get_cron_array();
237
238 // Remove all ao_cachechecker cron jobs (for now).
239 foreach ( $jobs as $when => $job ) {
240 $name = key( $job );
241
242 if ( false !== strpos( $name, 'ao_cachechecker' ) ) {
243 unset( $jobs[ $when ] );
244 }
245 }
246
247 // Save the data.
248 _set_cron_array( $jobs );
249 }
250
251 /**
252 * Migrate imgopt options from autoptimize_extra_settings to autoptimize_imgopt_settings
253 */
254 private function upgrade_from_2_4() {
255 $extra_settings = autoptimizeOptionWrapper::get_option( 'autoptimize_extra_settings', '' );
256 $imgopt_settings = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_settings', '' );
257 if ( empty( $imgopt_settings ) && ! empty( $extra_settings ) ) {
258 $imgopt_settings = autoptimizeConfig::get_ao_imgopt_default_options();
259 if ( array_key_exists( 'autoptimize_extra_checkbox_field_5', $extra_settings ) ) {
260 $imgopt_settings['autoptimize_imgopt_checkbox_field_1'] = $extra_settings['autoptimize_extra_checkbox_field_5'];
261 }
262 if ( array_key_exists( 'autoptimize_extra_select_field_6', $extra_settings ) ) {
263 $imgopt_settings['autoptimize_imgopt_select_field_2'] = $extra_settings['autoptimize_extra_select_field_6'];
264 }
265 autoptimizeOptionWrapper::update_option( 'autoptimize_imgopt_settings', $imgopt_settings );
266 }
267 }
268
269 /**
270 * Remove CCSS request limit option + update jquery exclusion to include WordPress 5.6 jquery.min.js.
271 */
272 private function upgrade_from_2_7() {
273 delete_option( 'autoptimize_ccss_rlimit' );
274 $js_exclusions = get_option( 'autoptimize_js_exclude', '' );
275 if ( strpos( $js_exclusions, 'js/jquery/jquery.js' ) !== false && strpos( $js_exclusions, 'js/jquery/jquery.min.js' ) === false ) {
276 $js_exclusions .= ', js/jquery/jquery.min.js';
277 autoptimizeOptionWrapper::update_option( 'autoptimize_js_exclude', $js_exclusions );
278 }
279 }
280
281 /**
282 * Set an option to indicate the AO installation predates the compatibility logic, this way we
283 * can avoid adding compatibility code that is likely not needed and maybe not wanted as it
284 * can introduce performance regressions.
285 */
286 private function upgrade_from_2_9_before_compatibility() {
287 autoptimizeOptionWrapper::update_option( 'autoptimize_installed_before_compatibility', true );
288 }
289
290 /**
291 * If the 404 handler is active, delete the current PHP-file so it can be re-created to fix the double underscore bug.
292 */
293 private function upgrade_from_3_1() {
294 $_version_from_db = autoptimizeOptionWrapper::get_option( 'autoptimize_version', 'none' );
295 if ( autoptimizeCache::do_fallback() && 'none' !== $_version_from_db && version_compare( $_version_from_db, '3.1.2', 'lt' ) ) {
296 $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . 'autoptimize_404_handler.php';
297 @unlink( $_fallback_php ); // @codingStandardsIgnoreLine
298 }
299 }
300 }
301