balmet.com

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

class-redux-dimensions.php (12642B)


      1 <?php
      2 /**
      3  * Dimension Field
      4  *
      5  * @package     ReduxFramework
      6  * @subpackage  Field_Dimensions
      7  * @author      Kevin Provance (kprovance)
      8  * @version     4.0.0
      9  */
     10 
     11 defined( 'ABSPATH' ) || exit;
     12 
     13 if ( ! class_exists( 'Redux_Dimensions', false ) ) {
     14 
     15 	/**
     16 	 * Class Redux_Dimensions
     17 	 */
     18 	class Redux_Dimensions extends Redux_Field {
     19 
     20 		/**
     21 		 * Set field and value defaults.
     22 		 */
     23 		public function set_defaults() {
     24 			// No errors please.
     25 			$defaults = array(
     26 				'width'          => true,
     27 				'height'         => true,
     28 				'units_extended' => false,
     29 				'units'          => 'px',
     30 				'mode'           => array(
     31 					'width'  => false,
     32 					'height' => false,
     33 				),
     34 			);
     35 
     36 			$this->field = wp_parse_args( $this->field, $defaults );
     37 
     38 			$defaults = array(
     39 				'width'  => '',
     40 				'height' => '',
     41 				'units'  => 'px',
     42 			);
     43 
     44 			$this->value = wp_parse_args( $this->value, $defaults );
     45 
     46 			if ( isset( $this->value['unit'] ) ) {
     47 				$this->value['units'] = $this->value['unit'];
     48 			}
     49 		}
     50 
     51 		/**
     52 		 * Field Render Function.
     53 		 * Takes the vars and outputs the HTML for the field in the settings
     54 		 *
     55 		 * @since ReduxFramework 1.0.0
     56 		 */
     57 		public function render() {
     58 			/*
     59 			 * Acceptable values checks.  If the passed variable doesn't pass muster, we unset them
     60 			 * and reset them with default values to avoid errors.
     61 			 */
     62 
     63 			$arr_units = Redux_Helpers::$array_units;
     64 
     65 			$unit_check   = $arr_units;
     66 			$unit_check[] = false;
     67 
     68 			// If units field has a value but is not an acceptable value, unset the variable.
     69 			if ( isset( $this->field['units'] ) && ! Redux_Helpers::array_in_array( $this->field['units'], $unit_check ) ) {
     70 				unset( $this->field['units'] );
     71 			}
     72 
     73 			// If there is a default unit value  but is not an accepted value, unset the variable.
     74 			if ( isset( $this->value['units'] ) && ! Redux_Helpers::array_in_array( $this->value['units'], $unit_check ) ) {
     75 				unset( $this->value['units'] );
     76 			}
     77 
     78 			/*
     79 			 * Since units field could be an array, string value or bool (to hide the unit field)
     80 			 * we need to separate our functions to avoid those nasty PHP index notices!
     81 			 */
     82 
     83 			// if field units has a value and IS an array, then evaluate as needed.
     84 			if ( isset( $this->field['units'] ) && ! is_array( $this->field['units'] ) ) {
     85 
     86 				// If units fields has a value but units value does not then make units value the field value.
     87 				if ( isset( $this->field['units'] ) && ! isset( $this->value['units'] ) || false === $this->field['units'] ) {
     88 					$this->value['units'] = $this->field['units'];
     89 
     90 					// If units field does NOT have a value and units value does NOT have a value, set both to blank (default?).
     91 				} elseif ( ! isset( $this->field['units'] ) && ! isset( $this->value['units'] ) ) {
     92 					$this->field['units'] = 'px';
     93 					$this->value['units'] = 'px';
     94 
     95 					// If units field has NO value but units value does, then set unit field to value field.
     96 				} elseif ( ! isset( $this->field['units'] ) && isset( $this->value['units'] ) ) {
     97 					$this->field['units'] = $this->value['units'];
     98 
     99 					// if unit value is set and unit value doesn't equal unit field (coz who knows why)
    100 					// then set unit value to unit field.
    101 				} elseif ( isset( $this->value['units'] ) && $this->value['units'] !== $this->field['units'] ) {
    102 					$this->value['units'] = $this->field['units'];
    103 				}
    104 
    105 				// do stuff based on unit field NOT set as an array.
    106 				// phpcs:ignore Generic.CodeAnalysis.EmptyStatement
    107 			} elseif ( isset( $this->field['units'] ) && is_array( $this->field['units'] ) ) {
    108 				// nothing to do here, but I'm leaving the construct just in case I have to debug this again.
    109 			}
    110 
    111 			echo '<fieldset id="' . esc_attr( $this->field['id'] ) . '-fieldset" class="redux-dimensions-container" data-id="' . esc_attr( $this->field['id'] ) . '">';
    112 
    113 			$this->select2_config['allowClear'] = false;
    114 
    115 			if ( isset( $this->field['select2'] ) ) {
    116 				$this->field['select2'] = wp_parse_args( $this->field['select2'], $this->select2_config );
    117 			} else {
    118 				$this->field['select2'] = $this->select2_config;
    119 			}
    120 
    121 			$this->field['select2'] = Redux_Functions::sanitize_camel_case_array_keys( $this->field['select2'] );
    122 
    123 			$select2_data = Redux_Functions::create_data_string( $this->field['select2'] );
    124 
    125 			// This used to be unit field, but was giving the PHP index error when it was an array,
    126 			// so I changed it.
    127 			echo '<input type="hidden" class="field-units" value="' . esc_attr( $this->value['units'] ) . '">';
    128 
    129 			/**
    130 			 * Width
    131 			 * */
    132 			if ( true === $this->field['width'] ) {
    133 				if ( ! empty( $this->value['width'] ) && false !== $this->value['units'] && strpos( $this->value['width'], strval( $this->value['units'] ) ) === false ) {
    134 					$this->value['width'] = filter_var( $this->value['width'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
    135 					if ( false !== $this->field['units'] ) {
    136 						$this->value['width'] .= $this->value['units'];
    137 					}
    138 				}
    139 				echo '<div class="field-dimensions-input input-prepend">';
    140 				echo '<span class="add-on"><i class="el el-resize-horizontal icon-large"></i></span>';
    141 				echo '<input
    142 						type="text"
    143 						class="redux-dimensions-input redux-dimensions-width mini ' . esc_attr( $this->field['class'] ) . '"
    144 						placeholder="' . esc_html__( 'Width', 'redux-framework' ) . '"
    145 						rel="' . esc_attr( $this->field['id'] ) . '-width"
    146 						value="' . esc_attr( filter_var( $this->value['width'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) ) . '">';
    147 
    148 				echo '<input
    149 						data-id="' . esc_attr( $this->field['id'] ) . '"
    150 						type="hidden"
    151 						id="' . esc_attr( $this->field['id'] ) . '-width"
    152 						name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] ) . '[width]"
    153 						value="' . esc_attr( $this->value['width'] ) . '">';
    154 
    155 				echo '</div>';
    156 			}
    157 
    158 			/**
    159 			 * Height
    160 			 * */
    161 			if ( true === $this->field['height'] ) {
    162 				if ( ! empty( $this->value['height'] ) && false !== $this->value['units'] && strpos( $this->value['height'], strval( $this->value['units'] ) ) === false ) {
    163 					$this->value['height'] = filter_var( $this->value['height'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
    164 					if ( false !== $this->field['units'] ) {
    165 						$this->value['height'] .= $this->value['units'];
    166 					}
    167 				}
    168 				echo '<div class="field-dimensions-input input-prepend">';
    169 				echo '<span class="add-on"><i class="el el-resize-vertical icon-large"></i></span>';
    170 				echo '<input
    171 						type="text"
    172 						class="redux-dimensions-input redux-dimensions-height mini ' . esc_attr( $this->field['class'] ) . '"
    173 						placeholder="' . esc_html__( 'Height', 'redux-framework' ) . '"
    174 						rel="' . esc_attr( $this->field['id'] ) . '-height"
    175 						value="' . esc_attr( filter_var( $this->value['height'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) ) . '">';
    176 
    177 				echo '<input
    178 						data-id="' . esc_attr( $this->field['id'] ) . '"
    179 						type="hidden"
    180 						id="' . esc_attr( $this->field['id'] ) . '-height"
    181 						name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] ) . '[height]"
    182 						value="' . esc_attr( $this->value['height'] ) . '">';
    183 						echo '</div>';
    184 			}
    185 
    186 			/**
    187 			 * Units
    188 			 * */
    189 			// If units field is set and units field NOT false then fill out the options object and show it, otherwise it's hidden
    190 			// and the default units value will apply.
    191 			if ( isset( $this->field['units'] ) && false !== $this->field['units'] ) {
    192 				echo '<div
    193 						class="select_wrapper dimensions-units"
    194 						original-title="' . esc_html__( 'Units', 'redux-framework' ) . '">';
    195 
    196 				echo '<select
    197 						data-id="' . esc_attr( $this->field['id'] ) . '"
    198 						data-placeholder="' . esc_html__( 'Units', 'redux-framework' ) . '"
    199 						class="redux-dimensions redux-dimensions-units select ' . esc_attr( $this->field['class'] ) . '"
    200 						original-title="' . esc_html__( 'Units', 'redux-framework' ) . '"
    201 						name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] ) . '[units]"' . esc_attr( $select2_data ) . '>';
    202 
    203 				// Extended units, show 'em all.
    204 				if ( $this->field['units_extended'] ) {
    205 					$test_units = $arr_units;
    206 				} else {
    207 					$test_units = array( 'px', 'em', 'rem', '%' );
    208 				}
    209 
    210 				if ( '' !== $this->field['units'] && is_array( $this->field['units'] ) ) {
    211 					$test_units = $this->field['units'];
    212 				}
    213 
    214 				if ( in_array( $this->field['units'], $test_units, true ) ) {
    215 					echo '<option value="' . esc_attr( $this->field['units'] ) . '" selected="selected">' . esc_attr( $this->field['units'] ) . '</option>';
    216 				} else {
    217 					foreach ( $test_units as $a_unit ) {
    218 						echo '<option value="' . esc_attr( $a_unit ) . '" ' . selected( $this->value['units'], $a_unit, false ) . '>' . esc_attr( $a_unit ) . '</option>';
    219 					}
    220 				}
    221 				echo '</select></div>';
    222 			};
    223 
    224 			echo '</fieldset>';
    225 		}
    226 
    227 		/**
    228 		 * Enqueue Function.
    229 		 * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
    230 		 *
    231 		 * @since ReduxFramework 1.0.0
    232 		 */
    233 		public function enqueue() {
    234 			wp_enqueue_style( 'select2-css' );
    235 
    236 			wp_enqueue_script(
    237 				'redux-field-dimensions-js',
    238 				Redux_Core::$url . 'inc/fields/dimensions/redux-dimensions' . Redux_Functions::is_min() . '.js',
    239 				array( 'jquery', 'select2-js', 'redux-js' ),
    240 				$this->timestamp,
    241 				true
    242 			);
    243 
    244 			if ( $this->parent->args['dev_mode'] ) {
    245 				wp_enqueue_style(
    246 					'redux-field-dimensions-css',
    247 					Redux_Core::$url . 'inc/fields/dimensions/redux-dimensions.css',
    248 					array(),
    249 					$this->timestamp
    250 				);
    251 			}
    252 		}
    253 
    254 		/**
    255 		 * Compile CSS style for output.
    256 		 *
    257 		 * @param string $data CSS data.
    258 		 *
    259 		 * @return string|void
    260 		 */
    261 		public function css_style( $data ) {
    262 			$style = '';
    263 
    264 			// If field units has a value and IS an array, then evaluate as needed.
    265 			if ( isset( $this->field['units'] ) && ! is_array( $this->field['units'] ) ) {
    266 
    267 				// If units fields has a value but units value does not then make units value the field value.
    268 				if ( isset( $this->field['units'] ) && ! isset( $this->value['units'] ) || false === $this->field['units'] ) {
    269 					$this->value['units'] = $this->field['units'];
    270 
    271 					// If units field does NOT have a value and units value does NOT have a value, set both to blank (default?).
    272 				} elseif ( ! isset( $this->field['units'] ) && ! isset( $this->value['units'] ) ) {
    273 					$this->field['units'] = 'px';
    274 					$this->value['units'] = 'px';
    275 
    276 					// If units field has NO value but units value does, then set unit field to value field.
    277 				} elseif ( ! isset( $this->field['units'] ) && isset( $this->value['units'] ) ) {
    278 					$this->field['units'] = $this->value['units'];
    279 
    280 					// If unit value is set and unit value doesn't equal unit field (coz who knows why)
    281 					// then set unit value to unit field.
    282 				} elseif ( isset( $this->value['units'] ) && $this->field['units'] !== $this->value['units'] ) {
    283 					$this->value['units'] = $this->field['units'];
    284 				}
    285 
    286 				// Do stuff based on unit field NOT set as an array.
    287 				// phpcs:ignore Generic.CodeAnalysis.EmptyStatement
    288 			} elseif ( isset( $this->field['units'] ) && is_array( $this->field['units'] ) ) {
    289 				// nothing to do here, but I'm leaving the construct just in case I have to debug this again.
    290 			}
    291 
    292 			$units = isset( $this->value['units'] ) ? $this->value['units'] : '';
    293 
    294 			if ( ! is_array( $this->field['mode'] ) ) {
    295 				$height = isset( $this->field['mode'] ) && ! empty( $this->field['mode'] ) ? $this->field['mode'] : 'height';
    296 				$width  = isset( $this->field['mode'] ) && ! empty( $this->field['mode'] ) ? $this->field['mode'] : 'width';
    297 			} else {
    298 				$height = false !== $this->field['mode']['height'] ? $this->field['mode']['height'] : 'height';
    299 				$width  = false !== $this->field['mode']['width'] ? $this->field['mode']['width'] : 'width';
    300 			}
    301 
    302 			$clean_value = array(
    303 				$height => isset( $this->value['height'] ) ? filter_var( $this->value['height'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : '',
    304 				$width  => isset( $this->value['width'] ) ? filter_var( $this->value['width'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : '',
    305 			);
    306 
    307 			foreach ( $clean_value as $key => $value ) {
    308 				// Output if it's a numeric entry.
    309 				if ( isset( $value ) && is_numeric( $value ) ) {
    310 					$style .= $key . ':' . $value . $units . ';';
    311 				}
    312 			}
    313 
    314 			return $style;
    315 		}
    316 
    317 		/**
    318 		 * Enable output_variables to be generated.
    319 		 *
    320 		 * @since       4.0.3
    321 		 * @return void
    322 		 */
    323 		public function output_variables() {
    324 			// No code needed, just defining the method is enough.
    325 		}
    326 	}
    327 }
    328 
    329 class_alias( 'Redux_Dimensions', 'ReduxFramework_Dimensions' );