|
1
|
|
|
<?php |
|
2
|
|
|
namespace Carbon_Fields\REST; |
|
3
|
|
|
|
|
4
|
|
|
use Carbon_Fields\Container\Container; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class for retrieving relative data for REST responses |
|
8
|
|
|
*/ |
|
9
|
|
|
class Data_Manager { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Special field types, that require |
|
13
|
|
|
* different data loading |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
public $special_field_types = array( |
|
18
|
|
|
'complex', |
|
19
|
|
|
'relationship', |
|
20
|
|
|
'association', |
|
21
|
|
|
'map' |
|
|
|
|
|
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Field types that should be excluded |
|
26
|
|
|
* from the REST response |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $exclude_field_types = array( |
|
31
|
|
|
'html', |
|
32
|
|
|
'separator', |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Instance of the Container_Validator class |
|
37
|
|
|
* |
|
38
|
|
|
* @var object |
|
39
|
|
|
*/ |
|
40
|
|
|
public $container_validator; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct( $validator ) { |
|
43
|
|
|
$this->container_validator = $validator; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the Carbon Fields data based |
|
48
|
|
|
* on $type and $id |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $type |
|
51
|
|
|
* @param string $id |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function get_data( $type, $id = '' ) { |
|
55
|
|
|
$response = array(); |
|
56
|
|
|
$containers = $this->filter_containers( $type, $id ); |
|
57
|
|
|
|
|
58
|
|
|
foreach ( $containers as $container ) { |
|
59
|
|
|
$fields = $this->filter_fields( $container->get_fields() ); |
|
60
|
|
|
|
|
61
|
|
|
foreach ( $fields as $field ) { |
|
62
|
|
|
|
|
63
|
|
|
if ( $id ) { |
|
64
|
|
|
$field->get_datastore()->set_id( $id ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$field->load(); |
|
68
|
|
|
|
|
69
|
|
|
$field_type = in_array( strtolower( $field->type ), $this->special_field_types ) ? strtolower( $field->type ) : 'generic'; |
|
70
|
|
|
|
|
71
|
|
|
$response[ $field->get_name() ] = call_user_func( array( $this, "load_{$field_type}_field_value" ), $field ); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $response; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Filters all available containers |
|
80
|
|
|
* based on $type |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $type |
|
83
|
|
|
* @param string $id |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function filter_containers( $type, $id = '' ) { |
|
87
|
|
|
return array_filter( Container::$active_containers, function( $container ) use ( $type, $id ) { |
|
88
|
|
|
return $this->container_validator->is_valid_container( $container, $type, $id ); |
|
89
|
|
|
} ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Checks if fields should be excluded from the response |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $fields |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function filter_fields( $fields ) { |
|
99
|
|
|
return array_filter( $fields, array( $this, 'should_load_field' ) ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Checks if a field should be excluded from the response |
|
104
|
|
|
* |
|
105
|
|
|
* @param object $field |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function should_load_field( $field ) { |
|
109
|
|
|
return $field->get_rest_visibility() && ! in_array( strtolower( $field->type ), $this->exclude_field_types ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Loads field value |
|
114
|
|
|
* |
|
115
|
|
|
* @param object $field |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function load_generic_field_value( $field ) { |
|
119
|
|
|
return $field->get_value(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Loads the value of a complex field |
|
124
|
|
|
* |
|
125
|
|
|
* @param object $field |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
public function load_complex_field_value( $field ) { |
|
129
|
|
|
$field_json = $field->to_json( false ); |
|
130
|
|
|
return $field_json['value']; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Load the value of a map field |
|
135
|
|
|
* |
|
136
|
|
|
* @param object $field |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
public function load_map_field_value( $field ) { |
|
140
|
|
|
$map_data = $field->to_json( false ); |
|
141
|
|
|
|
|
142
|
|
|
return array( |
|
143
|
|
|
'lat' => $map_data['lat'], |
|
144
|
|
|
'lng' => $map_data['lng'], |
|
145
|
|
|
'zoom' => $map_data['zoom'], |
|
146
|
|
|
'address' => $map_data['address'], |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Loads the value of a relationship field |
|
152
|
|
|
* |
|
153
|
|
|
* @param object $field |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
public function load_relationship_field_value( $field ) { |
|
157
|
|
|
return maybe_unserialize( $field->get_value() ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Loads the value of an association field |
|
162
|
|
|
* |
|
163
|
|
|
* @param object $field |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
public function load_association_field_value( $field ) { |
|
167
|
|
|
$field->process_value(); |
|
168
|
|
|
return $field->get_value(); |
|
169
|
|
|
} |
|
170
|
|
|
} |