api-field.php ➔ acf_render_field_wrap()   F
last analyzed

Complexity

Conditions 19
Paths 5184

Size

Total Lines 147

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 5184
nop 3
dl 0
loc 147
rs 0.2799
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
*  acf_get_field_types
5
*
6
*  This function will return all available field types
7
*
8
*  @type	function
9
*  @date	1/10/13
10
*  @since	5.0.0
11
*
12
*  @param	n/a
13
*  @return	(array)
14
*/
15
16
function acf_get_field_types() {
17
18
	return apply_filters('acf/get_field_types', array());
19
	
20
}
21
22
23
/*
24
*  acf_get_field_type_label
25
*
26
*  This function will return the label of a field type
27
*
28
*  @type	function
29
*  @date	1/10/13
30
*  @since	5.0.0
31
*
32
*  @param	n/a
33
*  @return	(array)
34
*/
35
36
function acf_get_field_type_label( $field_type ) {
37
38
	// vars
39
	$field_types = acf_get_field_types();
40
	
41
	
42
	// loop through categories
43
	foreach( $field_types as $category ) {
44
		
45
		if( isset( $category[ $field_type ] ) ) {
46
		
47
			return $category[ $field_type ];
48
			
49
		}
50
		
51
	}
52
	
53
	
54
	// return
55
	return '';
56
	
57
}
58
59
60
/*
61
*  acf_field_type_exists
62
*
63
*  This function will check if the field_type is available
64
*
65
*  @type	function
66
*  @date	1/10/13
67
*  @since	5.0.0
68
*
69
*  @param	$field_type (string)
70
*  @return	(boolean)
71
*/
72
73
function acf_field_type_exists( $field_type ) {
74
75
	// vars
76
	$label = acf_get_field_type_label( $field_type );
77
	
78
	
79
	// return true if label exists
80
	if( $label !== '' ) return true;
81
		
82
	
83
	// return
84
	return false;
85
	
86
}
87
88
89
/*
90
*  acf_is_field_key
91
*
92
*  This function will return true or false for the given $field_key parameter
93
*
94
*  @type	function
95
*  @date	6/12/2013
96
*  @since	5.0.0
97
*
98
*  @param	$field_key (string)
99
*  @return	(boolean)
100
*/
101
102 View Code Duplication
function acf_is_field_key( $key = '' ) {
0 ignored issues
show
Duplication introduced by
This function 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...
103
	
104
	// bail early if not string
105
	if( !is_string($key) ) return false;
106
	
107
	
108
	// bail early if is numeric (could be numeric string '123')
109
	if( is_numeric($key) ) return false;
110
	
111
	
112
	// default - starts with 'field_'
113
	if( substr($key, 0, 6) === 'field_' ) return true;
114
	
115
	
116
	// special - allow local field key to be any string
117
	if( acf_is_local_field($key) ) return true;
118
	
119
	
120
	// return
121
	return false;
122
	
123
}
124
125
126
/*
127
*  acf_get_valid_field
128
*
129
*  This function will fill in any missing keys to the $field array making it valid
130
*
131
*  @type	function
132
*  @date	28/09/13
133
*  @since	5.0.0
134
*
135
*  @param	$field (array)
136
*  @return	$field (array)
137
*/
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...
138
139
function acf_get_valid_field( $field = false ) {
140
	
141
	// $field must be an array
142
	if( !is_array($field) ) $field = array();
143
	
144
	
145
	// bail ealry if already valid
146
	if( !empty($field['_valid']) ) return $field;
147
	
148
	
149
	// defaults
150
	$field = acf_parse_args($field, array(
151
		'ID'				=> 0,
152
		'key'				=> '',
153
		'label'				=> '',
154
		'name'				=> '',
155
		'prefix'			=> '',
156
		'type'				=> 'text',
157
		'value'				=> null,
158
		'menu_order'		=> 0,
159
		'instructions'		=> '',
160
		'required'			=> 0,
161
		'id'				=> '',
162
		'class'				=> '',
163
		'conditional_logic'	=> 0,
164
		'parent'			=> 0,
165
		'wrapper'			=> array(
166
			'width'				=> '',
167
			'class'				=> '',
168
			'id'				=> ''
169
		),
170
		'_name'				=> '',
171
		'_input'			=> '',
172
		'_valid'			=> 0,
173
	));
174
	
175
	
176
	// _name
177
	$field['_name'] = $field['name'];
178
	
179
	
180
	// translate
181
	acf_translate_keys( $field, acf_get_setting('l10n_field') );
182
	
183
	
184
	// field specific defaults
185
	$field = apply_filters( "acf/get_valid_field", $field );
186
	$field = apply_filters( "acf/get_valid_field/type={$field['type']}", $field );
187
	
188
	
189
	// field is now valid
190
	$field['_valid'] = 1;
191
	
192
	
193
	// return
194
	return $field;
195
}
196
197
198
/*
199
*  acf_prepare_field
200
*
201
*  This function will prepare the field for input
202
*
203
*  @type	function
204
*  @date	12/02/2014
205
*  @since	5.0.0
206
*
207
*  @param	$field (array)
208
*  @return	$field (array)
209
*/
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...
210
211
function acf_prepare_field( $field ) {
212
	
213
	// bail early if already prepared
214
	if( $field['_input'] ) return $field;
215
	
216
	
217
	// _input
218
	$field['_input'] = $field['name'];
219
	
220
221
	// _input: key overrides name
222
	if( $field['key'] ) {
223
		
224
		$field['_input'] = $field['key'];
225
		
226
	}
227
228
	
229
	// _input: prefix prepends name
230
	if( $field['prefix'] ) {
231
		
232
		$field['_input'] = "{$field['prefix']}[{$field['_input']}]";
233
		
234
	}
235
	
236
	
237
	// add id (may be custom set)
238
	if( !$field['id'] ) {
239
		
240
		$field['id'] = str_replace(array('][', '[', ']'), array('-', '-', ''), $field['_input']);
241
		
242
	}
243
	
244
	
245
	// filter to 3rd party customization
246
	$field = apply_filters( "acf/prepare_field", $field );
247
	$field = apply_filters( "acf/prepare_field/type={$field['type']}", $field );
248
	$field = apply_filters( "acf/prepare_field/name={$field['name']}", $field );
249
	$field = apply_filters( "acf/prepare_field/key={$field['key']}", $field );
250
	
251
	
252
	// return
253
	return $field;
254
	
255
}
256
257
258
/*
259
*  acf_is_sub_field
260
*
261
*  This function will return true if the field is a sub field
262
*
263
*  @type	function
264
*  @date	17/05/2014
265
*  @since	5.0.0
266
*
267
*  @param	$field (array)
268
*  @return	(boolean)
269
*/
270
271
function acf_is_sub_field( $field ) {
272
	
273
	// local field uses a field instead of ID
274
	if( acf_is_field_key($field['parent']) ) return true;
275
	
276
	
277
	// attempt to load parent field
278
	if( acf_get_field($field['parent']) ) return true;
279
	
280
	
281
	// return
282
	return false;
283
	
284
}
285
286
287
/*
288
*  acf_get_field_label
289
*
290
*  This function will return the field label with appropriate required label
291
*
292
*  @type	function
293
*  @date	4/11/2013
294
*  @since	5.0.0
295
*
296
*  @param	$field (array)
297
*  @return	$label (string)
298
*/
0 ignored issues
show
Documentation introduced by
The doc-type $label could not be parsed: Unknown type name "$label" 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...
299
300
function acf_get_field_label( $field ) {
301
	
302
	// vars
303
	$label = $field['label'];
304
	
305
	
306
	if( $field['required'] ) {
307
		
308
		$label .= ' <span class="acf-required">*</span>';
309
		
310
	}
311
	
312
	
313
	// return
314
	return $label;
315
316
}
317
318
function acf_the_field_label( $field ) {
319
	
320
	echo acf_get_field_label( $field );
321
	
322
}
323
324
325
/*
326
*  acf_render_fields
327
*
328
*  This function will render an array of fields for a given form.
329
*  Becasue the $field's values have not been loaded yet, this function will also load values
330
*
331
*  @type	function
332
*  @date	8/10/13
333
*  @since	5.0.0
334
*
335
*  @param	$post_id (int) the post to load values from
336
*  @param	$fields (array) the fields to render
337
*  @param	$el (string) the wrapping element type
338
*  @param	$instruction (int) the instructions position
339
*  @return	n/a
340
*/
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...
341
342
function acf_render_fields( $post_id = 0, $fields, $el = 'div', $instruction = 'label' ) {
343
	
344
	// bail early if no fields
345
	if( empty($fields) ) return false;
346
	
347
		
348
	// remove corrupt fields
349
	$fields = array_filter($fields);
350
	
351
	
352
	// loop through fields
353
	foreach( $fields as $field ) {
354
		
355
		// load value
356
		if( $field['value'] === null ) {
357
			
358
			$field['value'] = acf_get_value( $post_id, $field );
359
			
360
		} 
361
		
362
		
363
		// set prefix for correct post name (prefix + key)
364
		$field['prefix'] = 'acf';
365
		
366
		
367
		// render
368
		acf_render_field_wrap( $field, $el, $instruction );
369
		
370
	}
371
	
372
}
373
374
375
/*
376
*  acf_render_field
377
*
378
*  This function will render a field input
379
*
380
*  @type	function
381
*  @date	28/09/13
382
*  @since	5.0.0
383
*
384
*  @param	$field (array)
385
*  @return	n/a
386
*/
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...
387
388
function acf_render_field( $field = false ) {
389
	
390
	// get valid field
391
	$field = acf_get_valid_field( $field );
392
	
393
	
394
	// prepare field for input
395
	$field = acf_prepare_field( $field );
396
	
397
	
398
	// update $field['name']
399
	$field['name'] = $field['_input'];
400
		
401
	
402
	// create field specific html
403
	do_action( "acf/render_field", $field );
404
	do_action( "acf/render_field/type={$field['type']}", $field );
405
	
406
}
407
408
409
/*
410
*  acf_render_field_wrap
411
*
412
*  This function will render the complete HTML wrap with label & field
413
*
414
*  @type	function
415
*  @date	28/09/13
416
*  @since	5.0.0
417
*
418
*  @param	$field (array) must be a valid ACF field array
419
*  @param	$el (string) modifys the rendered wrapping elements. Default to 'div', but can be 'tr', 'ul', 'ol', 'dt' or custom
420
*  @param	$instruction (string) specifys the placement of the instructions. Default to 'label', but can be 'field'
421
*  @param	$atts (array) an array of custom attributes to render on the $el
422
*  @return	N/A
423
*/
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...
424
425
function acf_render_field_wrap( $field, $el = 'div', $instruction = 'label' ) {
426
	
427
	// get valid field
428
	$field = acf_get_valid_field( $field );
429
	
430
	
431
	// prepare field for input
432
	$field = acf_prepare_field( $field );
433
	
434
	
435
	// el
436
	$elements = apply_filters('acf/render_field_wrap/elements', array(
437
		'div'	=> 'div',
438
		'tr'	=> 'td',
439
		'ul'	=> 'li',
440
		'ol'	=> 'li',
441
		'dl'	=> 'dt',
442
		'td'	=> 'div' // special case for sub field!
443
	));
444
	
445
	
446
	// validate $el
447
	if( !array_key_exists($el, $elements) ) {
448
		
449
		$el = 'div';
450
	
451
	}
452
	
453
	
454
	// wrapper
455
	$wrapper = array(
456
		'id'		=> '',
457
		'class'		=> 'acf-field',
458
		'width'		=> '',
459
		'style'		=> '',
460
		'data-name'	=> $field['name'],
461
		'data-type'	=> $field['type'],
462
		'data-key'	=> '',
463
	);
464
	
465
	
466
	// add required
467
	if( $field['required'] ) {
468
		
469
		$wrapper['data-required'] = 1;
470
		
471
	}
472
	
473
	
474
	// add type
475
	$wrapper['class'] .= " acf-field-{$field['type']}";
476
	
477
	
478
	// add key
479
	if( $field['key'] ) {
480
		
481
		$wrapper['class'] .= " acf-field-{$field['key']}";
482
		$wrapper['data-key'] = $field['key'];
483
		
484
	}
485
	
486
	
487
	// replace
488
	$wrapper['class'] = str_replace('_', '-', $wrapper['class']);
489
	$wrapper['class'] = str_replace('field-field-', 'field-', $wrapper['class']);
490
	
491
	
492
	// wrap classes have changed (5.2.7)
493
	if( acf_get_compatibility('field_wrapper_class') ) {
494
		
495
		$wrapper['class'] .= " field_type-{$field['type']}";
496
		
497
		if( $field['key'] ) {
498
			
499
			$wrapper['class'] .= " field_key-{$field['key']}";
500
			
501
		}
502
		
503
	}
504
	
505
	
506
	// merge in atts
507
	$wrapper = acf_merge_atts( $wrapper, $field['wrapper'] );
508
	
509
	
510
	// add width
511
	$width = (int) acf_extract_var( $wrapper, 'width' );
512
	
513
	if( $el == 'tr' || $el == 'td' ) {
514
		
515
		$width = 0;
0 ignored issues
show
Unused Code introduced by
$width is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
516
		
517
	} elseif( $width > 0 && $width < 100 ) {
518
		
519
		$wrapper['data-width'] = $width;
520
		$wrapper['style'] .= " width:{$width}%;";
521
		
522
	}
523
	
524
	
525
	// remove empty attributes
526
	foreach( $wrapper as $k => $v ) {
527
		
528
		if( $v == '' ) {
529
			
530
			unset($wrapper[$k]);
531
			
532
		}
533
		
534
	}
535
	
536
	
537
	// vars
538
	$show_label = true;
539
	
540
	if( $el == 'td' ) {
541
		
542
		$show_label = false;
543
		
544
	}
545
	
546
	
547
?><<?php echo $el; ?> <?php echo acf_esc_attr($wrapper); ?>>
548
<?php if( $show_label ): ?>
549
	<<?php echo $elements[ $el ]; ?> class="acf-label">
550
		<label for="<?php echo $field['id']; ?>"><?php echo acf_get_field_label($field); ?></label>
551
<?php if( $instruction == 'label' && $field['instructions'] ): ?>
552
		<p class="description"><?php echo $field['instructions']; ?></p>
553
<?php endif; ?>
554
	</<?php echo $elements[ $el ]; ?>>
555
<?php endif; ?>
556
	<<?php echo $elements[ $el ]; ?> class="acf-input">
557
		<?php acf_render_field( $field ); ?>
558
		
559
<?php if( $instruction == 'field' && $field['instructions'] ): ?>
560
		<p class="description"><?php echo $field['instructions']; ?></p>
561
<?php endif; ?>
562
	</<?php echo $elements[ $el ]; ?>>
563
<?php if( !empty($field['conditional_logic'])): ?>
564
	<script type="text/javascript">
565
		if(typeof acf !== 'undefined'){ acf.conditional_logic.add( '<?php echo $field['key']; ?>', <?php echo json_encode($field['conditional_logic']); ?>); }
566
	</script>
567
<?php endif; ?>
568
</<?php echo $el; ?>>
569
<?php
570
	
571
}
572
573
574
/*
575
*  acf_render_field_setting
576
*
577
*  This function will render a tr element containing a label and field cell, but also setting the tr data attribute for AJAX 
578
*
579
*  @type	function
580
*  @date	28/09/13
581
*  @since	5.0.0
582
*
583
*  @param	$field (array) the origional field being edited
584
*  @param	$setting (array) the settings field to create
585
*  @return	n/a
586
*/
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...
587
588
function acf_render_field_setting( $field, $setting, $global = false ) {
589
	
590
	// validate
591
	$setting = acf_get_valid_field( $setting );
592
	
593
	
594
	// if this setting is not global, add a data attribute
595
	if( !$global ) {
596
		
597
		$setting['wrapper']['data-setting'] = $field['type'];
598
		
599
	}
600
	
601
	
602
	// copy across prefix
603
	$setting['prefix'] = $field['prefix'];
604
		
605
		
606
	// copy across the $setting value
607 View Code Duplication
	if( isset($field[ $setting['name'] ]) ) {
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...
608
		
609
		$setting['value'] = $field[ $setting['name'] ];
610
		
611
	}
612
	
613
	
614
	// render
615
	acf_render_field_wrap( $setting, 'tr', 'label' );
616
	
617
}
618
619
620
/*
621
*  acf_get_fields
622
*
623
*  This function will return an array of fields for the given $parent
624
*
625
*  @type	function
626
*  @date	30/09/13
627
*  @since	5.0.0
628
*
629
*  @param	$parent (array) a field or field group
630
*  @return	(array)
631
*/
632
633
function acf_get_fields( $parent = false ) {
634
	
635
	// allow $parent to be a field group ID
636
	if( !is_array($parent) ) {
637
		
638
		$parent = acf_get_field_group( $parent );
639
	
640
	}
641
	
642
	
643
	// bail early if no parent
644
	if( !$parent ) return false;
645
	
646
	
647
	// vars
648
	$fields = array();
0 ignored issues
show
Unused Code introduced by
$fields is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
649
	
650
	
651
	// try JSON before DB to save query time
652
	if( acf_have_local_fields( $parent['key'] ) ) {
653
		
654
		$fields = acf_get_local_fields( $parent['key'] );
655
		
656
	} else {
657
		
658
		$fields = acf_get_fields_by_id( $parent['ID'] );
659
	
660
	}
661
	
662
	
663
	// return
664
	return apply_filters('acf/get_fields', $fields, $parent);
665
	
666
}
667
668
669
/*
670
*  acf_get_fields_by_id
671
*
672
*  This function will get all fields for the given parent
673
*
674
*  @type	function
675
*  @date	27/02/2014
676
*  @since	5.0.0
677
*
678
*  @param	$post_id (int)
679
*  @return	$fields (array)
680
*/
0 ignored issues
show
Documentation introduced by
The doc-type $fields could not be parsed: Unknown type name "$fields" 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...
681
682
function acf_get_fields_by_id( $id = 0 ) {
683
	
684
	// vars
685
	$fields = array();
686
	
687
	
688
	// bail early if no ID
689
	if( empty($id) ) return false;
690
	
691
	
692
	// cache
693
	$found = false;
694
	$cache = wp_cache_get( 'get_fields/parent=' . $id, 'acf', false, $found );
695
	
696
	if( $found ) return $cache;
697
	
698
	
699
	// args
700
	$args = array(
701
		'posts_per_page'			=> -1,
702
		'post_type'					=> 'acf-field',
703
		'orderby'					=> 'menu_order',
704
		'order'						=> 'ASC',
705
		'suppress_filters'			=> true, // DO NOT allow WPML to modify the query
706
		'post_parent'				=> $id,
707
		'post_status'				=> 'publish, trash', // 'any' won't get trashed fields
708
		'update_post_meta_cache'	=> false
709
	);
710
		
711
	
712
	// load fields
713
	$posts = get_posts( $args );
714
	
715
	if( $posts ) {
716
		
717
		foreach( $posts as $post ) {
718
			
719
			$fields[] = acf_get_field( $post->ID );
720
			
721
		}
722
			
723
	}
724
	
725
	
726
	// set cache
727
	wp_cache_set( 'get_fields/parent=' . $id, $fields, 'acf' );
728
		
729
	
730
	// return
731
	return $fields;
732
	
733
}
734
735
736
/*
737
*  acf_get_field
738
*
739
*  This function will return a field for the given selector. 
740
*
741
*  @type	function
742
*  @date	30/09/13
743
*  @since	5.0.0
744
*
745
*  @param	$selector (mixed) identifyer of field. Can be an ID, key, name or post object
746
*  @param	$db_only (boolean) return $field in it's raw form without filters or cache
747
*  @return	$field (array)
748
*/
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...
749
750
function acf_get_field( $selector = null, $db_only = false ) {
751
	
752
	// vars
753
	$field = false;
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
754
	$type = 'ID';
755
	
756
	
757
	// is $selector an ID
758
	if( is_numeric($selector) ) {
759
		
760
		// do nothing
761
	
762
	// is $selector a string (name|key)	
763
	} elseif( is_string($selector) ) {
764
		
765
		$type = acf_is_field_key($selector) ? 'key' : 'name';
766
	
767
	// is $selector an object
768
	} elseif( is_object($selector) ) {
769
		
770
		$selector = $selector->ID;
771
	
772
	// selector not valid
773
	} else {
774
		
775
		return false;
776
		
777
	}
778
	
779
	
780
	// get cache key
781
	$cache_key = "get_field/{$type}={$selector}";
782
	
783
	
784
	// get cache
785
	if( !$db_only ) {
786
		
787
		$found = false;
788
		$cache = wp_cache_get( $cache_key, 'acf', false, $found );
789
		
790
		if( $found ) return $cache;
791
		
792
	}
793
	
794
	
795
	// get field group from ID or key
796
	if( $type == 'ID' ) {
797
		
798
		$field = _acf_get_field_by_id( $selector, $db_only );
799
		
800
	} elseif( $type == 'name' ) {
801
		
802
		$field = _acf_get_field_by_name( $selector, $db_only );
803
		
804
	} else {
805
		
806
		$field = _acf_get_field_by_key( $selector, $db_only );
807
		
808
	}
809
	
810
	
811
	// bail early if db only value (no need to update cache)
812
	if( $db_only ) {
813
		
814
		return $field;
815
		
816
	}
817
	
818
	
819
	// filter for 3rd party customization
820
	if( $field ) {
821
		
822
		$field = apply_filters( "acf/load_field", $field);
823
		$field = apply_filters( "acf/load_field/type={$field['type']}", $field );
824
		$field = apply_filters( "acf/load_field/name={$field['name']}", $field );
825
		$field = apply_filters( "acf/load_field/key={$field['key']}", $field );
826
		
827
	}
828
	
829
	
830
	// set cache
831
	wp_cache_set( $cache_key, $field, 'acf' );
832
833
	
834
	// return
835
	return $field;
836
	
837
}
838
839
840
/*
841
*  _acf_get_field_by_id
842
*
843
*  This function will get a field via its ID
844
*
845
*  @type	function
846
*  @date	27/02/2014
847
*  @since	5.0.0
848
*
849
*  @param	$post_id (int)
850
*  @return	$field (array)
851
*/
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...
852
853
function _acf_get_field_by_id( $post_id = 0, $db_only = false ) {
854
	
855
	// get post
856
	$post = get_post( $post_id );
857
	
858
	
859
	// bail early if no post, or is not a field
860
	if( empty($post) || $post->post_type != 'acf-field' ) {
861
	
862
		return false;
863
		
864
	}
865
	
866
	
867
	// unserialize
868
	$field = maybe_unserialize( $post->post_content );
869
	
870
	
871
	// update attributes
872
	$field['ID'] = $post->ID;
873
	$field['key'] = $post->post_name;
874
	$field['label'] = $post->post_title;
875
	$field['name'] = $post->post_excerpt;
876
	$field['menu_order'] = $post->menu_order;
877
	$field['parent'] = $post->post_parent;
878
879
880
	// override with JSON
881
	if( !$db_only && acf_is_local_field($field['key']) ) {
882
		
883
		// extract some args
884
		$backup = acf_extract_vars($field, array(
885
			'ID',
886
			'parent'
887
		));
888
		
889
890
		// load JSON field
891
		$field = acf_get_local_field( $field['key'] );
892
		
893
		
894
		// merge in backup
895
		$field = array_merge($field, $backup);
896
		
897
	}
898
	
899
	
900
	// validate
901
	$field = acf_get_valid_field( $field );
0 ignored issues
show
Documentation introduced by
$field is of type array, but the function expects a boolean.

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...
902
	
903
	
904
	// return
905
	return $field;
906
	
907
}
908
909
910
/*
911
*  _acf_get_field_by_key
912
*
913
*  This function will get a field via its key
914
*
915
*  @type	function
916
*  @date	27/02/2014
917
*  @since	5.0.0
918
*
919
*  @param	$key (string)
920
*  @return	$field (array)
921
*/
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...
922
923
function _acf_get_field_by_key( $key = '', $db_only = false ) {
924
	
925
	// try JSON before DB to save query time
926
	if( !$db_only && acf_is_local_field( $key ) ) {
927
		
928
		$field = acf_get_local_field( $key );
929
		
930
		// validate
931
		$field = acf_get_valid_field( $field );
932
	
933
		// return
934
		return $field;
935
		
936
	}
937
	
938
	
939
	// vars
940
	$post_id = acf_get_field_id( $key );
941
	
942
	
943
	// bail early if no post_id
944
	if( !$post_id ) return false;
945
		
946
	
947
	// return
948
	return _acf_get_field_by_id( $post_id, $db_only );
949
	
950
}
951
952
953
/*
954
*  _acf_get_field_by_name
955
*
956
*  This function will get a field via its name
957
*
958
*  @type	function
959
*  @date	27/02/2014
960
*  @since	5.0.0
961
*
962
*  @param	$key (string)
963
*  @return	$field (array)
964
*/
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...
965
966
function _acf_get_field_by_name( $name = '', $db_only = false ) {
967
	
968
	// vars
969
	$args = array(
970
		'posts_per_page'	=> 1,
971
		'post_type'			=> 'acf-field',
972
		'orderby' 			=> 'menu_order title',
973
		'order'				=> 'ASC',
974
		'suppress_filters'	=> false,
975
		'acf_field_name'	=> $name
976
	);
977
	
978
	
979
	// load posts
980
	$posts = get_posts( $args );
981
	
982
	
983
	// bail early if no posts
984
	if( empty($posts) ) return false;
985
	
986
	
987
	// return
988
	return _acf_get_field_by_id( $posts[0]->ID, $db_only );
989
	
990
}
991
992
993
/*
994
*  acf_maybe_get_field
995
*
996
*  This function will return a field for the given selector.
997
*  It will also review the field_reference to ensure the correct field is returned which makes it useful for the template API
998
*
999
*  @type	function
1000
*  @date	4/08/2015
1001
*  @since	5.2.3
1002
*
1003
*  @param	$selector (mixed) identifyer of field. Can be an ID, key, name or post object
1004
*  @param	$post_id (mixed) the post_id of which the value is saved against
1005
*  @param	$strict (boolean) if true, return a field only when a field key is found.
1006
*  @return	$field (array)
1007
*/
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...
1008
1009
function acf_maybe_get_field( $selector, $post_id = false, $strict = true ) {
1010
	
1011
	// complete init
1012
	// this function may be used in a theme file before the init action has been run
1013
	acf()->init();
1014
	
1015
	
1016
	// vars
1017
	$field_name = false;
1018
	
1019
	
1020
	// get valid post_id
1021
	$post_id = acf_get_valid_post_id( $post_id );
0 ignored issues
show
Documentation introduced by
$post_id is of type boolean, but the function expects a integer.

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...
1022
	
1023
	
1024
	// load field reference if not a field_key
1025
	if( !acf_is_field_key($selector) ) {
1026
		
1027
		// save selector as field_name (could be sub field name)
1028
		$field_name = $selector;
1029
			
1030
			
1031
		// get reference
1032
		$field_key = acf_get_field_reference( $selector, $post_id );
1033
		
1034
		
1035
		if( $field_key ) {
1036
			
1037
			$selector = $field_key;
1038
			
1039
		} elseif( $strict ) {
1040
			
1041
			return false;
1042
			
1043
		}
1044
		
1045
	}
1046
	
1047
	
1048
	// get field key
1049
	$field = acf_get_field( $selector );
1050
	
1051
	
1052
	// bail early if no field
1053
	if( !$field ) return false;
1054
	
1055
	
1056
	// Override name - allows the $selector to be a sub field (images_0_image)
1057
	if( $field_name ) {
1058
	
1059
		$field['name'] = $field_name;	
1060
		
1061
	}
1062
	
1063
	
1064
	// return
1065
	return $field;
1066
	
1067
}
1068
1069
1070
/*
1071
*  acf_get_field_id
1072
*
1073
*  This function will lookup a field's ID from the DB
1074
*  Useful for local fields to find DB sibling
1075
*
1076
*  @type	function
1077
*  @date	25/06/2015
1078
*  @since	5.2.3
1079
*
1080
*  @param	$key (string)
1081
*  @return	$post_id (int)
1082
*/
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...
1083
1084
function acf_get_field_id( $key = '' ) {
1085
	
1086
	// vars
1087
	$args = array(
1088
		'posts_per_page'	=> 1,
1089
		'post_type'			=> 'acf-field',
1090
		'orderby' 			=> 'menu_order title',
1091
		'order'				=> 'ASC',
1092
		'suppress_filters'	=> false,
1093
		'acf_field_key'		=> $key
1094
	);
1095
	
1096
	
1097
	// load posts
1098
	$posts = get_posts( $args );
1099
	
1100
	
1101
	// validate
1102
	if( empty($posts) ) return 0;
1103
	
1104
	
1105
	// return
1106
	return $posts[0]->ID;
1107
	
1108
}
1109
1110
1111
/*
1112
*  acf_update_field
1113
*
1114
*  This function will update a field into the DB.
1115
*  The returned field will always contain an ID
1116
*
1117
*  @type	function
1118
*  @date	1/10/13
1119
*  @since	5.0.0
1120
*
1121
*  @param	$field (array)
1122
*  @return	$field (array)
1123
*/
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...
1124
1125
function acf_update_field( $field = false, $specific = false ) {
1126
	
1127
	// $field must be an array
1128
	if( !is_array($field) ) return false;
1129
	
1130
	
1131
	// validate
1132
	$field = acf_get_valid_field( $field );
0 ignored issues
show
Documentation introduced by
$field is of type array, but the function expects a boolean.

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...
1133
	
1134
	
1135
	// may have been posted. Remove slashes
1136
	$field = wp_unslash( $field );
1137
	
1138
	
1139
	// clean up conditional logic keys
1140
	if( !empty($field['conditional_logic']) ) {
1141
		
1142
		// extract groups
1143
		$groups = acf_extract_var( $field, 'conditional_logic' );
1144
		
1145
		
1146
		// clean array
1147
		$groups = array_filter($groups);
1148
		$groups = array_values($groups);
1149
		
1150
		
1151
		// clean rules
1152
		foreach( array_keys($groups) as $i ) {
1153
			
1154
			$groups[ $i ] = array_filter($groups[ $i ]);
1155
			$groups[ $i ] = array_values($groups[ $i ]);
1156
			
1157
		}
1158
		
1159
		
1160
		// reset conditional logic
1161
		$field['conditional_logic'] = $groups;
1162
		
1163
	}
1164
	
1165
	
1166
	// find correct parent
1167
	if( acf_is_field_key($field['parent']) ) {
1168
		
1169
		// get parent
1170
		$parent = acf_get_field( $field['parent'] );
1171
		
1172
1173
		// update to ID
1174
		$field['parent'] = acf_maybe_get( $parent, 'ID', 0 );
1175
		
1176
	}
1177
	
1178
	
1179
	// filter for 3rd party customization
1180
	$field = apply_filters( "acf/update_field", $field);
1181
	$field = apply_filters( "acf/update_field/type={$field['type']}", $field );
1182
	$field = apply_filters( "acf/update_field/name={$field['name']}", $field );
1183
	$field = apply_filters( "acf/update_field/key={$field['key']}", $field );
1184
	
1185
	
1186
	// store origional field for return
1187
	$data = $field;
1188
	
1189
	
1190
	// extract some args
1191
	$extract = acf_extract_vars($data, array(
1192
		'ID',
1193
		'key',
1194
		'label',
1195
		'name',
1196
		'prefix',
1197
		'value',
1198
		'menu_order',
1199
		'id',
1200
		'class',
1201
		'parent',
1202
		'_name',
1203
		'_input',
1204
		'_valid',
1205
	));
1206
	
1207
	
1208
	// serialize for DB
1209
	$data = maybe_serialize( $data );
1210
    
1211
    
1212
    // save
1213
    $save = array(
1214
    	'ID'			=> $extract['ID'],
1215
    	'post_status'	=> 'publish',
1216
    	'post_type'		=> 'acf-field',
1217
    	'post_title'	=> $extract['label'],
1218
    	'post_name'		=> $extract['key'],
1219
    	'post_excerpt'	=> $extract['name'],
1220
    	'post_content'	=> $data,
1221
    	'post_parent'	=> $extract['parent'],
1222
    	'menu_order'	=> $extract['menu_order'],
1223
    );
1224
    
1225
    
1226
    // $specific
1227
    if( !empty($specific) ) {
1228
	    
1229
	    // prepend ID
1230
    	array_unshift( $specific, 'ID' );
1231
    	
1232
    	
1233
	    // vars
1234
	    $_save = $save;
1235
	    
1236
	    
1237
	    // reset
1238
	    $save = array();
1239
	    
1240
    	
1241
    	// appen data
1242
    	foreach( $specific as $key ) {
1243
	    	
1244
	    	$save[ $key ] = $_save[ $key ];
1245
	    	
1246
    	}
1247
    	
1248
    }
1249
    
1250
    
1251
    // allow fields to contain the same name
1252
	add_filter( 'wp_unique_post_slug', 'acf_update_field_wp_unique_post_slug', 100, 6 ); 
1253
	
1254
	
1255
    // update the field and update the ID
1256
    if( $field['ID'] ) {
1257
	    
1258
	    wp_update_post( $save );
1259
	    
1260
    } else  {
1261
	    
1262
	    $field['ID'] = wp_insert_post( $save );
1263
	    
1264
    }
1265
	
1266
    
1267
    // clear cache
1268
	wp_cache_delete( "get_field/ID={$field['ID']}", 'acf' );
1269
	wp_cache_delete( "get_field/key={$field['key']}", 'acf' );
1270
	wp_cache_delete( "get_fields/parent={$field['parent']}", 'acf' );
1271
	
1272
	
1273
    // return
1274
    return $field;
1275
	
1276
}
1277
1278
function acf_update_field_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
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 $post_status 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 $post_parent 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...
1279
		
1280
	if( $post_type == 'acf-field' ) {
1281
	
1282
		$slug = $original_slug;
1283
	
1284
	}
1285
	
1286
	// return
1287
	return $slug;
1288
	
1289
}
1290
1291
1292
/*
1293
*  acf_duplicate_fields
1294
*
1295
*  This function will duplicate an array of fields and update conditional logic references
1296
*
1297
*  @type	function
1298
*  @date	16/06/2014
1299
*  @since	5.0.0
1300
*
1301
*  @param	$fields (array)
1302
*  @param	$new_parent (int)
1303
*  @return	n/a
1304
*/
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...
1305
1306
function acf_duplicate_fields( $fields, $new_parent = 0 ) {
1307
	
1308
	// bail early if no fields
1309
	if( empty($fields) ) return;
1310
	
1311
	
1312
	// create new field keys (for conditional logic fixes)
1313
	foreach( $fields as $field ) {
1314
		
1315
		// ensure a delay for unique ID
1316
		usleep(1);
1317
		
1318
		acf_update_setting( 'duplicate_key_' . $field['key'] , uniqid('field_') );
1319
		
1320
	}
1321
	
1322
	
1323
	// duplicate fields
1324
	foreach( $fields as $field ) {
1325
	
1326
		// duplicate
1327
		acf_duplicate_field( $field['ID'], $new_parent );
1328
		
1329
	}
1330
	
1331
}
1332
1333
1334
/*
1335
*  acf_duplicate_field
1336
*
1337
*  This function will duplicate a field and attach it to the given field group ID
1338
*
1339
*  @type	function
1340
*  @date	17/10/13
1341
*  @since	5.0.0
1342
*
1343
*  @param	$selector (int)
1344
*  @param	$new_parent (int)
1345
*  @return	$field (array) the new field
1346
*/
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...
1347
1348
function acf_duplicate_field( $selector = 0, $new_parent = 0 ){
1349
	
1350
	// disable JSON to avoid conflicts between DB and JSON
1351
	acf_disable_local();
1352
	
1353
	
1354
	// load the origional field
1355
	$field = acf_get_field( $selector );
1356
	
1357
	
1358
	// bail early if field did not load correctly
1359
	if( empty($field) ) {
1360
	
1361
		return false;
1362
		
1363
	}
1364
	
1365
	
1366
	// update ID
1367
	$field['ID'] = false;
1368
	
1369
	
1370
	// try duplicate keys
1371
	$field['key'] = acf_get_setting( 'duplicate_key_' . $field['key'] );
1372
	
1373
	
1374
	// default key
1375
	if( empty($field['key']) ) {
1376
		
1377
		$field['key'] = uniqid('field_');
1378
			
1379
	}
1380
	
1381
	
1382
	// update parent
1383
	if( $new_parent ) {
1384
	
1385
		$field['parent'] = $new_parent;
1386
		
1387
	}
1388
	
1389
	
1390
	// update conditional logic references (because field keys have changed)
1391
	if( !empty($field['conditional_logic']) ) {
1392
	
1393
		// extract groups
1394
		$groups = acf_extract_var( $field, 'conditional_logic' );
1395
		
1396
		
1397
		// loop over groups
1398
		foreach( array_keys($groups) as $g ) {
1399
			
1400
			// extract group
1401
			$group = acf_extract_var( $groups, $g );
1402
			
1403
			
1404
			// bail early if empty
1405
			if( empty($group) ) {
1406
				
1407
				continue;
1408
				
1409
			}
1410
			
1411
			
1412
			// loop over rules
1413
			foreach( array_keys($group) as $r ) {
1414
				
1415
				// extract rule
1416
				$rule = acf_extract_var( $group, $r );
1417
				
1418
				
1419
				// vars
1420
				$new_key = acf_get_setting( 'duplicate_key_' . $rule['field'] );
1421
				
1422
				
1423
				// update rule with new key
1424
				if( $new_key ) {
1425
					
1426
					$rule['field'] = $new_key;
1427
					
1428
				}
1429
				
1430
				
1431
				// append to group
1432
				$group[ $r ] = $rule;
1433
				
1434
			}
1435
			
1436
			
1437
			// append to groups
1438
			$groups[ $g ] = $group;
1439
			
1440
		}
1441
		
1442
		
1443
		// update conditional logic
1444
		$field['conditional_logic'] = $groups;
1445
		
1446
		
1447
	}
1448
	
1449
	
1450
	// filter for 3rd party customization
1451
	$field = apply_filters( "acf/duplicate_field", $field);
1452
	$field = apply_filters( "acf/duplicate_field/type={$field['type']}", $field );
1453
	
1454
	
1455
	// save
1456
	return acf_update_field( $field );
1457
	
1458
}
1459
1460
1461
/*
1462
*  acf_delete_field
1463
*
1464
*  This function will delete a field from the databse
1465
*
1466
*  @type	function
1467
*  @date	2/10/13
1468
*  @since	5.0.0
1469
*
1470
*  @param	$id (int)
1471
*  @return	(boolean)
1472
*/
1473
1474
function acf_delete_field( $selector = 0 ) {
1475
	
1476
	// disable JSON to avoid conflicts between DB and JSON
1477
	acf_disable_local();
1478
	
1479
	
1480
	// load the origional field gorup
1481
	$field = acf_get_field( $selector );
1482
	
1483
	
1484
	// bail early if field did not load correctly
1485
	if( empty($field) ) return false;
1486
	
1487
	
1488
	// delete field
1489
	wp_delete_post( $field['ID'], true );
1490
	
1491
	
1492
	// action for 3rd party customisation
1493
	do_action( "acf/delete_field", $field);
1494
	do_action( "acf/delete_field/type={$field['type']}", $field );
1495
	
1496
	
1497
	// clear cache
1498
	wp_cache_delete( "get_field/ID={$field['ID']}", 'acf' );
1499
	wp_cache_delete( "get_field/key={$field['key']}", 'acf' );
1500
	wp_cache_delete( "get_fields/parent={$field['parent']}", 'acf' );
1501
	
1502
	
1503
	// return
1504
	return true;
1505
}
1506
1507
1508
/*
1509
*  acf_trash_field
1510
*
1511
*  This function will trash a field from the databse
1512
*
1513
*  @type	function
1514
*  @date	2/10/13
1515
*  @since	5.0.0
1516
*
1517
*  @param	$id (int)
1518
*  @return	(boolean)
1519
*/
1520
1521 View Code Duplication
function acf_trash_field( $selector = 0 ) {
0 ignored issues
show
Duplication introduced by
This function 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...
1522
	
1523
	// disable JSON to avoid conflicts between DB and JSON
1524
	acf_disable_local();
1525
	
1526
	
1527
	// load the origional field gorup
1528
	$field = acf_get_field( $selector );
1529
	
1530
	
1531
	// bail early if field did not load correctly
1532
	if( empty($field) ) return false;
1533
	
1534
	
1535
	// delete field
1536
	wp_trash_post( $field['ID'] );
1537
	
1538
	
1539
	// action for 3rd party customisation
1540
	do_action( 'acf/trash_field', $field );
1541
	
1542
	
1543
	// return
1544
	return true;
1545
}
1546
1547
1548
/*
1549
*  acf_untrash_field
1550
*
1551
*  This function will restore a field from the trash
1552
*
1553
*  @type	function
1554
*  @date	2/10/13
1555
*  @since	5.0.0
1556
*
1557
*  @param	$id (int)
1558
*  @return	(boolean)
1559
*/
1560
1561 View Code Duplication
function acf_untrash_field( $selector = 0 ) {
0 ignored issues
show
Duplication introduced by
This function 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...
1562
	
1563
	// disable JSON to avoid conflicts between DB and JSON
1564
	acf_disable_local();
1565
	
1566
	
1567
	// load the origional field gorup
1568
	$field = acf_get_field( $selector );
1569
	
1570
	
1571
	// bail early if field did not load correctly
1572
	if( empty($field) ) return false;
1573
	
1574
	
1575
	// delete field
1576
	wp_untrash_post( $field['ID'] );
1577
	
1578
	
1579
	// action for 3rd party customisation
1580
	do_action( 'acf/untrash_field', $field );
1581
	
1582
	
1583
	// return
1584
	return true;
1585
}
1586
1587
1588
/*
1589
*  acf_prepare_fields_for_export
1590
*
1591
*  description
1592
*
1593
*  @type	function
1594
*  @date	11/03/2014
1595
*  @since	5.0.0
1596
*
1597
*  @param	$post_id (int)
1598
*  @return	$post_id (int)
1599
*/
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...
1600
1601
function acf_prepare_fields_for_export( $fields = false ) {
1602
	
1603
	// validate
1604
	if( empty($fields) ) return $fields;
1605
	
1606
	
1607
	// format
1608
	foreach( array_keys($fields) as $i ) {
1609
		
1610
		// prepare
1611
		$fields[ $i ] = acf_prepare_field_for_export( $fields[ $i ] );
1612
				
1613
	}
1614
	
1615
	
1616
	// return
1617
	return $fields;
1618
	
1619
}
1620
1621
1622
/*
1623
*  acf_prepare_field_for_export
1624
*
1625
*  description
1626
*
1627
*  @type	function
1628
*  @date	11/03/2014
1629
*  @since	5.0.0
1630
*
1631
*  @param	$post_id (int)
1632
*  @return	$post_id (int)
1633
*/
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...
1634
1635
function acf_prepare_field_for_export( $field ) {
1636
	
1637
	// extract some args
1638
	$extract = acf_extract_vars($field, array(
0 ignored issues
show
Unused Code introduced by
$extract is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1639
		'ID',
1640
		'prefix',
1641
		'value',
1642
		'menu_order',
1643
		'id',
1644
		'class',
1645
		'parent',
1646
		'_name',
1647
		'_input',
1648
		'_valid',
1649
	));
1650
	
1651
	
1652
	// filter for 3rd party customization
1653
	$field = apply_filters( "acf/prepare_field_for_export", $field );
1654
	
1655
	
1656
	// return
1657
	return $field;
1658
}
1659
1660
1661
/*
1662
*  acf_prepare_fields_for_import
1663
*
1664
*  description
1665
*
1666
*  @type	function
1667
*  @date	11/03/2014
1668
*  @since	5.0.0
1669
*
1670
*  @param	$post_id (int)
1671
*  @return	$post_id (int)
1672
*/
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...
1673
1674
function acf_prepare_fields_for_import( $fields = false ) {
1675
	
1676
	// validate
1677
	if( empty($fields) ) return $fields;
1678
	
1679
	
1680
	// re-index array
1681
	$fields = array_values($fields);
1682
	
1683
	
1684
	// vars
1685
	$i = 0;
1686
	
1687
	
1688
	// format
1689
	while( $i < count($fields) ) {
1690
		
1691
		// prepare field
1692
		$field = acf_prepare_field_for_import( $fields[ $i ] );
1693
		
1694
		
1695
		// $field may be an array of multiple fields (including sub fields)
1696
		if( !isset($field['key']) ) {
1697
			
1698
			$extra = $field;
1699
			
1700
			$field = array_shift($extra);
1701
			$fields = array_merge($fields, $extra);
1702
			
1703
		}
1704
		
1705
		// prepare
1706
		$fields[ $i ] = $field;
1707
		
1708
		
1709
		// $i
1710
		$i++;	
1711
	}
1712
	
1713
	
1714
	// filter for 3rd party customization
1715
	$fields = apply_filters('acf/prepare_fields_for_import', $fields);
1716
	
1717
	
1718
	// return
1719
	return $fields;
1720
	
1721
}
1722
1723
1724
/*
1725
*  acf_prepare_field_for_import
1726
*
1727
*  description
1728
*
1729
*  @type	function
1730
*  @date	11/03/2014
1731
*  @since	5.0.0
1732
*
1733
*  @param	$post_id (int)
1734
*  @return	$post_id (int)
1735
*/
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...
1736
1737
function acf_prepare_field_for_import( $field ) {
1738
	
1739
	// extract some args
1740
	$extract = acf_extract_vars($field, array(
0 ignored issues
show
Unused Code introduced by
$extract is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1741
		'value',
1742
		'id',
1743
		'class',
1744
		'_name',
1745
		'_input',
1746
		'_valid',
1747
	));
1748
	
1749
	
1750
	// filter for 3rd party customization
1751
	$field = apply_filters( "acf/prepare_field_for_import", $field );
1752
	
1753
	
1754
	// return
1755
	return $field;
1756
}
1757
1758
1759
/*
1760
*  acf_get_sub_field
1761
*
1762
*  This function will return a field for the given selector, and $field (parent). 
1763
*
1764
*  @type	function
1765
*  @date	30/09/13
1766
*  @since	5.0.0
1767
*
1768
*  @param	$selector (string)
1769
*  @param	$field (mixed)
1770
*  @return	$field (array)
1771
*/
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...
1772
1773
function acf_get_sub_field( $selector, $field ) {
1774
	
1775
	// sub fields
1776
	if( $field['type'] == 'repeater' ) {
1777
		
1778
		// extract sub fields
1779
		$sub_fields = acf_extract_var( $field, 'sub_fields');
1780
		
1781 View Code Duplication
		if( !empty($sub_fields) ) {
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...
1782
		
1783
			foreach( $sub_fields as $sub_field ) {
1784
				
1785
				if( $sub_field['name'] == $selector || $sub_field['key'] == $selector ) {
1786
					
1787
					// return
1788
					return $sub_field;
1789
					
1790
				}
1791
				// if
1792
				
1793
			}
1794
			// foreach
1795
			
1796
		}
1797
		// if
1798
		
1799
	} elseif( $field['type'] == 'flexible_content' ) {
1800
		
1801
		// vars
1802
		$layouts = acf_extract_var( $field, 'layouts');
1803
		$current = get_row_layout();
1804
		
1805
		
1806
		if( !empty($layouts) ) {
1807
			
1808
			foreach( $layouts as $layout ) {
1809
				
1810
				// skip layout if the current layout key does not match
1811
				if( $current && $current !== $layout['name'] ) {
1812
					
1813
					continue;
1814
					
1815
				} 
1816
				
1817
				
1818
				// extract sub fields
1819
				$sub_fields = acf_extract_var( $layout, 'sub_fields');
1820
				
1821 View Code Duplication
				if( !empty($sub_fields) ) {
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...
1822
					
1823
					foreach( $sub_fields as $sub_field ) {
1824
						
1825
						if( $sub_field['name'] == $selector || $sub_field['key'] == $selector ) {
1826
							
1827
							// return
1828
							return $sub_field;
1829
							
1830
						}
1831
						// if
1832
						
1833
					}
1834
					// foreach
1835
					
1836
				}
1837
				// if
1838
				
1839
			}
1840
			// foreach
1841
			
1842
		}
1843
		// if
1844
1845
	}
1846
	// if
1847
	
1848
	
1849
	// return
1850
	return false;
1851
	
1852
}
1853
1854
?>
1855