compliance = ( $cn->get_status() === 'active' ); // cookie compliance initialization if ( $this->compliance ) { // amp compatibility if ( $cn->options['general']['amp_support'] && cn_is_plugin_active( 'amp' ) ) include_once( COOKIE_NOTICE_PATH . 'includes/modules/amp/amp.php' ); // excluded script handles — stamp data-hu-category="1" so the widget never blocks them if ( $cn->options['general']['app_blocking'] && ! empty( $cn->options['general']['excluded_handles'] ) ) { add_filter( 'script_loader_tag', [ $this, 'exclude_handles_from_blocking' ], 10, 2 ); if ( $cn->options['general']['debug_mode'] ) add_action( 'wp_footer', [ $this, 'debug_excluded_handles' ], 999 ); } } } /** * Stamp excluded script handles with data-hu-category="1" (Essential). * * @param string $tag Full ' . "\n"; } /** * Initialize plugin. * * @return void */ public function init() { if ( is_admin() ) return; // purge cache if ( isset( $_GET['hu_purge_cache'], $_GET['_wpnonce'] ) && current_user_can( apply_filters( 'cn_manage_cookie_notice_cap', 'manage_options' ) ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'cn-purge-cache' ) ) { $this->purge_cache(); } // get main instance $cn = Cookie_Notice(); // compatibility fixes if ( $this->compliance ) { // is blocking active? if ( $cn->options['general']['app_blocking'] ) { // contact form 7 compatibility if ( cn_is_plugin_active( 'contactform7', 'captcha' ) ) include_once( COOKIE_NOTICE_PATH . 'includes/modules/contact-form-7/contact-form-7.php' ); } } } /** * Whether banner is allowed to display. * * @param array $args * @return bool */ public function maybe_display_banner( $args = [] ) { $defaults = [ 'skip_amp' => false ]; if ( is_array( $args ) ) $args = wp_parse_args( $args, $defaults ); else $args = $defaults; // get main instance $cn = Cookie_Notice(); // is cookie compliance active? if ( $this->compliance ) { // elementor compatibility, needed early for is_preview_mode if ( cn_is_plugin_active( 'elementor' ) ) include_once( COOKIE_NOTICE_PATH . 'includes/modules/elementor/elementor.php' ); // divi builder compatibility if ( cn_is_plugin_active( 'divi', 'theme' ) ) include_once( COOKIE_NOTICE_PATH . 'includes/modules/divi/divi.php' ); } // is it preview mode? if ( $this->is_preview_mode() ) return false; // is bot detection enabled and it's a bot? if ( $cn->options['general']['bot_detection'] && apply_filters( 'cn_is_bot', $cn->bot_detect->is_crawler() ) ) return false; // check amp if ( ! $args['skip_amp'] ) { if ( $cn->options['general']['amp_support'] && cn_is_plugin_active( 'amp' ) && function_exists( 'amp_is_request' ) && amp_is_request() ) return false; } // final check for conditional display return $this->check_conditions(); } /** * Check if WP_CACHE is active. * * @return bool */ public function wp_cache_check( $result ) { if ( defined( 'WP_CACHE' ) && WP_CACHE === true ) $result = false; return $result; } /** * Whether preview mode is active. * * @return bool */ public function is_preview_mode() { return isset( $_GET['cn_preview_mode'] ) || is_preview() || is_customize_preview() || defined( 'IFRAME_REQUEST' ) || ( function_exists( 'wp_is_json_request' ) && wp_is_json_request() ) || isset( $_GET[ 'fl_builder' ] ) || apply_filters( 'cn_is_preview_mode', false ); } /** * Check whether banner should be displayed based on specified conditions. * * @return bool */ public function check_conditions() { // get main instance $cn = Cookie_Notice(); if ( ! $cn->options['general']['conditional_active'] ) return true; // get conditions $rules = $cn->options['general']['conditional_rules']; // set access type $access_type = $cn->options['general']['conditional_display'] === 'show'; // get object $object = get_queried_object(); // no rules? if ( empty( $rules ) ) $final_access = true; else { // check the rules foreach( $rules as $index => $group ) { $give_group_access = true; foreach ( $group as $rule ) { $give_rule_access = false; switch ( $rule['param'] ) { case 'page_type': if ( ( $rule['operator'] === 'equal' && $rule['value'] === 'front' && is_front_page() ) || ( $rule['operator'] === 'not_equal' && $rule['value'] === 'front' && ! is_front_page() ) || ( $rule['operator'] === 'equal' && $rule['value'] === 'home' && is_home() ) || ( $rule['operator'] === 'not_equal' && $rule['value'] === 'home' && ! is_home() ) || ( $rule['operator'] === 'equal' && $rule['value'] === 'login' && $this->is_login() ) || ( $rule['operator'] === 'not_equal' && $rule['value'] === 'login' && ! $this->is_login() ) ) $give_rule_access = true; break; case 'page': if ( ( $rule['operator'] === 'equal' && ! empty( $object ) && is_a( $object, 'WP_Post' ) && property_exists( $object, 'ID' ) && is_page( $object->ID ) && (int) $object->ID === (int) $rule['value'] ) || ( $rule['operator'] === 'not_equal' && ( empty( $object ) || ! is_page() || ( is_page() && ! empty( $object ) && is_a( $object, 'WP_Post' ) && property_exists( $object, 'ID' ) && $object->ID !== (int) $rule['value'] ) ) ) ) $give_rule_access = true; break; case 'post_type': if ( ( $rule['operator'] === 'equal' && is_singular( $rule['value'] ) ) || ( $rule['operator'] === 'not_equal' && ! is_singular( $rule['value'] ) ) ) $give_rule_access = true; break; case 'post_type_archive': if ( ( $rule['operator'] === 'equal' && is_post_type_archive( $rule['value'] ) ) || ( $rule['operator'] === 'not_equal' && ! is_post_type_archive( $rule['value'] ) ) ) $give_rule_access = true; break; case 'user_type': if ( ( $rule['operator'] === 'equal' && $rule['value'] === 'logged_in' && is_user_logged_in() ) || ( $rule['operator'] === 'equal' && $rule['value'] === 'guest' && ! is_user_logged_in() ) || ( $rule['operator'] === 'not_equal' && $rule['value'] === 'logged_in' && ! is_user_logged_in() ) || ( $rule['operator'] === 'not_equal' && $rule['value'] === 'guest' && is_user_logged_in() ) ) $give_rule_access = true; break; case 'taxonomy_archive': // check value if ( strpos( $rule['value'], '|' ) !== false ) { // explode it $values = explode( '|', $rule['value'] ); // 2 chunks? if ( count( $values ) === 2 ) { $term_id = (int) $values[0]; if ( $values[1] === 'category' && ( ( $rule['operator'] === 'equal' && is_category( $term_id ) ) || ( $rule['operator'] === 'not_equal' && ! is_category( $term_id ) ) ) ) $give_rule_access = true; elseif ( $values[1] === 'post_tag' && ( ( $rule['operator'] === 'equal' && is_tag( $term_id ) ) || ( $rule['operator'] === 'not_equal' && ! is_tag( $term_id ) ) ) ) $give_rule_access = true; elseif ( ( $rule['operator'] === 'equal' && is_tax( $values[1], $term_id ) ) || ( $rule['operator'] === 'not_equal' && ! is_tax( $values[1], $term_id ) ) ) $give_rule_access = true; } } break; } // condition failed? if ( ! $give_rule_access ) { // group failed $give_group_access = false; // finish group checking break; } } // whole group successful? if ( $give_group_access ) { // set final access $final_access = $access_type; // finish rules checking break; } else $final_access = ! $access_type; } } return (bool) apply_filters( 'cn_conditional_display', $final_access, $object ); } /** * Determine whether the current request is for the login screen. * * @return bool */ public function is_login() { return ( function_exists( 'is_login' ) ? is_login() : ( stripos( wp_login_url(), $_SERVER['SCRIPT_NAME'] ) !== false ) ); } /** * Get Cookie Compliance options. * * @return array */ public function get_cc_options() { // get main instance $cn = Cookie_Notice(); // get site language $locale = get_locale(); $locale_code = explode( '_', $locale ); // exceptions, norwegian if ( is_array( $locale_code ) && in_array( $locale_code[0], [ 'nb', 'nn' ] ) ) $locale_code[0] = 'no'; // get active sources $sources = $cn->privacy_consent->get_active_sources(); // prepare huOptions $options = [ 'appID' => $cn->options['general']['app_id'], 'currentLanguage' => $locale_code[0], 'blocking' => ! is_user_logged_in() ? $cn->options['general']['app_blocking'] : false, 'globalCookie' => is_multisite() && $cn->options['general']['global_cookie'] && is_subdomain_install(), 'isAdmin' => current_user_can( apply_filters( 'cn_manage_cookie_notice_cap', 'manage_options' ) ), 'privacyConsent' => ! empty( $sources ) ]; // any active source? if ( ! empty( $sources ) ) $options['forms'] = []; // filter options $options = apply_filters( 'cn_cookie_compliance_args', $options ); // get config timestamp if ( is_multisite() && $cn->is_plugin_network_active() && $cn->network_options['general']['global_override'] ) $timestamp = (int) get_site_transient( 'cookie_notice_config_update' ); else $timestamp = (int) get_transient( 'cookie_notice_config_update' ); // update config? if ( $timestamp > 0 ) { $options['cachePurge'] = true; $options['cacheTimestamp'] = $timestamp; } // debug mode if ( $cn->options['general']['debug_mode'] ) $options['debugMode'] = true; // blocking data (custom patterns, providers, consent mode defaults) // always include in huOptions so the widget has the full configuration; // the huOptions.blocking flag controls whether scripts are actually blocked if ( $cn->is_network_options() ) $blocking = get_site_option( 'cookie_notice_app_blocking' ); else $blocking = get_option( 'cookie_notice_app_blocking' ); if ( ! empty( $blocking ) && is_array( $blocking ) ) { $options['customProviders'] = ! empty( $blocking['providers'] ) && is_array( $blocking['providers'] ) ? $blocking['providers'] : []; $options['customPatterns'] = ! empty( $blocking['patterns'] ) && is_array( $blocking['patterns'] ) ? $blocking['patterns'] : []; // google consent mode default categories if ( ! empty( $blocking['google_consent_default'] ) && is_array( $blocking['google_consent_default'] ) ) { $gcd = []; foreach ( $blocking['google_consent_default'] as $storage => $category ) { if ( in_array( $storage, ['ad_storage', 'analytics_storage', 'functionality_storage', 'personalization_storage', 'security_storage', 'ad_personalization', 'ad_user_data'], true ) ) $gcd[$storage] = (int) $category; } if ( ! empty( $gcd ) ) $options['googleConsentDefault'] = $gcd; } // facebook consent mode default categories if ( ! empty( $blocking['facebook_consent_default'] ) && is_array( $blocking['facebook_consent_default'] ) ) { $fcd = []; foreach ( $blocking['facebook_consent_default'] as $storage => $category ) { if ( in_array( $storage, ['consent'], true ) ) $fcd[$storage] = (int) $category; } if ( ! empty( $fcd ) ) $options['facebookConsentDefault'] = $fcd; } // microsoft consent mode default categories if ( ! empty( $blocking['microsoft_consent_default'] ) && is_array( $blocking['microsoft_consent_default'] ) ) { $mcd = []; foreach ( $blocking['microsoft_consent_default'] as $storage => $category ) { if ( in_array( $storage, ['ad_storage', 'analytics_storage'], true ) ) $mcd[$storage] = (int) $category; } if ( ! empty( $mcd ) ) $options['microsoftConsentDefault'] = $mcd; } } if ( isset( $_GET['cn_preview'] ) && $_GET['cn_preview'] === '1' && current_user_can( 'manage_options' ) ) { $options['forceShow'] = true; } return $options; } /** * Get Cookie Compliance output. * * @param array $options * @return string */ public function get_cc_output( $options ) { $output = ' '; return apply_filters( 'cn_cookie_compliance_output', $output, $options ); } /** * Add DNS Prefetch. * * @return void */ public function add_dns_prefetch() { if ( ! $this->compliance ) return; // is banner allowed to display? if ( ! $this->maybe_display_banner() ) return; // Derive prefetch host from widget URL so CN_APP_WIDGET_URL overrides are honoured. $widget_url = Cookie_Notice()->get_url( 'widget' ); $prefetch_host = '//' . wp_parse_url( 'https:' . $widget_url, PHP_URL_HOST ); echo ''; } /** * Run Cookie Compliance. * * @return void */ public function add_cookie_compliance() { // skip modal login iframe if ( current_filter() === 'login_head' && ! empty( $_REQUEST['interim-login'] ) ) return; // allow only for compliance if ( ! $this->compliance ) return; // is banner allowed to display? if ( ! $this->maybe_display_banner() ) return; // get options $options = $this->get_cc_options(); // display output echo $this->get_cc_output( $options ); } /** * Cookie notice output. * * @return void */ public function add_cookie_notice() { // skip modal login iframe if ( current_filter() === 'login_footer' && ! empty( $_REQUEST['interim-login'] ) ) return; if ( $this->compliance ) return; // is banner allowed to display? if ( ! $this->maybe_display_banner() ) return; // get main instance $cn = Cookie_Notice(); // WPML >= 3.2 if ( defined( 'ICL_SITEPRESS_VERSION' ) && version_compare( ICL_SITEPRESS_VERSION, '3.2', '>=' ) ) { $cn->options['general']['message_text'] = apply_filters( 'wpml_translate_single_string', $cn->options['general']['message_text'], 'Cookie Notice', 'Message in the notice' ); $cn->options['general']['accept_text'] = apply_filters( 'wpml_translate_single_string', $cn->options['general']['accept_text'], 'Cookie Notice', 'Button text' ); $cn->options['general']['refuse_text'] = apply_filters( 'wpml_translate_single_string', $cn->options['general']['refuse_text'], 'Cookie Notice', 'Refuse button text' ); $cn->options['general']['revoke_message_text'] = apply_filters( 'wpml_translate_single_string', $cn->options['general']['revoke_message_text'], 'Cookie Notice', 'Revoke message text' ); $cn->options['general']['revoke_text'] = apply_filters( 'wpml_translate_single_string', $cn->options['general']['revoke_text'], 'Cookie Notice', 'Revoke button text' ); $cn->options['general']['see_more_opt']['text'] = apply_filters( 'wpml_translate_single_string', $cn->options['general']['see_more_opt']['text'], 'Cookie Notice', 'Privacy policy text' ); $cn->options['general']['see_more_opt']['link'] = apply_filters( 'wpml_translate_single_string', $cn->options['general']['see_more_opt']['link'], 'Cookie Notice', 'Custom link' ); // WPML and Polylang compatibility } elseif ( function_exists( 'icl_t' ) ) { $cn->options['general']['message_text'] = icl_t( 'Cookie Notice', 'Message in the notice', $cn->options['general']['message_text'] ); $cn->options['general']['accept_text'] = icl_t( 'Cookie Notice', 'Button text', $cn->options['general']['accept_text'] ); $cn->options['general']['refuse_text'] = icl_t( 'Cookie Notice', 'Refuse button text', $cn->options['general']['refuse_text'] ); $cn->options['general']['revoke_message_text'] = icl_t( 'Cookie Notice', 'Revoke message text', $cn->options['general']['revoke_message_text'] ); $cn->options['general']['revoke_text'] = icl_t( 'Cookie Notice', 'Revoke button text', $cn->options['general']['revoke_text'] ); $cn->options['general']['see_more_opt']['text'] = icl_t( 'Cookie Notice', 'Privacy policy text', $cn->options['general']['see_more_opt']['text'] ); $cn->options['general']['see_more_opt']['link'] = icl_t( 'Cookie Notice', 'Custom link', $cn->options['general']['see_more_opt']['link'] ); } if ( $cn->options['general']['see_more_opt']['link_type'] === 'page' ) { // multisite with global override? if ( is_multisite() && $cn->is_plugin_network_active() && $cn->network_options['general']['global_override'] ) { // get main site id $main_site_id = get_main_site_id(); // switch to main site switch_to_blog( $main_site_id ); // update page id for current language if needed if ( function_exists( 'icl_object_id' ) ) $cn->options['general']['see_more_opt']['id'] = icl_object_id( $cn->options['general']['see_more_opt']['id'], 'page', true ); // get main site privacy policy link $permalink = get_permalink( $cn->options['general']['see_more_opt']['id'] ); // restore current site restore_current_blog(); } else { // update page id for current language if needed if ( function_exists( 'icl_object_id' ) ) $cn->options['general']['see_more_opt']['id'] = icl_object_id( $cn->options['general']['see_more_opt']['id'], 'page', true ); // get privacy policy link $permalink = get_permalink( $cn->options['general']['see_more_opt']['id'] ); } } // #2266: position is API-owned — read from cookie_notice_app_design for connected sites. // Falls back to cookie_notice_options["general"]["position"] for disconnected/legacy-only installs. $app_design = $cn->is_network_options() ? get_site_option( 'cookie_notice_app_design', [] ) : get_option( 'cookie_notice_app_design', [] ); $banner_position = ! empty( $app_design['position'] ) ? sanitize_key( $app_design['position'] ) : ( $cn->options['general']['position'] ?? 'bottom' ); // get cookie container args $options = apply_filters( 'cn_cookie_notice_args', [ 'position' => $banner_position, 'css_class' => $cn->options['general']['css_class'], 'button_class' => 'cn-button', 'colors' => $cn->options['general']['colors'], 'message_text' => $cn->options['general']['message_text'], 'accept_text' => $cn->options['general']['accept_text'], 'refuse_text' => $cn->options['general']['refuse_text'], 'revoke_message_text' => $cn->options['general']['revoke_message_text'], 'revoke_text' => $cn->options['general']['revoke_text'], 'refuse_opt' => $cn->options['general']['refuse_opt'], 'revoke_cookies' => $cn->options['general']['revoke_cookies'], 'see_more' => $cn->options['general']['see_more'], 'see_more_opt' => $cn->options['general']['see_more_opt'], 'link_target' => $cn->options['general']['link_target'], 'link_position' => $cn->options['general']['link_position'], 'aria_label' => 'Compliance by Hu-manity.co' ] ); // message output $output = '
'; add_filter( 'safe_style_css', [ $this, 'allow_style_attributes' ] ); $output = apply_filters( 'cn_cookie_notice_output', wp_kses_post( $output ), $options ); remove_filter( 'safe_style_css', [ $this, 'allow_style_attributes' ] ); // convert rgb color to hex $bg_rgb_color = $this->hex2rgb( $options['colors']['bar'] ); // invalid color? use default if ( $bg_rgb_color === false ) $bg_rgb_color = $this->hex2rgb( $cn->defaults['general']['colors']['bar'] ); // allow rgba background echo str_replace( '__CN_BG_COLOR__', esc_attr( 'rgba(' . implode( ',', $bg_rgb_color ) . ',' . ( (int) $options['colors']['bar_opacity'] ) * 0.01 . ');' ), $output ); } /** * Add new properties to style safe list. * * @param array $styles * @return array */ public function allow_style_attributes( $styles ) { $styles[] = 'display'; return $styles; } /** * Convert HEX to RGB color. * * @param string $color * @return bool|array */ public function hex2rgb( $color ) { if ( ! is_string( $color ) ) return false; // with hash? if ( $color[0] === '#' ) $color = substr( $color, 1 ); if ( sanitize_hex_color_no_hash( $color ) !== $color ) return false; // 6 hex digits? if ( strlen( $color ) === 6 ) list( $r, $g, $b ) = [ $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ]; // 3 hex digits? elseif ( strlen( $color ) === 3 ) list( $r, $g, $b ) = [ $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ]; else return false; return [ 'r' => hexdec( $r ), 'g' => hexdec( $g ), 'b' => hexdec( $b ) ]; } /** * Add blocking class to scripts, iframes and links. * * @param string $type * @param string $code * @return string */ public function add_block_class( $type, $code ) { // clear and disable libxml errors and allow user to fetch error information as needed libxml_use_internal_errors( true ); // create new dom object $document = new DOMDocument( '1.0', 'UTF-8' ); // set attributes $document->formatOutput = true; $document->preserveWhiteSpace = false; // load code $document->loadHTML( '