__NAMESPACE__ . '\load_assets' ) ); } add_action( 'init', __NAMESPACE__ . '\register_block' ); /** * Cookie-consent block registration/dependency declaration. * * @param array $attr Array containing the Cookie-consent block attributes. * @param string $content String containing the Cookie-consent block content. * * @return string */ function load_assets( $attr, $content ) { // We want to bust the cache even if the cookie isn't set. // This is needed for when the cookie expires, // and we should send fresh HTML with the cookie block in it. notify_batcache_that_content_changed(); // If the user has already accepted the cookie consent, don't show the block. if ( isset( $_COOKIE[ COOKIE_NAME ] ) ) { return ''; } /* * Enqueue necessary scripts and styles. */ Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); return sprintf( '
%2$s
', esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ), $content ); } /** * 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). * Because, by default, the cache doesn't vary around the cookie's value. This makes the cookie value part of the cache key. * * See: https://github.com/Automattic/batcache/blob/d4f617b335e9772a61b6d03ad3498b55c8137592/advanced-cache.php#L29 */ function notify_batcache_that_content_changed() { if ( function_exists( 'vary_cache_on_function' ) ) { vary_cache_on_function( 'return isset( $_COOKIE[ "' . COOKIE_NAME . '" ] );' ); } }