| @@ -1,0 +1,99 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Controls; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Classes\ControlBaseAbstract; | |
| 9 | + | |
| 10 | +class Dimensions extends ControlBaseAbstract { | |
| 11 | + public static function get_attribute_default_value( $is_responsive = false ) { | |
| 12 | + if ( $is_responsive ) { | |
| 13 | + return [ | |
| 14 | + 'isLinked' => true, | |
| 15 | + 'isLinkedTablet' => true, | |
| 16 | + 'isLinkedMobile' => true, | |
| 17 | + 'common' => '', | |
| 18 | + 'top' => '', | |
| 19 | + 'right' => '', | |
| 20 | + 'bottom' => '', | |
| 21 | + 'left' => '', | |
| 22 | + 'unit' => 'px', | |
| 23 | + 'commonTablet' => '', | |
| 24 | + 'topTablet' => '', | |
| 25 | + 'rightTablet' => '', | |
| 26 | + 'bottomTablet' => '', | |
| 27 | + 'leftTablet' => '', | |
| 28 | + 'unitTablet' => '', | |
| 29 | + 'commonMobile' => '', | |
| 30 | + 'topMobile' => '', | |
| 31 | + 'rightMobile' => '', | |
| 32 | + 'bottomMobile' => '', | |
| 33 | + 'leftMobile' => '', | |
| 34 | + 'unitMobile' => '', | |
| 35 | + ]; | |
| 36 | + }//end if | |
| 37 | + return [ | |
| 38 | + 'isLinked' => true, | |
| 39 | + 'common' => '', | |
| 40 | + 'top' => '', | |
| 41 | + 'right' => '', | |
| 42 | + 'bottom' => '', | |
| 43 | + 'left' => '', | |
| 44 | + 'unit' => 'px', | |
| 45 | + ]; | |
| 46 | + } | |
| 47 | + | |
| 48 | + public static function get_attribute( $attributeName, $isResponsive = false ) { | |
| 49 | + $attribute_value = self::get_attribute_default_value( $isResponsive ); | |
| 50 | + if ( $isResponsive ) { | |
| 51 | + return [ | |
| 52 | + $attributeName => [ | |
| 53 | + 'type' => 'object', | |
| 54 | + 'default' => $attribute_value | |
| 55 | + ] | |
| 56 | + ]; | |
| 57 | + } | |
| 58 | + return [ | |
| 59 | + $attributeName => [ | |
| 60 | + 'type' => 'object', | |
| 61 | + 'default' => $attribute_value | |
| 62 | + ] | |
| 63 | + ]; | |
| 64 | + } | |
| 65 | + public static function get_css( $attribute_value, $property = '', $device = '' ) { | |
| 66 | + $attribute_value = wp_parse_args( $attribute_value, $device ? self::responsive_defaults() : self::get_attribute_default_value( false ) ); | |
| 67 | + $css = []; | |
| 68 | + $unit = self::get_unit( $attribute_value, $device ); | |
| 69 | + | |
| 70 | + if ( (bool) $attribute_value[ 'isLinked' . $device ] ) { | |
| 71 | + if ( '' !== $attribute_value[ 'common' . $device ] ) { | |
| 72 | + $css[ $property ] = $attribute_value[ 'common' . $device ] . $unit; | |
| 73 | + } | |
| 74 | + } else { | |
| 75 | + /* | |
| 76 | + * A side may carry its own unit (`topUnit`, `rightUnit`, …), so an | |
| 77 | + * author can mix e.g. `padding-top: 2rem` with `padding-left: 10px`. | |
| 78 | + * Purely additive: a side that has never been given one falls back to | |
| 79 | + * the group unit, so anything saved before this compiles | |
| 80 | + * byte-for-byte as it did. | |
| 81 | + */ | |
| 82 | + foreach ( [ 'top', 'right', 'bottom', 'left' ] as $side ) { | |
| 83 | + if ( '' === $attribute_value[ $side . $device ] ) { | |
| 84 | + continue; | |
| 85 | + } | |
| 86 | + $side_unit = self::device_member( $attribute_value, $side . 'Unit', $device ); | |
| 87 | + if ( '' === $side_unit ) { | |
| 88 | + $side_unit = self::device_member( $attribute_value, $side . 'Unit' ); | |
| 89 | + } | |
| 90 | + if ( '' === $side_unit ) { | |
| 91 | + $side_unit = $unit; | |
| 92 | + } | |
| 93 | + $css[ $property . '-' . $side ] = $attribute_value[ $side . $device ] . $side_unit; | |
| 94 | + } | |
| 95 | + } | |
| 96 | + return $css; | |
| 97 | + } | |
| 98 | + | |
| 99 | +} | |