| @@ -5,9 +5,22 @@ | ||
| 5 | 5 | define('WP_OPTIMIZE_MINIFY_DIR', dirname(__FILE__)); |
| 6 | 6 | if (!defined('WP_OPTIMIZE_SHOW_MINIFY_ADVANCED')) define('WP_OPTIMIZE_SHOW_MINIFY_ADVANCED', false); |
| 7 | 7 | |
| 8 | 8 | class WP_Optimize_Minify { |
| 9 | + | |
| 9 | 10 | /** |
| 11 | + * Minify commands object | |
| 12 | + * | |
| 13 | + * @var WP_Optimize_Minify_Commands | |
| 14 | + */ | |
| 15 | + public $minify_commands; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * @var bool | |
| 19 | + */ | |
| 20 | + private $enabled; | |
| 21 | + | |
| 22 | + /** | |
| 10 | 23 | * Constructor - Initialize actions and filters |
| 11 | 24 | * |
| 12 | 25 | * @return void |
| 13 | 26 | */ |
| @@ -12,9 +25,8 @@ | ||
| 12 | 25 | * @return void |
| 13 | 26 | */ |
| 14 | 27 | public function __construct() { |
| 15 | 28 | |
| 16 | - if (!class_exists('WP_Optimize_Minify_Commands')) include_once(WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-commands.php'); | |
| 17 | 29 | $this->minify_commands = new WP_Optimize_Minify_Commands(); |
| 18 | 30 | |
| 19 | 31 | if (!class_exists('WP_Optimize_Minify_Config')) { |
| 20 | 32 | include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php'; |
| @@ -23,12 +35,9 @@ | ||
| 23 | 35 | $this->enabled = wp_optimize_minify_config()->is_enabled(); |
| 24 | 36 | |
| 25 | 37 | $this->load_admin(); |
| 26 | 38 | |
| 27 | - // Don't run the rest if PHP requirement isn't met | |
| 28 | - if (!WPO_MINIFY_PHP_VERSION_MET) return; | |
| 29 | - | |
| 30 | - add_filter('wpo_cache_admin_bar_menu_items', array($this, 'admin_bar_menu'), 30, 1); | |
| 39 | + add_filter('wpo_cache_admin_bar_menu_items', array($this, 'admin_bar_menu'), 30); | |
| 31 | 40 | |
| 32 | 41 | if (WP_Optimize::is_premium()) { |
| 33 | 42 | $this->load_premium(); |
| 34 | 43 | } |
| @@ -33,9 +42,9 @@ | ||
| 33 | 42 | $this->load_premium(); |
| 34 | 43 | } |
| 35 | 44 | |
| 36 | 45 | /** |
| 37 | - * Directory that stores the cache, including gzipped files and mobile specifc cache | |
| 46 | + * Directory that stores the cache, including gzipped files and mobile specific cache | |
| 38 | 47 | */ |
| 39 | 48 | if (!defined('WPO_CACHE_MIN_FILES_DIR')) define('WPO_CACHE_MIN_FILES_DIR', untrailingslashit(WP_CONTENT_DIR).'/cache/wpo-minify'); |
| 40 | 49 | if (!defined('WPO_CACHE_MIN_FILES_URL')) define('WPO_CACHE_MIN_FILES_URL', untrailingslashit(WP_CONTENT_URL).'/cache/wpo-minify'); |
| 41 | 50 | |
| @@ -46,9 +55,10 @@ | ||
| 46 | 55 | $this->load_frontend(); |
| 47 | 56 | |
| 48 | 57 | // cron job to delete old wpo_min cache |
| 49 | 58 | add_action('wpo_minify_purge_old_cache', array('WP_Optimize_Minify_Cache_Functions', 'purge_old')); |
| 50 | - // front-end actions; skip on certain post_types or if there are specific keys on the url or if editor or admin | |
| 59 | + | |
| 60 | + add_action('wp_loaded', array($this, 'schedule_or_unschedule_purge_old_cache_event')); | |
| 51 | 61 | |
| 52 | 62 | // Handle minify cache purging. |
| 53 | 63 | add_action('wp_loaded', array($this, 'handle_purge_minify_cache')); |
| 54 | 64 | |
| @@ -54,8 +64,34 @@ | ||
| 54 | 64 | |
| 55 | 65 | } |
| 56 | 66 | |
| 57 | 67 | /** |
| 68 | + * Schedule or unschedule purge old cache event. | |
| 69 | + * | |
| 70 | + * @return void | |
| 71 | + */ | |
| 72 | + public function schedule_or_unschedule_purge_old_cache_event() { | |
| 73 | + if ($this->enabled) { | |
| 74 | + $this->schedule_purge_old_cache_event(); | |
| 75 | + } else { | |
| 76 | + $this->unschedule_purge_old_cache_event(); | |
| 77 | + } | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Returns singleton instance object | |
| 82 | + * | |
| 83 | + * @return WP_Optimize_Minify Returns `WP_Optimize_Minify` object | |
| 84 | + */ | |
| 85 | + public static function instance() { | |
| 86 | + static $_instance = null; | |
| 87 | + if (null === $_instance) { | |
| 88 | + $_instance = new self(); | |
| 89 | + } | |
| 90 | + return $_instance; | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 58 | 94 | * Admin toolbar processing |
| 59 | 95 | * |
| 60 | 96 | * @param array $menu_items |
| 61 | 97 | * @return array |
| @@ -60,13 +96,11 @@ | ||
| 60 | 96 | * @param array $menu_items |
| 61 | 97 | * @return array |
| 62 | 98 | */ |
| 63 | 99 | public function admin_bar_menu($menu_items) { |
| 64 | - $wpo_minify_options = wp_optimize_minify_config()->get(); | |
| 65 | - | |
| 66 | - if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options') || !($wpo_minify_options['enable_css'] || $wpo_minify_options['enable_js'])) return $menu_items; | |
| 100 | + if (!$this->enabled || !$this->can_purge_cache()) return $menu_items; | |
| 67 | 101 | |
| 68 | - $act_url = remove_query_arg('wpo_minify_cache_purged'); | |
| 102 | + $act_url = WP_Optimize_Utils::get_url_without_cache_purge_params(); | |
| 69 | 103 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 70 | 104 | $cache_size_info = '<h4>'.__('Minify cache', 'wp-optimize').'</h4><span><span class="label">'.__('Cache size:', 'wp-optimize').'</span> <span class="stats">'.esc_html(WP_Optimize_Minify_Cache_Functions::get_cachestats($cache_path['cachedir'])).'</span></span>'; |
| 71 | 105 | |
| 72 | 106 | $menu_items[] = array( |
| @@ -90,33 +124,40 @@ | ||
| 90 | 124 | /** |
| 91 | 125 | * Check if purge single page action sent and purge cache. |
| 92 | 126 | */ |
| 93 | 127 | public function handle_purge_minify_cache() { |
| 94 | - $wpo_minify_options = wp_optimize_minify_config()->get(); | |
| 95 | - if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return; | |
| 128 | + if (!$this->enabled || !$this->can_purge_cache()) return; | |
| 96 | 129 | |
| 97 | - if (isset($_GET['wpo_minify_cache_purged'])) { | |
| 130 | + $is_cache_purged = TeamUpdraft\WP_Optimize\Includes\Fragments\fetch_superglobal('get', 'wpo_minify_cache_purged'); | |
| 131 | + if (null !== $is_cache_purged) { | |
| 98 | 132 | if (is_admin()) { |
| 99 | - add_action('admin_notices', array($this, 'notice_purge_minify_cache_success')); | |
| 133 | + $admin_notices_hook = is_network_admin() ? 'network_admin_notices' : 'admin_notices'; | |
| 134 | + $notice_function = 'notice_purge_minify_cache_' . ('false' === $is_cache_purged ? 'failure' : 'success'); | |
| 135 | + add_action($admin_notices_hook, array($this, $notice_function)); | |
| 136 | + add_action('admin_head', array('WP_Optimize_Utils', 'script_to_remove_cache_purge_params_from_url')); | |
| 100 | 137 | return; |
| 101 | 138 | } else { |
| 102 | - $message = __('Minify cache purged', 'wp-optmize'); | |
| 103 | - printf('<script>alert("%s");</script>', $message); | |
| 139 | + $message = 'false' === $is_cache_purged ? __('Failed to purge the minify cache.', 'wp-optimize') : __('Minify cache purged', 'wp-optimize'); | |
| 140 | + printf('<script>window.onload = function() {alert("%s");}</script>', esc_js($message)); | |
| 141 | + WP_Optimize_Utils::script_to_remove_cache_purge_params_from_url(); | |
| 104 | 142 | return; |
| 105 | 143 | } |
| 106 | 144 | } |
| 107 | 145 | |
| 108 | - if (!isset($_GET['_wpo_purge_minify_cache'])) return; | |
| 146 | + $nonce = TeamUpdraft\WP_Optimize\Includes\Fragments\verify_nonce('_wpo_purge_minify_cache', 'wpo_purge_minify_cache'); | |
| 109 | 147 | |
| 110 | - if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) { | |
| 148 | + if ($nonce) { | |
| 111 | 149 | $success = false; |
| 112 | 150 | |
| 113 | 151 | // Purge minify |
| 114 | 152 | $results = $this->minify_commands->purge_minify_cache(); |
| 115 | - if ("caches cleared" == $results['result']) $success = true; | |
| 153 | + if ("caches cleared" === $results['result']) $success = true; | |
| 116 | 154 | |
| 117 | 155 | // remove nonce from url and reload page. |
| 118 | - wp_redirect(add_query_arg('wpo_minify_cache_purged', $success, remove_query_arg('_wpo_purge_minify_cache'))); | |
| 156 | + $redirect_url = WP_Optimize_Utils::get_url_without_cache_purge_params(); | |
| 157 | + $redirect_url = add_query_arg('wpo_minify_cache_purged', $success ? $success : 'false', $redirect_url); | |
| 158 | + wp_redirect($redirect_url); | |
| 159 | + | |
| 119 | 160 | exit; |
| 120 | 161 | |
| 121 | 162 | } |
| 122 | 163 | } |
| @@ -127,12 +168,8 @@ | ||
| 127 | 168 | * @return void |
| 128 | 169 | */ |
| 129 | 170 | private function load_admin() { |
| 130 | 171 | if (!is_admin()) return; |
| 131 | - | |
| 132 | - if (!class_exists('WP_Optimize_Minify_Admin')) { | |
| 133 | - include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-admin.php'; | |
| 134 | - } | |
| 135 | 172 | new WP_Optimize_Minify_Admin(); |
| 136 | 173 | } |
| 137 | 174 | |
| 138 | 175 | /** |
| @@ -141,11 +178,8 @@ | ||
| 141 | 178 | * @return void |
| 142 | 179 | */ |
| 143 | 180 | private function load_frontend() { |
| 144 | 181 | if ($this->enabled) { |
| 145 | - if (!class_exists('WP_Optimize_Minify_Front_End')) { | |
| 146 | - include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-front-end.php'; | |
| 147 | - } | |
| 148 | 182 | new WP_Optimize_Minify_Front_End(); |
| 149 | 183 | } |
| 150 | 184 | } |
| 151 | 185 | |
| @@ -154,12 +188,9 @@ | ||
| 154 | 188 | * |
| 155 | 189 | * @return void |
| 156 | 190 | */ |
| 157 | 191 | private function load_premium() { |
| 158 | - if (!class_exists('WP_Optimize_Minify_Premium')) { | |
| 159 | - include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-premium.php'; | |
| 160 | - } | |
| 161 | - $this->premium = new WP_Optimize_Minify_Premium(); | |
| 192 | + new WP_Optimize_Minify_Premium(); | |
| 162 | 193 | } |
| 163 | 194 | |
| 164 | 195 | /** |
| 165 | 196 | * Run during activation |
| @@ -171,31 +202,42 @@ | ||
| 171 | 202 | // increment cache time |
| 172 | 203 | if (class_exists('WP_Optimize_Minify_Cache_Functions')) { |
| 173 | 204 | WP_Optimize_Minify_Cache_Functions::cache_increment(); |
| 174 | 205 | } |
| 175 | - | |
| 176 | - // old cache purge event cron | |
| 177 | - wp_clear_scheduled_hook('wpo_minify_purge_old_cache'); | |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * If the WP cron event for scheduling purging of the minify cache does not exist, then create it | |
| 210 | + */ | |
| 211 | + private function schedule_purge_old_cache_event() { | |
| 178 | 212 | if (!wp_next_scheduled('wpo_minify_purge_old_cache')) { |
| 179 | - wp_schedule_event(time() + 86400, 'daily', 'wpo_minify_purge_old_cache'); | |
| 213 | + wp_schedule_event(time() + 43200, 'daily', 'wpo_minify_purge_old_cache'); | |
| 180 | 214 | } |
| 181 | 215 | } |
| 182 | 216 | |
| 183 | 217 | /** |
| 218 | + * Unschedule purging of the minify cache | |
| 219 | + * | |
| 220 | + * @return void | |
| 221 | + */ | |
| 222 | + private function unschedule_purge_old_cache_event() { | |
| 223 | + // old cache purge event cron | |
| 224 | + wp_clear_scheduled_hook('wpo_minify_purge_old_cache'); | |
| 225 | + } | |
| 226 | + | |
| 227 | + /** | |
| 184 | 228 | * Run during plugin deactivation |
| 185 | 229 | * |
| 186 | 230 | * @return void |
| 187 | 231 | */ |
| 188 | 232 | public function plugin_deactivate() { |
| 189 | - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return; | |
| 190 | - if (class_exists('WP_Optimize_Minify_Cache_Functions')) { | |
| 233 | + if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) { | |
| 191 | 234 | WP_Optimize_Minify_Cache_Functions::purge_temp_files(); |
| 192 | 235 | WP_Optimize_Minify_Cache_Functions::purge_old(); |
| 193 | 236 | WP_Optimize_Minify_Cache_Functions::purge_others(); |
| 194 | 237 | } |
| 195 | 238 | |
| 196 | - // old cache purge event cron | |
| 197 | - wp_clear_scheduled_hook('wpo_minify_purge_old_cache'); | |
| 239 | + $this->unschedule_purge_old_cache_event(); | |
| 198 | 240 | } |
| 199 | 241 | |
| 200 | 242 | /** |
| 201 | 243 | * Run during plugin uninstall |
| @@ -202,9 +244,8 @@ | ||
| 202 | 244 | * |
| 203 | 245 | * @return void |
| 204 | 246 | */ |
| 205 | 247 | public function plugin_uninstall() { |
| 206 | - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return; | |
| 207 | 248 | // remove options from DB |
| 208 | 249 | if (!function_exists('wp_optimize_minify_config')) { |
| 209 | 250 | include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php'; |
| 210 | 251 | } |
| @@ -223,8 +264,15 @@ | ||
| 223 | 264 | $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success'); |
| 224 | 265 | } |
| 225 | 266 | |
| 226 | 267 | /** |
| 268 | + * Shows failure notice for purge minify cache | |
| 269 | + */ | |
| 270 | + public function notice_purge_minify_cache_failure() { | |
| 271 | + $this->show_notice(__('Failed to purge the minify cache.', 'wp-optimize'), 'error'); | |
| 272 | + } | |
| 273 | + | |
| 274 | + /** | |
| 227 | 275 | * Show notification in WordPress admin. |
| 228 | 276 | * |
| 229 | 277 | * @param string $message HTML (no further escaping is performed) |
| 230 | 278 | * @param string $type error, warning, success, or info |
| @@ -238,10 +286,10 @@ | ||
| 238 | 286 | window.addEventListener('load', function() { |
| 239 | 287 | (function(wp) { |
| 240 | 288 | if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) { |
| 241 | 289 | wp.data.dispatch('core/notices').createNotice( |
| 242 | - '<?php echo $type; ?>', | |
| 243 | - '<?php echo $message; ?>', | |
| 290 | + '<?php echo esc_js($type); ?>', | |
| 291 | + '<?php echo wp_kses_post($message); ?>', | |
| 244 | 292 | { |
| 245 | 293 | isDismissible: true, |
| 246 | 294 | } |
| 247 | 295 | ); |
| @@ -249,11 +297,20 @@ | ||
| 249 | 297 | })(window.wp); |
| 250 | 298 | }); |
| 251 | 299 | </script> |
| 252 | 300 | <?php else : ?> |
| 253 | - <div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible"> | |
| 254 | - <p><?php echo $message; ?></p> | |
| 301 | + <div class="notice wpo-notice notice-<?php echo esc_attr($type); ?> is-dismissible"> | |
| 302 | + <p><?php echo wp_kses_post($message); ?></p> | |
| 255 | 303 | </div> |
| 256 | 304 | <?php |
| 257 | 305 | endif; |
| 306 | + } | |
| 307 | + | |
| 308 | + /** | |
| 309 | + * Check if current user can purge cache. | |
| 310 | + * | |
| 311 | + * @return bool | |
| 312 | + */ | |
| 313 | + public function can_purge_cache() { | |
| 314 | + return WP_Optimize_Utils::current_user_can_purge_cache(); | |
| 258 | 315 | } |
| 259 | 316 | } |