PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/oldpaper/framework/redux-framework-master/ReduxCore/inc/fields/slider/field_slider.php

https://gitlab.com/eita/agencia-consumo-responsavel
PHP | 412 lines | 270 code | 74 blank | 68 comment | 65 complexity | 56993a3f7d38174cf5c52275e77b3a2b MD5 | raw file
  1. <?php
  2. /**
  3. * Redux Framework is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * any later version.
  7. * Redux Framework is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. * You should have received a copy of the GNU General Public License
  12. * along with Redux Framework. If not, see <http://www.gnu.org/licenses/>.
  13. *
  14. * @package ReduxFramework
  15. * @subpackage Field_Slider
  16. * @author Kevin Provance (kprovance)
  17. * @version 2.0.0
  18. */
  19. // Exit if accessed directly
  20. if ( ! defined( 'ABSPATH' ) ) {
  21. exit;
  22. }
  23. if ( ! class_exists( 'ReduxFramework_slider' ) ) {
  24. class ReduxFramework_slider {
  25. /**
  26. * Field Constructor.
  27. * Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function
  28. *
  29. * @since ReduxFramework 3.1.8
  30. */
  31. private $display_none = 0;
  32. private $display_label = 1;
  33. private $display_text = 2;
  34. private $display_select = 3;
  35. function __construct( $field = array(), $value = '', $parent ) {
  36. //parent::__construct( $parent->sections, $parent->args );
  37. $this->parent = $parent;
  38. $this->field = $field;
  39. $this->value = $value;
  40. // Set defaults
  41. $defaults = array(
  42. 'handles' => 1,
  43. 'resolution' => 1,
  44. 'display_value' => 'text',
  45. 'float_mark' => '.',
  46. );
  47. $this->field = wp_parse_args( $this->field, $defaults );
  48. // Sanitize float mark
  49. switch ( $this->field['float_mark'] ) {
  50. case ',':
  51. case '.':
  52. break;
  53. default:
  54. $this->field['float_mark'] = '.';
  55. break;
  56. }
  57. // Sanitize resolution value
  58. $this->field['resolution'] = $this->cleanVal( $this->field['resolution'] );
  59. // Sanitize handle value
  60. switch ( $this->field['handles'] ) {
  61. case 0:
  62. case 1:
  63. $this->field['handles'] = 1;
  64. break;
  65. default:
  66. $this->field['handles'] = 2;
  67. break;
  68. }
  69. // Sanitize display value
  70. switch ( $this->field['display_value'] ) {
  71. case 'label':
  72. $this->field['display_value'] = $this->display_label;
  73. break;
  74. case 'text':
  75. default:
  76. $this->field['display_value'] = $this->display_text;
  77. break;
  78. case 'select':
  79. $this->field['display_value'] = $this->display_select;
  80. break;
  81. case 'none':
  82. $this->field['display_value'] = $this->display_none;
  83. break;
  84. }
  85. }
  86. private function cleanVal( $var ) {
  87. if ( is_float( $var ) ) {
  88. $cleanVar = floatval( $var );
  89. } else {
  90. $cleanVar = intval( $var );
  91. }
  92. return $cleanVar;
  93. }
  94. private function cleanDefault( $val ) {
  95. if ( empty( $val ) && ! empty( $this->field['default'] ) && $this->cleanVal( $this->field['min'] ) >= 1 ) {
  96. $val = $this->cleanVal( $this->field['default'] );
  97. }
  98. if ( empty( $val ) && $this->cleanVal( $this->field['min'] ) >= 1 ) {
  99. $val = $this->cleanVal( $this->field['min'] );
  100. }
  101. if ( empty( $val ) ) {
  102. $val = 0;
  103. }
  104. // Extra Validation
  105. if ( $val < $this->field['min'] ) {
  106. $val = $this->cleanVal( $this->field['min'] );
  107. } else if ( $val > $this->field['max'] ) {
  108. $val = $this->cleanVal( $this->field['max'] );
  109. }
  110. return $val;
  111. }
  112. private function cleanDefaultArray( $val ) {
  113. $one = $this->value[1];
  114. $two = $this->value[2];
  115. if ( empty( $one ) && ! empty( $this->field['default'][1] ) && $this->cleanVal( $this->field['min'] ) >= 1 ) {
  116. $one = $this->cleanVal( $this->field['default'][1] );
  117. }
  118. if ( empty( $one ) && $this->cleanVal( $this->field['min'] ) >= 1 ) {
  119. $one = $this->cleanVal( $this->field['min'] );
  120. }
  121. if ( empty( $one ) ) {
  122. $one = 0;
  123. }
  124. if ( empty( $two ) && ! empty( $this->field['default'][2] ) && $this->cleanVal( $this->field['min'] ) >= 1 ) {
  125. $two = $this->cleanVal( $this->field['default'][1] + 1 );
  126. }
  127. if ( empty( $two ) && $this->cleanVal( $this->field['min'] ) >= 1 ) {
  128. $two = $this->cleanVal( $this->field['default'][1] + 1 );
  129. }
  130. if ( empty( $two ) ) {
  131. $two = $this->field['default'][1] + 1;
  132. }
  133. $val[0] = $one;
  134. $val[1] = $two;
  135. return $val;
  136. }
  137. /**
  138. * Clean the field data to the fields defaults given the parameters.
  139. *
  140. * @since Redux_Framework 3.1.8
  141. */
  142. function clean() {
  143. // Set min to 0 if no value is set.
  144. $this->field['min'] = empty( $this->field['min'] ) ? 0 : $this->cleanVal( $this->field['min'] );
  145. // Set max to min + 1 if empty.
  146. $this->field['max'] = empty( $this->field['max'] ) ? $this->field['min'] + 1 : $this->cleanVal( $this->field['max'] );
  147. // Set step to 1 if step is empty ot step > max.
  148. $this->field['step'] = empty( $this->field['step'] ) || $this->field['step'] > $this->field['max'] ? 1 : $this->cleanVal( $this->field['step'] );
  149. if ( 2 == $this->field['handles'] ) {
  150. if ( ! is_array( $this->value ) ) {
  151. $this->value[1] = 0;
  152. $this->value[2] = 1;
  153. }
  154. $this->value = $this->cleanDefaultArray( $this->value );
  155. } else {
  156. if ( is_array( $this->value ) ) {
  157. $this->value = 0;
  158. }
  159. $this->value = $this->cleanDefault( $this->value );
  160. }
  161. // More dummy checks
  162. //if ( ! is_array( $this->field['default'] ) && 2 == $this->field['handles'] ) {
  163. if ( ! is_array( $this->value ) && 2 == $this->field['handles'] ) {
  164. $this->value[0] = $this->field['min'];
  165. $this->value[1] = $this->field['min'] + 1;
  166. }
  167. //if ( is_array( $this->field['default'] ) && 1 == $this->field['handles'] ) {
  168. if ( is_array( $this->value ) && 1 == $this->field['handles'] ) {
  169. $this->value = $this->field['min'];
  170. }
  171. }
  172. /**
  173. * Enqueue Function.
  174. * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
  175. *
  176. * @since ReduxFramework 3.1.8
  177. */
  178. function enqueue() {
  179. $min = Redux_Functions::isMin();
  180. wp_enqueue_style( 'select2-css' );
  181. wp_enqueue_style(
  182. 'redux-nouislider-css',
  183. ReduxFramework::$_url . 'inc/fields/slider/vendor/nouislider/redux.jquery.nouislider.css',
  184. array(),
  185. '5.0.0',
  186. 'all'
  187. );
  188. wp_register_script(
  189. 'redux-nouislider-js',
  190. ReduxFramework::$_url . 'inc/fields/slider/vendor/nouislider/redux.jquery.nouislider' . $min . '.js',
  191. array( 'jquery' ),
  192. '5.0.0',
  193. true
  194. );
  195. wp_enqueue_script(
  196. 'redux-field-slider-js',
  197. ReduxFramework::$_url . 'inc/fields/slider/field_slider' . $min . '.js',
  198. array( 'jquery', 'redux-nouislider-js', 'redux-js', 'select2-js' ),
  199. time(),
  200. true
  201. );
  202. if ($this->parent->args['dev_mode']) {
  203. wp_enqueue_style(
  204. 'redux-field-slider-css',
  205. ReduxFramework::$_url . 'inc/fields/slider/field_slider.css',
  206. array(),
  207. time(),
  208. 'all'
  209. );
  210. }
  211. }
  212. //function
  213. /**
  214. * Field Render Function.
  215. * Takes the vars and outputs the HTML for the field in the settings
  216. *
  217. * @since ReduxFramework 0.0.4
  218. */
  219. function render() {
  220. $this->clean();
  221. $fieldID = $this->field['id'];
  222. $fieldName = $this->field['name'] . $this->field['name_suffix'];
  223. //$fieldName = $this->parent->args['opt_name'] . '[' . $this->field['id'] . ']';
  224. // Set handle number variable.
  225. $twoHandles = false;
  226. if ( 2 == $this->field['handles'] ) {
  227. $twoHandles = true;
  228. }
  229. // Set default values(s)
  230. if ( true == $twoHandles ) {
  231. $valOne = $this->value[0];
  232. $valTwo = $this->value[1];
  233. $html = 'data-default-one="' . $valOne . '" ';
  234. $html .= 'data-default-two="' . $valTwo . '" ';
  235. $nameOne = $fieldName . '[1]';
  236. $nameTwo = $fieldName . '[2]';
  237. $idOne = $fieldID . '[1]';
  238. $idTwo = $fieldID . '[2]';
  239. } else {
  240. $valOne = $this->value;
  241. $valTwo = '';
  242. $html = 'data-default-one="' . $valOne . '"';
  243. $nameOne = $fieldName;
  244. $nameTwo = '';
  245. $idOne = $fieldID;
  246. $idTwo = '';
  247. }
  248. $showInput = false;
  249. $showLabel = false;
  250. $showSelect = false;
  251. // TEXT output
  252. if ( $this->display_text == $this->field['display_value'] ) {
  253. $showInput = true;
  254. echo '<input type="text"
  255. name="' . $nameOne . '"
  256. id="' . $idOne . '"
  257. value="' . $valOne . '"
  258. class="redux-slider-input redux-slider-input-one-' . $fieldID . ' ' . $this->field['class'] . '"/>';
  259. // LABEL output
  260. } elseif ( $this->display_label == $this->field['display_value'] ) {
  261. $showLabel = true;
  262. $labelNum = $twoHandles ? '-one' : '';
  263. echo '<div class="redux-slider-label' . $labelNum . '"
  264. id="redux-slider-label-one-' . $fieldID . '"
  265. name="' . $nameOne . '">
  266. </div>';
  267. // SELECT output
  268. } elseif ( $this->display_select == $this->field['display_value'] ) {
  269. $showSelect = true;
  270. if ( isset( $this->field['select2'] ) ) { // if there are any let's pass them to js
  271. $select2_params = json_encode( $this->field['select2'] );
  272. $select2_params = htmlspecialchars( $select2_params, ENT_QUOTES );
  273. echo '<input type="hidden" class="select2_params" value="' . $select2_params . '">';
  274. }
  275. echo '<select class="redux-slider-select-one redux-slider-select-one-' . $fieldID . ' ' . $this->field['class'] . '"
  276. name="' . $nameOne . '"
  277. id="' . $idOne . '">
  278. </select>';
  279. }
  280. // DIV output
  281. echo
  282. '<div
  283. class="redux-slider-container ' . $this->field['class'] . '"
  284. id="' . $fieldID . '"
  285. data-id="' . $fieldID . '"
  286. data-min="' . $this->field['min'] . '"
  287. data-max="' . $this->field['max'] . '"
  288. data-step="' . $this->field['step'] . '"
  289. data-handles="' . $this->field['handles'] . '"
  290. data-display="' . $this->field['display_value'] . '"
  291. data-rtl="' . is_rtl() . '"
  292. data-float-mark="' . $this->field['float_mark'] . '"
  293. data-resolution="' . $this->field['resolution'] . '" ' . $html . '>
  294. </div>';
  295. // Double slider output
  296. if ( true == $twoHandles ) {
  297. // TEXT
  298. if ( true == $showInput ) {
  299. echo '<input type="text"
  300. name="' . $nameTwo . '"
  301. id="' . $idTwo . '"
  302. value="' . $valTwo . '"
  303. class="redux-slider-input redux-slider-input-two-' . $fieldID . ' ' . $this->field['class'] . '"/>';
  304. }
  305. // LABEL
  306. if ( true == $showLabel ) {
  307. echo '<div class="redux-slider-label-two"
  308. id="redux-slider-label-two-' . $fieldID . '"
  309. name="' . $nameTwo . '">
  310. </div>';
  311. }
  312. // SELECT
  313. if ( true == $showSelect ) {
  314. echo '<select class="redux-slider-select-two redux-slider-select-two-' . $fieldID . ' ' . $this->field['class'] . '"
  315. name="' . $nameTwo . '"
  316. id="' . $idTwo . '">
  317. </select>';
  318. }
  319. }
  320. // NO output (input hidden)
  321. if ( $this->display_none == $this->field['display_value'] || $this->display_label == $this->field['display_value'] ) {
  322. echo '<input type="hidden"
  323. class="redux-slider-value-one-' . $fieldID . ' ' . $this->field['class'] . '"
  324. name="' . $nameOne . '"
  325. id="' . $idOne . '"
  326. value="' . $valOne . '"/>';
  327. // double slider hidden output
  328. if ( true == $twoHandles ) {
  329. echo '<input type="hidden"
  330. class="redux-slider-value-two-' . $fieldID . ' ' . $this->field['class'] . '"
  331. name="' . $nameTwo . '"
  332. id="' . $idTwo . '"
  333. value="' . $valTwo . '"/>';
  334. }
  335. }
  336. }
  337. }
  338. }