cookie-consent.php
175 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 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit( 0 ); |
| 17 | } |
| 18 | |
| 19 | const COOKIE_NAME = 'eucookielaw'; |
| 20 | |
| 21 | /** |
| 22 | * Should the block be registered? |
| 23 | * In wp-admin, we only want to show the block in the site editor. |
| 24 | * |
| 25 | * @since 12.0 |
| 26 | * |
| 27 | * @return bool |
| 28 | */ |
| 29 | function should_register_block() { |
| 30 | global $pagenow; |
| 31 | |
| 32 | // Always register the widget if we're on the front end |
| 33 | if ( ! is_admin() ) { |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | if ( is_admin() && $pagenow === 'site-editor.php' ) { |
| 38 | return true; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Registers the block for use in Gutenberg |
| 44 | * This is done via an action so that we can disable |
| 45 | * registration if we need to. |
| 46 | */ |
| 47 | function register_block() { |
| 48 | if ( ! should_register_block() ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | Blocks::jetpack_register_block( |
| 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 | ) |
| 63 | ); |
| 64 | } |
| 65 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 66 | |
| 67 | /** |
| 68 | * Cookie-consent block registration/dependency declaration. |
| 69 | * |
| 70 | * @param array $attr Array containing the Cookie-consent block attributes. |
| 71 | * @param string $content String containing the Cookie-consent block content. |
| 72 | * |
| 73 | * @return string |
| 74 | */ |
| 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 | |
| 94 | // If the user has already accepted the cookie consent, don't show the block. |
| 95 | if ( isset( $_COOKIE[ COOKIE_NAME ] ) ) { |
| 96 | return ''; |
| 97 | } |
| 98 | |
| 99 | $option = get_option( 'cookie_consent_template' ); |
| 100 | if ( ! empty( $option ) && empty( $attr['render_from_template'] ) ) { |
| 101 | return ''; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Enqueue necessary scripts and styles. |
| 106 | */ |
| 107 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 108 | |
| 109 | return sprintf( |
| 110 | '<div class="%1$s">%2$s</div>', |
| 111 | esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), |
| 112 | $content |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * 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). |
| 118 | * Because, by default, the cache doesn't vary around the cookie's value. This makes the cookie value part of the cache key. |
| 119 | * |
| 120 | * See: https://github.com/Automattic/batcache/blob/d4f617b335e9772a61b6d03ad3498b55c8137592/advanced-cache.php#L29 |
| 121 | */ |
| 122 | function notify_batcache_that_content_changed() { |
| 123 | if ( function_exists( 'vary_cache_on_function' ) ) { |
| 124 | vary_cache_on_function( 'return isset( $_COOKIE[ "' . COOKIE_NAME . '" ] );' ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Render the cookie consent template. |
| 130 | * |
| 131 | * @since 12.4 |
| 132 | */ |
| 133 | function render_cookie_consent_template() { |
| 134 | |
| 135 | if ( is_admin() ) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // Check whether block theme functions exist. |
| 140 | if ( ! function_exists( 'parse_blocks' ) ) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $template = get_option( 'cookie_consent_template' ); |
| 145 | |
| 146 | if ( empty( $template ) ) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | $parsed = parse_blocks( $template ); |
| 151 | if ( ! empty( $parsed[0] ) ) { |
| 152 | $parsed[0]['attrs']['render_from_template'] = true; |
| 153 | echo render_block( $parsed[0] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 154 | } |
| 155 | } |
| 156 | add_action( 'wp_footer', __NAMESPACE__ . '\render_cookie_consent_template' ); |
| 157 | |
| 158 | /** |
| 159 | * Register cookie_consent_template setting |
| 160 | * |
| 161 | * @since 12.4 |
| 162 | */ |
| 163 | function cookie_consent_register_settings() { |
| 164 | register_setting( |
| 165 | 'general', |
| 166 | 'cookie_consent_template', |
| 167 | array( |
| 168 | 'type' => 'string', |
| 169 | 'show_in_rest' => true, |
| 170 | ) |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | add_action( 'rest_api_init', __NAMESPACE__ . '\cookie_consent_register_settings' ); |
| 175 |