ReduxFramework_dimensions

The evolve ReduxFramework dimensions class.

Defined (1)

The class is defined in the following location(s).

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