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