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

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