Passed
Push — master ( 11b010...ea8435 )
by Evgenii
01:20
created

Snippet::dataLayer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.9332
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, 'dataLayer'], 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, 'dataLayer'], 1, 0);
Loading history...
19
        add_action('wp_head', [$self, 'head'], 1, 0);
20
        add_action('wp_body_open', [$self, 'body'], 1, 0);
21
    }
22
23
    /**
24
     * Display the data layer snippet.
25
     *
26
     * @return void
27
     */
28
    public function dataLayer(): void
29
    {
30
        $snippet = '<script>%s=[%s]</script>';
31
32
        $dataLayerVariable = dataLayerVariable();
33
        $dataLayer         = dataLayer();
34
35
        if (empty($dataLayer)) {
36
            return;
37
        }
38
39
        echo sprintf(
40
            $snippet,
41
            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

41
            /** @scrutinizer ignore-call */ 
42
            esc_js($dataLayerVariable),
Loading history...
42
            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

42
            /** @scrutinizer ignore-call */ 
43
            wp_json_encode($dataLayer)
Loading history...
43
        ), "\n";
44
    }
45
46
    /**
47
     * Display the head snippet.
48
     *
49
     * @return void
50
     */
51
    public function head(): void
52
    {
53
        $snippet = <<<HTML
54
<!-- Google Tag Manager -->
55
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
56
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
57
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
58
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
59
})(window,document,'script','%s','%s');</script>
60
<!-- End Google Tag Manager -->
61
HTML;
62
63
        $dataLayerVariable = dataLayerVariable();
64
        $containerId       = containerId();
65
66
        echo sprintf(
67
            $snippet,
68
            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

68
            /** @scrutinizer ignore-call */ 
69
            esc_js($dataLayerVariable),
Loading history...
69
            esc_js($containerId)
70
        ), "\n";
71
    }
72
73
    /**
74
     * Display the body snippet.
75
     *
76
     * @return void
77
     */
78
    public function body(): void
79
    {
80
        $snippet = <<<HTML
81
<!-- Google Tag Manager (noscript) -->
82
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=%s"
83
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
84
<!-- End Google Tag Manager (noscript) -->
85
HTML;
86
87
        $containerId = containerId();
88
89
        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

89
        echo sprintf($snippet, /** @scrutinizer ignore-call */ esc_js($containerId)), "\n";
Loading history...
90
    }
91
}
92