buffer
3 years ago
script_loader_tag
3 years ago
traits
3 years ago
Consent_API_Helper.php
3 years ago
Cookie_Consent.php
3 years ago
Cookie_Consent_Interface.php
4 years ago
Cookiebot_Activated.php
3 years ago
Cookiebot_Automatic_Updates.php
3 years ago
Cookiebot_Deactivated.php
4 years ago
Cookiebot_Javascript_Helper.php
3 years ago
Cookiebot_Review.php
3 years ago
Cookiebot_WP.php
3 years 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
3 years ago
WP_Rocket_Helper.php
3 years ago
Widgets.php
3 years ago
global-deprecations.php
3 years ago
helper.php
3 years ago
Cookiebot_Javascript_Helper.php
166 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\lib; |
| 4 | |
| 5 | use cybot\cookiebot\shortcode\Cookiebot_Declaration_Shortcode; |
| 6 | use InvalidArgumentException; |
| 7 | |
| 8 | class Cookiebot_Javascript_Helper { |
| 9 | public function register_hooks() { |
| 10 | if ( is_admin() && ! Cookiebot_WP::cookiebot_disabled_in_admin() ) { |
| 11 | // adding cookie banner in admin area too |
| 12 | add_action( 'admin_head', array( $this, 'include_cookiebot_js' ), - 9999 ); |
| 13 | } |
| 14 | |
| 15 | // add JS |
| 16 | add_action( 'wp_head', array( $this, 'include_cookiebot_js' ), - 9997 ); |
| 17 | add_action( 'wp_head', array( $this, 'include_google_tag_manager_js' ), - 9998 ); |
| 18 | add_action( 'wp_head', array( $this, 'include_google_consent_mode_js' ), - 9999 ); |
| 19 | ( new Cookiebot_Declaration_Shortcode() )->register_hooks(); |
| 20 | } |
| 21 | |
| 22 | private function get_data_regions() { |
| 23 | $is_multi_config = ! empty( get_option( 'cookiebot-multiple-config' ) ) ? |
| 24 | get_option( 'cookiebot-multiple-config' ) : |
| 25 | false; |
| 26 | $second_banner_region = ! empty( get_option( 'cookiebot-second-banner-regions' ) ) ? |
| 27 | get_option( 'cookiebot-second-banner-regions' ) : |
| 28 | false; |
| 29 | $second_banner_id = ! empty( get_option( 'cookiebot-second-banner-id' ) ) ? |
| 30 | get_option( 'cookiebot-second-banner-id' ) : |
| 31 | false; |
| 32 | |
| 33 | $extra_banners = ! empty( get_option( 'cookiebot-multiple-banners' ) ) ? |
| 34 | get_option( 'cookiebot-multiple-banners' ) : |
| 35 | false; |
| 36 | |
| 37 | $regions = array(); |
| 38 | |
| 39 | if ( $is_multi_config !== false && $second_banner_region && $second_banner_id ) { |
| 40 | $regions[ $second_banner_id ] = $second_banner_region; |
| 41 | } |
| 42 | |
| 43 | if ( $is_multi_config !== false && $extra_banners ) { |
| 44 | foreach ( $extra_banners as $data ) { |
| 45 | $regions[ $data['group'] ] = $data['region']; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return $regions; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Cookiebot_WP Add Cookiebot JS to <head> |
| 54 | * |
| 55 | * @param false $return_html |
| 56 | * |
| 57 | * @return string |
| 58 | * @throws InvalidArgumentException |
| 59 | */ |
| 60 | public function include_cookiebot_js( $return_html = false ) { |
| 61 | $cbid = Cookiebot_WP::get_cbid(); |
| 62 | if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) { |
| 63 | if ( |
| 64 | // Is multisite - and disabled output is checked as network setting |
| 65 | ( is_multisite() && get_site_option( 'cookiebot-nooutput', false ) ) || |
| 66 | // Do not show JS - output disabled |
| 67 | get_option( 'cookiebot-nooutput', false ) || |
| 68 | // Do not show js if logged in output is disabled |
| 69 | ( |
| 70 | Cookiebot_WP::get_cookie_blocking_mode() === 'auto' && |
| 71 | Cookiebot_WP::can_current_user_edit_theme() && |
| 72 | $return_html === '' && |
| 73 | ( |
| 74 | get_site_option( 'cookiebot-output-logged-in' ) === false || |
| 75 | get_site_option( 'cookiebot-output-logged-in' ) === '' |
| 76 | ) |
| 77 | ) |
| 78 | ) { |
| 79 | return ''; |
| 80 | } |
| 81 | |
| 82 | $lang = cookiebot_get_language_from_setting(); |
| 83 | |
| 84 | if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-uc-attribute', 'custom' ) === 'custom' ) { |
| 85 | $tag_attr = get_option( 'cookiebot-script-tag-uc-attribute', 'async' ); |
| 86 | } else { |
| 87 | $tag_attr = get_site_option( 'cookiebot-script-tag-uc-attribute' ); |
| 88 | } |
| 89 | |
| 90 | $view_path = 'frontend/scripts/cookiebot-js.php'; |
| 91 | $view_args = array( |
| 92 | 'cbid' => $cbid, |
| 93 | 'lang' => $lang, |
| 94 | 'tag_attr' => $tag_attr, |
| 95 | 'cookie_blocking_mode' => Cookiebot_WP::get_cookie_blocking_mode(), |
| 96 | 'data_regions' => self::get_data_regions(), |
| 97 | ); |
| 98 | |
| 99 | if ( $return_html ) { |
| 100 | return get_view_html( $view_path, $view_args ); |
| 101 | } else { |
| 102 | include_view( $view_path, $view_args ); |
| 103 | } |
| 104 | } |
| 105 | return ''; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Cookiebot_WP Add Google Tag Manager JS to <head> |
| 110 | * |
| 111 | * @param bool $return_html |
| 112 | * |
| 113 | * @return string |
| 114 | * @throws InvalidArgumentException |
| 115 | */ |
| 116 | public function include_google_tag_manager_js( $return_html = false ) { |
| 117 | $option = get_option( 'cookiebot-gtm' ); |
| 118 | |
| 119 | if ( $option !== false && $option !== '' ) { |
| 120 | if ( empty( get_option( 'cookiebot-data-layer' ) ) ) { |
| 121 | $data_layer = 'dataLayer'; |
| 122 | } else { |
| 123 | $data_layer = get_option( 'cookiebot-data-layer' ); |
| 124 | } |
| 125 | |
| 126 | $view_path = 'frontend/scripts/google-tag-manager-js.php'; |
| 127 | $view_args = array( 'data_layer' => $data_layer ); |
| 128 | if ( $return_html ) { |
| 129 | return get_view_html( $view_path, $view_args ); |
| 130 | } else { |
| 131 | include_view( $view_path, $view_args ); |
| 132 | } |
| 133 | } |
| 134 | return ''; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Cookiebot_WP Add Google Consent Mode JS to <head> |
| 139 | * |
| 140 | * @param bool $return_html |
| 141 | * |
| 142 | * @return string |
| 143 | * @throws InvalidArgumentException |
| 144 | */ |
| 145 | public function include_google_consent_mode_js( $return_html = false ) { |
| 146 | $option = get_option( 'cookiebot-gcm' ); |
| 147 | |
| 148 | if ( $option !== false && $option !== '' ) { |
| 149 | if ( empty( get_option( 'cookiebot-data-layer' ) ) ) { |
| 150 | $data_layer = 'dataLayer'; |
| 151 | } else { |
| 152 | $data_layer = get_option( 'cookiebot-data-layer' ); |
| 153 | } |
| 154 | |
| 155 | $view_path = 'frontend/scripts/google-consent-mode-js.php'; |
| 156 | $view_args = array( 'data_layer' => $data_layer ); |
| 157 | if ( $return_html ) { |
| 158 | return get_view_html( $view_path, $view_args ); |
| 159 | } else { |
| 160 | include_view( $view_path, $view_args ); |
| 161 | } |
| 162 | } |
| 163 | return ''; |
| 164 | } |
| 165 | } |
| 166 |