PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.11.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.11.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 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / assets.php

assets.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.11.0, at includes/assets.php

832 lines 35.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\FontLoadLocally;
13 use ABlocks\Admin\Menu;
14 use ABlocks\Helper;
15
16 class Assets {
17 public $current_page_template_part = [];
18 public $current_page_blocks = [];
19 private $FileUpload;
20 private $current_page_slug = '';
21 private $theme_builder_locations = [];
22 public static function init() {
23 $self = new self();
24 $self->FileUpload = new FileUpload();
25 $self->current_page_slug = '';
26 add_action( 'admin_enqueue_scripts', [ $self, 'dashboard_scripts' ], 10 );
27 add_action( 'admin_enqueue_scripts', [ $self, 'demo_importer_scripts' ], 10 );
28 add_action( 'enqueue_block_assets', [ $self, 'block_editor_assets' ] );
29 add_action( 'enqueue_block_assets', [ $self, 'register_scripts' ] );
30 // Frontend fonts are now emitted by core via CoreFontRegistry (theme.json
31 // @font-face) with a Google Fonts fallback. See docs/FONT-MANAGEMENT-PLAN.md.
32
33 add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_assets' ], 99 );
34 // Global CSS
35 add_action( 'wp_enqueue_block_assets', [ $self, 'global_css_variable' ] );
36 add_action( 'wp_enqueue_scripts', [ $self, 'global_css_variable' ] );
37 add_action( 'enqueue_block_editor_assets', [ $self, 'global_css_variable' ] );
38 add_action( 'enqueue_block_editor_assets', [ $self, 'add_editor_inline_css' ] );
39 add_action( 'enqueue_block_editor_assets', [ $self, 'editor_google_fonts' ] );
40
41 // Detect page
42 add_action( 'wp', array( $self, 'detect_page' ) );
43
44 if ( ! is_admin() && Helper::is_enabled_assets_generation() ) {
45 if ( Helper::is_fse_theme() ) {
46 add_filter( 'pre_render_block', [ $self, 'set_current_page_template_part' ], 5, 2 );
47 add_action( 'ablocks/before_enqueue_frontend_scripts', [ $self, 'regenerate_missing_assets' ] );
48 } else {
49 add_action( 'ablocks_theme_builder_after_dispatch', [ $self, 'set_theme_builder_locations' ] );
50 add_action( 'wp', [ $self, 'regenerate_missing_assets' ], 20 );
51 }
52 }
53
54 }
55
56 public function detect_page() {
57 if ( is_404() ) {
58 $this->current_page_slug = '404';
59 } elseif ( is_search() ) {
60 $this->current_page_slug = 'search';
61 } elseif ( is_home() ) {
62 $posts_page_id = get_option( 'page_for_posts' );
63 $this->current_page_slug = $posts_page_id ? (string) $posts_page_id : 'blog';
64 } elseif ( is_post_type_archive() ) {
65 $this->current_page_slug = 'archive-' . get_post_type();
66 } elseif ( is_category() ) {
67 $term = get_queried_object();
68 $this->current_page_slug = 'category-' . $term->term_id; // or use slug
69 } elseif ( is_tag() ) {
70 $term = get_queried_object();
71 $this->current_page_slug = 'tag-' . $term->term_id;
72 } elseif ( is_tax() ) {
73 $term = get_queried_object();
74 $this->current_page_slug = 'tax-' . $term->taxonomy . '-' . $term->term_id;
75 } elseif ( is_author() ) {
76 $this->current_page_slug = 'author-' . get_the_author_meta( 'ID' );
77 } elseif ( is_date() ) {
78 $this->current_page_slug = 'date-archive';
79 } elseif ( is_singular() ) {
80 $this->current_page_slug = (string) get_the_ID(); // just the post/page/custom ID
81 } else {
82 $this->current_page_slug = (string) get_the_ID(); // fallback to ID
83 }//end if
84 }
85
86 public function get_current_archive_post_type() {
87 if ( is_post_type_archive() ) {
88 $post_type = get_query_var( 'post_type' );
89 if ( is_array( $post_type ) ) {
90 $post_type = reset( $post_type );
91 }
92 return $post_type;
93 }
94 return null;
95 }
96
97 public function get_localize_script_data() {
98 return [
99 'rest_url' => esc_url_raw( rest_url() ),
100 'namespace' => ABLOCKS_PLUGIN_SLUG . '/v1/',
101 'plugin_root_url' => ABLOCKS_ROOT_URL,
102 'plugin_root_path' => ABLOCKS_ROOT_DIR_PATH,
103 'admin_url' => admin_url(),
104 'site_url' => site_url(),
105 'route_path' => wp_parse_url( admin_url(), PHP_URL_PATH ),
106 'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
107 'is_pro' => (bool) Helper::is_active_ablocks_pro(),
108 'is_archive' => (bool) is_archive(),
109 'archive_post_type' => $this->get_current_archive_post_type(),
110 ];
111 }
112
113 public function get_localize_dashboard_data() {
114 if ( ! current_user_can( 'manage_options' ) ) {
115 return $this->get_localize_script_data();
116 }
117 return array_merge($this->get_localize_script_data(), [
118 'nonce' => wp_create_nonce( 'wp_rest' ),
119 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
120 ]);
121 }
122 public function get_localize_editor_data() {
123 if ( ! current_user_can( 'edit_posts' ) ) {
124 return $this->get_localize_script_data();
125 }
126 return array_merge($this->get_localize_script_data(), [
127 'nonce' => wp_create_nonce( 'wp_rest' ),
128 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
129 ]);
130 }
131 public function get_localize_frontend_data() {
132 $data = array_merge($this->get_localize_script_data(), [
133 'nonce' => wp_create_nonce( 'wp_rest' ),
134 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
135 ]);
136 // The absolute server path is only meaningful to admin-side code; on the
137 // frontend it would publish the filesystem layout in page source. No
138 // frontend script reads it (it is re-exported but never consumed).
139 unset( $data['plugin_root_path'] );
140 return $data;
141 }
142
143 /**
144 * Whether the frontend ABlocksGlobal payload has been attached this request.
145 *
146 * @var bool
147 */
148 private static $globals_localized = false;
149
150 /**
151 * Attach the frontend ABlocksGlobal payload to the shared data-only
152 * `ablocks-globals` handle, at most once per request.
153 *
154 * Previously each per-block script handle localized its own copy, so a page
155 * using N block types printed N identical `var ABlocksGlobal = {...}` blocks
156 * (and called wp_create_nonce() N times). Every aBlocks frontend script now
157 * depends on `ablocks-globals`, so WP prints the object once, before any
158 * consumer, in both asset-generation modes.
159 */
160 public static function localize_globals_once() {
161 if ( self::$globals_localized ) {
162 return;
163 }
164 self::$globals_localized = true;
165 // Self-register so callers never depend on RegisterScripts having run
166 // first — theme-builder templates and block render callbacks fire on
167 // different hooks, and a missing handle would drop the payload silently.
168 if ( ! wp_script_is( 'ablocks-globals', 'registered' ) ) {
169 wp_register_script( 'ablocks-globals', false, array(), ABLOCKS_VERSION, array() );
170 }
171 $self = new self();
172 wp_localize_script( 'ablocks-globals', 'ABlocksGlobal', $self->get_localize_frontend_data() );
173 }
174
175 public function get_dashboard_localize_script_data() {
176 $menu = new Menu();
177 $args = array(
178 'menu' => wp_json_encode( Helper::get_admin_menu_list() ),
179 'toplevel_menu_icon_url' => $menu->get_toplevel_menu_icon_url(),
180 'settings' => [
181 'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ),
182 'container_padding' => Helper::get_settings( 'container_padding', 10 ),
183 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ),
184 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ),
185 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ),
186 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ),
187 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
188 'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ),
189 'font_metric_fallback' => (bool) Helper::get_settings( 'font_metric_fallback', true ),
190 // Global typography picks from the same list the editor offers.
191 'theme_fonts' => \ABlocks\Classes\FontStack::get_theme_font_families(),
192 ],
193 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
194 'is_fse_theme' => Helper::is_fse_theme(),
195 'third_party_plugin_status' => [
196 'academy_lms' => Helper::is_active_academy(),
197 'storeengine' => Helper::is_active_storeengine(),
198 'wp_map_block' => Helper::is_active_wp_map_block(),
199 'easy_content_manager' => Helper::is_active_easy_content_manager(),
200 ]
201 );
202 return apply_filters(
203 'ablocks/assets/dashboard_scripts_data',
204 array_merge(
205 $this->get_localize_dashboard_data(),
206 $args
207 )
208 );
209 }
210 public function get_editor_localize_script_data() {
211 global $ablocks_blocks;
212 $post_types = Helper::get_public_post_type_options();
213 $args = array(
214 'settings' => [
215 'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ),
216 'container_padding' => Helper::get_settings( 'container_padding', 10 ),
217 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ),
218 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ),
219 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ),
220 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ),
221 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
222 'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ),
223 // Design system lock — the editor hides the custom pickers when these
224 // are on, leaving only the global presets.
225 'lock_global_typography' => (bool) Helper::get_settings( 'lock_global_typography', false ),
226 'lock_global_typography_strict' => (bool) Helper::get_settings( 'lock_global_typography_strict', false ),
227 'lock_global_colors' => (bool) Helper::get_settings( 'lock_global_colors', false ),
228 'frontend_dashboard_page' => (int) Helper::get_settings( 'frontend_dashboard_page' ),
229 'global_color' => (array) Helper::get_settings( 'global_color', [] ),
230 'global_typography' => (array) Helper::get_settings( 'global_typography', [] ),
231 'global_typography_list' => wp_list_pluck( Helper::get_settings( 'global_typography', [] ), 'value', 'id' ),
232 'global_font_family_fallback' => (string) Helper::get_settings( 'global_font_family_fallback', 'Sans-serif' ),
233 // Theme.json + Font Library families, so uploaded/theme fonts are
234 // selectable next to the Google catalog.
235 'theme_fonts' => \ABlocks\Classes\FontStack::get_theme_font_families(),
236 ],
237 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
238 'has_required_block_attribute_migration' => get_option( 'ablocks_has_required_block_attribute_migration' ),
239 'third_party_plugin_status' => [
240 'academy_lms' => Helper::is_active_academy(),
241 'storeengine' => Helper::is_active_storeengine(),
242 'wp_map_block' => Helper::is_active_wp_map_block(),
243 'quizpress' => Helper::is_active_quizpress(),
244 'easy_content_manager' => Helper::is_active_easy_content_manager(),
245 ],
246 'blocks_status' => $ablocks_blocks,
247 'post_types' => $post_types
248 );
249 return apply_filters(
250 'ablocks/assets/editor_scripts_data',
251 array_merge(
252 $this->get_localize_editor_data(),
253 $args
254 )
255 );
256 }
257
258 public function dashboard_scripts( $hook ) {
259 $demo_config = Helper::get_theme_demo_config();
260 if ( strpos( $hook, '_page_' . ABLOCKS_PLUGIN_SLUG ) !== false && strpos( $hook, $demo_config['menu_slug'] ) === false ) {
261 // Remove Notices
262 remove_all_actions( 'admin_notices' );
263 // dequeue third party plugin assets
264 add_action(
265 'wp_print_scripts',
266 function () {
267 $isSkip = apply_filters( 'ablocks/skip_no_conflict_backend_scripts', Helper::is_dev_mode_enable() );
268
269 if ( $isSkip ) {
270 return;
271 }
272
273 global $wp_scripts;
274 if ( ! $wp_scripts ) {
275 return;
276 }
277
278 $pluginUrl = plugins_url();
279 foreach ( $wp_scripts->queue as $script ) {
280 $src = $wp_scripts->registered[ $script ]->src;
281 if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) {
282 wp_dequeue_script( $wp_scripts->registered[ $script ]->handle );
283 }
284 }
285 },
286 1
287 );
288
289 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 );
290 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' );
291 wp_enqueue_style( 'ablocks-dashboard-style', ABLOCKS_ASSETS_URL . 'build/dashboard.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/dashboard.css' ), 'all' );
292
293 $dependencies = include ABLOCKS_ASSETS_PATH . 'build/dashboard.asset.php';
294 wp_enqueue_script(
295 'ablocks-dashboard-scripts',
296 ABLOCKS_ASSETS_URL . 'build/dashboard.js',
297 $dependencies['dependencies'],
298 $dependencies['version'],
299 true
300 );
301 wp_localize_script( 'ablocks-dashboard-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() );
302 wp_set_script_translations( 'ablocks-dashboard-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
303 }//end if
304 }
305
306 public function demo_importer_scripts( $hook ) {
307 $demo_config = Helper::get_theme_demo_config();
308 if ( strpos( $hook, $demo_config['menu_slug'] ) !== false ) {
309 // Remove Notices
310 remove_all_actions( 'admin_notices' );
311 // dequeue third party plugin assets
312 add_action(
313 'wp_print_scripts',
314 function () {
315 $isSkip = apply_filters( 'ablocks/skip_no_conflict_demo_importer_scripts', Helper::is_dev_mode_enable() );
316
317 if ( $isSkip ) {
318 return;
319 }
320
321 global $wp_scripts;
322 if ( ! $wp_scripts ) {
323 return;
324 }
325
326 $pluginUrl = plugins_url();
327 foreach ( $wp_scripts->queue as $script ) {
328 $src = $wp_scripts->registered[ $script ]->src;
329 if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) {
330 wp_dequeue_script( $wp_scripts->registered[ $script ]->handle );
331 }
332 }
333 },
334 1
335 );
336
337 $this->demo_template_importer_scripts();
338 }//end if
339 }
340
341 public function demo_template_importer_scripts() {
342 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 );
343 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' );
344 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' );
345
346 $dependencies = include ABLOCKS_ASSETS_PATH . 'build/demo-import.asset.php';
347 wp_enqueue_script(
348 'ablocks-demo-import-scripts',
349 ABLOCKS_ASSETS_URL . 'build/demo-import.js',
350 $dependencies['dependencies'],
351 $dependencies['version'],
352 true
353 );
354 wp_localize_script( 'ablocks-demo-import-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() );
355 wp_set_script_translations( 'ablocks-demo-import-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
356 }
357
358 public function build_google_fonts_url( array $fonts ): string {
359 $family_strings = [];
360 foreach ( $fonts as $family => $weights ) {
361 $family_encoded = str_replace( ' ', '+', $family );
362 $weights = array_unique( array_map( 'strval', (array) $weights ) );
363 sort( $weights );
364 $family_strings[] = ! empty( $weights ) ? $family_encoded . ':wght@' . implode( ';', $weights ) : $family_encoded;
365 }
366 return '//fonts.googleapis.com/css2?family=' . implode( '&family=', $family_strings ) . '&display=swap';
367 }
368
369 public function web_fonts_url( $font ) {
370 if ( 'off' === _x( 'on', 'Google font: on or off', 'ablocks' ) ) {
371 return '';
372 }
373 return '//fonts.googleapis.com/css2?family=' . $font;
374 }
375
376
377
378 public function block_editor_assets() {
379 if ( is_admin() ) {
380 wp_enqueue_style( 'ablocks-editor-font-awesome', ABLOCKS_ASSETS_URL . 'library/font-awesome/css/all.min.css', array(), ABLOCKS_VERSION, 'all' );
381 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 );
382 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' );
383 wp_enqueue_style( 'ablocks-editor-style', ABLOCKS_ASSETS_URL . 'build/blocks.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/blocks.css' ), 'all' );
384
385 // js
386 $dependencies = include ABLOCKS_ASSETS_PATH . 'build/blocks.asset.php';
387 wp_enqueue_script(
388 'ablocks-editor-scripts',
389 ABLOCKS_ASSETS_URL . 'build/blocks.js',
390 $dependencies['dependencies'],
391 $dependencies['version'],
392 true
393 );
394 wp_localize_script( 'ablocks-editor-scripts', 'ABlocksGlobal', $this->get_editor_localize_script_data() );
395 wp_set_script_translations( 'ablocks-editor-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
396 }
397 }
398 public function enqueue_frontend_assets() {
399 if ( ! Helper::is_enabled_assets_generation() ) {
400 return;
401 }
402
403 do_action( 'ablocks/before_enqueue_frontend_scripts' );
404
405 $script_loading_strategy = Helper::get_script_loading_strategy();
406
407 if ( ! $this->is_assets_generated() ) {
408 return;
409 }
410
411 if ( $this->current_page_slug ) {
412 // Enqueue google fonts
413 wp_enqueue_style( 'ablocks-frontend-google-fonts' );
414
415 $css_path = $this->FileUpload->get_file_path( $this->current_page_slug . '.min.css' );
416
417 // Performance Suite — inline the page CSS when it's small enough to
418 // avoid a render-blocking request; fall back to a normal <link> above
419 // the threshold. Opt-in via perf_inline_css.
420 $inline_css = (bool) apply_filters(
421 'ablocks/perf/perf_inline_css',
422 (bool) Helper::get_settings( 'perf_inline_css', true )
423 );
424 $inline_max = (int) apply_filters( 'ablocks/perf/inline_css_max_bytes', 50 * 1024 );
425 $css_size = file_exists( $css_path ) ? (int) filesize( $css_path ) : 0;
426
427 if ( $inline_css && $css_size > 0 && $css_size <= $inline_max ) {
428 wp_register_style( 'ablocks-blocks-combine-style', false, array(), ABLOCKS_VERSION );
429 wp_enqueue_style( 'ablocks-blocks-combine-style' );
430 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
431 wp_add_inline_style( 'ablocks-blocks-combine-style', (string) file_get_contents( $css_path ) );
432 } else {
433 wp_enqueue_style( 'ablocks-blocks-combine-style', $this->FileUpload->get_file_url( $this->current_page_slug . '.min.css' ), array(), filemtime( $css_path ), 'all' );
434
435 // Performance Suite — when the page stylesheet is too large to inline,
436 // load it non-blocking (media="print" + onload swap) so it no longer
437 // delays first paint. Opt-in via perf_async_css (default off: a too-
438 // aggressive critical set risks FOUC, so promote after QA). Editors
439 // viewing the frontend are bypassed so previews render immediately.
440 $async_css = (bool) apply_filters(
441 'ablocks/perf/perf_async_css',
442 (bool) Helper::get_settings( 'perf_async_css', false )
443 );
444 // Critical CSS — inline the above-the-fold/structural subset in
445 // <head> and load the full stylesheet non-blocking. This is a
446 // stronger form of async (it prevents the FOUC async alone can
447 // cause), so enabling it implies async delivery of the full file.
448 $critical_css = (bool) apply_filters(
449 'ablocks/perf/perf_critical_css',
450 (bool) Helper::get_settings( 'perf_critical_css', false )
451 );
452 $bypass_for_editor = is_user_logged_in() && current_user_can( 'edit_posts' )
453 && (bool) apply_filters( 'ablocks/perf/bypass_optimizations_for_editors', true );
454
455 if ( ( $async_css || $critical_css ) && ! $bypass_for_editor ) {
456 // Before deferring the full stylesheet, always inline the
457 // layout-critical CSS so the page paints with correct box
458 // sizes and never reflows when the deferred file loads. The
459 // split is property-based: the deferred remainder is cosmetic
460 // only (colors, shadows, hover states), so it cannot cause a
461 // layout shift (CLS). This makes async delivery safe.
462 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
463 list( $critical ) = \ABlocks\Classes\CriticalCss::split( (string) file_get_contents( $css_path ) );
464 if ( '' !== trim( (string) $critical ) ) {
465 wp_register_style( 'ablocks-critical-css', false, array(), ABLOCKS_VERSION );
466 wp_enqueue_style( 'ablocks-critical-css' );
467 wp_add_inline_style( 'ablocks-critical-css', $critical );
468 }
469 add_filter( 'style_loader_tag', [ $this, 'async_combined_style_tag' ], 20, 2 );
470 }
471 }
472
473 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 ] );
474 self::localize_globals_once();
475 wp_set_script_translations( 'ablocks-blocks-combine-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
476 } else {
477 // fallback if assets not available
478 add_filter( 'ablocks/is_allow_block_inline_assets', '__return_true' );
479 }
480 }
481
482 /**
483 * Rewrite the combined page stylesheet <link> so it loads without blocking
484 * render: fetch as media="print", then flip to media="all" on load, with a
485 * <noscript> fallback for JS-disabled clients. Only touches the aBlocks
486 * combined handle — third-party stylesheets are left untouched.
487 */
488 public function async_combined_style_tag( $tag, $handle ) {
489 if ( 'ablocks-blocks-combine-style' !== $handle ) {
490 return $tag;
491 }
492 $async_tag = preg_replace(
493 [ "/media=(['\"])[^'\"]*\\1/", '/>$/', '/\/>$/' ],
494 [ 'media="print" onload="this.media=\'all\'"', '>', '/>' ],
495 trim( $tag )
496 );
497 // Guarantee the print/onload attributes even if the original tag had no media.
498 if ( strpos( $async_tag, 'onload=' ) === false ) {
499 $async_tag = str_replace( '<link ', '<link media="print" onload="this.media=\'all\'" ', $async_tag );
500 }
501 $noscript = '<noscript>' . $tag . '</noscript>';
502 return $async_tag . "\n" . $noscript;
503 }
504
505 public function regenerate_missing_assets() {
506 if ( $this->is_assets_generated() ) {
507 return;
508 }
509
510 // classic
511 if ( ! Helper::is_fse_theme() ) {
512 $is_parse_main_content = false;
513 if ( is_array( $this->theme_builder_locations ) ) {
514 foreach ( $this->theme_builder_locations as $location_name => $location_id ) {
515 $post = get_post( $location_id );
516 if ( is_object( $post ) ) {
517 $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) );
518 }
519 if ( $location_name === 'header' ) {
520 $post = get_post( get_the_ID() );
521 if ( is_object( $post ) ) {
522 $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) );
523 }
524 $is_parse_main_content = true;
525 }
526 }
527 }
528 if ( false === $is_parse_main_content ) {
529 $post = get_post( get_the_ID() );
530 $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) );
531 }
532 }//end if
533
534 if ( count( $this->current_page_blocks ) ) {
535 $file_name = $this->current_page_slug;
536 AssetsGenerator::write_frontend_css_in_uploads_folder( $file_name, $this->current_page_blocks );
537 }
538 }
539 public function register_scripts() {
540 $register_styles = RegisterScripts::get_register_styles();
541 foreach ( $register_styles as $register_handler => $register_style ) {
542 wp_register_style( $register_handler, $register_style['url'], $register_style['deps'], ABLOCKS_VERSION, $register_style['media'] );
543 }
544 $register_scripts = RegisterScripts::get_register_scripts();
545 foreach ( $register_scripts as $register_handler => $register_script ) {
546 if ( isset( $register_script['dependencies'] ) ) {
547 $dependencies = include $register_script['dependencies'];
548 // Merge rather than overwrite: the generated asset.php only knows
549 // about build-time (@wordpress/*) dependencies, so replacing the
550 // declared list would silently drop hand-declared handles such as
551 // ablocks-globals.
552 $register_script['deps'] = array_values(
553 array_unique(
554 array_merge(
555 isset( $register_script['deps'] ) ? (array) $register_script['deps'] : array(),
556 $dependencies['dependencies']
557 )
558 )
559 );
560 $register_script['ver'] = $dependencies['version'];
561 }
562 wp_register_script(
563 $register_handler,
564 $register_script['url'],
565 $register_script['deps'],
566 $register_script['ver'],
567 $register_script['args']
568 );
569 }//end foreach
570 }
571 public function global_css_variable() {
572 wp_register_style( 'ablocks-editor-global-styles', false, array(), ABLOCKS_VERSION );
573 wp_enqueue_style( 'ablocks-editor-global-styles' );
574
575 // The :root variables only change when global settings change, so cache
576 // the generated string and rebuild it on settings save (see Blocks
577 // ::clear_global_css_cache) instead of looping settings on every request.
578 $css = get_transient( 'ablocks_global_css_vars' );
579 if ( false === $css ) {
580 $container_padding = Helper::get_settings( 'container_padding', 10 ) . 'px';
581 $css = ":root, body .editor-styles-wrapper {\n";
582 $css .= " --ablocks-container-padding: $container_padding;\n";
583
584 // Colors
585 $global_color = (array) Helper::get_settings( 'global_color', [] );
586 foreach ( $global_color as $color ) {
587 if ( isset( $color->id, $color->value ) ) {
588 $css .= " --ablocks-{$color->id}: {$color->value};\n";
589 }
590 }
591
592 // Typography
593 $global_typography = (array) Helper::get_settings( 'global_typography', [] );
594 foreach ( $global_typography as $typography ) {
595 if ( isset( $typography->id, $typography->value ) ) {
596 $id = $typography->id;
597 $value = $typography->value;
598
599 // Desktop values
600 if ( ! empty( $value->fontFamily ) ) {
601 // Every consumer of this variable drops it straight into
602 // font-family, so it has to carry the whole stack.
603 $font_stack = \ABlocks\Classes\FontStack::build(
604 $value->fontFamily,
605 isset( $value->fontFallback ) ? $value->fontFallback : ''
606 );
607 if ( '' !== $font_stack ) {
608 $css .= " --ablocks-{$id}-font-family: {$font_stack};\n";
609 }
610 }
611 if ( ! empty( $value->weight ) ) {
612 $css .= " --ablocks-{$id}-weight: {$value->weight};\n";
613 }
614 if ( ! empty( $value->transform ) ) {
615 $css .= " --ablocks-{$id}-transform: {$value->transform};\n";
616 }
617 if ( ! empty( $value->style ) ) {
618 $css .= " --ablocks-{$id}-style: {$value->style};\n";
619 }
620 if ( ! empty( $value->decoration ) ) {
621 $css .= " --ablocks-{$id}-decoration: {$value->decoration};\n";
622 }
623 if ( ! empty( $value->fontSize ) ) {
624 $unit = ! empty( $value->fontSizeUnit ) ? $value->fontSizeUnit : 'px';
625 $css .= " --ablocks-{$id}-font-size: {$value->fontSize}{$unit};\n";
626 }
627 if ( ! empty( $value->lineHeight ) ) {
628 $unit = ! empty( $value->lineHeightUnit ) ? $value->lineHeightUnit : 'px';
629 $css .= " --ablocks-{$id}-line-height: {$value->lineHeight}{$unit};\n";
630 }
631 if ( ! empty( $value->letterSpacing ) ) {
632 $unit = ! empty( $value->letterSpacingUnit ) ? $value->letterSpacingUnit : 'px';
633 $css .= " --ablocks-{$id}-letter-spacing: {$value->letterSpacing}{$unit};\n";
634 }
635 if ( ! empty( $value->wordSpacing ) ) {
636 $unit = ! empty( $value->wordSpacingUnit ) ? $value->wordSpacingUnit : 'px';
637 $css .= " --ablocks-{$id}-word-spacing: {$value->wordSpacing}{$unit};\n";
638 }
639
640 // Tablet values
641 if ( ! empty( $value->fontSizeTablet ) ) {
642 $unit = ! empty( $value->fontSizeUnitTablet ) ? $value->fontSizeUnitTablet : 'px';
643 $css .= " --ablocks-{$id}-font-size-tablet: {$value->fontSizeTablet}{$unit};\n";
644 }
645 if ( ! empty( $value->lineHeightTablet ) ) {
646 $unit = ! empty( $value->lineHeightUnitTablet ) ? $value->lineHeightUnitTablet : 'px';
647 $css .= " --ablocks-{$id}-line-height-tablet: {$value->lineHeightTablet}{$unit};\n";
648 }
649 if ( ! empty( $value->letterSpacingTablet ) ) {
650 $unit = ! empty( $value->letterSpacingUnitTablet ) ? $value->letterSpacingUnitTablet : 'px';
651 $css .= " --ablocks-{$id}-letter-spacing-tablet: {$value->letterSpacingTablet}{$unit};\n";
652 }
653 if ( ! empty( $value->wordSpacingTablet ) ) {
654 $unit = ! empty( $value->wordSpacingUnitTablet ) ? $value->wordSpacingUnitTablet : 'px';
655 $css .= " --ablocks-{$id}-word-spacing-tablet: {$value->wordSpacingTablet}{$unit};\n";
656 }
657
658 // Mobile values
659 if ( ! empty( $value->fontSizeMobile ) ) {
660 $unit = ! empty( $value->fontSizeUnitMobile ) ? $value->fontSizeUnitMobile : 'px';
661 $css .= " --ablocks-{$id}-font-size-mobile: {$value->fontSizeMobile}{$unit};\n";
662 }
663 if ( ! empty( $value->lineHeightMobile ) ) {
664 $unit = ! empty( $value->lineHeightUnitMobile ) ? $value->lineHeightUnitMobile : 'px';
665 $css .= " --ablocks-{$id}-line-height-mobile: {$value->lineHeightMobile}{$unit};\n";
666 }
667 if ( ! empty( $value->letterSpacingMobile ) ) {
668 $unit = ! empty( $value->letterSpacingUnitMobile ) ? $value->letterSpacingUnitMobile : 'px';
669 $css .= " --ablocks-{$id}-letter-spacing-mobile: {$value->letterSpacingMobile}{$unit};\n";
670 }
671 if ( ! empty( $value->wordSpacingMobile ) ) {
672 $unit = ! empty( $value->wordSpacingUnitMobile ) ? $value->wordSpacingUnitMobile : 'px';
673 $css .= " --ablocks-{$id}-word-spacing-mobile: {$value->wordSpacingMobile}{$unit};\n";
674 }
675 }//end if
676 }//end foreach
677
678 $css .= "}\n";
679 set_transient( 'ablocks_global_css_vars', $css, DAY_IN_SECONDS );
680 }
681
682 if ( ! is_admin() && ! Helper::is_gutenberg_editor() ) {
683 $css .= $this->get_common_css();
684 }
685 wp_add_inline_style( 'ablocks-editor-global-styles', $css );
686 wp_add_inline_style( 'ablocks-common-style', $css );
687 }
688
689 public function editor_google_fonts() {
690 // Load the fonts already used by the post being edited (plus globals) so
691 // existing content previews correctly on editor load. Live selections are
692 // handled client-side by the typography control.
693 $fonts = \ABlocks\Classes\FontCollector::get_global_fonts();
694
695 $post_id = 0;
696 if ( function_exists( 'get_the_ID' ) && get_the_ID() ) {
697 $post_id = (int) get_the_ID();
698 } elseif ( isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
699 $post_id = absint( $_GET['post'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
700 }
701 if ( $post_id ) {
702 $fonts = \ABlocks\Classes\FontCollector::merge( $fonts, \ABlocks\Classes\FontCollector::get_post_fonts( $post_id ) );
703 }
704
705 if ( empty( $fonts ) ) {
706 return;
707 }
708 $url = $this->build_google_fonts_url( $fonts );
709 wp_enqueue_style( 'ablocks-editor-google-fonts', $url, array(), null );
710 }
711
712 public function add_editor_inline_css() {
713 $custom_css = $this->get_common_css( '.editor-styles-wrapper ' );
714 add_theme_support( 'editor-styles' );
715 add_editor_style();
716 wp_add_inline_style( 'wp-block-library', $custom_css );
717 }
718
719 public function get_common_css( $parent_selector = '' ) {
720 global $ablocks_settings;
721 $attributes = [
722 'global_body_text_color' => $ablocks_settings->global_body_text_color,
723 'global_body_typography' => $ablocks_settings->global_body_typography,
724 'global_body_paragraph_space' => $ablocks_settings->global_body_paragraph_space,
725 'global_link_color' => $ablocks_settings->global_link_color,
726 'global_link_hover_color' => $ablocks_settings->global_link_hover_color,
727 'global_link_typography' => $ablocks_settings->global_link_typography,
728 'global_link_hover_typography' => $ablocks_settings->global_link_hover_typography,
729 'global_h1_color' => $ablocks_settings->global_h1_color,
730 'global_h1_typography' => $ablocks_settings->global_h1_typography,
731 'global_h2_color' => $ablocks_settings->global_h2_color,
732 'global_h2_typography' => $ablocks_settings->global_h2_typography,
733 'global_h3_color' => $ablocks_settings->global_h3_color,
734 'global_h3_typography' => $ablocks_settings->global_h3_typography,
735 'global_h4_color' => $ablocks_settings->global_h4_color,
736 'global_h4_typography' => $ablocks_settings->global_h4_typography,
737 'global_h5_color' => $ablocks_settings->global_h5_color,
738 'global_h5_typography' => $ablocks_settings->global_h5_typography,
739 'global_h6_color' => $ablocks_settings->global_h6_color,
740 'global_h6_typography' => $ablocks_settings->global_h6_typography,
741 ];
742 $css_generator = new GlobalCssGenerator( $attributes );
743
744 $css_generator->add_class_styles(
745 $parent_selector . 'body',
746 $css_generator->get_body_css( $attributes ),
747 $css_generator->get_body_css( $attributes, 'Tablet' ),
748 $css_generator->get_body_css( $attributes, 'Mobile' )
749 );
750 $css_generator->add_class_styles(
751 $parent_selector . 'p',
752 $css_generator->get_body_paragraph_css( $attributes ),
753 $css_generator->get_body_paragraph_css( $attributes, 'Tablet' ),
754 $css_generator->get_body_paragraph_css( $attributes, 'Mobile' )
755 );
756 $css_generator->add_class_styles(
757 $parent_selector . 'a',
758 $css_generator->get_body_anchor_css( $attributes ),
759 $css_generator->get_body_anchor_css( $attributes, 'Tablet' ),
760 $css_generator->get_body_anchor_css( $attributes, 'Mobile' )
761 );
762 $css_generator->add_class_styles(
763 $parent_selector . 'a:hover',
764 $css_generator->get_body_anchor_hover_css( $attributes ),
765 $css_generator->get_body_anchor_hover_css( $attributes, 'Tablet' ),
766 $css_generator->get_body_anchor_hover_css( $attributes, 'Mobile' )
767 );
768 $css_generator->add_class_styles(
769 $parent_selector . 'h1',
770 $css_generator->get_global_h1_css( $attributes ),
771 $css_generator->get_global_h1_css( $attributes, 'Tablet' ),
772 $css_generator->get_global_h1_css( $attributes, 'Mobile' )
773 );
774 $css_generator->add_class_styles(
775 $parent_selector . 'h2',
776 $css_generator->get_global_h2_css( $attributes ),
777 $css_generator->get_global_h2_css( $attributes, 'Tablet' ),
778 $css_generator->get_global_h2_css( $attributes, 'Mobile' )
779 );
780 $css_generator->add_class_styles(
781 $parent_selector . 'h3',
782 $css_generator->get_global_h3_css( $attributes ),
783 $css_generator->get_global_h3_css( $attributes, 'Tablet' ),
784 $css_generator->get_global_h3_css( $attributes, 'Mobile' )
785 );
786 $css_generator->add_class_styles(
787 $parent_selector . 'h4',
788 $css_generator->get_global_h4_css( $attributes ),
789 $css_generator->get_global_h4_css( $attributes, 'Tablet' ),
790 $css_generator->get_global_h4_css( $attributes, 'Mobile' )
791 );
792 $css_generator->add_class_styles(
793 $parent_selector . 'h5',
794 $css_generator->get_global_h5_css( $attributes ),
795 $css_generator->get_global_h5_css( $attributes, 'Tablet' ),
796 $css_generator->get_global_h5_css( $attributes, 'Mobile' )
797 );
798 $css_generator->add_class_styles(
799 $parent_selector . 'h6',
800 $css_generator->get_global_h6_css( $attributes ),
801 $css_generator->get_global_h6_css( $attributes, 'Tablet' ),
802 $css_generator->get_global_h6_css( $attributes, 'Mobile' )
803 );
804 return $css_generator->generate_css();
805 }
806
807
808 public function is_assets_generated() {
809 $file_name = $this->current_page_slug;
810 $css_file_path = $this->FileUpload->get_file_path( $file_name . '.min.css' );
811 return file_exists( $css_file_path );
812 }
813
814 public function set_current_page_template_part( $content, $block ) {
815 if ( ! isset( $block['blockName'] ) && is_array( $block ) ) {
816 foreach ( $block as $block_item ) {
817 if ( ! empty( $block_item['blockName'] ) && strpos( $block_item['blockName'], 'ablocks/' ) !== false ) {
818 $this->current_page_blocks[] = $block_item;
819 }
820 }
821 }
822 if ( ! empty( $block['blockName'] ) && strpos( $block['blockName'], 'ablocks/' ) !== false ) {
823 $this->current_page_blocks[] = $block;
824 }
825 return $content;
826 }
827
828 public function set_theme_builder_locations( $args ) {
829 $this->theme_builder_locations = $args;
830 }
831 }
832