1, 'resolution' => 1, 'display_value' => 'text', 'float_mark' => '.', 'forced' => true, 'min' => 0, 'max' => 1, 'step' => 1, ); $this->field = wp_parse_args( $this->field, $defaults ); // Sanitize float mark. if ( ',' !== $this->field['float_mark'] && '.' !== $this->field['float_mark'] ) { $this->field['float_mark'] = '.'; } // Sanitize resolution value. $this->field['resolution'] = $this->clean_val( $this->field['resolution'] ); // Sanitize handle value. if ( 0 === $this->field['handles'] || 1 === $this->field['handles'] ) { $this->field['handles'] = 1; } else { $this->field['handles'] = 2; } // Sanitize display value. if ( 'label' === $this->field['display_value'] ) { $this->field['display_value'] = $this->display_label; } elseif ( 'select' === $this->field['display_value'] ) { $this->field['display_value'] = $this->display_select; } elseif ( 'none' === $this->field['display_value'] ) { $this->field['display_value'] = $this->display_none; } else { $this->field['display_value'] = $this->display_text; } } /** * Sanitize value. * * @param mixed $val Value to sanitize. * * @return float|int */ private function clean_val( $val ) { if ( is_float( $val ) ) { $clear_var = floatval( $val ); } else { $clear_var = intval( $val ); } return $clear_var; } /** * Clean default values. * * @param mixed $val Default values. * * @return float|int */ private function clean_default( $val ) { if ( empty( $val ) && ! empty( $this->field['default'] ) && $this->clean_val( $this->field['min'] ) >= 1 ) { $val = $this->clean_val( $this->field['default'] ); } if ( empty( $val ) && $this->clean_val( $this->field['min'] ) >= 1 ) { $val = $this->clean_val( $this->field['min'] ); } if ( empty( $val ) ) { $val = 0; } // Extra Validation. if ( $val < $this->field['min'] ) { $val = $this->clean_val( $this->field['min'] ); } elseif ( $val > $this->field['max'] ) { $val = $this->clean_val( $this->field['max'] ); } return $val; } /** * Sanitize default array. * * @param array $val Defaults. * * @return array */ private function clean_default_array( array $val ): array { $one = $this->value[1]; $two = $this->value[2]; if ( empty( $one ) && ! empty( $this->field['default'][1] ) && $this->clean_val( $this->field['min'] ) >= 1 ) { $one = $this->clean_val( $this->field['default'][1] ); } if ( empty( $one ) && $this->clean_val( $this->field['min'] ) >= 1 ) { $one = $this->clean_val( $this->field['min'] ); } if ( empty( $one ) ) { $one = 0; } if ( empty( $two ) && ! empty( $this->field['default'][2] ) && $this->clean_val( $this->field['min'] ) >= 1 ) { $two = $this->clean_val( $this->field['default'][1] + 1 ); } if ( empty( $two ) && $this->clean_val( $this->field['min'] ) >= 1 ) { $two = $this->clean_val( $this->field['default'][1] + 1 ); } if ( empty( $two ) ) { $two = $this->field['default'][1] + 1; } $val[0] = $one; $val[1] = $two; return $val; } /** * Clean the field data to the fields defaults given the parameters. * * @since Redux_Framework 3.1.8 */ private function clean() { // Set min to 0 if no value is set. $this->field['min'] = empty( $this->field['min'] ) ? 0 : $this->clean_val( $this->field['min'] ); // Set max to min + 1 if empty. $this->field['max'] = empty( $this->field['max'] ) ? $this->field['min'] + 1 : $this->clean_val( $this->field['max'] ); // Set step to 1 if step is empty ot step > max. $this->field['step'] = empty( $this->field['step'] ) || $this->field['step'] > $this->field['max'] ? 1 : $this->clean_val( $this->field['step'] ); if ( 2 === $this->field['handles'] ) { if ( ! is_array( $this->value ) ) { $this->value[1] = 0; $this->value[2] = 1; } $this->value = $this->clean_default_array( $this->value ); } else { if ( is_array( $this->value ) ) { $this->value = 0; } $this->value = $this->clean_default( $this->value ); } // More dummy checks. if ( ! is_array( $this->value ) && 2 === $this->field['handles'] ) { $this->value[0] = $this->field['min']; $this->value[1] = $this->field['min'] + 1; } if ( is_array( $this->value ) && 1 === $this->field['handles'] ) { $this->value = $this->field['min']; } } /** * Enqueue Function. * If this field requires any scripts, or css define this function and register/enqueue the scripts/css * * @since ReduxFramework 3.1.8 */ public function enqueue() { $min = Redux_Functions::is_min(); wp_enqueue_style( 'select2-js' ); wp_enqueue_style( 'redux-nouislider', Redux_Core::$url . "assets/css/vendor/nouislider$min.css", array(), '5.0.0' ); wp_register_script( 'redux-nouislider', Redux_Core::$url . 'assets/js/vendor/nouislider/redux.jquery.nouislider' . $min . '.js', array( 'jquery' ), '5.0.0', true ); wp_enqueue_script( 'redux-field-slider', Redux_Core::$url . 'inc/fields/slider/redux-slider' . $min . '.js', array( 'jquery', 'redux-nouislider', 'redux-js', 'select2-js' ), $this->timestamp, true ); if ( $this->parent->args['dev_mode'] ) { wp_enqueue_style( 'redux-field-slider', Redux_Core::$url . 'inc/fields/slider/redux-slider.css', array(), $this->timestamp ); } } /** * Field Render Function. * Takes the vars and outputs the HTML for the field in the settings * * @since ReduxFramework 0.0.4 */ public function render() { $this->clean(); $field_id = $this->field['id']; $field_name = $this->field['name'] . $this->field['name_suffix']; // Set handle number variable. $two_handles = false; if ( 2 === $this->field['handles'] ) { $two_handles = true; } // Set default values(s). if ( true === $two_handles ) { $val_one = $this->value[0]; $val_two = $this->value[1]; $html = 'data-default-one=' . $val_one . ' '; $html .= 'data-default-two=' . $val_two . ' '; $name_one = $field_name . '[1]'; $name_two = $field_name . '[2]'; $id_one = $field_id . '[1]'; $id_two = $field_id . '[2]'; } else { $val_one = $this->value; $val_two = ''; $html = 'data-default-one=' . $val_one; $name_one = $field_name; $name_two = ''; $id_one = $field_id; $id_two = ''; } $show_input = false; $show_label = false; $show_select = false; // TEXT output. if ( $this->display_text === $this->field['display_value'] ) { $show_input = true; echo ''; // LABEL output. } elseif ( $this->display_label === $this->field['display_value'] ) { $show_label = true; $label_num = $two_handles ? '-one' : ''; echo '
'; // SELECT output. } elseif ( $this->display_select === $this->field['display_value'] ) { $show_select = true; if ( isset( $this->field['select2'] ) ) { $this->field['select2'] = wp_parse_args( $this->field['select2'], $this->select2_config ); } else { $this->field['select2'] = $this->select2_config; } $this->field['select2'] = Redux_Functions::sanitize_camel_case_array_keys( $this->field['select2'] ); $this->select2_data = Redux_Functions::create_data_string( $this->field['select2'] ); echo ''; } // DIV output. echo ''; // Double slider output. if ( true === $two_handles ) { // TEXT. if ( true === $show_input ) { echo ''; } // LABEL. if ( true === $show_label ) { echo ''; } // SELECT. if ( true === $show_select ) { echo ''; } } // NO output (input hidden). if ( $this->display_none === $this->field['display_value'] || $this->display_label === $this->field['display_value'] ) { echo ''; // double slider hidden output. if ( true === $two_handles ) { echo ''; } } } /** * Enable output_variables to be generated. * * @since 4.0.3 * @return void */ public function output_variables() { // No code needed, just defining the method is enough. } } } class_alias( 'Redux_Slider', 'ReduxFramework_Slider' );