| 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' => '400', |
| 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 => [ |
| 57 |
'type' => 'object', |
| 58 |
'default' => self::get_attribute_default_value( $isResponsive, $default ), |
| 59 |
] |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
public static function get_css( $attribute_value, $property = '', $device = '' ) { |
| 64 |
global $ablocks_fonts; |
| 65 |
if ( empty( $attribute_value ) ) { |
| 66 |
return []; |
| 67 |
} |
| 68 |
$default_attr_value = self::get_attribute_default_value( (bool) $device ); |
| 69 |
$value = wp_parse_args( $attribute_value, $default_attr_value ); |
| 70 |
$css = []; |
| 71 |
|
| 72 |
// Parse font family |
| 73 |
if ( ! empty( $value['fontFamily'] ) ) { |
| 74 |
$font_family = $value['fontFamily']; |
| 75 |
$font_weight = ! empty( $value['weight'] ) ? $value['weight'] : '400'; |
| 76 |
|
| 77 |
$css['font-family'] = $font_family; |
| 78 |
|
| 79 |
if ( isset( $ablocks_fonts[ $font_family ] ) ) { |
| 80 |
if ( ! in_array( $font_weight, $ablocks_fonts[ $font_family ], true ) ) { |
| 81 |
$ablocks_fonts[ $font_family ][] = $font_weight; |
| 82 |
} |
| 83 |
} else { |
| 84 |
$ablocks_fonts[ $font_family ] = [ $font_weight ]; |
| 85 |
} |
| 86 |
update_option( ABLOCKS_FONTS_SETTINGS_NAME, wp_json_encode( $ablocks_fonts ) ); |
| 87 |
} |
| 88 |
|
| 89 |
// For desktop-specific styles |
| 90 |
if ( empty( $device ) ) { |
| 91 |
if ( ! empty( $value['weight'] ) && $value['weight'] !== '400' ) { |
| 92 |
$css['font-weight'] = $value['weight']; |
| 93 |
} |
| 94 |
if ( ! empty( $value['transform'] ) ) { |
| 95 |
$css['text-transform'] = $value['transform']; |
| 96 |
} |
| 97 |
if ( ! empty( $value['style'] ) ) { |
| 98 |
$css['font-style'] = $value['style']; |
| 99 |
} |
| 100 |
if ( ! empty( $value['decoration'] ) ) { |
| 101 |
$css['text-decoration'] = $value['decoration']; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// Device-specific font properties |
| 106 |
$properties = [ 'fontSize', 'lineHeight', 'letterSpacing', 'wordSpacing' ]; |
| 107 |
foreach ( $properties as $prop ) { |
| 108 |
if ( ! empty( $value[ $prop . $device ] ) && ! empty( $value[ $prop . 'Unit' . $device ] ) ) { |
| 109 |
$css_prop = strtolower( preg_replace( '/([a-z])([A-Z])/', '$1-$2', $prop ) ); |
| 110 |
$css[ $css_prop ] = $value[ $prop . $device ] . $value[ $prop . 'Unit' . $device ]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $css; |
| 115 |
} |
| 116 |
} |
| 117 |
|