PluginProbe
Autoptimize / 2.7.6
Autoptimize v2.7.6
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 2.7.6, at classes/autoptimizeVersionUpdatesHandler.php

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