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