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