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

249 lines 7.3 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 * Admin toolbar processing
58 *
59 * @param array $menu_items
60 * @return array
61 */
62 public function admin_bar_menu($menu_items) {
63 $wpo_minify_options = wp_optimize_minify_config()->get();
64
65 if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options') || !($wpo_minify_options['enable_css'] || $wpo_minify_options['enable_js'])) return $menu_items;
66
67 $act_url = remove_query_arg('wpo_minify_cache_purged');
68 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
69 $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>';
70
71 $menu_items[] = array(
72 'id' => 'wpo_minify_cache_stats',
73 'title' => $cache_size_info,
74 'meta' => array(
75 'class' => 'wpo-cache-stats',
76 ),
77 'parent' => 'wpo_purge_cache',
78 );
79
80 $menu_items[] = array(
81 'parent' => 'wpo_purge_cache',
82 'id' => 'purge_minify_cache',
83 'title' => __('Purge minify cache', 'wp-optimize'),
84 'href' => add_query_arg('_wpo_purge_minify_cache', wp_create_nonce('wpo_purge_minify_cache'), $act_url),
85 );
86 return $menu_items;
87 }
88
89 /**
90 * Check if purge single page action sent and purge cache.
91 */
92 public function handle_purge_minify_cache() {
93 $wpo_minify_options = wp_optimize_minify_config()->get();
94 if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return;
95
96 if (isset($_GET['wpo_minify_cache_purged'])) {
97 if (is_admin()) {
98 add_action('admin_notices', array($this, 'notice_purge_minify_cache_success'));
99 return;
100 } else {
101 $message = __('Minify cache purged', 'wp-optmize');
102 printf('<script>alert("%s");</script>', $message);
103 return;
104 }
105 }
106
107 if (!isset($_GET['_wpo_purge_minify_cache'])) return;
108
109 if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) {
110 $success = false;
111
112 // Purge minify
113 $results = $this->minify_commands->purge_minify_cache();
114 if ("caches cleared" == $results['result']) $success = true;
115
116 // remove nonce from url and reload page.
117 wp_redirect(add_query_arg('wpo_minify_cache_purged', $success, remove_query_arg('_wpo_purge_minify_cache')));
118 exit;
119
120 }
121 }
122
123 /**
124 * Load the admin class
125 *
126 * @return void
127 */
128 private function load_admin() {
129 if (!is_admin()) return;
130 new WP_Optimize_Minify_Admin();
131 }
132
133 /**
134 * Load the frontend class
135 *
136 * @return void
137 */
138 private function load_frontend() {
139 if ($this->enabled) {
140 new WP_Optimize_Minify_Front_End();
141 }
142 }
143
144 /**
145 * Load the premium class
146 *
147 * @return void
148 */
149 private function load_premium() {
150 $this->premium = new WP_Optimize_Minify_Premium();
151 }
152
153 /**
154 * Run during activation
155 * Increment cache first as it will save files to that dir
156 *
157 * @return void
158 */
159 public function plugin_activate() {
160 // increment cache time
161 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
162 WP_Optimize_Minify_Cache_Functions::cache_increment();
163 }
164
165 // old cache purge event cron
166 wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
167 if (!wp_next_scheduled('wpo_minify_purge_old_cache')) {
168 wp_schedule_event(time() + 86400, 'daily', 'wpo_minify_purge_old_cache');
169 }
170 }
171
172 /**
173 * Run during plugin deactivation
174 *
175 * @return void
176 */
177 public function plugin_deactivate() {
178 if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
179 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
180 WP_Optimize_Minify_Cache_Functions::purge_temp_files();
181 WP_Optimize_Minify_Cache_Functions::purge_old();
182 WP_Optimize_Minify_Cache_Functions::purge_others();
183 }
184
185 // old cache purge event cron
186 wp_clear_scheduled_hook('wpo_minify_purge_old_cache');
187 }
188
189 /**
190 * Run during plugin uninstall
191 *
192 * @return void
193 */
194 public function plugin_uninstall() {
195 if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return;
196 // remove options from DB
197 if (!function_exists('wp_optimize_minify_config')) {
198 include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php';
199 }
200 wp_optimize_minify_config()->purge();
201 // remove minified files
202 if (class_exists('WP_Optimize_Minify_Cache_Functions')) {
203 WP_Optimize_Minify_Cache_Functions::purge();
204 WP_Optimize_Minify_Cache_Functions::purge_others();
205 }
206 }
207
208 /**
209 * Shows success notice for purge minify cache
210 */
211 public function notice_purge_minify_cache_success() {
212 $this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success');
213 }
214
215 /**
216 * Show notification in WordPress admin.
217 *
218 * @param string $message HTML (no further escaping is performed)
219 * @param string $type error, warning, success, or info
220 */
221 public function show_notice($message, $type) {
222 global $current_screen;
223
224 if ($current_screen && is_callable(array($current_screen, 'is_block_editor')) && $current_screen->is_block_editor()) :
225 ?>
226 <script>
227 window.addEventListener('load', function() {
228 (function(wp) {
229 if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) {
230 wp.data.dispatch('core/notices').createNotice(
231 '<?php echo $type; ?>',
232 '<?php echo $message; ?>',
233 {
234 isDismissible: true,
235 }
236 );
237 }
238 })(window.wp);
239 });
240 </script>
241 <?php else : ?>
242 <div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible">
243 <p><?php echo $message; ?></p>
244 </div>
245 <?php
246 endif;
247 }
248 }
249