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

262 lines 7.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 * Constructor - Initialize actions and filters
11 *
12 * @return void
13 */
14 public function __construct() {
15
16 $this->minify_commands = new WP_Optimize_Minify_Commands();
17
18 if (!class_exists('WP_Optimize_Minify_Config')) {
19 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php';
20 }
21
22 $this->enabled = wp_optimize_minify_config()->is_enabled();
23
24 $this->load_admin();
25
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);
30
31 if (WP_Optimize::is_premium()) {
32 $this->load_premium();
33 }
34
35 /**
36 * Directory that stores the cache, including gzipped files and mobile specifc cache
37 */
38 if (!defined('WPO_CACHE_MIN_FILES_DIR')) define('WPO_CACHE_MIN_FILES_DIR', untrailingslashit(WP_CONTENT_DIR).'/cache/wpo-minify');
39 if (!defined('WPO_CACHE_MIN_FILES_URL')) define('WPO_CACHE_MIN_FILES_URL', untrailingslashit(WP_CONTENT_URL).'/cache/wpo-minify');
40
41 if (!class_exists('WP_Optimize_Minify_Cache_Functions')) {
42 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-cache-functions.php';
43 }
44
45 $this->load_frontend();
46
47 // cron job to delete old wpo_min cache
48 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
50
51 // Handle minify cache purging.
52 add_action('wp_loaded', array($this, 'handle_purge_minify_cache'));
53
54 }
55
56 /**
57 * Returns singleton instance object
58 *
59 * @return WP_Optimize_Minify Returns `WP_Optimize_Minify` object
60 */
61 public static function instance() {
62 static $_instance = null;
63 if (null === $_instance) {
64 $_instance = new self();
65 }
66 return $_instance;
67 }
68
69 /**
70 * Admin toolbar processing
71 *
72 * @param array $menu_items
73 * @return array
74 */
75 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;
79
80 $act_url = remove_query_arg('wpo_minify_cache_purged');
81 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
82 $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
84 $menu_items[] = array(
85 'id' => 'wpo_minify_cache_stats',
86 'title' => $cache_size_info,
87 'meta' => array(
88 'class' => 'wpo-cache-stats',
89 ),
90 'parent' => 'wpo_purge_cache',
91 );
92
93 $menu_items[] = array(
94 'parent' => 'wpo_purge_cache',
95 'id' => 'purge_minify_cache',
96 'title' => __('Purge minify cache', 'wp-optimize'),
97 'href' => add_query_arg('_wpo_purge_minify_cache', wp_create_nonce('wpo_purge_minify_cache'), $act_url),
98 );
99 return $menu_items;
100 }
101
102 /**
103 * Check if purge single page action sent and purge cache.
104 */
105 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;
108
109 if (isset($_GET['wpo_minify_cache_purged'])) {
110 if (is_admin()) {
111 add_action('admin_notices', array($this, 'notice_purge_minify_cache_success'));
112 return;
113 } else {
114 $message = __('Minify cache purged', 'wp-optmize');
115 printf('<script>alert("%s");</script>', $message);
116 return;
117 }
118 }
119
120 if (!isset($_GET['_wpo_purge_minify_cache'])) return;
121
122 if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) {
123 $success = false;
124
125 // Purge minify
126 $results = $this->minify_commands->purge_minify_cache();
127 if ("caches cleared" == $results['result']) $success = true;
128
129 // 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')));
131 exit;
132
133 }
134 }
135
136 /**
137 * Load the admin class
138 *
139 * @return void
140 */
141 private function load_admin() {
142 if (!is_admin()) return;
143 new WP_Optimize_Minify_Admin();
144 }
145
146 /**
147 * Load the frontend class
148 *
149 * @return void
150 */
151 private function load_frontend() {
152 if ($this->enabled) {
153 new WP_Optimize_Minify_Front_End();
154 }
155 }
156
157 /**
158 * Load the premium class
159 *
160 * @return void
161 */
162 private function load_premium() {
163 $this->premium = new WP_Optimize_Minify_Premium();
164 }
165
166 /**
167 * Run during activation
168 * Increment cache first as it will save files to that dir
169 *
170 * @return void
171 */
172 public function plugin_activate() {
173 // increment cache time
174 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
175 WP_Optimize_Minify_Cache_Functions::cache_increment();
176 }
177
178 // old cache purge event cron
179 wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
180 if (!wp_next_scheduled('wpo_minify_purge_old_cache')) {
181 wp_schedule_event(time() + 86400, 'daily', 'wpo_minify_purge_old_cache');
182 }
183 }
184
185 /**
186 * Run during plugin deactivation
187 *
188 * @return void
189 */
190 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')) {
193 WP_Optimize_Minify_Cache_Functions::purge_temp_files();
194 WP_Optimize_Minify_Cache_Functions::purge_old();
195 WP_Optimize_Minify_Cache_Functions::purge_others();
196 }
197
198 // old cache purge event cron
199 wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
200 }
201
202 /**
203 * Run during plugin uninstall
204 *
205 * @return void
206 */
207 public function plugin_uninstall() {
208 if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
209 // remove options from DB
210 if (!function_exists('wp_optimize_minify_config')) {
211 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php';
212 }
213 wp_optimize_minify_config()->purge();
214 // remove minified files
215 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
216 WP_Optimize_Minify_Cache_Functions::purge();
217 WP_Optimize_Minify_Cache_Functions::purge_others();
218 }
219 }
220
221 /**
222 * Shows success notice for purge minify cache
223 */
224 public function notice_purge_minify_cache_success() {
225 $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success');
226 }
227
228 /**
229 * Show notification in WordPress admin.
230 *
231 * @param string $message HTML (no further escaping is performed)
232 * @param string $type error, warning, success, or info
233 */
234 public function show_notice($message, $type) {
235 global $current_screen;
236
237 if ($current_screen && is_callable(array($current_screen, 'is_block_editor')) && $current_screen->is_block_editor()) :
238 ?>
239 <script>
240 window.addEventListener('load', function() {
241 (function(wp) {
242 if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) {
243 wp.data.dispatch('core/notices').createNotice(
244 '<?php echo $type; ?>',
245 '<?php echo $message; ?>',
246 {
247 isDismissible: true,
248 }
249 );
250 }
251 })(window.wp);
252 });
253 </script>
254 <?php else : ?>
255 <div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible">
256 <p><?php echo $message; ?></p>
257 </div>
258 <?php
259 endif;
260 }
261 }
262