PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.16.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.16.0
2.16.0 2.15.0 2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 All 83 releases
← All changes | includes/assets.php +991 -0 1.0.2 → 2.16.0 View file →
@@ -1,0 +1,991 @@
1 +<?php
2 +namespace ABlocks;
3 +
4 +if ( ! defined( 'ABSPATH' ) ) {
5 + exit;
6 +}
7 +
8 +use ABlocks\Classes\FileUpload;
9 +use ABlocks\Classes\AssetsGenerator;
10 +use ABlocks\Classes\RegisterScripts;
11 +use ABlocks\Classes\GlobalCssGenerator;
12 +use ABlocks\Classes\GlobalClasses;
13 +use ABlocks\Classes\AtomicStyles;
14 +use ABlocks\Classes\FontLoadLocally;
15 +use ABlocks\Admin\Menu;
16 +use ABlocks\Helper;
17 +
18 +class Assets {
19 +
20 + /** Records which plugin version last generated the page stylesheets. */
21 + const BUILD_OPTION = 'ablocks_assets_build';
22 +
23 + public $current_page_template_part = [];
24 + public $current_page_blocks = [];
25 + private $FileUpload;
26 + private $current_page_slug = '';
27 + private $theme_builder_locations = [];
28 + public static function init() {
29 + $self = new self();
30 + $self->FileUpload = new FileUpload();
31 + $self->current_page_slug = '';
32 + add_action( 'admin_enqueue_scripts', [ $self, 'dashboard_scripts' ], 10 );
33 + add_action( 'admin_enqueue_scripts', [ $self, 'demo_importer_scripts' ], 10 );
34 + add_action( 'enqueue_block_assets', [ $self, 'block_editor_assets' ] );
35 + add_action( 'enqueue_block_assets', [ $self, 'register_scripts' ] );
36 + // Frontend fonts are now emitted by core via CoreFontRegistry (theme.json
37 + // @font-face) with a Google Fonts fallback. See docs/FONT-MANAGEMENT-PLAN.md.
38 +
39 + add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_assets' ], 99 );
40 + // Global CSS
41 + add_action( 'wp_enqueue_block_assets', [ $self, 'global_css_variable' ] );
42 + add_action( 'wp_enqueue_scripts', [ $self, 'global_css_variable' ] );
43 + add_action( 'enqueue_block_editor_assets', [ $self, 'global_css_variable' ] );
44 + add_action( 'enqueue_block_editor_assets', [ $self, 'add_editor_inline_css' ] );
45 + add_action( 'enqueue_block_editor_assets', [ $self, 'editor_google_fonts' ] );
46 +
47 + // The localized ablocks_nonce is minted once per page load, so a tab left
48 + // open past the nonce lifetime (or across a re-login) 403s on every
49 + // aBlocks request. Renew it the way core renews wp_rest.
50 + add_filter( 'wp_refresh_nonces', [ $self, 'refresh_heartbeat_nonce' ] );
51 + add_action( 'wp_ajax_ablocks/refresh_nonce', [ $self, 'ajax_refresh_nonce' ] );
52 +
53 + // Detect page
54 + add_action( 'wp', array( $self, 'detect_page' ) );
55 +
56 + if ( ! is_admin() && Helper::is_enabled_assets_generation() ) {
57 + if ( Helper::is_fse_theme() ) {
58 + add_filter( 'pre_render_block', [ $self, 'set_current_page_template_part' ], 5, 2 );
59 + add_action( 'ablocks/before_enqueue_frontend_scripts', [ $self, 'regenerate_missing_assets' ] );
60 + } else {
61 + add_action( 'ablocks_theme_builder_after_dispatch', [ $self, 'set_theme_builder_locations' ] );
62 + add_action( 'wp', [ $self, 'regenerate_missing_assets' ], 20 );
63 + }
64 + }
65 +
66 + }
67 +
68 + public function detect_page() {
69 + if ( is_404() ) {
70 + $this->current_page_slug = '404';
71 + } elseif ( is_search() ) {
72 + $this->current_page_slug = 'search';
73 + } elseif ( is_home() ) {
74 + $posts_page_id = get_option( 'page_for_posts' );
75 + $this->current_page_slug = $posts_page_id ? (string) $posts_page_id : 'blog';
76 + } elseif ( is_post_type_archive() ) {
77 + $this->current_page_slug = 'archive-' . get_post_type();
78 + } elseif ( is_category() ) {
79 + $term = get_queried_object();
80 + $this->current_page_slug = 'category-' . $term->term_id; // or use slug
81 + } elseif ( is_tag() ) {
82 + $term = get_queried_object();
83 + $this->current_page_slug = 'tag-' . $term->term_id;
84 + } elseif ( is_tax() ) {
85 + $term = get_queried_object();
86 + $this->current_page_slug = 'tax-' . $term->taxonomy . '-' . $term->term_id;
87 + } elseif ( is_author() ) {
88 + $this->current_page_slug = 'author-' . get_the_author_meta( 'ID' );
89 + } elseif ( is_date() ) {
90 + $this->current_page_slug = 'date-archive';
91 + } elseif ( is_singular() ) {
92 + $this->current_page_slug = (string) get_the_ID(); // just the post/page/custom ID
93 + } else {
94 + $this->current_page_slug = (string) get_the_ID(); // fallback to ID
95 + }//end if
96 + }
97 +
98 + public function get_current_archive_post_type() {
99 + if ( is_post_type_archive() ) {
100 + $post_type = get_query_var( 'post_type' );
101 + if ( is_array( $post_type ) ) {
102 + $post_type = reset( $post_type );
103 + }
104 + return $post_type;
105 + }
106 + return null;
107 + }
108 +
109 + public function get_localize_script_data() {
110 + return [
111 + 'rest_url' => esc_url_raw( rest_url() ),
112 + 'namespace' => ABLOCKS_PLUGIN_SLUG . '/v1/',
113 + 'plugin_root_url' => ABLOCKS_ROOT_URL,
114 + 'plugin_root_path' => ABLOCKS_ROOT_DIR_PATH,
115 + 'admin_url' => admin_url(),
116 + 'site_url' => site_url(),
117 + 'route_path' => wp_parse_url( admin_url(), PHP_URL_PATH ),
118 + 'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
119 + 'is_pro' => (bool) Helper::is_active_ablocks_pro(),
120 + 'is_archive' => (bool) is_archive(),
121 + 'archive_post_type' => $this->get_current_archive_post_type(),
122 + // Responsive breakpoint widths (px) shared with the JS CSS generators
123 + // and the block editor so media queries match the frontend.
124 + 'breakpoints' => Helper::get_breakpoints(),
125 + // Ordered device list (Desktop + built-ins + custom breakpoints) for
126 + // the atomic block's responsive device switcher. Widest-first, so
127 + // the editor and the frontend agree on breakpoint precedence.
128 + 'responsive_devices' => Helper::get_responsive_devices(),
129 + // Whether breakpoint queries overlap (cascade) or are exclusive
130 + // bands (strict). The editor preview builds matching queries.
131 + 'breakpoint_mode' => Helper::get_breakpoint_mode(),
132 + ];
133 + }
134 +
135 + public function get_localize_dashboard_data() {
136 + // Anyone permitted to reach an aBlocks screen needs the nonce, or the
137 + // dashboard loads and then 403s on every request it makes.
138 + if ( ! current_user_can( Permissions::ACCESS ) ) {
139 + return $this->get_localize_script_data();
140 + }
141 + return array_merge($this->get_localize_script_data(), [
142 + 'nonce' => wp_create_nonce( 'wp_rest' ),
143 + 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
144 + 'permissions' => Permissions::for_user(),
145 + 'is_admin_user' => Permissions::is_real_admin(),
146 + ]);
147 + }
148 + public function get_localize_editor_data() {
149 + if ( ! current_user_can( 'edit_posts' ) ) {
150 + return $this->get_localize_script_data();
151 + }
152 + return array_merge($this->get_localize_script_data(), [
153 + 'nonce' => wp_create_nonce( 'wp_rest' ),
154 + 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
155 + ]);
156 + }
157 + public function get_localize_frontend_data() {
158 + $data = array_merge($this->get_localize_script_data(), [
159 + 'nonce' => wp_create_nonce( 'wp_rest' ),
160 + 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
161 + ]);
162 + // The absolute server path is only meaningful to admin-side code; on the
163 + // frontend it would publish the filesystem layout in page source. No
164 + // frontend script reads it (it is re-exported but never consumed).
165 + unset( $data['plugin_root_path'] );
166 + return $data;
167 + }
168 +
169 + /**
170 + * Whether the current user is issued ablocks_nonce in the editor or the
171 + * dashboard, and so may have it renewed. Mirrors the gates above.
172 + */
173 + private function can_renew_nonce() {
174 + return is_user_logged_in() && ( current_user_can( 'edit_posts' ) || current_user_can( Permissions::ACCESS ) );
175 + }
176 +
177 + /**
178 + * Heartbeat: hand an open page a fresh ablocks_nonce whenever core refreshes
179 + * its own nonces (the page's nonces are ageing, or the session changed).
180 + *
181 + * @param array $response The Heartbeat response.
182 + * @return array
183 + */
184 + public function refresh_heartbeat_nonce( $response ) {
185 + if ( $this->can_renew_nonce() ) {
186 + $response['ablocks_nonce'] = wp_create_nonce( 'ablocks_nonce' );
187 + }
188 + return $response;
189 + }
190 +
191 + /**
192 + * A fresh ablocks_nonce for a request that was rejected with a stale one —
193 + * the aBlocks counterpart of core's `rest-nonce` action. Logged-in only, and
194 + * only for users who would be given the nonce on page load anyway.
195 + */
196 + public function ajax_refresh_nonce() {
197 + if ( ! $this->can_renew_nonce() ) {
198 + wp_send_json_error( [ 'message' => 'forbidden' ], 403 );
199 + }
200 + wp_send_json_success( [ 'nonce' => wp_create_nonce( 'ablocks_nonce' ) ] );
201 + }
202 +
203 + /**
204 + * Whether the frontend ABlocksGlobal payload has been attached this request.
205 + *
206 + * @var bool
207 + */
208 + private static $globals_localized = false;
209 +
210 + /**
211 + * Attach the frontend ABlocksGlobal payload to the shared data-only
212 + * `ablocks-globals` handle, at most once per request.
213 + *
214 + * Previously each per-block script handle localized its own copy, so a page
215 + * using N block types printed N identical `var ABlocksGlobal = {...}` blocks
216 + * (and called wp_create_nonce() N times). Every aBlocks frontend script now
217 + * depends on `ablocks-globals`, so WP prints the object once, before any
218 + * consumer, in both asset-generation modes.
219 + */
220 + public static function localize_globals_once() {
221 + if ( self::$globals_localized ) {
222 + return;
223 + }
224 + self::$globals_localized = true;
225 + // Self-register so callers never depend on RegisterScripts having run
226 + // first — theme-builder templates and block render callbacks fire on
227 + // different hooks, and a missing handle would drop the payload silently.
228 + if ( ! wp_script_is( 'ablocks-globals', 'registered' ) ) {
229 + wp_register_script( 'ablocks-globals', false, array(), ABLOCKS_VERSION, array() );
230 + }
231 + $self = new self();
232 + wp_localize_script( 'ablocks-globals', 'ABlocksGlobal', $self->get_localize_frontend_data() );
233 + }
234 +
235 + public function get_dashboard_localize_script_data() {
236 + $menu = new Menu();
237 + $args = array(
238 + 'menu' => wp_json_encode( Helper::get_admin_menu_list() ),
239 + 'toplevel_menu_icon_url' => $menu->get_toplevel_menu_icon_url(),
240 + 'settings' => [
241 + 'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ),
242 + 'container_padding' => Helper::get_settings( 'container_padding', 10 ),
243 + 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ),
244 + 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ),
245 + 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ),
246 + 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ),
247 + 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
248 + 'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ),
249 + 'font_metric_fallback' => (bool) Helper::get_settings( 'font_metric_fallback', true ),
250 + // Global typography picks from the same list the editor offers.
251 + 'theme_fonts' => \ABlocks\Classes\FontStack::get_theme_font_families(),
252 + ],
253 + 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
254 + 'is_fse_theme' => Helper::is_fse_theme(),
255 + 'third_party_plugin_status' => [
256 + 'academy_lms' => Helper::is_active_academy(),
257 + 'storeengine' => Helper::is_active_storeengine(),
258 + 'wp_map_block' => Helper::is_active_wp_map_block(),
259 + 'easy_content_manager' => Helper::is_active_easy_content_manager(),
260 + 'zencommunity' => Helper::is_active_zencommunity(),
261 + 'gemboards' => Helper::is_active_gemboards(),
262 + 'quizpress' => Helper::is_active_quizpress(),
263 + ]
264 + );
265 + return apply_filters(
266 + 'ablocks/assets/dashboard_scripts_data',
267 + array_merge(
268 + $this->get_localize_dashboard_data(),
269 + $args
270 + )
271 + );
272 + }
273 + public function get_editor_localize_script_data() {
274 + global $ablocks_blocks;
275 + $post_types = Helper::get_public_post_type_options();
276 + $args = array(
277 + 'settings' => [
278 + 'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ),
279 + 'container_padding' => Helper::get_settings( 'container_padding', 10 ),
280 + 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ),
281 + 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ),
282 + 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ),
283 + 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ),
284 + 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
285 + 'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ),
286 + // Design system lock — the editor hides the custom pickers when these
287 + // are on, leaving only the global presets.
288 + 'lock_global_typography' => (bool) Helper::get_settings( 'lock_global_typography', false ),
289 + 'lock_global_typography_strict' => (bool) Helper::get_settings( 'lock_global_typography_strict', false ),
290 + 'lock_global_colors' => (bool) Helper::get_settings( 'lock_global_colors', false ),
291 + 'frontend_dashboard_page' => (int) Helper::get_settings( 'frontend_dashboard_page' ),
292 + 'global_color' => (array) Helper::get_settings( 'global_color', [] ),
293 + 'global_typography' => (array) Helper::get_settings( 'global_typography', [] ),
294 + 'global_typography_list' => wp_list_pluck( Helper::get_settings( 'global_typography', [] ), 'value', 'id' ),
295 + 'global_font_family_fallback' => (string) Helper::get_settings( 'global_font_family_fallback', 'Sans-serif' ),
296 + // Editor paste — see includes/api/paste-controller.php.
297 + 'paste_google_docs' => (bool) Helper::get_settings( 'paste_google_docs', true ),
298 + 'paste_convert_webp' => (bool) Helper::get_settings( 'paste_convert_webp', true ),
299 + // Theme.json + Font Library families, so uploaded/theme fonts are
300 + // selectable next to the Google catalog.
301 + 'theme_fonts' => \ABlocks\Classes\FontStack::get_theme_font_families(),
302 + ],
303 + 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
304 + 'has_required_block_attribute_migration' => get_option( 'ablocks_has_required_block_attribute_migration' ),
305 + 'third_party_plugin_status' => [
306 + 'academy_lms' => Helper::is_active_academy(),
307 + 'storeengine' => Helper::is_active_storeengine(),
308 + 'wp_map_block' => Helper::is_active_wp_map_block(),
309 + 'quizpress' => Helper::is_active_quizpress(),
310 + 'easy_content_manager' => Helper::is_active_easy_content_manager(),
311 + ],
312 + 'blocks_status' => $ablocks_blocks,
313 + 'post_types' => $post_types,
314 + // What this user may do inside the editor. Read by
315 + // src/utils/permissions.js to hide controls they cannot use.
316 + 'permissions' => Permissions::for_user(),
317 + 'is_admin_user' => Permissions::is_real_admin(),
318 + );
319 + return apply_filters(
320 + 'ablocks/assets/editor_scripts_data',
321 + array_merge(
322 + $this->get_localize_editor_data(),
323 + $args
324 + )
325 + );
326 + }
327 +
328 + public function dashboard_scripts( $hook ) {
329 + $demo_config = Helper::get_theme_demo_config();
330 + if ( strpos( $hook, '_page_' . ABLOCKS_PLUGIN_SLUG ) !== false && strpos( $hook, $demo_config['menu_slug'] ) === false ) {
331 + // Remove Notices
332 + remove_all_actions( 'admin_notices' );
333 + // dequeue third party plugin assets
334 + add_action(
335 + 'wp_print_scripts',
336 + function () {
337 + $isSkip = apply_filters( 'ablocks/skip_no_conflict_backend_scripts', Helper::is_dev_mode_enable() );
338 +
339 + if ( $isSkip ) {
340 + return;
341 + }
342 +
343 + global $wp_scripts;
344 + if ( ! $wp_scripts ) {
345 + return;
346 + }
347 +
348 + $pluginUrl = plugins_url();
349 + foreach ( $wp_scripts->queue as $script ) {
350 + $src = $wp_scripts->registered[ $script ]->src;
351 + if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) {
352 + wp_dequeue_script( $wp_scripts->registered[ $script ]->handle );
353 + }
354 + }
355 + },
356 + 1
357 + );
358 +
359 + wp_enqueue_style( 'ablocks-fonts', $this->web_fonts_url( 'Roboto:ital,wght@0,300;0,400;0,500;0,700;1,300&display=swap' ), array(), ABLOCKS_VERSION );
360 + wp_enqueue_style( 'ablocks-icon', ABLOCKS_ASSETS_URL . 'library/css/ablocks-icon/style.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'library/css/ablocks-icon/style.css' ), 'all' );
361 + wp_enqueue_style( 'ablocks-dashboard-style', ABLOCKS_ASSETS_URL . 'build/dashboard.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/dashboard.css' ), 'all' );
362 +
363 + $dependencies = include ABLOCKS_ASSETS_PATH . 'build/dashboard.asset.php';
364 + wp_enqueue_script(
365 + 'ablocks-dashboard-scripts',
366 + ABLOCKS_ASSETS_URL . 'build/dashboard.js',
367 + $dependencies['dependencies'],
368 + $dependencies['version'],
369 + true
370 + );
371 + wp_localize_script( 'ablocks-dashboard-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() );
372 + wp_set_script_translations( 'ablocks-dashboard-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
373 + }//end if
374 + }
375 +
376 + public function demo_importer_scripts( $hook ) {
377 + $demo_config = Helper::get_theme_demo_config();
378 + if ( strpos( $hook, $demo_config['menu_slug'] ) !== false ) {
379 + // Remove Notices
380 + remove_all_actions( 'admin_notices' );
381 + // dequeue third party plugin assets
382 + add_action(
383 + 'wp_print_scripts',
384 + function () {
385 + $isSkip = apply_filters( 'ablocks/skip_no_conflict_demo_importer_scripts', Helper::is_dev_mode_enable() );
386 +
387 + if ( $isSkip ) {
388 + return;
389 + }
390 +
391 + global $wp_scripts;
392 + if ( ! $wp_scripts ) {
393 + return;
394 + }
395 +
396 + $pluginUrl = plugins_url();
397 + foreach ( $wp_scripts->queue as $script ) {
398 + $src = $wp_scripts->registered[ $script ]->src;
399 + if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) {
400 + wp_dequeue_script( $wp_scripts->registered[ $script ]->handle );
401 + }
402 + }
403 + },
404 + 1
405 + );
406 +
407 + $this->demo_template_importer_scripts();
408 + }//end if
409 + }
410 +
411 + public function demo_template_importer_scripts() {
412 + wp_enqueue_style( 'ablocks-fonts', $this->web_fonts_url( 'Roboto:ital,wght@0,300;0,400;0,500;0,700;1,300&display=swap' ), array(), ABLOCKS_VERSION );
413 + wp_enqueue_style( 'ablocks-icon', ABLOCKS_ASSETS_URL . 'library/css/ablocks-icon/style.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'library/css/ablocks-icon/style.css' ), 'all' );
414 + wp_enqueue_style( 'ablocks-dashboard-style', ABLOCKS_ASSETS_URL . 'build/demo-import.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/demo-import.css' ), 'all' );
415 +
416 + $dependencies = include ABLOCKS_ASSETS_PATH . 'build/demo-import.asset.php';
417 + wp_enqueue_script(
418 + 'ablocks-demo-import-scripts',
419 + ABLOCKS_ASSETS_URL . 'build/demo-import.js',
420 + $dependencies['dependencies'],
421 + $dependencies['version'],
422 + true
423 + );
424 + wp_localize_script( 'ablocks-demo-import-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() );
425 + wp_set_script_translations( 'ablocks-demo-import-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
426 + }
427 +
428 + public function build_google_fonts_url( array $fonts ): string {
429 + $family_strings = [];
430 + foreach ( $fonts as $family => $weights ) {
431 + $family_encoded = str_replace( ' ', '+', $family );
432 + $weights = array_unique( array_map( 'strval', (array) $weights ) );
433 + sort( $weights );
434 + $family_strings[] = ! empty( $weights ) ? $family_encoded . ':wght@' . implode( ';', $weights ) : $family_encoded;
435 + }
436 + return '//fonts.googleapis.com/css2?family=' . implode( '&family=', $family_strings ) . '&display=swap';
437 + }
438 +
439 + public function web_fonts_url( $font ) {
440 + if ( 'off' === _x( 'on', 'Google font: on or off', 'ablocks' ) ) {
441 + return '';
442 + }
443 + return '//fonts.googleapis.com/css2?family=' . $font;
444 + }
445 +
446 +
447 +
448 + public function block_editor_assets() {
449 + if ( is_admin() ) {
450 + wp_enqueue_style( 'ablocks-editor-font-awesome', ABLOCKS_ASSETS_URL . 'library/font-awesome/css/all.min.css', array(), ABLOCKS_VERSION, 'all' );
451 + wp_enqueue_style( 'ablocks-editor-fonts', $this->web_fonts_url( 'Roboto:ital,wght@0,300;0,400;0,500;0,700;1,300&display=swap' ), array(), ABLOCKS_VERSION );
452 + wp_enqueue_style( 'ablocks-editor-icon', ABLOCKS_ASSETS_URL . 'library/css/ablocks-icon/style.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'library/css/ablocks-icon/style.css' ), 'all' );
453 + wp_enqueue_style( 'ablocks-editor-style', ABLOCKS_ASSETS_URL . 'build/blocks.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/blocks.css' ), 'all' );
454 +
455 + // js
456 + $dependencies = include ABLOCKS_ASSETS_PATH . 'build/blocks.asset.php';
457 + wp_enqueue_script(
458 + 'ablocks-editor-scripts',
459 + ABLOCKS_ASSETS_URL . 'build/blocks.js',
460 + $dependencies['dependencies'],
461 + $dependencies['version'],
462 + true
463 + );
464 + wp_localize_script( 'ablocks-editor-scripts', 'ABlocksGlobal', $this->get_editor_localize_script_data() );
465 + wp_set_script_translations( 'ablocks-editor-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
466 + }
467 + }
468 + public function enqueue_frontend_assets() {
469 + if ( ! Helper::is_enabled_assets_generation() ) {
470 + return;
471 + }
472 +
473 + do_action( 'ablocks/before_enqueue_frontend_scripts' );
474 +
475 + $script_loading_strategy = Helper::get_script_loading_strategy();
476 +
477 + if ( ! $this->is_assets_generated() ) {
478 + return;
479 + }
480 +
481 + if ( $this->current_page_slug ) {
482 + // Enqueue google fonts
483 + wp_enqueue_style( 'ablocks-frontend-google-fonts' );
484 +
485 + $css_path = $this->FileUpload->get_file_path( $this->current_page_slug . '.min.css' );
486 +
487 + // Performance Suite — inline the page CSS when it's small enough to
488 + // avoid a render-blocking request; fall back to a normal <link> above
489 + // the threshold. Opt-in via perf_inline_css.
490 + $inline_css = (bool) apply_filters(
491 + 'ablocks/perf/perf_inline_css',
492 + (bool) Helper::get_settings( 'perf_inline_css', true )
493 + );
494 + $inline_max = (int) apply_filters( 'ablocks/perf/inline_css_max_bytes', 50 * 1024 );
495 + $css_size = file_exists( $css_path ) ? (int) filesize( $css_path ) : 0;
496 +
497 + if ( $inline_css && $css_size > 0 && $css_size <= $inline_max ) {
498 + wp_register_style( 'ablocks-blocks-combine-style', false, array(), ABLOCKS_VERSION );
499 + wp_enqueue_style( 'ablocks-blocks-combine-style' );
500 + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
501 + wp_add_inline_style( 'ablocks-blocks-combine-style', (string) file_get_contents( $css_path ) );
502 + } else {
503 + wp_enqueue_style( 'ablocks-blocks-combine-style', $this->FileUpload->get_file_url( $this->current_page_slug . '.min.css' ), array(), filemtime( $css_path ), 'all' );
504 +
505 + // Performance Suite — when the page stylesheet is too large to inline,
506 + // load it non-blocking (media="print" + onload swap) so it no longer
507 + // delays first paint. Opt-in via perf_async_css (default off: a too-
508 + // aggressive critical set risks FOUC, so promote after QA). Editors
509 + // viewing the frontend are bypassed so previews render immediately.
510 + $async_css = (bool) apply_filters(
511 + 'ablocks/perf/perf_async_css',
512 + (bool) Helper::get_settings( 'perf_async_css', false )
513 + );
514 + // Critical CSS — inline the above-the-fold/structural subset in
515 + // <head> and load the full stylesheet non-blocking. This is a
516 + // stronger form of async (it prevents the FOUC async alone can
517 + // cause), so enabling it implies async delivery of the full file.
518 + $critical_css = (bool) apply_filters(
519 + 'ablocks/perf/perf_critical_css',
520 + (bool) Helper::get_settings( 'perf_critical_css', false )
521 + );
522 + $bypass_for_editor = is_user_logged_in() && current_user_can( 'edit_posts' )
523 + && (bool) apply_filters( 'ablocks/perf/bypass_optimizations_for_editors', true );
524 +
525 + if ( ( $async_css || $critical_css ) && ! $bypass_for_editor ) {
526 + // Before deferring the full stylesheet, always inline the
527 + // layout-critical CSS so the page paints with correct box
528 + // sizes and never reflows when the deferred file loads. The
529 + // split is property-based: the deferred remainder is cosmetic
530 + // only (colors, shadows, hover states), so it cannot cause a
531 + // layout shift (CLS). This makes async delivery safe.
532 + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
533 + list( $critical ) = \ABlocks\Classes\CriticalCss::split( (string) file_get_contents( $css_path ) );
534 + if ( '' !== trim( (string) $critical ) ) {
535 + wp_register_style( 'ablocks-critical-css', false, array(), ABLOCKS_VERSION );
536 + wp_enqueue_style( 'ablocks-critical-css' );
537 + wp_add_inline_style( 'ablocks-critical-css', $critical );
538 + }
539 + add_filter( 'style_loader_tag', [ $this, 'async_combined_style_tag' ], 20, 2 );
540 + }
541 + }
542 +
543 + wp_enqueue_script( 'ablocks-blocks-combine-script', $this->FileUpload->get_file_url( $this->current_page_slug . '.min.js' ), array( 'ablocks-globals' ), filemtime( $this->FileUpload->get_file_path( $this->current_page_slug . '.min.js' ) ), true, [ 'strategy' => $script_loading_strategy ] );
544 + self::localize_globals_once();
545 + wp_set_script_translations( 'ablocks-blocks-combine-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
546 + } else {
547 + // fallback if assets not available
548 + add_filter( 'ablocks/is_allow_block_inline_assets', '__return_true' );
549 + }
550 + }
551 +
552 + /**
553 + * Rewrite the combined page stylesheet <link> so it loads without blocking
554 + * render: fetch as media="print", then flip to media="all" on load, with a
555 + * <noscript> fallback for JS-disabled clients. Only touches the aBlocks
556 + * combined handle — third-party stylesheets are left untouched.
557 + */
558 + public function async_combined_style_tag( $tag, $handle ) {
559 + if ( 'ablocks-blocks-combine-style' !== $handle ) {
560 + return $tag;
561 + }
562 + $async_tag = preg_replace(
563 + [ "/media=(['\"])[^'\"]*\\1/", '/>$/', '/\/>$/' ],
564 + [ 'media="print" onload="this.media=\'all\'"', '>', '/>' ],
565 + trim( $tag )
566 + );
567 + // Guarantee the print/onload attributes even if the original tag had no media.
568 + if ( strpos( $async_tag, 'onload=' ) === false ) {
569 + $async_tag = str_replace( '<link ', '<link media="print" onload="this.media=\'all\'" ', $async_tag );
570 + }
571 + $noscript = '<noscript>' . $tag . '</noscript>';
572 + return $async_tag . "\n" . $noscript;
573 + }
574 +
575 + public function regenerate_missing_assets() {
576 + if ( $this->is_assets_generated() ) {
577 + return;
578 + }
579 +
580 + // classic
581 + if ( ! Helper::is_fse_theme() ) {
582 + $is_parse_main_content = false;
583 + if ( is_array( $this->theme_builder_locations ) ) {
584 + foreach ( $this->theme_builder_locations as $location_name => $location_id ) {
585 + $post = get_post( $location_id );
586 + if ( is_object( $post ) ) {
587 + $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) );
588 + }
589 + if ( $location_name === 'header' ) {
590 + $post = get_post( get_the_ID() );
591 + if ( is_object( $post ) ) {
592 + $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) );
593 + }
594 + $is_parse_main_content = true;
595 + }
596 + }
597 + }
598 + if ( false === $is_parse_main_content ) {
599 + $post = get_post( get_the_ID() );
600 + $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) );
601 + }
602 + }//end if
603 +
604 + if ( count( $this->current_page_blocks ) ) {
605 + $file_name = $this->current_page_slug;
606 + AssetsGenerator::write_frontend_css_in_uploads_folder( $file_name, $this->current_page_blocks );
607 + }
608 + }
609 + public function register_scripts() {
610 + $register_styles = RegisterScripts::get_register_styles();
611 + foreach ( $register_styles as $register_handler => $register_style ) {
612 + wp_register_style( $register_handler, $register_style['url'], $register_style['deps'], ABLOCKS_VERSION, $register_style['media'] );
613 + }
614 + $register_scripts = RegisterScripts::get_register_scripts();
615 + foreach ( $register_scripts as $register_handler => $register_script ) {
616 + if ( isset( $register_script['dependencies'] ) ) {
617 + $dependencies = include $register_script['dependencies'];
618 + // Merge rather than overwrite: the generated asset.php only knows
619 + // about build-time (@wordpress/*) dependencies, so replacing the
620 + // declared list would silently drop hand-declared handles such as
621 + // ablocks-globals.
622 + $register_script['deps'] = array_values(
623 + array_unique(
624 + array_merge(
625 + isset( $register_script['deps'] ) ? (array) $register_script['deps'] : array(),
626 + $dependencies['dependencies']
627 + )
628 + )
629 + );
630 + $register_script['ver'] = $dependencies['version'];
631 + }
632 + wp_register_script(
633 + $register_handler,
634 + $register_script['url'],
635 + $register_script['deps'],
636 + $register_script['ver'],
637 + $register_script['args']
638 + );
639 + }//end foreach
640 + }
641 + public function global_css_variable() {
642 + wp_register_style( 'ablocks-editor-global-styles', false, array(), ABLOCKS_VERSION );
643 + wp_enqueue_style( 'ablocks-editor-global-styles' );
644 +
645 + // The :root variables only change when global settings change, so cache
646 + // the generated string and rebuild it on settings save (see Blocks
647 + // ::clear_global_css_cache) instead of looping settings on every request.
648 + $css = get_transient( 'ablocks_global_css_vars' );
649 + if ( false === $css ) {
650 + $container_padding = Helper::get_settings( 'container_padding', 10 ) . 'px';
651 + $css = ":root, body .editor-styles-wrapper {\n";
652 + $css .= " --ablocks-container-padding: $container_padding;\n";
653 +
654 + // Colors
655 + $global_color = (array) Helper::get_settings( 'global_color', [] );
656 + foreach ( $global_color as $color ) {
657 + if ( isset( $color->id, $color->value ) ) {
658 + $css .= " --ablocks-{$color->id}: {$color->value};\n";
659 + }
660 + }
661 +
662 + // Typography
663 + $global_typography = (array) Helper::get_settings( 'global_typography', [] );
664 + foreach ( $global_typography as $typography ) {
665 + if ( isset( $typography->id, $typography->value ) ) {
666 + $id = $typography->id;
667 + $value = $typography->value;
668 +
669 + // Desktop values
670 + if ( ! empty( $value->fontFamily ) ) {
671 + // Every consumer of this variable drops it straight into
672 + // font-family, so it has to carry the whole stack.
673 + $font_stack = \ABlocks\Classes\FontStack::build(
674 + $value->fontFamily,
675 + isset( $value->fontFallback ) ? $value->fontFallback : ''
676 + );
677 + if ( '' !== $font_stack ) {
678 + $css .= " --ablocks-{$id}-font-family: {$font_stack};\n";
679 + }
680 + }
681 + if ( ! empty( $value->weight ) ) {
682 + $css .= " --ablocks-{$id}-weight: {$value->weight};\n";
683 + }
684 + if ( ! empty( $value->transform ) ) {
685 + $css .= " --ablocks-{$id}-transform: {$value->transform};\n";
686 + }
687 + if ( ! empty( $value->style ) ) {
688 + $css .= " --ablocks-{$id}-style: {$value->style};\n";
689 + }
690 + if ( ! empty( $value->decoration ) ) {
691 + $css .= " --ablocks-{$id}-decoration: {$value->decoration};\n";
692 + }
693 + if ( ! empty( $value->fontSize ) ) {
694 + $unit = ! empty( $value->fontSizeUnit ) ? $value->fontSizeUnit : 'px';
695 + $css .= " --ablocks-{$id}-font-size: {$value->fontSize}{$unit};\n";
696 + }
697 + if ( ! empty( $value->lineHeight ) ) {
698 + $unit = ! empty( $value->lineHeightUnit ) ? $value->lineHeightUnit : 'px';
699 + $css .= " --ablocks-{$id}-line-height: {$value->lineHeight}{$unit};\n";
700 + }
701 + if ( ! empty( $value->letterSpacing ) ) {
702 + $unit = ! empty( $value->letterSpacingUnit ) ? $value->letterSpacingUnit : 'px';
703 + $css .= " --ablocks-{$id}-letter-spacing: {$value->letterSpacing}{$unit};\n";
704 + }
705 + if ( ! empty( $value->wordSpacing ) ) {
706 + $unit = ! empty( $value->wordSpacingUnit ) ? $value->wordSpacingUnit : 'px';
707 + $css .= " --ablocks-{$id}-word-spacing: {$value->wordSpacing}{$unit};\n";
708 + }
709 +
710 + // Tablet values
711 + if ( ! empty( $value->fontSizeTablet ) ) {
712 + $unit = ! empty( $value->fontSizeUnitTablet ) ? $value->fontSizeUnitTablet : 'px';
713 + $css .= " --ablocks-{$id}-font-size-tablet: {$value->fontSizeTablet}{$unit};\n";
714 + }
715 + if ( ! empty( $value->lineHeightTablet ) ) {
716 + $unit = ! empty( $value->lineHeightUnitTablet ) ? $value->lineHeightUnitTablet : 'px';
717 + $css .= " --ablocks-{$id}-line-height-tablet: {$value->lineHeightTablet}{$unit};\n";
718 + }
719 + if ( ! empty( $value->letterSpacingTablet ) ) {
720 + $unit = ! empty( $value->letterSpacingUnitTablet ) ? $value->letterSpacingUnitTablet : 'px';
721 + $css .= " --ablocks-{$id}-letter-spacing-tablet: {$value->letterSpacingTablet}{$unit};\n";
722 + }
723 + if ( ! empty( $value->wordSpacingTablet ) ) {
724 + $unit = ! empty( $value->wordSpacingUnitTablet ) ? $value->wordSpacingUnitTablet : 'px';
725 + $css .= " --ablocks-{$id}-word-spacing-tablet: {$value->wordSpacingTablet}{$unit};\n";
726 + }
727 +
728 + // Mobile values
729 + if ( ! empty( $value->fontSizeMobile ) ) {
730 + $unit = ! empty( $value->fontSizeUnitMobile ) ? $value->fontSizeUnitMobile : 'px';
731 + $css .= " --ablocks-{$id}-font-size-mobile: {$value->fontSizeMobile}{$unit};\n";
732 + }
733 + if ( ! empty( $value->lineHeightMobile ) ) {
734 + $unit = ! empty( $value->lineHeightUnitMobile ) ? $value->lineHeightUnitMobile : 'px';
735 + $css .= " --ablocks-{$id}-line-height-mobile: {$value->lineHeightMobile}{$unit};\n";
736 + }
737 + if ( ! empty( $value->letterSpacingMobile ) ) {
738 + $unit = ! empty( $value->letterSpacingUnitMobile ) ? $value->letterSpacingUnitMobile : 'px';
739 + $css .= " --ablocks-{$id}-letter-spacing-mobile: {$value->letterSpacingMobile}{$unit};\n";
740 + }
741 + if ( ! empty( $value->wordSpacingMobile ) ) {
742 + $unit = ! empty( $value->wordSpacingUnitMobile ) ? $value->wordSpacingUnitMobile : 'px';
743 + $css .= " --ablocks-{$id}-word-spacing-mobile: {$value->wordSpacingMobile}{$unit};\n";
744 + }
745 + }//end if
746 + }//end foreach
747 +
748 + $css .= "}\n";
749 + set_transient( 'ablocks_global_css_vars', $css, DAY_IN_SECONDS );
750 + }
751 +
752 + if ( ! is_admin() && ! Helper::is_gutenberg_editor() ) {
753 + $css .= $this->get_common_css();
754 + }
755 + wp_add_inline_style( 'ablocks-editor-global-styles', $css );
756 + wp_add_inline_style( 'ablocks-common-style', $css );
757 + }
758 +
759 + public function editor_google_fonts() {
760 + // Load the fonts already used by the post being edited (plus globals) so
761 + // existing content previews correctly on editor load. Live selections are
762 + // handled client-side by the typography control.
763 + $fonts = \ABlocks\Classes\FontCollector::get_global_fonts();
764 +
765 + $post_id = 0;
766 + if ( function_exists( 'get_the_ID' ) && get_the_ID() ) {
767 + $post_id = (int) get_the_ID();
768 + } elseif ( isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
769 + $post_id = absint( $_GET['post'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
770 + }
771 + if ( $post_id ) {
772 + $fonts = \ABlocks\Classes\FontCollector::merge( $fonts, \ABlocks\Classes\FontCollector::get_post_fonts( $post_id ) );
773 + }
774 +
775 + if ( empty( $fonts ) ) {
776 + return;
777 + }
778 + $url = $this->build_google_fonts_url( $fonts );
779 + wp_enqueue_style( 'ablocks-editor-google-fonts', $url, array(), null );
780 + }
781 +
782 + public function add_editor_inline_css() {
783 + $custom_css = $this->get_common_css( '.editor-styles-wrapper ' );
784 + add_theme_support( 'editor-styles' );
785 + add_editor_style();
786 + wp_add_inline_style( 'wp-block-library', $custom_css );
787 + }
788 +
789 + public function get_common_css( $parent_selector = '' ) {
790 + global $ablocks_settings;
791 + $attributes = [
792 + 'global_body_text_color' => $ablocks_settings->global_body_text_color,
793 + 'global_body_typography' => $ablocks_settings->global_body_typography,
794 + 'global_body_paragraph_space' => $ablocks_settings->global_body_paragraph_space,
795 + 'global_link_color' => $ablocks_settings->global_link_color,
796 + 'global_link_hover_color' => $ablocks_settings->global_link_hover_color,
797 + 'global_link_typography' => $ablocks_settings->global_link_typography,
798 + 'global_link_hover_typography' => $ablocks_settings->global_link_hover_typography,
799 + 'global_h1_color' => $ablocks_settings->global_h1_color,
800 + 'global_h1_typography' => $ablocks_settings->global_h1_typography,
801 + 'global_h2_color' => $ablocks_settings->global_h2_color,
802 + 'global_h2_typography' => $ablocks_settings->global_h2_typography,
803 + 'global_h3_color' => $ablocks_settings->global_h3_color,
804 + 'global_h3_typography' => $ablocks_settings->global_h3_typography,
805 + 'global_h4_color' => $ablocks_settings->global_h4_color,
806 + 'global_h4_typography' => $ablocks_settings->global_h4_typography,
807 + 'global_h5_color' => $ablocks_settings->global_h5_color,
808 + 'global_h5_typography' => $ablocks_settings->global_h5_typography,
809 + 'global_h6_color' => $ablocks_settings->global_h6_color,
810 + 'global_h6_typography' => $ablocks_settings->global_h6_typography,
811 + ];
812 + $css_generator = new GlobalCssGenerator( $attributes );
813 +
814 + $css_generator->add_class_styles(
815 + $parent_selector . 'body',
816 + $css_generator->get_body_css( $attributes ),
817 + $css_generator->get_body_css( $attributes, 'Tablet' ),
818 + $css_generator->get_body_css( $attributes, 'Mobile' )
819 + );
820 + $css_generator->add_class_styles(
821 + $parent_selector . 'p',
822 + $css_generator->get_body_paragraph_css( $attributes ),
823 + $css_generator->get_body_paragraph_css( $attributes, 'Tablet' ),
824 + $css_generator->get_body_paragraph_css( $attributes, 'Mobile' )
825 + );
826 + $css_generator->add_class_styles(
827 + $parent_selector . 'a',
828 + $css_generator->get_body_anchor_css( $attributes ),
829 + $css_generator->get_body_anchor_css( $attributes, 'Tablet' ),
830 + $css_generator->get_body_anchor_css( $attributes, 'Mobile' )
831 + );
832 + $css_generator->add_class_styles(
833 + $parent_selector . 'a:hover',
834 + $css_generator->get_body_anchor_hover_css( $attributes ),
835 + $css_generator->get_body_anchor_hover_css( $attributes, 'Tablet' ),
836 + $css_generator->get_body_anchor_hover_css( $attributes, 'Mobile' )
837 + );
838 + $css_generator->add_class_styles(
839 + $parent_selector . 'h1',
840 + $css_generator->get_global_h1_css( $attributes ),
841 + $css_generator->get_global_h1_css( $attributes, 'Tablet' ),
842 + $css_generator->get_global_h1_css( $attributes, 'Mobile' )
843 + );
844 + $css_generator->add_class_styles(
845 + $parent_selector . 'h2',
846 + $css_generator->get_global_h2_css( $attributes ),
847 + $css_generator->get_global_h2_css( $attributes, 'Tablet' ),
848 + $css_generator->get_global_h2_css( $attributes, 'Mobile' )
849 + );
850 + $css_generator->add_class_styles(
851 + $parent_selector . 'h3',
852 + $css_generator->get_global_h3_css( $attributes ),
853 + $css_generator->get_global_h3_css( $attributes, 'Tablet' ),
854 + $css_generator->get_global_h3_css( $attributes, 'Mobile' )
855 + );
856 + $css_generator->add_class_styles(
857 + $parent_selector . 'h4',
858 + $css_generator->get_global_h4_css( $attributes ),
859 + $css_generator->get_global_h4_css( $attributes, 'Tablet' ),
860 + $css_generator->get_global_h4_css( $attributes, 'Mobile' )
861 + );
862 + $css_generator->add_class_styles(
863 + $parent_selector . 'h5',
864 + $css_generator->get_global_h5_css( $attributes ),
865 + $css_generator->get_global_h5_css( $attributes, 'Tablet' ),
866 + $css_generator->get_global_h5_css( $attributes, 'Mobile' )
867 + );
868 + $css_generator->add_class_styles(
869 + $parent_selector . 'h6',
870 + $css_generator->get_global_h6_css( $attributes ),
871 + $css_generator->get_global_h6_css( $attributes, 'Tablet' ),
872 + $css_generator->get_global_h6_css( $attributes, 'Mobile' )
873 + );
874 + return $css_generator->generate_css();
875 + }
876 +
877 +
878 + public function is_assets_generated() {
879 + $file_name = $this->current_page_slug;
880 + $css_file_path = $this->FileUpload->get_file_path( $file_name . '.min.css' );
881 +
882 + if ( ! file_exists( $css_file_path ) ) {
883 + return false;
884 + }
885 +
886 + // The file embeds the global-class CSS this page needs, so editing a
887 + // class has to make every generated stylesheet stale. Comparing the
888 + // file's mtime against the library's change stamp rebuilds them lazily,
889 + // one page at a time on next visit, without stamping a revision into
890 + // every file name (which the per-post delete in Blocks relies on).
891 + //
892 + // Strictly greater, not >=: mtime has one-second resolution, so a file
893 + // written in the same second as a class edit is indistinguishable from
894 + // one written just after it. Treating that tie as stale costs one extra
895 + // regeneration; treating it as fresh would serve the old CSS until the
896 + // next edit.
897 + return filemtime( $css_file_path ) > max(
898 + GlobalClasses::get_revision(),
899 + self::build_revision()
900 + );
901 + }
902 +
903 + /**
904 + * When this plugin's own CSS last changed, as a timestamp.
905 + *
906 + * The generated file bakes in each block's static stylesheet (see
907 + * AssetsGenerator, which concatenates get_static_css() into it), so a CSS
908 + * fix shipped in an update reached nobody whose pages were generated before
909 + * it: the file still existed, still parsed, and nothing about it looked
910 + * stale, so it was served unchanged forever. Verified by planting a
911 + * stylesheet carrying the previous release's rules — it survived every
912 + * subsequent page load untouched. That is why a fix could land in the
913 + * editor, which loads each block's style.css directly, and never appear on
914 + * the front end.
915 + *
916 + * Keyed on the version rather than a file scan: it is one option read on
917 + * the common path, and every shipped CSS change comes with a version bump.
918 + * Each page then rebuilds once, on its next visit, exactly the way a
919 + * global-class edit already makes them rebuild.
920 + *
921 + * @return int Timestamp of the running version's first request.
922 + */
923 + public static function build_revision() {
924 + $stamp = get_option( self::BUILD_OPTION );
925 +
926 + // The compiler's output revision is part of the key, so an emission
927 + // change stales every baked page even without a version bump. See
928 + // AtomicStyles::OUTPUT_REVISION.
929 + $build = ABLOCKS_VERSION . '+' . AtomicStyles::OUTPUT_REVISION;
930 +
931 + if (
932 + is_array( $stamp ) &&
933 + isset( $stamp['version'], $stamp['time'] ) &&
934 + $build === $stamp['version']
935 + ) {
936 + return (int) $stamp['time'];
937 + }
938 +
939 + $now = time();
940 + update_option(
941 + self::BUILD_OPTION,
942 + [
943 + 'version' => $build,
944 + 'time' => $now,
945 + ],
946 + true
947 + );
948 +
949 + return $now;
950 + }
951 +
952 + public function set_current_page_template_part( $content, $block ) {
953 + if ( ! isset( $block['blockName'] ) && is_array( $block ) ) {
954 + foreach ( $block as $block_item ) {
955 + if ( $this->is_page_asset_block( $block_item ) ) {
956 + $this->current_page_blocks[] = $block_item;
957 + }
958 + }
959 + }
960 + if ( $this->is_page_asset_block( $block ) ) {
961 + $this->current_page_blocks[] = $block;
962 + }
963 + return $content;
964 + }
965 +
966 + /**
967 + * Whether a top-level block contributes to the page's generated assets.
968 + *
969 + * A synced pattern counts too: AssetsGenerator::recursive_block_parser()
970 + * already expands its reference. Collecting only `ablocks/*` names left a
971 + * page whose content is just a pattern with no combined CSS/JS at all — so a
972 + * Loop Filter placed from a pattern rendered unstyled and did nothing when
973 + * clicked.
974 + *
975 + * @param mixed $block Parsed block.
976 + * @return bool
977 + */
978 + private function is_page_asset_block( $block ) {
979 + if ( ! is_array( $block ) || empty( $block['blockName'] ) ) {
980 + return false;
981 + }
982 + if ( 'core/block' === $block['blockName'] ) {
983 + return ! empty( $block['attrs']['ref'] );
984 + }
985 + return strpos( $block['blockName'], 'ablocks/' ) !== false;
986 + }
987 +
988 + public function set_theme_builder_locations( $args ) {
989 + $this->theme_builder_locations = $args;
990 + }
991 +}