Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.61% covered (warning)
67.61%
48 / 71
55.56% covered (warning)
55.56%
5 / 9
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\CookieConsent\should_register_block
n/a
0 / 0
n/a
0 / 0
1
Automattic\Jetpack\Extensions\CookieConsent\register_block
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Extensions\CookieConsent\load_assets
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
5.15
Automattic\Jetpack\Extensions\CookieConsent\notify_batcache_that_content_changed
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
Automattic\Jetpack\Extensions\CookieConsent\render_cookie_consent_template
80.00% covered (warning)
80.00%
12 / 15
0.00% covered (danger)
0.00%
0 / 1
6.29
Automattic\Jetpack\Extensions\CookieConsent\get_custom_css_rules
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
Automattic\Jetpack\Extensions\CookieConsent\print_late_custom_css_rules
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
Automattic\Jetpack\Extensions\CookieConsent\cookie_consent_register_settings
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\Extensions\CookieConsent\add_css_fallback_values
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Cookie-consent Block.
4 *
5 * @since 12.0
6 *
7 * @package automattic/jetpack
8 */
9
10namespace Automattic\Jetpack\Extensions\CookieConsent;
11
12use Automattic\Jetpack\Blocks;
13use Jetpack_Gutenberg;
14
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19const COOKIE_NAME = 'eucookielaw';
20
21/**
22 * Whether the block should be registered.
23 *
24 * @since 12.0
25 * @deprecated $$next-version$$ The block is now registered in every editor.
26 *
27 * @return bool Always true.
28 */
29function should_register_block() {
30    _deprecated_function( __FUNCTION__, 'jetpack-$$next-version$$' );
31    return true;
32}
33
34/**
35 * Registers the block for use in Gutenberg
36 * This is done via an action so that we can disable
37 * registration if we need to.
38 */
39function register_block() {
40    Blocks::jetpack_register_block(
41        __DIR__,
42        array(
43            'render_callback' => __NAMESPACE__ . '\load_assets',
44            'attributes'      => array(
45                'render_from_template' => array(
46                    'default' => false,
47                    'type'    => 'boolean',
48                ),
49            ),
50        )
51    );
52}
53add_action( 'init', __NAMESPACE__ . '\register_block' );
54
55/**
56 * Cookie-consent block registration/dependency declaration.
57 *
58 * @param array  $attr    Array containing the Cookie-consent block attributes.
59 * @param string $content String containing the Cookie-consent block content.
60 *
61 * @return string
62 */
63function load_assets( $attr, $content ) {
64    // We want to bust the cache even if the cookie isn't set.
65    // This is needed for when the cookie expires,
66    // and we should send fresh HTML with the cookie block in it.
67    notify_batcache_that_content_changed();
68
69    if (
70        /**
71         * Filters the display of the Cookie-consent Block e.g by GDPR CMP banner on WordAds sites.
72         *
73         * @since 13.2
74         *
75         * @param bool $disable_cookie_consent_block Whether to disable the Cookie-consent Block.
76         */
77        apply_filters( 'jetpack_disable_cookie_consent_block', false )
78    ) {
79        return '';
80    }
81
82    // If the user has already accepted the cookie consent, don't show the block.
83    if ( isset( $_COOKIE[ COOKIE_NAME ] ) ) {
84        return '';
85    }
86
87    $option = get_option( 'cookie_consent_template' );
88    if ( ! empty( $option ) && empty( $attr['render_from_template'] ) ) {
89        return '';
90    }
91
92    /*
93     * Enqueue necessary scripts and styles.
94     */
95    Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
96
97    /*
98     * Add a fallback color to block styles.
99     */
100    $content = add_css_fallback_values( $content );
101
102    // The saved markup already carries the block classes; a wrapper would take core's Custom CSS class instead of the banner.
103    return $content;
104}
105
106/**
107 * Batcache busting: since the cookie consent is part of the cached response HTML, it can still render even when the cookie is set (when it shouldn't).
108 * Because, by default, the cache doesn't vary around the cookie's value. This makes the cookie value part of the cache key.
109 *
110 * See: https://github.com/Automattic/batcache/blob/d4f617b335e9772a61b6d03ad3498b55c8137592/advanced-cache.php#L29
111 */
112function notify_batcache_that_content_changed() {
113    if ( function_exists( 'vary_cache_on_function' ) ) {
114        vary_cache_on_function( 'return isset( $_COOKIE[ "' . COOKIE_NAME . '" ] );' );
115    }
116}
117
118/**
119 * Render the cookie consent template.
120 *
121 * @since 12.4
122 */
123function render_cookie_consent_template() {
124
125    if ( is_admin() ) {
126        return;
127    }
128
129    // Check whether block theme functions exist.
130    if ( ! function_exists( 'parse_blocks' ) ) {
131        return;
132    }
133
134    $template = get_option( 'cookie_consent_template' );
135
136    if ( empty( $template ) ) {
137        return;
138    }
139
140    $parsed = parse_blocks( $template );
141    if ( ! empty( $parsed[0] ) ) {
142        $parsed[0]['attrs']['render_from_template'] = true;
143
144        $custom_css_before = get_custom_css_rules();
145        $banner            = render_block( $parsed[0] );
146        if ( '' !== $banner ) {
147            print_late_custom_css_rules( $custom_css_before );
148            echo $banner; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
149        }
150    }
151}
152add_action( 'wp_footer', __NAMESPACE__ . '\render_cookie_consent_template' );
153
154/**
155 * Get the block Custom CSS rules queued so far.
156 *
157 * @since 16.4
158 *
159 * @return string[] Rules, empty when the core handle is not in use.
160 */
161function get_custom_css_rules() {
162    $rules = wp_styles()->get_data( 'wp-block-custom-css', 'after' );
163    return is_array( $rules ) ? $rules : array();
164}
165
166/**
167 * Print Custom CSS rules queued since the snapshot, if the core handle already printed in the head.
168 *
169 * The banner renders in the footer, too late for rules it queues to print on their own.
170 *
171 * @since 16.4
172 *
173 * @param string[] $rules_before Rules from get_custom_css_rules() before the render.
174 */
175function print_late_custom_css_rules( $rules_before ) {
176    if ( ! wp_style_is( 'wp-block-custom-css', 'done' ) ) {
177        return;
178    }
179
180    $new_rules = array_diff( get_custom_css_rules(), $rules_before );
181    if ( $new_rules ) {
182        $processor = new \WP_HTML_Tag_Processor( '<style></style>' );
183        $processor->next_tag();
184        $processor->set_attribute( 'id', 'jetpack-cookie-consent-custom-css' );
185        $processor->set_modifiable_text( implode( '', $new_rules ) );
186        echo $processor->get_updated_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
187    }
188}
189
190/**
191 * Register cookie_consent_template setting
192 *
193 * @since 12.4
194 */
195function cookie_consent_register_settings() {
196    register_setting(
197        'general',
198        'cookie_consent_template',
199        array(
200            'type'         => 'string',
201            'show_in_rest' => true,
202        )
203    );
204}
205
206add_action( 'rest_api_init', __NAMESPACE__ . '\cookie_consent_register_settings' );
207
208/**
209 * Add a fallback color to block styles.
210 *
211 * @since 15.1
212 *
213 * @param string $content The block content.
214 * @return string The content with fallback values added to CSS custom properties.
215 */
216function add_css_fallback_values( $content ) {
217    // Define the CSS custom properties and their fallback values.
218    $css_fallbacks = array(
219        'var(--wp--preset--color--contrast)' => 'var(--wp--preset--color--contrast, #000000)',
220        'var(--wp--preset--color--tertiary)' => 'var(--wp--preset--color--tertiary, #f0f0f0)',
221    );
222
223    // Replace CSS custom properties with their fallback versions.
224    foreach ( $css_fallbacks as $original => $with_fallback ) {
225        $content = str_replace( $original, $with_fallback, $content );
226    }
227
228    return $content;
229}