buffer
7 years ago
ioc
6 years ago
script-loader-tag
7 years ago
autoloader.php
7 years ago
cookie-consent-interface.php
7 years ago
cookie-consent.php
7 years ago
helper.php
6 years ago
settings-service-interface.php
7 years ago
settings-service.php
7 years ago
helper.php
287 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Check if a cache plugin is activated and in function. |
| 4 | * |
| 5 | * @return boolean True If attributes always should be added |
| 6 | * False If attributes only should be added if consent no given |
| 7 | */ |
| 8 | |
| 9 | function cookiebot_addons_enabled_cache_plugin() { |
| 10 | if( defined( "WP_ROCKET_PATH" ) ) { |
| 11 | return true; //WP Rocket - We need to ensure we not cache tags without attributes |
| 12 | } |
| 13 | if( defined( "W3TC" ) ) { |
| 14 | return true; //W3 Total Cache |
| 15 | } |
| 16 | if( defined( "WPCACHEHOME" ) ) { |
| 17 | return true; //WP Super Cache |
| 18 | } |
| 19 | if( defined( "WPFC_WP_PLUGIN_DIR" ) ) { |
| 20 | return true; //WP Fastest Cache |
| 21 | } |
| 22 | if( defined( "LSCWP_CONTENT_DIR" ) ) { |
| 23 | return true; //Litespeed Cache |
| 24 | } |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Removes action with class in callback |
| 31 | * |
| 32 | * @param $action string action name |
| 33 | * @param $class string class name |
| 34 | * @param $method string method name |
| 35 | * @param $priority integer action priority number |
| 36 | * |
| 37 | * @return boolean True if the action hook is deleted |
| 38 | * False If the action hook is not deleted |
| 39 | * |
| 40 | * @since 1.2.0 |
| 41 | */ |
| 42 | function cookiebot_addons_remove_class_action( $action, $class, $method, $priority = 10 ) { |
| 43 | global $wp_filter; |
| 44 | $deleted = false; |
| 45 | |
| 46 | if ( isset( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ][ $priority ] ) ) { |
| 47 | $len = strlen( $method ); |
| 48 | foreach ( $wp_filter[ $action ][ $priority ] as $name => $def ) { |
| 49 | if ( substr( $name, - $len ) == $method ) { |
| 50 | if ( is_array( $def['function'] ) ) { |
| 51 | if ( is_string( $def['function'][0] ) !== false ) { |
| 52 | $def_class = $def['function'][0]; |
| 53 | } else { |
| 54 | $def_class = get_class( $def['function'][0] ); |
| 55 | } |
| 56 | |
| 57 | if ( $def_class == $class ) { |
| 58 | if ( is_object( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ]->callbacks ) ) { |
| 59 | $wp_filter[ $action ]->remove_filter( $action, $name, $priority ); |
| 60 | $deleted = true; |
| 61 | } else { |
| 62 | unset( $wp_filter[ $action ][ $priority ][ $name ] ); |
| 63 | $deleted = true; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return $deleted; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Custom manipulation of the script |
| 76 | * |
| 77 | * @param $buffer |
| 78 | * @param $keywords |
| 79 | * @param $cookie_type |
| 80 | * |
| 81 | * @return mixed|null|string|string[] |
| 82 | * |
| 83 | * @version 2.0.4 |
| 84 | * @since 1.2.0 |
| 85 | */ |
| 86 | function cookiebot_addons_manipulate_script( $buffer, $keywords ) { |
| 87 | /** |
| 88 | * Pattern to get all scripts |
| 89 | * |
| 90 | * @version 2.0.4 |
| 91 | * @since 1.2.0 |
| 92 | */ |
| 93 | $pattern = "/<script[\s\S]*?>[\s\S]*?<\/script>/i"; |
| 94 | |
| 95 | /** |
| 96 | * Get all scripts and add cookieconsent if it does match with the criterion |
| 97 | */ |
| 98 | $updated_scripts = preg_replace_callback( $pattern, function ( $matches ) use ( $keywords ) { |
| 99 | /** |
| 100 | * Matched script data |
| 101 | */ |
| 102 | $data = ( isset( $matches[0] ) ) ? $matches[0] : ''; |
| 103 | |
| 104 | /** |
| 105 | * Check if the script contains the keywords, checks keywords one by one |
| 106 | * |
| 107 | * If one match, then the rest of the keywords will be skipped. |
| 108 | **/ |
| 109 | foreach ( $keywords as $needle => $cookie_type ) { |
| 110 | /** |
| 111 | * The script contains the needle |
| 112 | **/ |
| 113 | if ( strpos( $data, $needle ) !== false ) { |
| 114 | $data = preg_replace( '/\<script/', '<script type="text/plain" data-cookieconsent="' . cookiebot_addons_output_cookie_types( $cookie_type ) . '"', $data ); |
| 115 | $data = preg_replace( '/type\=\"text\/javascript\"/', '', $data ); |
| 116 | |
| 117 | /** |
| 118 | * matched already so we can skip other keywords |
| 119 | **/ |
| 120 | continue; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Return updated script data |
| 126 | */ |
| 127 | return $data; |
| 128 | }, $buffer ); |
| 129 | |
| 130 | /** |
| 131 | * Fallback when the regex fails to work due to PCRE_ERROR_JIT_STACKLIMIT |
| 132 | * |
| 133 | * @version 2.0.4 |
| 134 | * @since 2.0.4 |
| 135 | */ |
| 136 | if ( $updated_scripts === null ) { |
| 137 | $updated_scripts = $buffer; |
| 138 | |
| 139 | if ( get_option( 'cookiebot_regex_stacklimit' ) === false ) { |
| 140 | update_option( 'cookiebot_regex_stacklimit', 1 ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return $updated_scripts; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Compares array to string to add checked attribute in checkbox |
| 149 | * |
| 150 | * @param $helper |
| 151 | * @param $current |
| 152 | * @param bool $echo |
| 153 | * @param string $type |
| 154 | * |
| 155 | * @return string |
| 156 | * |
| 157 | * @since 1.3.0 |
| 158 | */ |
| 159 | function cookiebot_addons_checked_selected_helper( $helper, $current, $echo = true, $type = 'checked' ) { |
| 160 | if ( is_array( $helper ) && in_array( $current, $helper ) ) { |
| 161 | $result = " $type='$type'"; |
| 162 | } elseif ( is_string( $helper ) && is_string( $current ) && $helper === $current ) { |
| 163 | $result = " $type='$type'"; |
| 164 | } else { |
| 165 | $result = ''; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | if ( $echo ) { |
| 170 | echo $result; |
| 171 | } |
| 172 | |
| 173 | return $result; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns cookie types in a string |
| 178 | * Default is statistics |
| 179 | * |
| 180 | * @param $cookie_types |
| 181 | * |
| 182 | * @return string |
| 183 | * |
| 184 | * @since 1.3.0 |
| 185 | */ |
| 186 | function cookiebot_addons_output_cookie_types( $cookie_types ) { |
| 187 | if ( is_array( $cookie_types ) && count( $cookie_types ) > 0 ) { |
| 188 | return implode( ', ', $cookie_types ); |
| 189 | } elseif ( is_string( $cookie_types ) && $cookie_types != '' ) { |
| 190 | return $cookie_types; |
| 191 | } |
| 192 | |
| 193 | return 'statistics'; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Return 1 cookie type if more than 1 is selected |
| 198 | * |
| 199 | * @param $cookie_types |
| 200 | * |
| 201 | * @return string |
| 202 | * |
| 203 | * @since 1.3.0 |
| 204 | */ |
| 205 | function cookiebot_addons_get_one_cookie_type( $cookie_types ) { |
| 206 | if ( is_array( $cookie_types ) ) { |
| 207 | if ( in_array( 'marketing', $cookie_types ) ) { |
| 208 | return 'marketing'; |
| 209 | } elseif ( in_array( 'statistics', $cookie_types ) ) { |
| 210 | return 'statistics'; |
| 211 | } elseif ( in_array( 'preferences', $cookie_types ) ) { |
| 212 | return 'preferences'; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return ''; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns current site language |
| 221 | * |
| 222 | * @return mixed|string |
| 223 | * |
| 224 | * @since 1.9.0 |
| 225 | */ |
| 226 | function cookiebot_addons_get_language() { |
| 227 | $lang = get_locale(); //Gets language in en-US format |
| 228 | |
| 229 | /** |
| 230 | * Add support for 3rd party plugins |
| 231 | */ |
| 232 | $lang = apply_filters( 'cookiebot_addons_language', $lang ); |
| 233 | |
| 234 | return $lang; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get supported languages by the cookiebot |
| 239 | * |
| 240 | * @return array |
| 241 | * |
| 242 | * @since 1.9.0 |
| 243 | */ |
| 244 | function cookiebot_addons_get_supported_languages() { |
| 245 | $cookiebot = cookiebot(); |
| 246 | |
| 247 | return $cookiebot->get_supported_languages(); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Show languages in a select field |
| 252 | * |
| 253 | * @param $class |
| 254 | * @param $name |
| 255 | * @param $selected |
| 256 | * |
| 257 | * @return mixed |
| 258 | * |
| 259 | * @since 1.8.0 |
| 260 | */ |
| 261 | function cookiebot_addons_get_dropdown_languages( $class, $name, $selected ) { |
| 262 | $args = array( |
| 263 | 'name' => $name, |
| 264 | 'selected' => $selected, |
| 265 | 'show_option_site_default' => true, |
| 266 | 'echo' => false, |
| 267 | 'languages' => get_available_languages() |
| 268 | ); |
| 269 | $dropdown = wp_dropdown_languages( $args ); |
| 270 | |
| 271 | $output = str_replace( 'select ', 'select class="' . $class . '" ', $dropdown ); |
| 272 | |
| 273 | $output = str_replace( 'value="" ', 'value="en_US" ', $output ); |
| 274 | |
| 275 | return $output; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Run actions when the cookiebot plugin is deactivated |
| 280 | * |
| 281 | * @since 2.2.0 |
| 282 | */ |
| 283 | function cookiebot_addons_plugin_deactivated( ) { |
| 284 | $cookiebot_addons = \cookiebot_addons\Cookiebot_Addons::instance(); |
| 285 | $cookiebot_addons->cookiebot_deactivated(); |
| 286 | } |
| 287 |