PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-content/themes/minamaze/admin/main/inc/fields/color_rgba/field_color_rgba.php

https://gitlab.com/vanafroo/voipWEB
PHP | 291 lines | 164 code | 41 blank | 86 comment | 26 complexity | 2516e1123adf11f51312f49b8ae15272 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. *
  8. * Redux Framework is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with Redux Framework. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * @package Redux Framework
  17. * @subpackage Spectrum Color Picker
  18. * @author Kevin Provance (kprovance)
  19. * @version 1.0.0
  20. */
  21. // Exit if accessed directly
  22. if( !defined( 'ABSPATH' ) ) {
  23. exit;
  24. }
  25. // Don't duplicate me!
  26. if( !class_exists( 'ReduxFramework_color_rgba' ) ) {
  27. /**
  28. * Main ReduxFramework_color_rgba class
  29. *
  30. * @since 1.0.0
  31. */
  32. class ReduxFramework_color_rgba {
  33. /**
  34. * Class Constructor. Defines the args for the extions class
  35. *
  36. * @since 1.0.0
  37. * @access public
  38. * @param array $field Field sections.
  39. * @param array $value Values.
  40. * @param array $parent Parent object.
  41. * @return void
  42. */
  43. public function __construct( $field = array(), $value ='', $parent ) {
  44. // Set required variables
  45. $this->parent = $parent;
  46. $this->field = $field;
  47. $this->value = $value;
  48. $defaults = array(
  49. 'color' => '',
  50. 'alpha' => 1,
  51. 'rgba' => ''
  52. );
  53. $this->value = wp_parse_args( $this->value, $defaults );
  54. $this->field['options']['show_input'] = isset($this->field['options']['show_input']) ? $this->field['options']['show_input'] : true;
  55. $this->field['options']['show_initial'] = isset($this->field['options']['show_initial']) ? $this->field['options']['show_initial'] : false;
  56. $this->field['options']['show_alpha'] = isset($this->field['options']['show_alpha']) ? $this->field['options']['show_alpha'] : true;
  57. $this->field['options']['show_palette'] = isset($this->field['options']['show_palette']) ? $this->field['options']['show_palette'] : false;
  58. $this->field['options']['show_palette_only'] = isset($this->field['options']['show_palette_only']) ? $this->field['options']['show_palette_only'] : false;
  59. $this->field['options']['max_palette_size'] = isset($this->field['options']['max_palette_size']) ? $this->field['options']['max_palette_size'] : 10;
  60. $this->field['options']['show_selection_palette'] = isset($this->field['options']['show_selection_palette']) ? $this->field['options']['show_selection_palette'] : false;
  61. $this->field['options']['allow_empty'] = isset($this->field['options']['allow_empty']) ? $this->field['options']['allow_empty'] : true;
  62. $this->field['options']['clickout_fires_change'] = isset($this->field['options']['clickout_fires_change']) ? $this->field['options']['clickout_fires_change'] : false;
  63. $this->field['options']['choose_text'] = isset($this->field['options']['choose_text']) ? $this->field['options']['choose_text'] : 'Choose';
  64. $this->field['options']['cancel_text'] = isset($this->field['options']['cancel_text']) ? $this->field['options']['cancel_text'] : 'Cancel';
  65. $this->field['options']['show_buttons'] = isset($this->field['options']['show_buttons']) ? $this->field['options']['show_buttons'] : true;
  66. $this->field['options']['palette'] = isset($this->field['options']['palette']) ? $this->field['options']['palette'] : null;
  67. $this->field['options']['input_text'] = isset($this->field['options']['input_text']) ? $this->field['options']['input_text'] : 'Select Color';
  68. // Convert empty array to null, if there.
  69. $this->field['options']['palette'] = empty($this->field['options']['palette']) ? null : $this->field['options']['palette'];
  70. $this->field['output_transparent'] = isset($this->field['output_transparent']) ? $this->field['output_transparent'] : false;
  71. }
  72. /**
  73. * Field Render Function.
  74. *
  75. * Takes the vars and outputs the HTML for the field in the settings
  76. *
  77. * @since 1.0.0
  78. * @access public
  79. * @return void
  80. */
  81. public function render() {
  82. $field_id = $this->field['id'];
  83. // Color picker container
  84. echo '<div
  85. class="redux-color-rgba-container ' . $this->field['class'] . '"
  86. data-id="' . $field_id . '"
  87. data-show-input="' . $this->field['options']['show_input'] . '"
  88. data-show-initial="' . $this->field['options']['show_initial'] . '"
  89. data-show-alpha="' . $this->field['options']['show_alpha'] . '"
  90. data-show-palette="' . $this->field['options']['show_palette'] . '"
  91. data-show-palette-only="' . $this->field['options']['show_palette_only'] . '"
  92. data-show-selection-palette="' . $this->field['options']['show_selection_palette'] . '"
  93. data-max-palette-size="' . $this->field['options']['max_palette_size'] . '"
  94. data-allow-empty="' . $this->field['options']['allow_empty'] . '"
  95. data-clickout-fires-change="' . $this->field['options']['clickout_fires_change'] . '"
  96. data-choose-text="' . $this->field['options']['choose_text'] . '"
  97. data-cancel-text="' . $this->field['options']['cancel_text'] . '"
  98. data-input-text="' . $this->field['options']['input_text'] . '"
  99. data-show-buttons="' . $this->field['options']['show_buttons'] . '"
  100. data-palette="' . urlencode(json_encode($this->field['options']['palette'])) . '"
  101. >';
  102. // Colour picker layout
  103. $opt_name = $this->parent->args['opt_name'];
  104. if ('' == $this->value['color'] || 'transparent' == $this->value['color']) {
  105. $color = '';
  106. } else {
  107. $color = Redux_Helpers::hex2rgba($this->value['color'], $this->value['alpha']);
  108. }
  109. if ($this->value['rgba'] == ''){
  110. $this->value['rgba'] = Redux_Helpers::hex2rgba($this->value['color'], $this->value['alpha']);
  111. }
  112. echo '<input
  113. name="' . $this->field['name'] . $this->field['name_suffix'] . '[color]"
  114. id="' . $field_id . '-color"
  115. class="redux-color-rgba"
  116. type="text"
  117. value="' . $this->value['color'] . '"
  118. data-color="' . $color . '"
  119. data-id="' . $field_id . '"
  120. data-current-color="' . $this->value['color'] . '"
  121. data-block-id="' . $field_id . '"
  122. data-output-transparent="' . $this->field['output_transparent'] . '"
  123. />';
  124. echo '<input
  125. type="hidden"
  126. class="redux-hidden-color"
  127. data-id="' . $field_id . '-color"
  128. id="' . $field_id . '-color"
  129. value="' . $this->value['color'] . '"
  130. />';
  131. // Hidden input for alpha channel
  132. echo '<input
  133. type="hidden"
  134. class="redux-hidden-alpha"
  135. data-id="' . $field_id . '-alpha"
  136. name="' . $this->field['name'] . $this->field['name_suffix'] . '[alpha]' . '"
  137. id="' . $field_id . '-alpha"
  138. value="' . $this->value['alpha'] . '"
  139. />';
  140. // Hidden input for rgba
  141. echo '<input
  142. type="hidden"
  143. class="redux-hidden-rgba"
  144. data-id="' . $field_id . '-rgba"
  145. name="' . $this->field['name'] . $this->field['name_suffix'] . '[rgba]' . '"
  146. id="' . $field_id . '-rgba"
  147. value="' . $this->value['rgba'] . '"
  148. />';
  149. echo '</div>';
  150. }
  151. /**
  152. * Enqueue Function.
  153. *
  154. * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
  155. *
  156. * @since 1.0.0
  157. * @access public
  158. * @return void
  159. */
  160. public function enqueue() {
  161. // Set up min files for dev_mode = false.
  162. $min = Redux_Functions::isMin();
  163. // Field dependent JS
  164. if (!wp_script_is ( 'redux-field-color-rgba-js' )) {
  165. wp_enqueue_script(
  166. 'redux-field-color-rgba-js',
  167. ReduxFramework::$_url . 'inc/fields/color_rgba/field_color_rgba' . Redux_Functions::isMin() . '.js',
  168. array('jquery', 'redux-spectrum-js'),
  169. time(),
  170. true
  171. );
  172. }
  173. // Spectrum CSS
  174. if (!wp_style_is ( 'redux-spectrum-css' )) {
  175. wp_enqueue_style('redux-spectrum-css');
  176. }
  177. if ($this->parent->args['dev_mode']) {
  178. if (!wp_style_is ( 'redux-field-color-rgba-css' )) {
  179. wp_enqueue_style(
  180. 'redux-field-color-rgba-css',
  181. ReduxFramework::$_url . 'inc/fields/color_rgba/field_color_rgba.css',
  182. array(),
  183. time(),
  184. 'all'
  185. );
  186. }
  187. }
  188. }
  189. /**
  190. * getColorVal. Returns formatted color val in hex or rgba.
  191. *
  192. * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
  193. *
  194. * @since 1.0.0
  195. * @access private
  196. * @return string
  197. */
  198. private function getColorVal(){
  199. // No notices
  200. $color = '';
  201. $alpha = 1;
  202. $rgba = '';
  203. // Must be an array
  204. if (is_array($this->value)) {
  205. // Enum array to parse values
  206. foreach($this->value as $id => $val) {
  207. // Sanitize alpha
  208. if ($id == 'alpha') {
  209. $alpha = !empty($val) ? $val : 1;
  210. } elseif ($id == 'color') {
  211. $color = !empty($val) ? $val : '';
  212. } elseif ($id == 'rgba') {
  213. $rgba = !empty($val) ? $val : '';
  214. $rgba = Redux_Helpers::hex2rgba($color, $alpha);
  215. }
  216. }
  217. // Only build rgba output if alpha ia less than 1
  218. if ( $alpha < 1 && $alpha <> '' ) {
  219. $color = $rgba;
  220. }
  221. }
  222. return $color;
  223. }
  224. /**
  225. * Output Function.
  226. *
  227. * Used to enqueue to the front-end
  228. *
  229. * @since 1.0.0
  230. * @access public
  231. * @return void
  232. */
  233. public function output() {
  234. if (!empty($this->value)) {
  235. $style = '';
  236. $mode = ( isset( $this->field['mode'] ) && ! empty( $this->field['mode'] ) ? $this->field['mode'] : 'color' );
  237. $color_val = $this->getColorVal();
  238. $style .= $mode . ':' . $color_val . ';';
  239. if ( ! empty( $this->field['output'] ) && is_array( $this->field['output'] ) ) {
  240. $css = Redux_Functions::parseCSS( $this->field['output'], $style, $color_val );
  241. $this->parent->outputCSS .= $css;
  242. }
  243. if ( ! empty( $this->field['compiler'] ) && is_array( $this->field['compiler'] ) ) {
  244. $css = Redux_Functions::parseCSS( $this->field['compiler'], $style, $color_val );
  245. $this->parent->compilerCSS .= $css ;
  246. }
  247. }
  248. }
  249. }
  250. }