balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-redux-spinner.php (5663B)


      1 <?php
      2 /**
      3  * Spinner Field
      4  *
      5  * @package     Redux Framework/Fields
      6  * @author      Dovy Paukstys & Kevin Provance (kprovance)
      7  * @version     4.0.0
      8  */
      9 
     10 defined( 'ABSPATH' ) || exit;
     11 
     12 if ( ! class_exists( 'Redux_Spinner', false ) ) {
     13 
     14 	/**
     15 	 * Class Redux_Spinner
     16 	 */
     17 	class Redux_Spinner extends Redux_Field {
     18 
     19 		/**
     20 		 * Set field and value defaults.
     21 		 */
     22 		public function set_defaults() {
     23 			$params = array(
     24 				'min'     => 0,
     25 				'max'     => 1,
     26 				'step'    => 1,
     27 				'default' => '',
     28 				'edit'    => true,
     29 				'plus'    => '+',
     30 				'minus'   => '-',
     31 				'format'  => '',
     32 				'prefix'  => '',
     33 				'suffix'  => '',
     34 				'point'   => '.',
     35 				'places'  => null,
     36 			);
     37 
     38 			$this->field = wp_parse_args( $this->field, $params );
     39 		}
     40 
     41 		/**
     42 		 * Field Render Function.
     43 		 * Takes the vars and outputs the HTML for the field in the settings
     44 		 *
     45 		 * @since ReduxFramework 3.0.0
     46 		 */
     47 		public function render() {
     48 			$data_string = '';
     49 
     50 			foreach ( $this->field as $key => $val ) {
     51 				if ( in_array( $key, array( 'min', 'max', 'step', 'default', 'plus', 'minus', 'prefix', 'suffix', 'point', 'places' ), true ) ) {
     52 					$data_string .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $val ) . '" ';
     53 				}
     54 			}
     55 
     56 			$data_string .= ' data-val=' . $this->value;
     57 
     58 			// Don't allow input edit if there's a step.
     59 			$readonly = '';
     60 			if ( isset( $this->field['edit'] ) && false === $this->field['edit'] ) {
     61 				$readonly = ' readonly="readonly"';
     62 			}
     63 
     64 			echo '<div id="' . esc_attr( $this->field['id'] ) . '-spinner" class="redux_spinner" rel="' . esc_attr( $this->field['id'] ) . '">';
     65 
     66 			echo '<input type="text" ' . $data_string . ' name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] ) . '" id="' . esc_attr( $this->field['id'] ) . '" value="' . esc_attr( $this->value ) . '" class="mini spinner-input ' . esc_attr( $this->field['class'] ) . '"' . $readonly . '/>'; // phpcs:ignore WordPress.Security.EscapeOutput
     67 
     68 			echo '</div>';
     69 		}
     70 
     71 		/**
     72 		 * Clean the field data to the fields defaults given the parameters.
     73 		 *
     74 		 * @since Redux_Framework 3.1.1
     75 		 */
     76 		private function clean() {
     77 			if ( empty( $this->field['min'] ) ) {
     78 				$this->field['min'] = 0;
     79 			} else {
     80 				$this->field['min'] = intval( $this->field['min'] );
     81 			}
     82 
     83 			if ( empty( $this->field['max'] ) ) {
     84 				$this->field['max'] = intval( $this->field['min'] ) + 1;
     85 			} else {
     86 				$this->field['max'] = intval( $this->field['max'] );
     87 			}
     88 
     89 			if ( empty( $this->field['step'] ) || $this->field['step'] > $this->field['max'] ) {
     90 				$this->field['step'] = 1;
     91 			} else {
     92 				$this->field['step'] = intval( $this->field['step'] );
     93 			}
     94 
     95 			if ( empty( $this->value ) && ! empty( $this->field['default'] ) && intval( $this->field['min'] ) >= 1 ) {
     96 				$this->value = intval( $this->field['default'] );
     97 			}
     98 
     99 			if ( empty( $this->value ) && intval( $this->field['min'] ) >= 1 ) {
    100 				$this->value = intval( $this->field['min'] );
    101 			}
    102 
    103 			if ( empty( $this->value ) ) {
    104 				$this->value = 0;
    105 			}
    106 
    107 			// Extra Validation.
    108 			if ( $this->value < $this->field['min'] ) {
    109 				$this->value = intval( $this->field['min'] );
    110 			} elseif ( $this->value > $this->field['max'] ) {
    111 				$this->value = intval( $this->field['max'] );
    112 			}
    113 		}
    114 
    115 		/**
    116 		 * Enqueue Function.
    117 		 * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
    118 		 *
    119 		 * @since ReduxFramework 3.0.0
    120 		 */
    121 		public function enqueue() {
    122 			wp_enqueue_script(
    123 				'redux-field-spinner-custom-js',
    124 				Redux_Core::$url . 'inc/fields/spinner/vendor/jquery.ui.spinner' . Redux_Functions::is_min() . '.js',
    125 				array( 'jquery', 'redux-js' ),
    126 				$this->timestamp,
    127 				true
    128 			);
    129 
    130 			wp_enqueue_script(
    131 				'redux-field-spinner-js',
    132 				Redux_Core::$url . 'inc/fields/spinner/redux-spinner' . Redux_Functions::is_min() . '.js',
    133 				array( 'jquery', 'redux-field-spinner-custom-js', 'jquery-ui-core', 'jquery-ui-dialog', 'redux-js' ),
    134 				$this->timestamp,
    135 				true
    136 			);
    137 
    138 			if ( $this->parent->args['dev_mode'] ) {
    139 				wp_enqueue_style(
    140 					'redux-field-spinner-css',
    141 					Redux_Core::$url . 'inc/fields/spinner/redux-spinner.css',
    142 					array(),
    143 					$this->timestamp
    144 				);
    145 			}
    146 		}
    147 
    148 		/**
    149 		 * CSS/compiler output.
    150 		 *
    151 		 * @param string|null|array $style CSS styles.
    152 		 */
    153 		public function output( $style = '' ) {
    154 			$style = '';
    155 
    156 			if ( ! empty( $this->value ) ) {
    157 				if ( ! empty( $this->field['output'] ) && is_array( $this->field['output'] ) ) {
    158 					$css                      = $this->parse_css( $this->value, $this->field['output'] );
    159 					$this->parent->outputCSS .= esc_attr( $css );
    160 				}
    161 
    162 				if ( ! empty( $this->field['compiler'] ) && is_array( $this->field['compiler'] ) ) {
    163 					$css                        = $this->parse_css( $this->value, $this->field['compiler'] );
    164 					$this->parent->compilerCSS .= esc_attr( $css );
    165 				}
    166 			}
    167 		}
    168 
    169 		/**
    170 		 * Compile CSS data for output.
    171 		 *
    172 		 * @param mixed $value Value.
    173 		 * @param mixed $output .
    174 		 *
    175 		 * @return string
    176 		 */
    177 		private function parse_css( $value, $output ): string {
    178 			// No notices.
    179 			$css = '';
    180 
    181 			$unit = $this->field['output_unit'] ?? 'px';
    182 
    183 			// Must be an array.
    184 			if ( is_array( $output ) ) {
    185 				foreach ( $output as $selector => $mode ) {
    186 					if ( '' !== $mode && '' !== $selector ) {
    187 						$css .= $selector . '{' . $mode . ':' . $value . $unit . ';}';
    188 					}
    189 				}
    190 			}
    191 
    192 			return $css;
    193 		}
    194 
    195 		/**
    196 		 * Enable output_variables to be generated.
    197 		 *
    198 		 * @since       4.0.3
    199 		 * @return void
    200 		 */
    201 		public function output_variables() {
    202 			// No code needed, just defining the method is enough.
    203 		}
    204 	}
    205 }
    206 
    207 class_alias( 'Redux_Spinner', 'ReduxFramework_Spinner' );