| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 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 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | function should_register_block() { |
| 30 | global $pagenow; |
| 31 | |
| 32 | |
| 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 | |
| 44 | |
| 45 | |
| 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 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | function load_assets( $attr, $content ) { |
| 76 | |
| 77 | |
| 78 | |
| 79 | notify_batcache_that_content_changed(); |
| 80 | |
| 81 | if ( |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | apply_filters( 'jetpack_disable_cookie_consent_block', false ) |
| 90 | ) { |
| 91 | return ''; |
| 92 | } |
| 93 | |
| 94 | |
| 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 | |
| 106 | |
| 107 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | $content = add_css_fallback_values( $content ); |
| 113 | |
| 114 | |
| 115 | return $content; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | function notify_batcache_that_content_changed() { |
| 125 | if ( function_exists( 'vary_cache_on_function' ) ) { |
| 126 | vary_cache_on_function( 'return isset( $_COOKIE[ "' . COOKIE_NAME . '" ] );' ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | function render_cookie_consent_template() { |
| 136 | |
| 137 | if ( is_admin() ) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | if ( ! function_exists( 'parse_blocks' ) ) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | $template = get_option( 'cookie_consent_template' ); |
| 147 | |
| 148 | if ( empty( $template ) ) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | $parsed = parse_blocks( $template ); |
| 153 | if ( ! empty( $parsed[0] ) ) { |
| 154 | $parsed[0]['attrs']['render_from_template'] = true; |
| 155 | |
| 156 | $custom_css_before = get_custom_css_rules(); |
| 157 | $block_supports_before = get_block_supports_rules(); |
| 158 | $banner = render_block( $parsed[0] ); |
| 159 | if ( '' !== $banner ) { |
| 160 | print_late_custom_css_rules( $custom_css_before ); |
| 161 | print_late_block_supports_rules( $block_supports_before ); |
| 162 | echo $banner; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | add_action( 'wp_footer', __NAMESPACE__ . '\render_cookie_consent_template' ); |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | function get_custom_css_rules() { |
| 176 | $rules = wp_styles()->get_data( 'wp-block-custom-css', 'after' ); |
| 177 | return is_array( $rules ) ? $rules : array(); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | function print_late_custom_css_rules( $rules_before ) { |
| 190 | if ( ! wp_style_is( 'wp-block-custom-css', 'done' ) ) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | $new_rules = array_diff( get_custom_css_rules(), $rules_before ); |
| 195 | if ( $new_rules ) { |
| 196 | $processor = new \WP_HTML_Tag_Processor( '<style></style>' ); |
| 197 | $processor->next_tag(); |
| 198 | $processor->set_attribute( 'id', 'jetpack-cookie-consent-custom-css' ); |
| 199 | $processor->set_modifiable_text( implode( '', $new_rules ) ); |
| 200 | echo $processor->get_updated_html(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | function get_block_supports_rules() { |
| 212 | return \WP_Style_Engine_CSS_Rules_Store::get_store( 'block-supports' )->get_all_rules(); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | function print_late_block_supports_rules( $rules_before ) { |
| 225 | $new_rules = array_diff_key( get_block_supports_rules(), $rules_before ); |
| 226 | if ( ! $new_rules ) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | $processor = new \WP_Style_Engine_Processor(); |
| 231 | $processor->add_rules( array_values( $new_rules ) ); |
| 232 | $css = $processor->get_css( array( 'prettify' => false ) ); |
| 233 | if ( '' === $css ) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | $tag = new \WP_HTML_Tag_Processor( '<style></style>' ); |
| 238 | $tag->next_tag(); |
| 239 | $tag->set_attribute( 'id', 'jetpack-cookie-consent-block-supports-css' ); |
| 240 | $tag->set_modifiable_text( $css ); |
| 241 | echo $tag->get_updated_html(); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | function cookie_consent_register_settings() { |
| 250 | register_setting( |
| 251 | 'general', |
| 252 | 'cookie_consent_template', |
| 253 | array( |
| 254 | 'type' => 'string', |
| 255 | 'show_in_rest' => true, |
| 256 | ) |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | add_action( 'rest_api_init', __NAMESPACE__ . '\cookie_consent_register_settings' ); |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | function add_css_fallback_values( $content ) { |
| 271 | |
| 272 | $css_fallbacks = array( |
| 273 | 'var(--wp--preset--color--contrast)' => 'var(--wp--preset--color--contrast, #000000)', |
| 274 | 'var(--wp--preset--color--tertiary)' => 'var(--wp--preset--color--tertiary, #f0f0f0)', |
| 275 | ); |
| 276 | |
| 277 | |
| 278 | foreach ( $css_fallbacks as $original => $with_fallback ) { |
| 279 | $content = str_replace( $original, $with_fallback, $content ); |
| 280 | } |
| 281 | |
| 282 | return $content; |
| 283 | } |