PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
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
← All changes | minify/class-wp-optimize-minify-admin.php +137 -58 3.2.3 → 4.7.0 View file →
@@ -2,10 +2,8 @@
2 2 if (!defined('ABSPATH')) die('No direct access allowed');
3 3
4 4 class WP_Optimize_Minify_Admin {
5 5
6 - private $wp_version_required = '4.5';
7 -
8 6 /**
9 7 * Initialize, add actions and filters
10 8 *
11 9 * @return void
@@ -10,14 +8,12 @@
10 8 *
11 9 * @return void
12 10 */
13 11 public function __construct() {
14 - if (WPO_MINIFY_PHP_VERSION_MET) {
15 - // exclude processing for editors and administrators (fix editors)
16 - add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'check_permissions_admin_notices'));
17 - }
12 + // exclude processing for editors and administrators (fix editors)
13 + add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'check_permissions_admin_notices'));
18 14
19 - add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'admin_notices_activation_errors'));
15 + add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'admin_notices_activation_error'));
20 16
21 17 add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
22 18
23 19 // This function runs when WordPress updates or installs/remove something. Forces new cache
@@ -29,11 +25,12 @@
29 25 add_action('updraftcentral_version_updated', array('WP_Optimize_Minify_Cache_Functions', 'reset'));
30 26 add_action('elementor/editor/after_save', array('WP_Optimize_Minify_Cache_Functions', 'reset'));
31 27 add_action('fusion_cache_reset_after', array('WP_Optimize_Minify_Cache_Functions', 'reset'));
32 28 // Output asset preload placeholder, replaced by premium
33 - add_action('wpo_minify_settings_tabs', array($this, 'output_assets_preload_placeholder'), 10, 1);
29 + add_action('wpo_minify_settings_tabs', array($this, 'output_assets_preload_placeholder'));
34 30
35 31 add_action('wp_optimize_register_admin_content', array($this, 'register_content'));
32 + add_action('wpo_after_minify_javascript_options', array($this, 'output_capo_js_settings'));
36 33 }
37 34
38 35 /**
39 36 * Register the content
@@ -44,8 +41,9 @@
44 41 add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'output_status'), 20);
45 42 add_action('wp_optimize_admin_page_wpo_minify_settings', array($this, 'output_settings'), 20);
46 43 add_action('wp_optimize_admin_page_wpo_minify_advanced', array($this, 'output_advanced'), 20);
47 44 add_action('wp_optimize_admin_page_wpo_minify_font', array($this, 'output_font_settings'), 20);
45 + add_action('wp_optimize_admin_page_wpo_minify_analytics', array($this, 'output_analytics_settings'), 20);
48 46 add_action('wp_optimize_admin_page_wpo_minify_css', array($this, 'output_css_settings'), 20);
49 47 add_action('wp_optimize_admin_page_wpo_minify_js', array($this, 'output_js_settings'), 20);
50 48 add_action('wp_optimize_admin_page_wpo_minify_preload', array($this, 'output_preload_settings'), 20);
51 49 }
@@ -50,8 +48,21 @@
50 48 add_action('wp_optimize_admin_page_wpo_minify_preload', array($this, 'output_preload_settings'), 20);
51 49 }
52 50
53 51 /**
52 + * Output Capo JS settings
53 + *
54 + * @return void
55 + */
56 + public function output_capo_js_settings() {
57 + $wpo_minify_options = wp_optimize_minify_config()->get();
58 + $extract = array(
59 + 'enable_capo_js' => $wpo_minify_options['enable_capo_js'] ?? false,
60 + );
61 + WP_Optimize()->include_template('minify/capo-js.php', false, $extract);
62 + }
63 +
64 + /**
54 65 * Load scripts for controlling the admin pages
55 66 *
56 67 * @param string $hook
57 68 * @return void
@@ -56,44 +67,26 @@
56 67 * @param string $hook
57 68 * @return void
58 69 */
59 70 public function admin_enqueue_scripts($hook) {
60 - $enqueue_version = (defined('WP_DEBUG') && WP_DEBUG) ? WPO_VERSION.'.'.time() : WPO_VERSION;
61 - $min_or_not_internal = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '-'. str_replace('.', '-', WPO_VERSION). '.min';
71 + $enqueue_version = WP_Optimize()->get_enqueue_version();
72 + $min_or_not_internal = WP_Optimize()->get_min_or_not_internal_string();
62 73 if (preg_match('/wp\-optimize/i', $hook)) {
63 - wp_enqueue_script('wp-optimize-min-js', WPO_PLUGIN_URL.'js/minify' . $min_or_not_internal . '.js', array('jquery', 'wp-optimize-admin-js'), $enqueue_version);
74 + wp_enqueue_script('wp-optimize-min-js', WPO_PLUGIN_URL.'js/minify' . $min_or_not_internal . '.js', array('jquery', 'wp-optimize-admin-js'), $enqueue_version, true);
64 75 }
65 76 }
66 77
67 78 /**
68 - * Conditionally runs upon the WP action admin_notices to display errors
79 + * Conditionally runs upon the WP action admin_notices to display error
69 80 *
70 81 * @return void
71 82 */
72 - public function admin_notices_activation_errors() {
73 - global $wp_version;
74 - include ABSPATH . WPINC . '/version.php';
75 - $errors = array();
76 -
77 - if (!WPO_MINIFY_PHP_VERSION_MET) {
78 - $errors[] = sprintf(__('WP-Optimize Minify requires PHP 5.4 or higher. You’re using version %s.', 'wp-optimize'), PHP_VERSION);
79 - }
80 -
83 + public function admin_notices_activation_error() {
81 84 if (!extension_loaded('mbstring')) {
82 - $errors[] = __('WP-Optimize Minify requires the PHP mbstring module to be installed on the server; please ask your web hosting company for advice on how to enable it on your server.', 'wp-optimize');
85 + echo '<div class="notice notice-error wpo-warning">';
86 + echo '<p>' . esc_html__('WP-Optimize Minify requires the PHP mbstring module to be installed on the server; please ask your web hosting company for advice on how to enable it on your server.', 'wp-optimize') . '</p>';
87 + echo '</div>';
83 88 }
84 -
85 - if (version_compare($wp_version, $this->wp_version_required, '<')) {
86 - $errors[] = sprintf(__('WP-Optimize Minify requires WordPress version %s or higher. You’re using version %s.', 'wp-optimize'), $this->wp_version_required, $wp_version);
87 - }
88 -
89 - foreach ($errors as $error) {
90 - ?>
91 - <div class="notice notice-error wpo-warning">
92 - <p><?php echo $error; ?></p>
93 - </div>
94 - <?php
95 - }
96 89 }
97 90
98 91 /**
99 92 * Display an admin notice if the user has inadequate filesystem permissions
@@ -103,27 +96,34 @@
103 96 public function check_permissions_admin_notices() {
104 97 // get cache path
105 98 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
106 99 $cache_dir = $cache_path['cachedir'];
107 - if (is_dir($cache_dir) && !is_writable($cache_dir)) {
100 + if (is_dir($cache_dir) && !wp_is_writable($cache_dir)) {
108 101 $chmod = substr(sprintf('%o', fileperms($cache_dir)), -4);
109 102 ?>
110 103 <div class="notice notice-error wpo-warning">
111 104 <p>
112 - <?php printf(__('WP-Optimize Minify needs write permissions on the folder %s.', 'wp-optimize'), "<strong>".htmlspecialchars($cache_dir)."</strpmg>"); ?>
105 + <?php
106 + // translators: %s is a directory
107 + echo wp_kses_post(sprintf(__('WP-Optimize Minify needs write permissions on the folder %s.', 'wp-optimize'), "<strong>". esc_html($cache_dir)."</strong>"));
108 + ?>
113 109 </p>
114 110 </div>
115 111 <div class="notice notice-error wpo-warning">
116 112 <p>
117 - <?php printf(__('The current permissions for WP-Optimize Minify are chmod %s.', 'wp-optimize'), "<strong>$chmod</strong>"); ?>
113 + <?php
114 + // translators: %s is a file permissions code
115 + echo wp_kses_post(sprintf(__('The current permissions for WP-Optimize Minify are chmod %s.', 'wp-optimize'), "<strong>" . esc_html($chmod) . "</strong>"));
116 + ?>
118 117 </p>
119 118 </div>
120 119 <div class="notice notice-error wpo-warning">
121 120 <p>
122 121 <?php
123 - printf(__('If you need something more than %s for it to work, then your server is probably misconfigured.', 'wp-optimize'), '<strong>775</strong>');
122 + // translators: %s is a file permissions code
123 + echo wp_kses_post(sprintf(__('If you need something more than %s for it to work, then your server is probably misconfigured.', 'wp-optimize'), '<strong>775</strong>'));
124 124 echo " ";
125 - _e('Please contact your hosting provider.', 'wp-optimize');
125 + esc_html_e('Please contact your hosting provider.', 'wp-optimize');
126 126 ?>
127 127 </p>
128 128 </div>
129 129 <?php
@@ -135,12 +135,9 @@
135 135 *
136 136 * @return void
137 137 */
138 138 public function output_status() {
139 - if (!class_exists('WP_Optimize_Detect_Minify_Plugins')) {
140 - require_once(WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-detect-minify-plugins.php');
141 - }
142 - $this->found_incompatible_plugins = WP_Optimize_Detect_Minify_Plugins::get_instance()->get_active_minify_plugins();
139 + $found_incompatible_plugins = WP_Optimize_Detect_Minify_Plugins::get_instance()->get_active_minify_plugins();
143 140 $wpo_minify_options = wp_optimize_minify_config()->get();
144 141 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
145 142 WP_Optimize()->include_template(
146 143 'minify/status-tab.php',
@@ -148,10 +145,10 @@
148 145 array(
149 146 'wpo_minify_options' => $wpo_minify_options,
150 147 'show_information_notice' => !get_user_meta(get_current_user_id(), 'wpo-hide-minify-information-notice', true),
151 148 'cache_dir' => $cache_path['cachedir'],
152 - 'can_purge_the_cache' => WP_Optimize()->can_purge_the_cache(),
153 - 'active_minify_plugins' => apply_filters('wpo_minify_found_incompatible_plugins', $this->found_incompatible_plugins),
149 + 'can_purge_the_cache' => WP_Optimize()->get_minify()->can_purge_cache(),
150 + 'active_minify_plugins' => apply_filters('wpo_minify_found_incompatible_plugins', $found_incompatible_plugins),
154 151 )
155 152 );
156 153 }
157 154
@@ -161,30 +158,115 @@
161 158 * @return void
162 159 */
163 160 public function output_font_settings() {
164 161 $wpo_minify_options = wp_optimize_minify_config()->get();
162 +
165 163 WP_Optimize()->include_template(
166 164 'minify/font-settings-tab.php',
167 165 false,
168 166 array(
169 - 'wpo_minify_options' => $wpo_minify_options
167 + 'wpo_minify_options' => $wpo_minify_options,
168 + 'fonts_cache_size' => WP_Optimize()::is_premium() ? WP_Optimize_Host_Google_Fonts::instance()->get_cache_stats() : '',
170 169 )
171 170 );
172 171 }
172 +
173 + /**
174 + * Minify - Outputs the analytics settings tab
175 + *
176 + * @return void
177 + */
178 + public function output_analytics_settings() {
179 + $config = wp_optimize_minify_config()->get();
180 + $id = $config['tracking_id'] ?? '';
181 + $method = $config['analytics_method'] ?? '';
182 + $is_enabled = $config['enable_analytics'] ?? false;
183 +
184 + WP_Optimize()->include_template(
185 + 'minify/analytics-settings-tab.php',
186 + false,
187 + array(
188 + 'id' => $id,
189 + 'method'=> $method,
190 + 'is_enabled'=> $is_enabled
191 + )
192 + );
193 + }
173 194
174 195 /**
196 + * Minify - Get the output settings for the CSS or JS settings tabs
197 + *
198 + * @param string $type - css or js
199 + *
200 + * @return array
201 + */
202 + private function get_output_setting($type) {
203 + $wpo_minify_options = wp_optimize_minify_config()->get();
204 + $is_http1 = WP_Optimize_Utils::is_request_protocol_http1();
205 + $protocol = $is_http1 ? 'HTTP/1.x' : 'HTTP/2/3';
206 +
207 + $merge_enabled = !empty($wpo_minify_options['enable_merging_of_'.$type]);
208 +
209 + // Determine if the notice should be shown
210 + $should_show_notice = (($is_http1 && !$merge_enabled) || (!$is_http1 && $merge_enabled));
211 + $user_dismissed = get_user_meta(get_current_user_id(), 'wpo_hide_'.$type.'_merging_notice', true);
212 +
213 + // Only show if a user hasn't dismissed yet
214 + $show_section_notice = $should_show_notice && !$user_dismissed;
215 + $is_enabled_minification = $wpo_minify_options['enable_'.$type.'_minification'];
216 +
217 + $label = 'js' === $type ? 'JavaScript' : 'CSS';
218 +
219 + // Notice text
220 + $notice_text = sprintf(
221 + /* translators: 1: File type (CSS/JS), 2: HTTP protocol */
222 + __('Merging %1$s files may improve performance on HTTP/1.1, but could reduce speed on HTTP/2/3.<br><b>Note:</b> Your site is currently using %2$s.', 'wp-optimize'),
223 + $label,
224 + $protocol
225 + );
226 +
227 + // Tooltip parts
228 + $tooltip_parts = array(
229 + sprintf(
230 + /* translators: %s: File type (CSS/JS) */
231 + __('Combines multiple %s files into one to reduce HTTP requests.', 'wp-optimize'),
232 + $label
233 + ),
234 + __('May improve performance on HTTP/1.1, but could reduce speed on HTTP/2/3.', 'wp-optimize'),
235 + __('Disable this option if your site experiences issues.', 'wp-optimize'),
236 + sprintf(
237 + /* translators: %s: HTTP protocol */
238 + __('Note: Your site is currently using %s.', 'wp-optimize'),
239 + $protocol
240 + ),
241 + );
242 + $tooltip = implode(' ', $tooltip_parts);
243 +
244 + return array(
245 + 'protocol' => $protocol,
246 + 'show_section_notice' => $show_section_notice,
247 + 'wpo_minify_options' => $wpo_minify_options,
248 + 'is_enabled_minification' => $is_enabled_minification,
249 + 'notice_text' => $notice_text,
250 + 'tooltip' => $tooltip,
251 + );
252 + }
253 +
254 + /**
175 255 * Minify - Outputs the CSS settings tab
176 256 *
177 257 * @return void
178 258 */
179 259 public function output_css_settings() {
180 - $wpo_minify_options = wp_optimize_minify_config()->get();
260 + $template_args = $this->get_output_setting('css');
261 + $template_args['show_unused_css'] = !WP_Optimize::is_premium() || (WP_Optimize::is_premium() && $template_args['is_enabled_minification']);
262 + $template_args['show_unused_css_advertise'] = !WP_Optimize::is_premium();
263 + $template_args['is_page_cache_enabled'] = WPO_Cache_Config::instance()->get_option('enable_page_caching');
264 +
181 265 WP_Optimize()->include_template(
182 266 'minify/css-settings-tab.php',
183 267 false,
184 - array(
185 - 'wpo_minify_options' => $wpo_minify_options
186 - )
268 + $template_args
187 269 );
188 270 }
189 271
190 272 /**
@@ -192,15 +274,13 @@
192 274 *
193 275 * @return void
194 276 */
195 277 public function output_js_settings() {
196 - $wpo_minify_options = wp_optimize_minify_config()->get();
278 + $template_args = $this->get_output_setting('js');
197 279 WP_Optimize()->include_template(
198 280 'minify/js-settings-tab.php',
199 281 false,
200 - array(
201 - 'wpo_minify_options' => $wpo_minify_options
202 - )
282 + $template_args
203 283 );
204 284 }
205 285
206 286 /**
@@ -209,9 +289,9 @@
209 289 * @return void
210 290 */
211 291 public function output_settings() {
212 292 $wpo_minify_options = wp_optimize_minify_config()->get();
213 - $url = parse_url(get_home_url());
293 + $url = wp_parse_url(get_home_url());
214 294 WP_Optimize()->include_template(
215 295 'minify/settings-tab.php',
216 296 false,
217 297 array(
@@ -244,9 +324,8 @@
244 324 public function output_preload_settings() {
245 325 $wpo_minify_preloader = WP_Optimize_Minify_Preloader::instance();
246 326 $is_running = $wpo_minify_preloader->is_running();
247 327 $status = $wpo_minify_preloader->get_status_info();
248 - if (!class_exists('WPO_Cache_Config')) require_once(WPO_PLUGIN_MAIN_PATH . '/cache/class-wpo-cache-config.php');
249 328 $cache_config = WPO_Cache_Config::instance();
250 329 WP_Optimize()->include_template(
251 330 'minify/preload-tab.php',
252 331 false,
@@ -252,9 +331,9 @@
252 331 false,
253 332 array(
254 333 'is_cache_enabled' => $cache_config->get_option('enable_page_caching'),
255 334 'is_running' => $is_running,
256 - 'status_message' => isset($status['message']) ? $status['message'] : '',
335 + 'status_message' => $status['message'] ?? '',
257 336 )
258 337 );
259 338 }
260 339
@@ -265,9 +344,9 @@
265 344 */
266 345 public function output_advanced() {
267 346 $wpo_minify_options = wp_optimize_minify_config()->get();
268 347 $files = false;
269 - if (apply_filters('wpo_minify_status_show_files_on_load', true) && WPO_MINIFY_PHP_VERSION_MET) {
348 + if (apply_filters('wpo_minify_status_show_files_on_load', true)) {
270 349 $files = WP_Optimize_Minify_Cache_Functions::get_cached_files();
271 350 }
272 351
273 352 // WP_Optimize_Minify_Functions is only loaded when Minify is active