| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
if ( ! class_exists( 'ReduxFrameworkLegacy_spacing' ) ) { |
| 9 |
class ReduxFrameworkLegacy_spacing { |
| 10 |
|
| 11 |
/** |
| 12 |
* Field Constructor. |
| 13 |
* Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function |
| 14 |
* |
| 15 |
* @since ReduxFrameworkLegacy 1.0.0 |
| 16 |
*/ |
| 17 |
function __construct( $field = array(), $value = '', $parent = null ) { |
| 18 |
$this->parent = $parent; |
| 19 |
$this->field = $field; |
| 20 |
$this->value = $value; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Field Render Function. |
| 25 |
* Takes the vars and outputs the HTML for the field in the settings |
| 26 |
* |
| 27 |
* @since ReduxFrameworkLegacy 1.0.0 |
| 28 |
*/ |
| 29 |
function render() { |
| 30 |
/* |
| 31 |
* So, in_array() wasn't doing it's job for checking a passed array for a proper value. |
| 32 |
* It's wonky. It only wants to check the keys against our array of acceptable values, and not the key's |
| 33 |
* value. So we'll use this instead. Fortunately, a single no array value can be passed and it won't |
| 34 |
* take a dump. |
| 35 |
*/ |
| 36 |
|
| 37 |
// No errors please |
| 38 |
// Set field values |
| 39 |
$defaults = array( |
| 40 |
'units' => '', |
| 41 |
'mode' => 'padding', |
| 42 |
'top' => true, |
| 43 |
'bottom' => true, |
| 44 |
'all' => false, |
| 45 |
'left' => true, |
| 46 |
'right' => true, |
| 47 |
'units_extended' => false, |
| 48 |
'display_units' => true, |
| 49 |
); |
| 50 |
|
| 51 |
$this->field = wp_parse_args( $this->field, $defaults ); |
| 52 |
|
| 53 |
// Set default values |
| 54 |
$defaults = array( |
| 55 |
'top' => '', |
| 56 |
'right' => '', |
| 57 |
'bottom' => '', |
| 58 |
'left' => '', |
| 59 |
'units' => 'px', |
| 60 |
); |
| 61 |
|
| 62 |
$this->value = wp_parse_args( $this->value, $defaults ); |
| 63 |
|
| 64 |
/* |
| 65 |
* Acceptable values checks. If the passed variable doesn't pass muster, we unset them |
| 66 |
* and reset them with default values to avoid errors. |
| 67 |
*/ |
| 68 |
|
| 69 |
// If units field has a value but is not an acceptable value, unset the variable |
| 70 |
if ( isset( $this->field['units'] ) && ! ReduxLegacy_Helpers::array_in_array( |
| 71 |
$this->field['units'], |
| 72 |
array( |
| 73 |
'', |
| 74 |
false, |
| 75 |
'%', |
| 76 |
'in', |
| 77 |
'cm', |
| 78 |
'mm', |
| 79 |
'em', |
| 80 |
'rem', |
| 81 |
'ex', |
| 82 |
'pt', |
| 83 |
'pc', |
| 84 |
'px', |
| 85 |
) |
| 86 |
) |
| 87 |
) { |
| 88 |
unset( $this->field['units'] ); |
| 89 |
} |
| 90 |
|
| 91 |
// if there is a default unit value but is not an accepted value, unset the variable |
| 92 |
if ( isset( $this->value['units'] ) && ! ReduxLegacy_Helpers::array_in_array( |
| 93 |
$this->value['units'], |
| 94 |
array( |
| 95 |
'', |
| 96 |
'%', |
| 97 |
'in', |
| 98 |
'cm', |
| 99 |
'mm', |
| 100 |
'em', |
| 101 |
'rem', |
| 102 |
'ex', |
| 103 |
'pt', |
| 104 |
'pc', |
| 105 |
'px', |
| 106 |
) |
| 107 |
) |
| 108 |
) { |
| 109 |
unset( $this->value['units'] ); |
| 110 |
} |
| 111 |
|
| 112 |
// if ($this->field['mode'] == "absolute") { |
| 113 |
// $this->field['units'] = ""; |
| 114 |
// $this->value['units'] = ""; |
| 115 |
// } |
| 116 |
|
| 117 |
if ( $this->field['units'] == false ) { |
| 118 |
$this->value == ''; |
| 119 |
} |
| 120 |
|
| 121 |
if ( isset( $this->field['mode'] ) && ! in_array( |
| 122 |
$this->field['mode'], |
| 123 |
array( |
| 124 |
'margin', |
| 125 |
'padding', |
| 126 |
) |
| 127 |
) |
| 128 |
) { |
| 129 |
if ( $this->field['mode'] == 'absolute' ) { |
| 130 |
$absolute = true; |
| 131 |
} |
| 132 |
$this->field['mode'] = ''; |
| 133 |
} |
| 134 |
|
| 135 |
$value = array( |
| 136 |
'top' => isset( $this->value[ $this->field['mode'] . '-top' ] ) ? filter_var( $this->value[ $this->field['mode'] . '-top' ], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : filter_var( $this->value['top'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ), |
| 137 |
'right' => isset( $this->value[ $this->field['mode'] . '-right' ] ) ? filter_var( $this->value[ $this->field['mode'] . '-right' ], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : filter_var( $this->value['right'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ), |
| 138 |
'bottom' => isset( $this->value[ $this->field['mode'] . '-bottom' ] ) ? filter_var( $this->value[ $this->field['mode'] . '-bottom' ], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : filter_var( $this->value['bottom'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ), |
| 139 |
'left' => isset( $this->value[ $this->field['mode'] . '-left' ] ) ? filter_var( $this->value[ $this->field['mode'] . '-left' ], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : filter_var( $this->value['left'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ), |
| 140 |
); |
| 141 |
|
| 142 |
// if field units has a value and is NOT an array, then evaluate as needed. |
| 143 |
if ( isset( $this->field['units'] ) && ! is_array( $this->field['units'] ) ) { |
| 144 |
|
| 145 |
// if units fields has a value and is not empty but units value does not then make units value the field value |
| 146 |
if ( isset( $this->field['units'] ) && $this->field['units'] != '' && ! isset( $this->value['units'] ) || $this->field['units'] == false ) { |
| 147 |
$this->value['units'] = $this->field['units']; |
| 148 |
|
| 149 |
// If units field does NOT have a value and units value does NOT have a value, set both to blank (default?) |
| 150 |
} elseif ( ! isset( $this->field['units'] ) && ! isset( $this->value['units'] ) ) { |
| 151 |
$this->field['units'] = 'px'; |
| 152 |
$this->value['units'] = 'px'; |
| 153 |
|
| 154 |
// If units field has NO value but units value does, then set unit field to value field |
| 155 |
} elseif ( ! isset( $this->field['units'] ) && isset( $this->value['units'] ) ) { // If Value is defined |
| 156 |
$this->field['units'] = $this->value['units']; |
| 157 |
|
| 158 |
// if unit value is set and unit value doesn't equal unit field (coz who knows why) |
| 159 |
// then set unit value to unit field |
| 160 |
} elseif ( isset( $this->value['units'] ) && $this->value['units'] !== $this->field['units'] ) { |
| 161 |
$this->value['units'] = $this->field['units']; |
| 162 |
} |
| 163 |
|
| 164 |
// do stuff based on unit field NOT set as an array |
| 165 |
} elseif ( isset( $this->field['units'] ) && is_array( $this->field['units'] ) ) { |
| 166 |
// nothing to do here, but I'm leaving the construct just in case I have to debug this again. |
| 167 |
} |
| 168 |
|
| 169 |
if ( isset( $this->field['units'] ) ) { |
| 170 |
$value['units'] = $this->value['units']; |
| 171 |
} |
| 172 |
|
| 173 |
$this->value = $value; |
| 174 |
|
| 175 |
if ( ! empty( $this->field['mode'] ) ) { |
| 176 |
$this->field['mode'] = $this->field['mode'] . '-'; |
| 177 |
} |
| 178 |
|
| 179 |
$defaults = array( |
| 180 |
'top' => '', |
| 181 |
'right' => '', |
| 182 |
'bottom' => '', |
| 183 |
'left' => '', |
| 184 |
'units' => '', |
| 185 |
); |
| 186 |
|
| 187 |
$this->value = wp_parse_args( $this->value, $defaults ); |
| 188 |
|
| 189 |
if ( isset( $this->field['select2'] ) ) { // if there are any let's pass them to js |
| 190 |
$select2_params = json_encode( $this->field['select2'] ); |
| 191 |
$select2_params = htmlspecialchars( $select2_params, ENT_QUOTES ); |
| 192 |
|
| 193 |
echo '<input type="hidden" class="select2_params" value="' . $select2_params . '">'; |
| 194 |
} |
| 195 |
|
| 196 |
echo '<input type="hidden" class="field-units" value="' . $this->value['units'] . '">'; |
| 197 |
|
| 198 |
if ( isset( $this->field['all'] ) && $this->field['all'] == true ) { |
| 199 |
echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-fullscreen icon-large"></i></span><input type="text" class="redux-spacing-all redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'All', 'redux-framework' ) . '" rel="' . $this->field['id'] . '-all" value="' . $this->value['top'] . '"></div>'; |
| 200 |
} |
| 201 |
|
| 202 |
if ( $this->field['top'] === true ) { |
| 203 |
echo '<input type="hidden" class="redux-spacing-value" id="' . $this->field['id'] . '-top" name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $this->field['mode'] . 'top]' . '" value="' . $this->value['top'] . ( ! empty( $this->value['top'] ) ? $this->value['units'] : '' ) . '">'; |
| 204 |
} |
| 205 |
|
| 206 |
if ( $this->field['right'] === true ) { |
| 207 |
echo '<input type="hidden" class="redux-spacing-value" id="' . $this->field['id'] . '-right" name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $this->field['mode'] . 'right]' . '" value="' . $this->value['right'] . ( ! empty( $this->value['right'] ) ? $this->value['units'] : '' ) . '">'; |
| 208 |
} |
| 209 |
|
| 210 |
if ( $this->field['bottom'] === true ) { |
| 211 |
echo '<input type="hidden" class="redux-spacing-value" id="' . $this->field['id'] . '-bottom" name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $this->field['mode'] . 'bottom]' . '" value="' . $this->value['bottom'] . ( ! empty( $this->value['bottom'] ) ? $this->value['units'] : '' ) . '">'; |
| 212 |
} |
| 213 |
|
| 214 |
if ( $this->field['left'] === true ) { |
| 215 |
echo '<input type="hidden" class="redux-spacing-value" id="' . $this->field['id'] . '-left" name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $this->field['mode'] . 'left]' . '" value="' . $this->value['left'] . ( ! empty( $this->value['left'] ) ? $this->value['units'] : '' ) . '">'; |
| 216 |
} |
| 217 |
|
| 218 |
if ( ! isset( $this->field['all'] ) || $this->field['all'] !== true ) { |
| 219 |
/** |
| 220 |
* Top |
| 221 |
* */ |
| 222 |
if ( $this->field['top'] === true ) { |
| 223 |
echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-arrow-up icon-large"></i></span><input type="text" class="redux-spacing-top redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'Top', 'redux-framework' ) . '" rel="' . $this->field['id'] . '-top" value="' . $this->value['top'] . '"></div>'; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Right |
| 228 |
* */ |
| 229 |
if ( $this->field['right'] === true ) { |
| 230 |
echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-arrow-right icon-large"></i></span><input type="text" class="redux-spacing-right redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'Right', 'redux-framework' ) . '" rel="' . $this->field['id'] . '-right" value="' . $this->value['right'] . '"></div>'; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Bottom |
| 235 |
* */ |
| 236 |
if ( $this->field['bottom'] === true ) { |
| 237 |
echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-arrow-down icon-large"></i></span><input type="text" class="redux-spacing-bottom redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'Bottom', 'redux-framework' ) . '" rel="' . $this->field['id'] . '-bottom" value="' . $this->value['bottom'] . '"></div>'; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Left |
| 242 |
* */ |
| 243 |
if ( $this->field['left'] === true ) { |
| 244 |
echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-arrow-left icon-large"></i></span><input type="text" class="redux-spacing-left redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'Left', 'redux-framework' ) . '" rel="' . $this->field['id'] . '-left" value="' . $this->value['left'] . '"></div>'; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Units |
| 250 |
* */ |
| 251 |
if ( $this->field['units'] !== false && is_array( $this->field['units'] ) /* && !isset($absolute) */ && $this->field['display_units'] == true ) { |
| 252 |
|
| 253 |
echo '<div class="select_wrapper spacing-units" original-title="' . __( 'Units', 'redux-framework' ) . '">'; |
| 254 |
echo '<select data-placeholder="' . __( 'Units', 'redux-framework' ) . '" class="redux-spacing redux-spacing-units select ' . $this->field['class'] . '" original-title="' . __( 'Units', 'redux-framework' ) . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[units]' . '" id="' . $this->field['id'] . '_units">'; |
| 255 |
|
| 256 |
if ( $this->field['units_extended'] ) { |
| 257 |
$testUnits = array( 'px', 'em', 'rem', '%', 'in', 'cm', 'mm', 'ex', 'pt', 'pc' ); |
| 258 |
} else { |
| 259 |
$testUnits = array( 'px', 'em', 'pt', 'rem', '%' ); |
| 260 |
} |
| 261 |
|
| 262 |
if ( $this->field['units'] != '' || is_array( $this->field['units'] ) ) { |
| 263 |
$testUnits = $this->field['units']; |
| 264 |
} |
| 265 |
|
| 266 |
echo '<option></option>'; |
| 267 |
|
| 268 |
if ( in_array( $this->field['units'], $testUnits ) ) { |
| 269 |
echo '<option value="' . $this->field['units'] . '" selected="selected">' . $this->field['units'] . '</option>'; |
| 270 |
} else { |
| 271 |
foreach ( $testUnits as $aUnit ) { |
| 272 |
echo '<option value="' . $aUnit . '" ' . selected( $this->value['units'], $aUnit, false ) . '>' . $aUnit . '</option>'; |
| 273 |
} |
| 274 |
} |
| 275 |
echo '</select></div>'; |
| 276 |
|
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Enqueue Function. |
| 282 |
* If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
| 283 |
* |
| 284 |
* @since ReduxFrameworkLegacy 1.0.0 |
| 285 |
*/ |
| 286 |
function enqueue() { |
| 287 |
wp_enqueue_style( 'select2-css' ); |
| 288 |
|
| 289 |
wp_enqueue_script( |
| 290 |
'redux-field-spacing-js', |
| 291 |
ReduxFrameworkLegacy::$_url . 'inc/fields/spacing/field_spacing' . ReduxLegacy_Functions::isMin() . '.js', |
| 292 |
array( 'jquery', 'select2-js', 'redux-js' ), |
| 293 |
time(), |
| 294 |
true |
| 295 |
); |
| 296 |
|
| 297 |
if ( $this->parent->args['dev_mode'] ) { |
| 298 |
wp_enqueue_style( |
| 299 |
'redux-field-spacing-css', |
| 300 |
ReduxFrameworkLegacy::$_url . 'inc/fields/spacing/field_spacing.css', |
| 301 |
array(), |
| 302 |
time(), |
| 303 |
'all' |
| 304 |
); |
| 305 |
} |
| 306 |
} //function |
| 307 |
|
| 308 |
public function output() { |
| 309 |
|
| 310 |
if ( ! isset( $this->field['mode'] ) ) { |
| 311 |
$this->field['mode'] = 'padding'; |
| 312 |
} |
| 313 |
|
| 314 |
if ( isset( $this->field['mode'] ) && ! in_array( |
| 315 |
$this->field['mode'], |
| 316 |
array( |
| 317 |
'padding', |
| 318 |
'absolute', |
| 319 |
'margin', |
| 320 |
) |
| 321 |
) |
| 322 |
) { |
| 323 |
$this->field['mode'] = ''; |
| 324 |
} |
| 325 |
|
| 326 |
$mode = ( $this->field['mode'] != 'absolute' ) ? $this->field['mode'] : ''; |
| 327 |
$units = isset( $this->value['units'] ) ? $this->value['units'] : ''; |
| 328 |
$style = ''; |
| 329 |
|
| 330 |
if ( ! empty( $mode ) ) { |
| 331 |
foreach ( $this->value as $key => $value ) { |
| 332 |
if ( $key == 'units' ) { |
| 333 |
continue; |
| 334 |
} |
| 335 |
|
| 336 |
// Strip off any alpha for is_numeric test - kp |
| 337 |
$num_no_alpha = preg_replace( '/[^\d.-]/', '', $value ); |
| 338 |
|
| 339 |
// Output if it's a numeric entry |
| 340 |
if ( isset( $value ) && is_numeric( $num_no_alpha ) ) { |
| 341 |
$style .= $key . ':' . $value . ';'; |
| 342 |
} |
| 343 |
} |
| 344 |
} else { |
| 345 |
$this->value['top'] = isset( $this->value['top'] ) ? $this->value['top'] : 0; |
| 346 |
$this->value['bottom'] = isset( $this->value['bottom'] ) ? $this->value['bottom'] : 0; |
| 347 |
$this->value['left'] = isset( $this->value['left'] ) ? $this->value['left'] : 0; |
| 348 |
$this->value['right'] = isset( $this->value['right'] ) ? $this->value['right'] : 0; |
| 349 |
|
| 350 |
$cleanValue = array( |
| 351 |
'top' => isset( $this->value[ $mode . '-top' ] ) ? filter_var( $this->value[ $mode . '-top' ], FILTER_SANITIZE_NUMBER_INT ) : filter_var( $this->value['top'], FILTER_SANITIZE_NUMBER_INT ), |
| 352 |
'right' => isset( $this->value[ $mode . '-right' ] ) ? filter_var( $this->value[ $mode . '-right' ], FILTER_SANITIZE_NUMBER_INT ) : filter_var( $this->value['right'], FILTER_SANITIZE_NUMBER_INT ), |
| 353 |
'bottom' => isset( $this->value[ $mode . '-bottom' ] ) ? filter_var( $this->value[ $mode . '-bottom' ], FILTER_SANITIZE_NUMBER_INT ) : filter_var( $this->value['bottom'], FILTER_SANITIZE_NUMBER_INT ), |
| 354 |
'left' => isset( $this->value[ $mode . '-left' ] ) ? filter_var( $this->value[ $mode . '-left' ], FILTER_SANITIZE_NUMBER_INT ) : filter_var( $this->value['left'], FILTER_SANITIZE_NUMBER_INT ), |
| 355 |
); |
| 356 |
|
| 357 |
if ( isset( $this->field['all'] ) && true == $this->field['all'] ) { |
| 358 |
$style .= $mode . 'top:' . $cleanValue['top'] . $units . ';'; |
| 359 |
$style .= $mode . 'bottom:' . $cleanValue['top'] . $units . ';'; |
| 360 |
$style .= $mode . 'right:' . $cleanValue['top'] . $units . ';'; |
| 361 |
$style .= $mode . 'left:' . $cleanValue['top'] . $units . ';'; |
| 362 |
} else { |
| 363 |
if ( true == $this->field['top'] ) { |
| 364 |
$style .= $mode . 'top:' . $cleanValue['top'] . $units . ';'; |
| 365 |
} |
| 366 |
|
| 367 |
if ( true == $this->field['bottom'] ) { |
| 368 |
$style .= $mode . 'bottom:' . $cleanValue['bottom'] . $units . ';'; |
| 369 |
} |
| 370 |
|
| 371 |
if ( true == $this->field['left'] ) { |
| 372 |
$style .= $mode . 'left:' . $cleanValue['left'] . $units . ';'; |
| 373 |
} |
| 374 |
|
| 375 |
if ( true == $this->field['right'] ) { |
| 376 |
$style .= $mode . 'right:' . $cleanValue['right'] . $units . ';'; |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
if ( ! empty( $style ) ) { |
| 382 |
|
| 383 |
if ( ! empty( $this->field['output'] ) && is_array( $this->field['output'] ) ) { |
| 384 |
$keys = implode( ',', $this->field['output'] ); |
| 385 |
$this->parent->outputCSS .= $keys . '{' . $style . '}'; |
| 386 |
} |
| 387 |
|
| 388 |
if ( ! empty( $this->field['compiler'] ) && is_array( $this->field['compiler'] ) ) { |
| 389 |
$keys = implode( ',', $this->field['compiler'] ); |
| 390 |
$this->parent->compilerCSS .= $keys . '{' . $style . '}'; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|