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