forked from gravitywiz/snippet-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgppa-object-type-json-api.php
201 lines (152 loc) · 4.61 KB
/
gppa-object-type-json-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Gravity Perks // Populate Anything // JSON API Object Type
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* This snippet adds a new Object Type that pulls from a JSON file containing airports and their IATA codes.
* Use this snippet as a starting point for other HTTP-based APIs that you would like to populate fields from.
*
* Installation: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
*/
class GPPA_Object_Type_JSON_API extends GPPA_Object_Type {
public function __construct( $id ) {
parent::__construct( $id );
add_action( 'gppa_pre_object_type_query_' . $this->id, array( $this, 'add_filter_hooks' ) );
}
public function add_filter_hooks() {
add_filter( 'gppa_object_type_' . $this->id . '_filter', array( $this, 'process_filter_default' ), 10, 4 );
}
public function get_object_id( $object, $primary_property_value = null ) {
return $object['iata'];
}
public function get_label() {
return esc_html__( 'JSON API', 'gp-populate-anything' );
}
public function get_groups() {
return array(
'columns' => array(
'label' => esc_html__( 'Columns', 'gp-populate-anything' ),
),
);
}
public function get_properties( $_ = null ) {
$properties = array();
$json_props = array(
'iata' => 'IATA Code',
'lon' => 'Longitude',
'iso' => 'ISO Code',
'status' => 'Status',
'name' => 'Airport Name',
'continent' => 'Continent (Abbr)',
'type' => 'Type',
'lat' => 'Latitude',
'size' => 'Size',
);
foreach ( $json_props as $json_prop => $label ) {
$properties[ $json_prop ] = array(
'group' => 'columns',
'label' => $label,
'value' => $json_prop,
'orderby' => false,
'callable' => '__return_empty_array',
'operators' => array(
'is',
'isnot',
'contains',
),
);
}
return $properties;
}
public function fetch() {
$cache_key = 'airports_json';
if ( $data = get_transient( $cache_key ) ) {
return $data;
}
$url = esc_url_raw( 'https://raw.githubusercontent.com/jbrooksuk/JSON-Airports/master/airports.json' );
$results = wp_remote_get( $url );
$api_response = json_decode( wp_remote_retrieve_body( $results ), true );
set_transient( $cache_key, $api_response );
return $api_response;
}
public function process_filter_default( $search, $args ) {
/**
* @var $filter_value
* @var $filter
* @var $filter_group
* @var $filter_group_index
* @var $primary_property_value
* @var $property
* @var $property_id
*/
extract( $args );
$search[ $filter_group_index ][] = array(
'property' => $property_id,
'operator' => $filter['operator'],
'value' => $filter_value,
);
return $search;
}
public function perform_search( $var, $search ) {
switch ( $search['operator'] ) {
case 'is':
return ( $var[ $search['property'] ] == $search['value'] );
case 'isnot':
return ( $var[ $search['property'] ] != $search['value'] );
case 'contains':
return ( stripos( $var[ $search['property'] ], $search['value'] ) !== false );
default:
throw new Error( 'Invalid operator provided.' );
}
}
/**
* Each search group is an OR
*
* If everything matches in one group, we can immediately bail out as we have a positive match.
*
* @param $var
* @param $search_params
*
* @return bool
*/
public function search( $var, $search_params ) {
foreach ( $search_params as $search_group ) {
// For GPPA 2.0+, search_group may also read the numeric "limit" value, so skip for non-array values.
if ( ! is_array( $search_group ) ) {
continue;
}
$matches_group = true;
foreach ( $search_group as $search ) {
$matches_group = $this->perform_search( $var, $search );
if ( ! $matches_group ) {
break;
}
}
if ( $matches_group ) {
return true;
}
}
return false;
}
public function query( $args ) {
$search_params = $this->process_filter_groups( $args );
$results = $this->fetch();
if ( ! empty( $search_params ) ) {
$results = array_filter( $results, function ( $var ) use ( $search_params ) {
return $this->search( $var, $search_params );
} );
}
$query_limit = gp_populate_anything()->get_query_limit( $this, $args['field'] );
$query_results = array_slice( $results, 0, $query_limit );
return $query_results;
}
public function get_object_prop_value( $object, $prop ) {
if ( ! isset( $object[ $prop ] ) ) {
return null;
}
return $object[ $prop ];
}
}
add_action('init', function() {
gp_populate_anything()->register_object_type( 'json-api', 'GPPA_Object_Type_JSON_API' );
});