PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.2
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
wp-optimize / minify / class-wp-optimize-minify.php

class-wp-optimize-minify.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.2, at minify/class-wp-optimize-minify.php

312 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('No direct access allowed');
3
4 define('WP_OPTIMIZE_MINIFY_VERSION', '2.6.5');
5 define('WP_OPTIMIZE_MINIFY_DIR', dirname(__FILE__));
6 if (!defined('WP_OPTIMIZE_SHOW_MINIFY_ADVANCED')) define('WP_OPTIMIZE_SHOW_MINIFY_ADVANCED', false);
7
8 class WP_Optimize_Minify {
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 /**
23 * Constructor - Initialize actions and filters
24 *
25 * @return void
26 */
27 public function __construct() {
28
29 $this->minify_commands = new WP_Optimize_Minify_Commands();
30
31 if (!class_exists('WP_Optimize_Minify_Config')) {
32 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php';
33 }
34
35 $this->enabled = wp_optimize_minify_config()->is_enabled();
36
37 $this->load_admin();
38
39 add_filter('wpo_cache_admin_bar_menu_items', array($this, 'admin_bar_menu'), 30);
40
41 if (WP_Optimize::is_premium()) {
42 $this->load_premium();
43 }
44
45 /**
46 * Directory that stores the cache, including gzipped files and mobile specific cache
47 */
48 if (!defined('WPO_CACHE_MIN_FILES_DIR')) define('WPO_CACHE_MIN_FILES_DIR', untrailingslashit(WP_CONTENT_DIR).'/cache/wpo-minify');
49 if (!defined('WPO_CACHE_MIN_FILES_URL')) define('WPO_CACHE_MIN_FILES_URL', untrailingslashit(WP_CONTENT_URL).'/cache/wpo-minify');
50
51 if (!class_exists('WP_Optimize_Minify_Cache_Functions')) {
52 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-cache-functions.php';
53 }
54
55 $this->load_frontend();
56
57 // cron job to delete old wpo_min cache
58 add_action('wpo_minify_purge_old_cache', array('WP_Optimize_Minify_Cache_Functions', 'purge_old'));
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() {
73 if ($this->enabled) {
74 $this->schedule_purge_old_cache_event();
75 } else {
76 $this->unschedule_purge_old_cache_event();
77 }
78 }
79
80 /**
81 * Returns singleton instance object
82 *
83 * @return WP_Optimize_Minify Returns `WP_Optimize_Minify` object
84 */
85 public static function instance() {
86 static $_instance = null;
87 if (null === $_instance) {
88 $_instance = new self();
89 }
90 return $_instance;
91 }
92
93 /**
94 * Admin toolbar processing
95 *
96 * @param array $menu_items
97 * @return array
98 */
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;
103
104 $act_url = remove_query_arg('wpo_minify_cache_purged');
105 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
106 $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
108 $menu_items[] = array(
109 'id' => 'wpo_minify_cache_stats',
110 'title' => $cache_size_info,
111 'meta' => array(
112 'class' => 'wpo-cache-stats',
113 ),
114 'parent' => 'wpo_purge_cache',
115 );
116
117 $menu_items[] = array(
118 'parent' => 'wpo_purge_cache',
119 'id' => 'purge_minify_cache',
120 'title' => __('Purge minify cache', 'wp-optimize'),
121 'href' => add_query_arg('_wpo_purge_minify_cache', wp_create_nonce('wpo_purge_minify_cache'), $act_url),
122 );
123 return $menu_items;
124 }
125
126 /**
127 * Check if purge single page action sent and purge cache.
128 */
129 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;
132
133 $is_cache_purged = TeamUpdraft\WP_Optimize\Includes\Fragments\fetch_superglobal('get', 'wpo_minify_cache_purged');
134 if (null !== $is_cache_purged) {
135 if (is_admin()) {
136 add_action('admin_notices', array($this, 'notice_purge_minify_cache_success'));
137 return;
138 } else {
139 $message = __('Minify cache purged', 'wp-optimize');
140 printf('<script>alert("%s");</script>', esc_html($message));
141 return;
142 }
143 }
144
145 $nonce = TeamUpdraft\WP_Optimize\Includes\Fragments\verify_nonce('_wpo_purge_minify_cache', 'wpo_purge_minify_cache');
146
147 if ($nonce) {
148 $success = false;
149
150 // Purge minify
151 $results = $this->minify_commands->purge_minify_cache();
152 if ("caches cleared" === $results['result']) $success = true;
153
154 // 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 exit;
157
158 }
159 }
160
161 /**
162 * Load the admin class
163 *
164 * @return void
165 */
166 private function load_admin() {
167 if (!is_admin()) return;
168 new WP_Optimize_Minify_Admin();
169 }
170
171 /**
172 * Load the frontend class
173 *
174 * @return void
175 */
176 private function load_frontend() {
177 if ($this->enabled) {
178 new WP_Optimize_Minify_Front_End();
179 }
180 }
181
182 /**
183 * Load the premium class
184 *
185 * @return void
186 */
187 private function load_premium() {
188 new WP_Optimize_Minify_Premium();
189 }
190
191 /**
192 * Run during activation
193 * Increment cache first as it will save files to that dir
194 *
195 * @return void
196 */
197 public function plugin_activate() {
198 // increment cache time
199 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
200 WP_Optimize_Minify_Cache_Functions::cache_increment();
201 }
202 }
203
204 /**
205 * If the WP cron event for scheduling purging of the minify cache does not exist, then create it
206 */
207 private function schedule_purge_old_cache_event() {
208 if (!wp_next_scheduled('wpo_minify_purge_old_cache')) {
209 wp_schedule_event(time() + 43200, 'daily', 'wpo_minify_purge_old_cache');
210 }
211 }
212
213 /**
214 * Unschedule purging of the minify cache
215 *
216 * @return void
217 */
218 private function unschedule_purge_old_cache_event() {
219 // old cache purge event cron
220 wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
221 }
222
223 /**
224 * Run during plugin deactivation
225 *
226 * @return void
227 */
228 public function plugin_deactivate() {
229 if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) {
230 WP_Optimize_Minify_Cache_Functions::purge_temp_files();
231 WP_Optimize_Minify_Cache_Functions::purge_old();
232 WP_Optimize_Minify_Cache_Functions::purge_others();
233 }
234
235 $this->unschedule_purge_old_cache_event();
236 }
237
238 /**
239 * Run during plugin uninstall
240 *
241 * @return void
242 */
243 public function plugin_uninstall() {
244 // remove options from DB
245 if (!function_exists('wp_optimize_minify_config')) {
246 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php';
247 }
248 wp_optimize_minify_config()->purge();
249 // remove minified files
250 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
251 WP_Optimize_Minify_Cache_Functions::purge();
252 WP_Optimize_Minify_Cache_Functions::purge_others();
253 }
254 }
255
256 /**
257 * Shows success notice for purge minify cache
258 */
259 public function notice_purge_minify_cache_success() {
260 $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success');
261 }
262
263 /**
264 * Show notification in WordPress admin.
265 *
266 * @param string $message HTML (no further escaping is performed)
267 * @param string $type error, warning, success, or info
268 */
269 public function show_notice($message, $type) {
270 global $current_screen;
271
272 if ($current_screen && is_callable(array($current_screen, 'is_block_editor')) && $current_screen->is_block_editor()) :
273 ?>
274 <script>
275 window.addEventListener('load', function() {
276 (function(wp) {
277 if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) {
278 wp.data.dispatch('core/notices').createNotice(
279 '<?php echo esc_js($type); ?>',
280 '<?php echo wp_kses_post($message); ?>',
281 {
282 isDismissible: true,
283 }
284 );
285 }
286 })(window.wp);
287 });
288 </script>
289 <?php else : ?>
290 <div class="notice wpo-notice notice-<?php echo esc_attr($type); ?> is-dismissible">
291 <p><?php echo wp_kses_post($message); ?></p>
292 </div>
293 <?php
294 endif;
295 }
296
297 /**
298 * Check if current user can purge cache.
299 *
300 * @return bool
301 */
302 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 }
310 }
311 }
312