| 1 |
<?php |
| 2 |
namespace ABlocks\Controls; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\ControlBaseAbstract; |
| 9 |
|
| 10 |
class Typography extends ControlBaseAbstract { |
| 11 |
public static function get_attribute_default_value( $is_responsive = false, $default = [] ) { |
| 12 |
$base_values = [ |
| 13 |
'fontFamily' => '', |
| 14 |
'weight' => '', |
| 15 |
'transform' => '', |
| 16 |
'style' => '', |
| 17 |
'decoration' => '', |
| 18 |
'fontSize' => '', |
| 19 |
'fontSizeUnit' => 'px', |
| 20 |
'lineHeight' => '', |
| 21 |
'lineHeightUnit' => 'px', |
| 22 |
'letterSpacing' => '', |
| 23 |
'letterSpacingUnit' => 'px', |
| 24 |
'wordSpacing' => '', |
| 25 |
'wordSpacingUnit' => 'px', |
| 26 |
]; |
| 27 |
|
| 28 |
if ( $is_responsive ) { |
| 29 |
$responsive_values = [ |
| 30 |
'fontSizeTablet' => '', |
| 31 |
'fontSizeMobile' => '', |
| 32 |
'fontSizeUnitTablet' => 'px', |
| 33 |
'fontSizeUnitMobile' => 'px', |
| 34 |
'lineHeightTablet' => '', |
| 35 |
'lineHeightMobile' => '', |
| 36 |
'lineHeightUnitTablet' => 'px', |
| 37 |
'lineHeightUnitMobile' => 'px', |
| 38 |
'letterSpacingTablet' => '', |
| 39 |
'letterSpacingMobile' => '', |
| 40 |
'letterSpacingUnitTablet' => 'px', |
| 41 |
'letterSpacingUnitMobile' => 'px', |
| 42 |
'wordSpacingTablet' => '', |
| 43 |
'wordSpacingMobile' => '', |
| 44 |
'wordSpacingUnitTablet' => 'px', |
| 45 |
'wordSpacingUnitMobile' => 'px', |
| 46 |
]; |
| 47 |
return array_merge( $base_values, $responsive_values, $default ); |
| 48 |
}//end if |
| 49 |
|
| 50 |
return array_merge( $base_values, $default ); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
public static function get_attribute( $attributeName, $isResponsive = false, $default = [] ) { |
| 55 |
return [ |
| 56 |
$attributeName . 'Global' => [ |
| 57 |
'type' => 'string', |
| 58 |
'default' => '', |
| 59 |
], |
| 60 |
$attributeName => [ |
| 61 |
'type' => 'object', |
| 62 |
'default' => self::get_attribute_default_value( $isResponsive, $default ), |
| 63 |
] |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
public static function get_css( $attribute_value, $property = '', $device = '', $attribute_global_name = '' ) { |
| 68 |
global $ablocks_fonts; |
| 69 |
$global_value = []; |
| 70 |
if ( $attribute_global_name ) { |
| 71 |
$global_typography = wp_list_pluck( \Ablocks\Helper::get_settings( 'global_typography', [] ), 'value', 'id' ); |
| 72 |
$global_value = ( isset( $global_typography[ $attribute_global_name ] ) ? (array) $global_typography[ $attribute_global_name ] : [] ); |
| 73 |
} |
| 74 |
|
| 75 |
if ( empty( $attribute_value ) && ! count( $global_value ) ) { |
| 76 |
return []; |
| 77 |
} |
| 78 |
|
| 79 |
$default_attr_value = self::get_attribute_default_value( (bool) $device ); |
| 80 |
$value = wp_parse_args( $attribute_value, $default_attr_value ); |
| 81 |
$css = []; |
| 82 |
|
| 83 |
// Helper: add CSS property with global fallback |
| 84 |
$add_prop = function( $prop, $local_val, $local_unit, $global_key, $css_var_name ) use ( $attribute_global_name, $global_value, &$css ) { |
| 85 |
if ( ! empty( $global_value[ $global_key ] ) ) { |
| 86 |
$css[ $prop ] = "var(--ablocks-{$attribute_global_name}-{$css_var_name})"; |
| 87 |
} elseif ( ! empty( $local_val ) ) { |
| 88 |
$css[ $prop ] = $local_unit ? $local_val . $local_unit : $local_val; |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
// Font family (special: also track font weights for enqueue) |
| 93 |
if ( ! empty( $global_value['fontFamily'] ) ) { |
| 94 |
$css['font-family'] = "var(--ablocks-{$attribute_global_name}-font-family)"; |
| 95 |
} elseif ( ! empty( $value['fontFamily'] ) ) { |
| 96 |
$font_family = $value['fontFamily']; |
| 97 |
$font_weight = ! empty( $value['weight'] ) ? $value['weight'] : '400'; |
| 98 |
$css['font-family'] = $font_family; |
| 99 |
|
| 100 |
if ( isset( $ablocks_fonts[ $font_family ] ) ) { |
| 101 |
if ( ! in_array( $font_weight, $ablocks_fonts[ $font_family ], true ) ) { |
| 102 |
$ablocks_fonts[ $font_family ][] = $font_weight; |
| 103 |
} |
| 104 |
} else { |
| 105 |
$ablocks_fonts[ $font_family ] = [ $font_weight ]; |
| 106 |
} |
| 107 |
update_option( ABLOCKS_FONTS_SETTINGS_NAME, wp_json_encode( $ablocks_fonts ) ); |
| 108 |
} |
| 109 |
|
| 110 |
// Desktop-only styles |
| 111 |
if ( empty( $device ) ) { |
| 112 |
$add_prop( 'font-weight', $value['weight'] ?? '', null, 'weight', 'weight' ); |
| 113 |
$add_prop( 'text-transform', $value['transform'] ?? '', null, 'transform', 'transform' ); |
| 114 |
$add_prop( 'font-style', $value['style'] ?? '', null, 'style', 'style' ); |
| 115 |
$add_prop( 'text-decoration', $value['decoration'] ?? '', null, 'decoration', 'decoration' ); |
| 116 |
} |
| 117 |
|
| 118 |
// Device-specific typography |
| 119 |
$map = [ |
| 120 |
'fontSize' => 'font-size', |
| 121 |
'lineHeight' => 'line-height', |
| 122 |
'letterSpacing' => 'letter-spacing', |
| 123 |
'wordSpacing' => 'word-spacing', |
| 124 |
]; |
| 125 |
|
| 126 |
foreach ( $map as $prop => $css_prop ) { |
| 127 |
$local_val = $value[ $prop . $device ] ?? ''; |
| 128 |
$local_unit = $value[ $prop . 'Unit' . $device ] ?? ''; |
| 129 |
$global_key = $prop . $device; |
| 130 |
|
| 131 |
$css_var_name = $css_prop . ( $device ? '-' . strtolower( $device ) : '' ); |
| 132 |
|
| 133 |
$add_prop( $css_prop, $local_val, $local_unit, $global_key, $css_var_name ); |
| 134 |
} |
| 135 |
|
| 136 |
return $css; |
| 137 |
} |
| 138 |
} |
| 139 |
|