[], 'version' => ABLOCKS_VERSION, ]; wp_enqueue_style( 'ablocks-cookie-consent', ABLOCKS_ASSETS_URL . 'build/cookie-consent.css', [], $asset['version'] ); wp_enqueue_script( 'ablocks-cookie-consent', ABLOCKS_ASSETS_URL . 'build/cookie-consent.js', [], $asset['version'], true ); // `wp_localize_script` casts every scalar to a string, which would turn // the policy version into "1" and break the strict comparison the // Consent Mode reader in
makes against the integer. An inline // JSON assignment keeps the types. wp_add_inline_script( 'ablocks-cookie-consent', 'window.ABlocksConsentConfig = ' . wp_json_encode( $this->script_config() ) . ';', 'before' ); } /** * Everything the client needs to decide what to show and what to release. * * @return array */ private function script_config() { $categories = []; foreach ( Helper::active_categories() as $category ) { $categories[] = [ 'slug' => $category['slug'], 'locked' => ! empty( $category['locked'] ), ]; } return [ 'cookie' => Helper::get( 'cookie_name', 'ablocks_consent' ), 'cookiePrev' => Helper::get( 'cookie_name_previous', '' ), 'days' => (int) Helper::get( 'cookie_days', 365 ), 'version' => (int) Helper::get( 'policy_version', 1 ), 'reconsentDays' => (int) Helper::get( 'reconsent_days', 0 ), 'mode' => Helper::get( 'mode', 'optin' ), 'categories' => $categories, 'consentMode' => (bool) Helper::get( 'consent_mode', true ), 'consentModeAds' => (bool) Helper::get( 'consent_mode_ads', true ), 'signals' => ConsentMode::signal_map(), 'record' => (bool) Helper::get( 'record_enabled', true ), 'endpoint' => rest_url( ABLOCKS_REST_NAMESPACE . '/consent' ), 'delay' => (int) Helper::banner( 'delay', 0 ), 'reopen' => (bool) Helper::banner( 'reopen', true ), 'canClose' => (bool) Helper::banner( 'show_banner_close', false ), 'closeAction' => Helper::banner( 'close_behaviour', 'dismiss' ), 'dismissDays' => (int) Helper::banner( 'dismiss_days', 0 ), ]; } /** * The CSS custom properties the stylesheet reads. * * Emitted as variables rather than as rules so the whole banner can be * restyled from the settings screen without the stylesheet knowing anything * about a particular site's palette, and so a theme can override any one of * them without fighting specificity. * * @return string */ private function style_vars() { $banner = Helper::get( 'banner', [] ); $vars = [ '--ablocks-cc-bg' => $banner['bg'], '--ablocks-cc-text' => $banner['text'], '--ablocks-cc-muted' => $banner['muted'], '--ablocks-cc-border' => $banner['border'], '--ablocks-cc-accent' => $banner['accent'], '--ablocks-cc-accent-text' => $banner['accent_text'], '--ablocks-cc-secondary-bg' => $banner['secondary_bg'], '--ablocks-cc-secondary-text' => $banner['secondary_text'], '--ablocks-cc-save-bg' => $banner['save_bg'], '--ablocks-cc-save-text' => $banner['save_text'], '--ablocks-cc-locked-bg' => $banner['locked_bg'], '--ablocks-cc-locked-text' => $banner['locked_text'], '--ablocks-cc-radius' => (int) $banner['radius'] . 'px', '--ablocks-cc-max-width' => (int) $banner['max_width'] . 'px', '--ablocks-cc-prefs-max-width' => (int) $banner['prefs_max_width'] . 'px', ]; $out = ''; foreach ( $vars as $name => $value ) { $out .= sprintf( '%s:%s;', $name, esc_attr( $value ) ); } return $out; } public function render() { if ( ! Helper::should_render_banner() ) { return; } $banner = Helper::get( 'banner', [] ); $modal = 'modal' === $banner['prefs_layout']; $classes = [ 'ablocks-consent', 'ablocks-consent--' . sanitize_html_class( $banner['layout'] ), 'ablocks-consent--' . sanitize_html_class( $banner['position'] ), ]; if ( ! empty( $banner['shadow'] ) ) { $classes[] = 'has-shadow'; } ?>