'
. '
'
. ''. ( $options['see_more'] ? do_shortcode( $options['message_text'] ) : $options['message_text'] ) . ''
. ''
. ( $options['refuse_opt'] ? '' : '' )
. ( $options['see_more'] && $options['link_position'] === 'banner' ? '' : '' )
. ''
. '
' . ( $options['refuse_opt'] && $options['revoke_cookies'] ?
'
'
. ( ! empty( $options['revoke_message_text'] ) ? '' . $options['revoke_message_text'] . '' : '' )
. '
' : '' ) . '
';
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( '' . wp_kses( trim( $code ), Cookie_Notice()->get_allowed_html( $type ) ) . '
', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
$container = $document->getElementsByTagName( 'div' )->item( 0 );
$container = $container->parentNode->removeChild( $container );
while ( $document->firstChild ) {
$document->removeChild( $document->firstChild );
}
while ( $container->firstChild ) {
$document->appendChild( $container->firstChild );
}
// set blocked tags
if ( $type === 'body' )
$blocked_tags = [ 'script', 'iframe' ];
elseif ( $type === 'head' )
$blocked_tags = [ 'script', 'link' ];
foreach ( $blocked_tags as $blocked_tag ) {
$tags = $document->getElementsByTagName( $blocked_tag );
// any tags?
if ( ! empty( $tags ) && is_object( $tags ) ) {
foreach ( $tags as $tag ) {
$tag->setAttribute( 'class', 'hu-block' );
}
}
}
// save new HTML
$output = $document->saveHTML();
// reenable libxml errors
libxml_use_internal_errors( false );
return $output;
}
/**
* Load notice scripts and styles - frontend.
*
* @return void
*/
public function wp_enqueue_notice_scripts() {
// skip modal login iframe
if ( current_filter() === 'login_enqueue_scripts' && ! 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();
wp_enqueue_script( 'cookie-notice-front', COOKIE_NOTICE_URL . '/js/front' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.js', [], $cn->defaults['version'], isset( $cn->options['general']['script_placement'] ) && $cn->options['general']['script_placement'] === 'footer' );
// not array? changeable by cn_cookie_expiry filter
if ( is_array( $cn->settings->times ) ) {
// check cookie expiration time
if ( array_key_exists( $cn->options['general']['time'], $cn->settings->times ) && array_key_exists( 1, $cn->settings->times[$cn->options['general']['time']] ) )
$cookie_time = (int) $cn->settings->times[$cn->options['general']['time']][1];
else {
// fallback to default length of month
$cookie_time = MONTH_IN_SECONDS;
}
// check cookie rejection expiration time
if ( array_key_exists( $cn->options['general']['time_rejected'], $cn->settings->times ) && array_key_exists( 1, $cn->settings->times[$cn->options['general']['time_rejected']] ) )
$cookie_time_rejected = (int) $cn->settings->times[$cn->options['general']['time_rejected']][1];
else {
// fallback to default length of month
$cookie_time_rejected = MONTH_IN_SECONDS;
}
} else {
// fallback to default length of month
$cookie_time = $cookie_time_rejected = MONTH_IN_SECONDS;
}
// prepare script data
$script_data = [
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'cn_save_cases' ),
'hideEffect' => $cn->options['general']['hide_effect'],
'position' => $cn->options['general']['position'],
'onScroll' => $cn->options['general']['on_scroll'],
'onScrollOffset' => (int) $cn->options['general']['on_scroll_offset'],
'onClick' => $cn->options['general']['on_click'],
'cookieName' => 'cookie_notice_accepted',
'cookieTime' => $cookie_time,
'cookieTimeRejected' => $cookie_time_rejected,
'globalCookie' => is_multisite() && $cn->options['general']['global_cookie'] && is_subdomain_install(),
'redirection' => $cn->options['general']['redirection'],
'cache' => defined( 'WP_CACHE' ) && WP_CACHE,
'revokeCookies' => $cn->options['general']['revoke_cookies'],
'revokeCookiesOpt' => $cn->options['general']['revoke_cookies_opt']
];
wp_add_inline_script( 'cookie-notice-front', 'var cnArgs = ' . wp_json_encode( $script_data ) . ";\n", 'before' );
wp_enqueue_style( 'cookie-notice-front', COOKIE_NOTICE_URL . '/css/front' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.css', [], $cn->defaults['version'] );
}
/**
* Print non functional JavaScript in body.
*
* @return void
*/
public function wp_print_footer_scripts() {
// get main instance
$cn = Cookie_Notice();
if ( $cn->cookies_accepted() || $this->compliance ) {
$scripts = apply_filters( 'cn_refuse_code_scripts_html', $cn->options['general']['refuse_code'], 'body' );
if ( ! empty( $scripts ) )
echo html_entity_decode( wp_kses( $scripts, $cn->get_allowed_html( 'body' ) ) );
}
}
/**
* Print non functional JavaScript in header.
*
* @return void
*/
public function wp_print_header_scripts() {
// get main instance
$cn = Cookie_Notice();
if ( $cn->cookies_accepted() || $this->compliance ) {
$scripts = apply_filters( 'cn_refuse_code_scripts_html', $cn->options['general']['refuse_code_head'], 'head' );
if ( ! empty( $scripts ) )
echo html_entity_decode( wp_kses( $scripts, $cn->get_allowed_html( 'head' ) ) );
}
}
/**
* Add new body classes.
*
* @param array $classes Body classes
* @return array
*/
public function change_body_class( $classes ) {
if ( is_admin() )
return $classes;
if ( Cookie_Notice()->cookies_set() ) {
$classes[] = 'cookies-set';
if ( Cookie_Notice()->cookies_accepted() )
$classes[] = 'cookies-accepted';
else
$classes[] = 'cookies-refused';
} else
$classes[] = 'cookies-not-set';
return $classes;
}
/**
* Purge config cache.
*
* @return void
*/
public function purge_cache() {
// get main instance
$cn = Cookie_Notice();
if ( is_multisite() && $cn->is_plugin_network_active() && $cn->network_options['general']['global_override'] ) {
$app_id = $cn->network_options['general']['app_id'];
$app_key = $cn->network_options['general']['app_key'];
} else {
$app_id = $cn->options['general']['app_id'];
$app_key = $cn->options['general']['app_key'];
}
// compliance active only
if ( $app_id !== '' && $app_key !== '' ) {
// request for new config data too
$cn->welcome_api->get_app_config( $app_id, true );
}
}
}