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 +735 -86 2.9.1 → 2.16.0 View file →
@@ -7,30 +7,106 @@
7 7
8 8 use ABlocks\Classes\FileUpload;
9 9 use ABlocks\Classes\AssetsGenerator;
10 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;
11 15 use ABlocks\Admin\Menu;
12 16 use ABlocks\Helper;
13 17
14 18 class Assets {
15 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 = [];
16 28 public static function init() {
17 29 $self = new self();
30 + $self->FileUpload = new FileUpload();
31 + $self->current_page_slug = '';
18 32 add_action( 'admin_enqueue_scripts', [ $self, 'dashboard_scripts' ], 10 );
19 33 add_action( 'admin_enqueue_scripts', [ $self, 'demo_importer_scripts' ], 10 );
20 34 add_action( 'enqueue_block_assets', [ $self, 'block_editor_assets' ] );
21 35 add_action( 'enqueue_block_assets', [ $self, 'register_scripts' ] );
22 - add_action( 'wp_enqueue_scripts', [ $self, 'front_end_google_fonts' ] );
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.
23 38
24 39 add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_assets' ], 99 );
25 - add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_localize_script' ] );
26 - add_action( 'wp', [ $self, 'regenerate_missing_assets' ] );
27 40 // Global CSS
28 - add_action( 'wp_enqueue_block_assets', [ $self, 'global_css_variable' ], 999 );
29 - add_action( 'wp_enqueue_scripts', [ $self, 'global_css_variable' ], 999 );
30 - add_action( 'enqueue_block_editor_assets', [ $self, 'global_css_variable' ], 999 );
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 +
31 66 }
32 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 +
33 109 public function get_localize_script_data() {
34 110 return [
35 111 'rest_url' => esc_url_raw( rest_url() ),
36 112 'namespace' => ABLOCKS_PLUGIN_SLUG . '/v1/',
@@ -39,14 +115,124 @@
39 115 'admin_url' => admin_url(),
40 116 'site_url' => site_url(),
41 117 'route_path' => wp_parse_url( admin_url(), PHP_URL_PATH ),
42 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(), [
43 142 'nonce' => wp_create_nonce( 'wp_rest' ),
44 143 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
45 - 'is_pro' => (bool) Helper::is_active_ablocks_pro(),
46 - ];
144 + 'permissions' => Permissions::for_user(),
145 + 'is_admin_user' => Permissions::is_real_admin(),
146 + ]);
47 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 + }
48 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 +
49 235 public function get_dashboard_localize_script_data() {
50 236 $menu = new Menu();
51 237 $args = array(
52 238 'menu' => wp_json_encode( Helper::get_admin_menu_list() ),
@@ -56,10 +242,14 @@
56 242 'container_padding' => Helper::get_settings( 'container_padding', 10 ),
57 243 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ),
58 244 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ),
59 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 ),
60 247 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
61 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(),
62 252 ],
63 253 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
64 254 'is_fse_theme' => Helper::is_fse_theme(),
65 255 'third_party_plugin_status' => [
@@ -65,14 +255,18 @@
65 255 'third_party_plugin_status' => [
66 256 'academy_lms' => Helper::is_active_academy(),
67 257 'storeengine' => Helper::is_active_storeengine(),
68 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(),
69 263 ]
70 264 );
71 265 return apply_filters(
72 266 'ablocks/assets/dashboard_scripts_data',
73 267 array_merge(
74 - $this->get_localize_script_data(),
268 + $this->get_localize_dashboard_data(),
75 269 $args
76 270 )
77 271 );
78 272 }
@@ -77,8 +271,9 @@
77 271 );
78 272 }
79 273 public function get_editor_localize_script_data() {
80 274 global $ablocks_blocks;
275 + $post_types = Helper::get_public_post_type_options();
81 276 $args = array(
82 277 'settings' => [
83 278 'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ),
84 279 'container_padding' => Helper::get_settings( 'container_padding', 10 ),
@@ -84,10 +279,27 @@
84 279 'container_padding' => Helper::get_settings( 'container_padding', 10 ),
85 280 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ),
86 281 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ),
87 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 ),
88 284 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
89 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(),
90 302 ],
91 303 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
92 304 'has_required_block_attribute_migration' => get_option( 'ablocks_has_required_block_attribute_migration' ),
93 305 'third_party_plugin_status' => [
@@ -93,15 +305,22 @@
93 305 'third_party_plugin_status' => [
94 306 'academy_lms' => Helper::is_active_academy(),
95 307 'storeengine' => Helper::is_active_storeengine(),
96 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(),
97 311 ],
98 - 'blocks_status' => $ablocks_blocks
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(),
99 318 );
100 319 return apply_filters(
101 320 'ablocks/assets/editor_scripts_data',
102 321 array_merge(
103 - $this->get_localize_script_data(),
322 + $this->get_localize_editor_data(),
104 323 $args
105 324 )
106 325 );
107 326 }
@@ -184,60 +403,49 @@
184 403 },
185 404 1
186 405 );
187 406
188 - 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 );
189 - 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' );
190 - 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' );
191 -
192 - $dependencies = include ABLOCKS_ASSETS_PATH . 'build/demo-import.asset.php';
193 - wp_enqueue_script(
194 - 'ablocks-demo-import-scripts',
195 - ABLOCKS_ASSETS_URL . 'build/demo-import.js',
196 - $dependencies['dependencies'],
197 - $dependencies['version'],
198 - true
199 - );
200 - wp_localize_script( 'ablocks-demo-import-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() );
201 - wp_set_script_translations( 'ablocks-demo-import-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
407 + $this->demo_template_importer_scripts();
202 408 }//end if
203 409 }
204 410
205 - public function web_fonts_url( $font ) {
206 - $font_url = '';
207 - if ( 'off' !== _x( 'on', 'Google font: on or off', 'ablocks' ) ) {
208 - $font_url = add_query_arg( 'family', rawurlencode( $font ), '//fonts.googleapis.com/css' );
209 - }
210 - return $font_url;
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' );
211 426 }
212 427
213 - public function front_end_google_fonts() {
214 - global $ablocks_fonts;
215 -
216 - if ( empty( $ablocks_fonts ) || ! is_array( $ablocks_fonts ) ) {
217 - return false;
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;
218 435 }
436 + return '//fonts.googleapis.com/css2?family=' . implode( '&family=', $family_strings ) . '&display=swap';
437 + }
219 438
220 - $font_families = [];
221 -
222 - foreach ( $ablocks_fonts as $family => $weights ) {
223 - $font_family_string = $family;
224 - $total_weights = count( $weights );
225 -
226 - if ( $total_weights > 0 ) {
227 - $font_family_string .= ':wght@' . implode( ';', $weights );
228 - }
229 -
230 - $font_families[] = $font_family_string;
439 + public function web_fonts_url( $font ) {
440 + if ( 'off' === _x( 'on', 'Google font: on or off', 'ablocks' ) ) {
441 + return '';
231 442 }
443 + return '//fonts.googleapis.com/css2?family=' . $font;
444 + }
232 445
233 - // Generate the URL using the web_fonts_url method
234 - $google_fonts_url = $this->web_fonts_url( implode( '|', $font_families ) ) . '&display=swap';
235 446
236 - wp_register_style( 'ablocks-frontend-google-fonts', esc_url( $google_fonts_url ), array(), ABLOCKS_VERSION );
237 - }
238 447
239 -
240 448 public function block_editor_assets() {
241 449 if ( is_admin() ) {
242 450 wp_enqueue_style( 'ablocks-editor-font-awesome', ABLOCKS_ASSETS_URL . 'library/font-awesome/css/all.min.css', array(), ABLOCKS_VERSION, 'all' );
243 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 );
@@ -260,44 +468,143 @@
260 468 public function enqueue_frontend_assets() {
261 469 if ( ! Helper::is_enabled_assets_generation() ) {
262 470 return;
263 471 }
264 - if ( ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) ) {
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() ) {
265 478 return;
266 479 }
267 480
268 - $post_id = get_the_ID();
269 - if ( $post_id ) {
270 - $FileUpload = new FileUpload();
271 - $css_file_path = $FileUpload->get_file_path( get_the_ID() . '.min.css' );
272 - if ( file_exists( $css_file_path ) ) {
273 - $css_file_url = $FileUpload->get_file_url( get_the_ID() . '.min.css' );
274 - // Enqueue google fonts
275 - wp_enqueue_style( 'ablocks-frontend-google-fonts' );
276 - // Combine CSS
277 - wp_enqueue_style( 'ablocks-blocks-combine-style', $css_file_url, array(), filemtime( $css_file_path ), 'all' );
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 + }
278 541 }
279 - $js_file_path = $FileUpload->get_file_path( get_the_ID() . '.min.js' );
280 - if ( file_exists( $js_file_path ) ) {
281 - $js_file_url = $FileUpload->get_file_url( get_the_ID() . '.min.js' );
282 - wp_enqueue_script( 'ablocks-blocks-combine-script', $js_file_url, array(), filemtime( $js_file_path ), true );
283 - wp_localize_script( 'ablocks-blocks-combine-script', 'ABlocksGlobal', $this->get_localize_script_data() );
284 - }
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' );
285 549 }
286 550 }
287 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 +
288 575 public function regenerate_missing_assets() {
289 - if ( ! Helper::is_enabled_assets_generation() ) {
576 + if ( $this->is_assets_generated() ) {
290 577 return;
291 578 }
292 579
293 - $post_id = (int) get_the_ID();
294 - if ( $post_id ) {
295 - $FileUpload = new FileUpload();
296 - $has_upload_file = $FileUpload->has_upload_file( get_the_ID() . '.min.css' );
297 - if ( ! $has_upload_file ) {
298 - AssetsGenerator::write_frontend_css_in_uploads_folder( $post_id );
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 + }
299 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 );
300 607 }
301 608 }
302 609 public function register_scripts() {
303 610 $register_styles = RegisterScripts::get_register_styles();
@@ -307,9 +614,20 @@
307 614 $register_scripts = RegisterScripts::get_register_scripts();
308 615 foreach ( $register_scripts as $register_handler => $register_script ) {
309 616 if ( isset( $register_script['dependencies'] ) ) {
310 617 $dependencies = include $register_script['dependencies'];
311 - $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 + );
312 630 $register_script['ver'] = $dependencies['version'];
313 631 }
314 632 wp_register_script(
315 633 $register_handler,
@@ -317,26 +635,357 @@
317 635 $register_script['deps'],
318 636 $register_script['ver'],
319 637 $register_script['args']
320 638 );
321 - }
639 + }//end foreach
322 640 }
323 641 public function global_css_variable() {
324 642 wp_register_style( 'ablocks-editor-global-styles', false, array(), ABLOCKS_VERSION );
325 643 wp_enqueue_style( 'ablocks-editor-global-styles' );
326 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 ) {
327 650 $container_padding = Helper::get_settings( 'container_padding', 10 ) . 'px';
328 - $css = ":root, body .editor-styles-wrapper {
329 - --ablocks-container-padding: $container_padding;
330 - }";
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 + }
331 755 wp_add_inline_style( 'ablocks-editor-global-styles', $css );
756 + wp_add_inline_style( 'ablocks-common-style', $css );
332 757 }
333 758
334 - public function enqueue_frontend_localize_script() {
335 - wp_localize_script( 'ablocks-blocks-combine-script', 'ABlocksGlobal', $this->get_localize_script_data() );
336 - wp_set_script_translations( 'ablocks-blocks-combine-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
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();
337 764
338 - wp_localize_script( 'ablocks-common-script', 'ABlocksGlobal', $this->get_localize_script_data() );
339 - wp_set_script_translations( 'ablocks-common-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
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 );
340 780 }
341 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 + }
342 991 }