Completed
Push — develop ( fc3971...71605d )
by David
03:09 queued 11s
created

Jsonld_Converter::make_single()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * Define the Wordlift_Mapping_Jsonld_Converter class to add JSON-LD generated from mappings.
4
 *
5
 * @since   3.25.0
6
 * @package Wordlift
7
 * @subpackage Wordlift\Mappings
8
 */
9
10
namespace Wordlift\Mappings;
11
12
/**
13
 * This class takes the output from json-ld service and alter depends on the
14
 * rule and properties defined in sync mappings.
15
 *
16
 * @since 3.25.0
17
 */
18
class Jsonld_Converter {
19
	/**
20
	 * Enumerations for the field types.
21
	 * Enumerations for the field types.
22
	 */
23
	const FIELD_TYPE_TEXT_FIELD = 'text';
24
	const FIELD_TYPE_CUSTOM_FIELD = 'custom_field';
25
	const FIELD_TYPE_ACF = 'acf';
26
	/**
27
	 * The {@link Mappings_Validator} instance to test.
28
	 *
29
	 * @since  3.25.0
30
	 * @access private
31
	 * @var Mappings_Validator $validator The {@link Mappings_Validator} instance.
32
	 */
33
	private $validator;
34
35
	/**
36
	 * The {@link Mappings_Transform_Functions_Registry} instance.
37
	 *
38
	 * @since  3.25.0
39
	 * @access private
40
	 * @var Mappings_Transform_Functions_Registry $transform_functions_registry The {@link Mappings_Transform_Functions_Registry} instance.
41
	 */
42
	private $transform_functions_registry;
43
44
	/**
45
	 * Initialize all dependencies required.
46
	 *
47
	 * @param Mappings_Validator $validator A {@link Mappings_Validator} instance.
48
	 * @param Mappings_Transform_Functions_Registry $transform_functions_registry
49
	 */
50
	public function __construct( $validator, $transform_functions_registry ) {
51
52
		$this->validator                    = $validator;
53
		$this->transform_functions_registry = $transform_functions_registry;
54
55
		// Hook to refactor the JSON-LD.
56
		add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 );
57
		add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 );
58
59
	}
60
61
	/**
62
	 * Hook to `wl_post_jsonld_array` and `wl_entity_jsonld_array`.
63
	 *
64
	 * Receive the JSON-LD and the references in the array along with the post ID and transform them according to
65
	 * the configuration.
66
	 *
67
	 * @param array $value {
68
	 *      The array containing the JSON-LD and the references.
69
	 *
70
	 * @type array $jsonld The JSON-LD array.
71
	 * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter).
72
	 * }
73
	 *
74
	 * @param int $post_id The post ID.
75
	 *
76
	 * @return array An array with the updated JSON-LD and references.
77
	 */
78
	public function wl_post_jsonld_array( $value, $post_id ) {
79
80
		$jsonld     = $value['jsonld'];
81
		$references = $value['references'];
82
83
		return array(
84
			'jsonld'     => $this->wl_post_jsonld( $jsonld, $post_id, $references ),
85
			'references' => $references,
86
		);
87
	}
88
89
	/**
90
	 * Returns JSON-LD data after applying transformation functions.
91
	 *
92
	 * @param array $jsonld The JSON-LD structure.
93
	 * @param int $post_id The {@link WP_Post} id.
94
	 * @param array $references An array of post references.
95
	 *
96
	 * @return array the new refactored array structure.
97
	 * @since 3.25.0
98
	 */
99
	private function wl_post_jsonld( $jsonld, $post_id, &$references ) {
100
101
		// @@todo I think there's an issue here with the Validator, because you're changing the instance state and the
102
		// instance may be reused afterwards.
103
104
		$properties = $this->validator->validate( $post_id );
105
106
		foreach ( $properties as $property ) {
107
			$transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] );
108
			$data               = $this->get_data_from_data_source( $post_id, $property );
109
			if ( null !== $transform_instance ) {
110
				$transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id );
111
				if ( null !== $transform_data ) {
112
					$jsonld[ $property['property_name'] ] = $this->make_single( $transform_data );
113
				}
114
			} else {
115
				$jsonld[ $property['property_name'] ] = $this->make_single( $data );
116
			}
117
		}
118
119
		return $jsonld;
120
	}
121
122
	/**
123
	 * Returns data from data source.
124
	 *
125
	 * @param int $post_id Id of the post.
126
	 * @param array $property_data The property data for the post_id.
127
	 *
128
	 * @return array Returns key, value array, if the value is not found, then it returns null.
129
	 */
130
	final public function get_data_from_data_source( $post_id, $property_data ) {
131
		$value = $property_data['field_name'];
132
133
		// Do 1 to 1 mapping and return result.
134
		switch ( $property_data['field_type'] ) {
135
			case self::FIELD_TYPE_ACF:
136
				if ( ! function_exists( 'get_field' ) ) {
137
					return array();
138
				}
139
140
				return get_field( $property_data['field_name'], $post_id );
141
142
			case self::FIELD_TYPE_CUSTOM_FIELD:
143
144
				return array_map( 'wp_strip_all_tags', get_post_meta( $post_id, $value ) );
145
146
			default:
147
				return $value;
148
		}
149
150
	}
151
152
	private function make_single( $value ) {
153
154
		$values = (array) $value;
155
156
		if ( empty( $values ) ) {
157
			return null;
158
		}
159
160
		if ( 1 === count( $values ) ) {
161
			return $values[0];
162
		}
163
164
		return $values;
165
	}
166
167
}
168