base.php
3 months ago
border.php
3 months ago
dimensions.php
3 months ago
range.php
3 months ago
typography.php
3 months ago
typography.php
74 lines
| 1 | <?php |
| 2 | namespace Crocoblock\Blocks_Style\Field_Handlers; |
| 3 | |
| 4 | class Typography extends Base { |
| 5 | |
| 6 | /** |
| 7 | * Replace data in the string with CSS variable macros. |
| 8 | * |
| 9 | * @param string $string String with macros. |
| 10 | * @param array $data Data to replace in the string. |
| 11 | * |
| 12 | * @return array |
| 13 | */ |
| 14 | public function parse_variable( $variable = array() ) { |
| 15 | |
| 16 | $prefix = isset($variable['prefix']) ? $variable['prefix'] : ''; |
| 17 | $name = isset($variable['name']) ? $variable['name'] : false; |
| 18 | $full_name = isset($variable['full_name']) ? $variable['full_name'] : $prefix . '-' . $name; |
| 19 | |
| 20 | if ( ! $full_name ) { |
| 21 | return array(); |
| 22 | } |
| 23 | |
| 24 | if ( isset( $variable['suffix'] ) ) { |
| 25 | $full_name .= $variable['suffix']; |
| 26 | } |
| 27 | |
| 28 | $parsed_values = $this->get_parsed_value(); |
| 29 | $result = array(); |
| 30 | $skip_keys = array( 'lh_unit', 'ls_unit', 's_unit' ); |
| 31 | |
| 32 | foreach ( $parsed_values as $key => $value ) { |
| 33 | |
| 34 | if ( in_array( $key, $skip_keys, true ) ) { |
| 35 | continue; // Skip legacy units. |
| 36 | } |
| 37 | |
| 38 | if ( 'inherit' === $value ) { |
| 39 | continue; // Skip inherit values. |
| 40 | } |
| 41 | |
| 42 | $result[] = $full_name . '__' . $key . ':'. $value; |
| 43 | } |
| 44 | |
| 45 | return $result; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the parsed value. |
| 50 | * |
| 51 | * @return array |
| 52 | */ |
| 53 | public function get_parsed_value() { |
| 54 | |
| 55 | if ( isset( $this->raw_value['lineHeight'] ) ) { |
| 56 | $this->raw_value['lineheight'] = $this->raw_value['lineHeight']; |
| 57 | } |
| 58 | |
| 59 | return [ |
| 60 | 'family' => isset($this->raw_value['family']) ? $this->raw_value['family'] : 'inherit', |
| 61 | 'weight' => isset($this->raw_value['weight']) ? $this->raw_value['weight'] : 'inherit', |
| 62 | 'transform' => isset($this->raw_value['transform']) ? $this->raw_value['transform'] : 'inherit', |
| 63 | 'style' => isset($this->raw_value['style']) ? $this->raw_value['style'] : 'inherit', |
| 64 | 'decoration' => isset($this->raw_value['decoration']) ? $this->raw_value['decoration'] : 'inherit', |
| 65 | 'lineheight' => isset($this->raw_value['lineheight']) ? $this->raw_value['lineheight'] : 'inherit', |
| 66 | 'lh_unit' => '', // legacy from old SM for style decorations consistency |
| 67 | 'letterspacing' => isset($this->raw_value['letterSpacing']) ? $this->raw_value['letterSpacing'] : 'inherit', |
| 68 | 'ls_unit' => '', // legacy from old SM for style decorations consistency |
| 69 | 'size' => isset($this->raw_value['size']) ? $this->raw_value['size'] : 'inherit', |
| 70 | 's_unit' => '', // legacy from old SM for style decorations consistency |
| 71 | ]; |
| 72 | } |
| 73 | } |
| 74 |