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

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