| 1 |
<?php |
| 2 |
/** |
| 3 |
* Media Optimization |
| 4 |
* |
| 5 |
* @package PoweredCache |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PoweredCache; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class MediaOptimizer |
| 12 |
*/ |
| 13 |
class MediaOptimizer { |
| 14 |
/** |
| 15 |
* Plugin settings |
| 16 |
* |
| 17 |
* @var array $settings |
| 18 |
*/ |
| 19 |
private $settings; |
| 20 |
|
| 21 |
/** |
| 22 |
* Return an instance of the current class |
| 23 |
* |
| 24 |
* @return MediaOptimizer |
| 25 |
*/ |
| 26 |
public static function factory() { |
| 27 |
static $instance = false; |
| 28 |
|
| 29 |
if ( ! $instance ) { |
| 30 |
$instance = new self(); |
| 31 |
$instance->setup(); |
| 32 |
} |
| 33 |
|
| 34 |
return $instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Setup routine |
| 39 |
*/ |
| 40 |
public function setup() { |
| 41 |
$this->settings = \PoweredCache\Utils\get_settings(); |
| 42 |
|
| 43 |
if ( $this->settings['disable_wp_embeds'] ) { |
| 44 |
add_action( 'init', [ $this, 'disable_embed' ], 9999 ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( $this->settings['disable_emoji_scripts'] ) { |
| 48 |
add_action( 'init', [ $this, 'disable_emoji' ] ); |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Disable WP embeds |
| 55 |
*/ |
| 56 |
public function disable_embed() { |
| 57 |
remove_action( 'rest_api_init', 'wp_oembed_register_route' ); |
| 58 |
add_filter( 'embed_oembed_discover', '__return_false' ); |
| 59 |
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 ); |
| 60 |
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); |
| 61 |
remove_action( 'wp_head', 'wp_oembed_add_host_js' ); |
| 62 |
remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 ); |
| 63 |
|
| 64 |
add_filter( 'tiny_mce_plugins', [ $this, 'disable_embeds_tinymce' ] ); |
| 65 |
add_filter( 'rewrite_rules_array', [ $this, 'disable_embeds_rewrite' ] ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Remove wpembed from the registered plugins |
| 70 |
* |
| 71 |
* @param array $plugins registered tinymce plugins |
| 72 |
* |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
public function disable_embeds_tinymce( $plugins ) { |
| 76 |
return array_diff( $plugins, [ 'wpembed' ] ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Remove embed specific rewrite rules |
| 81 |
* |
| 82 |
* @param array $rules rewrite rules |
| 83 |
* |
| 84 |
* @return mixed |
| 85 |
*/ |
| 86 |
public function disable_embeds_rewrite( $rules ) { |
| 87 |
foreach ( $rules as $rule => $rewrite ) { |
| 88 |
if ( false !== strpos( $rewrite, 'embed=true' ) ) { |
| 89 |
unset( $rules[ $rule ] ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return $rules; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Disable emoji |
| 98 |
*/ |
| 99 |
public function disable_emoji() { |
| 100 |
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
| 101 |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 102 |
remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 103 |
remove_action( 'admin_print_styles', 'print_emoji_styles' ); |
| 104 |
remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); |
| 105 |
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); |
| 106 |
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); |
| 107 |
add_filter( 'tiny_mce_plugins', [ $this, 'disable_emoji_tinymce' ] ); |
| 108 |
add_filter( 'wp_resource_hints', [ $this, 'disable_emoji_dns_prefetch' ], 10, 2 ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Remove emoji from plugins |
| 113 |
* |
| 114 |
* @param array $plugins tinymce plugins |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function disable_emoji_tinymce( $plugins ) { |
| 119 |
if ( is_array( $plugins ) ) { |
| 120 |
return array_diff( $plugins, [ 'wpemoji' ] ); |
| 121 |
} |
| 122 |
|
| 123 |
return []; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Remove emoji CDN from prefetch |
| 128 |
* |
| 129 |
* @param array $urls prefetch urls |
| 130 |
* @param string $relation_type rel type |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
public function disable_emoji_dns_prefetch( $urls, $relation_type ) { |
| 135 |
if ( 'dns-prefetch' === $relation_type ) { |
| 136 |
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' ); |
| 137 |
|
| 138 |
$urls = array_diff( $urls, [ $emoji_svg_url ] ); |
| 139 |
} |
| 140 |
|
| 141 |
return $urls; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
} |
| 146 |
|