PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | minify/class-wp-optimize-minify.php +46 -36 3.5.0 → 4.7.0 View file →
@@ -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,17 +56,26 @@
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
60 + add_action('wp_loaded', array($this, 'schedule_or_unschedule_purge_old_cache_event'));
61 +
62 + // Handle minify cache purging.
63 + add_action('wp_loaded', array($this, 'handle_purge_minify_cache'));
64 +
65 + }
66 +
67 + /**
68 + * Schedule or unschedule purge old cache event.
69 + *
70 + * @return void
71 + */
72 + public function schedule_or_unschedule_purge_old_cache_event() {
63 73 if ($this->enabled) {
64 74 $this->schedule_purge_old_cache_event();
65 75 } else {
66 76 $this->unschedule_purge_old_cache_event();
67 77 }
68 -
69 - // Handle minify cache purging.
70 - add_action('wp_loaded', array($this, 'handle_purge_minify_cache'));
71 -
72 78 }
73 79
74 80 /**
75 81 * Returns singleton instance object
@@ -90,13 +96,11 @@
90 96 * @param array $menu_items
91 97 * @return array
92 98 */
93 99 public function admin_bar_menu($menu_items) {
94 - $wpo_minify_options = wp_optimize_minify_config()->get();
95 -
96 - 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;
97 101
98 - $act_url = remove_query_arg('wpo_minify_cache_purged');
102 + $act_url = WP_Optimize_Utils::get_url_without_cache_purge_params();
99 103 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
100 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>';
101 105
102 106 $menu_items[] = array(
@@ -120,33 +124,40 @@
120 124 /**
121 125 * Check if purge single page action sent and purge cache.
122 126 */
123 127 public function handle_purge_minify_cache() {
124 - $wpo_minify_options = wp_optimize_minify_config()->get();
125 - if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return;
128 + if (!$this->enabled || !$this->can_purge_cache()) return;
126 129
127 - 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) {
128 132 if (is_admin()) {
129 - 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'));
130 137 return;
131 138 } else {
132 - $message = __('Minify cache purged', 'wp-optimize');
133 - 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();
134 142 return;
135 143 }
136 144 }
137 145
138 - 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');
139 147
140 - if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) {
148 + if ($nonce) {
141 149 $success = false;
142 150
143 151 // Purge minify
144 152 $results = $this->minify_commands->purge_minify_cache();
145 - if ("caches cleared" == $results['result']) $success = true;
153 + if ("caches cleared" === $results['result']) $success = true;
146 154
147 155 // remove nonce from url and reload page.
148 - 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 +
149 160 exit;
150 161
151 162 }
152 163 }
@@ -205,9 +216,9 @@
205 216
206 217 /**
207 218 * Unschedule purging of the minify cache
208 219 *
209 - * @retrun void
220 + * @return void
210 221 */
211 222 private function unschedule_purge_old_cache_event() {
212 223 // old cache purge event cron
213 224 wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
@@ -218,9 +229,8 @@
218 229 *
219 230 * @return void
220 231 */
221 232 public function plugin_deactivate() {
222 - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
223 233 if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) {
224 234 WP_Optimize_Minify_Cache_Functions::purge_temp_files();
225 235 WP_Optimize_Minify_Cache_Functions::purge_old();
226 236 WP_Optimize_Minify_Cache_Functions::purge_others();
@@ -234,9 +244,8 @@
234 244 *
235 245 * @return void
236 246 */
237 247 public function plugin_uninstall() {
238 - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
239 248 // remove options from DB
240 249 if (!function_exists('wp_optimize_minify_config')) {
241 250 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php';
242 251 }
@@ -255,8 +264,15 @@
255 264 $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success');
256 265 }
257 266
258 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 + /**
259 275 * Show notification in WordPress admin.
260 276 *
261 277 * @param string $message HTML (no further escaping is performed)
262 278 * @param string $type error, warning, success, or info
@@ -270,10 +286,10 @@
270 286 window.addEventListener('load', function() {
271 287 (function(wp) {
272 288 if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) {
273 289 wp.data.dispatch('core/notices').createNotice(
274 - '<?php echo $type; ?>',
275 - '<?php echo $message; ?>',
290 + '<?php echo esc_js($type); ?>',
291 + '<?php echo wp_kses_post($message); ?>',
276 292 {
277 293 isDismissible: true,
278 294 }
279 295 );
@@ -281,10 +297,10 @@
281 297 })(window.wp);
282 298 });
283 299 </script>
284 300 <?php else : ?>
285 - <div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible">
286 - <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>
287 303 </div>
288 304 <?php
289 305 endif;
290 306 }
@@ -294,13 +310,7 @@
294 310 *
295 311 * @return bool
296 312 */
297 313 public function can_purge_cache() {
298 - $required_capability = is_multisite() ? 'manage_network_options' : 'manage_options';
299 -
300 - if (WP_Optimize::is_premium()) {
301 - return current_user_can($required_capability) || WP_Optimize_Premium()->can_purge_the_cache();
302 - } else {
303 - return current_user_can($required_capability);
304 - }
314 + return WP_Optimize_Utils::current_user_can_purge_cache();
305 315 }
306 316 }