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

317 lines 8.8 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'] || !WP_Optimize()->current_user_can('manage_options') || !($wpo_minify_options['enable_css'] || $wpo_minify_options['enable_js'])) return $menu_items;
103
104 $act_url = WP_Optimize_Utils::get_url_without_cache_purge_params();
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'] || !WP_Optimize()->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 $notice_function = 'notice_purge_minify_cache_' . ('false' === $is_cache_purged ? 'failure' : 'success');
137 add_action('admin_notices', array($this, $notice_function));
138 return;
139 } else {
140 $message = 'false' === $is_cache_purged ? __('Failed to purge the minify cache.', 'wp-optimize') : __('Minify cache purged', 'wp-optimize');
141 printf('<script>window.onload = function() {alert("%s");}</script>', esc_js($message));
142 return;
143 }
144 }
145
146 $nonce = TeamUpdraft\WP_Optimize\Includes\Fragments\verify_nonce('_wpo_purge_minify_cache', 'wpo_purge_minify_cache');
147
148 if ($nonce) {
149 $success = false;
150
151 // Purge minify
152 $results = $this->minify_commands->purge_minify_cache();
153 if ("caches cleared" === $results['result']) $success = true;
154
155 // remove nonce from url and reload page.
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
160 exit;
161
162 }
163 }
164
165 /**
166 * Load the admin class
167 *
168 * @return void
169 */
170 private function load_admin() {
171 if (!is_admin()) return;
172 new WP_Optimize_Minify_Admin();
173 }
174
175 /**
176 * Load the frontend class
177 *
178 * @return void
179 */
180 private function load_frontend() {
181 if ($this->enabled) {
182 new WP_Optimize_Minify_Front_End();
183 }
184 }
185
186 /**
187 * Load the premium class
188 *
189 * @return void
190 */
191 private function load_premium() {
192 new WP_Optimize_Minify_Premium();
193 }
194
195 /**
196 * Run during activation
197 * Increment cache first as it will save files to that dir
198 *
199 * @return void
200 */
201 public function plugin_activate() {
202 // increment cache time
203 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
204 WP_Optimize_Minify_Cache_Functions::cache_increment();
205 }
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() {
212 if (!wp_next_scheduled('wpo_minify_purge_old_cache')) {
213 wp_schedule_event(time() + 43200, 'daily', 'wpo_minify_purge_old_cache');
214 }
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 }
226
227 /**
228 * Run during plugin deactivation
229 *
230 * @return void
231 */
232 public function plugin_deactivate() {
233 if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) {
234 WP_Optimize_Minify_Cache_Functions::purge_temp_files();
235 WP_Optimize_Minify_Cache_Functions::purge_old();
236 WP_Optimize_Minify_Cache_Functions::purge_others();
237 }
238
239 $this->unschedule_purge_old_cache_event();
240 }
241
242 /**
243 * Run during plugin uninstall
244 *
245 * @return void
246 */
247 public function plugin_uninstall() {
248 // remove options from DB
249 if (!function_exists('wp_optimize_minify_config')) {
250 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php';
251 }
252 wp_optimize_minify_config()->purge();
253 // remove minified files
254 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
255 WP_Optimize_Minify_Cache_Functions::purge();
256 WP_Optimize_Minify_Cache_Functions::purge_others();
257 }
258 }
259
260 /**
261 * Shows success notice for purge minify cache
262 */
263 public function notice_purge_minify_cache_success() {
264 $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success');
265 }
266
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 /**
275 * Show notification in WordPress admin.
276 *
277 * @param string $message HTML (no further escaping is performed)
278 * @param string $type error, warning, success, or info
279 */
280 public function show_notice($message, $type) {
281 global $current_screen;
282
283 if ($current_screen && is_callable(array($current_screen, 'is_block_editor')) && $current_screen->is_block_editor()) :
284 ?>
285 <script>
286 window.addEventListener('load', function() {
287 (function(wp) {
288 if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) {
289 wp.data.dispatch('core/notices').createNotice(
290 '<?php echo esc_js($type); ?>',
291 '<?php echo wp_kses_post($message); ?>',
292 {
293 isDismissible: true,
294 }
295 );
296 }
297 })(window.wp);
298 });
299 </script>
300 <?php else : ?>
301 <div class="notice wpo-notice notice-<?php echo esc_attr($type); ?> is-dismissible">
302 <p><?php echo wp_kses_post($message); ?></p>
303 </div>
304 <?php
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();
315 }
316 }
317