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 +89 -34 3.2.9 → 4.7.0 View file →
@@ -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,21 @@
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 + /**
57 81 * Returns singleton instance object
58 82 *
59 83 * @return WP_Optimize_Minify Returns `WP_Optimize_Minify` object
60 84 */
@@ -72,13 +96,11 @@
72 96 * @param array $menu_items
73 97 * @return array
74 98 */
75 99 public function admin_bar_menu($menu_items) {
76 - $wpo_minify_options = wp_optimize_minify_config()->get();
77 -
78 - 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;
79 101
80 - $act_url = remove_query_arg('wpo_minify_cache_purged');
102 + $act_url = WP_Optimize_Utils::get_url_without_cache_purge_params();
81 103 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
82 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>';
83 105
84 106 $menu_items[] = array(
@@ -102,33 +124,40 @@
102 124 /**
103 125 * Check if purge single page action sent and purge cache.
104 126 */
105 127 public function handle_purge_minify_cache() {
106 - $wpo_minify_options = wp_optimize_minify_config()->get();
107 - if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return;
128 + if (!$this->enabled || !$this->can_purge_cache()) return;
108 129
109 - 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) {
110 132 if (is_admin()) {
111 - 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'));
112 137 return;
113 138 } else {
114 - $message = __('Minify cache purged', 'wp-optmize');
115 - 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();
116 142 return;
117 143 }
118 144 }
119 145
120 - 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');
121 147
122 - if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) {
148 + if ($nonce) {
123 149 $success = false;
124 150
125 151 // Purge minify
126 152 $results = $this->minify_commands->purge_minify_cache();
127 - if ("caches cleared" == $results['result']) $success = true;
153 + if ("caches cleared" === $results['result']) $success = true;
128 154
129 155 // remove nonce from url and reload page.
130 - 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 +
131 160 exit;
132 161
133 162 }
134 163 }
@@ -159,9 +188,9 @@
159 188 *
160 189 * @return void
161 190 */
162 191 private function load_premium() {
163 - $this->premium = new WP_Optimize_Minify_Premium();
192 + new WP_Optimize_Minify_Premium();
164 193 }
165 194
166 195 /**
167 196 * Run during activation
@@ -173,31 +202,42 @@
173 202 // increment cache time
174 203 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
175 204 WP_Optimize_Minify_Cache_Functions::cache_increment();
176 205 }
177 -
178 - // old cache purge event cron
179 - 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() {
180 212 if (!wp_next_scheduled('wpo_minify_purge_old_cache')) {
181 - wp_schedule_event(time() + 86400, 'daily', 'wpo_minify_purge_old_cache');
213 + wp_schedule_event(time() + 43200, 'daily', 'wpo_minify_purge_old_cache');
182 214 }
183 215 }
184 216
185 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 + /**
186 228 * Run during plugin deactivation
187 229 *
188 230 * @return void
189 231 */
190 232 public function plugin_deactivate() {
191 - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
192 - if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
233 + if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) {
193 234 WP_Optimize_Minify_Cache_Functions::purge_temp_files();
194 235 WP_Optimize_Minify_Cache_Functions::purge_old();
195 236 WP_Optimize_Minify_Cache_Functions::purge_others();
196 237 }
197 238
198 - // old cache purge event cron
199 - wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
239 + $this->unschedule_purge_old_cache_event();
200 240 }
201 241
202 242 /**
203 243 * Run during plugin uninstall
@@ -204,9 +244,8 @@
204 244 *
205 245 * @return void
206 246 */
207 247 public function plugin_uninstall() {
208 - if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
209 248 // remove options from DB
210 249 if (!function_exists('wp_optimize_minify_config')) {
211 250 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php';
212 251 }
@@ -225,8 +264,15 @@
225 264 $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success');
226 265 }
227 266
228 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 + /**
229 275 * Show notification in WordPress admin.
230 276 *
231 277 * @param string $message HTML (no further escaping is performed)
232 278 * @param string $type error, warning, success, or info
@@ -240,10 +286,10 @@
240 286 window.addEventListener('load', function() {
241 287 (function(wp) {
242 288 if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) {
243 289 wp.data.dispatch('core/notices').createNotice(
244 - '<?php echo $type; ?>',
245 - '<?php echo $message; ?>',
290 + '<?php echo esc_js($type); ?>',
291 + '<?php echo wp_kses_post($message); ?>',
246 292 {
247 293 isDismissible: true,
248 294 }
249 295 );
@@ -251,11 +297,20 @@
251 297 })(window.wp);
252 298 });
253 299 </script>
254 300 <?php else : ?>
255 - <div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible">
256 - <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>
257 303 </div>
258 304 <?php
259 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();
260 315 }
261 316 }