cookie-consent.php
99 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Cookie-consent Block. |
| 4 | * |
| 5 | * @since 12.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\CookieConsent; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | const FEATURE_NAME = 'cookie-consent'; |
| 16 | const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
| 17 | const COOKIE_NAME = 'eucookielaw'; |
| 18 | |
| 19 | /** |
| 20 | * Should the block be registered? |
| 21 | * In wp-admin, we only want to show the block in the site editor. |
| 22 | * |
| 23 | * @since 12.0 |
| 24 | * |
| 25 | * @return bool |
| 26 | */ |
| 27 | function should_register_block() { |
| 28 | global $pagenow; |
| 29 | |
| 30 | // Always register the widget if we're on the front end |
| 31 | if ( ! is_admin() ) { |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | if ( is_admin() && $pagenow === 'site-editor.php' ) { |
| 36 | return true; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Registers the block for use in Gutenberg |
| 42 | * This is done via an action so that we can disable |
| 43 | * registration if we need to. |
| 44 | */ |
| 45 | function register_block() { |
| 46 | if ( ! should_register_block() ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | Blocks::jetpack_register_block( |
| 51 | BLOCK_NAME, |
| 52 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 53 | ); |
| 54 | } |
| 55 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 56 | |
| 57 | /** |
| 58 | * Cookie-consent block registration/dependency declaration. |
| 59 | * |
| 60 | * @param array $attr Array containing the Cookie-consent block attributes. |
| 61 | * @param string $content String containing the Cookie-consent block content. |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | function load_assets( $attr, $content ) { |
| 66 | // We want to bust the cache even if the cookie isn't set. |
| 67 | // This is needed for when the cookie expires, |
| 68 | // and we should send fresh HTML with the cookie block in it. |
| 69 | notify_batcache_that_content_changed(); |
| 70 | |
| 71 | // If the user has already accepted the cookie consent, don't show the block. |
| 72 | if ( isset( $_COOKIE[ COOKIE_NAME ] ) ) { |
| 73 | return ''; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Enqueue necessary scripts and styles. |
| 78 | */ |
| 79 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
| 80 | |
| 81 | return sprintf( |
| 82 | '<div class="%1$s">%2$s</div>', |
| 83 | esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ), |
| 84 | $content |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * 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). |
| 90 | * Because, by default, the cache doesn't vary around the cookie's value. This makes the cookie value part of the cache key. |
| 91 | * |
| 92 | * See: https://github.com/Automattic/batcache/blob/d4f617b335e9772a61b6d03ad3498b55c8137592/advanced-cache.php#L29 |
| 93 | */ |
| 94 | function notify_batcache_that_content_changed() { |
| 95 | if ( function_exists( 'vary_cache_on_function' ) ) { |
| 96 | vary_cache_on_function( 'return isset( $_COOKIE[ "' . COOKIE_NAME . '" ] );' ); |
| 97 | } |
| 98 | } |
| 99 |