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 +366 -42 2.8.0 → 2.16.0 View file →
@@ -8,13 +8,19 @@
8 8 use ABlocks\Classes\FileUpload;
9 9 use ABlocks\Classes\AssetsGenerator;
10 10 use ABlocks\Classes\RegisterScripts;
11 11 use ABlocks\Classes\GlobalCssGenerator;
12 +use ABlocks\Classes\GlobalClasses;
13 +use ABlocks\Classes\AtomicStyles;
12 14 use ABlocks\Classes\FontLoadLocally;
13 15 use ABlocks\Admin\Menu;
14 16 use ABlocks\Helper;
15 17
16 18 class Assets {
19 +
20 + /** Records which plugin version last generated the page stylesheets. */
21 + const BUILD_OPTION = 'ablocks_assets_build';
22 +
17 23 public $current_page_template_part = [];
18 24 public $current_page_blocks = [];
19 25 private $FileUpload;
20 26 private $current_page_slug = '';
@@ -26,9 +32,10 @@
26 32 add_action( 'admin_enqueue_scripts', [ $self, 'dashboard_scripts' ], 10 );
27 33 add_action( 'admin_enqueue_scripts', [ $self, 'demo_importer_scripts' ], 10 );
28 34 add_action( 'enqueue_block_assets', [ $self, 'block_editor_assets' ] );
29 35 add_action( 'enqueue_block_assets', [ $self, 'register_scripts' ] );
30 - add_action( 'wp_enqueue_scripts', [ $self, 'front_end_google_fonts' ], 999 );
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.
31 38
32 39 add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_assets' ], 99 );
33 40 // Global CSS
34 41 add_action( 'wp_enqueue_block_assets', [ $self, 'global_css_variable' ] );
@@ -34,9 +41,16 @@
34 41 add_action( 'wp_enqueue_block_assets', [ $self, 'global_css_variable' ] );
35 42 add_action( 'wp_enqueue_scripts', [ $self, 'global_css_variable' ] );
36 43 add_action( 'enqueue_block_editor_assets', [ $self, 'global_css_variable' ] );
37 44 add_action( 'enqueue_block_editor_assets', [ $self, 'add_editor_inline_css' ] );
45 + add_action( 'enqueue_block_editor_assets', [ $self, 'editor_google_fonts' ] );
38 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 +
39 53 // Detect page
40 54 add_action( 'wp', array( $self, 'detect_page' ) );
41 55
42 56 if ( ! is_admin() && Helper::is_enabled_assets_generation() ) {
@@ -104,18 +118,32 @@
104 118 'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
105 119 'is_pro' => (bool) Helper::is_active_ablocks_pro(),
106 120 'is_archive' => (bool) is_archive(),
107 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(),
108 132 ];
109 133 }
110 134
111 135 public function get_localize_dashboard_data() {
112 - if ( ! current_user_can( 'manage_options' ) ) {
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 ) ) {
113 139 return $this->get_localize_script_data();
114 140 }
115 141 return array_merge($this->get_localize_script_data(), [
116 142 'nonce' => wp_create_nonce( 'wp_rest' ),
117 143 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
144 + 'permissions' => Permissions::for_user(),
145 + 'is_admin_user' => Permissions::is_real_admin(),
118 146 ]);
119 147 }
120 148 public function get_localize_editor_data() {
121 149 if ( ! current_user_can( 'edit_posts' ) ) {
@@ -126,14 +154,85 @@
126 154 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
127 155 ]);
128 156 }
129 157 public function get_localize_frontend_data() {
130 - return array_merge($this->get_localize_script_data(), [
158 + $data = array_merge($this->get_localize_script_data(), [
131 159 'nonce' => wp_create_nonce( 'wp_rest' ),
132 160 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
133 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;
134 167 }
135 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 +
136 235 public function get_dashboard_localize_script_data() {
137 236 $menu = new Menu();
138 237 $args = array(
139 238 'menu' => wp_json_encode( Helper::get_admin_menu_list() ),
@@ -146,8 +245,11 @@
146 245 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ),
147 246 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ),
148 247 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
149 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(),
150 252 ],
151 253 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
152 254 'is_fse_theme' => Helper::is_fse_theme(),
153 255 'third_party_plugin_status' => [
@@ -154,8 +256,11 @@
154 256 'academy_lms' => Helper::is_active_academy(),
155 257 'storeengine' => Helper::is_active_storeengine(),
156 258 'wp_map_block' => Helper::is_active_wp_map_block(),
157 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(),
158 263 ]
159 264 );
160 265 return apply_filters(
161 266 'ablocks/assets/dashboard_scripts_data',
@@ -177,13 +282,24 @@
177 282 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ),
178 283 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ),
179 284 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
180 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 ),
181 291 'frontend_dashboard_page' => (int) Helper::get_settings( 'frontend_dashboard_page' ),
182 292 'global_color' => (array) Helper::get_settings( 'global_color', [] ),
183 293 'global_typography' => (array) Helper::get_settings( 'global_typography', [] ),
184 294 'global_typography_list' => wp_list_pluck( Helper::get_settings( 'global_typography', [] ), 'value', 'id' ),
185 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(),
186 302 ],
187 303 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
188 304 'has_required_block_attribute_migration' => get_option( 'ablocks_has_required_block_attribute_migration' ),
189 305 'third_party_plugin_status' => [
@@ -190,11 +306,16 @@
190 306 'academy_lms' => Helper::is_active_academy(),
191 307 'storeengine' => Helper::is_active_storeengine(),
192 308 'wp_map_block' => Helper::is_active_wp_map_block(),
193 309 'quizpress' => Helper::is_active_quizpress(),
310 + 'easy_content_manager' => Helper::is_active_easy_content_manager(),
194 311 ],
195 312 'blocks_status' => $ablocks_blocks,
196 - 'post_types' => $post_types
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(),
197 318 );
198 319 return apply_filters(
199 320 'ablocks/assets/editor_scripts_data',
200 321 array_merge(
@@ -303,44 +424,28 @@
303 424 wp_localize_script( 'ablocks-demo-import-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() );
304 425 wp_set_script_translations( 'ablocks-demo-import-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
305 426 }
306 427
307 - public function web_fonts_url( $font ) {
308 - $font_url = '';
309 - if ( 'off' !== _x( 'on', 'Google font: on or off', 'ablocks' ) ) {
310 - $font_url = add_query_arg( 'family', rawurlencode( $font ), '//fonts.googleapis.com/css' );
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;
311 435 }
312 - return $font_url;
436 + return '//fonts.googleapis.com/css2?family=' . implode( '&family=', $family_strings ) . '&display=swap';
313 437 }
314 438
315 - public function front_end_google_fonts() {
316 - global $ablocks_fonts;
317 - if ( empty( $ablocks_fonts ) || ! is_array( $ablocks_fonts ) ) {
318 - return false;
439 + public function web_fonts_url( $font ) {
440 + if ( 'off' === _x( 'on', 'Google font: on or off', 'ablocks' ) ) {
441 + return '';
319 442 }
443 + return '//fonts.googleapis.com/css2?family=' . $font;
444 + }
320 445
321 - if ( Helper::get_settings( 'enabled_load_google_font_locally', false ) ) {
322 - $FontLoadLocally = new FontLoadLocally();
323 - $FontLoadLocally->enqueue_fonts( $ablocks_fonts ); // Attempt to load local fonts (fallback inside method)
324 - } else {
325 - $font_families = [];
326 - foreach ( $ablocks_fonts as $family => $weights ) {
327 - $font_family_string = $family;
328 - $total_weights = count( $weights );
329 446
330 - if ( $total_weights > 0 ) {
331 - $font_family_string .= ':wght@' . implode( ';', $weights );
332 - }
333 447
334 - $font_families[] = $font_family_string;
335 - }
336 - // Generate the URL using the web_fonts_url method
337 - $google_fonts_url = $this->web_fonts_url( implode( '|', $font_families ) ) . '&display=swap';
338 - wp_register_style( 'ablocks-frontend-google-fonts', esc_url( $google_fonts_url ), array(), ABLOCKS_VERSION );
339 - }
340 - }
341 -
342 -
343 448 public function block_editor_assets() {
344 449 if ( is_admin() ) {
345 450 wp_enqueue_style( 'ablocks-editor-font-awesome', ABLOCKS_ASSETS_URL . 'library/font-awesome/css/all.min.css', array(), ABLOCKS_VERSION, 'all' );
346 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 );
@@ -375,12 +480,69 @@
375 480
376 481 if ( $this->current_page_slug ) {
377 482 // Enqueue google fonts
378 483 wp_enqueue_style( 'ablocks-frontend-google-fonts' );
379 - wp_enqueue_style( 'ablocks-blocks-combine-style', $this->FileUpload->get_file_url( $this->current_page_slug . '.min.css' ), array(), filemtime( $this->FileUpload->get_file_path( $this->current_page_slug . '.min.css' ) ), 'all' );
380 484
381 - wp_enqueue_script( 'ablocks-blocks-combine-script', $this->FileUpload->get_file_url( $this->current_page_slug . '.min.js' ), array(), filemtime( $this->FileUpload->get_file_path( $this->current_page_slug . '.min.js' ) ), true, [ 'strategy' => $script_loading_strategy ] );
382 - wp_localize_script( 'ablocks-blocks-combine-script', 'ABlocksGlobal', $this->get_localize_frontend_data() );
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();
383 545 wp_set_script_translations( 'ablocks-blocks-combine-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
384 546 } else {
385 547 // fallback if assets not available
386 548 add_filter( 'ablocks/is_allow_block_inline_assets', '__return_true' );
@@ -386,8 +548,31 @@
386 548 add_filter( 'ablocks/is_allow_block_inline_assets', '__return_true' );
387 549 }
388 550 }
389 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 +
390 575 public function regenerate_missing_assets() {
391 576 if ( $this->is_assets_generated() ) {
392 577 return;
393 578 }
@@ -429,9 +614,20 @@
429 614 $register_scripts = RegisterScripts::get_register_scripts();
430 615 foreach ( $register_scripts as $register_handler => $register_script ) {
431 616 if ( isset( $register_script['dependencies'] ) ) {
432 617 $dependencies = include $register_script['dependencies'];
433 - $register_script['deps'] = $dependencies['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 + );
434 630 $register_script['ver'] = $dependencies['version'];
435 631 }
436 632 wp_register_script(
437 633 $register_handler,
@@ -439,14 +635,19 @@
439 635 $register_script['deps'],
440 636 $register_script['ver'],
441 637 $register_script['args']
442 638 );
443 - }
639 + }//end foreach
444 640 }
445 641 public function global_css_variable() {
446 642 wp_register_style( 'ablocks-editor-global-styles', false, array(), ABLOCKS_VERSION );
447 643 wp_enqueue_style( 'ablocks-editor-global-styles' );
448 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 ) {
449 650 $container_padding = Helper::get_settings( 'container_padding', 10 ) . 'px';
450 651 $css = ":root, body .editor-styles-wrapper {\n";
451 652 $css .= " --ablocks-container-padding: $container_padding;\n";
452 653
@@ -466,9 +667,17 @@
466 667 $value = $typography->value;
467 668
468 669 // Desktop values
469 670 if ( ! empty( $value->fontFamily ) ) {
470 - $css .= " --ablocks-{$id}-font-family: {$value->fontFamily};\n";
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 + }
471 680 }
472 681 if ( ! empty( $value->weight ) ) {
473 682 $css .= " --ablocks-{$id}-weight: {$value->weight};\n";
474 683 }
@@ -536,8 +745,10 @@
536 745 }//end if
537 746 }//end foreach
538 747
539 748 $css .= "}\n";
749 + set_transient( 'ablocks_global_css_vars', $css, DAY_IN_SECONDS );
750 + }
540 751
541 752 if ( ! is_admin() && ! Helper::is_gutenberg_editor() ) {
542 753 $css .= $this->get_common_css();
543 754 }
@@ -544,8 +755,31 @@
544 755 wp_add_inline_style( 'ablocks-editor-global-styles', $css );
545 756 wp_add_inline_style( 'ablocks-common-style', $css );
546 757 }
547 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 +
548 782 public function add_editor_inline_css() {
549 783 $custom_css = $this->get_common_css( '.editor-styles-wrapper ' );
550 784 add_theme_support( 'editor-styles' );
551 785 add_editor_style();
@@ -643,23 +877,113 @@
643 877
644 878 public function is_assets_generated() {
645 879 $file_name = $this->current_page_slug;
646 880 $css_file_path = $this->FileUpload->get_file_path( $file_name . '.min.css' );
647 - return file_exists( $css_file_path );
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 + );
648 901 }
649 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 +
650 952 public function set_current_page_template_part( $content, $block ) {
651 953 if ( ! isset( $block['blockName'] ) && is_array( $block ) ) {
652 954 foreach ( $block as $block_item ) {
653 - if ( ! empty( $block_item['blockName'] ) && strpos( $block_item['blockName'], 'ablocks/' ) !== false ) {
955 + if ( $this->is_page_asset_block( $block_item ) ) {
654 956 $this->current_page_blocks[] = $block_item;
655 957 }
656 958 }
657 959 }
658 - if ( ! empty( $block['blockName'] ) && strpos( $block['blockName'], 'ablocks/' ) !== false ) {
960 + if ( $this->is_page_asset_block( $block ) ) {
659 961 $this->current_page_blocks[] = $block;
660 962 }
661 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;
662 986 }
663 987
664 988 public function set_theme_builder_locations( $args ) {
665 989 $this->theme_builder_locations = $args;