PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.13
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.13
2.12.8 2.12.7 2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 All 98 releases
sureforms / modules / gutenberg / classes / class-spec-gb-helper.php

class-spec-gb-helper.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 0.0.13, at modules/gutenberg/classes/class-spec-gb-helper.php

1,100 lines 30.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sureforms Gb Helper.
4 *
5 * @package Sureforms
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 if ( ! class_exists( 'Spec_Gb_Helper' ) ) {
13
14 /**
15 * Class Spec_Gb_Helper.
16 */
17 final class Spec_Gb_Helper {
18
19
20 /**
21 * Member Variable
22 *
23 * @since 0.0.1
24 * @var instance
25 */
26 private static $instance;
27
28 /**
29 * Member Variable
30 *
31 * @since 0.0.1
32 * @var instance
33 */
34 public static $block_list;
35
36 /**
37 * Current Block List
38 *
39 * @since 0.0.1
40 * @var current_block_list
41 */
42 public static $current_block_list = [];
43
44 /**
45 * Page Blocks Variable
46 *
47 * @since 0.0.1
48 * @var instance
49 */
50 public static $page_blocks;
51
52 /**
53 * Stylesheet
54 *
55 * @since 0.0.1
56 * @var stylesheet
57 */
58 public static $stylesheet;
59
60 /**
61 * Script
62 *
63 * @since 0.0.1
64 * @var script
65 */
66 public static $script;
67
68 /**
69 * Sureforms Block Flag
70 *
71 * @since 0.0.1
72 * @var srfm_flag
73 */
74 public static $srfm_flag = false;
75
76 /**
77 * Static CSS Added Array
78 *
79 * @since 0.0.1
80 * @var array
81 */
82 public $static_css_blocks = [];
83
84 /**
85 * Google fonts to enqueue
86 *
87 * @since 0.0.1
88 * @var array
89 */
90 public static $gfonts = [];
91
92 /**
93 * As our svg icon is too long array so we will divide that into number of icon chunks.
94 *
95 * @var int
96 * @since 0.0.1
97 */
98 public static $number_of_icon_chunks = 4;
99
100 /**
101 * Store Json variable
102 *
103 * @since 0.0.1
104 * @var instance
105 */
106 public static $icon_json;
107
108 /**
109 * We have icon list in chunks in this variable we will merge all insides array into one single array.
110 *
111 * @var array
112 * @since 0.0.1
113 */
114 public static $icon_array_merged = [];
115
116 /**
117 * Initiator
118 *
119 * @since 0.0.1
120 */
121 public static function get_instance() {
122 if ( ! isset( self::$instance ) ) {
123 self::$instance = new self();
124 }
125 return self::$instance;
126 }
127
128 /**
129 * Constructor
130 */
131 public function __construct() {
132 require SRFM_DIR . 'modules/gutenberg/classes/class-spec-block-config.php';
133 require SRFM_DIR . 'modules/gutenberg/classes/class-spec-block-helper.php';
134 require SRFM_DIR . 'modules/gutenberg/classes/class-spec-block-js.php';
135
136 self::$block_list = Spec_Block_Config::get_block_attributes();
137
138 add_action( 'wp', [ $this, 'wp_actions' ], 10 );
139
140 add_filter( 'render_block', [ $this, 'generate_render_styles' ], 10, 2 );
141 }
142
143 /**
144 * Get form id content.
145 *
146 * @param string $id form id.
147 * @since 0.0.1
148 * @return void
149 */
150 public function form_content_by_id( $id ) {
151 $args = [
152 'p' => $id,
153 'post_type' => 'sureforms_form',
154 ];
155 $loop = new WP_Query( $args );
156
157 if ( isset( $loop->posts[0] ) && $loop->posts[0] ) {
158
159 /**
160 * Filters the post to build stylesheet for.
161 *
162 * @param \WP_Post $this_post The global post.
163 */
164 $this_post = apply_filters( 'srfm_post_for_stylesheet', $loop->posts[0] );
165
166 $this->get_generated_stylesheet( $this_post );
167 }
168 }
169
170 /**
171 * Render function.
172 *
173 * @param string $block_content Entire Block Content.
174 * @param array $block Block Properties As An Array.
175 * @return string
176 */
177 public function generate_render_styles( $block_content, $block ) {
178
179 if ( isset( $block['blockName'] ) && ( 'srfm/form' === $block['blockName'] ) ) {
180
181 if ( isset( $block['attrs']['id'] ) && $block['attrs']['id'] ) {
182 self::form_content_by_id( $block['attrs']['id'] );
183 }
184 }
185
186 if ( isset( $block['blockName'] ) && ( 'core/shortcode' === $block['blockName'] ) ) {
187
188 if ( isset( $block['innerHTML'] ) ) {
189 $start_explode = ( explode( 'form-id="', $block['innerHTML'] ) );
190 if ( isset( $start_explode[1] ) ) {
191 $end_explode = explode( '"', $start_explode[1] );
192 if ( isset( $end_explode ) ) {
193 self::form_content_by_id( $end_explode[0] );
194 }
195 }
196 }
197 }
198
199 return $block_content;
200 }
201
202 /**
203 * WP Actions.
204 */
205 public function wp_actions() {
206 $this->generate_assets();
207 add_action( 'wp_enqueue_scripts', [ $this, 'block_assets' ], 10 );
208 add_action( 'wp_head', [ $this, 'frontend_gfonts' ], 120 );
209 add_action( 'wp_head', [ $this, 'print_stylesheet' ], 80 );
210 add_action( 'wp_footer', [ $this, 'print_script' ], 1000 );
211 }
212
213 /**
214 * Load the front end Google Fonts.
215 */
216 public function frontend_gfonts() {
217
218 if ( empty( self::$gfonts ) ) {
219 return;
220 }
221 $show_google_fonts = apply_filters( 'srfm_blocks_show_google_fonts', true );
222 if ( ! $show_google_fonts ) {
223 return;
224 }
225 $link = '';
226 $subsets = [];
227 foreach ( self::$gfonts as $key => $gfont_values ) {
228 if ( ! empty( $link ) ) {
229 $link .= '%7C'; // Append a new font to the string.
230 }
231 $link .= $gfont_values['fontfamily'];
232 if ( ! empty( $gfont_values['fontvariants'] ) ) {
233 $link .= ':';
234 $link .= implode( ',', $gfont_values['fontvariants'] );
235 }
236 if ( ! empty( $gfont_values['fontsubsets'] ) ) {
237 foreach ( $gfont_values['fontsubsets'] as $subset ) {
238 if ( ! in_array( $subset, $subsets, true ) ) {
239 array_push( $subsets, $subset );
240 }
241 }
242 }
243 }
244 if ( ! empty( $subsets ) ) {
245 $link .= '&amp;subset=' . implode( ',', $subsets );
246 }
247
248 if ( isset( $link ) && ! empty( $link ) ) {
249 wp_enqueue_style( 'srfm-google-fonts', '//fonts.googleapis.com/css?family=' . esc_attr( str_replace( '|', '%7C', $link ) ), [], SRFM_VER, 'all' );
250 }
251 }
252
253 /**
254 * Set alignment css function.
255 *
256 * @param string $align passed.
257 * @since 0.0.1
258 * @return array
259 */
260 public static function alignment_css( $align ) {
261 $align_css = [];
262 switch ( $align ) {
263 case 'left':
264 $align_css = [
265 'margin-left' => 0,
266 'margin-right' => 'auto',
267 ];
268 break;
269 case 'center':
270 $align_css = [
271 'margin-left' => 'auto',
272 'margin-right' => 'auto',
273 ];
274 break;
275 case 'right':
276 $align_css = [
277 'margin-right' => 0,
278 'margin-left' => 'auto',
279 ];
280 break;
281 }
282 return $align_css;
283 }
284
285 /**
286 * Print the Script in footer.
287 */
288 public function print_script() {
289
290 if ( is_null( self::$script ) || '' === self::$script ) {
291 return;
292 }
293
294 $js_uri = SRFM_URL . 'modules/gutenberg/assets/js/';
295
296 wp_enqueue_script( SRFM_SLUG . '-gutenberg', $js_uri . 'gutenberg.js', [], SRFM_VER, true );
297
298 $srfm_inline_js = self::$script;
299 if ( $srfm_inline_js ) {
300 wp_add_inline_script( SRFM_SLUG . '-gutenberg', $srfm_inline_js );
301 }
302
303 }
304
305 /**
306 * Print the Stylesheet in header.
307 */
308 public function print_stylesheet() {
309
310 if ( is_null( self::$stylesheet ) || '' === self::$stylesheet ) {
311 return;
312 }
313
314 $css_uri = SRFM_URL . 'modules/gutenberg/assets/css/';
315
316 wp_enqueue_style( SRFM_SLUG . '-gutenberg', $css_uri . 'gutenberg.css', [], SRFM_VER );
317
318 $srfm_inline_css = self::$stylesheet;
319 if ( $srfm_inline_css ) {
320 wp_add_inline_style( SRFM_SLUG . '-gutenberg', $srfm_inline_css );
321 }
322
323 }
324
325 /**
326 * Generates stylesheet and appends in head tag.
327 *
328 * @since 0.0.1
329 */
330 public function generate_assets() {
331
332 $this_post = [];
333
334 global $post;
335 $this_post = $post;
336
337 if ( ! is_object( $this_post ) ) {
338 return;
339 }
340
341 /**
342 * Filters the post to build stylesheet for.
343 *
344 * @param \WP_Post $this_post The global post.
345 */
346 $this_post = apply_filters( 'srfm_post_for_stylesheet', $this_post );
347
348 $this->get_generated_stylesheet( $this_post );
349
350 }
351
352 /**
353 * Generates stylesheet in loop.
354 *
355 * @param object $this_post Current Post Object.
356 * @since 0.0.1
357 */
358 public function get_generated_stylesheet( $this_post ) {
359
360 if ( is_object( $this_post ) && isset( $this_post->ID ) && has_blocks( $this_post->ID ) && isset( $this_post->post_content ) ) {
361
362 $blocks = $this->parse( $this_post->post_content );
363
364 $count = count( $blocks );
365 for ( $i = 0; $i < $count; $i++ ) {
366 if ( isset( $blocks[ $i ]['blockName'] ) && 'srfm/form' === $blocks[ $i ]['blockName'] ) {
367 if ( isset( $blocks[ $i ]['attrs']['id'] ) && $blocks[ $i ]['attrs']['id'] ) {
368 $form_post = get_post( $blocks[ $i ]['attrs']['id'] );
369 if ( $form_post ) {
370 $blocks = array_merge( $blocks, $this->parse( $form_post->post_content ) );
371 }
372 }
373 }
374
375 if ( has_shortcode( $this_post->post_content, 'sureforms' ) && isset( $blocks[ $i ]['blockName'] ) && 'core/shortcode' === $blocks[ $i ]['blockName'] ) {
376 if ( isset( $blocks[ $i ]['innerHTML'] ) && $blocks[ $i ]['innerHTML'] && str_contains( $blocks[ $i ]['innerHTML'], '[sureforms' ) ) {
377 $form_id = preg_replace( '/\D+/', '', $blocks[ $i ]['innerHTML'] );
378 if ( $form_id ) {
379 $form_post = get_post( $form_id );
380 if ( $form_post ) {
381 $blocks = array_merge( $blocks, $this->parse( $form_post->post_content ) );
382 }
383 }
384 }
385 }
386 }
387
388 self::$page_blocks = $blocks;
389
390 if ( ! is_array( $blocks ) || empty( $blocks ) ) {
391 return;
392 }
393
394 $assets = $this->get_assets( $blocks );
395
396 self::$stylesheet .= $assets['css'];
397 self::$script .= $assets['js'];
398 }
399 }
400
401 /**
402 * Enqueue Gutenberg block assets for both frontend + backend.
403 *
404 * @since 0.0.1
405 */
406 public function block_assets() {
407
408 $block_list_for_assets = self::$current_block_list;
409
410 $blocks = Spec_Block_Config::get_block_attributes();
411
412 foreach ( $block_list_for_assets as $key => $curr_block_name ) {
413
414 $js_assets = ( isset( $blocks[ $curr_block_name ]['js_assets'] ) ) ? $blocks[ $curr_block_name ]['js_assets'] : [];
415
416 $css_assets = ( isset( $blocks[ $curr_block_name ]['css_assets'] ) ) ? $blocks[ $curr_block_name ]['css_assets'] : [];
417
418 foreach ( $js_assets as $asset_handle => $val ) {
419 // Scripts.
420 wp_enqueue_script( $val );
421 }
422
423 foreach ( $css_assets as $asset_handle => $val ) {
424 // Styles.
425 wp_enqueue_style( $val );
426 }
427 }
428
429 }
430
431 /**
432 * Parse Guten Block.
433 *
434 * @param string $content the content string.
435 * @since 0.0.1
436 */
437 public function parse( $content ) {
438
439 global $wp_version;
440
441 return ( version_compare( $wp_version, '5', '>=' ) ) ? parse_blocks( $content ) : gutenberg_parse_blocks( $content );
442 }
443
444 /**
445 * Generates stylesheet for reusable blocks.
446 *
447 * @param array $blocks Blocks array.
448 * @since 0.0.1
449 */
450 public function get_assets( $blocks ) {
451
452 $desktop = '';
453 $tablet = '';
454 $mobile = '';
455
456 $tab_styling_css = '';
457 $mob_styling_css = '';
458
459 $js = '';
460
461 foreach ( $blocks as $i => $block ) {
462
463 if ( is_array( $block ) ) {
464
465 if ( '' === $block['blockName'] ) {
466 continue;
467 }
468 if ( 'core/block' === $block['blockName'] ) {
469 $id = ( isset( $block['attrs']['ref'] ) ) ? $block['attrs']['ref'] : 0;
470
471 if ( $id ) {
472 $content = get_post_field( 'post_content', $id );
473
474 $reusable_blocks = $this->parse( $content );
475
476 $assets = $this->get_assets( $reusable_blocks );
477
478 self::$stylesheet .= $assets['css'];
479 self::$script .= $assets['js'];
480
481 }
482 } else {
483
484 $block_assets = $this->get_block_css_and_js( $block );
485
486 // Get CSS for the Block.
487 $css = $block_assets['css'];
488
489 if ( ! empty( $css['common'] ) ) {
490 $desktop .= $css['common'];
491 }
492
493 if ( isset( $css['desktop'] ) ) {
494 $desktop .= $css['desktop'];
495 $tablet .= $css['tablet'];
496 $mobile .= $css['mobile'];
497 }
498
499 $js .= $block_assets['js'];
500 }
501 }
502 }
503
504 if ( ! empty( $tablet ) ) {
505 $tab_styling_css .= '@media only screen and (max-width: ' . SRFM_TABLET_BREAKPOINT . 'px) {';
506 $tab_styling_css .= $tablet;
507 $tab_styling_css .= '}';
508 }
509
510 if ( ! empty( $mobile ) ) {
511 $mob_styling_css .= '@media only screen and (max-width: ' . SRFM_MOBILE_BREAKPOINT . 'px) {';
512 $mob_styling_css .= $mobile;
513 $mob_styling_css .= '}';
514 }
515
516 return [
517 'css' => $desktop . $tab_styling_css . $mob_styling_css,
518 'js' => $js,
519 ];
520 }
521
522 /**
523 * Get Typography Dynamic CSS.
524 *
525 * @param array $attr The Attribute array.
526 * @param string $slug The field slug.
527 * @param string $selector The selector array.
528 * @param array $combined_selectors The combined selector array.
529 * @since 0.0.1
530 * @return bool|string
531 */
532 public static function get_typography_css( $attr, $slug, $selector, $combined_selectors ) {
533
534 $typo_css_desktop = [];
535 $typo_css_tablet = [];
536 $typo_css_mobile = [];
537
538 $already_selectors_desktop = ( isset( $combined_selectors['desktop'][ $selector ] ) ) ? $combined_selectors['desktop'][ $selector ] : [];
539 $already_selectors_tablet = ( isset( $combined_selectors['tablet'][ $selector ] ) ) ? $combined_selectors['tablet'][ $selector ] : [];
540 $already_selectors_mobile = ( isset( $combined_selectors['mobile'][ $selector ] ) ) ? $combined_selectors['mobile'][ $selector ] : [];
541
542 $family_slug = ( '' === $slug ) ? 'fontFamily' : $slug . 'FontFamily';
543 $weight_slug = ( '' === $slug ) ? 'fontWeight' : $slug . 'FontWeight';
544 $transform_slug = ( '' === $slug ) ? 'fontTransform' : $slug . 'Transform';
545 $decoration_slug = ( '' === $slug ) ? 'fontDecoration' : $slug . 'Decoration';
546 $style_slug = ( '' === $slug ) ? 'fontStyle' : $slug . 'FontStyle';
547
548 $l_ht_slug = ( '' === $slug ) ? 'lineHeight' : $slug . 'LineHeight';
549 $f_sz_slug = ( '' === $slug ) ? 'fontSize' : $slug . 'FontSize';
550 $l_ht_type_slug = ( '' === $slug ) ? 'lineHeightType' : $slug . 'LineHeightType';
551 $f_sz_type_slug = ( '' === $slug ) ? 'fontSizeType' : $slug . 'FontSizeType';
552 $l_sp_slug = ( '' === $slug ) ? 'letterSpacing' : $slug . 'LetterSpacing';
553 $l_sp_type_slug = ( '' === $slug ) ? 'letterSpacingType' : $slug . 'LetterSpacingType';
554
555 $text_transform = isset( $attr[ $transform_slug ] ) ? $attr[ $transform_slug ] : 'normal';
556 $text_decoration = isset( $attr[ $decoration_slug ] ) ? $attr[ $decoration_slug ] : 'none';
557 $font_style = isset( $attr[ $style_slug ] ) ? $attr[ $style_slug ] : 'normal';
558
559 $typo_css_desktop[ $selector ] = [
560 'font-family' => $attr[ $family_slug ],
561 'text-transform' => $text_transform,
562 'text-decoration' => $text_decoration,
563 'font-style' => $font_style,
564 'font-weight' => $attr[ $weight_slug ],
565 'font-size' => ( isset( $attr[ $f_sz_slug ] ) ) ? self::get_css_value( $attr[ $f_sz_slug ], $attr[ $f_sz_type_slug ] ) : '',
566 'line-height' => ( isset( $attr[ $l_ht_slug ] ) ) ? self::get_css_value( $attr[ $l_ht_slug ], $attr[ $l_ht_type_slug ] ) : '',
567 'letter-spacing' => ( isset( $attr[ $l_sp_slug ] ) ) ? self::get_css_value( $attr[ $l_sp_slug ], $attr[ $l_sp_type_slug ] ) : '',
568 ];
569
570 $typo_css_desktop[ $selector ] = array_merge(
571 $typo_css_desktop[ $selector ],
572 $already_selectors_desktop
573 );
574
575 $typo_css_tablet[ $selector ] = [
576 'font-size' => ( isset( $attr[ $f_sz_slug . 'Tablet' ] ) ) ? self::get_css_value( $attr[ $f_sz_slug . 'Tablet' ], $attr[ $f_sz_type_slug ] ) : '',
577 'line-height' => ( isset( $attr[ $l_ht_slug . 'Tablet' ] ) ) ? self::get_css_value( $attr[ $l_ht_slug . 'Tablet' ], $attr[ $l_ht_type_slug ] ) : '',
578 'letter-spacing' => ( isset( $attr[ $l_sp_slug . 'Tablet' ] ) ) ? self::get_css_value( $attr[ $l_sp_slug . 'Tablet' ], $attr[ $l_sp_type_slug ] ) : '',
579 ];
580
581 $typo_css_tablet[ $selector ] = array_merge(
582 $typo_css_tablet[ $selector ],
583 $already_selectors_tablet
584 );
585
586 $typo_css_mobile[ $selector ] = [
587 'font-size' => ( isset( $attr[ $f_sz_slug . 'Mobile' ] ) ) ? self::get_css_value( $attr[ $f_sz_slug . 'Mobile' ], $attr[ $f_sz_type_slug ] ) : '',
588 'line-height' => ( isset( $attr[ $l_ht_slug . 'Mobile' ] ) ) ? self::get_css_value( $attr[ $l_ht_slug . 'Mobile' ], $attr[ $l_ht_type_slug ] ) : '',
589 'letter-spacing' => ( isset( $attr[ $l_sp_slug . 'Mobile' ] ) ) ? self::get_css_value( $attr[ $l_sp_slug . 'Mobile' ], $attr[ $l_sp_type_slug ] ) : '',
590 ];
591
592 $typo_css_mobile[ $selector ] = array_merge(
593 $typo_css_mobile[ $selector ],
594 $already_selectors_mobile
595 );
596
597 return [
598 'desktop' => array_merge(
599 $combined_selectors['desktop'],
600 $typo_css_desktop
601 ),
602 'tablet' => array_merge(
603 $combined_selectors['tablet'],
604 $typo_css_tablet
605 ),
606 'mobile' => array_merge(
607 $combined_selectors['mobile'],
608 $typo_css_mobile
609 ),
610 ];
611 }
612
613 /**
614 * Get CSS value
615 *
616 * Syntax:
617 *
618 * get_css_value( VALUE, UNIT );
619 *
620 * E.g.
621 *
622 * get_css_value( VALUE, 'em' );
623 *
624 * @param string $value CSS value.
625 * @param string $unit CSS unit.
626 * @since 0.0.1
627 */
628 public static function get_css_value( $value = '', $unit = '' ) {
629
630 $css_val = '';
631
632 if ( '' !== $value ) {
633 $css_val = esc_attr( $value ) . $unit;
634 }
635
636 return $css_val;
637 }
638
639 /**
640 * Parse CSS into correct CSS syntax.
641 *
642 * @param array $combined_selectors The combined selector array.
643 * @param string $id The selector ID.
644 * @since 0.0.1
645 */
646 public static function generate_all_css( $combined_selectors, $id ) {
647
648 return [
649 'desktop' => self::generate_css( $combined_selectors['desktop'], $id ),
650 'tablet' => self::generate_css( $combined_selectors['tablet'], $id ),
651 'mobile' => self::generate_css( $combined_selectors['mobile'], $id ),
652 ];
653 }
654
655 /**
656 * Generate the Box Shadow or Text Shadow CSS.
657 *
658 * For Text Shadow CSS:
659 * ( 'spread', 'position' ) should not be sent as params during the function call.
660 * ( 'spread_unit' ) will have no effect.
661 *
662 * For Box/Text Shadow Hover CSS:
663 * ( 'alt_color' ) should be set as the attribute used for ( 'color' ) in Box/Text Shadow Normal CSS.
664 *
665 * @param array $shadow_properties Array containing the necessary shadow properties.
666 * @return string The generated border CSS or an empty string on early return.
667 *
668 * @since 0.0.1
669 */
670 public static function generate_shadow_css( $shadow_properties ) {
671 // Get the Object Properties.
672 $horizontal = isset( $shadow_properties['horizontal'] ) ? $shadow_properties['horizontal'] : '';
673 $vertical = isset( $shadow_properties['vertical'] ) ? $shadow_properties['vertical'] : '';
674 $blur = isset( $shadow_properties['blur'] ) ? $shadow_properties['blur'] : '';
675 $spread = isset( $shadow_properties['spread'] ) ? $shadow_properties['spread'] : '';
676 $horizontal_unit = isset( $shadow_properties['horizontal_unit'] ) ? $shadow_properties['horizontal_unit'] : 'px';
677 $vertical_unit = isset( $shadow_properties['vertical_unit'] ) ? $shadow_properties['vertical_unit'] : 'px';
678 $blur_unit = isset( $shadow_properties['blur_unit'] ) ? $shadow_properties['blur_unit'] : 'px';
679 $spread_unit = isset( $shadow_properties['spread_unit'] ) ? $shadow_properties['spread_unit'] : 'px';
680 $color = isset( $shadow_properties['color'] ) ? $shadow_properties['color'] : '';
681 $position = isset( $shadow_properties['position'] ) ? $shadow_properties['position'] : 'outset';
682 $alt_color = isset( $shadow_properties['alt_color'] ) ? $shadow_properties['alt_color'] : '';
683
684 // Although optional, color is required for Sarafi on PC. Return early if color isn't set.
685 if ( ! $color && ! $alt_color ) {
686 return '';
687 }
688
689 // Get the CSS units for the number properties.
690
691 $horizontal = self::get_css_value( $horizontal, $horizontal_unit );
692 if ( '' === $horizontal ) {
693 $horizontal = 0;
694 }
695
696 $vertical = self::get_css_value( $vertical, $vertical_unit );
697 if ( '' === $vertical ) {
698 $vertical = 0;
699 }
700
701 $blur = self::get_css_value( $blur, $blur_unit );
702 if ( '' === $blur ) {
703 $blur = 0;
704 }
705
706 $spread = self::get_css_value( $spread, $spread_unit );
707 if ( '' === $spread ) {
708 $spread = 0;
709 }
710
711 // If all numeric unit values are exactly 0, don't render the CSS.
712 if ( ( 0 === $horizontal && 0 === $vertical ) && ( 0 === $blur && 0 === $spread ) ) {
713 return '';
714 }
715
716 // Return the CSS with horizontal, vertical, blur, and color - and conditionally render spread and position.
717 return (
718 $horizontal . ' ' . $vertical . ' ' . $blur . ( $spread ? " {$spread}" : '' ) . ' ' . ( $color ? $color : $alt_color ) . ( 'outset' === $position ? '' : " {$position}" )
719 );
720 }
721
722 /**
723 * Parse CSS into correct CSS syntax.
724 *
725 * @param array $selectors The block selectors.
726 * @param string $id The selector ID.
727 * @since 0.0.1
728 */
729 public static function generate_css( $selectors, $id ) {
730 $styling_css = '';
731
732 if ( ! empty( $selectors ) ) {
733 foreach ( $selectors as $key => $value ) {
734
735 $css = '';
736
737 foreach ( $value as $j => $val ) {
738
739 if ( 'font-family' === $j && 'Default' === $val ) {
740 continue;
741 }
742
743 if ( ! empty( $val ) && is_array( $val ) ) {
744 foreach ( $val as $key => $css_value ) {
745 $css .= $key . ': ' . $css_value . ';';
746 }
747 } elseif ( ! empty( $val ) || 0 === $val ) {
748 if ( 'font-family' === $j ) {
749 $css .= $j . ': "' . $val . '";';
750 } else {
751 $css .= $j . ': ' . $val . ';';
752 }
753 }
754 }
755
756 if ( ! empty( $css ) ) {
757 $styling_css .= $id;
758 $styling_css .= $key . '{';
759 $styling_css .= $css . '}';
760 }
761 }
762 }
763
764 return $styling_css;
765 }
766
767 /**
768 * Adds Google fonts all blocks.
769 *
770 * @param array $load_google_font the blocks attr.
771 * @param string $font_family the blocks attr.
772 * @param array $font_weight the blocks attr.
773 */
774 public static function blocks_google_font( $load_google_font, $font_family, $font_weight ) {
775
776 if ( true === $load_google_font ) {
777 if ( ! array_key_exists( $font_family, self::$gfonts ) ) {
778 $add_font = [
779 'fontfamily' => $font_family,
780 'fontvariants' => ( isset( $font_weight ) && ! empty( $font_weight ) ? [ $font_weight ] : [] ),
781 ];
782 self::$gfonts[ $font_family ] = $add_font;
783 } else {
784 if ( isset( $font_weight ) && ! empty( $font_weight ) && ! in_array( $font_weight, self::$gfonts[ $font_family ]['fontvariants'], true ) ) {
785 array_push( self::$gfonts[ $font_family ]['fontvariants'], $font_weight );
786 }
787 }
788 }
789 }
790
791 /**
792 * Generates CSS recurrsively.
793 *
794 * @param object $block The block object.
795 * @since 0.0.1
796 */
797 public function get_block_css_and_js( $block ) {
798
799 $block = (array) $block;
800
801 $name = $block['blockName'];
802 $css = [];
803 $js = '';
804 $block_id = '';
805
806 if ( isset( $name ) ) {
807
808 if ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) {
809 $blockattr = $block['attrs'];
810 if ( isset( $blockattr['block_id'] ) ) {
811 $block_id = $blockattr['block_id'];
812 }
813 }
814
815 self::$current_block_list[] = $name;
816
817 if ( strpos( $name, 'srfm/' ) !== false ) {
818 self::$srfm_flag = true;
819 }
820
821 switch ( $name ) {
822 case 'srfm/separator':
823 $css = Spec_Block_Helper::get_separator_css( $blockattr, $block_id );
824 Spec_Block_JS::blocks_separator_gfont( $blockattr );
825 break;
826
827 case 'srfm/image':
828 $css = Spec_Block_Helper::get_image_css( $blockattr, $block_id );
829 Spec_Block_JS::blocks_image_gfont( $blockattr );
830 break;
831
832 case 'srfm/icon':
833 $css = Spec_Block_Helper::get_icon_css( $blockattr, $block_id );
834 break;
835
836 case 'srfm/advanced-heading':
837 $css = Spec_Block_Helper::get_advanced_heading_css( $blockattr, $block_id );
838 Spec_Block_JS::blocks_advanced_heading_gfont( $blockattr );
839 break;
840
841 default:
842 // Nothing to do here.
843 break;
844 }
845
846 // Add static css here.
847 $block_css_arr = Spec_Block_Config::get_block_assets_css();
848
849 if ( isset( $block_css_arr[ $name ] ) && ! in_array( $block_css_arr[ $name ]['name'], $this->static_css_blocks, true ) ) {
850
851 $common_css = [
852 'common' => $this->get_block_static_css( $block_css_arr[ $name ]['name'] ),
853 ];
854 $css += $common_css;
855 }
856
857 if ( isset( $block['innerBlocks'] ) ) {
858 foreach ( $block['innerBlocks'] as $j => $inner_block ) {
859 if ( 'core/block' === $inner_block['blockName'] ) {
860 $id = ( isset( $inner_block['attrs']['ref'] ) ) ? $inner_block['attrs']['ref'] : 0;
861
862 if ( $id ) {
863 $content = get_post_field( 'post_content', $id );
864
865 $reusable_blocks = $this->parse( $content );
866
867 $assets = $this->get_assets( $reusable_blocks );
868
869 self::$stylesheet .= $assets['css'];
870 self::$script .= $assets['js'];
871 }
872 } else {
873 // Get CSS for the Block.
874 $inner_assets = $this->get_block_css_and_js( $inner_block );
875 $inner_block_css = $inner_assets['css'];
876
877 $css_common = ( isset( $css['common'] ) ? $css['common'] : '' );
878 $css_desktop = ( isset( $css['desktop'] ) ? $css['desktop'] : '' );
879 $css_tablet = ( isset( $css['tablet'] ) ? $css['tablet'] : '' );
880 $css_mobile = ( isset( $css['mobile'] ) ? $css['mobile'] : '' );
881
882 if ( isset( $inner_block_css['common'] ) ) {
883 $css['common'] = $css_common . $inner_block_css['common'];
884 }
885
886 if ( isset( $inner_block_css['desktop'] ) ) {
887 $css['desktop'] = $css_desktop . $inner_block_css['desktop'];
888 $css['tablet'] = $css_tablet . $inner_block_css['tablet'];
889 $css['mobile'] = $css_mobile . $inner_block_css['mobile'];
890 }
891
892 $js .= $inner_assets['js'];
893 }
894 }
895 }
896
897 self::$current_block_list = array_unique( self::$current_block_list );
898 }
899
900 return [
901 'css' => $css,
902 'js' => $js,
903 ];
904
905 }
906
907 /**
908 * Get block dynamic CSS selector with filters applied for extending it.
909 *
910 * @param string $block_name Block name to filter.
911 * @param array $selectors Array of selectors to filter.
912 * @param array $attr Attributes.
913 * @return array Combined selectors array.
914 * @since 0.0.1
915 */
916 public static function get_combined_selectors( $block_name, $selectors, $attr ) {
917 if ( ! is_array( $selectors ) ) {
918 return $selectors;
919 }
920
921 $combined_selectors = [];
922
923 foreach ( $selectors as $key => $selector ) {
924 $hook_prefix = ( 'desktop' === $key ) ? '' : '_' . $key;
925 $combined_selectors[ $key ] = apply_filters( 'srfm_' . $block_name . $hook_prefix . '_styling', $selector, $attr );
926 }
927
928 return $combined_selectors;
929 }
930
931 /**
932 * Get Static CSS of Block.
933 *
934 * @param string $block_name Block Name.
935 *
936 * @return string Static CSS.
937 * @since 0.0.1
938 */
939 public function get_block_static_css( $block_name ) {
940
941 $css = '';
942
943 $block_static_css_path = SRFM_DIR . 'modules/gutenberg/assets/css/blocks/' . $block_name . '.css';
944
945 if ( file_exists( $block_static_css_path ) ) {
946
947 $file_system = spec_filesystem();
948
949 $css = $file_system->get_contents( $block_static_css_path );
950 }
951
952 array_push( $this->static_css_blocks, $block_name );
953
954 return $css;
955 }
956
957 /**
958 * Border attribute generation Function.
959 *
960 * @since 0.0.1
961 * @param array $prefix Attribute Prefix.
962 * @param array $default_args default attributes args.
963 * @return array
964 */
965 public function generate_php_border_attribute( $prefix, $default_args = [] ) {
966
967 $border_attr = [];
968
969 $device = [ '', 'Tablet', 'Mobile' ];
970
971 foreach ( $device as $slug => $data ) {
972
973 $border_attr[ "{$prefix}BorderTopWidth{$data}" ] = [
974 'type' => 'number',
975 ];
976 $border_attr[ "{$prefix}BorderLeftWidth{$data}" ] = [
977 'type' => 'number',
978 ];
979 $border_attr[ "{$prefix}BorderRightWidth{$data}" ] = [
980 'type' => 'number',
981 ];
982 $border_attr[ "{$prefix}BorderBottomWidth{$data}" ] = [
983 'type' => 'number',
984 ];
985 $border_attr[ "{$prefix}BorderTopLeftRadius{$data}" ] = [
986 'type' => 'number',
987 ];
988 $border_attr[ "{$prefix}BorderTopRightRadius{$data}" ] = [
989 'type' => 'number',
990 ];
991 $border_attr[ "{$prefix}BorderBottomLeftRadius{$data}" ] = [
992 'type' => 'number',
993 ];
994 $border_attr[ "{$prefix}BorderBottomRightRadius{$data}" ] = [
995 'type' => 'number',
996 ];
997 $border_attr[ "{$prefix}BorderRadiusUnit{$data}" ] = [
998 'type' => 'number',
999 ];
1000 }
1001
1002 $border_attr[ "{$prefix}BorderStyle" ] = [
1003 'type' => 'string',
1004 ];
1005 $border_attr[ "{$prefix}BorderColor" ] = [
1006 'type' => 'string',
1007 ];
1008 $border_attr[ "{$prefix}BorderHColor" ] = [
1009 'type' => 'string',
1010 ];
1011 $border_attr[ "{$prefix}BorderLink" ] = [
1012 'type' => 'boolean',
1013 'default' => true,
1014 ];
1015 $border_attr[ "{$prefix}BorderRadiusLink" ] = [
1016 'type' => 'boolean',
1017 'default' => true,
1018 ];
1019
1020 return $border_attr;
1021 }
1022
1023 /**
1024 * Get Json Data.
1025 *
1026 * @since 0.0.1
1027 * @return array
1028 */
1029 public static function backend_load_font_awesome_icons() {
1030
1031 if ( null !== self::$icon_json ) {
1032 return self::$icon_json;
1033 }
1034
1035 $icons_chunks = [];
1036 for ( $i = 0; $i < self::$number_of_icon_chunks; $i++ ) {
1037 $json_file = SRFM_DIR . "modules/gutenberg/icons/icons-v6-{$i}.php";
1038
1039 if ( file_exists( $json_file ) ) {
1040 $icons_chunks[] = include $json_file;
1041 }
1042 }
1043
1044 if ( empty( $icons_chunks ) ) {
1045 return [];
1046 }
1047
1048 self::$icon_json = $icons_chunks;
1049 return self::$icon_json;
1050 }
1051
1052 /**
1053 * Generate SVG.
1054 *
1055 * @since 0.0.1
1056 * @param array $icon Decoded fontawesome json file data.
1057 * @param bool $return The parameter decides whether to return or echo the svg markup.
1058 */
1059 public static function render_svg_html( $icon, $return = false ) {
1060 $icon = sanitize_text_field( esc_attr( $icon ) );
1061
1062 $json = self::backend_load_font_awesome_icons();
1063
1064 if ( ! empty( $json ) ) {
1065 if ( empty( $icon_array_merged ) ) {
1066 foreach ( $json as $value ) {
1067 self::$icon_array_merged = array_merge( self::$icon_array_merged, $value );
1068 }
1069 }
1070 $json = self::$icon_array_merged;
1071 }
1072
1073 $icon_brand_or_solid = isset( $json[ $icon ]['svg']['brands'] ) ? $json[ $icon ]['svg']['brands'] : ( isset( $json[ $icon ]['svg']['solid'] ) ? $json[ $icon ]['svg']['solid'] : [] );
1074 $path = isset( $icon_brand_or_solid['path'] ) ? $icon_brand_or_solid['path'] : '';
1075 $view = isset( $icon_brand_or_solid['width'] ) && isset( $icon_brand_or_solid['height'] ) ? '0 0 ' . $icon_brand_or_solid['width'] . ' ' . $icon_brand_or_solid['height'] : null;
1076
1077 if ( $path && $view ) {
1078 if ( $return ) {
1079 ob_start();
1080 ?>
1081 <svg xmlns="https://www.w3.org/2000/svg" viewBox= "<?php echo esc_attr( $view ); ?>"><path d="<?php echo esc_attr( $path ); ?>"></path></svg>
1082 <?php
1083 return ob_get_clean();
1084 } else {
1085 ?>
1086 <svg xmlns="https://www.w3.org/2000/svg" viewBox= "<?php echo esc_attr( $view ); ?>"><path d="<?php echo esc_attr( $path ); ?>"></path></svg>
1087 <?php
1088 }
1089 }
1090 }
1091
1092 }
1093
1094 /**
1095 * Prepare if class 'Spec_Gb_Helper' exist.
1096 * Kicking this off by calling 'get_instance()' method
1097 */
1098 Spec_Gb_Helper::get_instance();
1099 }
1100