Test Failed
Push — master ( 6db55f...0b650e )
by Chris
21:02
created

EmbedDisabler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 1
b 0
f 0
dl 0
loc 56
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A disableEmbedsRewrites() 0 9 3
A doInitAction() 0 3 1
A disableEmbedsTinyMcePlugin() 0 3 1
A hook() 0 3 1
A disableEmbeds() 0 23 1
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