Typography.php
96 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles CSS output for typography fields. |
| 4 | * |
| 5 | * @package Kirki |
| 6 | * @subpackage Controls |
| 7 | * @copyright Copyright (c) 2023, Themeum |
| 8 | * @license https://opensource.org/licenses/MIT |
| 9 | * @since 2.2.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Kirki\Field\CSS; |
| 13 | |
| 14 | use Kirki\Module\CSS\Output; |
| 15 | |
| 16 | /** |
| 17 | * Output overrides. |
| 18 | */ |
| 19 | class Typography extends Output { |
| 20 | |
| 21 | /** |
| 22 | * Processes a single item from the `output` array. |
| 23 | * |
| 24 | * @access protected |
| 25 | * @param array $output The `output` item. |
| 26 | * @param array $value The field's value. |
| 27 | */ |
| 28 | protected function process_output( $output, $value ) { |
| 29 | |
| 30 | $output['media_query'] = ( isset( $output['media_query'] ) ) ? $output['media_query'] : 'global'; |
| 31 | $output['element'] = ( isset( $output['element'] ) ) ? $output['element'] : 'body'; |
| 32 | $output['prefix'] = ( isset( $output['prefix'] ) ) ? $output['prefix'] : ''; |
| 33 | $output['suffix'] = ( isset( $output['suffix'] ) ) ? $output['suffix'] : ''; |
| 34 | |
| 35 | $properties = [ |
| 36 | 'font-family', |
| 37 | 'font-size', |
| 38 | 'variant', |
| 39 | 'font-weight', |
| 40 | 'font-style', |
| 41 | 'letter-spacing', |
| 42 | 'word-spacing', |
| 43 | 'line-height', |
| 44 | 'text-align', |
| 45 | 'text-transform', |
| 46 | 'text-decoration', |
| 47 | 'color', |
| 48 | 'margin-top', |
| 49 | 'margin-bottom', |
| 50 | ]; |
| 51 | |
| 52 | foreach ( $properties as $property ) { |
| 53 | |
| 54 | // Early exit if the value is not in the defaults. |
| 55 | if ( ! isset( $this->field['default'][ $property ] ) ) { |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | // Early exit if the value is not saved in the values. |
| 60 | if ( ! isset( $value[ $property ] ) || ! $value[ $property ] ) { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | // Early exit if we use "choice" but not for this property. |
| 65 | if ( isset( $output['choice'] ) && $output['choice'] !== $property ) { |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | // Take care of variants. |
| 70 | if ( 'variant' === $property && isset( $value['variant'] ) && ! empty( $value['variant'] ) ) { |
| 71 | |
| 72 | // Get the font_weight. |
| 73 | $font_weight = str_replace( 'italic', '', $value['variant'] ); |
| 74 | $font_weight = ( in_array( $font_weight, [ '', 'regular' ], true ) ) ? '400' : $font_weight; |
| 75 | |
| 76 | // Is this italic? |
| 77 | $is_italic = ( false !== strpos( $value['variant'], 'italic' ) ); |
| 78 | |
| 79 | $this->styles[ $output['media_query'] ][ $output['element'] ]['font-weight'] = $font_weight; |
| 80 | |
| 81 | if ( $is_italic ) { |
| 82 | $this->styles[ $output['media_query'] ][ $output['element'] ]['font-style'] = 'italic'; |
| 83 | } |
| 84 | |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | $property_value = $this->process_property_value( $property, $value[ $property ] ); |
| 89 | $property = ( isset( $output['choice'] ) && isset( $output['property'] ) ) ? $output['property'] : $property; |
| 90 | $property_value = ( is_array( $property_value ) && isset( $property_value[0] ) ) ? $property_value[0] : $property_value; |
| 91 | $this->styles[ $output['media_query'] ][ $output['element'] ][ $property ] = $output['prefix'] . $property_value . $output['suffix']; |
| 92 | } |
| 93 | |
| 94 | } |
| 95 | } |
| 96 |