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-commands.php

class-wp-optimize-minify-commands.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.3, at minify/class-wp-optimize-minify-commands.php

211 lines 6.2 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 if (!class_exists('WP_Optimize_Minify_Config')) require_once(dirname(__FILE__) . '/class-wp-optimize-minify-config.php');
5 if (!class_exists('WP_Optimize_Minify_Preloader')) require_once(dirname(__FILE__) . '/class-wpo-minify-preloader.php');
6
7 /**
8 * All cache commands that are intended to be available for calling from any sort of control interface (e.g. wp-admin, UpdraftCentral) go in here. All public methods should either return the data to be returned, or a WP_Error with associated error code, message and error data.
9 */
10 class WP_Optimize_Minify_Commands {
11
12 /**
13 * List all cache files
14 *
15 * @param array $data - The $_POST data
16 * @return array
17 */
18 public function get_minify_cached_files($data = array()) {
19 if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize'));
20 $stamp = isset($data['stamp']) ? $data['stamp'] : 0;
21 $files = WP_Optimize_Minify_Cache_Functions::get_cached_files($stamp, false);
22 $files['js'] = array_map(array('WP_Optimize_Minify_Cache_Functions', 'format_file_logs'), $files['js']);
23 $files['css'] = array_map(array('WP_Optimize_Minify_Cache_Functions', 'format_file_logs'), $files['css']);
24 return $files;
25 }
26
27 /**
28 * Removes the entire cache dir.
29 * Use with caution, as cached html may still reference those files.
30 *
31 * @return array
32 */
33 public function purge_all_minify_cache() {
34 if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize'));
35 WP_Optimize_Minify_Cache_Functions::purge();
36 WP_Optimize_Minify_Cache_Functions::cache_increment();
37 $others = WP_Optimize_Minify_Cache_Functions::purge_others();
38 $files = $this->get_minify_cached_files();
39 $message = array(
40 __('The minification cache was deleted.', 'wp-optimize'),
41 strip_tags($others, '<strong>'),
42 );
43 $message = array_filter($message);
44 return array(
45 'success' => true,
46 'message' => implode("\n", $message),
47 'files' => $files
48 );
49 }
50
51 /**
52 * Forces a new Cache to be built
53 *
54 * @return array
55 */
56 public function minify_increment_cache() {
57 if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize'));
58 WP_Optimize_Minify_Cache_Functions::cache_increment();
59 $files = $this->get_minify_cached_files();
60 return array(
61 'success' => true,
62 'files' => $files
63 );
64 }
65
66 /**
67 * Purge the cache
68 *
69 * @return array
70 */
71 public function purge_minify_cache() {
72 if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize'));
73 if (!WP_Optimize()->can_purge_the_cache()) return array('error' => __('You do not have permission to purge the cache', 'wp-optimize'));
74
75 // deletes temp files and old caches incase CRON isn't working
76 WP_Optimize_Minify_Cache_Functions::cache_increment();
77 if (wp_optimize_minify_config()->always_purge_everything()) {
78 WP_Optimize_Minify_Cache_Functions::purge();
79 $state = array();
80 $old = array();
81 } else {
82 $state = WP_Optimize_Minify_Cache_Functions::purge_temp_files();
83 $old = WP_Optimize_Minify_Cache_Functions::purge_old();
84 }
85 $others = WP_Optimize_Minify_Cache_Functions::purge_others();
86 $files = $this->get_minify_cached_files();
87
88 $notice = array(
89 __('All caches from WP-Optimize Minify have been purged.', 'wp-optimize'),
90 strip_tags($others, '<strong>'),
91 );
92 $notice = array_filter($notice);
93 $notice = json_encode($notice); // encode
94
95 return array(
96 'result' => 'caches cleared',
97 'others' => $others,
98 'state' => $state,
99 'message' => $notice,
100 'old' => $old,
101 'files' => $files
102 );
103 }
104
105 /**
106 * Save options to the config
107 *
108 * @param array $data
109 * @return array
110 */
111 public function save_minify_settings($data) {
112
113 $new_data = array();
114 foreach ($data as $key => $value) {
115 if ('true' === $value) {
116 $new_data[$key] = true;
117 } elseif ('false' === $value) {
118 $new_data[$key] = false;
119 } else {
120 $new_data[$key] = $value;
121 }
122 }
123
124 if (isset($data['minify_advanced_tab'])) {
125 // Make sure that empty settings are still saved
126 if (!isset($new_data['ignore_list'])) $new_data['ignore_list'] = array();
127 if (!isset($new_data['blacklist'])) $new_data['blacklist'] = array();
128 }
129
130 /**
131 * Filters the data before saving it
132 *
133 * @param array $new_data - The original data
134 * @return array The data, altered or not
135 */
136 $new_data = apply_filters('wpo_save_minify_settings', $new_data);
137
138 if (!class_exists('WP_Optimize_Minify_Config')) return array(
139 'success' => false,
140 'message' => "WP_Optimize_Minify_Config class doesn't exist",
141 );
142 $working = wp_optimize_minify_config()->update($new_data);
143 if (!$working) {
144 return array(
145 'success' => false,
146 'error' => 'failed to save'
147 );
148 }
149 $purged = $this->purge_minify_cache();
150 return array(
151 'success' => true,
152 'files' => $purged['files']
153 );
154 }
155
156 /**
157 * Hide the information notice for the current user
158 *
159 * @return array
160 */
161 public function hide_minify_notice() {
162 return array(
163 'success' => update_user_meta(get_current_user_id(), 'wpo-hide-minify-information-notice', true)
164 );
165 }
166
167 /**
168 * Get the current status
169 *
170 * @return array
171 */
172 public function get_status() {
173 $config = wp_optimize_minify_config()->get();
174 return array(
175 'enabled' => $config['enabled'],
176 'js' => $config['enable_js'],
177 'css' => $config['enable_css'],
178 'html' => $config['html_minification'],
179 'stats' => $this->get_minify_cached_files()
180 );
181 }
182
183 /**
184 * Run minify preload action.
185 *
186 * @return void|array - Doesn't return anything if run() is successfull (Run() prints a JSON object and closed browser connection) or an array if failed.
187 */
188 public function run_minify_preload() {
189 return WP_Optimize_Minify_Preloader::instance()->run('manual');
190 }
191
192 /**
193 * Cancel minify preload action.
194 *
195 * @return array
196 */
197 public function cancel_minify_preload() {
198 WP_Optimize_Minify_Preloader::instance()->cancel_preload();
199 return WP_Optimize_Minify_Preloader::instance()->get_status_info();
200 }
201
202 /**
203 * Get status of minify preload.
204 *
205 * @return array
206 */
207 public function get_minify_preload_status() {
208 return WP_Optimize_Minify_Preloader::instance()->get_status_info();
209 }
210 }
211