PluginProbe
Autoptimize / 3.0.2
Autoptimize v3.0.2
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 2.7.8 All 107 releases
autoptimize / classes / autoptimizeVersionUpdatesHandler.php

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

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