| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Redux Framework is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 2 of the License, or |
| 7 |
* any later version. |
| 8 |
* Redux Framework is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with Redux Framework. If not, see <http://www.gnu.org/licenses/>. |
| 14 |
* |
| 15 |
* @package ReduxFramework |
| 16 |
* @subpackage Field_Slider |
| 17 |
* @author Kevin Provance (kprovance) |
| 18 |
* @version 2.0.0 |
| 19 |
*/ |
| 20 |
|
| 21 |
// Exit if accessed directly |
| 22 |
if ( ! defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
if ( ! class_exists( 'ReduxFramework_slider' ) ) { |
| 27 |
class ReduxFramework_slider { |
| 28 |
|
| 29 |
/** |
| 30 |
* Field Constructor. |
| 31 |
* Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function |
| 32 |
* |
| 33 |
* @since ReduxFramework 3.1.8 |
| 34 |
*/ |
| 35 |
private $display_none = 0; |
| 36 |
private $display_label = 1; |
| 37 |
private $display_text = 2; |
| 38 |
private $display_select = 3; |
| 39 |
|
| 40 |
function __construct( $field = array(), $value = '', $parent ) { |
| 41 |
|
| 42 |
//parent::__construct( $parent->sections, $parent->args ); |
| 43 |
$this->parent = $parent; |
| 44 |
$this->field = $field; |
| 45 |
$this->value = $value; |
| 46 |
|
| 47 |
// Set defaults |
| 48 |
$defaults = array( |
| 49 |
'handles' => 1, |
| 50 |
'resolution' => 1, |
| 51 |
'display_value' => 'text', |
| 52 |
'float_mark' => '.', |
| 53 |
'forced' => true |
| 54 |
); |
| 55 |
|
| 56 |
$this->field = wp_parse_args( $this->field, $defaults ); |
| 57 |
|
| 58 |
// Sanitize float mark |
| 59 |
switch ( $this->field['float_mark'] ) { |
| 60 |
case ',': |
| 61 |
case '.': |
| 62 |
break; |
| 63 |
default: |
| 64 |
$this->field['float_mark'] = '.'; |
| 65 |
break; |
| 66 |
} |
| 67 |
|
| 68 |
// Sanitize resolution value |
| 69 |
$this->field['resolution'] = $this->cleanVal( $this->field['resolution'] ); |
| 70 |
|
| 71 |
// Sanitize handle value |
| 72 |
switch ( $this->field['handles'] ) { |
| 73 |
case 0: |
| 74 |
case 1: |
| 75 |
$this->field['handles'] = 1; |
| 76 |
break; |
| 77 |
default: |
| 78 |
$this->field['handles'] = 2; |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
// Sanitize display value |
| 83 |
switch ( $this->field['display_value'] ) { |
| 84 |
case 'label': |
| 85 |
$this->field['display_value'] = $this->display_label; |
| 86 |
break; |
| 87 |
case 'text': |
| 88 |
default: |
| 89 |
$this->field['display_value'] = $this->display_text; |
| 90 |
break; |
| 91 |
case 'select': |
| 92 |
$this->field['display_value'] = $this->display_select; |
| 93 |
break; |
| 94 |
case 'none': |
| 95 |
$this->field['display_value'] = $this->display_none; |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
private function cleanVal( $var ) { |
| 101 |
if ( is_float( $var ) ) { |
| 102 |
$cleanVar = floatval( $var ); |
| 103 |
} else { |
| 104 |
$cleanVar = intval( $var ); |
| 105 |
} |
| 106 |
|
| 107 |
return $cleanVar; |
| 108 |
} |
| 109 |
|
| 110 |
private function cleanDefault( $val ) { |
| 111 |
if ( empty( $val ) && ! empty( $this->field['default'] ) && $this->cleanVal( $this->field['min'] ) >= 1 ) { |
| 112 |
$val = $this->cleanVal( $this->field['default'] ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( empty( $val ) && $this->cleanVal( $this->field['min'] ) >= 1 ) { |
| 116 |
$val = $this->cleanVal( $this->field['min'] ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( empty( $val ) ) { |
| 120 |
$val = 0; |
| 121 |
} |
| 122 |
|
| 123 |
// Extra Validation |
| 124 |
if ( $val < $this->field['min'] ) { |
| 125 |
$val = $this->cleanVal( $this->field['min'] ); |
| 126 |
} else if ( $val > $this->field['max'] ) { |
| 127 |
$val = $this->cleanVal( $this->field['max'] ); |
| 128 |
} |
| 129 |
|
| 130 |
return $val; |
| 131 |
} |
| 132 |
|
| 133 |
private function cleanDefaultArray( $val ) { |
| 134 |
$one = $this->value[1]; |
| 135 |
$two = $this->value[2]; |
| 136 |
|
| 137 |
if ( empty( $one ) && ! empty( $this->field['default'][1] ) && $this->cleanVal( $this->field['min'] ) >= 1 ) { |
| 138 |
$one = $this->cleanVal( $this->field['default'][1] ); |
| 139 |
} |
| 140 |
|
| 141 |
if ( empty( $one ) && $this->cleanVal( $this->field['min'] ) >= 1 ) { |
| 142 |
$one = $this->cleanVal( $this->field['min'] ); |
| 143 |
} |
| 144 |
|
| 145 |
if ( empty( $one ) ) { |
| 146 |
$one = 0; |
| 147 |
} |
| 148 |
|
| 149 |
if ( empty( $two ) && ! empty( $this->field['default'][2] ) && $this->cleanVal( $this->field['min'] ) >= 1 ) { |
| 150 |
$two = $this->cleanVal( $this->field['default'][1] + 1 ); |
| 151 |
} |
| 152 |
|
| 153 |
if ( empty( $two ) && $this->cleanVal( $this->field['min'] ) >= 1 ) { |
| 154 |
$two = $this->cleanVal( $this->field['default'][1] + 1 ); |
| 155 |
} |
| 156 |
|
| 157 |
if ( empty( $two ) ) { |
| 158 |
$two = $this->field['default'][1] + 1; |
| 159 |
} |
| 160 |
|
| 161 |
$val[0] = $one; |
| 162 |
$val[1] = $two; |
| 163 |
|
| 164 |
return $val; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* Clean the field data to the fields defaults given the parameters. |
| 170 |
* |
| 171 |
* @since Redux_Framework 3.1.8 |
| 172 |
*/ |
| 173 |
function clean() { |
| 174 |
|
| 175 |
// Set min to 0 if no value is set. |
| 176 |
$this->field['min'] = empty( $this->field['min'] ) ? 0 : $this->cleanVal( $this->field['min'] ); |
| 177 |
|
| 178 |
// Set max to min + 1 if empty. |
| 179 |
$this->field['max'] = empty( $this->field['max'] ) ? $this->field['min'] + 1 : $this->cleanVal( $this->field['max'] ); |
| 180 |
|
| 181 |
// Set step to 1 if step is empty ot step > max. |
| 182 |
$this->field['step'] = empty( $this->field['step'] ) || $this->field['step'] > $this->field['max'] ? 1 : $this->cleanVal( $this->field['step'] ); |
| 183 |
|
| 184 |
if ( 2 == $this->field['handles'] ) { |
| 185 |
if ( ! is_array( $this->value ) ) { |
| 186 |
$this->value[1] = 0; |
| 187 |
$this->value[2] = 1; |
| 188 |
} |
| 189 |
$this->value = $this->cleanDefaultArray( $this->value ); |
| 190 |
} else { |
| 191 |
if ( is_array( $this->value ) ) { |
| 192 |
$this->value = 0; |
| 193 |
} |
| 194 |
$this->value = $this->cleanDefault( $this->value ); |
| 195 |
} |
| 196 |
|
| 197 |
// More dummy checks |
| 198 |
//if ( ! is_array( $this->field['default'] ) && 2 == $this->field['handles'] ) { |
| 199 |
if ( ! is_array( $this->value ) && 2 == $this->field['handles'] ) { |
| 200 |
$this->value[0] = $this->field['min']; |
| 201 |
$this->value[1] = $this->field['min'] + 1; |
| 202 |
} |
| 203 |
|
| 204 |
//if ( is_array( $this->field['default'] ) && 1 == $this->field['handles'] ) { |
| 205 |
if ( is_array( $this->value ) && 1 == $this->field['handles'] ) { |
| 206 |
$this->value = $this->field['min']; |
| 207 |
} |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Enqueue Function. |
| 213 |
* If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
| 214 |
* |
| 215 |
* @since ReduxFramework 3.1.8 |
| 216 |
*/ |
| 217 |
function enqueue() { |
| 218 |
|
| 219 |
$min = Redux_Functions::isMin(); |
| 220 |
|
| 221 |
wp_enqueue_style( 'select2-css' ); |
| 222 |
|
| 223 |
wp_enqueue_style( |
| 224 |
'redux-nouislider-css', |
| 225 |
ReduxFramework::$_url . 'inc/fields/slider/vendor/nouislider/redux.jquery.nouislider.css', |
| 226 |
array(), |
| 227 |
'5.0.0', |
| 228 |
'all' |
| 229 |
); |
| 230 |
|
| 231 |
wp_register_script( |
| 232 |
'redux-nouislider-js', |
| 233 |
ReduxFramework::$_url . 'inc/fields/slider/vendor/nouislider/redux.jquery.nouislider' . $min . '.js', |
| 234 |
array( 'jquery' ), |
| 235 |
'5.0.0', |
| 236 |
true |
| 237 |
); |
| 238 |
|
| 239 |
wp_enqueue_script( |
| 240 |
'redux-field-slider-js', |
| 241 |
ReduxFramework::$_url . 'inc/fields/slider/field_slider' . $min . '.js', |
| 242 |
array( 'jquery', 'redux-nouislider-js', 'redux-js', 'select2-js' ), |
| 243 |
time(), |
| 244 |
true |
| 245 |
); |
| 246 |
|
| 247 |
if ($this->parent->args['dev_mode']) { |
| 248 |
wp_enqueue_style( |
| 249 |
'redux-field-slider-css', |
| 250 |
ReduxFramework::$_url . 'inc/fields/slider/field_slider.css', |
| 251 |
array(), |
| 252 |
time(), |
| 253 |
'all' |
| 254 |
); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
//function |
| 259 |
|
| 260 |
/** |
| 261 |
* Field Render Function. |
| 262 |
* Takes the vars and outputs the HTML for the field in the settings |
| 263 |
* |
| 264 |
* @since ReduxFramework 0.0.4 |
| 265 |
*/ |
| 266 |
function render() { |
| 267 |
|
| 268 |
$this->clean(); |
| 269 |
|
| 270 |
$fieldID = $this->field['id']; |
| 271 |
$fieldName = $this->field['name'] . $this->field['name_suffix']; |
| 272 |
//$fieldName = $this->parent->args['opt_name'] . '[' . $this->field['id'] . ']'; |
| 273 |
|
| 274 |
// Set handle number variable. |
| 275 |
$twoHandles = false; |
| 276 |
if ( 2 == $this->field['handles'] ) { |
| 277 |
$twoHandles = true; |
| 278 |
} |
| 279 |
|
| 280 |
// Set default values(s) |
| 281 |
if ( true == $twoHandles ) { |
| 282 |
$valOne = $this->value[0]; |
| 283 |
$valTwo = $this->value[1]; |
| 284 |
|
| 285 |
$html = 'data-default-one="' . $valOne . '" '; |
| 286 |
$html .= 'data-default-two="' . $valTwo . '" '; |
| 287 |
|
| 288 |
$nameOne = $fieldName . '[1]'; |
| 289 |
$nameTwo = $fieldName . '[2]'; |
| 290 |
|
| 291 |
$idOne = $fieldID . '[1]'; |
| 292 |
$idTwo = $fieldID . '[2]'; |
| 293 |
} else { |
| 294 |
$valOne = $this->value; |
| 295 |
$valTwo = ''; |
| 296 |
|
| 297 |
$html = 'data-default-one="' . $valOne . '"'; |
| 298 |
|
| 299 |
$nameOne = $fieldName; |
| 300 |
$nameTwo = ''; |
| 301 |
|
| 302 |
$idOne = $fieldID; |
| 303 |
$idTwo = ''; |
| 304 |
} |
| 305 |
|
| 306 |
$showInput = false; |
| 307 |
$showLabel = false; |
| 308 |
$showSelect = false; |
| 309 |
|
| 310 |
// TEXT output |
| 311 |
if ( $this->display_text == $this->field['display_value'] ) { |
| 312 |
$showInput = true; |
| 313 |
echo '<input type="text" |
| 314 |
name="' . $nameOne . '" |
| 315 |
id="' . $idOne . '" |
| 316 |
value="' . $valOne . '" |
| 317 |
class="redux-slider-input redux-slider-input-one-' . $fieldID . ' ' . $this->field['class'] . '"/>'; |
| 318 |
|
| 319 |
// LABEL output |
| 320 |
} elseif ( $this->display_label == $this->field['display_value'] ) { |
| 321 |
$showLabel = true; |
| 322 |
|
| 323 |
$labelNum = $twoHandles ? '-one' : ''; |
| 324 |
|
| 325 |
echo '<div class="redux-slider-label' . $labelNum . '" |
| 326 |
id="redux-slider-label-one-' . $fieldID . '" |
| 327 |
name="' . $nameOne . '"> |
| 328 |
</div>'; |
| 329 |
|
| 330 |
// SELECT output |
| 331 |
} elseif ( $this->display_select == $this->field['display_value'] ) { |
| 332 |
$showSelect = true; |
| 333 |
|
| 334 |
if ( isset( $this->field['select2'] ) ) { // if there are any let's pass them to js |
| 335 |
$select2_params = json_encode( $this->field['select2'] ); |
| 336 |
$select2_params = htmlspecialchars( $select2_params, ENT_QUOTES ); |
| 337 |
|
| 338 |
echo '<input type="hidden" class="select2_params" value="' . $select2_params . '">'; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
echo '<select class="redux-slider-select-one redux-slider-select-one-' . $fieldID . ' ' . $this->field['class'] . '" |
| 343 |
name="' . $nameOne . '" |
| 344 |
id="' . $idOne . '"> |
| 345 |
</select>'; |
| 346 |
} |
| 347 |
|
| 348 |
// DIV output |
| 349 |
echo |
| 350 |
'<div |
| 351 |
class="redux-slider-container ' . $this->field['class'] . '" |
| 352 |
id="' . $fieldID . '" |
| 353 |
data-id="' . $fieldID . '" |
| 354 |
data-min="' . $this->field['min'] . '" |
| 355 |
data-max="' . $this->field['max'] . '" |
| 356 |
data-step="' . $this->field['step'] . '" |
| 357 |
data-handles="' . $this->field['handles'] . '" |
| 358 |
data-display="' . $this->field['display_value'] . '" |
| 359 |
data-rtl="' . is_rtl() . '" |
| 360 |
data-forced="' . $this->field['forced'] . '" |
| 361 |
data-float-mark="' . $this->field['float_mark'] . '" |
| 362 |
data-resolution="' . $this->field['resolution'] . '" ' . $html . '> |
| 363 |
</div>'; |
| 364 |
|
| 365 |
// Double slider output |
| 366 |
if ( true == $twoHandles ) { |
| 367 |
|
| 368 |
// TEXT |
| 369 |
if ( true == $showInput ) { |
| 370 |
echo '<input type="text" |
| 371 |
name="' . $nameTwo . '" |
| 372 |
id="' . $idTwo . '" |
| 373 |
value="' . $valTwo . '" |
| 374 |
class="redux-slider-input redux-slider-input-two-' . $fieldID . ' ' . $this->field['class'] . '"/>'; |
| 375 |
} |
| 376 |
|
| 377 |
// LABEL |
| 378 |
if ( true == $showLabel ) { |
| 379 |
echo '<div class="redux-slider-label-two" |
| 380 |
id="redux-slider-label-two-' . $fieldID . '" |
| 381 |
name="' . $nameTwo . '"> |
| 382 |
</div>'; |
| 383 |
} |
| 384 |
|
| 385 |
// SELECT |
| 386 |
if ( true == $showSelect ) { |
| 387 |
echo '<select class="redux-slider-select-two redux-slider-select-two-' . $fieldID . ' ' . $this->field['class'] . '" |
| 388 |
name="' . $nameTwo . '" |
| 389 |
id="' . $idTwo . '"> |
| 390 |
</select>'; |
| 391 |
|
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
// NO output (input hidden) |
| 396 |
if ( $this->display_none == $this->field['display_value'] || $this->display_label == $this->field['display_value'] ) { |
| 397 |
echo '<input type="hidden" |
| 398 |
class="redux-slider-value-one-' . $fieldID . ' ' . $this->field['class'] . '" |
| 399 |
name="' . $nameOne . '" |
| 400 |
id="' . $idOne . '" |
| 401 |
value="' . $valOne . '"/>'; |
| 402 |
|
| 403 |
// double slider hidden output |
| 404 |
if ( true == $twoHandles ) { |
| 405 |
echo '<input type="hidden" |
| 406 |
class="redux-slider-value-two-' . $fieldID . ' ' . $this->field['class'] . '" |
| 407 |
name="' . $nameTwo . '" |
| 408 |
id="' . $idTwo . '" |
| 409 |
value="' . $valTwo . '"/>'; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
} |