| 1 |
<?php |
| 2 |
namespace WPEXtra\Modules\Frontend; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use WPEXtra\Settings; |
| 9 |
use WPEXtra\Helper; |
| 10 |
use WPEXtra\Base; |
| 11 |
|
| 12 |
class Optimize extends Base { |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
parent::__construct(); |
| 16 |
} |
| 17 |
|
| 18 |
protected $features = [ |
| 19 |
'remove_global_styles', |
| 20 |
'disable_emojis', |
| 21 |
'disable_dashicons', |
| 22 |
'gutenberg', |
| 23 |
'font_preconnect', |
| 24 |
'query_strings', |
| 25 |
]; |
| 26 |
|
| 27 |
public function remove_global_styles() { |
| 28 |
add_action('init', [$this, 'removeGlobalStyles']); |
| 29 |
} |
| 30 |
|
| 31 |
public function removeGlobalStyles() { |
| 32 |
remove_action('wp_enqueue_scripts', 'wp_enqueue_global_styles'); |
| 33 |
remove_action('wp_footer', 'wp_enqueue_global_styles'); |
| 34 |
remove_action('wp_body_open', 'wp_global_styles_render_svg_filters'); |
| 35 |
remove_action('in_admin_header', 'wp_global_styles_render_svg_filters'); |
| 36 |
add_action('wp_enqueue_scripts', function() { |
| 37 |
wp_dequeue_style('global-styles'); |
| 38 |
wp_dequeue_style('classic-theme-styles'); |
| 39 |
}, 100); |
| 40 |
} |
| 41 |
|
| 42 |
public function disable_emojis() { |
| 43 |
add_action('init', [$this, 'disableEmojis']); |
| 44 |
} |
| 45 |
|
| 46 |
public function disableEmojis() { |
| 47 |
remove_action('wp_head', 'print_emoji_detection_script', 7); |
| 48 |
remove_action('admin_print_scripts', 'print_emoji_detection_script'); |
| 49 |
remove_action('wp_print_styles', 'print_emoji_styles'); |
| 50 |
remove_action('admin_print_styles', 'print_emoji_styles'); |
| 51 |
remove_filter('the_content_feed', 'wp_staticize_emoji'); |
| 52 |
remove_filter('comment_text_rss', 'wp_staticize_emoji'); |
| 53 |
remove_filter('wp_mail', 'wp_staticize_emoji_for_email'); |
| 54 |
add_filter('tiny_mce_plugins', [$this, 'disableEmojisTinyMCE']); |
| 55 |
add_filter('wp_resource_hints', [$this, 'disableEmojisDNSPrefetch'], 10, 2); |
| 56 |
if (!is_admin()) { |
| 57 |
add_filter('emoji_svg_url', '__return_false'); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function disableEmojisTinyMCE($plugins) { |
| 62 |
return is_array($plugins) ? array_diff($plugins, ['wpemoji']) : []; |
| 63 |
} |
| 64 |
|
| 65 |
public function disableEmojisDNSPrefetch($urls, $relation_type) { |
| 66 |
if ($relation_type === 'dns-prefetch') { |
| 67 |
$emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/2.2.1/svg/'); |
| 68 |
$urls = array_diff($urls, [$emoji_svg_url]); |
| 69 |
} |
| 70 |
return $urls; |
| 71 |
} |
| 72 |
|
| 73 |
public function disable_dashicons() { |
| 74 |
add_action('wp_enqueue_scripts', [$this, 'disableDashicons']); |
| 75 |
} |
| 76 |
|
| 77 |
public function disableDashicons() { |
| 78 |
if (!is_user_logged_in()) { |
| 79 |
wp_dequeue_style('dashicons'); |
| 80 |
wp_deregister_style('dashicons'); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public function gutenberg() { |
| 85 |
add_action('wp_enqueue_scripts', [$this, 'remove_wp_block_library_css'], 100); |
| 86 |
} |
| 87 |
|
| 88 |
public function remove_wp_block_library_css() { |
| 89 |
wp_dequeue_style('wp-block-library'); |
| 90 |
wp_dequeue_style('wp-block-library-theme'); |
| 91 |
wp_dequeue_style('wc-block-style'); |
| 92 |
wp_dequeue_style('global-styles'); |
| 93 |
wp_dequeue_style('classic-theme-styles'); |
| 94 |
} |
| 95 |
|
| 96 |
public function font_preconnect() { |
| 97 |
add_action('wp_head', [$this, 'render_font_preconnect'], 1); |
| 98 |
} |
| 99 |
|
| 100 |
public function render_font_preconnect() { |
| 101 |
if (!is_admin()) { |
| 102 |
echo '<link rel="preconnect" href="https://fonts.googleapis.com">' . "\n"; |
| 103 |
echo '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>' . "\n"; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public function query_strings() { |
| 108 |
add_filter('script_loader_src', [$this, 'remove_parameter'], 9999); |
| 109 |
add_filter('style_loader_src', [$this, 'remove_parameter'], 9999); |
| 110 |
} |
| 111 |
|
| 112 |
public function remove_parameter($src) { |
| 113 |
if (is_admin() || empty($src)) { |
| 114 |
return $src; |
| 115 |
} |
| 116 |
return strpos($src, 'ver=') !== false ? remove_query_arg('ver', $src) : $src; |
| 117 |
} |
| 118 |
|
| 119 |
} |