| 1 |
<?php |
| 2 |
namespace ABlocks\Controls; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\ControlBaseAbstractTwo; |
| 9 |
|
| 10 |
class Range extends ControlBaseAbstractTwo { |
| 11 |
|
| 12 |
public static function get_attribute_default_value( $args ) { |
| 13 |
|
| 14 |
$unitObject = $args['hasUnit'] ? [ |
| 15 |
$args['attributeObjectKey'] . 'Unit' => $args['unitDefaultValue'], |
| 16 |
$args['attributeObjectKey'] . 'UnitTablet' => '', |
| 17 |
$args['attributeObjectKey'] . 'UnitMobile' => '', |
| 18 |
] : []; |
| 19 |
|
| 20 |
if ( $args['isResponsive'] ) { |
| 21 |
return array_merge([ |
| 22 |
$args['attributeObjectKey'] => $args['defaultValue'], |
| 23 |
$args['attributeObjectKey'] . 'Tablet' => $args['defaultValueTablet'], |
| 24 |
$args['attributeObjectKey'] . 'Mobile' => $args['defaultValueMobile'] |
| 25 |
], $unitObject); |
| 26 |
} elseif ( ! $args['isResponsive'] && $args['hasUnit'] ) { |
| 27 |
return array_merge([ |
| 28 |
$args['attributeObjectKey'] => $args['defaultValue'], |
| 29 |
$args['attributeObjectKey'] . 'Unit' => $args['unitDefaultValue'] |
| 30 |
], $unitObject); |
| 31 |
} |
| 32 |
|
| 33 |
return $args['defaultValue']; |
| 34 |
} |
| 35 |
|
| 36 |
public static function get_attribute( $args ) { |
| 37 |
$defaults = [ |
| 38 |
'attributeName' => '', |
| 39 |
'isResponsive' => false, |
| 40 |
'defaultValue' => '', |
| 41 |
'defaultValueTablet' => '', |
| 42 |
'defaultValueMobile' => '', |
| 43 |
'hasUnit' => false, |
| 44 |
'unitDefaultValue' => 'px', |
| 45 |
'attributeObjectKey' => 'value', |
| 46 |
]; |
| 47 |
|
| 48 |
$args = wp_parse_args( $args, $defaults ); |
| 49 |
|
| 50 |
if ( $args['isResponsive'] ) { |
| 51 |
return [ |
| 52 |
$args['attributeName'] => [ |
| 53 |
'type' => 'object', |
| 54 |
'default' => self::get_attribute_default_value( $args ) |
| 55 |
] |
| 56 |
]; |
| 57 |
} elseif ( ! $args['isResponsive'] && $args['hasUnit'] ) { |
| 58 |
return [ |
| 59 |
$args['attributeName'] => [ |
| 60 |
'type' => 'object', |
| 61 |
'default' => self::get_attribute_default_value( $args ) |
| 62 |
] |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
return [ |
| 67 |
$args['attributeName'] => [ |
| 68 |
'type' => 'number', |
| 69 |
'default' => $args['defaultValue'] |
| 70 |
] |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
public static function get_css( $args ) { |
| 75 |
if ( ! isset( $args['property'] ) || |
| 76 |
empty( $args['property'] ) |
| 77 |
) { |
| 78 |
return []; |
| 79 |
} |
| 80 |
|
| 81 |
// Set default values for missing arguments |
| 82 |
$args = wp_parse_args( $args, [ |
| 83 |
'attributeValue' => '', |
| 84 |
'isResponsive' => false, |
| 85 |
'defaultValue' => '', |
| 86 |
'defaultValueTablet' => '', |
| 87 |
'defaultValueMobile' => '', |
| 88 |
'hasUnit' => false, |
| 89 |
'unitDefaultValue' => 'px', |
| 90 |
'attributeObjectKey' => 'value', |
| 91 |
'device' => '', |
| 92 |
]); |
| 93 |
|
| 94 |
$value = $args['attributeValue']; |
| 95 |
$css = []; |
| 96 |
|
| 97 |
if ( ! $args['isResponsive'] && ! $args['hasUnit'] ) { |
| 98 |
if ( $args['property'] === 'value' ) { |
| 99 |
$css['value'] = $value; |
| 100 |
$css['valueUnit'] = $args['unitDefaultValue']; |
| 101 |
} else { |
| 102 |
$css[ $args['property'] ] = $value . $args['unitDefaultValue']; |
| 103 |
} |
| 104 |
return $css; |
| 105 |
} |
| 106 |
|
| 107 |
// Check and generate CSS for the specified device |
| 108 |
$deviceValueKey = $args['attributeObjectKey'] . $args['device']; |
| 109 |
if ( ! empty( $value[ $deviceValueKey ] ) ) { |
| 110 |
$unit = self::get_unit( [ |
| 111 |
'attributeValue' => $value, |
| 112 |
'attributeObjectKey' => $args['attributeObjectKey'], |
| 113 |
'unitDefaultValue' => $args['unitDefaultValue'], |
| 114 |
'device' => $args['device'], |
| 115 |
] ); |
| 116 |
|
| 117 |
if ( $args['property'] === 'value' ) { |
| 118 |
$css['value'] = $value[ $deviceValueKey ]; |
| 119 |
$css['valueUnit'] = $unit; |
| 120 |
} else { |
| 121 |
$css[ $args['property'] ] = $value[ $deviceValueKey ] . $unit; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return $css; |
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|