| @@ -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 | */ |
| @@ -22,12 +35,9 @@ | ||
| 22 | 35 | $this->enabled = wp_optimize_minify_config()->is_enabled(); |
| 23 | 36 | |
| 24 | 37 | $this->load_admin(); |
| 25 | 38 | |
| 26 | - // Don't run the rest if PHP requirement isn't met | |
| 27 | - if (!WPO_MINIFY_PHP_VERSION_MET) return; | |
| 28 | - | |
| 29 | - 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); | |
| 30 | 40 | |
| 31 | 41 | if (WP_Optimize::is_premium()) { |
| 32 | 42 | $this->load_premium(); |
| 33 | 43 | } |
| @@ -32,9 +42,9 @@ | ||
| 32 | 42 | $this->load_premium(); |
| 33 | 43 | } |
| 34 | 44 | |
| 35 | 45 | /** |
| 36 | - * 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 | |
| 37 | 47 | */ |
| 38 | 48 | if (!defined('WPO_CACHE_MIN_FILES_DIR')) define('WPO_CACHE_MIN_FILES_DIR', untrailingslashit(WP_CONTENT_DIR).'/cache/wpo-minify'); |
| 39 | 49 | if (!defined('WPO_CACHE_MIN_FILES_URL')) define('WPO_CACHE_MIN_FILES_URL', untrailingslashit(WP_CONTENT_URL).'/cache/wpo-minify'); |
| 40 | 50 | |
| @@ -45,9 +55,10 @@ | ||
| 45 | 55 | $this->load_frontend(); |
| 46 | 56 | |
| 47 | 57 | // cron job to delete old wpo_min cache |
| 48 | 58 | add_action('wpo_minify_purge_old_cache', array('WP_Optimize_Minify_Cache_Functions', 'purge_old')); |
| 49 | - // 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')); | |
| 50 | 61 | |
| 51 | 62 | // Handle minify cache purging. |
| 52 | 63 | add_action('wp_loaded', array($this, 'handle_purge_minify_cache')); |
| 53 | 64 | |
| @@ -53,8 +64,34 @@ | ||
| 53 | 64 | |
| 54 | 65 | } |
| 55 | 66 | |
| 56 | 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 | + /** | |
| 57 | 94 | * Admin toolbar processing |
| 58 | 95 | * |
| 59 | 96 | * @param array $menu_items |
| 60 | 97 | * @return array |
| @@ -59,13 +96,11 @@ | ||
| 59 | 96 | * @param array $menu_items |
| 60 | 97 | * @return array |
| 61 | 98 | */ |
| 62 | 99 | public function admin_bar_menu($menu_items) { |
| 63 | - $wpo_minify_options = wp_optimize_minify_config()->get(); | |
| 64 | - | |
| 65 | - 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; | |
| 66 | 101 | |
| 67 | - $act_url = remove_query_arg('wpo_minify_cache_purged'); | |
| 102 | + $act_url = WP_Optimize_Utils::get_url_without_cache_purge_params(); | |
| 68 | 103 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 69 | 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>'; |
| 70 | 105 | |
| 71 | 106 | $menu_items[] = array( |
| @@ -89,33 +124,40 @@ | ||
| 89 | 124 | /** |
| 90 | 125 | * Check if purge single page action sent and purge cache. |
| 91 | 126 | */ |
| 92 | 127 | public function handle_purge_minify_cache() { |
| 93 | - $wpo_minify_options = wp_optimize_minify_config()->get(); | |
| 94 | - if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return; | |
| 128 | + if (!$this->enabled || !$this->can_purge_cache()) return; | |
| 95 | 129 | |
| 96 | - 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) { | |
| 97 | 132 | if (is_admin()) { |
| 98 | - 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')); | |
| 99 | 137 | return; |
| 100 | 138 | } else { |
| 101 | - $message = __('Minify cache purged', 'wp-optmize'); | |
| 102 | - 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(); | |
| 103 | 142 | return; |
| 104 | 143 | } |
| 105 | 144 | } |
| 106 | 145 | |
| 107 | - 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'); | |
| 108 | 147 | |
| 109 | - if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) { | |
| 148 | + if ($nonce) { | |
| 110 | 149 | $success = false; |
| 111 | 150 | |
| 112 | 151 | // Purge minify |
| 113 | 152 | $results = $this->minify_commands->purge_minify_cache(); |
| 114 | - if ("caches cleared" == $results['result']) $success = true; | |
| 153 | + if ("caches cleared" === $results['result']) $success = true; | |
| 115 | 154 | |
| 116 | 155 | // remove nonce from url and reload page. |
| 117 | - 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 | + | |
| 118 | 160 | exit; |
| 119 | 161 | |
| 120 | 162 | } |
| 121 | 163 | } |
| @@ -146,9 +188,9 @@ | ||
| 146 | 188 | * |
| 147 | 189 | * @return void |
| 148 | 190 | */ |
| 149 | 191 | private function load_premium() { |
| 150 | - $this->premium = new WP_Optimize_Minify_Premium(); | |
| 192 | + new WP_Optimize_Minify_Premium(); | |
| 151 | 193 | } |
| 152 | 194 | |
| 153 | 195 | /** |
| 154 | 196 | * Run during activation |
| @@ -160,31 +202,42 @@ | ||
| 160 | 202 | // increment cache time |
| 161 | 203 | if (class_exists('WP_Optimize_Minify_Cache_Functions')) { |
| 162 | 204 | WP_Optimize_Minify_Cache_Functions::cache_increment(); |
| 163 | 205 | } |
| 164 | - | |
| 165 | - // old cache purge event cron | |
| 166 | - 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() { | |
| 167 | 212 | if (!wp_next_scheduled('wpo_minify_purge_old_cache')) { |
| 168 | - wp_schedule_event(time() + 86400, 'daily', 'wpo_minify_purge_old_cache'); | |
| 213 | + wp_schedule_event(time() + 43200, 'daily', 'wpo_minify_purge_old_cache'); | |
| 169 | 214 | } |
| 170 | 215 | } |
| 171 | 216 | |
| 172 | 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 | + /** | |
| 173 | 228 | * Run during plugin deactivation |
| 174 | 229 | * |
| 175 | 230 | * @return void |
| 176 | 231 | */ |
| 177 | 232 | public function plugin_deactivate() { |
| 178 | - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return; | |
| 179 | - if (class_exists('WP_Optimize_Minify_Cache_Functions')) { | |
| 233 | + if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) { | |
| 180 | 234 | WP_Optimize_Minify_Cache_Functions::purge_temp_files(); |
| 181 | 235 | WP_Optimize_Minify_Cache_Functions::purge_old(); |
| 182 | 236 | WP_Optimize_Minify_Cache_Functions::purge_others(); |
| 183 | 237 | } |
| 184 | 238 | |
| 185 | - // old cache purge event cron | |
| 186 | - wp_clear_scheduled_hook('wpo_minify_purge_old_cache'); | |
| 239 | + $this->unschedule_purge_old_cache_event(); | |
| 187 | 240 | } |
| 188 | 241 | |
| 189 | 242 | /** |
| 190 | 243 | * Run during plugin uninstall |
| @@ -191,9 +244,8 @@ | ||
| 191 | 244 | * |
| 192 | 245 | * @return void |
| 193 | 246 | */ |
| 194 | 247 | public function plugin_uninstall() { |
| 195 | - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return; | |
| 196 | 248 | // remove options from DB |
| 197 | 249 | if (!function_exists('wp_optimize_minify_config')) { |
| 198 | 250 | include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php'; |
| 199 | 251 | } |
| @@ -212,8 +264,15 @@ | ||
| 212 | 264 | $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success'); |
| 213 | 265 | } |
| 214 | 266 | |
| 215 | 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 | + /** | |
| 216 | 275 | * Show notification in WordPress admin. |
| 217 | 276 | * |
| 218 | 277 | * @param string $message HTML (no further escaping is performed) |
| 219 | 278 | * @param string $type error, warning, success, or info |
| @@ -227,10 +286,10 @@ | ||
| 227 | 286 | window.addEventListener('load', function() { |
| 228 | 287 | (function(wp) { |
| 229 | 288 | if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) { |
| 230 | 289 | wp.data.dispatch('core/notices').createNotice( |
| 231 | - '<?php echo $type; ?>', | |
| 232 | - '<?php echo $message; ?>', | |
| 290 | + '<?php echo esc_js($type); ?>', | |
| 291 | + '<?php echo wp_kses_post($message); ?>', | |
| 233 | 292 | { |
| 234 | 293 | isDismissible: true, |
| 235 | 294 | } |
| 236 | 295 | ); |
| @@ -238,11 +297,20 @@ | ||
| 238 | 297 | })(window.wp); |
| 239 | 298 | }); |
| 240 | 299 | </script> |
| 241 | 300 | <?php else : ?> |
| 242 | - <div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible"> | |
| 243 | - <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> | |
| 244 | 303 | </div> |
| 245 | 304 | <?php |
| 246 | 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(); | |
| 247 | 315 | } |
| 248 | 316 | } |