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