Margin.php
190 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The margin control. |
| 4 | * |
| 5 | * Creates a margin control. |
| 6 | * |
| 7 | * @package kirki-margin |
| 8 | * @since 1.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\Control; |
| 12 | |
| 13 | use Kirki\Control\Base; |
| 14 | use Kirki\URL; |
| 15 | |
| 16 | /** |
| 17 | * Slider control. |
| 18 | * |
| 19 | * @since 1.0 |
| 20 | */ |
| 21 | class Margin extends Base { |
| 22 | |
| 23 | /** |
| 24 | * The control type. |
| 25 | * |
| 26 | * @since 1.0 |
| 27 | * @access public |
| 28 | * @var string |
| 29 | */ |
| 30 | public $type = 'kirki-margin'; |
| 31 | |
| 32 | /** |
| 33 | * The control version. |
| 34 | * |
| 35 | * @since 1.0 |
| 36 | * @access public |
| 37 | * @var string |
| 38 | */ |
| 39 | public static $control_ver = '0.1.0'; |
| 40 | |
| 41 | /** |
| 42 | * Control's value unit. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | public $value_unit = 'px'; |
| 47 | |
| 48 | /** |
| 49 | * The default of control's default. |
| 50 | * This will be parsed with $args['default'] and without the unit. |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
| 54 | public $default_array = [ |
| 55 | 'top' => '', |
| 56 | 'right' => '', |
| 57 | 'bottom' => '', |
| 58 | 'left' => '', |
| 59 | ]; |
| 60 | |
| 61 | /** |
| 62 | * The default of control's value. |
| 63 | * This will be parsed with $this->value() and without the unit. |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
| 67 | public $value_array = [ |
| 68 | 'top' => '', |
| 69 | 'right' => '', |
| 70 | 'bottom' => '', |
| 71 | 'left' => '', |
| 72 | ]; |
| 73 | |
| 74 | /** |
| 75 | * Control's constructor. |
| 76 | * |
| 77 | * @since 1.0.0 |
| 78 | * |
| 79 | * @param WP_Customize_Manager $wp_customize WP_Customize_Manager instance. |
| 80 | * @param string $id The control's ID. |
| 81 | * @param array $args The control's arguments. |
| 82 | */ |
| 83 | public function __construct( $wp_customize, $id, $args = array() ) { |
| 84 | |
| 85 | parent::__construct( $wp_customize, $id, $args ); |
| 86 | |
| 87 | // If `unit` choice is defined. |
| 88 | if ( ! empty( $this->choices['unit'] ) ) { |
| 89 | $this->value_unit = $this->choices['unit']; |
| 90 | } |
| 91 | |
| 92 | $this->value_unit = strtolower( $this->value_unit ); |
| 93 | |
| 94 | // Parse $args['default'] with $this->default_array. |
| 95 | if ( ! empty( $args['default'] ) && is_array( $args['default'] ) ) { |
| 96 | $this->default_array = wp_parse_args( $args['default'], $this->default_array ); |
| 97 | } |
| 98 | |
| 99 | $this->default_array = $this->remove_unit( $this->default_array ); |
| 100 | |
| 101 | // Parse $this->value() with $this->value_array. |
| 102 | if ( ! empty( $this->value() ) && is_array( $this->value() ) ) { |
| 103 | $this->value_array = wp_parse_args( $this->value(), $this->value_array ); |
| 104 | } |
| 105 | |
| 106 | $this->value_array = $this->remove_unit( $this->value_array ); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Remove unit from values. |
| 112 | * |
| 113 | * @param array $values The provided values. |
| 114 | * @return array |
| 115 | */ |
| 116 | public function remove_unit( $values ) { |
| 117 | |
| 118 | foreach ( $values as $position => $value ) { |
| 119 | if ( '' !== $value ) { |
| 120 | // Force $value to not using unit. |
| 121 | if ( ! is_numeric( $value ) ) { |
| 122 | $unit = preg_replace( '/\d+/', '', $value ); |
| 123 | $value = str_ireplace( $unit, '', $value ); |
| 124 | $value = (float) $value; |
| 125 | } else { |
| 126 | $value = (float) $value; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | $values[ $position ] = $value; |
| 131 | } |
| 132 | |
| 133 | return $values; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Enqueue control related styles/scripts. |
| 139 | * |
| 140 | * @since 1.0 |
| 141 | * @access public |
| 142 | */ |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Refresh the parameters passed to the JavaScript via JSON. |
| 147 | * |
| 148 | * @see WP_Customize_Control::to_json() |
| 149 | * |
| 150 | * @since 1.0 |
| 151 | * @access public |
| 152 | */ |
| 153 | public function to_json() { |
| 154 | |
| 155 | parent::to_json(); |
| 156 | |
| 157 | if ( isset( $this->json['label'] ) ) { |
| 158 | $this->json['label'] = html_entity_decode( $this->json['label'] ); |
| 159 | } |
| 160 | |
| 161 | if ( isset( $this->json['description'] ) ) { |
| 162 | $this->json['description'] = html_entity_decode( $this->json['description'] ); |
| 163 | } |
| 164 | |
| 165 | $this->json['valueArray'] = $this->value_array; |
| 166 | $this->json['defaultArray'] = $this->value_array; |
| 167 | $this->json['valueUnit'] = $this->value_unit; |
| 168 | |
| 169 | $this->json['value'] = []; |
| 170 | |
| 171 | foreach ( $this->json['valueArray'] as $position => $value ) { |
| 172 | $this->json['value'][ $position ] = $value . $this->value_unit; |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * An Underscore (JS) template for this control's content (but not its container). |
| 179 | * |
| 180 | * Class variables for this control class are available in the `data` JS object; |
| 181 | * export custom variables by overriding WP_Customize_Control::to_json(). |
| 182 | * |
| 183 | * @see WP_Customize_Control::print_template() |
| 184 | * |
| 185 | * @since 1.0 |
| 186 | */ |
| 187 | protected function content_template() {} |
| 188 | |
| 189 | } |
| 190 |