1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leonidas\Framework\Site\Module; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Leonidas\Framework\Module\Abstracts\Module; |
7
|
|
|
use Leonidas\Hooks\TargetsInitHook; |
8
|
|
|
|
9
|
|
|
class EmbedDisabler extends Module |
10
|
|
|
{ |
11
|
|
|
use TargetsInitHook; |
12
|
|
|
|
13
|
|
|
public function hook(): void |
14
|
|
|
{ |
15
|
|
|
$this->targetInitHook(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
protected function doInitAction(): void |
19
|
|
|
{ |
20
|
|
|
$this->disableEmbeds(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Disable WP embeds |
25
|
|
|
*/ |
26
|
|
|
protected function disableEmbeds(): void |
27
|
|
|
{ |
28
|
|
|
// Remove the REST API endpoint. |
29
|
|
|
remove_action('rest_api_init', 'wp_oembed_register_route'); |
30
|
|
|
|
31
|
|
|
// Turn off oEmbed auto discovery. |
32
|
|
|
add_filter('embed_oembed_discover', '__return_false'); |
33
|
|
|
|
34
|
|
|
// Don't filter oEmbed results. |
35
|
|
|
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10); |
36
|
|
|
|
37
|
|
|
// Remove oEmbed discovery links. |
38
|
|
|
remove_action('wp_head', 'wp_oembed_add_discovery_links'); |
39
|
|
|
|
40
|
|
|
// Remove oEmbed-specific JavaScript from the front-end and back-end. |
41
|
|
|
remove_action('wp_head', 'wp_oembed_add_host_js'); |
42
|
|
|
add_filter('tiny_mce_plugins', Closure::fromCallable([$this, 'disableEmbedsTinyMcePlugin'])); |
43
|
|
|
|
44
|
|
|
// Remove all embeds rewrite rules. |
45
|
|
|
add_filter('rewrite_rules_array', Closure::fromCallable([$this, 'disableEmbedsRewrites'])); |
46
|
|
|
|
47
|
|
|
// Remove filter of the oEmbed result before any HTTP requests are made. |
48
|
|
|
remove_filter('pre_oembed_result', 'wp_filter_pre_oembed_result', 10); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function disableEmbedsTinyMcePlugin(array $plugins): array |
52
|
|
|
{ |
53
|
|
|
return array_diff($plugins, ['wpembed']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function disableEmbedsRewrites($rules) |
57
|
|
|
{ |
58
|
|
|
foreach ($rules as $rule => $rewrite) { |
59
|
|
|
if (false !== strpos($rewrite, 'embed=true')) { |
60
|
|
|
unset($rules[$rule]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $rules; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|