-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTagManagerExtended.php
More file actions
172 lines (151 loc) · 5.89 KB
/
TagManagerExtended.php
File metadata and controls
172 lines (151 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\TagManagerExtended;
class TagManagerExtended extends \Piwik\Plugin
{
public function registerEvents()
{
return array(
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'AssetManager.getJavaScriptFiles' => 'getJavaScriptFiles',
'TagManager.filterTags' => 'filterTags',
'TagManager.filterVariables' => 'filterVariables',
'TagManager.filterTriggers' => 'filterTriggers',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
'API.TagManager.getContainerTags.end' => 'fillMissingTypeMetadata',
'API.TagManager.getContainerTag.end' => 'fillMissingTypeMetadata',
'API.TagManager.getContainerTriggers.end' => 'fillMissingTypeMetadata',
'API.TagManager.getContainerTrigger.end' => 'fillMissingTypeMetadata',
'API.TagManager.getContainerVariables.end' => 'fillMissingTypeMetadata',
'API.TagManager.getContainerVariable.end' => 'fillMissingTypeMetadata',
);
}
/**
* When an entity (tag, trigger, variable) references a type that is no longer
* registered (plugin removed/disabled, or a built-in type filtered out), the
* core sets typeMetadata to null. The Vue frontend then crashes while rendering
* the list because it reads typeMetadata.description / .name unguarded.
*
* Inject a minimal placeholder typeMetadata so the row renders gracefully and
* the user can still see, edit or delete the orphan entity.
*/
public function fillMissingTypeMetadata(&$returnedValue, $extraInfo)
{
if (empty($returnedValue)) {
return;
}
if ($this->isEntityArray($returnedValue)) {
$this->ensureTypeMetadata($returnedValue);
return;
}
if (is_array($returnedValue)) {
foreach ($returnedValue as &$entity) {
if ($this->isEntityArray($entity)) {
$this->ensureTypeMetadata($entity);
}
}
}
}
private function isEntityArray($value): bool
{
return is_array($value) && array_key_exists('typeMetadata', $value) && array_key_exists('type', $value);
}
private function ensureTypeMetadata(array &$entity): void
{
if (!empty($entity['typeMetadata'])) {
return;
}
$missingType = isset($entity['type']) ? (string) $entity['type'] : '';
$label = \Piwik\Piwik::translate('TagManagerExtended_MissingType', [$missingType !== '' ? $missingType : '?']);
$entity['typeMetadata'] = array(
'id' => $missingType,
'name' => $label,
'description' => $label,
'category' => '',
'icon' => '',
'help' => '',
'order' => 9999,
'contexts' => array(),
'hasAdvancedSettings' => false,
'isCustomTemplate' => false,
'parameters' => array(),
);
}
public function getClientSideTranslationKeys(&$translationKeys)
{
$translationKeys[] = 'TagManagerExtended_BulkActions';
$translationKeys[] = 'TagManagerExtended_SelectAll';
$translationKeys[] = 'TagManagerExtended_DeselectAll';
$translationKeys[] = 'TagManagerExtended_Selected';
$translationKeys[] = 'TagManagerExtended_BulkDelete';
$translationKeys[] = 'TagManagerExtended_BulkPause';
$translationKeys[] = 'TagManagerExtended_BulkResume';
$translationKeys[] = 'TagManagerExtended_ConfirmBulkDelete';
$translationKeys[] = 'TagManagerExtended_ConfirmBulkPause';
$translationKeys[] = 'TagManagerExtended_ConfirmBulkResume';
$translationKeys[] = 'TagManagerExtended_BulkSuccess';
$translationKeys[] = 'TagManagerExtended_BulkPartialSuccess';
}
public function getStylesheetFiles(&$files)
{
$files[] = "plugins/TagManagerExtended/stylesheets/style.less";
}
public function getJavaScriptFiles(&$files)
{
$files[] = "plugins/TagManagerExtended/javascripts/script.js";
}
public function filterTags(&$tags)
{
$found = false;
foreach ($tags as $key => &$tag) {
if (in_array($tag->getId(), ['Axeptio', 'CookieYes', 'Cookiebot', 'OneTrust', 'Hotjar', 'GoogleAdsConversion', 'GoogleAnalytics4Event', 'GoogleTag']) && $this->isPartOfTagManagerPlugin($tag)) {
$found = true;
unset($tags[$key]);
}
}
if ($found) {
$tags = array_values($tags);
}
}
public function filterVariables(&$variables)
{
$found = false;
foreach ($variables as $key => &$variable) {
if (in_array($variable->getId(), ['ClickDataAttribute']) && $this->isPartOfTagManagerPlugin($variable)) {
$found = true;
unset($variables[$key]);
}
}
if ($found) {
$variables = array_values($variables);
}
}
public function filterTriggers(&$triggers)
{
$found = false;
foreach ($triggers as $key => &$trigger) {
if (in_array($trigger->getId(), ['CustomEvent']) && $this->isPartOfTagManagerPlugin($trigger)) {
$found = true;
unset($triggers[$key]);
}
}
if ($found) {
$triggers = array_values($triggers);
}
}
private function isPartOfTagManagerPlugin($object): bool
{
$classname = get_class($object);
$parts = explode('\\', $classname);
$pluginName = 'TagManager';
if (count($parts) >= 4 && $parts[1] === 'Plugins') {
$pluginName = $parts[2];
}
return $pluginName === 'TagManager';
}
}