/wp-content/themes/osmosis/includes/framework/inc/fields/spacing/field_spacing.php

https://github.com/Canuckaholic/Pop-Digital · PHP · 381 lines · 253 code · 65 blank · 63 comment · 83 complexity · c3f2856916e15704f0f9dcf0f939389c MD5 · raw file

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