acf_field_post_object::get_choices()   C
last analyzed

Complexity

Conditions 12
Paths 25

Size

Total Lines 148

Duplication

Lines 88
Ratio 59.46 %

Importance

Changes 0
Metric Value
cc 12
nc 25
nop 1
dl 88
loc 148
rs 5.5733
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 Post Object Field Class
5
*
6
*  All the logic for this field type
7
*
8
*  @class 		acf_field_post_object
9
*  @extends		acf_field
10
*  @package		ACF
11
*  @subpackage	Fields
12
*/
13
14
if( ! class_exists('acf_field_post_object') ) :
15
16
class acf_field_post_object 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
	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...
33
		
34
		// vars
35
		$this->name = 'post_object';
36
		$this->label = __("Post Object",'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 = 'relational';
38
		$this->defaults = array(
39
			'post_type'		=> array(),
40
			'taxonomy'		=> array(),
41
			'allow_null' 	=> 0,
42
			'multiple'		=> 0,
43
			'return_format'	=> 'object',
44
			'ui'			=> 1,
45
		);
46
		
47
		
48
		// extra
49
		add_action('wp_ajax_acf/fields/post_object/query',			array($this, 'ajax_query'));
50
		add_action('wp_ajax_nopriv_acf/fields/post_object/query',	array($this, 'ajax_query'));
51
		
52
		
53
		// do not delete!
54
    	parent::__construct();
55
		
56
	}
57
	
58
	
59
	/*
60
	*  get_choices
61
	*
62
	*  This function will return an array of data formatted for use in a select2 AJAX response
63
	*
64
	*  @type	function
65
	*  @date	15/10/2014
66
	*  @since	5.0.9
67
	*
68
	*  @param	$options (array)
69
	*  @return	(array)
70
	*/
71
	
72
	function get_choices( $options = array() ) {
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...
73
		
74
		// defaults
75
   		$options = acf_parse_args($options, array(
76
			'post_id'		=> 0,
77
			's'				=> '',
78
			'field_key'		=> '',
79
			'paged'			=> 1
80
		));
81
		
82
		
83
		// vars
84
   		$r = array();
85
   		$args = array();
86
   		
87
		
88
		// paged
89
   		$args['posts_per_page'] = 20;
90
   		$args['paged'] = $options['paged'];
91
   		
92
   		
93
		// load field
94
		$field = acf_get_field( $options['field_key'] );
95
		
96
		if( !$field ) {
97
		
98
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by acf_field_post_object::get_choices of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
99
			
100
		}
101
		
102
		
103
		// update $args
104 View Code Duplication
		if( !empty($field['post_type']) ) {
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...
105
		
106
			$args['post_type'] = acf_get_array( $field['post_type'] );
107
			
108
		} else {
109
			
110
			$args['post_type'] = acf_get_post_types();
111
			
112
		}
113
114
		
115
		// create tax queries
116 View Code Duplication
		if( !empty($field['taxonomy']) ) {
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...
117
			
118
			// append to $args
119
			$args['tax_query'] = array();
120
			
121
			
122
			// decode terms
123
			$taxonomies = acf_decode_taxonomy_terms( $field['taxonomy'] );
124
			
125
			
126
			// now create the tax queries
127
			foreach( $taxonomies as $taxonomy => $terms ) {
128
			
129
				$args['tax_query'][] = array(
130
					'taxonomy'	=> $taxonomy,
131
					'field'		=> 'slug',
132
					'terms'		=> $terms,
133
				);
134
				
135
			}
136
			
137
		}
138
		
139
		
140
		// search
141
		if( $options['s'] ) {
142
		
143
			$args['s'] = $options['s'];
144
			
145
		}
146
		
147
		
148
		// filters
149
		$args = apply_filters('acf/fields/post_object/query', $args, $field, $options['post_id']);
150
		$args = apply_filters('acf/fields/post_object/query/name=' . $field['name'], $args, $field, $options['post_id'] );
151
		$args = apply_filters('acf/fields/post_object/query/key=' . $field['key'], $args, $field, $options['post_id'] );
152
		
153
		
154
		// get posts grouped by post type
155
		$groups = acf_get_grouped_posts( $args );
156
		
157 View Code Duplication
		if( !empty($groups) ) {
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...
158
			
159
			foreach( array_keys($groups) as $group_title ) {
160
				
161
				// vars
162
				$posts = acf_extract_var( $groups, $group_title );
163
				$titles = array();
0 ignored issues
show
Unused Code introduced by
$titles 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...
164
				
165
				
166
				// data
167
				$data = array(
168
					'text'		=> $group_title,
169
					'children'	=> array()
170
				);
171
				
172
				
173
				foreach( array_keys($posts) as $post_id ) {
174
					
175
					// override data
176
					$posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'] );
177
					
178
				};
179
				
180
				
181
				// order by search
182
				if( !empty($args['s']) ) {
183
					
184
					$posts = acf_order_by_search( $posts, $args['s'] );
185
					
186
				}
187
				
188
				
189
				// append to $data
190
				foreach( array_keys($posts) as $post_id ) {
191
					
192
					$data['children'][] = array(
193
						'id'	=> $post_id,
194
						'text'	=> $posts[ $post_id ]
195
					);
196
					
197
				}
198
				
199
				
200
				// append to $r
201
				$r[] = $data;
202
				
203
			}
204
			
205
			
206
			// optgroup or single
207
			if( count($args['post_type']) == 1 ) {
208
				
209
				$r = $r[0]['children'];
210
				
211
			}
212
			
213
		}
214
		
215
		
216
		// return
217
		return $r;
218
		
219
	}
220
	
221
	
222
	/*
223
	*  ajax_query
224
	*
225
	*  description
226
	*
227
	*  @type	function
228
	*  @date	24/10/13
229
	*  @since	5.0.0
230
	*
231
	*  @param	$post_id (int)
232
	*  @return	$post_id (int)
233
	*/
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...
234
	
235 View Code Duplication
	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...
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...
236
		
237
		// validate
238
		if( !acf_verify_ajax() ) {
239
		
240
			die();
241
			
242
		}
243
		
244
		
245
		// get choices
246
		$choices = $this->get_choices( $_POST );
247
		
248
		
249
		// validate
250
		if( !$choices ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $choices of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
251
			
252
			die();
253
			
254
		}
255
		
256
		
257
		// return JSON
258
		echo json_encode( $choices );
259
		die();
260
			
261
	}
262
	
263
	
264
	/*
265
	*  get_post_title
266
	*
267
	*  This function returns the HTML for a result
268
	*
269
	*  @type	function
270
	*  @date	1/11/2013
271
	*  @since	5.0.0
272
	*
273
	*  @param	$post (object)
274
	*  @param	$field (array)
275
	*  @param	$post_id (int) the post_id to which this value is saved to
276
	*  @return	(string)
277
	*/
278
	
279 View Code Duplication
	function get_post_title( $post, $field, $post_id = 0 ) {
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...
280
		
281
		// get post_id
282
		if( !$post_id ) {
283
			
284
			$form_data = acf_get_setting('form_data');
285
			
286
			if( !empty($form_data['post_id']) ) {
287
				
288
				$post_id = $form_data['post_id'];
289
				
290
			} else {
291
				
292
				$post_id = get_the_ID();
293
				
294
			}
295
			
296
		}
297
		
298
		
299
		// vars
300
		$title = acf_get_post_title( $post );
301
			
302
		
303
		// filters
304
		$title = apply_filters('acf/fields/post_object/result', $title, $post, $field, $post_id);
305
		$title = apply_filters('acf/fields/post_object/result/name=' . $field['_name'], $title, $post, $field, $post_id);
306
		$title = apply_filters('acf/fields/post_object/result/key=' . $field['key'], $title, $post, $field, $post_id);
307
		
308
		
309
		// return
310
		return $title;
311
	}
312
	
313
	
314
	/*
315
	*  render_field()
316
	*
317
	*  Create the HTML interface for your field
318
	*
319
	*  @param	$field - an array holding all the field's data
320
	*
321
	*  @type	action
322
	*  @since	3.6
323
	*  @date	23/01/13
324
	*/
325
	
326
	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...
327
		
328
		// Change Field into a select
329
		$field['type'] = 'select';
330
		$field['ui'] = 1;
331
		$field['ajax'] = 1;
332
		$field['choices'] = array();
333
		
334
		
335
		// populate choices if value exists
336
		if( !empty($field['value']) ) {
337
			
338
			// get posts
339
			$posts = acf_get_posts(array(
340
				'post__in' => $field['value'],
341
				'post_type'	=> $field['post_type']
342
			));
343
			
344
			
345
			// set choices
346 View Code Duplication
			if( !empty($posts) ) {
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...
347
				
348
				foreach( array_keys($posts) as $i ) {
349
					
350
					// vars
351
					$post = acf_extract_var( $posts, $i );
352
					
353
					
354
					// append to choices
355
					$field['choices'][ $post->ID ] = $this->get_post_title( $post, $field );
356
					
357
				}
358
				
359
			}
360
			
361
		}
362
363
		
364
		// render
365
		acf_render_field( $field );
0 ignored issues
show
Documentation introduced by
$field is of type array<string,string|inte...string|integer|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...
366
	}
367
	
368
	
369
	/*
370
	*  render_field_settings()
371
	*
372
	*  Create extra options for your field. This is rendered when editing a field.
373
	*  The value of $field['name'] can be used (like bellow) to save extra data to the $field
374
	*
375
	*  @type	action
376
	*  @since	3.6
377
	*  @date	23/01/13
378
	*
379
	*  @param	$field	- an array holding all the field's data
380
	*/
381
	
382
	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...
383
		
384
		// default_value
385
		acf_render_field_setting( $field, array(
386
			'label'			=> __('Filter by Post Type','acf'),
387
			'instructions'	=> '',
388
			'type'			=> 'select',
389
			'name'			=> 'post_type',
390
			'choices'		=> acf_get_pretty_post_types(),
391
			'multiple'		=> 1,
392
			'ui'			=> 1,
393
			'allow_null'	=> 1,
394
			'placeholder'	=> __("All post types",'acf'),
395
		));
396
		
397
		
398
		// default_value
399
		acf_render_field_setting( $field, array(
400
			'label'			=> __('Filter by Taxonomy','acf'),
401
			'instructions'	=> '',
402
			'type'			=> 'select',
403
			'name'			=> 'taxonomy',
404
			'choices'		=> acf_get_taxonomy_terms(),
405
			'multiple'		=> 1,
406
			'ui'			=> 1,
407
			'allow_null'	=> 1,
408
			'placeholder'	=> __("All taxonomies",'acf'),
409
		));
410
		
411
		
412
		// allow_null
413
		acf_render_field_setting( $field, array(
414
			'label'			=> __('Allow Null?','acf'),
415
			'instructions'	=> '',
416
			'type'			=> 'radio',
417
			'name'			=> 'allow_null',
418
			'choices'		=> array(
419
				1				=> __("Yes",'acf'),
420
				0				=> __("No",'acf'),
421
			),
422
			'layout'	=>	'horizontal',
423
		));
424
		
425
		
426
		// multiple
427
		acf_render_field_setting( $field, array(
428
			'label'			=> __('Select multiple values?','acf'),
429
			'instructions'	=> '',
430
			'type'			=> 'radio',
431
			'name'			=> 'multiple',
432
			'choices'		=> array(
433
				1				=> __("Yes",'acf'),
434
				0				=> __("No",'acf'),
435
			),
436
			'layout'	=>	'horizontal',
437
		));
438
		
439
		
440
		// return_format
441
		acf_render_field_setting( $field, array(
442
			'label'			=> __('Return Format','acf'),
443
			'instructions'	=> '',
444
			'type'			=> 'radio',
445
			'name'			=> 'return_format',
446
			'choices'		=> array(
447
				'object'		=> __("Post Object",'acf'),
448
				'id'			=> __("Post ID",'acf'),
449
			),
450
			'layout'	=>	'horizontal',
451
		));
452
				
453
	}
454
	
455
	
456
	/*
457
	*  load_value()
458
	*
459
	*  This filter is applied to the $value after it is loaded from the db
460
	*
461
	*  @type	filter
462
	*  @since	3.6
463
	*  @date	23/01/13
464
	*
465
	*  @param	$value (mixed) the value found in the database
466
	*  @param	$post_id (mixed) the $post_id from which the value was loaded
467
	*  @param	$field (array) the field array holding all the field options
468
	*  @return	$value
469
	*/
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...
470
	
471
	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...
472
		
473
		// ACF4 null
474
		if( $value === 'null' ) {
475
		
476
			return false;
477
			
478
		}
479
		
480
		
481
		// return
482
		return $value;
483
	}
484
	
485
	
486
	/*
487
	*  format_value()
488
	*
489
	*  This filter is appied to the $value after it is loaded from the db and before it is returned to the template
490
	*
491
	*  @type	filter
492
	*  @since	3.6
493
	*  @date	23/01/13
494
	*
495
	*  @param	$value (mixed) the value which was loaded from the database
496
	*  @param	$post_id (mixed) the $post_id from which the value was loaded
497
	*  @param	$field (array) the field array holding all the field options
498
	*
499
	*  @return	$value (mixed) the modified value
500
	*/
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...
501
	
502
	function format_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...
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...
503
		
504
		// bail early if no value
505
		if( empty($value) ) {
506
			
507
			return $value;
508
		
509
		}
510
		
511
		
512
		// force value to array
513
		$value = acf_get_array( $value );
514
		
515
		
516
		// convert values to int
517
		$value = array_map('intval', $value);
518
		
519
		
520
		// load posts if needed
521 View Code Duplication
		if( $field['return_format'] == 'object' ) {
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...
522
			
523
			// get posts
524
			$value = acf_get_posts(array(
525
				'post__in' => $value,
526
				'post_type'	=> $field['post_type']
527
			));
528
		
529
		}
530
		
531
		
532
		// convert back from array if neccessary
533
		if( !$field['multiple'] ) {
534
		
535
			$value = array_shift($value);
536
			
537
		}
538
		
539
		
540
		// return value
541
		return $value;
542
		
543
	}
544
	
545
	
546
	/*
547
	*  update_value()
548
	*
549
	*  This filter is appied to the $value before it is updated in the db
550
	*
551
	*  @type	filter
552
	*  @since	3.6
553
	*  @date	23/01/13
554
	*
555
	*  @param	$value - the value which will be saved in the database
556
	*  @param	$post_id - the $post_id of which the value will be saved
557
	*  @param	$field - the field array holding all the field options
558
	*
559
	*  @return	$value - the modified value
560
	*/
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...
561
	
562 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...
563
		
564
		// validate
565
		if( empty($value) ) {
566
		
567
			return $value;
568
			
569
		}
570
		
571
		
572
		// format
573
		if( is_array($value) ) {
574
			
575
			// array
576
			foreach( $value as $k => $v ){
577
			
578
				// object?
579
				if( is_object($v) && isset($v->ID) ) {
580
					
581
					$value[ $k ] = $v->ID;
582
				
583
				}
584
			
585
			}
586
			
587
			
588
			// save value as strings, so we can clearly search for them in SQL LIKE statements
589
			$value = array_map('strval', $value);
590
			
591
		} elseif( is_object($value) && isset($value->ID) ) {
592
			
593
			// object
594
			$value = $value->ID;
595
			
596
		}
597
		
598
		
599
		// return
600
		return $value;
601
		
602
	}
603
	
604
}
605
606
new acf_field_post_object();
607
608
endif;
609
610
?>
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...
611