Completed
Pull Request — develop (#1080)
by Naveen
03:17
created

Acf_Data_Source::get_data()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @since      3.27.0
4
 * @package    Wordlift
5
 * @subpackage Wordlift/Mappings/Data_Source
6
 */
7
namespace Wordlift\Mappings\Data_Source;
8
9
class Acf_Data_Source implements Abstract_Data_Source {
10
11
	public function get_data( $post_id, $property_data ) {
12
13
		if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) {
14
			return array();
15
		}
16
17
		return $this->get_data_for_acf_field( $property_data['field_name'], $post_id );
18
	}
19
20
	/**
21
	 * Gets data from acf, format the data if it is a repeater field.
22
	 *
23
	 * @param $field_name
24
	 * @param $post_id
25
	 *
26
	 * @return array|mixed
27
	 */
28
	private function get_data_for_acf_field( $field_name, $post_id ) {
29
		if ( get_queried_object() instanceof \WP_Term ) {
0 ignored issues
show
Bug introduced by
The class WP_Term does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
30
			// Data fetching method for term is different.
31
			$term       = get_queried_object();
32
			$field_data = get_field_object( $field_name, $term );
33
			$data       = get_field( $field_name, $term );
34
		} else {
35
			$field_data = get_field_object( $field_name, $post_id );
36
			$data       = get_field( $field_name, $post_id );
37
		}
38
		// only process if it is a repeater field, else return the data.
39
		if ( is_array( $field_data ) && array_key_exists( 'type', $field_data )
40
		     && $field_data['type'] === 'repeater' ) {
41
			/**
42
			 * check if we have only one sub field, currently we only support one subfield,
43
			 * so each repeater item should be checked if there is a single sub field.
44
			 */
45
			if ( is_array( $data ) &&
46
			     count( $data ) > 0 &&
47
			     count( array_keys( $data[0] ) ) === 1 ) {
48
				$repeater_formatted_data = array();
49
				foreach ( $data as $item ) {
50
					$repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) );
51
				}
52
				// Remove non unique values.
53
				$repeater_formatted_data = array_unique( $repeater_formatted_data );
54
				// Remove empty values
55
				$repeater_formatted_data = array_filter( $repeater_formatted_data, 'strlen' );
56
57
				// re-index all the values.
58
				return array_values( $repeater_formatted_data );
59
			}
60
		}
61
62
		// Return normal acf data if it is not a repeater field.
63
		return $data;
64
	}
65
}
66