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

316 lines 8.6 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 // 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);
43
44 if (WP_Optimize::is_premium()) {
45 $this->load_premium();
46 }
47
48 /**
49 * Directory that stores the cache, including gzipped files and mobile specific cache
50 */
51 if (!defined('WPO_CACHE_MIN_FILES_DIR')) define('WPO_CACHE_MIN_FILES_DIR', untrailingslashit(WP_CONTENT_DIR).'/cache/wpo-minify');
52 if (!defined('WPO_CACHE_MIN_FILES_URL')) define('WPO_CACHE_MIN_FILES_URL', untrailingslashit(WP_CONTENT_URL).'/cache/wpo-minify');
53
54 if (!class_exists('WP_Optimize_Minify_Cache_Functions')) {
55 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-cache-functions.php';
56 }
57
58 $this->load_frontend();
59
60 // cron job to delete old wpo_min cache
61 add_action('wpo_minify_purge_old_cache', array('WP_Optimize_Minify_Cache_Functions', 'purge_old'));
62
63 add_action('init', array($this, 'schedule_or_unschedule_purge_old_cache_event'));
64
65 // Handle minify cache purging.
66 add_action('wp_loaded', array($this, 'handle_purge_minify_cache'));
67
68 }
69
70 /**
71 * Schedule or unschedule purge old cache event.
72 *
73 * @return void
74 */
75 public function schedule_or_unschedule_purge_old_cache_event() {
76 if ($this->enabled) {
77 $this->schedule_purge_old_cache_event();
78 } else {
79 $this->unschedule_purge_old_cache_event();
80 }
81 }
82
83 /**
84 * Returns singleton instance object
85 *
86 * @return WP_Optimize_Minify Returns `WP_Optimize_Minify` object
87 */
88 public static function instance() {
89 static $_instance = null;
90 if (null === $_instance) {
91 $_instance = new self();
92 }
93 return $_instance;
94 }
95
96 /**
97 * Admin toolbar processing
98 *
99 * @param array $menu_items
100 * @return array
101 */
102 public function admin_bar_menu($menu_items) {
103 $wpo_minify_options = wp_optimize_minify_config()->get();
104
105 if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options') || !($wpo_minify_options['enable_css'] || $wpo_minify_options['enable_js'])) return $menu_items;
106
107 $act_url = remove_query_arg('wpo_minify_cache_purged');
108 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
109 $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>';
110
111 $menu_items[] = array(
112 'id' => 'wpo_minify_cache_stats',
113 'title' => $cache_size_info,
114 'meta' => array(
115 'class' => 'wpo-cache-stats',
116 ),
117 'parent' => 'wpo_purge_cache',
118 );
119
120 $menu_items[] = array(
121 'parent' => 'wpo_purge_cache',
122 'id' => 'purge_minify_cache',
123 'title' => __('Purge minify cache', 'wp-optimize'),
124 'href' => add_query_arg('_wpo_purge_minify_cache', wp_create_nonce('wpo_purge_minify_cache'), $act_url),
125 );
126 return $menu_items;
127 }
128
129 /**
130 * Check if purge single page action sent and purge cache.
131 */
132 public function handle_purge_minify_cache() {
133 $wpo_minify_options = wp_optimize_minify_config()->get();
134 if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return;
135
136 if (isset($_GET['wpo_minify_cache_purged'])) {
137 if (is_admin()) {
138 add_action('admin_notices', array($this, 'notice_purge_minify_cache_success'));
139 return;
140 } else {
141 $message = __('Minify cache purged', 'wp-optimize');
142 printf('<script>alert("%s");</script>', $message);
143 return;
144 }
145 }
146
147 if (!isset($_GET['_wpo_purge_minify_cache'])) return;
148
149 if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) {
150 $success = false;
151
152 // Purge minify
153 $results = $this->minify_commands->purge_minify_cache();
154 if ("caches cleared" == $results['result']) $success = true;
155
156 // remove nonce from url and reload page.
157 wp_redirect(add_query_arg('wpo_minify_cache_purged', $success, remove_query_arg('_wpo_purge_minify_cache')));
158 exit;
159
160 }
161 }
162
163 /**
164 * Load the admin class
165 *
166 * @return void
167 */
168 private function load_admin() {
169 if (!is_admin()) return;
170 new WP_Optimize_Minify_Admin();
171 }
172
173 /**
174 * Load the frontend class
175 *
176 * @return void
177 */
178 private function load_frontend() {
179 if ($this->enabled) {
180 new WP_Optimize_Minify_Front_End();
181 }
182 }
183
184 /**
185 * Load the premium class
186 *
187 * @return void
188 */
189 private function load_premium() {
190 new WP_Optimize_Minify_Premium();
191 }
192
193 /**
194 * Run during activation
195 * Increment cache first as it will save files to that dir
196 *
197 * @return void
198 */
199 public function plugin_activate() {
200 // increment cache time
201 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
202 WP_Optimize_Minify_Cache_Functions::cache_increment();
203 }
204 }
205
206 /**
207 * If the WP cron event for scheduling purging of the minify cache does not exist, then create it
208 */
209 private function schedule_purge_old_cache_event() {
210 if (!wp_next_scheduled('wpo_minify_purge_old_cache')) {
211 wp_schedule_event(time() + 43200, 'daily', 'wpo_minify_purge_old_cache');
212 }
213 }
214
215 /**
216 * Unschedule purging of the minify cache
217 *
218 * @retrun void
219 */
220 private function unschedule_purge_old_cache_event() {
221 // old cache purge event cron
222 wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
223 }
224
225 /**
226 * Run during plugin deactivation
227 *
228 * @return void
229 */
230 public function plugin_deactivate() {
231 if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
232 if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) {
233 WP_Optimize_Minify_Cache_Functions::purge_temp_files();
234 WP_Optimize_Minify_Cache_Functions::purge_old();
235 WP_Optimize_Minify_Cache_Functions::purge_others();
236 }
237
238 $this->unschedule_purge_old_cache_event();
239 }
240
241 /**
242 * Run during plugin uninstall
243 *
244 * @return void
245 */
246 public function plugin_uninstall() {
247 if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
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 * Show notification in WordPress admin.
269 *
270 * @param string $message HTML (no further escaping is performed)
271 * @param string $type error, warning, success, or info
272 */
273 public function show_notice($message, $type) {
274 global $current_screen;
275
276 if ($current_screen && is_callable(array($current_screen, 'is_block_editor')) && $current_screen->is_block_editor()) :
277 ?>
278 <script>
279 window.addEventListener('load', function() {
280 (function(wp) {
281 if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) {
282 wp.data.dispatch('core/notices').createNotice(
283 '<?php echo $type; ?>',
284 '<?php echo $message; ?>',
285 {
286 isDismissible: true,
287 }
288 );
289 }
290 })(window.wp);
291 });
292 </script>
293 <?php else : ?>
294 <div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible">
295 <p><?php echo $message; ?></p>
296 </div>
297 <?php
298 endif;
299 }
300
301 /**
302 * Check if current user can purge cache.
303 *
304 * @return bool
305 */
306 public function can_purge_cache() {
307 $required_capability = is_multisite() ? 'manage_network_options' : 'manage_options';
308
309 if (WP_Optimize::is_premium()) {
310 return current_user_can($required_capability) || WP_Optimize_Premium()->can_purge_the_cache();
311 } else {
312 return current_user_can($required_capability);
313 }
314 }
315 }
316