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
base.php
118 lines
| 1 | <?php |
| 2 | namespace Crocoblock\Blocks_Style\Field_Handlers; |
| 3 | |
| 4 | class Base { |
| 5 | |
| 6 | protected $raw_value = ''; |
| 7 | |
| 8 | /** |
| 9 | * Constructor |
| 10 | * |
| 11 | * @param string $raw_value Raw value to be parsed. |
| 12 | */ |
| 13 | public function __construct( $raw_value = '' ) { |
| 14 | $this->raw_value = $raw_value; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Get the parsed value. |
| 19 | * |
| 20 | * @return array |
| 21 | */ |
| 22 | public function get_parsed_value() { |
| 23 | return array( |
| 24 | 'value' => $this->raw_value, |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * String with CSS variable macros. |
| 30 | * Should be rewritten in the classes with multiple parameters |
| 31 | * returned by get_parsed_value() method. |
| 32 | */ |
| 33 | public function css_var_value_format() { |
| 34 | return '{{VALUE}}'; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Replace data in the string with CSS variable macros. |
| 39 | * |
| 40 | * @param string $string String with macros. |
| 41 | * @param array $data Data to replace in the string. |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | public function parse_variable( $variable = array() ) { |
| 46 | |
| 47 | $prefix = isset($variable['prefix']) ? $variable['prefix'] : ''; |
| 48 | $name = isset($variable['name']) ? $variable['name'] : false; |
| 49 | $full_name = isset($variable['full_name']) ? $variable['full_name'] : $prefix . '-' . $name; |
| 50 | |
| 51 | if ( ! $full_name ) { |
| 52 | return array(); |
| 53 | } |
| 54 | |
| 55 | if ( isset( $variable['suffix'] ) ) { |
| 56 | $full_name .= $variable['suffix']; |
| 57 | } |
| 58 | |
| 59 | $parsed_values = $this->get_parsed_value(); |
| 60 | $result = array(); |
| 61 | |
| 62 | if ( ! empty( $parsed_values['value'] ) ) { |
| 63 | $result[] = $full_name . ':' . $parsed_values['value']; |
| 64 | } else { |
| 65 | $values = self::replace_data( $this->css_var_value_format(), $parsed_values ); |
| 66 | $values = trim( $values ); |
| 67 | |
| 68 | if ( ! empty( $values ) ) { |
| 69 | $result[] = $full_name . ':' . $values; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return $result; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Parses data with macros. |
| 78 | * |
| 79 | * @param string $data Data to parse. |
| 80 | * @param array $macros Macros to replace. |
| 81 | * @return string Parsed data. |
| 82 | */ |
| 83 | public static function replace_data( $data = '', $macros = array() ) { |
| 84 | |
| 85 | if ( empty( $data ) || ! is_string( $data ) ) { |
| 86 | return ''; |
| 87 | } |
| 88 | |
| 89 | if ( ! empty( $macros ) && is_array( $macros ) ) { |
| 90 | $data = preg_replace_callback( |
| 91 | '/\{\{\s*([\w-]+)\s*\}\}/', |
| 92 | function ( $matches ) use ( $macros ) { |
| 93 | $key = strtolower( $matches[1] ); |
| 94 | return isset( $macros[ $key ] ) ? $macros[ $key ] : ''; |
| 95 | }, |
| 96 | $data |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return trim( $data ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get CSS rule string with replaced macros. |
| 105 | * |
| 106 | * @param string $rule_string String with CSS rule and macros. |
| 107 | * @return string Parsed CSS rule. |
| 108 | */ |
| 109 | public function get_css_rule( $rule_string = '' ) { |
| 110 | $parsed_values = $this->get_parsed_value(); |
| 111 | |
| 112 | if ( empty( $parsed_values ) || ! is_array( $parsed_values ) ) { |
| 113 | return ''; |
| 114 | } |
| 115 | |
| 116 | return self::replace_data( $rule_string, $parsed_values ); |
| 117 | } |
| 118 | } |