-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolylang.php
50 lines (41 loc) · 1.61 KB
/
Polylang.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
<?php
namespace GeneroWP\GeneroCmp\Integrations;
use GeneroWP\GeneroCmp\Models\Consent;
use GeneroWP\GeneroCmp\Plugin;
use PLL_Integrations;
class Polylang
{
public function __construct(
protected Plugin $plugin,
) {
// Needs to run before plugins_loaded 0
add_filter('pll_is_cache_active', '__return_true');
// Needs to run after pll_init 10
add_action('pll_init', [$this, 'init'], 11);
}
public function init(): void
{
// We need to be in cache-compatibility mode to not set the cookie in PHP
/** @var \PLL_Cache_Compat $pllCacheIntegration */
$pllCacheIntegration = PLL_Integrations::instance()->cache_compat;
// Remove the default script and instead wait for consent
remove_action('wp_print_footer_scripts', [$pllCacheIntegration, 'add_cookie_script']);
add_action('wp_print_footer_scripts', [$this, 'addConsentTag']);
}
public function addConsentTag(): void
{
/** @var \PLL_Cache_Compat $pllCacheIntegration */
$pllCacheIntegration = PLL_Integrations::instance()->cache_compat;
ob_start();
$pllCacheIntegration->add_cookie_script();
$script = ob_get_clean();
if (str_contains($script, ' type="text/javascript"')) {
$script = str_replace(' type="text/javascript"', '', $script);
}
$script = str_replace(['<script>', '</script>', "<script type='text/javascript'>"], '', $script);
echo wp_get_inline_script_tag($script, [
'data-gds-cmp-consent' => Consent::PREFERENCES,
'type' => 'text/plain',
]);
}
}