PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | extensions/blocks/cookie-consent/cookie-consent.php +127 -7 12.0.3 → 16.3-a.1 View file →
@@ -11,12 +11,14 @@
11 11
12 12 use Automattic\Jetpack\Blocks;
13 13 use Jetpack_Gutenberg;
14 14
15 -const FEATURE_NAME = 'cookie-consent';
16 -const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
17 -const COOKIE_NAME = 'eucookielaw';
15 +if ( ! defined( 'ABSPATH' ) ) {
16 + exit( 0 );
17 +}
18 18
19 +const COOKIE_NAME = 'eucookielaw';
20 +
19 21 /**
20 22 * Should the block be registered?
21 23 * In wp-admin, we only want to show the block in the site editor.
22 24 *
@@ -47,10 +49,18 @@
47 49 return;
48 50 }
49 51
50 52 Blocks::jetpack_register_block(
51 - BLOCK_NAME,
52 - array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
53 + __DIR__,
54 + array(
55 + 'render_callback' => __NAMESPACE__ . '\load_assets',
56 + 'attributes' => array(
57 + 'render_from_template' => array(
58 + 'default' => false,
59 + 'type' => 'boolean',
60 + ),
61 + ),
62 + )
53 63 );
54 64 }
55 65 add_action( 'init', __NAMESPACE__ . '\register_block' );
56 66
@@ -62,21 +72,131 @@
62 72 *
63 73 * @return string
64 74 */
65 75 function load_assets( $attr, $content ) {
76 + // We want to bust the cache even if the cookie isn't set.
77 + // This is needed for when the cookie expires,
78 + // and we should send fresh HTML with the cookie block in it.
79 + notify_batcache_that_content_changed();
80 +
81 + if (
82 + /**
83 + * Filters the display of the Cookie-consent Block e.g by GDPR CMP banner on WordAds sites.
84 + *
85 + * @since 13.2
86 + *
87 + * @param bool $disable_cookie_consent_block Whether to disable the Cookie-consent Block.
88 + */
89 + apply_filters( 'jetpack_disable_cookie_consent_block', false )
90 + ) {
91 + return '';
92 + }
93 +
66 94 // If the user has already accepted the cookie consent, don't show the block.
67 95 if ( isset( $_COOKIE[ COOKIE_NAME ] ) ) {
68 96 return '';
69 97 }
70 98
99 + $option = get_option( 'cookie_consent_template' );
100 + if ( ! empty( $option ) && empty( $attr['render_from_template'] ) ) {
101 + return '';
102 + }
103 +
71 104 /*
72 105 * Enqueue necessary scripts and styles.
73 106 */
74 - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
107 + Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
75 108
109 + /*
110 + * Add a fallback color to block styles.
111 + */
112 + $content = add_css_fallback_values( $content );
113 +
76 114 return sprintf(
77 115 '<div class="%1$s">%2$s</div>',
78 - esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ),
116 + esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ),
79 117 $content
80 118 );
81 119 }
82 120
121 +/**
122 + * 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).
123 + * Because, by default, the cache doesn't vary around the cookie's value. This makes the cookie value part of the cache key.
124 + *
125 + * See: https://github.com/Automattic/batcache/blob/d4f617b335e9772a61b6d03ad3498b55c8137592/advanced-cache.php#L29
126 + */
127 +function notify_batcache_that_content_changed() {
128 + if ( function_exists( 'vary_cache_on_function' ) ) {
129 + vary_cache_on_function( 'return isset( $_COOKIE[ "' . COOKIE_NAME . '" ] );' );
130 + }
131 +}
132 +
133 +/**
134 + * Render the cookie consent template.
135 + *
136 + * @since 12.4
137 + */
138 +function render_cookie_consent_template() {
139 +
140 + if ( is_admin() ) {
141 + return;
142 + }
143 +
144 + // Check whether block theme functions exist.
145 + if ( ! function_exists( 'parse_blocks' ) ) {
146 + return;
147 + }
148 +
149 + $template = get_option( 'cookie_consent_template' );
150 +
151 + if ( empty( $template ) ) {
152 + return;
153 + }
154 +
155 + $parsed = parse_blocks( $template );
156 + if ( ! empty( $parsed[0] ) ) {
157 + $parsed[0]['attrs']['render_from_template'] = true;
158 + echo render_block( $parsed[0] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
159 + }
160 +}
161 +add_action( 'wp_footer', __NAMESPACE__ . '\render_cookie_consent_template' );
162 +
163 +/**
164 + * Register cookie_consent_template setting
165 + *
166 + * @since 12.4
167 + */
168 +function cookie_consent_register_settings() {
169 + register_setting(
170 + 'general',
171 + 'cookie_consent_template',
172 + array(
173 + 'type' => 'string',
174 + 'show_in_rest' => true,
175 + )
176 + );
177 +}
178 +
179 +add_action( 'rest_api_init', __NAMESPACE__ . '\cookie_consent_register_settings' );
180 +
181 +/**
182 + * Add a fallback color to block styles.
183 + *
184 + * @since 15.1
185 + *
186 + * @param string $content The block content.
187 + * @return string The content with fallback values added to CSS custom properties.
188 + */
189 +function add_css_fallback_values( $content ) {
190 + // Define the CSS custom properties and their fallback values.
191 + $css_fallbacks = array(
192 + 'var(--wp--preset--color--contrast)' => 'var(--wp--preset--color--contrast, #000000)',
193 + 'var(--wp--preset--color--tertiary)' => 'var(--wp--preset--color--tertiary, #f0f0f0)',
194 + );
195 +
196 + // Replace CSS custom properties with their fallback versions.
197 + foreach ( $css_fallbacks as $original => $with_fallback ) {
198 + $content = str_replace( $original, $with_fallback, $content );
199 + }
200 +
201 + return $content;
202 +}