-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-gf-field-helper-endpoint.php
235 lines (208 loc) · 5.65 KB
/
class-gf-field-helper-endpoint.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Field Helper for Gravity Forms Endpoint
*
* @package brilliant-plugins/field-helper-for-gravity-forms
*/
if ( ! class_exists( 'GFForms' ) ) {
die();
}
GFForms::include_addon_framework();
/**
* Field Helper for Gravity Forms Endpoint
*
* @package brilliant-plugins/field-helper-for-gravity-forms
*/
class GF_Field_Helper_Endpoint extends GF_REST_Entries_Controller {
/**
* Plugin slug.
*
* @since 1.0.0
*
* @var string $_slug
*/
protected $_slug = GF_FIELD_HELPER_SLUG;
/**
* API base.
*
* @since 1.0.0
*
* @var string $rest_base
*/
public $rest_base = 'json';
/**
* Register our REST endpoint.
*
* @since 1.0.0
* @since 1.0.2 Added support for forms.
*
* @return void
*/
public function register_rest_routes() {
$namespace = $this->namespace;
$base = $this->rest_base;
/**
* Entries.
*/
register_rest_route(
$namespace,
'/entries/' . $base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
)
);
register_rest_route(
$namespace,
'/entries/(?P<entry_id>[\d]+)/' . $base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
)
);
/**
* Forms.
*/
register_rest_route(
$namespace,
'/forms/(?P<form_id>[\d]+)/entries/' . $base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
)
);
register_rest_route(
$namespace,
'/forms/(?P<form_id>[\d]+)/entries/(?P<entry_id>[\d]+)/' . $base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
)
);
}
/**
* Include our custom query parameters.
*
* @since 1.4.0
*
* @return array
*/
public function get_collection_params() {
$core = parent::get_collection_params();
$custom = array(
'after' => array(
'description' => __( 'Entry ID or timestamp', 'gravity-forms-field-helper' ),
),
);
return array_merge( $core, $custom );
}
/**
* Parses the entry search, sort and paging parameters from the request
*
* @since 1.4.0
*
* @param WP_REST_Request $request Full data about the request.
*
* @return array Returns an associative array with the "search_criteria", "paging" and "sorting" keys appropriately populated.
*/
public function parse_entry_search_params( $request ) {
$params = parent::parse_entry_search_params( $request );
if ( ! $request->has_param( 'after' ) ) {
return $params;
}
$after = $request->get_param( 'after' );
if ( array_key_exists( 'entry_id', $after ) ) {
$params['search_criteria']['field_filters'][] = array(
'key' => 'id',
'operator' => '>',
'value' => absint( $after['entry_id'] ),
);
}
if ( array_key_exists( 'time', $after ) ) {
$params['search_criteria']['field_filters'][] = array(
'key' => 'date_created',
'operator' => '>',
'value' => ( new DateTimeImmutable( sanitize_text_field( wp_unslash( $after['time'] ) ) ) )->format( 'Y-m-d H:i:s' ),
);
}
return $params;
}
/**
* Get a collection of entries.
*
* @since 1.0.0
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$original = parent::get_items( $request );
return $this->customize_rest_request( $original );
}
/**
* Get a single entry.
*
* @since 1.0.0
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$original = parent::get_item( $request );
return $this->customize_rest_request( $original, true );
}
/**
* Add friendly field names to REST API response.
*
* @since 1.0.0
*
* @param WP_REST_Response|WP_Error $response Original API response.
* @param bool $single Whether this is a single entry or multiple entries.
*
* @return mixed Result to send to the client.
*/
public function customize_rest_request( $response, $single = false ) {
if ( is_wp_error( $response ) ) {
return $response;
}
$results = $response->get_data();
if ( $single ) {
$results = GF_Field_Helper_Common::replace_field_names( $results );
} else {
foreach ( $results['entries'] as $key => $result ) {
$results['entries'][ $key ] = GF_Field_Helper_Common::replace_field_names( $result );
}
}
/**
* Filter the API repsonse containing frienldy field names.
*
* @since 1.6.0
*
* @param array $results Form entry with friendly field names.
* @param WP_Rest_Response|WP_Error $response Original API response
* @param bool $single Whether this is a single entry or multiple entries.
*
* @return array
*/
$results = apply_filters( 'gf_field_helper_api_response', $results, $response, $single );
return new WP_REST_Response( $results, $response->get_status() );
}
}