Passed
Push — master ( bf78a1...9e90bd )
by Dominik
05:18 queued 19s
created

isTrackingEnabled()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Components\FeatureGoogleAnalytics;
4
5
use Flynt\Utils\Options;
6
use Timber\Timber;
7
8
function isTrackingEnabled($gaId)
9
{
10
    if ($gaId) {
11
        $user = wp_get_current_user();
12
        $trackingEnabled = $gaId === 'debug' || !in_array('administator', $user->roles);
13
        return $trackingEnabled;
14
    }
15
    return false;
16
}
17
18
add_filter('Flynt/addComponentData?name=FeatureGoogleAnalytics', function ($data) {
19
    $googleAnalyticsOptions = Options::getGlobal('GoogleAnalytics');
20
21
    if ($googleAnalyticsOptions) {
22
        $isTrackingEnabled = isTrackingEnabled($googleAnalyticsOptions['gaId']);
23
        $data['jsonData'] = json_encode([
24
            'gaId' => $googleAnalyticsOptions['gaId'],
25
            'anonymizeIp' => $googleAnalyticsOptions['anonymizeIp'],
26
            'isOptInComponentRegistered' => did_action('Flynt/thirdPartyCookies/initializeOptions'),
27
        ]);
28
        $data['isTrackingEnabled'] = $isTrackingEnabled;
29
    }
30
31
    return $data;
32
});
33
34
add_action('Flynt/thirdPartyCookies/initializeOptions', function () {
35
    Options::addTranslatable('GoogleAnalytics', [
36
        [
37
            'label' => 'Accept Google Analytics label',
38
            'name' => 'acceptGoogleAnalyticsLabel',
39
            'type' => 'text',
40
            'default_value' => 'Google Analytics Cookies',
41
            'required' => 1,
42
        ],
43
    ]);
44
});
45
46
add_action('wp_footer', function () {
47
    $context = Timber::get_context();
48
    Timber::render_string('{{ renderComponent("FeatureGoogleAnalytics") }}', $context);
49
});
50
51
add_filter('Flynt/thirdPartyCookies', function ($features) {
52
    $googleAnalyticsTranslatableOptions = Options::getTranslatable('GoogleAnalytics');
53
54
    $features = array_merge($features, [
55
        [
56
            'id' => 'GA_accept',
57
            'name' => 'GA_accept',
58
            'label' => $googleAnalyticsTranslatableOptions['acceptGoogleAnalyticsLabel'],
59
        ]
60
    ]);
61
    return $features;
62
});
63
64
Options::addGlobal('GoogleAnalytics', [
65
    [
66
        'name' => 'gaId',
67
        'label' => 'Google Analytics ID',
68
        'type' => 'text',
69
        'maxlength' => 20,
70
        'prepend' => '',
71
        'append' => '',
72
        'placeholder' => 'XX-XXXXXXXX-X',
73
        'instructions' => 'You can enter \'debug\' to activate debug mode. It will only log to console and overwrite all other settings.'
74
    ],
75
    [
76
        'name' => 'anonymizeIp',
77
        'label' => 'Anonymize IP',
78
        'type' => 'true_false',
79
        'ui' => 'no',
80
        'default' => 1,
81
        'ui_on_text' => 'Yes',
82
        'ui_off_text' => 'No',
83
        'message' => ''
84
    ],
85
]);
86