| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
class FrmSliderStyleComponent extends FrmStyleComponent { |
| 6 |
|
| 7 |
/** |
| 8 |
* The view file name. |
| 9 |
* |
| 10 |
* @since 6.14 |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected $view_name = 'slider'; |
| 15 |
|
| 16 |
/** |
| 17 |
* The FrmStyleComponent data. |
| 18 |
* |
| 19 |
* @since 6.14 |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $data; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param string $field_name |
| 27 |
* @param mixed $field_value |
| 28 |
* @param array $data |
| 29 |
*/ |
| 30 |
public function __construct( $field_name, $field_value, $data ) { |
| 31 |
$this->init_field_data( $data, $field_name, $field_value ); |
| 32 |
|
| 33 |
if ( true === $this->hide_component() ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$this->data['unit_measurement'] = $this->detect_unit_measurement(); |
| 38 |
$this->data['has-multiple-values'] = count( $this->get_values() ) > 1; |
| 39 |
$this->data['units'] = $this->get_units_list( $data ); |
| 40 |
$this->data['value_label'] = $this->detect_unit_measurement() ? (float) $field_value : $field_value; |
| 41 |
|
| 42 |
$this->init_defaults(); |
| 43 |
$this->init_icon(); |
| 44 |
$this->init_multiple_values(); |
| 45 |
|
| 46 |
parent::get_instance(); |
| 47 |
$this->load_view(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Retrieves the list of units for the slider style component. |
| 52 |
* |
| 53 |
* If the units array is empty in the provided data, it returns an array containing the default units: 'px', 'em', and '%'. |
| 54 |
* Otherwise, it merges the units array from the provided data with the default units array and returns the result. |
| 55 |
* |
| 56 |
* @param array $data The data containing the units array. |
| 57 |
* |
| 58 |
* @return array The list of units for the slider style component. |
| 59 |
*/ |
| 60 |
private function get_units_list( $data ) { |
| 61 |
if ( empty( $data['units'] ) ) { |
| 62 |
return array( '', 'px', 'em', '%' ); |
| 63 |
} |
| 64 |
array_unshift( $data['units'], '' ); |
| 65 |
return $data['units']; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Init components default values |
| 70 |
* |
| 71 |
* @since 6.14 |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
private function init_defaults() { |
| 76 |
$this->data['max_value'] = ! empty( $this->data['max_value'] ) ? $this->data['max_value'] : 100; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Init the slider multiple values data. It works with sliders which has multiple values only: top&bottom and left&right. |
| 81 |
* This is used for cases when there are 4 sliders in the same field. |
| 82 |
* |
| 83 |
* @since 6.14 |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
private function init_multiple_values() { |
| 88 |
if ( ! $this->data['has-multiple-values'] ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$values = $this->get_values(); |
| 93 |
$top = $values[0]; |
| 94 |
$bottom = ! empty( $values[2] ) ? $values[2] : $values[0]; |
| 95 |
$left = ! empty( $values[3] ) ? $values[3] : $values[1]; |
| 96 |
$right = $values[1]; |
| 97 |
|
| 98 |
$this->data['vertical'] = array( |
| 99 |
'unit' => $this->detect_unit_measurement( $top ), |
| 100 |
'value' => $this->detect_unit_measurement( $top ) ? (float) $top : $top, |
| 101 |
); |
| 102 |
|
| 103 |
$this->data['horizontal'] = array( |
| 104 |
'unit' => $this->detect_unit_measurement( $right ), |
| 105 |
'value' => $this->detect_unit_measurement( $right ) ? (float) $right : $right, |
| 106 |
); |
| 107 |
|
| 108 |
$this->data['top'] = array( |
| 109 |
'unit' => $this->detect_unit_measurement( $top ), |
| 110 |
'value' => $this->detect_unit_measurement( $top ) ? (float) $top : $top, |
| 111 |
); |
| 112 |
|
| 113 |
$this->data['bottom'] = array( |
| 114 |
'unit' => $this->detect_unit_measurement( $bottom ), |
| 115 |
'value' => $this->detect_unit_measurement( $bottom ) ? (float) $bottom : $bottom, |
| 116 |
); |
| 117 |
|
| 118 |
$this->data['left'] = array( |
| 119 |
'unit' => $this->detect_unit_measurement( $left ), |
| 120 |
'value' => $this->detect_unit_measurement( $left ) ? (float) $left : $left, |
| 121 |
); |
| 122 |
|
| 123 |
$this->data['right'] = array( |
| 124 |
'unit' => $this->detect_unit_measurement( $right ), |
| 125 |
'value' => $this->detect_unit_measurement( $right ) ? (float) $right : $right, |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Split the field value by space from string to an array. |
| 131 |
* For instance: '10px 20px 30px 40px' will be converted to array( '10px', '20px', '30px', '40px' ). |
| 132 |
* |
| 133 |
* @since 6.14 |
| 134 |
* |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
private function get_values() { |
| 138 |
return explode( ' ', $this->field_value ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Detect the unit measurement from the value. |
| 143 |
* Possible values are: "px", "%", "em" or empty "" |
| 144 |
* |
| 145 |
* @since 6.14 |
| 146 |
* |
| 147 |
* @param string|null $value |
| 148 |
* |
| 149 |
* @return string |
| 150 |
*/ |
| 151 |
private function detect_unit_measurement( $value = null ) { |
| 152 |
if ( null === $value ) { |
| 153 |
$value = $this->field_value; |
| 154 |
} |
| 155 |
|
| 156 |
if ( preg_match( '/%$/', $value ) ) { |
| 157 |
return '%'; |
| 158 |
} |
| 159 |
|
| 160 |
if ( preg_match( '/em$/', $value ) ) { |
| 161 |
return 'em'; |
| 162 |
} |
| 163 |
|
| 164 |
return preg_match( '/px$/', $value ) ? 'px' : ''; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Init the field icon |
| 169 |
* |
| 170 |
* @since 6.14 |
| 171 |
* |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
private function init_icon() { |
| 175 |
if ( ! empty( $this->data['icon'] ) ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
if ( empty( $this->data['type'] ) ) { |
| 180 |
$this->data['icon'] = ''; |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
switch ( $this->data['type'] ) { |
| 185 |
case 'vertical-margin': |
| 186 |
$this->data['icon'] = 'frmfont frm-margin-top-bottom'; |
| 187 |
return; |
| 188 |
|
| 189 |
case 'horizontal-margin': |
| 190 |
$this->data['icon'] = 'frmfont frm-margin-left-right'; |
| 191 |
return; |
| 192 |
|
| 193 |
default: |
| 194 |
$this->data['icon'] = ''; |
| 195 |
return; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|