acf_compatibility::get_valid_field_group()   F
last analyzed

Complexity

Conditions 19
Paths 216

Size

Total Lines 180

Duplication

Lines 40
Ratio 22.22 %

Importance

Changes 0
Metric Value
cc 19
nc 216
nop 1
dl 40
loc 180
rs 2.7866
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
class acf_compatibility {
4
	
5
	/*
6
	*  __construct
7
	*
8
	*  description
9
	*
10
	*  @type	function
11
	*  @date	30/04/2014
12
	*  @since	5.0.0
13
	*
14
	*  @param	$post_id (int)
15
	*  @return	$post_id (int)
16
	*/
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...
17
	
18
	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...
19
		
20
		// fields
21
		add_filter('acf/get_valid_field',					array($this, 'get_valid_field'), 20, 1);
22
		add_filter('acf/get_valid_field/type=textarea',		array($this, 'get_valid_textarea_field'), 20, 1);
23
		add_filter('acf/get_valid_field/type=relationship',	array($this, 'get_valid_relationship_field'), 20, 1);
24
		add_filter('acf/get_valid_field/type=post_object',	array($this, 'get_valid_relationship_field'), 20, 1);
25
		add_filter('acf/get_valid_field/type=page_link',	array($this, 'get_valid_relationship_field'), 20, 1);
26
		add_filter('acf/get_valid_field/type=image',		array($this, 'get_valid_image_field'), 20, 1);
27
		add_filter('acf/get_valid_field/type=file',			array($this, 'get_valid_image_field'), 20, 1);
28
		add_filter('acf/get_valid_field/type=wysiwyg',		array($this, 'get_valid_wysiwyg_field'), 20, 1);
29
		add_filter('acf/get_valid_field/type=date_picker',	array($this, 'get_valid_date_picker_field'), 20, 1);
30
		add_filter('acf/get_valid_field/type=taxonomy',		array($this, 'get_valid_taxonomy_field'), 20, 1);
31
		
32
		
33
		// field groups
34
		add_filter('acf/get_valid_field_group',				array($this, 'get_valid_field_group'), 20, 1);
35
		
36
		
37
		// settings
38
		add_filter('acf/settings/show_admin',				array($this, 'settings_acf_lite'), 5, 1);
39
		add_filter('acf/settings/l10n_textdomain',			array($this, 'settings_export_textdomain'), 5, 1);
40
		add_filter('acf/settings/l10n_field',				array($this, 'settings_export_translate'), 5, 1);
41
		add_filter('acf/settings/l10n_field_group',			array($this, 'settings_export_translate'), 5, 1);
42
		
43
	}
44
	
45
	
46
	/*
47
	*  settings
48
	*
49
	*  description
50
	*
51
	*  @type	function
52
	*  @date	19/05/2014
53
	*  @since	5.0.0
54
	*
55
	*  @param	$post_id (int)
56
	*  @return	$post_id (int)
57
	*/
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...
58
	
59
	function settings_acf_lite( $setting ) {
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...
60
		
61
		// 5.0.0 - removed ACF_LITE
62
		if( defined('ACF_LITE') && ACF_LITE ) {
63
			
64
			$setting = false;
65
			
66
		}
67
		
68
		
69
		// return
70
		return $setting;
71
		
72
	}
73
	
74
	function settings_export_textdomain( $setting ) {
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...
75
		
76
		// 5.3.3 - changed filter name
77
		return acf_get_setting( 'export_textdomain', $setting );
78
		
79
	}
80
	
81
	function settings_export_translate( $setting ) {
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...
82
		
83
		// 5.3.3 - changed filter name
84
		return acf_get_setting( 'export_translate', $setting );
85
		
86
	}
87
	
88
	
89
	/*
90
	*  get_valid_field
91
	*
92
	*  This function will provide compatibility with ACF4 fields
93
	*
94
	*  @type	function
95
	*  @date	23/04/2014
96
	*  @since	5.0.0
97
	*
98
	*  @param	$field (array)
99
	*  @return	$field
100
	*/
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...
101
	
102
	function get_valid_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...
103
		
104
		// conditional logic has changed
105
		if( isset($field['conditional_logic']['status']) ) {
106
			
107
			// extract logic
108
			$logic = acf_extract_var( $field, 'conditional_logic' );
109
			
110
			
111
			// disabled
112 View Code Duplication
			if( !empty($logic['status']) ) {
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...
113
				
114
				// reset
115
				$field['conditional_logic'] = array();
116
				
117
				
118
				// vars
119
				$group = 0;
120
		 		$all_or_any = $logic['allorany'];
121
		 		
122
		 		
123
		 		// loop over rules
124
		 		if( !empty($logic['rules']) ) {
125
			 		
126
			 		foreach( $logic['rules'] as $rule ) {
127
				 		
128
					 	// sperate groups?
129
					 	if( $all_or_any == 'any' ) {
130
					 	
131
						 	$group++;
132
						 	
133
					 	}
134
					 	
135
					 	
136
					 	// add to group
137
					 	$field['conditional_logic'][ $group ][] = $rule;
138
			 	
139
				 	}
140
				 	
141
		 		}
142
			 	
143
			 	
144
			 	// reset keys
145
				$field['conditional_logic'] = array_values($field['conditional_logic']);
146
				
147
				
148
			} else {
149
				
150
				$field['conditional_logic'] = 0;
151
				
152
			}
153
		 	
154
		}
155
		
156
		
157
		// return
158
		return $field;
159
		
160
	}
161
	
162
	
163
	/*
164
	*  get_valid_relationship_field
165
	*
166
	*  This function will provide compatibility with ACF4 fields
167
	*
168
	*  @type	function
169
	*  @date	23/04/2014
170
	*  @since	5.0.0
171
	*
172
	*  @param	$field (array)
173
	*  @return	$field
174
	*/
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...
175
	
176
	function get_valid_relationship_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...
177
		
178
		// force array
179
		$field['post_type'] = acf_get_array($field['post_type']);
180
		$field['taxonomy'] = acf_get_array($field['taxonomy']);
181
		
182
		
183
		// remove 'all' from post_type
184
		if( acf_in_array('all', $field['post_type']) ) {
185
			
186
			$field['post_type'] = array();
187
			
188
		}
189
		
190
		
191
		// remove 'all' from taxonomy
192
		if( acf_in_array('all', $field['taxonomy']) ) {
193
			
194
			$field['taxonomy'] = array();
195
			
196
		}
197
		
198
		
199
		// save_format is now return_format
200
		if( !empty($field['result_elements']) ) {
201
			
202
			$field['elements'] = acf_extract_var( $field, 'result_elements' );
203
			
204
		}
205
		
206
		
207
		
208
		// return
209
		return $field;
210
	}
211
	
212
	
213
	/*
214
	*  get_valid_textarea_field
215
	*
216
	*  This function will provide compatibility with ACF4 fields
217
	*
218
	*  @type	function
219
	*  @date	23/04/2014
220
	*  @since	5.0.0
221
	*
222
	*  @param	$field (array)
223
	*  @return	$field
224
	*/
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...
225
	
226
	function get_valid_textarea_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...
227
		
228
		// formatting has been removed
229
		$formatting = acf_extract_var( $field, 'formatting' );
230
		
231
		if( $formatting === 'br' ) {
232
			
233
			$field['new_lines'] = 'br';
234
			
235
		}
236
		
237
		
238
		// return
239
		return $field;
240
	}
241
	
242
	
243
	/*
244
	*  get_valid_image_field
245
	*
246
	*  This function will provide compatibility with ACF4 fields
247
	*
248
	*  @type	function
249
	*  @date	23/04/2014
250
	*  @since	5.0.0
251
	*
252
	*  @param	$field (array)
253
	*  @return	$field
254
	*/
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...
255
	
256
	function get_valid_image_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...
257
		
258
		// save_format is now return_format
259
		if( !empty($field['save_format']) ) {
260
			
261
			$field['return_format'] = acf_extract_var( $field, 'save_format' );
262
			
263
		}
264
		
265
		
266
		// object is now array
267
		if( $field['return_format'] == 'object' ) {
268
			
269
			$field['return_format'] = 'array';
270
			
271
		}
272
		
273
		
274
		// return
275
		return $field;
276
	}
277
	
278
	
279
	/*
280
	*  get_valid_wysiwyg_field
281
	*
282
	*  This function will provide compatibility with ACF4 fields
283
	*
284
	*  @type	function
285
	*  @date	23/04/2014
286
	*  @since	5.0.0
287
	*
288
	*  @param	$field (array)
289
	*  @return	$field
290
	*/
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...
291
	
292
	function get_valid_wysiwyg_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...
293
		
294
		// media_upload is now numeric
295
		if( $field['media_upload'] === 'yes' ) {
296
			
297
			$field['media_upload'] = 1;
298
			
299
		} elseif( $field['media_upload'] === 'no' ) {
300
			
301
			$field['media_upload'] = 0;
302
			
303
		}
304
		
305
		
306
		// return
307
		return $field;
308
	}
309
	
310
	
311
	/*
312
	*  get_valid_date_picker_field
313
	*
314
	*  This function will provide compatibility with ACF4 fields
315
	*
316
	*  @type	function
317
	*  @date	23/04/2014
318
	*  @since	5.0.0
319
	*
320
	*  @param	$field (array)
321
	*  @return	$field
322
	*/
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...
323
	
324
	function get_valid_date_picker_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...
325
		
326
		// v4 used date_format
327
		if( !empty($field['date_format']) ) {
328
			
329
			// extract vars
330
			$date_format = acf_extract_var( $field, 'date_format' );
331
			$display_format = acf_extract_var( $field, 'display_format' );
332
			
333
			
334
			// convert from js to php
335
			$date_format = acf_convert_date_to_php( $date_format );
336
			$display_format = acf_convert_date_to_php( $display_format );
337
			
338
			
339
			// append settings
340
			$field['return_format'] = $date_format;
341
			$field['display_format'] = $display_format;
342
			
343
		}
344
		
345
		
346
		// return
347
		return $field;
348
		
349
	}
350
	
351
	
352
	/*
353
	*  get_valid_taxonomy_field
354
	*
355
	*  This function will provide compatibility with ACF4 fields
356
	*
357
	*  @type	function
358
	*  @date	23/04/2014
359
	*  @since	5.0.0
360
	*
361
	*  @param	$field (array)
362
	*  @return	$field
363
	*/
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...
364
	
365
	function get_valid_taxonomy_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...
366
		
367
		// 5.2.7
368
		if( isset($field['load_save_terms']) ) {
369
			
370
			$field['save_terms'] = $field['load_save_terms'];
371
			
372
		}
373
		
374
		
375
		// return
376
		return $field;
377
		
378
	}
379
	
380
	
381
	/*
382
	*  get_valid_field_group
383
	*
384
	*  This function will provide compatibility with ACF4 field groups
385
	*
386
	*  @type	function
387
	*  @date	23/04/2014
388
	*  @since	5.0.0
389
	*
390
	*  @param	$field_group (array)
391
	*  @return	$field_group
392
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $field_group could not be parsed: Unknown type name "$field_group" 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...
393
	
394
	function get_valid_field_group( $field_group ) {
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...
395
		
396
		// global
397
		global $wpdb;
398
		
399
		
400
		// vars
401
		$v = 5;
402
		
403
		
404
		// add missing 'key' (v5.0.0)
405
		if( empty($field_group['key']) ) {
406
			
407
			// update version
408
			$v = 4;
409
			
410
			
411
			// add missing key
412
			$field_group['key'] = empty($field_group['id']) ? uniqid('group_') : 'group_' . $field_group['id'];
413
			
414
		}
415
		
416
		
417
		// extract options (v5.0.0)
418
		if( !empty($field_group['options']) ) {
419
			
420
			$options = acf_extract_var($field_group, 'options');
421
			$field_group = array_merge($field_group, $options);
422
			
423
		}
424
		
425
		
426
		// location rules changed to groups (v5.0.0)
427 View Code Duplication
		if( !empty($field_group['location']['rules']) ) {
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...
428
			
429
			// extract location
430
			$location = acf_extract_var( $field_group, 'location' );
431
			
432
			
433
			// reset location
434
			$field_group['location'] = array();
435
			
436
			
437
			// vars
438
			$group = 0;
439
	 		$all_or_any = $location['allorany'];
440
	 		
441
	 		
442
	 		// loop over rules
443
	 		if( !empty($location['rules']) ) {
444
		 		
445
		 		foreach( $location['rules'] as $rule ) {
446
			 		
447
				 	// sperate groups?
448
				 	if( $all_or_any == 'any' ) {
449
				 	
450
					 	$group++;
451
					 	
452
				 	}
453
				 	
454
				 	
455
				 	// add to group
456
				 	$field_group['location'][ $group ][] = $rule;
457
		 	
458
			 	}
459
			 	
460
	 		}
461
		 	
462
		 	
463
		 	// reset keys
464
			$field_group['location'] = array_values($field_group['location']);
465
		 	
466
		}
467
		
468
		
469
		// some location rules have changed (v5.0.0)
470
		if( !empty($field_group['location']) ) {
471
			
472
			// param changes
473
		 	$param_replace = array(
474
		 		'taxonomy'		=> 'post_taxonomy',
475
		 		'ef_media'		=> 'attachment',
476
		 		'ef_taxonomy'	=> 'taxonomy',
477
		 		'ef_user'		=> 'user_role',
478
		 		'user_type'		=> 'current_user_role' // 5.2.0
479
		 	);
480
		 	
481
		 	
482
		 	// remove conflicting param
483
		 	if( $v == 5 ) {
484
			 	
485
			 	unset($param_replace['taxonomy']);
486
			 	
487
		 	}
488
		 	
489
		 	
490
			// loop over location groups
491
			foreach( array_keys($field_group['location']) as $i ) {
492
				
493
				// extract group
494
				$group = acf_extract_var( $field_group['location'], $i );
495
				
496
				
497
				// bail early if group is empty
498
				if( empty($group) ) {
499
					
500
					continue;
501
					
502
				}
503
				
504
				
505
				// loop over group rules
506
				foreach( array_keys($group) as $j ) {
507
					
508
					// extract rule
509
					$rule = acf_extract_var( $group, $j );
510
					
511
					
512
					// migrate param
513
					if( isset($param_replace[ $rule['param'] ]) ) {
514
						
515
						$rule['param'] = $param_replace[ $rule['param'] ];
516
						
517
					}
518
					
519
					 	
520
				 	// category / taxonomy terms are saved differently
521
				 	if( $rule['param'] == 'post_category' || $rule['param'] == 'post_taxonomy' ) {
522
					 	
523
					 	if( is_numeric($rule['value']) ) {
524
						 	
525
						 	$term_id = $rule['value'];
526
						 	$taxonomy = $wpdb->get_var( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d LIMIT 1", $term_id) );
527
						 	$term = get_term( $term_id, $taxonomy );
528
						 	
529
						 	// update rule value
530
						 	$rule['value'] = "{$term->taxonomy}:{$term->slug}";
531
						 	
532
					 	}
533
					 	
534
				 	}
535
				 	
536
				 	
537
				 	// append rule
538
				 	$group[ $j ] = $rule;
539
				 	
540
				}
541
				// foreach
542
				
543
				
544
				// append group
545
				$field_group['location'][ $i ] = $group;
546
				
547
			}
548
			// foreach
549
			
550
		}
551
		// if
552
		
553
		
554
		// change layout to style (v5.0.0)
555
		if( !empty($field_group['layout']) ) {
556
		
557
			$field_group['style'] = acf_extract_var($field_group, 'layout');
558
			
559
		}
560
		
561
		
562
		// change no_box to seamless (v5.0.0)
563
		if( $field_group['style'] === 'no_box' ) {
564
		
565
			$field_group['style'] = 'seamless';
566
			
567
		}
568
		
569
		
570
		//return
571
		return $field_group;
572
		
573
	}
574
	
575
}
576
577
new acf_compatibility();
578
579
?>
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...
580