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 +57 -40 3.2.21 → 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,11 +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 - if ($this->enabled) {
64 - $this->schedule_purge_old_cache_event();
65 - }
60 + add_action('wp_loaded', array($this, 'schedule_or_unschedule_purge_old_cache_event'));
66 61
67 62 // Handle minify cache purging.
68 63 add_action('wp_loaded', array($this, 'handle_purge_minify_cache'));
69 64
@@ -69,8 +64,21 @@
69 64
70 65 }
71 66
72 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 + /**
73 81 * Returns singleton instance object
74 82 *
75 83 * @return WP_Optimize_Minify Returns `WP_Optimize_Minify` object
76 84 */
@@ -88,13 +96,11 @@
88 96 * @param array $menu_items
89 97 * @return array
90 98 */
91 99 public function admin_bar_menu($menu_items) {
92 - $wpo_minify_options = wp_optimize_minify_config()->get();
93 -
94 - 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;
95 101
96 - $act_url = remove_query_arg('wpo_minify_cache_purged');
102 + $act_url = WP_Optimize_Utils::get_url_without_cache_purge_params();
97 103 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
98 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>';
99 105
100 106 $menu_items[] = array(
@@ -118,33 +124,40 @@
118 124 /**
119 125 * Check if purge single page action sent and purge cache.
120 126 */
121 127 public function handle_purge_minify_cache() {
122 - $wpo_minify_options = wp_optimize_minify_config()->get();
123 - if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return;
128 + if (!$this->enabled || !$this->can_purge_cache()) return;
124 129
125 - 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) {
126 132 if (is_admin()) {
127 - 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'));
128 137 return;
129 138 } else {
130 - $message = __('Minify cache purged', 'wp-optimize');
131 - 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();
132 142 return;
133 143 }
134 144 }
135 145
136 - 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');
137 147
138 - if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) {
148 + if ($nonce) {
139 149 $success = false;
140 150
141 151 // Purge minify
142 152 $results = $this->minify_commands->purge_minify_cache();
143 - if ("caches cleared" == $results['result']) $success = true;
153 + if ("caches cleared" === $results['result']) $success = true;
144 154
145 155 // remove nonce from url and reload page.
146 - 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 +
147 160 exit;
148 161
149 162 }
150 163 }
@@ -189,12 +202,8 @@
189 202 // increment cache time
190 203 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
191 204 WP_Optimize_Minify_Cache_Functions::cache_increment();
192 205 }
193 -
194 - // old cache purge event cron
195 - wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
196 - $this->schedule_purge_old_cache_event();
197 206 }
198 207
199 208 /**
200 209 * If the WP cron event for scheduling purging of the minify cache does not exist, then create it
@@ -203,8 +212,18 @@
203 212 if (!wp_next_scheduled('wpo_minify_purge_old_cache')) {
204 213 wp_schedule_event(time() + 43200, 'daily', 'wpo_minify_purge_old_cache');
205 214 }
206 215 }
216 +
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 + }
207 226
208 227 /**
209 228 * Run during plugin deactivation
210 229 *
@@ -210,9 +229,8 @@
210 229 *
211 230 * @return void
212 231 */
213 232 public function plugin_deactivate() {
214 - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
215 233 if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) {
216 234 WP_Optimize_Minify_Cache_Functions::purge_temp_files();
217 235 WP_Optimize_Minify_Cache_Functions::purge_old();
218 236 WP_Optimize_Minify_Cache_Functions::purge_others();
@@ -217,10 +235,9 @@
217 235 WP_Optimize_Minify_Cache_Functions::purge_old();
218 236 WP_Optimize_Minify_Cache_Functions::purge_others();
219 237 }
220 238
221 - // old cache purge event cron
222 - wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
239 + $this->unschedule_purge_old_cache_event();
223 240 }
224 241
225 242 /**
226 243 * Run during plugin uninstall
@@ -227,9 +244,8 @@
227 244 *
228 245 * @return void
229 246 */
230 247 public function plugin_uninstall() {
231 - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
232 248 // remove options from DB
233 249 if (!function_exists('wp_optimize_minify_config')) {
234 250 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php';
235 251 }
@@ -248,8 +264,15 @@
248 264 $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success');
249 265 }
250 266
251 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 + /**
252 275 * Show notification in WordPress admin.
253 276 *
254 277 * @param string $message HTML (no further escaping is performed)
255 278 * @param string $type error, warning, success, or info
@@ -263,10 +286,10 @@
263 286 window.addEventListener('load', function() {
264 287 (function(wp) {
265 288 if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) {
266 289 wp.data.dispatch('core/notices').createNotice(
267 - '<?php echo $type; ?>',
268 - '<?php echo $message; ?>',
290 + '<?php echo esc_js($type); ?>',
291 + '<?php echo wp_kses_post($message); ?>',
269 292 {
270 293 isDismissible: true,
271 294 }
272 295 );
@@ -274,10 +297,10 @@
274 297 })(window.wp);
275 298 });
276 299 </script>
277 300 <?php else : ?>
278 - <div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible">
279 - <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>
280 303 </div>
281 304 <?php
282 305 endif;
283 306 }
@@ -287,13 +310,7 @@
287 310 *
288 311 * @return bool
289 312 */
290 313 public function can_purge_cache() {
291 - $required_capability = is_multisite() ? 'manage_network_options' : 'manage_options';
292 -
293 - if (WP_Optimize::is_premium()) {
294 - return current_user_can($required_capability) || WP_Optimize_Premium()->can_purge_the_cache();
295 - } else {
296 - return current_user_can($required_capability);
297 - }
314 + return WP_Optimize_Utils::current_user_can_purge_cache();
298 315 }
299 316 }