acf_field_select::update_field()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/*
4
*  ACF Select Field Class
5
*
6
*  All the logic for this field type
7
*
8
*  @class 		acf_field_select
9
*  @extends		acf_field
10
*  @package		ACF
11
*  @subpackage	Fields
12
*/
13
14
if( ! class_exists('acf_field_select') ) :
15
16
class acf_field_select extends acf_field {
17
	
18
	
19
	/*
20
	*  __construct
21
	*
22
	*  This function will setup the field type data
23
	*
24
	*  @type	function
25
	*  @date	5/03/2014
26
	*  @since	5.0.0
27
	*
28
	*  @param	n/a
29
	*  @return	n/a
30
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
31
	
32 View Code Duplication
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
		
34
		// vars
35
		$this->name = 'select';
36
		$this->label = __("Select",'acf');
0 ignored issues
show
Bug introduced by
The property label does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
		$this->category = 'choice';
38
		$this->defaults = array(
39
			'multiple' 		=> 0,
40
			'allow_null' 	=> 0,
41
			'choices'		=> array(),
42
			'default_value'	=> '',
43
			'ui'			=> 0,
44
			'ajax'			=> 0,
45
			'placeholder'	=> '',
46
			'disabled'		=> 0,
47
			'readonly'		=> 0,
48
		);
49
		
50
		
51
		// ajax
52
		add_action('wp_ajax_acf/fields/select/query',				array($this, 'ajax_query'));
53
		add_action('wp_ajax_nopriv_acf/fields/select/query',		array($this, 'ajax_query'));
54
		
55
		
56
		// do not delete!
57
    	parent::__construct();
58
    	
59
	}
60
61
	
62
	/*
63
	*  query_posts
64
	*
65
	*  description
66
	*
67
	*  @type	function
68
	*  @date	24/10/13
69
	*  @since	5.0.0
70
	*
71
	*  @param	n/a
72
	*  @return	n/a
73
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
74
	
75
	function ajax_query() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
76
		
77
   		// options
78
   		$options = acf_parse_args( $_POST, array(
79
			'post_id'					=>	0,
80
			's'							=>	'',
81
			'field_key'					=>	'',
82
			'nonce'						=>	'',
83
		));
84
		
85
		
86
		// load field
87
		$field = acf_get_field( $options['field_key'] );
88
		
89
		if( !$field ) {
90
		
91
			die();
92
			
93
		}
94
		
95
		
96
		// vars
97
		$r = array();
98
		$s = false;
99
		
100
		
101
		// search
102
		if( $options['s'] !== '' ) {
103
			
104
			// search may be integer
105
			$s = strval($options['s']);
106
			
107
			
108
			// strip slashes
109
			$s = wp_unslash($s);
110
			
111
		}		
112
		
113
		
114
		// loop through choices
115
		if( !empty($field['choices']) ) {
116
		
117
			foreach( $field['choices'] as $k => $v ) {
118
				
119
				// if searching, but doesn't exist
120
				if( $s !== false && stripos($v, $s) === false ) {
121
				
122
					continue;
123
					
124
				}
125
				
126
				
127
				// append
128
				$r[] = array(
129
					'id'	=> $k,
130
					'text'	=> strval( $v )
131
				);
132
				
133
			}
134
			
135
		}
136
		
137
		
138
		// return JSON
139
		echo json_encode( $r );
140
		die();
141
			
142
	}
143
	
144
	
145
	/*
146
	*  render_field()
147
	*
148
	*  Create the HTML interface for your field
149
	*
150
	*  @param	$field - an array holding all the field's data
151
	*
152
	*  @type	action
153
	*  @since	3.6
154
	*  @date	23/01/13
155
	*/
156
	
157
	function render_field( $field ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
158
	
159
		// convert
160
		$field['value'] = acf_get_array($field['value'], false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
161
		$field['choices'] = acf_get_array($field['choices']);
162
		
163
		
164
		// placeholder
165
		if( empty($field['placeholder']) ) {
166
		
167
			$field['placeholder'] = __("Select",'acf');
168
			
169
		}
170
		
171
		
172
		// add empty value (allows '' to be selected)
173
		if( !count($field['value']) ) {
174
			
175
			$field['value'][''] = '';
176
			
177
		}
178
		
179
		
180
		// null
181
		if( $field['allow_null'] && !$field['multiple'] ) {
182
			
183
			$prepend = array(''	=> '- ' . $field['placeholder'] . ' -');
184
			$field['choices'] = $prepend + $field['choices'];
185
			
186
		}
187
		
188
		
189
		// vars
190
		$atts = array(
191
			'id'				=> $field['id'],
192
			'class'				=> $field['class'],
193
			'name'				=> $field['name'],
194
			'data-ui'			=> $field['ui'],
195
			'data-ajax'			=> $field['ajax'],
196
			'data-multiple'		=> $field['multiple'],
197
			'data-placeholder'	=> $field['placeholder'],
198
			'data-allow_null'	=> $field['allow_null']
199
		);
200
		
201
		
202
		// multiple
203
		if( $field['multiple'] ) {
204
		
205
			$atts['multiple'] = 'multiple';
206
			$atts['size'] = 5;
207
			$atts['name'] .= '[]';
208
			
209
		} 
210
		
211
		
212
		// special atts
213 View Code Duplication
		foreach( array( 'readonly', 'disabled' ) as $k ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
		
215
			if( !empty($field[ $k ]) ) $atts[ $k ] = $k;
216
			
217
		}
218
		
219
		
220
		// hidden input
221
		if( $field['ui'] ) {
222
			
223
			$v = $field['value'];
224
			
225
			if( $field['multiple'] ) {
226
				
227
				$v = implode('||', $v);
228
				
229
			} else {
230
				
231
				$v = acf_maybe_get($v, 0, '');
232
				
233
			}
234
			
235
			acf_hidden_input(array(
236
				'id'	=> $field['id'] . '-input',
237
				'name'	=> $field['name'],
238
				'value'	=> $v
239
			));
240
			
241
		} elseif( $field['multiple'] ) {
242
			
243
			acf_hidden_input(array(
244
				'id'	=> $field['id'] . '-input',
245
				'name'	=> $field['name']
246
			));
247
			
248
		}
249
		
250
		
251
		
252
		// open
253
		echo '<select ' . acf_esc_attr($atts) . '>';	
254
		
255
		
256
		// walk
257
		$this->walk( $field['choices'], $field['value'] );
258
		
259
		
260
		// close
261
		echo '</select>';
262
		
263
	}
264
	
265
	
266
	/*
267
	*  walk
268
	*
269
	*  description
270
	*
271
	*  @type	function
272
	*  @date	22/12/2015
273
	*  @since	5.3.2
274
	*
275
	*  @param	$post_id (int)
276
	*  @return	$post_id (int)
277
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
278
	
279
	function walk( $choices, $values ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
280
		
281
		// bail ealry if no choices
282
		if( empty($choices) ) return;
283
		
284
		
285
		// loop
286
		foreach( $choices as $k => $v ) {
287
			
288
			// optgroup
289
			if( is_array($v) ){
290
				
291
				// optgroup
292
				echo '<optgroup label="' . esc_attr($k) . '">';
293
				
294
				
295
				// walk
296
				$this->walk( $v, $values );
297
				
298
				
299
				// close optgroup
300
				echo '</optgroup>';
301
				
302
				
303
				// break
304
				continue;
305
				
306
			}
307
			
308
			
309
			// vars
310
			$search = html_entity_decode($k);
311
			$pos = array_search($search, $values);
312
			$atts = array( 'value' => $k );
313
			
314
			
315
			// validate selected
316
			if( $pos !== false ) {
317
				
318
				$atts['selected'] = 'selected';
319
				$atts['data-i'] = $pos;
320
				
321
			}
322
			
323
			
324
			// option
325
			echo '<option ' . acf_esc_attr($atts) . '>' . $v . '</option>';
326
			
327
		}
328
		
329
	}
330
	
331
	
332
	/*
333
	*  render_field_settings()
334
	*
335
	*  Create extra options for your field. This is rendered when editing a field.
336
	*  The value of $field['name'] can be used (like bellow) to save extra data to the $field
337
	*
338
	*  @type	action
339
	*  @since	3.6
340
	*  @date	23/01/13
341
	*
342
	*  @param	$field	- an array holding all the field's data
343
	*/
344
	
345
	function render_field_settings( $field ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
346
		
347
		// encode choices (convert from array)
348
		$field['choices'] = acf_encode_choices($field['choices']);
349
		$field['default_value'] = acf_encode_choices($field['default_value']);
350
		
351
		
352
		// choices
353
		acf_render_field_setting( $field, array(
354
			'label'			=> __('Choices','acf'),
355
			'instructions'	=> __('Enter each choice on a new line.','acf') . '<br /><br />' . __('For more control, you may specify both a value and label like this:','acf'). '<br /><br />' . __('red : Red','acf'),
356
			'type'			=> 'textarea',
357
			'name'			=> 'choices',
358
		));	
359
		
360
		
361
		// default_value
362
		acf_render_field_setting( $field, array(
363
			'label'			=> __('Default Value','acf'),
364
			'instructions'	=> __('Enter each default value on a new line','acf'),
365
			'type'			=> 'textarea',
366
			'name'			=> 'default_value',
367
		));
368
		
369
		
370
		// allow_null
371
		acf_render_field_setting( $field, array(
372
			'label'			=> __('Allow Null?','acf'),
373
			'instructions'	=> '',
374
			'type'			=> 'radio',
375
			'name'			=> 'allow_null',
376
			'choices'		=> array(
377
				1				=> __("Yes",'acf'),
378
				0				=> __("No",'acf'),
379
			),
380
			'layout'	=>	'horizontal',
381
		));
382
		
383
		
384
		// multiple
385
		acf_render_field_setting( $field, array(
386
			'label'			=> __('Select multiple values?','acf'),
387
			'instructions'	=> '',
388
			'type'			=> 'radio',
389
			'name'			=> 'multiple',
390
			'choices'		=> array(
391
				1				=> __("Yes",'acf'),
392
				0				=> __("No",'acf'),
393
			),
394
			'layout'	=>	'horizontal',
395
		));
396
		
397
		
398
		// ui
399
		acf_render_field_setting( $field, array(
400
			'label'			=> __('Stylised UI','acf'),
401
			'instructions'	=> '',
402
			'type'			=> 'radio',
403
			'name'			=> 'ui',
404
			'choices'		=> array(
405
				1				=> __("Yes",'acf'),
406
				0				=> __("No",'acf'),
407
			),
408
			'layout'	=>	'horizontal',
409
		));
410
				
411
		
412
		// ajax
413
		acf_render_field_setting( $field, array(
414
			'label'			=> __('Use AJAX to lazy load choices?','acf'),
415
			'instructions'	=> '',
416
			'type'			=> 'radio',
417
			'name'			=> 'ajax',
418
			'choices'		=> array(
419
				1				=> __("Yes",'acf'),
420
				0				=> __("No",'acf'),
421
			),
422
			'layout'	=>	'horizontal',
423
		));
424
			
425
	}
426
	
427
	
428
	/*
429
	*  load_value()
430
	*
431
	*  This filter is applied to the $value after it is loaded from the db
432
	*
433
	*  @type	filter
434
	*  @since	3.6
435
	*  @date	23/01/13
436
	*
437
	*  @param	$value (mixed) the value found in the database
438
	*  @param	$post_id (mixed) the $post_id from which the value was loaded
439
	*  @param	$field (array) the field array holding all the field options
440
	*  @return	$value
441
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $value could not be parsed: Unknown type name "$value" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
442
	
443
	function load_value( $value, $post_id, $field ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
444
		
445
		// ACF4 null
446
		if( $value === 'null' ) return false;
447
		
448
		
449
		// return
450
		return $value;
451
	}
452
	
453
	
454
	/*
455
	*  update_field()
456
	*
457
	*  This filter is appied to the $field before it is saved to the database
458
	*
459
	*  @type	filter
460
	*  @since	3.6
461
	*  @date	23/01/13
462
	*
463
	*  @param	$field - the field array holding all the field options
464
	*  @param	$post_id - the field group ID (post_type = acf)
465
	*
466
	*  @return	$field - the modified field
467
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $field could not be parsed: Unknown type name "$field" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
468
469 View Code Duplication
	function update_field( $field ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
470
		
471
		// decode choices (convert to array)
472
		$field['choices'] = acf_decode_choices($field['choices']);
473
		$field['default_value'] = acf_decode_choices($field['default_value']);
474
		
475
		
476
		// return
477
		return $field;
478
	}
479
	
480
	
481
	/*
482
	*  update_value()
483
	*
484
	*  This filter is appied to the $value before it is updated in the db
485
	*
486
	*  @type	filter
487
	*  @since	3.6
488
	*  @date	23/01/13
489
	*
490
	*  @param	$value - the value which will be saved in the database
491
	*  @param	$post_id - the $post_id of which the value will be saved
492
	*  @param	$field - the field array holding all the field options
493
	*
494
	*  @return	$value - the modified value
495
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $value could not be parsed: Unknown type name "$value" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
496
	
497 View Code Duplication
	function update_value( $value, $post_id, $field ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
498
		
499
		// validate
500
		if( empty($value) ) {
501
		
502
			return $value;
503
			
504
		}
505
		
506
		
507
		// array
508
		if( is_array($value) ) {
509
			
510
			// save value as strings, so we can clearly search for them in SQL LIKE statements
511
			$value = array_map('strval', $value);
512
			
513
		}
514
		
515
		
516
		// return
517
		return $value;
518
	}
519
	
520
	
521
	/*
522
	*  enqueue_assets
523
	*
524
	*  This function will enqueue the Select2 JS library
525
	*  moved to the footer in an attempt to allow 3rd party to register Select2 paths
526
	*
527
	*  @type	function
528
	*  @date	16/12/2015
529
	*  @since	5.3.3
530
	*
531
	*  @param	n/a
532
	*  @return	n/a
533
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
534
	
535
	function input_admin_enqueue_scripts() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
536
		
537
		$this->enqueue_assets();
538
		
539
	}
540
	
541
	
542
	function field_group_admin_enqueue_scripts() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
543
		
544
		$this->enqueue_assets();
545
		
546
	}
547
	
548
	function enqueue_assets() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
549
		
550
		// vars
551
		$version = '3.5.2';
552
		$lang = get_locale();
553
		$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
554
		
555
		
556
		// v4
557
/*
558
		wp_enqueue_script('select2', acf_get_dir("assets/inc/select2/dist/js/select2.full.js"), array('jquery'), '4.0', true );
559
		wp_enqueue_style('select2', acf_get_dir("assets/inc/select2/dist/css/select2{$min}.css"), '', '4.0' );
560
		return;
561
*/
562
		
563
		// scripts
564
		wp_enqueue_script('select2', acf_get_dir("assets/inc/select2/select2{$min}.js"), array('jquery'), $version, true );
565
		
566
		
567
		// styles
568
		wp_enqueue_style('select2', acf_get_dir('assets/inc/select2/select2.css'), '', $version );
569
		
570
		
571
		// bail early if no language
572
		if( !$lang ) return;
573
		
574
		
575
		// vars
576
		$lang = str_replace('_', '-', $lang);
577
		$lang_code = substr($lang, 0, 2);
578
		$src = '';
579
		
580
		
581
		// attempt 1
582
		if( file_exists(acf_get_path("assets/inc/select2/select2_locale_{$lang_code}.js")) ) {
583
			
584
			$src = acf_get_dir("assets/inc/select2/select2_locale_{$lang_code}.js");
585
			
586
		} elseif( file_exists(acf_get_path("assets/inc/select2/select2_locale_{$lang}.js")) ) {
587
			
588
			$src = acf_get_dir("assets/inc/select2/select2_locale_{$lang}.js");
589
			
590
		}
591
		
592
		
593
		// bail early if no language
594
		if( !$src ) return;
595
		
596
		
597
		// scripts
598
		wp_enqueue_script('select2-l10n', $src, '', $version, true );
599
		
600
	}
601
	
602
}
603
604
new acf_field_select();
605
606
endif;
607
608
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
609