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