-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
235 lines (210 loc) · 8.06 KB
/
Plugin.php
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace GeneroWP\GeneroCmp;
use GeneroWP\GeneroCmp\Integrations\CtxFeed;
use GeneroWP\GeneroCmp\Integrations\DataLayer;
use GeneroWP\GeneroCmp\Integrations\DuracelltommiGoogleTagManager;
use GeneroWP\GeneroCmp\Integrations\ElfsightInstagramFeed;
use GeneroWP\GeneroCmp\Integrations\Embeds;
use GeneroWP\GeneroCmp\Integrations\FacebookForWooCommerce;
use GeneroWP\GeneroCmp\Integrations\GoogleAppsLogin;
use GeneroWP\GeneroCmp\Integrations\Gravityforms;
use GeneroWP\GeneroCmp\Integrations\PixelYourSite;
use GeneroWP\GeneroCmp\Integrations\Polylang;
use GeneroWP\GeneroCmp\Integrations\Redirection;
use GeneroWP\GeneroCmp\Integrations\TiktokForBusiness;
use GeneroWP\GeneroCmp\Integrations\WpConsentApi;
use GeneroWP\GeneroCmp\Integrations\WpTiktokFeed;
use GeneroWP\GeneroCmp\Models\Consent;
class Plugin
{
public string $name = 'genero-cmp';
public string $file;
public string $path;
public string $url;
protected static Plugin $instance;
public static function getInstance(): self
{
if (! isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct()
{
$this->file = realpath(__DIR__ . '/../genero-cmp.php');
$this->path = untrailingslashit(plugin_dir_path($this->file));
$this->url = untrailingslashit(plugin_dir_url($this->file));
add_action('init', [$this, 'loadTextdomain']);
add_action('wp_enqueue_scripts', [$this, 'registerAssets']);
add_action('enqueue_block_editor_assets', [$this, 'blockEditorAssets'], 0);
add_action('plugins_loaded', [$this, 'initializeIntegrations'], -1);
add_filter('register_block_type_args', [$this, 'registerBlockAttributes']);
new Admin($this->name, $this);
new Frontend($this->name, $this);
}
public function settings(?string $option = null)
{
$options = get_option($this->name) ?: [];
return $option ? ($options[$option] ?? null) : $options;
}
public function initializeIntegrations(): void
{
new CtxFeed($this);
new DataLayer($this);
new DuracelltommiGoogleTagManager($this);
new Embeds($this);
new FacebookForWooCommerce($this);
new Gravityforms($this);
new TiktokForBusiness($this);
new WpConsentApi($this);
new Polylang($this);
new GoogleAppsLogin($this);
new Redirection($this);
new WpTiktokFeed($this);
new ElfsightInstagramFeed($this);
new PixelYourSite($this);
}
public function registerBlockAttributes(array $args): array
{
$args['attributes']['consentOptin'] = [
'type' => 'boolean',
'default' => false,
];
$args['attributes']['consentOptout'] = [
'type' => 'boolean',
'default' => false,
];
return $args;
}
/**
* Assets
*/
public function registerAssets(): void
{
wp_register_script(
"{$this->name}/js",
"{$this->url}/dist/main.js",
[],
filemtime($this->path . '/dist/main.js'),
version_compare($GLOBALS['wp_version'], '6.3') >= 0 ? ['strategy' => 'async'] : false,
);
// Support async loading if a plugin implements it.
wp_script_add_data("{$this->name}/js", 'async', true);
wp_add_inline_script(
"{$this->name}/js",
file_get_contents($this->path . '/dist/inline.js'),
'before'
);
wp_localize_script("{$this->name}/js", 'gdsCmp', [
'consents' => $this->consentCategories(),
'necessary_cookies' => $this->necessaryCookies(),
]);
wp_register_style(
"{$this->name}/css",
"{$this->url}/dist/main.css",
[],
filemtime($this->path . '/dist/main.css')
);
wp_style_add_data("{$this->name}/css", 'path', "{$this->path}/dist/main.css");
wp_register_script(
"{$this->name}/tcfapi/js",
"{$this->url}/dist/tcfapi.js",
[],
filemtime($this->path . '/dist/tcfapi.js'),
version_compare($GLOBALS['wp_version'], '6.3') >= 0 ? ['strategy' => 'async'] : false,
);
// Support async loading if a plugin implements it.
wp_script_add_data("{$this->name}/tcfapi/js", 'async', true);
}
public function blockEditorAssets(): void
{
if ($manifest = include $this->path . '/dist/editor.asset.php') {
wp_enqueue_script(
"{$this->name}/editor.js",
"{$this->url}/dist/editor.js",
$manifest['dependencies'],
filemtime($this->path . '/dist/editor.js')
);
wp_localize_script("{$this->name}/editor.js", 'gdsCmp', [
'consents' => $this->consentCategories(),
]);
}
}
/**
* Text domain
*/
public function loadTextdomain(): void
{
load_plugin_textdomain(
$this->name,
false,
dirname(plugin_basename($this->file)) . '/languages'
);
}
/**
* @return Consent[]
*/
public function consentCategories(): array
{
$consents[] = new Consent(
id: Consent::NECESSARY,
label: __('Necessary', 'genero-cmp'),
description: __('These cookies are technically required for our core website to work properly, e.g. security functions or your cookie consent preferences.', 'genero-cmp'),
necessary: true,
consent: true,
wpConsentApiCategory: 'functional',
gtmConsentModes: ['security_storage'],
);
$consents[] = new Consent(
id: Consent::PREFERENCES,
label: __('Preferences', 'genero-cmp'),
description: __('Preference cookies enable a website to remember information that changes the way the website behaves or looks, like your preferred language or the region that you are in.', 'genero-cmp'),
wpConsentApiCategory: 'preferences',
// Noting that we do not distinguish functional from necessary so we use "preferences"
gtmConsentModes: ['functionality_storage', 'personalization_storage'],
);
$consents[] = new Consent(
id: Consent::STATISTICS,
label: __('Statistics', 'genero-cmp'),
description: __('In order to improve our website going forward, we anonymously collect data for statistical and analytical purposes. With these cookies we can, for instance, monitor the number or duration of visits of specific pages of our website helping us in optimizing user experience.', 'genero-cmp'),
wpConsentApiCategory: 'statistics',
gtmConsentModes: ['analytics_storage'],
);
$consents[] = new Consent(
id: Consent::MARKETING,
label: __('Marketing', 'genero-cmp'),
description: __('These cookies help us in measuring and optimizing our marketing efforts.', 'genero-cmp'),
wpConsentApiCategory: 'marketing',
gtmConsentModes: ['ad_storage', 'ad_user_data', 'ad_personalization'],
);
return apply_filters('gds_cmp_consents', $consents);
}
public function getConsentCategory(string $id): ?Consent
{
foreach ($this->consentCategories() as $consent) {
if ($consent->id === $id) {
return $consent;
}
}
return null;
}
public function consentHash(): string
{
return md5(json_encode(
array_map(fn(Consent $consent) => $consent->hash(), $this->consentCategories())
));
}
public function necessaryCookies(): array
{
return apply_filters('gds_cmp_necessary_cookies', [
'wp-settings-',
'wp-settings-time-',
'wordpress_test_cookie',
'wordpress_logged_in_',
'wordpress_sec_',
'wp_woocommerce_session_',
'woocommerce_cart_hash',
'woocommerce_items_in_cart',
]);
}
}