PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.6.1
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.6.1
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-admin.php

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

371 lines 12.0 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 class WP_Optimize_Minify_Admin {
5
6 /**
7 * Initialize, add actions and filters
8 *
9 * @return void
10 */
11 public function __construct() {
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'));
14
15 add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'admin_notices_activation_error'));
16
17 add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
18
19 // This function runs when WordPress updates or installs/remove something. Forces new cache
20 add_action('upgrader_process_complete', array('WP_Optimize_Minify_Cache_Functions', 'cache_increment'));
21 // This function runs when an active theme or plugin is updated
22 add_action('wpo_active_plugin_or_theme_updated', array('WP_Optimize_Minify_Cache_Functions', 'reset'));
23 add_action('upgrader_overwrote_package', array('WP_Optimize_Minify_Cache_Functions', 'reset'));
24 add_action('after_switch_theme', array('WP_Optimize_Minify_Cache_Functions', 'cache_increment'));
25 add_action('updraftcentral_version_updated', array('WP_Optimize_Minify_Cache_Functions', 'reset'));
26 add_action('elementor/editor/after_save', array('WP_Optimize_Minify_Cache_Functions', 'reset'));
27 add_action('fusion_cache_reset_after', array('WP_Optimize_Minify_Cache_Functions', 'reset'));
28 // Output asset preload placeholder, replaced by premium
29 add_action('wpo_minify_settings_tabs', array($this, 'output_assets_preload_placeholder'));
30
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'));
33 }
34
35 /**
36 * Register the content
37 *
38 * @return void
39 */
40 public function register_content() {
41 add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'output_status'), 20);
42 add_action('wp_optimize_admin_page_wpo_minify_settings', array($this, 'output_settings'), 20);
43 add_action('wp_optimize_admin_page_wpo_minify_advanced', array($this, 'output_advanced'), 20);
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);
46 add_action('wp_optimize_admin_page_wpo_minify_css', array($this, 'output_css_settings'), 20);
47 add_action('wp_optimize_admin_page_wpo_minify_js', array($this, 'output_js_settings'), 20);
48 add_action('wp_optimize_admin_page_wpo_minify_preload', array($this, 'output_preload_settings'), 20);
49 }
50
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 /**
65 * Load scripts for controlling the admin pages
66 *
67 * @param string $hook
68 * @return void
69 */
70 public function admin_enqueue_scripts($hook) {
71 $enqueue_version = WP_Optimize()->get_enqueue_version();
72 $min_or_not_internal = WP_Optimize()->get_min_or_not_internal_string();
73 if (preg_match('/wp\-optimize/i', $hook)) {
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);
75 }
76 }
77
78 /**
79 * Conditionally runs upon the WP action admin_notices to display error
80 *
81 * @return void
82 */
83 public function admin_notices_activation_error() {
84 if (!extension_loaded('mbstring')) {
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>';
88 }
89 }
90
91 /**
92 * Display an admin notice if the user has inadequate filesystem permissions
93 *
94 * @return void
95 */
96 public function check_permissions_admin_notices() {
97 // get cache path
98 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
99 $cache_dir = $cache_path['cachedir'];
100 if (is_dir($cache_dir) && !wp_is_writable($cache_dir)) {
101 $chmod = substr(sprintf('%o', fileperms($cache_dir)), -4);
102 ?>
103 <div class="notice notice-error wpo-warning">
104 <p>
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 ?>
109 </p>
110 </div>
111 <div class="notice notice-error wpo-warning">
112 <p>
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 ?>
117 </p>
118 </div>
119 <div class="notice notice-error wpo-warning">
120 <p>
121 <?php
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 echo " ";
125 esc_html_e('Please contact your hosting provider.', 'wp-optimize');
126 ?>
127 </p>
128 </div>
129 <?php
130 }
131 }
132
133 /**
134 * Minify - Outputs the status tab
135 *
136 * @return void
137 */
138 public function output_status() {
139 $found_incompatible_plugins = WP_Optimize_Detect_Minify_Plugins::get_instance()->get_active_minify_plugins();
140 $wpo_minify_options = wp_optimize_minify_config()->get();
141 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
142 WP_Optimize()->include_template(
143 'minify/status-tab.php',
144 false,
145 array(
146 'wpo_minify_options' => $wpo_minify_options,
147 'show_information_notice' => !get_user_meta(get_current_user_id(), 'wpo-hide-minify-information-notice', true),
148 'cache_dir' => $cache_path['cachedir'],
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),
151 )
152 );
153 }
154
155 /**
156 * Minify - Outputs the font settings tab
157 *
158 * @return void
159 */
160 public function output_font_settings() {
161 $wpo_minify_options = wp_optimize_minify_config()->get();
162
163 WP_Optimize()->include_template(
164 'minify/font-settings-tab.php',
165 false,
166 array(
167 'wpo_minify_options' => $wpo_minify_options,
168 'fonts_cache_size' => WP_Optimize()::is_premium() ? WP_Optimize_Host_Google_Fonts::instance()->get_cache_stats() : '',
169 )
170 );
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 }
194
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 /**
255 * Minify - Outputs the CSS settings tab
256 *
257 * @return void
258 */
259 public function output_css_settings() {
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 WP_Optimize()->include_template(
264 'minify/css-settings-tab.php',
265 false,
266 $template_args
267 );
268 }
269
270 /**
271 * Minify - Outputs the JS settings tab
272 *
273 * @return void
274 */
275 public function output_js_settings() {
276 $template_args = $this->get_output_setting('js');
277 WP_Optimize()->include_template(
278 'minify/js-settings-tab.php',
279 false,
280 $template_args
281 );
282 }
283
284 /**
285 * Minify - Outputs the settings tab
286 *
287 * @return void
288 */
289 public function output_settings() {
290 $wpo_minify_options = wp_optimize_minify_config()->get();
291 $url = wp_parse_url(get_home_url());
292 WP_Optimize()->include_template(
293 'minify/settings-tab.php',
294 false,
295 array(
296 'wpo_minify_options' => $wpo_minify_options,
297 'default_protocol' => $url['scheme']
298 )
299 );
300 }
301
302 /**
303 * Minify - Outputs the settings tab
304 *
305 * @return void
306 */
307 public function output_assets_preload_placeholder($wpo_minify_options) {
308 WP_Optimize()->include_template(
309 'minify/asset-preload.php',
310 false,
311 array(
312 'wpo_minify_options' => $wpo_minify_options
313 )
314 );
315 }
316
317 /**
318 * Minify - Outputs the preload tab
319 *
320 * @return void
321 */
322 public function output_preload_settings() {
323 $wpo_minify_preloader = WP_Optimize_Minify_Preloader::instance();
324 $is_running = $wpo_minify_preloader->is_running();
325 $status = $wpo_minify_preloader->get_status_info();
326 $cache_config = WPO_Cache_Config::instance();
327 WP_Optimize()->include_template(
328 'minify/preload-tab.php',
329 false,
330 array(
331 'is_cache_enabled' => $cache_config->get_option('enable_page_caching'),
332 'is_running' => $is_running,
333 'status_message' => $status['message'] ?? '',
334 )
335 );
336 }
337
338 /**
339 * Minify - Outputs the advanced tab
340 *
341 * @return void
342 */
343 public function output_advanced() {
344 $wpo_minify_options = wp_optimize_minify_config()->get();
345 $files = false;
346 if (apply_filters('wpo_minify_status_show_files_on_load', true)) {
347 $files = WP_Optimize_Minify_Cache_Functions::get_cached_files();
348 }
349
350 // WP_Optimize_Minify_Functions is only loaded when Minify is active
351 if (class_exists('WP_Optimize_Minify_Functions')) {
352 $default_ignore = WP_Optimize_Minify_Functions::get_default_ignore();
353 $default_ie_blacklist = WP_Optimize_Minify_Functions::get_default_ie_blacklist();
354 } else {
355 $default_ignore = array();
356 $default_ie_blacklist = array();
357 }
358
359 WP_Optimize()->include_template(
360 'minify/advanced-tab.php',
361 false,
362 array(
363 'wpo_minify_options' => $wpo_minify_options,
364 'files' => $files,
365 'default_ignore' => $default_ignore,
366 'default_ie_blacklist' => $default_ie_blacklist
367 )
368 );
369 }
370 }
371