| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP_Optimize_Updates class using for run updates in database from version to version. |
| 4 |
*/ |
| 5 |
|
| 6 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 7 |
|
| 8 |
if (!class_exists('WP_Optimize_Updates')) : |
| 9 |
|
| 10 |
class WP_Optimize_Updates { |
| 11 |
|
| 12 |
/** |
| 13 |
* Format: key=<version>, value=array of method names to call |
| 14 |
* Example Usage: |
| 15 |
* private static $db_updates = array( |
| 16 |
* '1.0.1' => array( |
| 17 |
* 'update_101_add_new_column', |
| 18 |
* ), |
| 19 |
* ); |
| 20 |
* |
| 21 |
* @var Mixed |
| 22 |
*/ |
| 23 |
private static $updates = array( |
| 24 |
'3.0.12' => array('delete_old_locks'), |
| 25 |
'3.0.17' => array('disable_cache_directories_viewing'), |
| 26 |
'3.1.0' => array('reset_wpo_plugin_cron_tasks_schedule'), |
| 27 |
'3.1.4' => array('enable_minify_defer'), |
| 28 |
'3.1.5' => array('update_minify_excludes'), |
| 29 |
); |
| 30 |
|
| 31 |
/** |
| 32 |
* See if any database schema updates are needed, and perform them if so. |
| 33 |
* Example Usage: |
| 34 |
* public static function update_101_add_new_column() { |
| 35 |
* $wpdb = $GLOBALS['wpdb']; |
| 36 |
* $wpdb->query('ALTER TABLE tm_tasks ADD task_expiry varchar(300) AFTER id'); |
| 37 |
* } |
| 38 |
*/ |
| 39 |
public static function check_updates() { |
| 40 |
$our_version = WPO_VERSION; |
| 41 |
$db_version = get_option('wpo_update_version'); |
| 42 |
if (!$db_version || version_compare($our_version, $db_version, '>')) { |
| 43 |
foreach (self::$updates as $version => $updates) { |
| 44 |
if (version_compare($version, $db_version, '>')) { |
| 45 |
foreach ($updates as $update) { |
| 46 |
call_user_func(array(__CLASS__, $update)); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
update_option('wpo_update_version', WPO_VERSION); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Delete old semaphore locks from options database table. |
| 56 |
*/ |
| 57 |
public static function delete_old_locks() { |
| 58 |
global $wpdb; |
| 59 |
|
| 60 |
// using this query we delete all rows related to locks. |
| 61 |
$query = "DELETE FROM {$wpdb->options}". |
| 62 |
" WHERE (option_name LIKE ('updraft_semaphore_%')". |
| 63 |
" OR option_name LIKE ('updraft_last_lock_time_%')". |
| 64 |
" OR option_name LIKE ('updraft_locked_%')". |
| 65 |
" OR option_name LIKE ('updraft_unlocked_%'))". |
| 66 |
" AND ". |
| 67 |
"(option_name LIKE ('%smush')". |
| 68 |
" OR option_name LIKE ('%load-url-task'));"; |
| 69 |
|
| 70 |
$wpdb->query($query); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Disable cache directories viewing. |
| 75 |
*/ |
| 76 |
public static function disable_cache_directories_viewing() { |
| 77 |
wpo_disable_cache_directories_viewing(); |
| 78 |
} |
| 79 |
|
| 80 |
public static function reset_wpo_plugin_cron_tasks_schedule() { |
| 81 |
wp_clear_scheduled_hook('wpo_plugin_cron_tasks'); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Update Minify Defer option (The option was hidden until now, but we're changing the setting) |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public static function enable_minify_defer() { |
| 90 |
if (!function_exists('wp_optimize_minify_config')) { |
| 91 |
include_once WPO_PLUGIN_MAIN_PATH . '/minify/class-wp-optimize-minify-config.php'; |
| 92 |
} |
| 93 |
$current_setting = wp_optimize_minify_config()->get('enable_defer_js'); |
| 94 |
if (true === $current_setting) { |
| 95 |
wp_optimize_minify_config()->update(array('enable_defer_js' => 'all')); |
| 96 |
} else { |
| 97 |
wp_optimize_minify_config()->update(array('enable_defer_js' => 'individual')); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Update Minify default exclude options |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public static function update_minify_excludes() { |
| 107 |
if (!WPO_MINIFY_PHP_VERSION_MET) return; |
| 108 |
if (!function_exists('wp_optimize_minify_config')) { |
| 109 |
include_once WPO_PLUGIN_MAIN_PATH . '/minify/class-wp-optimize-minify-config.php'; |
| 110 |
} |
| 111 |
|
| 112 |
if (!class_exists('WP_Optimize_Minify_Functions')) { |
| 113 |
include_once WPO_PLUGIN_MAIN_PATH . '/minify/class-wp-optimize-minify-functions.php'; |
| 114 |
} |
| 115 |
|
| 116 |
$new_default_items = array( |
| 117 |
'elementor-admin-bar', |
| 118 |
'pdfjs-dist', |
| 119 |
'wordpress-popular-posts', |
| 120 |
); |
| 121 |
|
| 122 |
$user_excluded_blacklist_items = array(); |
| 123 |
$user_excluded_ignorelist_items = array(); |
| 124 |
|
| 125 |
// Get the lists as saved in the DB |
| 126 |
$current_blacklist = wp_optimize_minify_config()->get('blacklist'); |
| 127 |
$current_ignorelist = wp_optimize_minify_config()->get('ignore_list'); |
| 128 |
|
| 129 |
// Only proceed if the the upgrade hasn't been done yet, i.e. the values aren't arrays |
| 130 |
if (is_array($current_blacklist) && is_array($current_ignorelist)) return; |
| 131 |
|
| 132 |
$current_blacklist = array_map('trim', explode("\n", $current_blacklist)); |
| 133 |
$current_ignorelist = array_map('trim', explode("\n", $current_ignorelist)); |
| 134 |
|
| 135 |
// Get the default lists |
| 136 |
$default_blacklist = WP_Optimize_Minify_Functions::get_default_ie_blacklist(); |
| 137 |
$default_ignorelist = WP_Optimize_Minify_Functions::get_default_ignore(); |
| 138 |
|
| 139 |
foreach ($default_blacklist as $bl_item) { |
| 140 |
// If a blacklist item isn't in the list, it was manually removed by the user, so we save that. |
| 141 |
if (!in_array($bl_item, $current_blacklist)) $user_excluded_blacklist_items[] = $bl_item; |
| 142 |
} |
| 143 |
|
| 144 |
foreach ($default_ignorelist as $il_item) { |
| 145 |
if (!in_array($il_item, $current_ignorelist) && !in_array($il_item, $new_default_items)) $user_excluded_ignorelist_items[] = $il_item; |
| 146 |
} |
| 147 |
|
| 148 |
// Update the options |
| 149 |
wp_optimize_minify_config()->update(array('blacklist' => $user_excluded_blacklist_items)); |
| 150 |
wp_optimize_minify_config()->update(array('ignore_list' => $user_excluded_ignorelist_items)); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
endif; |
| 155 |
|