Passed
Push — master ( c01ad7...11b010 )
by Evgenii
01:14
created

Snippet::head()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helick\GTM;
4
5
use Helick\GTM\Contracts\Bootable;
6
7
final class Snippet implements Bootable
8
{
9
    /**
10
     * Boot the service.
11
     *
12
     * @return void
13
     */
14
    public static function boot(): void
15
    {
16
        $self = new static;
17
18
        add_action('wp_head', [$self, 'head'], 1, 0);
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
        /** @scrutinizer ignore-call */ 
19
        add_action('wp_head', [$self, 'head'], 1, 0);
Loading history...
19
        add_action('wp_body_open', [$self, 'body'], 1, 0);
20
    }
21
22
    /**
23
     * Display the head snippet.
24
     *
25
     * @return void
26
     */
27
    public function head(): void
28
    {
29
        $this->displayDataLayer();
30
31
        $this->displayHeadSnippet();
32
    }
33
34
    /**
35
     * Display the body snippet.
36
     *
37
     * @return void
38
     */
39
    public function body(): void
40
    {
41
        $snippet = <<<HTML
42
<!-- Google Tag Manager (noscript) -->
43
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=%s"
44
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
45
<!-- End Google Tag Manager (noscript) -->
46
HTML;
47
48
        $containerId = containerId();
49
50
        echo sprintf($snippet, esc_js($containerId)), "\n";
0 ignored issues
show
Bug introduced by
The function esc_js was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        echo sprintf($snippet, /** @scrutinizer ignore-call */ esc_js($containerId)), "\n";
Loading history...
51
    }
52
53
    /**
54
     * Display the data layer.
55
     *
56
     * @return void
57
     */
58
    private function displayDataLayer(): void
59
    {
60
        $snippet = '<script>%s=[%s]</script>';
61
62
        $dataLayerVariable = dataLayerVariable();
63
        $dataLayer         = dataLayer();
64
65
        if (empty($dataLayer)) {
66
            return;
67
        }
68
69
        echo sprintf(
70
            $snippet,
71
            esc_js($dataLayerVariable),
0 ignored issues
show
Bug introduced by
The function esc_js was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            /** @scrutinizer ignore-call */ 
72
            esc_js($dataLayerVariable),
Loading history...
72
            wp_json_encode($dataLayer)
0 ignored issues
show
Bug introduced by
The function wp_json_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            /** @scrutinizer ignore-call */ 
73
            wp_json_encode($dataLayer)
Loading history...
73
        ), "\n";
74
    }
75
76
    /**
77
     * Display the head snippet.
78
     *
79
     * @return void
80
     */
81
    private function displayHeadSnippet(): void
82
    {
83
        $snippet = <<<HTML
84
<!-- Google Tag Manager -->
85
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
86
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
87
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
88
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
89
})(window,document,'script','%s','%s');</script>
90
<!-- End Google Tag Manager -->
91
HTML;
92
93
        $dataLayerVariable = dataLayerVariable();
94
        $containerId       = containerId();
95
96
        echo sprintf(
97
            $snippet,
98
            esc_js($dataLayerVariable),
0 ignored issues
show
Bug introduced by
The function esc_js was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
            /** @scrutinizer ignore-call */ 
99
            esc_js($dataLayerVariable),
Loading history...
99
            esc_js($containerId)
100
        ), "\n";
101
    }
102
}
103