PluginProbe
Featherweight / 2.2.0
Featherweight v2.2.0
2.2.3 2.2.2 2.2.0 2.2.1 2.0.2 trunk 1.4.5 1.5.0 1.5.19 1.5.20 1.5.21 1.5.22 1.6.0 1.6.1 2.0.0 2.0.1
wp-disable / lib / class-wpperformance-admin.php

class-wpperformance-admin.php in Featherweight 2.2.0, at lib/class-wpperformance-admin.php

502 lines 19.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || exit; // Prevent direct access.
3 class WpPerformance_Admin {
4
5 public function __construct() {
6 add_action( 'init', array( $this, 'wp_performance_yoast_seo_settings' ) );
7 add_action( 'init', array( $this, 'wp_performance_disable_emojis' ) );
8 add_action( 'init', array( $this, 'wp_performance_speed_stop_loading_wp_embed' ) );
9 add_filter( 'script_loader_src', array( $this, 'wp_performance_remove_script_version' ), 15, 1 );
10 add_filter( 'style_loader_src', array( $this, 'wp_performance_remove_script_version' ), 15, 1 );
11 add_action( 'init', array( $this, 'wp_performace_disable_woo_stuffs' ) );
12 add_action( 'init', array( $this, 'wp_performance_optimize_cleanups' ) );
13 add_action( 'wp_loaded', array( $this, 'wp_performance_disable_google_maps' ) );
14 add_action( 'wp_default_scripts', array( $this, 'remove_jquery_migrate' ) );
15 add_action( 'wp_enqueue_scripts', array( $this, 'wp_performance_dequeue_woocommerce_cart_fragments' ), 11 );
16 add_action( 'wp_loaded', array( $this, 'wp_performance_save_dashboard_settings' ) );
17 $this->heartbeat_handler();
18 }
19
20 public function wp_performance_yoast_seo_settings(){
21
22 if ( defined( 'WPSEO_VERSION' ) ){
23
24 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
25
26 if ( isset( $settings['remove_yoast_comment'] ) && 1 === (int) $settings['remove_yoast_comment'] ) {
27 add_action( 'get_header', array( $this, 'remove_yoast_comment' ) );
28 add_action( 'wp_head', array( $this, 'remove_yoast_comment_complete' ), 999 );
29 }
30
31 if ( isset( $settings['remove_yoast_breadcrumbs_duplicates'] ) && 1 === (int) $settings['remove_yoast_breadcrumbs_duplicates'] ) {
32 add_filter('wpseo_breadcrumb_single_link', array( $this, 'remove_yoast_breadcrumb_last_link' ) );
33 }
34 }
35 }
36
37 public function remove_yoast_comment() {
38 ob_start( array( $this, 'remove_yoast_comment_replace' ) );
39 }
40
41 public function remove_yoast_comment_replace($html) {
42 return preg_replace( '/^<!--.*?[Y]oast.*?-->$/mi', '', $html );
43 }
44
45 public function remove_yoast_comment_complete() {
46 ob_end_flush();
47 }
48
49 public function remove_yoast_breadcrumb_last_link($link_output) {
50 return false !== strpos( $link_output, 'breadcrumb_last' ) ? '' : $link_output;
51 }
52
53 public function disable_emojis_tinymce( $plugins ) {
54 $ret = is_array( $plugins ) ? array_diff( $plugins, array( 'wpemoji' ) ) : array();
55 return $ret;
56 }
57
58 public function wp_performance_disable_emojis() {
59 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
60 if ( isset( $settings['disable_emoji'] ) && 1 === (int) $settings['disable_emoji'] ) {
61 remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
62 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
63 remove_action( 'wp_print_styles', 'print_emoji_styles' );
64 remove_action( 'admin_print_styles', 'print_emoji_styles' );
65 remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
66 remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
67 remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
68 add_filter( 'tiny_mce_plugins', array( $this, 'disable_emojis_tinymce' ) );
69 }
70 }
71
72 public function wp_performance_speed_stop_loading_wp_embed() {
73 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
74 if ( isset( $settings['disable_embeds'] ) && 1 === (int) $settings['disable_embeds'] ) {
75 if ( ! is_admin() ) {
76 wp_deregister_script( 'wp-embed' );
77 }
78 }
79 }
80
81 public function wp_performance_remove_script_version( $src ) {
82 if ( ! is_admin() && ! current_user_can('administrator') && ! current_user_can('editor') ) {
83 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
84 if ( isset( $settings['remove_querystrings'] ) && 1 === (int) $settings['remove_querystrings'] ) {
85 $parts = explode( '?ver', $src );
86 return $parts[0];
87 }
88 }
89 return $src;
90 }
91
92 public function wp_performace_disable_woo_stuffs() {
93 if( WpPerformance::is_woocommerce_enabled() ){
94 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
95 if ( isset( $settings['disable_woocommerce_non_pages'] ) && 1 === (int) $settings['disable_woocommerce_non_pages'] ) {
96 add_action( 'wp_print_scripts', array( $this, 'wp_performance_woocommerce_de_script' ), 100 );
97 add_action( 'wp_enqueue_scripts', array( $this, 'wp_performance_remove_woocommerce_generator' ), 99 );
98 add_action( 'wp_enqueue_scripts', array( $this, 'child_manage_woocommerce_css' ) );
99 }
100 }
101 }
102
103 public function wp_performance_remove_woocommerce_generator() {
104 if( WpPerformance::is_woocommerce_enabled() ){
105 if ( function_exists( 'is_woocommerce' ) ) {
106 if ( ! is_woocommerce() ) {
107 // if we're not on a woo page, remove the generator tag.
108 remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
109 }
110 }
111 }
112 }
113
114 public function child_manage_woocommerce_css() {
115 if( WpPerformance::is_woocommerce_enabled() ){
116 if ( function_exists( 'is_woocommerce' ) ) {
117 if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_account_page() ) {
118 // this adds the styles back on woocommerce pages. If you're using a custom script, you could remove these and enter in the path to your own CSS file (if different from your basic style.css file).
119 wp_dequeue_style( 'woocommerce-layout' );
120 wp_dequeue_style( 'woocommerce-smallscreen' );
121 wp_dequeue_style( 'woocommerce-general' );
122 }
123 }
124 }
125 }
126
127 public function wp_performance_woocommerce_de_script() {
128 if( WpPerformance::is_woocommerce_enabled() ){
129 if ( function_exists( 'is_woocommerce' ) ) {
130 if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_account_page() ) {
131 // if we're not on a Woocommerce page, dequeue all of these scripts.
132 wp_dequeue_script( 'wc-add-to-cart' );
133 wp_dequeue_script( 'woocommerce' );
134 wp_dequeue_script( 'wc-cart-fragments' );
135 }
136 }
137 }
138 }
139
140 public function disabler_kill_autosave() {
141 wp_deregister_script( 'autosave' );
142 }
143
144 public function wcs_woo_remove_reviews_tab( $tabs ) {
145 unset( $tabs['reviews'] );
146 return $tabs;
147 }
148
149 public function wp_performance_optimize_cleanups() {
150 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
151
152 if ( isset( $settings['rsd_clean'] ) && $settings['rsd_clean'] ) {
153 remove_action( 'wp_head', 'rsd_link' );
154 }
155
156 if ( isset( $settings['remove_rsd'] ) && $settings['remove_rsd'] ) {
157 remove_action ('wp_head', 'rsd_link');
158 }
159
160 if ( isset( $settings['remove_windows_live_writer'] ) && $settings['remove_windows_live_writer'] ) {
161 remove_action( 'wp_head', 'wlwmanifest_link' );
162 }
163
164 if ( isset( $settings['remove_wordpress_generator_tag'] ) && $settings['remove_wordpress_generator_tag'] ) {
165 remove_action( 'wp_head', 'wp_generator' );
166 }
167
168 if ( isset( $settings['remove_shortlink_tag'] ) && $settings['remove_shortlink_tag'] ) {
169 remove_action( 'wp_head', 'wp_shortlink_wp_head' );
170 }
171
172 if ( isset( $settings['remove_wordpress_api_from_header'] ) && $settings['remove_wordpress_api_from_header'] ) {
173 remove_action( 'wp_head', 'rest_output_link_wp_head' );
174 }
175
176 if ( isset( $settings['disable_revisions'] ) ) {
177
178 switch ( $settings['disable_revisions'] ) {
179 case 'default':
180 $this->wp_config_remove_post_revisions();
181 break;
182 default:
183 $this->wp_config_set_post_revisions( (int) $settings['disable_revisions'] );
184 }
185 }
186
187 if ( isset( $settings['disable_xmlrpc'] ) && $settings['disable_xmlrpc'] ) {
188 add_filter( 'xmlrpc_enabled', '__return_false' );
189 }
190
191 if ( isset( $settings['disable_autosave'] ) && $settings['disable_autosave'] ) {
192 add_action( 'wp_print_scripts', array( $this, 'disabler_kill_autosave' ) );
193 }
194
195 if( WpPerformance::is_woocommerce_enabled() ){
196 if ( isset( $settings['disable_woocommerce_reviews'] ) && $settings['disable_woocommerce_reviews'] ) {
197 add_filter( 'woocommerce_product_tabs', array( $this, 'wcs_woo_remove_reviews_tab' ), 98 );
198 }
199 }
200 }
201
202 private function wp_config_set_post_revisions( $revisions_num = null ) {
203
204 if ( null !== $revisions_num ) {
205
206 wpperformance_init_wp_filesystem();
207
208 global $wp_filesystem;
209
210 if ( $wp_filesystem ) {
211
212 $revisions_num = (int) $revisions_num;
213
214 $revisions_str = sprintf( "define('WP_POST_REVISIONS', %s); // Added by WP Disable\r\n", $revisions_num );
215
216 $file = $this->wp_config_filepath();
217
218 if ( $file ) {
219
220 $contents = $wp_filesystem->get_contents_array( $file );
221
222 if ( $contents ) {
223
224 $exists = false;
225 $need_update = true;
226
227 foreach ( $contents as $key => $line ) {
228
229 $found = preg_match( '/^define\(\s*\'([A-Z_]+)\',(.*)\)/', $line, $match );
230
231 if ( $found && 'WP_POST_REVISIONS' === $match[1] ) {
232
233 if ( $revisions_num === (int) $match[2] ) {
234 $need_update = false;
235 } else {
236 $exists = true;
237 $contents[ $key ] = $revisions_str;
238 }
239 break;
240 }
241 }
242
243 if ( $need_update ) {
244
245 if ( ! $exists ) {
246 array_shift( $contents );
247 array_unshift( $contents, "<?php\r\n", $revisions_str );
248 }
249
250 $wp_filesystem->put_contents(
251 $file,
252 implode( '', $contents ),
253 FS_CHMOD_FILE // predefined mode settings for WP files
254 );
255 }
256
257 unset( $contents );
258 }// End if().
259 }// End if().
260 }// End if().
261 }// End if().
262 }
263
264 private function wp_config_remove_post_revisions() {
265
266 wpperformance_init_wp_filesystem();
267
268 global $wp_filesystem;
269
270 if ( $wp_filesystem ) {
271
272 $file = $this->wp_config_filepath();
273
274 if ( $file ) {
275
276 $contents = $wp_filesystem->get_contents_array( $file );
277 $contents_new = array();
278
279 if ( $contents ) {
280
281 $exists = false;
282
283 foreach ( $contents as $key => $line ) {
284
285 $found = preg_match( '/^define\(\s*\'([A-Z_]+)\',(.*)\)/', $line, $match );
286
287 if ( $found && 'WP_POST_REVISIONS' === $match[1] ) {
288 $exists = true;
289 } else {
290 $contents_new[] = $line;
291 }
292 }
293
294 unset( $contents );
295
296 if ( $exists ) {
297 $wp_filesystem->put_contents(
298 $file,
299 implode( '', $contents_new ),
300 FS_CHMOD_FILE // predefined mode settings for WP files
301 );
302 }
303
304 unset( $contents_new );
305 }
306 }
307 }// End if().
308 }
309
310 private function wp_config_filepath() {
311
312 wpperformance_init_wp_filesystem();
313
314 global $wp_filesystem;
315
316 $config_file = ABSPATH . 'wp-config.php';
317 $config_file_alt = dirname( ABSPATH ) . '/wp-config.php';
318
319 if ( $wp_filesystem->exists( $config_file ) ) {
320 return $config_file;
321 } elseif ( $wp_filesystem->exists( $config_file_alt ) ) {
322 return $config_file_alt;
323 }
324
325 // No writable file found
326 return false;
327 }
328
329 public function wp_performance_disable_google_maps() {
330 if ( !is_admin() ) {
331 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
332 if ( isset( $settings['disable_google_maps'] ) && 1 === (int) $settings['disable_google_maps'] ) {
333 ob_start( 'wpperformance_disable_google_maps_ob_end' );
334 }
335 }
336 }
337
338 public function remove_jquery_migrate( $scripts ) {
339 if ( is_admin() ) {
340 return;
341 }
342
343 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
344
345 if ( isset( $settings['remove_jquery_migrate'] ) && 1 === (int) $settings['remove_jquery_migrate'] ) {
346 // Drop jquery-migrate from the 'jquery' meta-handle's dependencies.
347 // The old code re-registered jquery pinned to 1.12.4, which DOWNGRADED
348 // core jQuery on modern WP (5.6+ ships jQuery 3.x) and broke sites.
349 if ( ! empty( $scripts->registered['jquery'] ) && is_array( $scripts->registered['jquery']->deps ) ) {
350 $scripts->registered['jquery']->deps = array_diff(
351 $scripts->registered['jquery']->deps,
352 array( 'jquery-migrate' )
353 );
354 }
355 }
356 }
357
358 public function wp_performance_dequeue_woocommerce_cart_fragments() {
359 if( WpPerformance::is_woocommerce_enabled() ){
360 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
361 if ( isset( $settings['disable_woocommerce_cart_fragments'] ) && 1 === (int) $settings['disable_woocommerce_cart_fragments'] ) {
362 if ( is_front_page() ) {
363 wp_dequeue_script( 'wc-cart-fragments' );
364 }
365 }
366 }
367 }
368
369 public function heartbeat_stop(){
370 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
371 $location = isset( $settings['heartbeat_location'] ) ? $settings['heartbeat_location'] : 'default';
372 switch( $location ){
373 case 'disable_everywhere':
374 wp_deregister_script('heartbeat');
375 break;
376 case 'disable_on_dashboard_page':
377 global $pagenow;
378 if ( 'index.php' === $pagenow ){
379 wp_deregister_script('heartbeat');
380 }
381 break;
382 case 'allow_only_on_post_edit_pages':
383 global $pagenow;
384 if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ){
385 wp_deregister_script('heartbeat');
386 }
387 break;
388 }
389 }
390
391 public function heartbeat_frequency( $args ){
392 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
393 if ( isset( $settings['heartbeat_frequency'] ) ) {
394 $args['interval'] = (int) $settings['heartbeat_frequency'];
395 }
396 return $args;
397 }
398
399 public function heartbeat_handler(){
400 $settings = get_option( WpPerformance::OPTION_KEY . '_settings', array() );
401 if ( isset( $settings['heartbeat_frequency'] ) && $settings['heartbeat_frequency'] ) {
402 if ( 0 < (int) $settings['heartbeat_frequency'] ) {
403 add_filter( 'heartbeat_settings', array( $this, 'heartbeat_frequency' ) );
404 }
405 }
406 if ( isset( $settings['heartbeat_location'] ) && 'default' !== $settings['heartbeat_location'] ) {
407 add_action( 'init', array( $this, 'heartbeat_stop' ), 1 );
408 }
409 }
410
411 public function wp_performance_save_dashboard_settings(){
412 if ( is_admin() && current_user_can( 'manage_options' ) ) {
413
414 $post_req = wp_unslash( $_POST );
415
416 if( isset( $post_req['wpperformance_admin_settings_nonce'] ) ){
417
418 if( wp_verify_nonce( sanitize_text_field( $post_req['wpperformance_admin_settings_nonce'] ), 'wpperformance-admin-nonce' ) ) {
419
420 self::persist_settings( $post_req );
421 }
422
423 }
424 }
425 }
426
427 /**
428 * Build, sanitise and persist the settings option from a request array.
429 *
430 * Shared by the legacy admin-form handler (above) and the Folium UI ajax
431 * save endpoint (Optimisationio_Dashboard::ajax_app_save). Callers MUST gate
432 * capability + nonce before calling this. Input uses the same checkbox
433 * semantics as the form: a toggle key present means "on", absent means "off".
434 *
435 * @param array $post_req Unslashed request data (real option field names).
436 */
437 public static function persist_settings( array $post_req ) {
438
439 $options = array(
440 'disable_gravatars' => isset( $post_req['disable_gravatars'] ) ? 1 : 0,
441 'disable_referral_spam' => isset( $post_req['disable_referral_spam'] ) ? 1 : 0,
442 'remove_jquery_migrate' => isset( $post_req['remove_jquery_migrate'] ) ? 1 : 0,
443 'dns_prefetch' => isset( $post_req['dns_prefetch'] ) ? 1 : 0,
444 'dns_prefetch_host_list' => isset( $post_req['dns_prefetch_host_list'] ) ? sanitize_textarea_field( $post_req['dns_prefetch_host_list'] ) : '',
445 'disable_emoji' => isset( $post_req['disable_emoji'] ) ? 1 : 0,
446 'disable_embeds' => isset( $post_req['disable_embeds'] ) ? 1 : 0,
447 'remove_querystrings' => isset( $post_req['remove_querystrings'] ) ? 1 : 0,
448 'lazy_load_google_fonts' => isset( $post_req['lazy_load_google_fonts'] ) ? 1 : 0,
449 'lazy_load_font_awesome' => isset( $post_req['lazy_load_font_awesome'] ) ? 1 : 0,
450 'remove_yoast_comment' => isset( $post_req['remove_yoast_comment'] ) ? 1 : 0,
451 'remove_yoast_breadcrumbs_duplicates' => isset( $post_req['remove_yoast_breadcrumbs_duplicates'] ) ? 1 : 0,
452 'default_ping_status' => isset( $post_req['default_ping_status'] ) ? 1 : 0,
453 'disable_all_comments' => isset( $post_req['disable_all_comments'] ) ? 1 : 0,
454 'disable_author_pages' => isset( $post_req['disable_author_pages'] ) ? 1 : 0,
455 'disable_comments_on_certain_post_types' => isset( $post_req['disable_comments_on_certain_post_types'] ) ? 1 : 0,
456 'disable_comments_on_post_types' => isset( $post_req['disable_comments_on_post_types'] ) && is_array( $post_req['disable_comments_on_post_types'] ) ? array_map( 'intval', $post_req['disable_comments_on_post_types'] ) : array(),
457 'close_comments' => isset( $post_req['close_comments'] ) ? 1 : 0,
458 'paginate_comments' => isset( $post_req['paginate_comments'] ) ? 1 : 0,
459 'remove_comments_links' => isset( $post_req['remove_comments_links'] ) ? 1 : 0,
460 'heartbeat_frequency' => isset( $post_req['heartbeat_frequency'] ) ? sanitize_text_field( $post_req['heartbeat_frequency'] ) : 'default',
461 'heartbeat_location' => isset( $post_req['heartbeat_location'] ) ? sanitize_text_field( $post_req['heartbeat_location'] ) : 'default',
462 'remove_rsd' => isset( $post_req['remove_rsd'] ) ? 1 : 0,
463 'remove_windows_live_writer' => isset( $post_req['remove_windows_live_writer'] ) ? 1 : 0,
464 'remove_wordpress_generator_tag' => isset( $post_req['remove_wordpress_generator_tag'] ) ? 1 : 0,
465 'remove_shortlink_tag' => isset( $post_req['remove_shortlink_tag'] ) ? 1 : 0,
466 'remove_wordpress_api_from_header' => isset( $post_req['remove_wordpress_api_from_header'] ) ? 1 : 0,
467 'disable_rss' => isset( $post_req['disable_rss'] ) ? 1 : 0,
468 'not_disable_global_feeds' => isset( $post_req['not_disable_global_feeds'] ) ? 1 : 0,
469 'disabled_feed_behaviour' => isset( $post_req['disabled_feed_behaviour'] ) && '404_error' === $post_req['disabled_feed_behaviour'] ? '404_error' : 'redirect',
470 'disable_xmlrpc' => isset( $post_req['disable_xmlrpc'] ) ? 1 : 0,
471 'spam_comments_cleaner' => isset( $post_req['spam_comments_cleaner'] ) ? 1 : 0,
472 'delete_spam_comments' => isset( $post_req['delete_spam_comments'] ) ? sanitize_text_field( $post_req['delete_spam_comments'] ) : 'daily',
473 'disable_autosave' => isset( $post_req['disable_autosave'] ) ? 1 : 0,
474 'disable_admin_notices' => isset( $post_req['disable_admin_notices'] ) ? 1 : 0,
475 'disable_revisions' => isset( $post_req['disable_revisions'] ) ? sanitize_text_field( $post_req['disable_revisions'] ) : 'default',
476 'disable_woocommerce_non_pages' => isset( $post_req['disable_woocommerce_non_pages'] ) ? 1 : 0,
477 'disable_woocommerce_cart_fragments' => isset( $post_req['disable_woocommerce_cart_fragments'] ) ? 1 : 0,
478 'disable_woocommerce_reviews' => isset( $post_req['disable_woocommerce_reviews'] ) ? 1 : 0,
479 'disable_woocommerce_password_meter' => isset( $post_req['disable_woocommerce_password_meter'] ) ? 1 : 0,
480 'disable_wordpress_password_meter' => isset( $post_req['disable_wordpress_password_meter'] ) ? 1 : 0,
481 'disable_front_dashicons_when_disabled_toolbar' => isset( $post_req['disable_front_dashicons_when_disabled_toolbar'] ) ? 1 : 0,
482 'disable_google_maps' => isset( $post_req['disable_google_maps'] ) ? 1 : 0,
483 'exclude_from_disable_google_maps' => isset( $post_req['exclude_from_disable_google_maps'] ) ? sanitize_text_field( trim( $post_req['exclude_from_disable_google_maps'] ) ) : '',
484 );
485
486 WpPerformance::synchronize_discussion_data( $post_req );
487
488 $settings = update_option( WpPerformance::OPTION_KEY . '_settings', $options );
489
490 WpPerformance::delete_transients();
491
492 if ( isset( $post_req['delete_spam_comments_now'] ) ) {
493 WpPerformance::delete_spam_comments();
494 WpPerformance::check_spam_comments_delete( true );
495 }
496 else {
497 WpPerformance::check_spam_comments_delete( false );
498 }
499 }
500
501 }
502