| @@ -96,13 +96,11 @@ | ||
| 96 | 96 | * @param array $menu_items |
| 97 | 97 | * @return array |
| 98 | 98 | */ |
| 99 | 99 | public function admin_bar_menu($menu_items) { |
| 100 | - $wpo_minify_options = wp_optimize_minify_config()->get(); | |
| 101 | - | |
| 102 | - 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; | |
| 103 | 101 | |
| 104 | - $act_url = remove_query_arg('wpo_minify_cache_purged'); | |
| 102 | + $act_url = WP_Optimize_Utils::get_url_without_cache_purge_params(); | |
| 105 | 103 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 106 | 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>'; |
| 107 | 105 | |
| 108 | 106 | $menu_items[] = array( |
| @@ -126,19 +124,22 @@ | ||
| 126 | 124 | /** |
| 127 | 125 | * Check if purge single page action sent and purge cache. |
| 128 | 126 | */ |
| 129 | 127 | public function handle_purge_minify_cache() { |
| 130 | - $wpo_minify_options = wp_optimize_minify_config()->get(); | |
| 131 | - if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return; | |
| 128 | + if (!$this->enabled || !$this->can_purge_cache()) return; | |
| 132 | 129 | |
| 133 | 130 | $is_cache_purged = TeamUpdraft\WP_Optimize\Includes\Fragments\fetch_superglobal('get', 'wpo_minify_cache_purged'); |
| 134 | 131 | if (null !== $is_cache_purged) { |
| 135 | 132 | if (is_admin()) { |
| 136 | - 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')); | |
| 137 | 137 | return; |
| 138 | 138 | } else { |
| 139 | - $message = __('Minify cache purged', 'wp-optimize'); | |
| 140 | - printf('<script>alert("%s");</script>', esc_html($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(); | |
| 141 | 142 | return; |
| 142 | 143 | } |
| 143 | 144 | } |
| 144 | 145 | |
| @@ -151,9 +152,12 @@ | ||
| 151 | 152 | $results = $this->minify_commands->purge_minify_cache(); |
| 152 | 153 | if ("caches cleared" === $results['result']) $success = true; |
| 153 | 154 | |
| 154 | 155 | // remove nonce from url and reload page. |
| 155 | - 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 | + | |
| 156 | 160 | exit; |
| 157 | 161 | |
| 158 | 162 | } |
| 159 | 163 | } |
| @@ -260,8 +264,15 @@ | ||
| 260 | 264 | $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success'); |
| 261 | 265 | } |
| 262 | 266 | |
| 263 | 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 | + /** | |
| 264 | 275 | * Show notification in WordPress admin. |
| 265 | 276 | * |
| 266 | 277 | * @param string $message HTML (no further escaping is performed) |
| 267 | 278 | * @param string $type error, warning, success, or info |
| @@ -299,13 +310,7 @@ | ||
| 299 | 310 | * |
| 300 | 311 | * @return bool |
| 301 | 312 | */ |
| 302 | 313 | public function can_purge_cache() { |
| 303 | - $required_capability = is_multisite() ? 'manage_network_options' : 'manage_options'; | |
| 304 | - | |
| 305 | - if (WP_Optimize::is_premium()) { | |
| 306 | - return current_user_can($required_capability) || WP_Optimize_Premium()->can_purge_the_cache(); | |
| 307 | - } else { | |
| 308 | - return current_user_can($required_capability); | |
| 309 | - } | |
| 314 | + return WP_Optimize_Utils::current_user_can_purge_cache(); | |
| 310 | 315 | } |
| 311 | 316 | } |