| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cartflows Gb Helper. |
| 4 |
* |
| 5 |
* @package Cartflows |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! class_exists( 'Cartflows_Gb_Helper' ) ) { |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Cartflows_Gb_Helper. |
| 16 |
*/ |
| 17 |
final class Cartflows_Gb_Helper { |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Member Variable |
| 22 |
* |
| 23 |
* @since 1.6.15 |
| 24 |
* @var instance |
| 25 |
*/ |
| 26 |
private static $instance; |
| 27 |
|
| 28 |
/** |
| 29 |
* Member Variable |
| 30 |
* |
| 31 |
* @since 1.6.15 |
| 32 |
* @var instance |
| 33 |
*/ |
| 34 |
public static $block_list; |
| 35 |
|
| 36 |
/** |
| 37 |
* Current Block List |
| 38 |
* |
| 39 |
* @since 1.6.15 |
| 40 |
* @var current_block_list |
| 41 |
*/ |
| 42 |
public static $current_block_list = array(); |
| 43 |
|
| 44 |
/** |
| 45 |
* Page Blocks Variable |
| 46 |
* |
| 47 |
* @since 1.6.15 |
| 48 |
* @var instance |
| 49 |
*/ |
| 50 |
public static $page_blocks; |
| 51 |
|
| 52 |
/** |
| 53 |
* Stylesheet |
| 54 |
* |
| 55 |
* @since 1.6.15 |
| 56 |
* @var stylesheet |
| 57 |
*/ |
| 58 |
public static $stylesheet; |
| 59 |
|
| 60 |
/** |
| 61 |
* Script |
| 62 |
* |
| 63 |
* @since 1.6.15 |
| 64 |
* @var script |
| 65 |
*/ |
| 66 |
public static $script; |
| 67 |
|
| 68 |
/** |
| 69 |
* Cartflows Block Flag |
| 70 |
* |
| 71 |
* @since 1.6.15 |
| 72 |
* @var cf_flag |
| 73 |
*/ |
| 74 |
public static $cf_flag = false; |
| 75 |
|
| 76 |
/** |
| 77 |
* Google fonts to enqueue |
| 78 |
* |
| 79 |
* @var array |
| 80 |
*/ |
| 81 |
public static $gfonts = array(); |
| 82 |
|
| 83 |
/** |
| 84 |
* Initiator |
| 85 |
* |
| 86 |
* @since 1.6.15 |
| 87 |
*/ |
| 88 |
public static function get_instance() { |
| 89 |
if ( ! isset( self::$instance ) ) { |
| 90 |
self::$instance = new self(); |
| 91 |
} |
| 92 |
return self::$instance; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Constructor |
| 97 |
*/ |
| 98 |
public function __construct() { |
| 99 |
require CARTFLOWS_DIR . 'modules/gutenberg/classes/class-cartflows-block-config.php'; |
| 100 |
require CARTFLOWS_DIR . 'modules/gutenberg/classes/class-cartflows-block-helper.php'; |
| 101 |
require CARTFLOWS_DIR . 'modules/gutenberg/classes/class-cartflows-block-js.php'; |
| 102 |
|
| 103 |
add_action( 'init', array( $this, 'prepare_block_list' ) ); |
| 104 |
add_action( 'wp', array( $this, 'wp_actions' ), 10 ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Prepare Block List |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
public function prepare_block_list() { |
| 113 |
self::$block_list = Cartflows_Block_Config::get_block_attributes(); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* WP Actions. |
| 118 |
*/ |
| 119 |
public function wp_actions() { |
| 120 |
|
| 121 |
$this->generate_assets(); |
| 122 |
add_action( 'wp_head', array( $this, 'print_stylesheet' ), 80 ); |
| 123 |
|
| 124 |
if ( wcf()->utils->is_step_post_type() && ! Cartflows_Helper::is_instant_layout_enabled() ) { |
| 125 |
add_action( 'wp_enqueue_scripts', array( $this, 'block_assets' ), 10 ); |
| 126 |
add_action( 'wp_head', array( $this, 'frontend_gfonts' ), 120 ); |
| 127 |
add_action( 'wp_footer', array( $this, 'print_script' ), 1000 ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Load the front end Google Fonts. |
| 133 |
*/ |
| 134 |
public function frontend_gfonts() { |
| 135 |
|
| 136 |
if ( empty( self::$gfonts ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
$show_google_fonts = apply_filters( 'cf_blocks_show_google_fonts', true ); |
| 140 |
if ( ! $show_google_fonts ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
$link = ''; |
| 144 |
$subsets = array(); |
| 145 |
foreach ( self::$gfonts as $key => $gfont_values ) { |
| 146 |
if ( ! empty( $link ) ) { |
| 147 |
$link .= '%7C'; // Append a new font to the string. |
| 148 |
} |
| 149 |
$link .= $gfont_values['fontfamily']; |
| 150 |
if ( ! empty( $gfont_values['fontvariants'] ) ) { |
| 151 |
$link .= ':'; |
| 152 |
$link .= implode( ',', $gfont_values['fontvariants'] ); |
| 153 |
} |
| 154 |
if ( ! empty( $gfont_values['fontsubsets'] ) ) { |
| 155 |
foreach ( $gfont_values['fontsubsets'] as $subset ) { |
| 156 |
if ( ! in_array( $subset, $subsets, true ) ) { |
| 157 |
array_push( $subsets, $subset ); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
if ( ! empty( $subsets ) ) { |
| 163 |
$link .= '&subset=' . implode( ',', $subsets ); |
| 164 |
} |
| 165 |
|
| 166 |
if ( isset( $link ) && ! empty( $link ) ) { |
| 167 |
echo '<link id="cf_show_google_fonts" href="//fonts.googleapis.com/css?family=' . esc_attr( str_replace( '|', '%7C', $link ) ) . '" rel="stylesheet">'; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Print the Script in footer. |
| 173 |
*/ |
| 174 |
public function print_script() { |
| 175 |
|
| 176 |
if ( is_null( self::$script ) || '' === self::$script ) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
ob_start(); |
| 181 |
?> |
| 182 |
<script type="text/javascript" id="cf-script-frontend"><?php echo self::$script; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></script> |
| 183 |
<?php |
| 184 |
ob_end_flush(); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Print the Stylesheet in header. |
| 189 |
*/ |
| 190 |
public function print_stylesheet() { |
| 191 |
|
| 192 |
if ( is_null( self::$stylesheet ) || '' === self::$stylesheet ) { |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
ob_start(); |
| 197 |
?> |
| 198 |
<style id="cf-style-frontend"><?php echo self::$stylesheet; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></style> |
| 199 |
<?php |
| 200 |
ob_end_flush(); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
/** |
| 205 |
* Generates stylesheet and appends in head tag. |
| 206 |
* |
| 207 |
* @since 1.6.15 |
| 208 |
*/ |
| 209 |
public function generate_assets() { |
| 210 |
|
| 211 |
$this_post = array(); |
| 212 |
|
| 213 |
global $post; |
| 214 |
$this_post = $post; |
| 215 |
|
| 216 |
if ( ! is_object( $this_post ) ) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Filters the post to build stylesheet for. |
| 222 |
* |
| 223 |
* @param \WP_Post $this_post The global post. |
| 224 |
*/ |
| 225 |
$this_post = apply_filters( 'cf_post_for_stylesheet', $this_post ); |
| 226 |
|
| 227 |
$this->get_generated_stylesheet( $this_post ); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Generates stylesheet in loop. |
| 232 |
* |
| 233 |
* @param object $this_post Current Post Object. |
| 234 |
* @since 1.6.15 |
| 235 |
*/ |
| 236 |
public function get_generated_stylesheet( $this_post ) { |
| 237 |
|
| 238 |
if ( is_object( $this_post ) && isset( $this_post->ID ) && has_blocks( $this_post->ID ) && isset( $this_post->post_content ) ) { |
| 239 |
|
| 240 |
$blocks = $this->parse( $this_post->post_content ); |
| 241 |
|
| 242 |
self::$page_blocks = $blocks; |
| 243 |
|
| 244 |
if ( ! is_array( $blocks ) || empty( $blocks ) ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
$assets = $this->get_assets( $blocks ); |
| 249 |
|
| 250 |
self::$stylesheet .= $assets['css']; |
| 251 |
self::$script .= $assets['js']; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Enqueue Gutenberg block assets for both frontend + backend. |
| 257 |
* |
| 258 |
* @since 1.6.15 |
| 259 |
*/ |
| 260 |
public function block_assets() { |
| 261 |
|
| 262 |
$block_list_for_assets = self::$current_block_list; |
| 263 |
|
| 264 |
$blocks = Cartflows_Block_Config::get_block_attributes(); |
| 265 |
|
| 266 |
foreach ( $block_list_for_assets as $key => $curr_block_name ) { |
| 267 |
|
| 268 |
$js_assets = ( isset( $blocks[ $curr_block_name ]['js_assets'] ) ) ? $blocks[ $curr_block_name ]['js_assets'] : array(); |
| 269 |
|
| 270 |
$css_assets = ( isset( $blocks[ $curr_block_name ]['css_assets'] ) ) ? $blocks[ $curr_block_name ]['css_assets'] : array(); |
| 271 |
|
| 272 |
foreach ( $js_assets as $asset_handle => $val ) { |
| 273 |
// Scripts. |
| 274 |
wp_enqueue_script( $val ); |
| 275 |
} |
| 276 |
|
| 277 |
foreach ( $css_assets as $asset_handle => $val ) { |
| 278 |
// Styles. |
| 279 |
wp_enqueue_style( $val ); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Parse Guten Block. |
| 286 |
* |
| 287 |
* @param string $content the content string. |
| 288 |
* @since 1.6.15 |
| 289 |
*/ |
| 290 |
public function parse( $content ) { |
| 291 |
|
| 292 |
global $wp_version; |
| 293 |
|
| 294 |
return ( version_compare( $wp_version, '5', '>=' ) ) ? parse_blocks( $content ) : gutenberg_parse_blocks( $content ); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Generates stylesheet for reusable blocks. |
| 299 |
* |
| 300 |
* @param array $blocks Blocks array. |
| 301 |
* @since 1.6.15 |
| 302 |
*/ |
| 303 |
public function get_assets( $blocks ) { |
| 304 |
|
| 305 |
$desktop = ''; |
| 306 |
$tablet = ''; |
| 307 |
$mobile = ''; |
| 308 |
|
| 309 |
$tab_styling_css = ''; |
| 310 |
$mob_styling_css = ''; |
| 311 |
|
| 312 |
$js = ''; |
| 313 |
|
| 314 |
foreach ( $blocks as $i => $block ) { |
| 315 |
|
| 316 |
if ( is_array( $block ) ) { |
| 317 |
|
| 318 |
if ( '' === $block['blockName'] ) { |
| 319 |
continue; |
| 320 |
} |
| 321 |
if ( 'core/block' === $block['blockName'] ) { |
| 322 |
$id = ( isset( $block['attrs']['ref'] ) ) ? $block['attrs']['ref'] : 0; |
| 323 |
|
| 324 |
if ( $id ) { |
| 325 |
$content = get_post_field( 'post_content', $id ); |
| 326 |
|
| 327 |
$reusable_blocks = $this->parse( $content ); |
| 328 |
|
| 329 |
$assets = $this->get_assets( $reusable_blocks ); |
| 330 |
|
| 331 |
self::$stylesheet .= $assets['css']; |
| 332 |
self::$script .= $assets['js']; |
| 333 |
|
| 334 |
} |
| 335 |
} else { |
| 336 |
|
| 337 |
$block_assets = $this->get_block_css_and_js( $block ); |
| 338 |
// Get CSS for the Block. |
| 339 |
$css = $block_assets['css']; |
| 340 |
|
| 341 |
if ( isset( $css['desktop'] ) ) { |
| 342 |
$desktop .= $css['desktop']; |
| 343 |
$tablet .= $css['tablet']; |
| 344 |
$mobile .= $css['mobile']; |
| 345 |
} |
| 346 |
|
| 347 |
$js .= $block_assets['js']; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
if ( ! empty( $tablet ) ) { |
| 353 |
$tab_styling_css .= '@media only screen and (max-width: ' . CF_TABLET_BREAKPOINT . 'px) {'; |
| 354 |
$tab_styling_css .= $tablet; |
| 355 |
$tab_styling_css .= '}'; |
| 356 |
} |
| 357 |
|
| 358 |
if ( ! empty( $mobile ) ) { |
| 359 |
$mob_styling_css .= '@media only screen and (max-width: ' . CF_MOBILE_BREAKPOINT . 'px) {'; |
| 360 |
$mob_styling_css .= $mobile; |
| 361 |
$mob_styling_css .= '}'; |
| 362 |
} |
| 363 |
|
| 364 |
return array( |
| 365 |
'css' => $desktop . $tab_styling_css . $mob_styling_css, |
| 366 |
'js' => $js, |
| 367 |
); |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Get Typography Dynamic CSS. |
| 372 |
* |
| 373 |
* @param array $attr The Attribute array. |
| 374 |
* @param string $slug The field slug. |
| 375 |
* @param string $selector The selector array. |
| 376 |
* @param array $combined_selectors The combined selector array. |
| 377 |
* @since 1.6.15 |
| 378 |
* @return bool|string |
| 379 |
*/ |
| 380 |
public static function get_typography_css( $attr, $slug, $selector, $combined_selectors ) { |
| 381 |
|
| 382 |
$typo_css_desktop = array(); |
| 383 |
$typo_css_tablet = array(); |
| 384 |
$typo_css_mobile = array(); |
| 385 |
|
| 386 |
$already_selectors_desktop = ( isset( $combined_selectors['desktop'][ $selector ] ) ) ? $combined_selectors['desktop'][ $selector ] : array(); |
| 387 |
$already_selectors_tablet = ( isset( $combined_selectors['tablet'][ $selector ] ) ) ? $combined_selectors['tablet'][ $selector ] : array(); |
| 388 |
$already_selectors_mobile = ( isset( $combined_selectors['mobile'][ $selector ] ) ) ? $combined_selectors['mobile'][ $selector ] : array(); |
| 389 |
|
| 390 |
$family_slug = ( '' === $slug ) ? 'fontFamily' : $slug . 'FontFamily'; |
| 391 |
$weight_slug = ( '' === $slug ) ? 'fontWeight' : $slug . 'FontWeight'; |
| 392 |
$transform_slug = ( '' === $slug ) ? 'fontTransform' : $slug . 'Transform'; |
| 393 |
$decoration_slug = ( '' === $slug ) ? 'fontDecoration' : $slug . 'Decoration'; |
| 394 |
$style_slug = ( '' === $slug ) ? 'fontStyle' : $slug . 'FontStyle'; |
| 395 |
|
| 396 |
$l_ht_slug = ( '' === $slug ) ? 'lineHeight' : $slug . 'LineHeight'; |
| 397 |
$f_sz_slug = ( '' === $slug ) ? 'fontSize' : $slug . 'FontSize'; |
| 398 |
$l_ht_type_slug = ( '' === $slug ) ? 'lineHeightType' : $slug . 'LineHeightType'; |
| 399 |
$f_sz_type_slug = ( '' === $slug ) ? 'fontSizeType' : $slug . 'FontSizeType'; |
| 400 |
$l_sp_slug = ( '' === $slug ) ? 'letterSpacing' : $slug . 'LetterSpacing'; |
| 401 |
$l_sp_type_slug = ( '' === $slug ) ? 'letterSpacingType' : $slug . 'LetterSpacingType'; |
| 402 |
|
| 403 |
$text_transform = isset( $attr[ $transform_slug ] ) ? $attr[ $transform_slug ] : 'normal'; |
| 404 |
$text_decoration = isset( $attr[ $decoration_slug ] ) ? $attr[ $decoration_slug ] : 'none'; |
| 405 |
$font_style = isset( $attr[ $style_slug ] ) ? $attr[ $style_slug ] : 'normal'; |
| 406 |
|
| 407 |
$typo_css_desktop[ $selector ] = array( |
| 408 |
'font-family' => $attr[ $family_slug ], |
| 409 |
'text-transform' => $text_transform, |
| 410 |
'text-decoration' => $text_decoration, |
| 411 |
'font-style' => $font_style, |
| 412 |
'font-weight' => $attr[ $weight_slug ], |
| 413 |
'font-size' => ( isset( $attr[ $f_sz_slug ] ) ) ? self::get_css_value( $attr[ $f_sz_slug ], $attr[ $f_sz_type_slug ] ) : '', |
| 414 |
'line-height' => ( isset( $attr[ $l_ht_slug ] ) ) ? self::get_css_value( $attr[ $l_ht_slug ], $attr[ $l_ht_type_slug ] ) : '', |
| 415 |
'letter-spacing' => ( isset( $attr[ $l_sp_slug ] ) ) ? self::get_css_value( $attr[ $l_sp_slug ], $attr[ $l_sp_type_slug ] ) : '', |
| 416 |
); |
| 417 |
|
| 418 |
$typo_css_desktop[ $selector ] = array_merge( |
| 419 |
$typo_css_desktop[ $selector ], |
| 420 |
$already_selectors_desktop |
| 421 |
); |
| 422 |
|
| 423 |
$typo_css_tablet[ $selector ] = array( |
| 424 |
'font-size' => ( isset( $attr[ $f_sz_slug . 'Tablet' ] ) ) ? self::get_css_value( $attr[ $f_sz_slug . 'Tablet' ], $attr[ $f_sz_type_slug ] ) : '', |
| 425 |
'line-height' => ( isset( $attr[ $l_ht_slug . 'Tablet' ] ) ) ? self::get_css_value( $attr[ $l_ht_slug . 'Tablet' ], $attr[ $l_ht_type_slug ] ) : '', |
| 426 |
'letter-spacing' => ( isset( $attr[ $l_sp_slug . 'Tablet' ] ) ) ? self::get_css_value( $attr[ $l_sp_slug . 'Tablet' ], $attr[ $l_sp_type_slug ] ) : '', |
| 427 |
); |
| 428 |
|
| 429 |
$typo_css_tablet[ $selector ] = array_merge( |
| 430 |
$typo_css_tablet[ $selector ], |
| 431 |
$already_selectors_tablet |
| 432 |
); |
| 433 |
|
| 434 |
$typo_css_mobile[ $selector ] = array( |
| 435 |
'font-size' => ( isset( $attr[ $f_sz_slug . 'Mobile' ] ) ) ? self::get_css_value( $attr[ $f_sz_slug . 'Mobile' ], $attr[ $f_sz_type_slug ] ) : '', |
| 436 |
'line-height' => ( isset( $attr[ $l_ht_slug . 'Mobile' ] ) ) ? self::get_css_value( $attr[ $l_ht_slug . 'Mobile' ], $attr[ $l_ht_type_slug ] ) : '', |
| 437 |
'letter-spacing' => ( isset( $attr[ $l_sp_slug . 'Mobile' ] ) ) ? self::get_css_value( $attr[ $l_sp_slug . 'Mobile' ], $attr[ $l_sp_type_slug ] ) : '', |
| 438 |
); |
| 439 |
|
| 440 |
$typo_css_mobile[ $selector ] = array_merge( |
| 441 |
$typo_css_mobile[ $selector ], |
| 442 |
$already_selectors_mobile |
| 443 |
); |
| 444 |
|
| 445 |
return array( |
| 446 |
'desktop' => array_merge( |
| 447 |
$combined_selectors['desktop'], |
| 448 |
$typo_css_desktop |
| 449 |
), |
| 450 |
'tablet' => array_merge( |
| 451 |
$combined_selectors['tablet'], |
| 452 |
$typo_css_tablet |
| 453 |
), |
| 454 |
'mobile' => array_merge( |
| 455 |
$combined_selectors['mobile'], |
| 456 |
$typo_css_mobile |
| 457 |
), |
| 458 |
); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Get CSS value |
| 463 |
* |
| 464 |
* Syntax: |
| 465 |
* |
| 466 |
* get_css_value( VALUE, UNIT ); |
| 467 |
* |
| 468 |
* E.g. |
| 469 |
* |
| 470 |
* get_css_value( VALUE, 'em' ); |
| 471 |
* |
| 472 |
* @param string $value CSS value. |
| 473 |
* @param string $unit CSS unit. |
| 474 |
* @since 1.6.15 |
| 475 |
*/ |
| 476 |
public static function get_css_value( $value = '', $unit = '' ) { |
| 477 |
|
| 478 |
$css_val = ''; |
| 479 |
|
| 480 |
if ( '' !== $value ) { |
| 481 |
$css_val = esc_attr( $value ) . $unit; |
| 482 |
} |
| 483 |
|
| 484 |
return $css_val; |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* Parse CSS into correct CSS syntax. |
| 489 |
* |
| 490 |
* @param array $combined_selectors The combined selector array. |
| 491 |
* @param string $id The selector ID. |
| 492 |
* @since 1.6.15 |
| 493 |
*/ |
| 494 |
public static function generate_all_css( $combined_selectors, $id ) { |
| 495 |
|
| 496 |
return array( |
| 497 |
'desktop' => self::generate_css( $combined_selectors['desktop'], $id ), |
| 498 |
'tablet' => self::generate_css( $combined_selectors['tablet'], $id ), |
| 499 |
'mobile' => self::generate_css( $combined_selectors['mobile'], $id ), |
| 500 |
); |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* Parse CSS into correct CSS syntax. |
| 505 |
* |
| 506 |
* @param array $selectors The block selectors. |
| 507 |
* @param string $id The selector ID. |
| 508 |
* @since 1.6.15 |
| 509 |
*/ |
| 510 |
public static function generate_css( $selectors, $id ) { |
| 511 |
$styling_css = ''; |
| 512 |
|
| 513 |
if ( ! empty( $selectors ) ) { |
| 514 |
foreach ( $selectors as $key => $value ) { |
| 515 |
|
| 516 |
$css = ''; |
| 517 |
|
| 518 |
foreach ( $value as $j => $val ) { |
| 519 |
|
| 520 |
if ( 'font-family' === $j && 'Default' === $val ) { |
| 521 |
continue; |
| 522 |
} |
| 523 |
|
| 524 |
if ( ! empty( $val ) && is_array( $val ) ) { |
| 525 |
foreach ( $val as $key => $css_value ) { |
| 526 |
$css .= $key . ': ' . $css_value . ';'; |
| 527 |
} |
| 528 |
} elseif ( ! empty( $val ) || 0 === $val ) { |
| 529 |
if ( 'font-family' === $j ) { |
| 530 |
$css .= $j . ': "' . $val . '";'; |
| 531 |
} else { |
| 532 |
$css .= $j . ': ' . $val . ';'; |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
if ( ! empty( $css ) ) { |
| 538 |
$styling_css .= $id; |
| 539 |
$styling_css .= $key . '{'; |
| 540 |
$styling_css .= $css . '}'; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
return $styling_css; |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* Adds Google fonts all blocks. |
| 550 |
* |
| 551 |
* @param array $load_google_font the blocks attr. |
| 552 |
* @param string $font_family the blocks attr. |
| 553 |
* @param array $font_weight the blocks attr. |
| 554 |
* @param array $font_subset the blocks attr. |
| 555 |
*/ |
| 556 |
public static function blocks_google_font( $load_google_font, $font_family, $font_weight, $font_subset ) { |
| 557 |
|
| 558 |
if ( true === $load_google_font ) { |
| 559 |
if ( ! array_key_exists( $font_family, self::$gfonts ) ) { |
| 560 |
$add_font = array( |
| 561 |
'fontfamily' => $font_family, |
| 562 |
'fontvariants' => ( isset( $font_weight ) && ! empty( $font_weight ) ? array( $font_weight ) : array() ), |
| 563 |
'fontsubsets' => ( isset( $font_subset ) && ! empty( $font_subset ) ? array( $font_subset ) : array() ), |
| 564 |
); |
| 565 |
self::$gfonts[ $font_family ] = $add_font; |
| 566 |
} else { |
| 567 |
if ( isset( $font_weight ) && ! empty( $font_weight ) && ! in_array( $font_weight, self::$gfonts[ $font_family ]['fontvariants'], true ) ) { |
| 568 |
array_push( self::$gfonts[ $font_family ]['fontvariants'], $font_weight ); |
| 569 |
} |
| 570 |
if ( isset( $font_subset ) && ! empty( $font_subset ) && ! in_array( $font_subset, self::$gfonts[ $font_family ]['fontsubsets'], true ) ) { |
| 571 |
array_push( self::$gfonts[ $font_family ]['fontsubsets'], $font_subset ); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
/** |
| 579 |
* Generates CSS recurrsively. |
| 580 |
* |
| 581 |
* @param object $block The block object. |
| 582 |
* @since 1.6.15 |
| 583 |
*/ |
| 584 |
public function get_block_css_and_js( $block ) { |
| 585 |
|
| 586 |
$block = (array) $block; |
| 587 |
|
| 588 |
$name = $block['blockName']; |
| 589 |
$css = array(); |
| 590 |
$js = ''; |
| 591 |
$block_id = ''; |
| 592 |
|
| 593 |
if ( isset( $name ) ) { |
| 594 |
|
| 595 |
if ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) { |
| 596 |
$blockattr = $block['attrs']; |
| 597 |
if ( isset( $blockattr['block_id'] ) ) { |
| 598 |
$block_id = $blockattr['block_id']; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
self::$current_block_list[] = $name; |
| 603 |
|
| 604 |
if ( strpos( $name, 'wcfb/' ) !== false ) { |
| 605 |
self::$cf_flag = true; |
| 606 |
} |
| 607 |
|
| 608 |
switch ( $name ) { |
| 609 |
case 'wcfb/next-step-button': |
| 610 |
$css = Cartflows_Block_Helper::get_next_step_button_css( $blockattr, $block_id ); |
| 611 |
Cartflows_Block_JS::blocks_next_step_button_gfont( $blockattr ); |
| 612 |
break; |
| 613 |
|
| 614 |
case 'wcfb/order-detail-form': |
| 615 |
$css = Cartflows_Block_Helper::get_order_detail_form_css( $blockattr, $block_id ); |
| 616 |
Cartflows_Block_JS::blocks_order_detail_form_gfont( $blockattr ); |
| 617 |
break; |
| 618 |
|
| 619 |
case 'wcfb/checkout-form': |
| 620 |
$css = Cartflows_Block_Helper::get_checkout_form_css( $blockattr, $block_id ); |
| 621 |
Cartflows_Block_JS::blocks_checkout_form_gfont( $blockattr ); |
| 622 |
break; |
| 623 |
|
| 624 |
case 'wcfb/optin-form': |
| 625 |
$css = Cartflows_Block_Helper::get_optin_form_css( $blockattr, $block_id ); |
| 626 |
Cartflows_Block_JS::blocks_optin_form_gfont( $blockattr ); |
| 627 |
break; |
| 628 |
|
| 629 |
default: |
| 630 |
// Nothing to do here. |
| 631 |
break; |
| 632 |
} |
| 633 |
|
| 634 |
if ( isset( $block['innerBlocks'] ) ) { |
| 635 |
foreach ( $block['innerBlocks'] as $j => $inner_block ) { |
| 636 |
if ( 'core/block' === $inner_block['blockName'] ) { |
| 637 |
$id = ( isset( $inner_block['attrs']['ref'] ) ) ? $inner_block['attrs']['ref'] : 0; |
| 638 |
|
| 639 |
if ( $id ) { |
| 640 |
$content = get_post_field( 'post_content', $id ); |
| 641 |
|
| 642 |
$reusable_blocks = $this->parse( $content ); |
| 643 |
|
| 644 |
$assets = $this->get_assets( $reusable_blocks ); |
| 645 |
|
| 646 |
self::$stylesheet .= $assets['css']; |
| 647 |
self::$script .= $assets['js']; |
| 648 |
} |
| 649 |
} else { |
| 650 |
// Get CSS for the Block. |
| 651 |
$inner_assets = $this->get_block_css_and_js( $inner_block ); |
| 652 |
$inner_block_css = $inner_assets['css']; |
| 653 |
|
| 654 |
$css_desktop = ( isset( $css['desktop'] ) ? $css['desktop'] : '' ); |
| 655 |
$css_tablet = ( isset( $css['tablet'] ) ? $css['tablet'] : '' ); |
| 656 |
$css_mobile = ( isset( $css['mobile'] ) ? $css['mobile'] : '' ); |
| 657 |
|
| 658 |
if ( isset( $inner_block_css['desktop'] ) ) { |
| 659 |
$css['desktop'] = $css_desktop . $inner_block_css['desktop']; |
| 660 |
$css['tablet'] = $css_tablet . $inner_block_css['tablet']; |
| 661 |
$css['mobile'] = $css_mobile . $inner_block_css['mobile']; |
| 662 |
} |
| 663 |
|
| 664 |
$js .= $inner_assets['js']; |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
self::$current_block_list = array_unique( self::$current_block_list ); |
| 670 |
} |
| 671 |
|
| 672 |
return array( |
| 673 |
'css' => $css, |
| 674 |
'js' => $js, |
| 675 |
); |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Border attribute generation Function. |
| 680 |
* |
| 681 |
* @since 2.0.0-beta.3 |
| 682 |
* @param array $prefix Attribute Prefix. |
| 683 |
* @param array $default_args default attributes args. |
| 684 |
* @return array |
| 685 |
*/ |
| 686 |
public function generate_php_border_attribute( $prefix, $default_args = array() ) { |
| 687 |
|
| 688 |
$border_attr = array(); |
| 689 |
|
| 690 |
$device = array( '', 'Tablet', 'Mobile' ); |
| 691 |
|
| 692 |
foreach ( $device as $slug => $data ) { |
| 693 |
|
| 694 |
$border_attr[ "{$prefix}BorderTopWidth{$data}" ] = array( |
| 695 |
'type' => 'number', |
| 696 |
); |
| 697 |
$border_attr[ "{$prefix}BorderLeftWidth{$data}" ] = array( |
| 698 |
'type' => 'number', |
| 699 |
); |
| 700 |
$border_attr[ "{$prefix}BorderRightWidth{$data}" ] = array( |
| 701 |
'type' => 'number', |
| 702 |
); |
| 703 |
$border_attr[ "{$prefix}BorderBottomWidth{$data}" ] = array( |
| 704 |
'type' => 'number', |
| 705 |
); |
| 706 |
$border_attr[ "{$prefix}BorderTopLeftRadius{$data}" ] = array( |
| 707 |
'type' => 'number', |
| 708 |
); |
| 709 |
$border_attr[ "{$prefix}BorderTopRightRadius{$data}" ] = array( |
| 710 |
'type' => 'number', |
| 711 |
); |
| 712 |
$border_attr[ "{$prefix}BorderBottomLeftRadius{$data}" ] = array( |
| 713 |
'type' => 'number', |
| 714 |
); |
| 715 |
$border_attr[ "{$prefix}BorderBottomRightRadius{$data}" ] = array( |
| 716 |
'type' => 'number', |
| 717 |
); |
| 718 |
$border_attr[ "{$prefix}BorderRadiusUnit{$data}" ] = array( |
| 719 |
'type' => 'number', |
| 720 |
); |
| 721 |
} |
| 722 |
|
| 723 |
$border_attr[ "{$prefix}BorderStyle" ] = array( |
| 724 |
'type' => 'string', |
| 725 |
); |
| 726 |
$border_attr[ "{$prefix}BorderColor" ] = array( |
| 727 |
'type' => 'string', |
| 728 |
); |
| 729 |
$border_attr[ "{$prefix}BorderHColor" ] = array( |
| 730 |
'type' => 'string', |
| 731 |
); |
| 732 |
$border_attr[ "{$prefix}BorderLink" ] = array( |
| 733 |
'type' => 'boolean', |
| 734 |
'default' => true, |
| 735 |
); |
| 736 |
$border_attr[ "{$prefix}BorderRadiusLink" ] = array( |
| 737 |
'type' => 'boolean', |
| 738 |
'default' => true, |
| 739 |
); |
| 740 |
|
| 741 |
return $border_attr; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
/** |
| 746 |
* Generate advanced settings clsses. |
| 747 |
* |
| 748 |
* @param array $attributes Blocks Attribute. |
| 749 |
* @return array |
| 750 |
*/ |
| 751 |
public function generate_advanced_setting_classes( $attributes ) { |
| 752 |
|
| 753 |
$is_visible = array_key_exists( 'UAGHideDesktop', $attributes ) || array_key_exists( 'UAGHideTab', $attributes ) || array_key_exists( 'UAGHideMob', $attributes ); |
| 754 |
$desktop_class = $is_visible && isset( $attributes['UAGHideDesktop'] ) ? 'uag-hide-desktop' : ''; |
| 755 |
$tab_class = $is_visible && isset( $attributes['UAGHideTab'] ) ? 'uag-hide-tab' : ''; |
| 756 |
$mob_class = $is_visible && isset( $attributes['UAGHideMob'] ) ? 'uag-hide-mob' : ''; |
| 757 |
$zindex_wrap = array(); |
| 758 |
$zindex_extention_enabled = ( isset( $attributes['zIndex'] ) || isset( $attributes['zIndexTablet'] ) || isset( $attributes['zIndexMobile'] ) ); |
| 759 |
$zindex_desktop = $zindex_extention_enabled && isset( $attributes['zIndex'] ) ? '--z-index-desktop:' . $attributes['zIndex'] . ';' : false; |
| 760 |
$zindex_tablet = $zindex_extention_enabled && isset( $attributes['zIndexTablet'] ) ? '--z-index-tablet:' . $attributes['zIndexTablet'] . ';' : false; |
| 761 |
$zindex_mobile = $zindex_extention_enabled && isset( $attributes['zIndexMobile'] ) ? '--z-index-mobile:' . $attributes['zIndexMobile'] . ';' : false; |
| 762 |
|
| 763 |
if ( $zindex_desktop ) { |
| 764 |
array_push( $zindex_wrap, $zindex_desktop ); |
| 765 |
} |
| 766 |
if ( $zindex_tablet ) { |
| 767 |
array_push( $zindex_wrap, $zindex_tablet ); |
| 768 |
} |
| 769 |
if ( $zindex_mobile ) { |
| 770 |
array_push( $zindex_wrap, $zindex_mobile ); |
| 771 |
} |
| 772 |
|
| 773 |
return array( |
| 774 |
'desktop_class' => $desktop_class, |
| 775 |
'tab_class' => $tab_class, |
| 776 |
'mob_class' => $mob_class, |
| 777 |
'zindex_wrap' => $zindex_wrap, |
| 778 |
'zindex_extention_enabled' => $zindex_extention_enabled, |
| 779 |
); |
| 780 |
} |
| 781 |
} |
| 782 |
/** |
| 783 |
* Prepare if class 'Cartflows_Gb_Helper' exist. |
| 784 |
* Kicking this off by calling 'get_instance()' method |
| 785 |
*/ |
| 786 |
Cartflows_Gb_Helper::get_instance(); |
| 787 |
} |
| 788 |
|