ReduxFramework_color_rgba

Main ReduxFramework_color_rgba class.

Defined (1)

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

/inc/ReduxFramework/ReduxCore/inc/fields/color_rgba/field_color_rgba.php  
  1. class ReduxFramework_color_rgba { 
  2.  
  3. /** 
  4. * Class Constructor. Defines the args for the extions class 
  5. * @since 1.0.0 
  6. * @access public 
  7. * @param array $field Field sections. 
  8. * @param array $value Values. 
  9. * @param array $parent Parent object. 
  10. * @return void 
  11. */ 
  12. public function __construct( $field = array(), $value ='', $parent ) { 
  13.  
  14. // Set required variables 
  15. $this->parent = $parent; 
  16. $this->field = $field; 
  17. $this->value = $value; 
  18.  
  19. $defaults = array( 
  20. 'color' => '',  
  21. 'alpha' => 1,  
  22. 'rgba' => '' 
  23. ); 
  24.  
  25. $this->value = wp_parse_args( $this->value, $defaults ); 
  26.  
  27. $this->field['options']['show_input'] = isset($this->field['options']['show_input']) ? $this->field['options']['show_input'] : true; 
  28. $this->field['options']['show_initial'] = isset($this->field['options']['show_initial']) ? $this->field['options']['show_initial'] : false; 
  29. $this->field['options']['show_alpha'] = isset($this->field['options']['show_alpha']) ? $this->field['options']['show_alpha'] : true; 
  30. $this->field['options']['show_palette'] = isset($this->field['options']['show_palette']) ? $this->field['options']['show_palette'] : false; 
  31. $this->field['options']['show_palette_only'] = isset($this->field['options']['show_palette_only']) ? $this->field['options']['show_palette_only'] : false; 
  32. $this->field['options']['max_palette_size'] = isset($this->field['options']['max_palette_size']) ? $this->field['options']['max_palette_size'] : 10; 
  33. $this->field['options']['show_selection_palette'] = isset($this->field['options']['show_selection_palette']) ? $this->field['options']['show_selection_palette'] : false; 
  34. $this->field['options']['allow_empty'] = isset($this->field['options']['allow_empty']) ? $this->field['options']['allow_empty'] : true; 
  35. $this->field['options']['clickout_fires_change'] = isset($this->field['options']['clickout_fires_change']) ? $this->field['options']['clickout_fires_change'] : false; 
  36. $this->field['options']['choose_text'] = isset($this->field['options']['choose_text']) ? $this->field['options']['choose_text'] : 'Choose'; 
  37. $this->field['options']['cancel_text'] = isset($this->field['options']['cancel_text']) ? $this->field['options']['cancel_text'] : 'Cancel'; 
  38. $this->field['options']['show_buttons'] = isset($this->field['options']['show_buttons']) ? $this->field['options']['show_buttons'] : true; 
  39. $this->field['options']['palette'] = isset($this->field['options']['palette']) ? $this->field['options']['palette'] : null; 
  40. $this->field['options']['input_text'] = isset($this->field['options']['input_text']) ? $this->field['options']['input_text'] : 'Select Color'; 
  41.  
  42. // Convert empty array to null, if there. 
  43. $this->field['options']['palette'] = empty($this->field['options']['palette']) ? null : $this->field['options']['palette']; 
  44.  
  45. $this->field['output_transparent'] = isset($this->field['output_transparent']) ? $this->field['output_transparent'] : false; 
  46.  
  47.  
  48. /** 
  49. * Field Render Function. 
  50. * Takes the vars and outputs the HTML for the field in the settings 
  51. * @since 1.0.0 
  52. * @access public 
  53. * @return void 
  54. */ 
  55. public function render() { 
  56.  
  57. $field_id = $this->field['id']; 
  58.  
  59. // Color picker container 
  60. echo '<div  
  61. class="redux-color-rgba-container ' . $this->field['class'] . '"  
  62. data-id="' . $field_id . '" 
  63. data-show-input="' . $this->field['options']['show_input'] . '" 
  64. data-show-initial="' . $this->field['options']['show_initial'] . '" 
  65. data-show-alpha="' . $this->field['options']['show_alpha'] . '" 
  66. data-show-palette="' . $this->field['options']['show_palette'] . '" 
  67. data-show-palette-only="' . $this->field['options']['show_palette_only'] . '" 
  68. data-show-selection-palette="' . $this->field['options']['show_selection_palette'] . '" 
  69. data-max-palette-size="' . $this->field['options']['max_palette_size'] . '" 
  70. data-allow-empty="' . $this->field['options']['allow_empty'] . '" 
  71. data-clickout-fires-change="' . $this->field['options']['clickout_fires_change'] . '" 
  72. data-choose-text="' . $this->field['options']['choose_text'] . '" 
  73. data-cancel-text="' . $this->field['options']['cancel_text'] . '" 
  74. data-input-text="' . $this->field['options']['input_text'] . '" 
  75. data-show-buttons="' . $this->field['options']['show_buttons'] . '" 
  76. data-palette="' . urlencode(json_encode($this->field['options']['palette'])) . '" 
  77. >'; 
  78.  
  79. // Colour picker layout 
  80. $opt_name = $this->parent->args['opt_name']; 
  81.  
  82. if ('' == $this->value['color'] || 'transparent' == $this->value['color']) { 
  83. $color = ''; 
  84. } else { 
  85. $color = Redux_Helpers::hex2rgba($this->value['color'], $this->value['alpha']); 
  86. }  
  87.  
  88. if ($this->value['rgba'] == '') { 
  89. $this->value['rgba'] = Redux_Helpers::hex2rgba($this->value['color'], $this->value['alpha']); 
  90.  
  91. echo '<input 
  92. name="' . $this->field['name'] . $this->field['name_suffix'] . '[color]" 
  93. id="' . $field_id . '-color" 
  94. class="redux-color-rgba" 
  95. type="text" 
  96. value="' . $this->value['color'] . '" 
  97. data-color="' . $color . '" 
  98. data-id="' . $field_id . '" 
  99. data-current-color="' . $this->value['color'] . '" 
  100. data-block-id="' . $field_id . '" 
  101. data-output-transparent="' . $this->field['output_transparent'] . '" 
  102. />';  
  103.  
  104. echo '<input 
  105. type="hidden" 
  106. class="redux-hidden-color" 
  107. data-id="' . $field_id . '-color" 
  108. id="' . $field_id . '-color" 
  109. value="' . $this->value['color'] . '" 
  110. />';  
  111.  
  112. // Hidden input for alpha channel 
  113. echo '<input 
  114. type="hidden" 
  115. class="redux-hidden-alpha" 
  116. data-id="' . $field_id . '-alpha" 
  117. name="' . $this->field['name'] . $this->field['name_suffix'] . '[alpha]' . '" 
  118. id="' . $field_id . '-alpha" 
  119. value="' . $this->value['alpha'] . '" 
  120. />'; 
  121.  
  122. // Hidden input for rgba 
  123. echo '<input 
  124. type="hidden" 
  125. class="redux-hidden-rgba" 
  126. data-id="' . $field_id . '-rgba" 
  127. name="' . $this->field['name'] . $this->field['name_suffix'] . '[rgba]' . '" 
  128. id="' . $field_id . '-rgba" 
  129. value="' . $this->value['rgba'] . '" 
  130. />';  
  131.  
  132. echo '</div>'; 
  133.  
  134. /** 
  135. * Enqueue Function. 
  136. * If this field requires any scripts, or css define this function and register/enqueue the scripts/css 
  137. * @since 1.0.0 
  138. * @access public 
  139. * @return void 
  140. */ 
  141. public function enqueue() { 
  142.  
  143. // Set up min files for dev_mode = false. 
  144. $min = Redux_Functions::isMin(); 
  145.  
  146. // Field dependent JS 
  147. if (!wp_script_is ( 'redux-field-color-rgba-js' )) { 
  148. wp_enqueue_script( 
  149. 'redux-field-color-rgba-js',  
  150. ReduxFramework::$_url . 'inc/fields/color_rgba/field_color_rgba' . Redux_Functions::isMin() . '.js',  
  151. array('jquery', 'redux-spectrum-js'),  
  152. time(),  
  153. true 
  154. ); 
  155.  
  156. // Spectrum CSS 
  157. if (!wp_style_is ( 'redux-spectrum-css' )) { 
  158. wp_enqueue_style('redux-spectrum-css'); 
  159.  
  160. if ($this->parent->args['dev_mode']) { 
  161. if (!wp_style_is ( 'redux-field-color-rgba-css' )) { 
  162. wp_enqueue_style( 
  163. 'redux-field-color-rgba-css',  
  164. ReduxFramework::$_url . 'inc/fields/color_rgba/field_color_rgba.css',  
  165. array(),  
  166. time(),  
  167. 'all' 
  168. ); 
  169.  
  170. /** 
  171. * getColorVal. Returns formatted color val in hex or rgba. 
  172. * If this field requires any scripts, or css define this function and register/enqueue the scripts/css 
  173. * @since 1.0.0 
  174. * @access private 
  175. * @return string 
  176. */  
  177. private function getColorVal() { 
  178.  
  179. // No notices 
  180. $color = ''; 
  181. $alpha = 1; 
  182. $rgba = ''; 
  183.  
  184. // Must be an array 
  185. if (is_array($this->value)) { 
  186.  
  187. // Enum array to parse values 
  188. foreach($this->value as $id => $val) { 
  189.  
  190. // Sanitize alpha 
  191. if ($id == 'alpha') { 
  192. $alpha = !empty($val) ? $val : 1; 
  193. } elseif ($id == 'color') { 
  194. $color = !empty($val) ? $val : ''; 
  195. } elseif ($id == 'rgba') { 
  196. $rgba = !empty($val) ? $val : ''; 
  197. $rgba = Redux_Helpers::hex2rgba($color, $alpha); 
  198.  
  199. // Only build rgba output if alpha ia less than 1 
  200. if ( $alpha < 1 && $alpha <> '' ) { 
  201. $color = $rgba; 
  202.  
  203. return $color; 
  204.  
  205. /** 
  206. * Output Function. 
  207. * Used to enqueue to the front-end 
  208. * @since 1.0.0 
  209. * @access public 
  210. * @return void 
  211. */  
  212. public function output() { 
  213. if (!empty($this->value)) { 
  214. $style = ''; 
  215.  
  216. $mode = ( isset( $this->field['mode'] ) && ! empty( $this->field['mode'] ) ? $this->field['mode'] : 'color' ); 
  217.  
  218. $color_val = $this->getColorVal(); 
  219.  
  220. $style .= $mode . ':' . $color_val . ';'; 
  221.  
  222. if ( ! empty( $this->field['output'] ) && is_array( $this->field['output'] ) ) { 
  223. $css = Redux_Functions::parseCSS( $this->field['output'], $style, $color_val ); 
  224. $this->parent->outputCSS .= $css; 
  225.  
  226. if ( ! empty( $this->field['compiler'] ) && is_array( $this->field['compiler'] ) ) { 
  227. $css = Redux_Functions::parseCSS( $this->field['compiler'], $style, $color_val ); 
  228. $this->parent->compilerCSS .= $css ;