buffer
3 years ago
script_loader_tag
2 years ago
traits
3 years ago
Consent_API_Helper.php
2 years ago
Cookie_Consent.php
3 years ago
Cookie_Consent_Interface.php
4 years ago
Cookiebot_Activated.php
2 years ago
Cookiebot_Automatic_Updates.php
3 years ago
Cookiebot_Deactivated.php
4 years ago
Cookiebot_Javascript_Helper.php
1 year ago
Cookiebot_Review.php
2 years ago
Cookiebot_WP.php
1 year ago
Dependency_Container.php
3 years ago
Settings_Page_Tab.php
3 years ago
Settings_Service.php
3 years ago
Settings_Service_Interface.php
3 years ago
Supported_Languages.php
4 years ago
Supported_Regions.php
2 years ago
WP_Rocket_Helper.php
3 years ago
Widgets.php
3 years ago
global-deprecations.php
3 years ago
helper.php
2 years ago
Consent_API_Helper.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\lib; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | |
| 7 | class Consent_API_Helper { |
| 8 | public function register_hooks() { |
| 9 | // Include integration to WP Consent Level API if available |
| 10 | if ( $this->is_wp_consent_api_active() ) { |
| 11 | add_action( 'wp_enqueue_scripts', array( $this, 'cookiebot_enqueue_consent_api_scripts' ) ); |
| 12 | add_filter( 'wp_get_consent_type', array( $this, 'wp_consent_api_get_consent_type' ) ); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | public function wp_consent_api_get_consent_type() { |
| 17 | $region = get_option( 'cookiebot-primary-domain-region' ); |
| 18 | return ! empty( $region ) ? self::get_consent_type( $region ) : 'optin'; |
| 19 | } |
| 20 | |
| 21 | public static function get_consent_type( $region ) { |
| 22 | $consent_type = 'optin'; |
| 23 | |
| 24 | if ( in_array( $region, Supported_Regions::OPTOUT_REGIONS, true ) ) { |
| 25 | $consent_type = 'optout'; |
| 26 | } |
| 27 | |
| 28 | return $consent_type; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Cookiebot_WP Check if WP Cookie Consent API is active |
| 33 | * |
| 34 | * @version 3.5.0 |
| 35 | * @since 3.5.0 |
| 36 | */ |
| 37 | public function is_wp_consent_api_active() { |
| 38 | return is_plugin_active( 'wp-consent-api/wp-consent-api.php' ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Cookiebot_WP Enqueue JS for integration with WP Consent Level API |
| 43 | * |
| 44 | * @throws InvalidArgumentException |
| 45 | * @since 3.5.0 |
| 46 | * @version 3.5.0 |
| 47 | */ |
| 48 | public function cookiebot_enqueue_consent_api_scripts() { |
| 49 | wp_register_script( |
| 50 | 'cookiebot-wp-consent-level-api-integration', |
| 51 | asset_url( 'js/frontend/cookiebot-wp-consent-level-api-integration.js' ), |
| 52 | null, |
| 53 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 54 | false |
| 55 | ); |
| 56 | wp_enqueue_script( 'cookiebot-wp-consent-level-api-integration' ); |
| 57 | wp_localize_script( |
| 58 | 'cookiebot-wp-consent-level-api-integration', |
| 59 | 'cookiebot_category_mapping', |
| 60 | $this->get_wp_consent_api_mapping() |
| 61 | ); |
| 62 | wp_localize_script( |
| 63 | 'cookiebot-wp-consent-level-api-integration', |
| 64 | 'cookiebot_consent_type', |
| 65 | array( |
| 66 | 'type' => $this->wp_consent_api_get_consent_type(), |
| 67 | ) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Cookiebot_WP Get the mapping between Consent Level API and Cookiebot |
| 73 | * Returns array where key is the consent level api category and value |
| 74 | * is the mapped Cookiebot category. |
| 75 | * |
| 76 | * @version 3.5.0 |
| 77 | * @since 3.5.0 |
| 78 | */ |
| 79 | public function get_wp_consent_api_mapping() { |
| 80 | $default_wp_consent_api_mapping = $this->get_default_wp_consent_api_mapping(); |
| 81 | $mapping = get_option( 'cookiebot-consent-mapping', $default_wp_consent_api_mapping ); |
| 82 | |
| 83 | $mapping = ( '' === $mapping ) ? $default_wp_consent_api_mapping : $mapping; |
| 84 | |
| 85 | foreach ( $default_wp_consent_api_mapping as $k => $v ) { |
| 86 | if ( ! isset( $mapping[ $k ] ) ) { |
| 87 | $mapping[ $k ] = $v; |
| 88 | } else { |
| 89 | foreach ( $v as $vck => $vcv ) { |
| 90 | if ( ! isset( $mapping[ $k ][ $vck ] ) ) { |
| 91 | $mapping[ $k ][ $vck ] = $vcv; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return $mapping; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Cookiebot_WP Default consent level mappings |
| 102 | * |
| 103 | * @version 3.5.0 |
| 104 | * @since 3.5.0 |
| 105 | */ |
| 106 | public function get_default_wp_consent_api_mapping() { |
| 107 | return array( |
| 108 | 'n=1;p=1;s=1;m=1' => |
| 109 | array( |
| 110 | 'preferences' => 1, |
| 111 | 'statistics' => 1, |
| 112 | 'statistics-anonymous' => 0, |
| 113 | 'marketing' => 1, |
| 114 | ), |
| 115 | 'n=1;p=1;s=1;m=0' => |
| 116 | array( |
| 117 | 'preferences' => 1, |
| 118 | 'statistics' => 1, |
| 119 | 'statistics-anonymous' => 1, |
| 120 | 'marketing' => 0, |
| 121 | ), |
| 122 | 'n=1;p=1;s=0;m=1' => |
| 123 | array( |
| 124 | 'preferences' => 1, |
| 125 | 'statistics' => 0, |
| 126 | 'statistics-anonymous' => 0, |
| 127 | 'marketing' => 1, |
| 128 | ), |
| 129 | 'n=1;p=1;s=0;m=0' => |
| 130 | array( |
| 131 | 'preferences' => 1, |
| 132 | 'statistics' => 0, |
| 133 | 'statistics-anonymous' => 0, |
| 134 | 'marketing' => 0, |
| 135 | ), |
| 136 | 'n=1;p=0;s=1;m=1' => |
| 137 | array( |
| 138 | 'preferences' => 0, |
| 139 | 'statistics' => 1, |
| 140 | 'statistics-anonymous' => 0, |
| 141 | 'marketing' => 1, |
| 142 | ), |
| 143 | 'n=1;p=0;s=1;m=0' => |
| 144 | array( |
| 145 | 'preferences' => 0, |
| 146 | 'statistics' => 1, |
| 147 | 'statistics-anonymous' => 0, |
| 148 | 'marketing' => 0, |
| 149 | ), |
| 150 | 'n=1;p=0;s=0;m=1' => |
| 151 | array( |
| 152 | 'preferences' => 0, |
| 153 | 'statistics' => 0, |
| 154 | 'statistics-anonymous' => 0, |
| 155 | 'marketing' => 1, |
| 156 | ), |
| 157 | 'n=1;p=0;s=0;m=0' => |
| 158 | array( |
| 159 | 'preferences' => 0, |
| 160 | 'statistics' => 0, |
| 161 | 'statistics-anonymous' => 0, |
| 162 | 'marketing' => 0, |
| 163 | ), |
| 164 | ); |
| 165 | } |
| 166 | } |
| 167 |