| @@ -35,12 +35,9 @@ | ||
| 35 | 35 | $this->enabled = wp_optimize_minify_config()->is_enabled(); |
| 36 | 36 | |
| 37 | 37 | $this->load_admin(); |
| 38 | 38 | |
| 39 | - // Don't run the rest if PHP requirement isn't met | |
| 40 | - if (!WPO_MINIFY_PHP_VERSION_MET) return; | |
| 41 | - | |
| 42 | - 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); | |
| 43 | 40 | |
| 44 | 41 | if (WP_Optimize::is_premium()) { |
| 45 | 42 | $this->load_premium(); |
| 46 | 43 | } |
| @@ -59,9 +56,9 @@ | ||
| 59 | 56 | |
| 60 | 57 | // cron job to delete old wpo_min cache |
| 61 | 58 | add_action('wpo_minify_purge_old_cache', array('WP_Optimize_Minify_Cache_Functions', 'purge_old')); |
| 62 | 59 | |
| 63 | - add_action('init', array($this, 'schedule_or_unschedule_purge_old_cache_event')); | |
| 60 | + add_action('wp_loaded', array($this, 'schedule_or_unschedule_purge_old_cache_event')); | |
| 64 | 61 | |
| 65 | 62 | // Handle minify cache purging. |
| 66 | 63 | add_action('wp_loaded', array($this, 'handle_purge_minify_cache')); |
| 67 | 64 | |
| @@ -99,13 +96,11 @@ | ||
| 99 | 96 | * @param array $menu_items |
| 100 | 97 | * @return array |
| 101 | 98 | */ |
| 102 | 99 | public function admin_bar_menu($menu_items) { |
| 103 | - $wpo_minify_options = wp_optimize_minify_config()->get(); | |
| 104 | - | |
| 105 | - 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; | |
| 106 | 101 | |
| 107 | - $act_url = remove_query_arg('wpo_minify_cache_purged'); | |
| 102 | + $act_url = WP_Optimize_Utils::get_url_without_cache_purge_params(); | |
| 108 | 103 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 109 | 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>'; |
| 110 | 105 | |
| 111 | 106 | $menu_items[] = array( |
| @@ -129,33 +124,40 @@ | ||
| 129 | 124 | /** |
| 130 | 125 | * Check if purge single page action sent and purge cache. |
| 131 | 126 | */ |
| 132 | 127 | public function handle_purge_minify_cache() { |
| 133 | - $wpo_minify_options = wp_optimize_minify_config()->get(); | |
| 134 | - if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return; | |
| 128 | + if (!$this->enabled || !$this->can_purge_cache()) return; | |
| 135 | 129 | |
| 136 | - 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) { | |
| 137 | 132 | if (is_admin()) { |
| 138 | - 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')); | |
| 139 | 137 | return; |
| 140 | 138 | } else { |
| 141 | - $message = __('Minify cache purged', 'wp-optimize'); | |
| 142 | - 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(); | |
| 143 | 142 | return; |
| 144 | 143 | } |
| 145 | 144 | } |
| 146 | 145 | |
| 147 | - 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'); | |
| 148 | 147 | |
| 149 | - if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) { | |
| 148 | + if ($nonce) { | |
| 150 | 149 | $success = false; |
| 151 | 150 | |
| 152 | 151 | // Purge minify |
| 153 | 152 | $results = $this->minify_commands->purge_minify_cache(); |
| 154 | - if ("caches cleared" == $results['result']) $success = true; | |
| 153 | + if ("caches cleared" === $results['result']) $success = true; | |
| 155 | 154 | |
| 156 | 155 | // remove nonce from url and reload page. |
| 157 | - 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 | + | |
| 158 | 160 | exit; |
| 159 | 161 | |
| 160 | 162 | } |
| 161 | 163 | } |
| @@ -214,9 +216,9 @@ | ||
| 214 | 216 | |
| 215 | 217 | /** |
| 216 | 218 | * Unschedule purging of the minify cache |
| 217 | 219 | * |
| 218 | - * @retrun void | |
| 220 | + * @return void | |
| 219 | 221 | */ |
| 220 | 222 | private function unschedule_purge_old_cache_event() { |
| 221 | 223 | // old cache purge event cron |
| 222 | 224 | wp_clear_scheduled_hook('wpo_minify_purge_old_cache'); |
| @@ -227,9 +229,8 @@ | ||
| 227 | 229 | * |
| 228 | 230 | * @return void |
| 229 | 231 | */ |
| 230 | 232 | public function plugin_deactivate() { |
| 231 | - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return; | |
| 232 | 233 | if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) { |
| 233 | 234 | WP_Optimize_Minify_Cache_Functions::purge_temp_files(); |
| 234 | 235 | WP_Optimize_Minify_Cache_Functions::purge_old(); |
| 235 | 236 | WP_Optimize_Minify_Cache_Functions::purge_others(); |
| @@ -243,9 +244,8 @@ | ||
| 243 | 244 | * |
| 244 | 245 | * @return void |
| 245 | 246 | */ |
| 246 | 247 | public function plugin_uninstall() { |
| 247 | - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return; | |
| 248 | 248 | // remove options from DB |
| 249 | 249 | if (!function_exists('wp_optimize_minify_config')) { |
| 250 | 250 | include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php'; |
| 251 | 251 | } |
| @@ -264,8 +264,15 @@ | ||
| 264 | 264 | $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success'); |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 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 | + /** | |
| 268 | 275 | * Show notification in WordPress admin. |
| 269 | 276 | * |
| 270 | 277 | * @param string $message HTML (no further escaping is performed) |
| 271 | 278 | * @param string $type error, warning, success, or info |
| @@ -279,10 +286,10 @@ | ||
| 279 | 286 | window.addEventListener('load', function() { |
| 280 | 287 | (function(wp) { |
| 281 | 288 | if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) { |
| 282 | 289 | wp.data.dispatch('core/notices').createNotice( |
| 283 | - '<?php echo $type; ?>', | |
| 284 | - '<?php echo $message; ?>', | |
| 290 | + '<?php echo esc_js($type); ?>', | |
| 291 | + '<?php echo wp_kses_post($message); ?>', | |
| 285 | 292 | { |
| 286 | 293 | isDismissible: true, |
| 287 | 294 | } |
| 288 | 295 | ); |
| @@ -290,10 +297,10 @@ | ||
| 290 | 297 | })(window.wp); |
| 291 | 298 | }); |
| 292 | 299 | </script> |
| 293 | 300 | <?php else : ?> |
| 294 | - <div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible"> | |
| 295 | - <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> | |
| 296 | 303 | </div> |
| 297 | 304 | <?php |
| 298 | 305 | endif; |
| 299 | 306 | } |
| @@ -303,13 +310,7 @@ | ||
| 303 | 310 | * |
| 304 | 311 | * @return bool |
| 305 | 312 | */ |
| 306 | 313 | public function can_purge_cache() { |
| 307 | - $required_capability = is_multisite() ? 'manage_network_options' : 'manage_options'; | |
| 308 | - | |
| 309 | - if (WP_Optimize::is_premium()) { | |
| 310 | - return current_user_can($required_capability) || WP_Optimize_Premium()->can_purge_the_cache(); | |
| 311 | - } else { | |
| 312 | - return current_user_can($required_capability); | |
| 313 | - } | |
| 314 | + return WP_Optimize_Utils::current_user_can_purge_cache(); | |
| 314 | 315 | } |
| 315 | 316 | } |