PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.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.9.0, at includes/assets.php

678 lines 28.1 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 add_action( 'wp_enqueue_scripts', [ $self, 'front_end_google_fonts' ], 999 );
31
32 add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_assets' ], 99 );
33 // Global CSS
34 add_action( 'wp_enqueue_block_assets', [ $self, 'global_css_variable' ] );
35 add_action( 'wp_enqueue_scripts', [ $self, 'global_css_variable' ] );
36 add_action( 'enqueue_block_editor_assets', [ $self, 'global_css_variable' ] );
37 add_action( 'enqueue_block_editor_assets', [ $self, 'add_editor_inline_css' ] );
38 add_action( 'enqueue_block_editor_assets', [ $self, 'editor_google_fonts' ] );
39
40 // Detect page
41 add_action( 'wp', array( $self, 'detect_page' ) );
42
43 if ( ! is_admin() && Helper::is_enabled_assets_generation() ) {
44 if ( Helper::is_fse_theme() ) {
45 add_filter( 'pre_render_block', [ $self, 'set_current_page_template_part' ], 5, 2 );
46 add_action( 'ablocks/before_enqueue_frontend_scripts', [ $self, 'regenerate_missing_assets' ] );
47 } else {
48 add_action( 'ablocks_theme_builder_after_dispatch', [ $self, 'set_theme_builder_locations' ] );
49 add_action( 'wp', [ $self, 'regenerate_missing_assets' ], 20 );
50 }
51 }
52
53 }
54
55 public function detect_page() {
56 if ( is_404() ) {
57 $this->current_page_slug = '404';
58 } elseif ( is_search() ) {
59 $this->current_page_slug = 'search';
60 } elseif ( is_home() ) {
61 $posts_page_id = get_option( 'page_for_posts' );
62 $this->current_page_slug = $posts_page_id ? (string) $posts_page_id : 'blog';
63 } elseif ( is_post_type_archive() ) {
64 $this->current_page_slug = 'archive-' . get_post_type();
65 } elseif ( is_category() ) {
66 $term = get_queried_object();
67 $this->current_page_slug = 'category-' . $term->term_id; // or use slug
68 } elseif ( is_tag() ) {
69 $term = get_queried_object();
70 $this->current_page_slug = 'tag-' . $term->term_id;
71 } elseif ( is_tax() ) {
72 $term = get_queried_object();
73 $this->current_page_slug = 'tax-' . $term->taxonomy . '-' . $term->term_id;
74 } elseif ( is_author() ) {
75 $this->current_page_slug = 'author-' . get_the_author_meta( 'ID' );
76 } elseif ( is_date() ) {
77 $this->current_page_slug = 'date-archive';
78 } elseif ( is_singular() ) {
79 $this->current_page_slug = (string) get_the_ID(); // just the post/page/custom ID
80 } else {
81 $this->current_page_slug = (string) get_the_ID(); // fallback to ID
82 }//end if
83 }
84
85 public function get_current_archive_post_type() {
86 if ( is_post_type_archive() ) {
87 $post_type = get_query_var( 'post_type' );
88 if ( is_array( $post_type ) ) {
89 $post_type = reset( $post_type );
90 }
91 return $post_type;
92 }
93 return null;
94 }
95
96 public function get_localize_script_data() {
97 return [
98 'rest_url' => esc_url_raw( rest_url() ),
99 'namespace' => ABLOCKS_PLUGIN_SLUG . '/v1/',
100 'plugin_root_url' => ABLOCKS_ROOT_URL,
101 'plugin_root_path' => ABLOCKS_ROOT_DIR_PATH,
102 'admin_url' => admin_url(),
103 'site_url' => site_url(),
104 'route_path' => wp_parse_url( admin_url(), PHP_URL_PATH ),
105 'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
106 'is_pro' => (bool) Helper::is_active_ablocks_pro(),
107 'is_archive' => (bool) is_archive(),
108 'archive_post_type' => $this->get_current_archive_post_type(),
109 ];
110 }
111
112 public function get_localize_dashboard_data() {
113 if ( ! current_user_can( 'manage_options' ) ) {
114 return $this->get_localize_script_data();
115 }
116 return array_merge($this->get_localize_script_data(), [
117 'nonce' => wp_create_nonce( 'wp_rest' ),
118 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
119 ]);
120 }
121 public function get_localize_editor_data() {
122 if ( ! current_user_can( 'edit_posts' ) ) {
123 return $this->get_localize_script_data();
124 }
125 return array_merge($this->get_localize_script_data(), [
126 'nonce' => wp_create_nonce( 'wp_rest' ),
127 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
128 ]);
129 }
130 public function get_localize_frontend_data() {
131 return array_merge($this->get_localize_script_data(), [
132 'nonce' => wp_create_nonce( 'wp_rest' ),
133 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ),
134 ]);
135 }
136
137 public function get_dashboard_localize_script_data() {
138 $menu = new Menu();
139 $args = array(
140 'menu' => wp_json_encode( Helper::get_admin_menu_list() ),
141 'toplevel_menu_icon_url' => $menu->get_toplevel_menu_icon_url(),
142 'settings' => [
143 'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ),
144 'container_padding' => Helper::get_settings( 'container_padding', 10 ),
145 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ),
146 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ),
147 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ),
148 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ),
149 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
150 'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ),
151 ],
152 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
153 'is_fse_theme' => Helper::is_fse_theme(),
154 'third_party_plugin_status' => [
155 'academy_lms' => Helper::is_active_academy(),
156 'storeengine' => Helper::is_active_storeengine(),
157 'wp_map_block' => Helper::is_active_wp_map_block(),
158 'easy_content_manager' => Helper::is_active_easy_content_manager(),
159 ]
160 );
161 return apply_filters(
162 'ablocks/assets/dashboard_scripts_data',
163 array_merge(
164 $this->get_localize_dashboard_data(),
165 $args
166 )
167 );
168 }
169 public function get_editor_localize_script_data() {
170 global $ablocks_blocks;
171 $post_types = Helper::get_public_post_type_options();
172 $args = array(
173 'settings' => [
174 'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ),
175 'container_padding' => Helper::get_settings( 'container_padding', 10 ),
176 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ),
177 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ),
178 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ),
179 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ),
180 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ),
181 'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ),
182 'frontend_dashboard_page' => (int) Helper::get_settings( 'frontend_dashboard_page' ),
183 'global_color' => (array) Helper::get_settings( 'global_color', [] ),
184 'global_typography' => (array) Helper::get_settings( 'global_typography', [] ),
185 'global_typography_list' => wp_list_pluck( Helper::get_settings( 'global_typography', [] ), 'value', 'id' ),
186 'global_font_family_fallback' => (string) Helper::get_settings( 'global_font_family_fallback', 'Sans-serif' ),
187 ],
188 'is_gutenberg_editor' => Helper::is_gutenberg_editor(),
189 'has_required_block_attribute_migration' => get_option( 'ablocks_has_required_block_attribute_migration' ),
190 'third_party_plugin_status' => [
191 'academy_lms' => Helper::is_active_academy(),
192 'storeengine' => Helper::is_active_storeengine(),
193 'wp_map_block' => Helper::is_active_wp_map_block(),
194 'quizpress' => Helper::is_active_quizpress(),
195 'easy_content_manager' => Helper::is_active_easy_content_manager(),
196 ],
197 'blocks_status' => $ablocks_blocks,
198 'post_types' => $post_types
199 );
200 return apply_filters(
201 'ablocks/assets/editor_scripts_data',
202 array_merge(
203 $this->get_localize_editor_data(),
204 $args
205 )
206 );
207 }
208
209 public function dashboard_scripts( $hook ) {
210 $demo_config = Helper::get_theme_demo_config();
211 if ( strpos( $hook, '_page_' . ABLOCKS_PLUGIN_SLUG ) !== false && strpos( $hook, $demo_config['menu_slug'] ) === false ) {
212 // Remove Notices
213 remove_all_actions( 'admin_notices' );
214 // dequeue third party plugin assets
215 add_action(
216 'wp_print_scripts',
217 function () {
218 $isSkip = apply_filters( 'ablocks/skip_no_conflict_backend_scripts', Helper::is_dev_mode_enable() );
219
220 if ( $isSkip ) {
221 return;
222 }
223
224 global $wp_scripts;
225 if ( ! $wp_scripts ) {
226 return;
227 }
228
229 $pluginUrl = plugins_url();
230 foreach ( $wp_scripts->queue as $script ) {
231 $src = $wp_scripts->registered[ $script ]->src;
232 if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) {
233 wp_dequeue_script( $wp_scripts->registered[ $script ]->handle );
234 }
235 }
236 },
237 1
238 );
239
240 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 );
241 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' );
242 wp_enqueue_style( 'ablocks-dashboard-style', ABLOCKS_ASSETS_URL . 'build/dashboard.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/dashboard.css' ), 'all' );
243
244 $dependencies = include ABLOCKS_ASSETS_PATH . 'build/dashboard.asset.php';
245 wp_enqueue_script(
246 'ablocks-dashboard-scripts',
247 ABLOCKS_ASSETS_URL . 'build/dashboard.js',
248 $dependencies['dependencies'],
249 $dependencies['version'],
250 true
251 );
252 wp_localize_script( 'ablocks-dashboard-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() );
253 wp_set_script_translations( 'ablocks-dashboard-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
254 }//end if
255 }
256
257 public function demo_importer_scripts( $hook ) {
258 $demo_config = Helper::get_theme_demo_config();
259 if ( strpos( $hook, $demo_config['menu_slug'] ) !== false ) {
260 // Remove Notices
261 remove_all_actions( 'admin_notices' );
262 // dequeue third party plugin assets
263 add_action(
264 'wp_print_scripts',
265 function () {
266 $isSkip = apply_filters( 'ablocks/skip_no_conflict_demo_importer_scripts', Helper::is_dev_mode_enable() );
267
268 if ( $isSkip ) {
269 return;
270 }
271
272 global $wp_scripts;
273 if ( ! $wp_scripts ) {
274 return;
275 }
276
277 $pluginUrl = plugins_url();
278 foreach ( $wp_scripts->queue as $script ) {
279 $src = $wp_scripts->registered[ $script ]->src;
280 if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) {
281 wp_dequeue_script( $wp_scripts->registered[ $script ]->handle );
282 }
283 }
284 },
285 1
286 );
287
288 $this->demo_template_importer_scripts();
289 }//end if
290 }
291
292 public function demo_template_importer_scripts() {
293 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 );
294 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' );
295 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' );
296
297 $dependencies = include ABLOCKS_ASSETS_PATH . 'build/demo-import.asset.php';
298 wp_enqueue_script(
299 'ablocks-demo-import-scripts',
300 ABLOCKS_ASSETS_URL . 'build/demo-import.js',
301 $dependencies['dependencies'],
302 $dependencies['version'],
303 true
304 );
305 wp_localize_script( 'ablocks-demo-import-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() );
306 wp_set_script_translations( 'ablocks-demo-import-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
307 }
308
309 public function build_google_fonts_url( array $fonts ): string {
310 $family_strings = [];
311 foreach ( $fonts as $family => $weights ) {
312 $family_encoded = str_replace( ' ', '+', $family );
313 $weights = array_unique( array_map( 'strval', (array) $weights ) );
314 sort( $weights );
315 $family_strings[] = ! empty( $weights ) ? $family_encoded . ':wght@' . implode( ';', $weights ) : $family_encoded;
316 }
317 return '//fonts.googleapis.com/css2?family=' . implode( '&family=', $family_strings ) . '&display=swap';
318 }
319
320 public function web_fonts_url( $font ) {
321 if ( 'off' === _x( 'on', 'Google font: on or off', 'ablocks' ) ) {
322 return '';
323 }
324 return '//fonts.googleapis.com/css2?family=' . $font;
325 }
326
327 public function front_end_google_fonts() {
328 global $ablocks_fonts;
329 if ( empty( $ablocks_fonts ) || ! is_array( $ablocks_fonts ) ) {
330 return false;
331 }
332
333 if ( Helper::get_settings( 'enabled_load_google_font_locally', false ) ) {
334 $FontLoadLocally = new FontLoadLocally();
335 $FontLoadLocally->enqueue_fonts( $ablocks_fonts );
336 } else {
337 $url = $this->build_google_fonts_url( $ablocks_fonts );
338 error_log( 'Enqueuing Google Fonts: ' . $url );
339 wp_enqueue_style( 'ablocks-frontend-google-fonts', $url, array(), null );
340 }
341 }
342
343
344 public function block_editor_assets() {
345 if ( is_admin() ) {
346 wp_enqueue_style( 'ablocks-editor-font-awesome', ABLOCKS_ASSETS_URL . 'library/font-awesome/css/all.min.css', array(), ABLOCKS_VERSION, 'all' );
347 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 );
348 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' );
349 wp_enqueue_style( 'ablocks-editor-style', ABLOCKS_ASSETS_URL . 'build/blocks.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/blocks.css' ), 'all' );
350
351 // js
352 $dependencies = include ABLOCKS_ASSETS_PATH . 'build/blocks.asset.php';
353 wp_enqueue_script(
354 'ablocks-editor-scripts',
355 ABLOCKS_ASSETS_URL . 'build/blocks.js',
356 $dependencies['dependencies'],
357 $dependencies['version'],
358 true
359 );
360 wp_localize_script( 'ablocks-editor-scripts', 'ABlocksGlobal', $this->get_editor_localize_script_data() );
361 wp_set_script_translations( 'ablocks-editor-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
362 }
363 }
364 public function enqueue_frontend_assets() {
365 if ( ! Helper::is_enabled_assets_generation() ) {
366 return;
367 }
368
369 do_action( 'ablocks/before_enqueue_frontend_scripts' );
370
371 $script_loading_strategy = Helper::get_script_loading_strategy();
372
373 if ( ! $this->is_assets_generated() ) {
374 return;
375 }
376
377 if ( $this->current_page_slug ) {
378 // Enqueue google fonts
379 wp_enqueue_style( 'ablocks-frontend-google-fonts' );
380 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' );
381
382 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 ] );
383 wp_localize_script( 'ablocks-blocks-combine-script', 'ABlocksGlobal', $this->get_localize_frontend_data() );
384 wp_set_script_translations( 'ablocks-blocks-combine-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' );
385 } else {
386 // fallback if assets not available
387 add_filter( 'ablocks/is_allow_block_inline_assets', '__return_true' );
388 }
389 }
390
391 public function regenerate_missing_assets() {
392 if ( $this->is_assets_generated() ) {
393 return;
394 }
395
396 // classic
397 if ( ! Helper::is_fse_theme() ) {
398 $is_parse_main_content = false;
399 if ( is_array( $this->theme_builder_locations ) ) {
400 foreach ( $this->theme_builder_locations as $location_name => $location_id ) {
401 $post = get_post( $location_id );
402 if ( is_object( $post ) ) {
403 $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) );
404 }
405 if ( $location_name === 'header' ) {
406 $post = get_post( get_the_ID() );
407 if ( is_object( $post ) ) {
408 $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) );
409 }
410 $is_parse_main_content = true;
411 }
412 }
413 }
414 if ( false === $is_parse_main_content ) {
415 $post = get_post( get_the_ID() );
416 $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) );
417 }
418 }//end if
419
420 if ( count( $this->current_page_blocks ) ) {
421 $file_name = $this->current_page_slug;
422 AssetsGenerator::write_frontend_css_in_uploads_folder( $file_name, $this->current_page_blocks );
423 }
424 }
425 public function register_scripts() {
426 $register_styles = RegisterScripts::get_register_styles();
427 foreach ( $register_styles as $register_handler => $register_style ) {
428 wp_register_style( $register_handler, $register_style['url'], $register_style['deps'], ABLOCKS_VERSION, $register_style['media'] );
429 }
430 $register_scripts = RegisterScripts::get_register_scripts();
431 foreach ( $register_scripts as $register_handler => $register_script ) {
432 if ( isset( $register_script['dependencies'] ) ) {
433 $dependencies = include $register_script['dependencies'];
434 $register_script['deps'] = $dependencies['dependencies'];
435 $register_script['ver'] = $dependencies['version'];
436 }
437 wp_register_script(
438 $register_handler,
439 $register_script['url'],
440 $register_script['deps'],
441 $register_script['ver'],
442 $register_script['args']
443 );
444 }
445 }
446 public function global_css_variable() {
447 wp_register_style( 'ablocks-editor-global-styles', false, array(), ABLOCKS_VERSION );
448 wp_enqueue_style( 'ablocks-editor-global-styles' );
449
450 $container_padding = Helper::get_settings( 'container_padding', 10 ) . 'px';
451 $css = ":root, body .editor-styles-wrapper {\n";
452 $css .= " --ablocks-container-padding: $container_padding;\n";
453
454 // Colors
455 $global_color = (array) Helper::get_settings( 'global_color', [] );
456 foreach ( $global_color as $color ) {
457 if ( isset( $color->id, $color->value ) ) {
458 $css .= " --ablocks-{$color->id}: {$color->value};\n";
459 }
460 }
461
462 // Typography
463 $global_typography = (array) Helper::get_settings( 'global_typography', [] );
464 foreach ( $global_typography as $typography ) {
465 if ( isset( $typography->id, $typography->value ) ) {
466 $id = $typography->id;
467 $value = $typography->value;
468
469 // Desktop values
470 if ( ! empty( $value->fontFamily ) ) {
471 $css .= " --ablocks-{$id}-font-family: {$value->fontFamily};\n";
472 }
473 if ( ! empty( $value->weight ) ) {
474 $css .= " --ablocks-{$id}-weight: {$value->weight};\n";
475 }
476 if ( ! empty( $value->transform ) ) {
477 $css .= " --ablocks-{$id}-transform: {$value->transform};\n";
478 }
479 if ( ! empty( $value->style ) ) {
480 $css .= " --ablocks-{$id}-style: {$value->style};\n";
481 }
482 if ( ! empty( $value->decoration ) ) {
483 $css .= " --ablocks-{$id}-decoration: {$value->decoration};\n";
484 }
485 if ( ! empty( $value->fontSize ) ) {
486 $unit = ! empty( $value->fontSizeUnit ) ? $value->fontSizeUnit : 'px';
487 $css .= " --ablocks-{$id}-font-size: {$value->fontSize}{$unit};\n";
488 }
489 if ( ! empty( $value->lineHeight ) ) {
490 $unit = ! empty( $value->lineHeightUnit ) ? $value->lineHeightUnit : 'px';
491 $css .= " --ablocks-{$id}-line-height: {$value->lineHeight}{$unit};\n";
492 }
493 if ( ! empty( $value->letterSpacing ) ) {
494 $unit = ! empty( $value->letterSpacingUnit ) ? $value->letterSpacingUnit : 'px';
495 $css .= " --ablocks-{$id}-letter-spacing: {$value->letterSpacing}{$unit};\n";
496 }
497 if ( ! empty( $value->wordSpacing ) ) {
498 $unit = ! empty( $value->wordSpacingUnit ) ? $value->wordSpacingUnit : 'px';
499 $css .= " --ablocks-{$id}-word-spacing: {$value->wordSpacing}{$unit};\n";
500 }
501
502 // Tablet values
503 if ( ! empty( $value->fontSizeTablet ) ) {
504 $unit = ! empty( $value->fontSizeUnitTablet ) ? $value->fontSizeUnitTablet : 'px';
505 $css .= " --ablocks-{$id}-font-size-tablet: {$value->fontSizeTablet}{$unit};\n";
506 }
507 if ( ! empty( $value->lineHeightTablet ) ) {
508 $unit = ! empty( $value->lineHeightUnitTablet ) ? $value->lineHeightUnitTablet : 'px';
509 $css .= " --ablocks-{$id}-line-height-tablet: {$value->lineHeightTablet}{$unit};\n";
510 }
511 if ( ! empty( $value->letterSpacingTablet ) ) {
512 $unit = ! empty( $value->letterSpacingUnitTablet ) ? $value->letterSpacingUnitTablet : 'px';
513 $css .= " --ablocks-{$id}-letter-spacing-tablet: {$value->letterSpacingTablet}{$unit};\n";
514 }
515 if ( ! empty( $value->wordSpacingTablet ) ) {
516 $unit = ! empty( $value->wordSpacingUnitTablet ) ? $value->wordSpacingUnitTablet : 'px';
517 $css .= " --ablocks-{$id}-word-spacing-tablet: {$value->wordSpacingTablet}{$unit};\n";
518 }
519
520 // Mobile values
521 if ( ! empty( $value->fontSizeMobile ) ) {
522 $unit = ! empty( $value->fontSizeUnitMobile ) ? $value->fontSizeUnitMobile : 'px';
523 $css .= " --ablocks-{$id}-font-size-mobile: {$value->fontSizeMobile}{$unit};\n";
524 }
525 if ( ! empty( $value->lineHeightMobile ) ) {
526 $unit = ! empty( $value->lineHeightUnitMobile ) ? $value->lineHeightUnitMobile : 'px';
527 $css .= " --ablocks-{$id}-line-height-mobile: {$value->lineHeightMobile}{$unit};\n";
528 }
529 if ( ! empty( $value->letterSpacingMobile ) ) {
530 $unit = ! empty( $value->letterSpacingUnitMobile ) ? $value->letterSpacingUnitMobile : 'px';
531 $css .= " --ablocks-{$id}-letter-spacing-mobile: {$value->letterSpacingMobile}{$unit};\n";
532 }
533 if ( ! empty( $value->wordSpacingMobile ) ) {
534 $unit = ! empty( $value->wordSpacingUnitMobile ) ? $value->wordSpacingUnitMobile : 'px';
535 $css .= " --ablocks-{$id}-word-spacing-mobile: {$value->wordSpacingMobile}{$unit};\n";
536 }
537 }//end if
538 }//end foreach
539
540 $css .= "}\n";
541
542 if ( ! is_admin() && ! Helper::is_gutenberg_editor() ) {
543 $css .= $this->get_common_css();
544 }
545 wp_add_inline_style( 'ablocks-editor-global-styles', $css );
546 wp_add_inline_style( 'ablocks-common-style', $css );
547 }
548
549 public function editor_google_fonts() {
550 $saved_fonts = json_decode( get_option( ABLOCKS_FONTS_SETTINGS_NAME, '{}' ), true );
551 if ( empty( $saved_fonts ) || ! is_array( $saved_fonts ) ) {
552 return;
553 }
554 $url = $this->build_google_fonts_url( $saved_fonts );
555 wp_enqueue_style( 'ablocks-editor-google-fonts', $url, array(), null );
556 }
557
558 public function add_editor_inline_css() {
559 $custom_css = $this->get_common_css( '.editor-styles-wrapper ' );
560 add_theme_support( 'editor-styles' );
561 add_editor_style();
562 wp_add_inline_style( 'wp-block-library', $custom_css );
563 }
564
565 public function get_common_css( $parent_selector = '' ) {
566 global $ablocks_settings;
567 $attributes = [
568 'global_body_text_color' => $ablocks_settings->global_body_text_color,
569 'global_body_typography' => $ablocks_settings->global_body_typography,
570 'global_body_paragraph_space' => $ablocks_settings->global_body_paragraph_space,
571 'global_link_color' => $ablocks_settings->global_link_color,
572 'global_link_hover_color' => $ablocks_settings->global_link_hover_color,
573 'global_link_typography' => $ablocks_settings->global_link_typography,
574 'global_link_hover_typography' => $ablocks_settings->global_link_hover_typography,
575 'global_h1_color' => $ablocks_settings->global_h1_color,
576 'global_h1_typography' => $ablocks_settings->global_h1_typography,
577 'global_h2_color' => $ablocks_settings->global_h2_color,
578 'global_h2_typography' => $ablocks_settings->global_h2_typography,
579 'global_h3_color' => $ablocks_settings->global_h3_color,
580 'global_h3_typography' => $ablocks_settings->global_h3_typography,
581 'global_h4_color' => $ablocks_settings->global_h4_color,
582 'global_h4_typography' => $ablocks_settings->global_h4_typography,
583 'global_h5_color' => $ablocks_settings->global_h5_color,
584 'global_h5_typography' => $ablocks_settings->global_h5_typography,
585 'global_h6_color' => $ablocks_settings->global_h6_color,
586 'global_h6_typography' => $ablocks_settings->global_h6_typography,
587 ];
588 $css_generator = new GlobalCssGenerator( $attributes );
589
590 $css_generator->add_class_styles(
591 $parent_selector . 'body',
592 $css_generator->get_body_css( $attributes ),
593 $css_generator->get_body_css( $attributes, 'Tablet' ),
594 $css_generator->get_body_css( $attributes, 'Mobile' )
595 );
596 $css_generator->add_class_styles(
597 $parent_selector . 'p',
598 $css_generator->get_body_paragraph_css( $attributes ),
599 $css_generator->get_body_paragraph_css( $attributes, 'Tablet' ),
600 $css_generator->get_body_paragraph_css( $attributes, 'Mobile' )
601 );
602 $css_generator->add_class_styles(
603 $parent_selector . 'a',
604 $css_generator->get_body_anchor_css( $attributes ),
605 $css_generator->get_body_anchor_css( $attributes, 'Tablet' ),
606 $css_generator->get_body_anchor_css( $attributes, 'Mobile' )
607 );
608 $css_generator->add_class_styles(
609 $parent_selector . 'a:hover',
610 $css_generator->get_body_anchor_hover_css( $attributes ),
611 $css_generator->get_body_anchor_hover_css( $attributes, 'Tablet' ),
612 $css_generator->get_body_anchor_hover_css( $attributes, 'Mobile' )
613 );
614 $css_generator->add_class_styles(
615 $parent_selector . 'h1',
616 $css_generator->get_global_h1_css( $attributes ),
617 $css_generator->get_global_h1_css( $attributes, 'Tablet' ),
618 $css_generator->get_global_h1_css( $attributes, 'Mobile' )
619 );
620 $css_generator->add_class_styles(
621 $parent_selector . 'h2',
622 $css_generator->get_global_h2_css( $attributes ),
623 $css_generator->get_global_h2_css( $attributes, 'Tablet' ),
624 $css_generator->get_global_h2_css( $attributes, 'Mobile' )
625 );
626 $css_generator->add_class_styles(
627 $parent_selector . 'h3',
628 $css_generator->get_global_h3_css( $attributes ),
629 $css_generator->get_global_h3_css( $attributes, 'Tablet' ),
630 $css_generator->get_global_h3_css( $attributes, 'Mobile' )
631 );
632 $css_generator->add_class_styles(
633 $parent_selector . 'h4',
634 $css_generator->get_global_h4_css( $attributes ),
635 $css_generator->get_global_h4_css( $attributes, 'Tablet' ),
636 $css_generator->get_global_h4_css( $attributes, 'Mobile' )
637 );
638 $css_generator->add_class_styles(
639 $parent_selector . 'h5',
640 $css_generator->get_global_h5_css( $attributes ),
641 $css_generator->get_global_h5_css( $attributes, 'Tablet' ),
642 $css_generator->get_global_h5_css( $attributes, 'Mobile' )
643 );
644 $css_generator->add_class_styles(
645 $parent_selector . 'h6',
646 $css_generator->get_global_h6_css( $attributes ),
647 $css_generator->get_global_h6_css( $attributes, 'Tablet' ),
648 $css_generator->get_global_h6_css( $attributes, 'Mobile' )
649 );
650 return $css_generator->generate_css();
651 }
652
653
654 public function is_assets_generated() {
655 $file_name = $this->current_page_slug;
656 $css_file_path = $this->FileUpload->get_file_path( $file_name . '.min.css' );
657 return file_exists( $css_file_path );
658 }
659
660 public function set_current_page_template_part( $content, $block ) {
661 if ( ! isset( $block['blockName'] ) && is_array( $block ) ) {
662 foreach ( $block as $block_item ) {
663 if ( ! empty( $block_item['blockName'] ) && strpos( $block_item['blockName'], 'ablocks/' ) !== false ) {
664 $this->current_page_blocks[] = $block_item;
665 }
666 }
667 }
668 if ( ! empty( $block['blockName'] ) && strpos( $block['blockName'], 'ablocks/' ) !== false ) {
669 $this->current_page_blocks[] = $block;
670 }
671 return $content;
672 }
673
674 public function set_theme_builder_locations( $args ) {
675 $this->theme_builder_locations = $args;
676 }
677 }
678