← All changes
|
extensions/blocks/cookie-consent/cookie-consent.php
+51
-8
12.5.2
→
16.3-a.7
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,12 +49,12 @@ | ||
| 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__, | |
| 53 | 54 | array( |
| 54 | - 'attributes' => array( | |
| 55 | + 'render_callback' => __NAMESPACE__ . '\load_assets', | |
| 56 | + 'attributes' => array( | |
| 55 | 57 | 'render_from_template' => array( |
| 56 | 58 | 'default' => false, |
| 57 | 59 | 'type' => 'boolean', |
| 58 | 60 | ), |
| @@ -75,8 +77,21 @@ | ||
| 75 | 77 | // This is needed for when the cookie expires, |
| 76 | 78 | // and we should send fresh HTML with the cookie block in it. |
| 77 | 79 | notify_batcache_that_content_changed(); |
| 78 | 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 | + | |
| 79 | 94 | // If the user has already accepted the cookie consent, don't show the block. |
| 80 | 95 | if ( isset( $_COOKIE[ COOKIE_NAME ] ) ) { |
| 81 | 96 | return ''; |
| 82 | 97 | } |
| @@ -88,13 +103,18 @@ | ||
| 88 | 103 | |
| 89 | 104 | /* |
| 90 | 105 | * Enqueue necessary scripts and styles. |
| 91 | 106 | */ |
| 92 | - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); | |
| 107 | + Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 93 | 108 | |
| 109 | + /* | |
| 110 | + * Add a fallback color to block styles. | |
| 111 | + */ | |
| 112 | + $content = add_css_fallback_values( $content ); | |
| 113 | + | |
| 94 | 114 | return sprintf( |
| 95 | 115 | '<div class="%1$s">%2$s</div>', |
| 96 | - esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ), | |
| 116 | + esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), | |
| 97 | 117 | $content |
| 98 | 118 | ); |
| 99 | 119 | } |
| 100 | 120 | |
| @@ -156,4 +176,27 @@ | ||
| 156 | 176 | ); |
| 157 | 177 | } |
| 158 | 178 | |
| 159 | 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 | +} | |