-
Notifications
You must be signed in to change notification settings - Fork 24
/
script-loader-tag.php
57 lines (50 loc) · 1.24 KB
/
script-loader-tag.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
<?php
namespace cookiebot_addons\lib\script_loader_tag;
class Script_Loader_Tag implements Script_Loader_Tag_Interface {
/**
* List of tags to load in cookiebot attributes
*
* @var array
*
* @since 1.1.0
*/
private $tags = array();
/**
* Cookiebot_Script_Loader_Tag constructor.
* Adds filter to enhance script attribute
*
* @since 1.1.0
*/
public function __construct() {
add_filter( 'script_loader_tag', array( $this, 'cookiebot_add_consent_attribute_to_tag' ), 10, 3 );
}
/**
* Adds enqueue script handle tag to the array of tags.
* So that the script can be updated with cookieconsent attribute.
*
* @param $tag string Enqueue script handle name
* @param $type array
*
* @since 1.2.0
*/
public function add_tag( $tag, $type ) {
$this->tags[ $tag ] = $type;
}
/**
* Modifies external links to google analytics
*
* @param $tag
* @param $handle
* @param $src
*
* @return string
*
* @since 1.2.0
*/
public function cookiebot_add_consent_attribute_to_tag( $tag, $handle, $src ) {
if ( array_key_exists( $handle, $this->tags ) ) {
return '<script src="' . $src . '" type="text/plain" data-cookieconsent="' . implode( ',', $this->tags[ $handle ] ) . '"></script>';
}
return $tag;
}
}