ReduxFramework_spacing

The evolve ReduxFramework spacing class.

Defined (1)

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

/library/admin/redux-framework/inc/fields/spacing/field_spacing.php  
  1. class ReduxFramework_spacing { 
  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.  
  13. /** 
  14. * Field Render Function. 
  15. * Takes the vars and outputs the HTML for the field in the settings 
  16. * @since ReduxFramework 1.0.0 
  17. */ 
  18. function render() { 
  19. /** 
  20. * So, in_array() wasn't doing it's job for checking a passed array for a proper value. 
  21. * It's wonky. It only wants to check the keys against our array of acceptable values, and not the key's 
  22. * value. So we'll use this instead. Fortunately, a single no array value can be passed and it won't 
  23. * take a dump. 
  24. */ 
  25.  
  26. // No errors please 
  27. // Set field values 
  28. $defaults = array( 
  29. 'units' => '',  
  30. 'mode' => 'padding',  
  31. 'top' => true,  
  32. 'bottom' => true,  
  33. 'all' => false,  
  34. 'left' => true,  
  35. 'right' => true,  
  36. 'units_extended' => false,  
  37. 'display_units' => true 
  38. ); 
  39.  
  40. $this->field = wp_parse_args( $this->field, $defaults ); 
  41.  
  42. // Set default values 
  43. $defaults = array( 
  44. 'top' => '',  
  45. 'right' => '',  
  46. 'bottom' => '',  
  47. 'left' => '',  
  48. 'units' => 'px' 
  49. ); 
  50.  
  51. $this->value = wp_parse_args( $this->value, $defaults ); 
  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. 'rem',  
  68. 'ex',  
  69. 'pt',  
  70. 'pc',  
  71. 'px' 
  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. 'rem',  
  85. 'ex',  
  86. 'pt',  
  87. 'pc',  
  88. 'px' 
  89. ) ) 
  90. ) { 
  91. unset( $this->value['units'] ); 
  92.  
  93. // if ($this->field['mode'] == "absolute") { 
  94. // $this->field['units'] = ""; 
  95. // $this->value['units'] = ""; 
  96. // } 
  97.  
  98. if ( $this->field['units'] == false ) { 
  99. $this->value == ""; 
  100.  
  101. if ( isset( $this->field['mode'] ) && ! in_array( $this->field['mode'], array( 
  102. 'margin',  
  103. 'padding' 
  104. ) ) 
  105. ) { 
  106. if ( $this->field['mode'] == "absolute" ) { 
  107. $absolute = true; 
  108. $this->field['mode'] = ""; 
  109.  
  110. $value = array( 
  111. 'top' => isset( $this->value[ $this->field['mode'] . '-top' ] ) ? filter_var( $this->value[ $this->field['mode'] . '-top' ], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : filter_var( $this->value['top'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ),  
  112. 'right' => isset( $this->value[ $this->field['mode'] . '-right' ] ) ? filter_var( $this->value[ $this->field['mode'] . '-right' ], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : filter_var( $this->value['right'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ),  
  113. 'bottom' => isset( $this->value[ $this->field['mode'] . '-bottom' ] ) ? filter_var( $this->value[ $this->field['mode'] . '-bottom' ], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : filter_var( $this->value['bottom'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ),  
  114. 'left' => isset( $this->value[ $this->field['mode'] . '-left' ] ) ? filter_var( $this->value[ $this->field['mode'] . '-left' ], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : filter_var( $this->value['left'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) 
  115. ); 
  116.  
  117. // if field units has a value and is NOT an array, then evaluate as needed. 
  118. if ( isset( $this->field['units'] ) && ! is_array( $this->field['units'] ) ) { 
  119.  
  120. //if units fields has a value and is not empty but units value does not then make units value the field value 
  121. if ( isset( $this->field['units'] ) && $this->field['units'] != "" && ! isset( $this->value['units'] ) || $this->field['units'] == false ) { 
  122. $this->value['units'] = $this->field['units']; 
  123.  
  124. // If units field does NOT have a value and units value does NOT have a value, set both to blank (default?) 
  125. } else if ( ! isset( $this->field['units'] ) && ! isset( $this->value['units'] ) ) { 
  126. $this->field['units'] = 'px'; 
  127. $this->value['units'] = 'px'; 
  128.  
  129. // If units field has NO value but units value does, then set unit field to value field 
  130. } else if ( ! isset( $this->field['units'] ) && isset( $this->value['units'] ) ) { // If Value is defined 
  131. $this->field['units'] = $this->value['units']; 
  132.  
  133. // if unit value is set and unit value doesn't equal unit field (coz who knows why) 
  134. // then set unit value to unit field 
  135. } elseif ( isset( $this->value['units'] ) && $this->value['units'] !== $this->field['units'] ) { 
  136. $this->value['units'] = $this->field['units']; 
  137.  
  138. // do stuff based on unit field NOT set as an array 
  139. } elseif ( isset( $this->field['units'] ) && is_array( $this->field['units'] ) ) { 
  140. // nothing to do here, but I'm leaving the construct just in case I have to debug this again. 
  141.  
  142. if ( isset( $this->field['units'] ) ) { 
  143. $value['units'] = $this->value['units']; 
  144.  
  145. $this->value = $value; 
  146.  
  147. if ( ! empty( $this->field['mode'] ) ) { 
  148. $this->field['mode'] = $this->field['mode'] . "-"; 
  149.  
  150.  
  151. $defaults = array( 
  152. 'top' => '',  
  153. 'right' => '',  
  154. 'bottom' => '',  
  155. 'left' => '',  
  156. 'units' => '' 
  157. ); 
  158.  
  159. $this->value = wp_parse_args( $this->value, $defaults ); 
  160.  
  161. if ( isset( $this->field['select2'] ) ) { // if there are any let's pass them to js 
  162. $select2_params = json_encode( $this->field['select2'] ); 
  163. $select2_params = htmlspecialchars( $select2_params, ENT_QUOTES ); 
  164.  
  165. echo '<input type="hidden" class="select2_params" value="' . $select2_params . '">'; 
  166.  
  167. echo '<input type="hidden" class="field-units" value="' . $this->value['units'] . '">'; 
  168.  
  169. if ( isset( $this->field['all'] ) && $this->field['all'] == true ) { 
  170. echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-fullscreen icon-large"></i></span><input type="text" class="redux-spacing-all redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'All', 'evolve' ) . '" rel="' . $this->field['id'] . '-all" value="' . $this->value['top'] . '"></div>'; 
  171.  
  172. if ( $this->field['top'] === true ) { 
  173. echo '<input type="hidden" class="redux-spacing-value" id="' . $this->field['id'] . '-top" name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $this->field['mode'] . 'top]' . '" value="' . $this->value['top'] . ( ! empty( $this->value['top'] ) ? $this->value['units'] : '' ) . '">'; 
  174.  
  175. if ( $this->field['right'] === true ) { 
  176. echo '<input type="hidden" class="redux-spacing-value" id="' . $this->field['id'] . '-right" name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $this->field['mode'] . 'right]' . '" value="' . $this->value['right'] . ( ! empty( $this->value['right'] ) ? $this->value['units'] : '' ) . '">'; 
  177.  
  178. if ( $this->field['bottom'] === true ) { 
  179. echo '<input type="hidden" class="redux-spacing-value" id="' . $this->field['id'] . '-bottom" name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $this->field['mode'] . 'bottom]' . '" value="' . $this->value['bottom'] . ( ! empty( $this->value['bottom'] ) ? $this->value['units'] : '' ) . '">'; 
  180.  
  181. if ( $this->field['left'] === true ) { 
  182. echo '<input type="hidden" class="redux-spacing-value" id="' . $this->field['id'] . '-left" name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $this->field['mode'] . 'left]' . '" value="' . $this->value['left'] . ( ! empty( $this->value['left'] ) ? $this->value['units'] : '' ) . '">'; 
  183.  
  184. if ( ! isset( $this->field['all'] ) || $this->field['all'] !== true ) { 
  185. /** 
  186. * Top 
  187. * */ 
  188. if ( $this->field['top'] === true ) { 
  189. echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-arrow-up icon-large"></i></span><input type="text" class="redux-spacing-top redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'Top', 'evolve' ) . '" rel="' . $this->field['id'] . '-top" value="' . $this->value['top'] . '"></div>'; 
  190.  
  191. /** 
  192. * Right 
  193. * */ 
  194. if ( $this->field['right'] === true ) { 
  195. echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-arrow-right icon-large"></i></span><input type="text" class="redux-spacing-right redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'Right', 'evolve' ) . '" rel="' . $this->field['id'] . '-right" value="' . $this->value['right'] . '"></div>'; 
  196.  
  197. /** 
  198. * Bottom 
  199. * */ 
  200. if ( $this->field['bottom'] === true ) { 
  201. echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-arrow-down icon-large"></i></span><input type="text" class="redux-spacing-bottom redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'Bottom', 'evolve' ) . '" rel="' . $this->field['id'] . '-bottom" value="' . $this->value['bottom'] . '"></div>'; 
  202.  
  203. /** 
  204. * Left 
  205. * */ 
  206. if ( $this->field['left'] === true ) { 
  207. echo '<div class="field-spacing-input input-prepend"><span class="add-on"><i class="el el-arrow-left icon-large"></i></span><input type="text" class="redux-spacing-left redux-spacing-input mini ' . $this->field['class'] . '" placeholder="' . __( 'Left', 'evolve' ) . '" rel="' . $this->field['id'] . '-left" value="' . $this->value['left'] . '"></div>'; 
  208.  
  209. /** 
  210. * Units 
  211. * */ 
  212. if ( $this->field['units'] !== false && is_array( $this->field['units'] ) /** && !isset($absolute) */ && $this->field['display_units'] == true ) { 
  213.  
  214. echo '<div class="select_wrapper spacing-units" original-title="' . __( 'Units', 'evolve' ) . '">'; 
  215. echo '<select data-placeholder="' . __( 'Units', 'evolve' ) . '" class="redux-spacing redux-spacing-units select ' . $this->field['class'] . '" original-title="' . __( 'Units', 'evolve' ) . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[units]' . '" id="' . $this->field['id'] . '_units">'; 
  216.  
  217. if ( $this->field['units_extended'] ) { 
  218. $testUnits = array( 'px', 'em', 'rem', '%', 'in', 'cm', 'mm', 'ex', 'pt', 'pc' ); 
  219. } else { 
  220. $testUnits = array( 'px', 'em', 'pt', 'rem', '%' ); 
  221.  
  222. if ( $this->field['units'] != "" || is_array( $this->field['units'] ) ) { 
  223. $testUnits = $this->field['units']; 
  224.  
  225. echo '<option></option>'; 
  226.  
  227. if ( in_array( $this->field['units'], $testUnits ) ) { 
  228. echo '<option value="' . $this->field['units'] . '" selected="selected">' . $this->field['units'] . '</option>'; 
  229. } else { 
  230. foreach ( $testUnits as $aUnit ) { 
  231. echo '<option value="' . $aUnit . '" ' . selected( $this->value['units'], $aUnit, false ) . '>' . $aUnit . '</option>'; 
  232. echo '</select></div>'; 
  233.  
  234.  
  235. /** 
  236. * Enqueue Function. 
  237. * If this field requires any scripts, or css define this function and register/enqueue the scripts/css 
  238. * @since ReduxFramework 1.0.0 
  239. */ 
  240. function enqueue() { 
  241. wp_enqueue_style( 'select2-css' ); 
  242.  
  243. wp_enqueue_script( 
  244. 'redux-field-spacing-js',  
  245. ReduxFramework::$_url . 'inc/fields/spacing/field_spacing' . Redux_Functions::isMin() . '.js',  
  246. array( 'jquery', 'select2-js', 'redux-js' ),  
  247. time(),  
  248. true 
  249. ); 
  250.  
  251. if ($this->parent->args['dev_mode']) { 
  252. wp_enqueue_style( 
  253. 'redux-field-spacing-css',  
  254. ReduxFramework::$_url . 'inc/fields/spacing/field_spacing.css',  
  255. array(),  
  256. time(),  
  257. 'all' 
  258. ); 
  259. } //function 
  260.  
  261. public function output() { 
  262.  
  263. if ( ! isset( $this->field['mode'] ) ) { 
  264. $this->field['mode'] = "padding"; 
  265.  
  266. if ( isset( $this->field['mode'] ) && ! in_array( $this->field['mode'], array( 
  267. 'padding',  
  268. 'absolute',  
  269. 'margin' 
  270. ) ) 
  271. ) { 
  272. $this->field['mode'] = ""; 
  273.  
  274. $mode = ( $this->field['mode'] != "absolute" ) ? $this->field['mode'] : ""; 
  275. $units = isset( $this->value['units'] ) ? $this->value['units'] : ""; 
  276. $style = ''; 
  277.  
  278. if ( ! empty( $mode ) ) { 
  279. foreach ( $this->value as $key => $value ) { 
  280. if ( $key == "units" ) { 
  281. continue; 
  282.  
  283. // Strip off any alpha for is_numeric test - kp 
  284. $num_no_alpha = preg_replace('/[^\d.-]/', '', $value); 
  285.  
  286. // Output if it's a numeric entry 
  287. if ( isset( $value ) && is_numeric( $num_no_alpha ) ) { 
  288. $style .= $key . ':' . $value . ';'; 
  289.  
  290. } else { 
  291. $this->value['top'] = isset( $this->value['top'] ) ? $this->value['top'] : 0; 
  292. $this->value['bottom'] = isset( $this->value['bottom'] ) ? $this->value['bottom'] : 0; 
  293. $this->value['left'] = isset( $this->value['left'] ) ? $this->value['left'] : 0; 
  294. $this->value['right'] = isset( $this->value['right'] ) ? $this->value['right'] : 0; 
  295.  
  296. $cleanValue = array( 
  297. 'top' => isset( $this->value[ $mode . '-top' ] ) ? filter_var( $this->value[ $mode . '-top' ], FILTER_SANITIZE_NUMBER_INT ) : filter_var( $this->value['top'], FILTER_SANITIZE_NUMBER_INT ),  
  298. 'right' => isset( $this->value[ $mode . '-right' ] ) ? filter_var( $this->value[ $mode . '-right' ], FILTER_SANITIZE_NUMBER_INT ) : filter_var( $this->value['right'], FILTER_SANITIZE_NUMBER_INT ),  
  299. 'bottom' => isset( $this->value[ $mode . '-bottom' ] ) ? filter_var( $this->value[ $mode . '-bottom' ], FILTER_SANITIZE_NUMBER_INT ) : filter_var( $this->value['bottom'], FILTER_SANITIZE_NUMBER_INT ),  
  300. 'left' => isset( $this->value[ $mode . '-left' ] ) ? filter_var( $this->value[ $mode . '-left' ], FILTER_SANITIZE_NUMBER_INT ) : filter_var( $this->value['left'], FILTER_SANITIZE_NUMBER_INT ) 
  301. ); 
  302.  
  303. if ( isset( $this->field['all'] ) && true == $this->field['all'] ) { 
  304. $style .= $mode . 'top:' . $cleanValue['top'] . $units . ';'; 
  305. $style .= $mode . 'bottom:' . $cleanValue['top'] . $units . ';'; 
  306. $style .= $mode . 'right:' . $cleanValue['top'] . $units . ';'; 
  307. $style .= $mode . 'left:' . $cleanValue['top'] . $units . ';'; 
  308. } else { 
  309. if ( true == $this->field['top'] ) { 
  310. $style .= $mode . 'top:' . $cleanValue['top'] . $units . ';'; 
  311.  
  312. if ( true == $this->field['bottom'] ) { 
  313. $style .= $mode . 'bottom:' . $cleanValue['bottom'] . $units . ';'; 
  314.  
  315. if ( true == $this->field['left'] ) { 
  316. $style .= $mode . 'left:' . $cleanValue['left'] . $units . ';'; 
  317.  
  318. if ( true == $this->field['right'] ) { 
  319. $style .= $mode . 'right:' . $cleanValue['right'] . $units . ';'; 
  320.  
  321. if ( ! empty( $style ) ) { 
  322.  
  323. if ( ! empty( $this->field['output'] ) && is_array( $this->field['output'] ) ) { 
  324. $keys = implode( ", ", $this->field['output'] ); 
  325. $this->parent->outputCSS .= $keys . "{" . $style . '}'; 
  326.  
  327. if ( ! empty( $this->field['compiler'] ) && is_array( $this->field['compiler'] ) ) { 
  328. $keys = implode( ", ", $this->field['compiler'] ); 
  329. $this->parent->compilerCSS .= $keys . "{" . $style . '}';