PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / fields / class-acf-field-color_picker.php
secure-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 1 year ago class-acf-field-button-group.php 1 year ago class-acf-field-checkbox.php 1 year ago class-acf-field-color_picker.php 1 year ago class-acf-field-date_picker.php 1 year ago class-acf-field-date_time_picker.php 1 year ago class-acf-field-email.php 1 year ago class-acf-field-file.php 1 year ago class-acf-field-google-map.php 1 year ago class-acf-field-group.php 1 year ago class-acf-field-icon_picker.php 1 year ago class-acf-field-image.php 1 year ago class-acf-field-link.php 1 year ago class-acf-field-message.php 1 year ago class-acf-field-number.php 1 year ago class-acf-field-oembed.php 1 year ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 1 year ago class-acf-field-password.php 1 year ago class-acf-field-post_object.php 1 year ago class-acf-field-radio.php 1 year ago class-acf-field-range.php 1 year ago class-acf-field-relationship.php 1 year ago class-acf-field-select.php 1 year ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 1 year ago class-acf-field-text.php 1 year ago class-acf-field-textarea.php 1 year ago class-acf-field-time_picker.php 1 year ago class-acf-field-true_false.php 1 year ago class-acf-field-url.php 1 year ago class-acf-field-user.php 1 year ago class-acf-field-wysiwyg.php 1 year ago class-acf-field.php 1 year ago index.php 1 year ago
class-acf-field-color_picker.php
280 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_color_picker' ) ) :
4
5 class acf_field_color_picker extends acf_field {
6
7
8 /**
9 * This function will setup the field type data
10 *
11 * @type function
12 * @date 5/03/2014
13 * @since 5.0.0
14 *
15 * @param n/a
16 * @return n/a
17 */
18 function initialize() {
19
20 // vars
21 $this->name = 'color_picker';
22 $this->label = __( 'Color Picker', 'acf' );
23 $this->category = 'advanced';
24 $this->description = __( 'An interactive UI for selecting a color, or specifying a Hex value.', 'acf' );
25 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-color-picker.png';
26 $this->doc_url = 'https://www.advancedcustomfields.com/resources/color-picker/';
27 $this->defaults = array(
28 'default_value' => '',
29 'enable_opacity' => false,
30 'return_format' => 'string', // 'string'|'array'
31 );
32 }
33
34
35 /**
36 * description
37 *
38 * @type function
39 * @date 16/12/2015
40 * @since 5.3.2
41 *
42 * @param $post_id (int)
43 * @return $post_id (int)
44 */
45 function input_admin_enqueue_scripts() {
46
47 // Register scripts for non-admin.
48 // Applies logic from wp_default_scripts() function defined in "wp-includes/script-loader.php".
49 if ( ! is_admin() ) {
50 $suffix = defined( 'SCF_DEVELOPMENT_MODE' ) && SCF_DEVELOPMENT_MODE ? '' : '.min';
51 $scripts = wp_scripts();
52 $scripts->add( 'iris', '/wp-admin/js/iris.min.js', array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), '1.0.7', 1 );
53 $scripts->add( 'wp-color-picker', "/wp-admin/js/color-picker$suffix.js", array( 'iris' ), false, 1 );
54
55 // Handle localisation across multiple WP versions.
56 // WP 5.0+
57 if ( method_exists( $scripts, 'set_translations' ) ) {
58 $scripts->set_translations( 'wp-color-picker' );
59 // WP 4.9
60 } else {
61 $scripts->localize(
62 'wp-color-picker',
63 'wpColorPickerL10n',
64 array(
65 'clear' => __( 'Clear', 'acf' ),
66 'clearAriaLabel' => __( 'Clear color', 'acf' ),
67 'defaultString' => __( 'Default', 'acf' ),
68 'defaultAriaLabel' => __( 'Select default color', 'acf' ),
69 'pick' => __( 'Select Color', 'acf' ),
70 'defaultLabel' => __( 'Color value', 'acf' ),
71 )
72 );
73 }
74 }
75
76 // Enqueue alpha color picker assets.
77 wp_enqueue_script(
78 'acf-color-picker-alpha',
79 acf_get_url( 'assets/inc/color-picker-alpha/wp-color-picker-alpha.js' ),
80 array( 'jquery', 'wp-color-picker' ),
81 '3.0.0'
82 );
83
84 // Enqueue.
85 wp_enqueue_style( 'wp-color-picker' );
86 wp_enqueue_script( 'wp-color-picker' );
87
88 acf_localize_data(
89 array(
90 'colorPickerL10n' => array(
91 'hex_string' => __( 'Hex String', 'acf' ),
92 'rgba_string' => __( 'RGBA String', 'acf' ),
93 ),
94 )
95 );
96 }
97
98
99 /**
100 * Create the HTML interface for your field
101 *
102 * @param $field - an array holding all the field's data
103 *
104 * @type action
105 * @since 3.6
106 * @date 23/01/13
107 */
108 function render_field( $field ) {
109 $text_input = acf_get_sub_array( $field, array( 'id', 'class', 'name', 'value' ) );
110 $hidden_input = acf_get_sub_array( $field, array( 'name', 'value' ) );
111 $text_input['data-alpha-skip-debounce'] = true;
112
113 // Color picker alpha library requires a specific data attribute to exist.
114 if ( $field['enable_opacity'] ) {
115 $text_input['data-alpha-enabled'] = true;
116 }
117
118 // html
119 ?>
120 <div class="acf-color-picker">
121 <?php acf_hidden_input( $hidden_input ); ?>
122 <?php acf_text_input( $text_input ); ?>
123 </div>
124 <?php
125 }
126
127
128 /**
129 * Create extra options for your field. This is rendered when editing a field.
130 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
131 *
132 * @type action
133 * @since 3.6
134 * @date 23/01/13
135 *
136 * @param $field - an array holding all the field's data
137 */
138 function render_field_settings( $field ) {
139
140 // display_format
141 acf_render_field_setting(
142 $field,
143 array(
144 'label' => __( 'Default Value', 'acf' ),
145 'instructions' => '',
146 'type' => 'text',
147 'name' => 'default_value',
148 'placeholder' => '#FFFFFF',
149 )
150 );
151
152 // Toggle opacity control.
153 acf_render_field_setting(
154 $field,
155 array(
156 'label' => __( 'Enable Transparency', 'acf' ),
157 'instructions' => '',
158 'type' => 'true_false',
159 'name' => 'enable_opacity',
160 'ui' => 1,
161 )
162 );
163
164 // Return format control.
165 acf_render_field_setting(
166 $field,
167 array(
168 'label' => __( 'Return Format', 'acf' ),
169 'instructions' => '',
170 'type' => 'radio',
171 'name' => 'return_format',
172 'layout' => 'horizontal',
173 'choices' => array(
174 'string' => __( 'Hex String', 'acf' ),
175 'array' => __( 'RGBA Array', 'acf' ),
176 ),
177 )
178 );
179 }
180
181 /**
182 * Format the value for use in templates. At this stage, the value has been loaded from the
183 * database and is being returned by an API function such as get_field(), the_field(), etc.
184 *
185 * @since 5.10
186 *
187 * @param mixed $value The field value
188 * @param integer $post_id The post ID
189 * @param array $field The field array
190 * @return string|array
191 */
192 public function format_value( $value, $post_id, $field ) {
193 if ( isset( $field['return_format'] ) && $field['return_format'] === 'array' ) {
194 $value = $this->string_to_array( $value );
195 }
196
197 return $value;
198 }
199
200 /**
201 * Convert either a Hexadecimal or RGBA string to an RGBA array.
202 *
203 * @since 5.10
204 * @date 15/12/20
205 *
206 * @param string $value
207 * @return array
208 */
209 private function string_to_array( $value ) {
210 $value = is_string( $value ) ? trim( $value ) : '';
211
212 // Match and collect r,g,b values from 6 digit hex code. If there are 4
213 // match-results, we have the values we need to build an r,g,b,a array.
214 preg_match( '/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i', $value, $matches );
215 if ( count( $matches ) === 4 ) {
216 return array(
217 'red' => hexdec( $matches[1] ),
218 'green' => hexdec( $matches[2] ),
219 'blue' => hexdec( $matches[3] ),
220 'alpha' => (float) 1,
221 );
222 }
223
224 // Match and collect r,g,b values from 3 digit hex code. If there are 4
225 // match-results, we have the values we need to build an r,g,b,a array.
226 // We have to duplicate the matched hex digit for 3 digit hex codes.
227 preg_match( '/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i', $value, $matches );
228 if ( count( $matches ) === 4 ) {
229 return array(
230 'red' => hexdec( $matches[1] . $matches[1] ),
231 'green' => hexdec( $matches[2] . $matches[2] ),
232 'blue' => hexdec( $matches[3] . $matches[3] ),
233 'alpha' => (float) 1,
234 );
235 }
236
237 // Attempt to match an rgba(…) or rgb(…) string (case-insensitive), capturing the decimals
238 // as a string. If there are two match results, we have the RGBA decimal values as a
239 // comma-separated string. Break it apart and, depending on the number of values, return
240 // our formatted r,g,b,a array.
241 preg_match( '/^rgba?\(([0-9,.]+)\)/i', $value, $matches );
242 if ( count( $matches ) === 2 ) {
243 $decimals = explode( ',', $matches[1] );
244
245 // Handle rgba() format.
246 if ( count( $decimals ) === 4 ) {
247 return array(
248 'red' => (int) $decimals[0],
249 'green' => (int) $decimals[1],
250 'blue' => (int) $decimals[2],
251 'alpha' => (float) $decimals[3],
252 );
253 }
254
255 // Handle rgb() format.
256 if ( count( $decimals ) === 3 ) {
257 return array(
258 'red' => (int) $decimals[0],
259 'green' => (int) $decimals[1],
260 'blue' => (int) $decimals[2],
261 'alpha' => (float) 1,
262 );
263 }
264 }
265
266 return array(
267 'red' => 0,
268 'green' => 0,
269 'blue' => 0,
270 'alpha' => (float) 0,
271 );
272 }
273 }
274
275 // initialize
276 acf_register_field_type( 'acf_field_color_picker' );
277 endif; // class_exists check
278
279 ?>
280