| @@ -142,14 +142,22 @@ | ||
| 142 | 142 | $block_name = $block_instance->name; |
| 143 | 143 | do_action( 'ablocks/before_render_' . explode( '/', $block_name )[1] . '_block_content', $block_name ); |
| 144 | 144 | do_action( 'ablocks/render_callback', $block_name, $attributes ); |
| 145 | 145 | |
| 146 | + // Animation CSS is no longer a global dependency (see get_style_depends). | |
| 147 | + // In the fallback path (asset-generation off) enqueue it only when this | |
| 148 | + // block actually animates, so pages without animation never load it. | |
| 149 | + $this->maybe_enqueue_animate_style( $attributes ); | |
| 150 | + | |
| 146 | 151 | // Dynamic block |
| 147 | 152 | if ( ! $content || $this->is_skip_inner_block ) { |
| 148 | 153 | // When called from the editor's ServerSideRender (REST API), RenderContainer (JS) already |
| 149 | 154 | // provides the outer ablocks-block-{blockId} wrapper via useBlockProps. Including it here |
| 150 | 155 | // too causes the Advanced-settings CSS selector to match two elements → double border/padding. |
| 151 | - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { | |
| 156 | + // Only that preview render (it always sends context=edit): front-end REST endpoints such as the | |
| 157 | + // loop filter re-render blocks into the page, where the wrapper and its scoped styles are needed. | |
| 158 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 159 | + if ( defined( 'REST_REQUEST' ) && REST_REQUEST && 'edit' === ( $_REQUEST['context'] ?? '' ) ) { | |
| 152 | 160 | ob_start(); |
| 153 | 161 | echo $this->render_block_content( $attributes, $content, $block_instance ); |
| 154 | 162 | $content = ob_get_clean(); |
| 155 | 163 | } else { |
| @@ -186,8 +194,23 @@ | ||
| 186 | 194 | } |
| 187 | 195 | return self::$asset_version_cache[ $path ]; |
| 188 | 196 | } |
| 189 | 197 | |
| 198 | + /** | |
| 199 | + * Enqueue the animate.css library only when a block actually uses an | |
| 200 | + * animation, and only in the fallback (per-block) asset path — with | |
| 201 | + * asset-generation on, the combined generator bakes it in on demand instead. | |
| 202 | + */ | |
| 203 | + private function maybe_enqueue_animate_style( $attributes ) { | |
| 204 | + if ( is_admin() || Helper::is_enabled_assets_generation() ) { | |
| 205 | + return; | |
| 206 | + } | |
| 207 | + $animation_type = isset( $attributes['_animation']['animationType'] ) ? $attributes['_animation']['animationType'] : ''; | |
| 208 | + if ( ! empty( $animation_type ) && 'none' !== $animation_type ) { | |
| 209 | + wp_enqueue_style( 'ablocks-animate-style' ); | |
| 210 | + } | |
| 211 | + } | |
| 212 | + | |
| 190 | 213 | private function enqueue_static_assets( $block_name ) { |
| 191 | 214 | // Library |
| 192 | 215 | if ( count( $this->get_style_depends() ) ) { |
| 193 | 216 | foreach ( $this->get_style_depends() as $style_handler ) { |
| @@ -212,21 +235,19 @@ | ||
| 212 | 235 | $args = [ 'strategy' => $script_loading_strategy ]; |
| 213 | 236 | |
| 214 | 237 | if ( false !== $this->cached_asset_version( $this->assets_path . 'build/blocks/' . $block_name . '/view.js' ) && ! wp_script_is( 'ablocks-' . $block_name . '-block-static-script', 'enqueued' ) ) { |
| 215 | 238 | $dependencies = include $this->assets_path . 'build/blocks/' . $block_name . '/view.asset.php'; |
| 239 | + // Depend on the shared data-only handle so ABlocksGlobal is printed | |
| 240 | + // before this script runs, without each block carrying its own copy. | |
| 241 | + $deps = array_values( array_unique( array_merge( array( 'ablocks-globals' ), $dependencies['dependencies'] ) ) ); | |
| 216 | 242 | wp_enqueue_script( |
| 217 | 243 | 'ablocks-' . $block_name . '-block-static-script', |
| 218 | 244 | $this->assets_url . 'build/blocks/' . $block_name . '/view.js', |
| 219 | - $dependencies['dependencies'], | |
| 245 | + $deps, | |
| 220 | 246 | $dependencies['version'], |
| 221 | 247 | $args |
| 222 | 248 | ); |
| 223 | - $Assets = new Assets(); | |
| 224 | - wp_localize_script( | |
| 225 | - 'ablocks-' . $block_name . '-block-static-script', | |
| 226 | - 'ABlocksGlobal', | |
| 227 | - $Assets->get_localize_frontend_data() | |
| 228 | - ); | |
| 249 | + Assets::localize_globals_once(); | |
| 229 | 250 | } |
| 230 | 251 | |
| 231 | 252 | } |
| 232 | 253 | |
| @@ -245,13 +266,89 @@ | ||
| 245 | 266 | if ( file_exists( $this->assets_path . 'build/blocks/' . $this->block_name . '/style.css' ) ) { |
| 246 | 267 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 247 | 268 | $has_static_css = file_get_contents( $this->assets_path . 'build/blocks/' . $this->block_name . '/style.css' ); |
| 248 | 269 | if ( $has_static_css ) { |
| 249 | - return $has_static_css; | |
| 270 | + // A block's style.css doubles as its editorStyle, so it can carry | |
| 271 | + // editor-chrome rules (.block-editor-*, .editor-styles-wrapper …) | |
| 272 | + // that never match on the front end. Strip them from the frontend | |
| 273 | + // static CSS (this getter feeds the combined/inline page CSS; the | |
| 274 | + // editor loads style.css directly via block.json). | |
| 275 | + return self::strip_editor_only_css( $has_static_css ); | |
| 250 | 276 | } |
| 251 | 277 | } |
| 252 | 278 | return ''; |
| 253 | 279 | } |
| 280 | + | |
| 281 | + /** | |
| 282 | + * Remove rules whose selector is entirely editor-chrome (every | |
| 283 | + * comma-separated part references an editor-only class), leaving mixed and | |
| 284 | + * front-end rules untouched. Brace-aware so @media blocks stay intact. | |
| 285 | + */ | |
| 286 | + private static $editor_css_cache = []; | |
| 287 | + public static function strip_editor_only_css( $css ) { | |
| 288 | + $key = md5( $css ); | |
| 289 | + if ( isset( self::$editor_css_cache[ $key ] ) ) { | |
| 290 | + return self::$editor_css_cache[ $key ]; | |
| 291 | + } | |
| 292 | + $markers = [ '.block-editor-', '.editor-styles-wrapper', '.block-list-appender', '.components-base-control', '.block-editor-block-variation-picker', '.block-editor-button-block-appender' ]; | |
| 293 | + | |
| 294 | + $out = ''; | |
| 295 | + $len = strlen( $css ); | |
| 296 | + $i = 0; | |
| 297 | + $buf = ''; | |
| 298 | + while ( $i < $len ) { | |
| 299 | + $ch = $css[ $i ]; | |
| 300 | + $buf .= $ch; | |
| 301 | + if ( '{' === $ch ) { | |
| 302 | + $prelude = trim( substr( $buf, 0, -1 ) ); | |
| 303 | + $depth = 1; | |
| 304 | + $i++; | |
| 305 | + while ( $i < $len && $depth > 0 ) { | |
| 306 | + $c = $css[ $i ]; | |
| 307 | + $buf .= $c; | |
| 308 | + if ( '{' === $c ) { | |
| 309 | + $depth++; | |
| 310 | + } elseif ( '}' === $c ) { | |
| 311 | + $depth--; | |
| 312 | + } | |
| 313 | + $i++; | |
| 314 | + } | |
| 315 | + $drop = false; | |
| 316 | + if ( '' !== $prelude && '@' !== $prelude[0] ) { | |
| 317 | + $parts = array_map( 'trim', explode( ',', $prelude ) ); | |
| 318 | + $drop = true; | |
| 319 | + foreach ( $parts as $part ) { | |
| 320 | + // Drop :not(...) negations first — a selector like | |
| 321 | + // ":not(.block-editor-block-list__block) .foo" is a FRONT-END | |
| 322 | + // rule (applies everywhere except the editor), NOT editor | |
| 323 | + // chrome, so the marker inside :not() must not count. | |
| 324 | + $positive = preg_replace( '/:not\([^)]*\)/', '', $part ); | |
| 325 | + $is_editor = false; | |
| 326 | + foreach ( $markers as $m ) { | |
| 327 | + if ( false !== strpos( $positive, $m ) ) { | |
| 328 | + $is_editor = true; | |
| 329 | + break; | |
| 330 | + } | |
| 331 | + } | |
| 332 | + if ( ! $is_editor ) { | |
| 333 | + $drop = false; // a front-end part exists — keep the rule | |
| 334 | + break; | |
| 335 | + } | |
| 336 | + } | |
| 337 | + } | |
| 338 | + if ( ! $drop ) { | |
| 339 | + $out .= $buf; | |
| 340 | + } | |
| 341 | + $buf = ''; | |
| 342 | + continue; | |
| 343 | + } | |
| 344 | + $i++; | |
| 345 | + } | |
| 346 | + $out .= $buf; | |
| 347 | + | |
| 348 | + self::$editor_css_cache[ $key ] = $out; | |
| 349 | + return $out; | |
| 350 | + } | |
| 254 | 351 | public function get_static_js() { |
| 255 | 352 | if ( file_exists( $this->assets_path . 'build/blocks/' . $this->block_name . '/view.js' ) ) { |
| 256 | 353 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 257 | 354 | $has_static_js = file_get_contents( $this->assets_path . 'build/blocks/' . $this->block_name . '/view.js' ); |
| @@ -261,9 +358,14 @@ | ||
| 261 | 358 | } |
| 262 | 359 | return ''; |
| 263 | 360 | } |
| 264 | 361 | public function get_style_depends() { |
| 265 | - return apply_filters( 'ablocks/block_style_depends', array_merge( $this->style_depends, array( 'ablocks-frontend-google-fonts', 'ablocks-animate-style', 'ablocks-common-style' ) ) ); | |
| 362 | + // NOTE: 'ablocks-animate-style' is intentionally NOT included here. Animation | |
| 363 | + // CSS is heavy and most blocks don't animate, so it's added on demand only: | |
| 364 | + // the combined generator adds it when a block's _animation attribute is set | |
| 365 | + // (AssetsGenerator::recursive_block_parser), and the fallback render path | |
| 366 | + // enqueues it per-block via maybe_enqueue_animate_style(). | |
| 367 | + return apply_filters( 'ablocks/block_style_depends', array_merge( $this->style_depends, array( 'ablocks-frontend-google-fonts', 'ablocks-common-style' ) ) ); | |
| 266 | 368 | } |
| 267 | 369 | public function get_script_depends() { |
| 268 | 370 | return apply_filters( 'ablocks/block_script_depends', array_merge( $this->script_depends, array( 'ablocks-common-script' ) ) ); |
| 269 | 371 | } |