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