|
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 ) { |
|
|
|
|
|
|
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
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto 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
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.