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 3.9 3.9-beta 3.9.1 3.9.2 3.9.3 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1-beta 4.1.1 4.1.2 4.2 4.2-beta 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6 4.6.1 4.7 4.8 4.8.1 4.9 4.9.1 4.9.2 5.0 5.0.1
contact-form-7 / includes / rest-api.php
contact-form-7 / includes Last commit date
block-editor 5 years ago css 5 years ago js 5 years ago capabilities.php 7 years ago config-validator.php 5 years ago contact-form-functions.php 5 years ago contact-form-template.php 5 years ago contact-form.php 5 years ago controller.php 7 years ago form-tag.php 5 years ago form-tags-manager.php 6 years ago formatting.php 5 years ago functions.php 5 years ago integration.php 7 years ago l10n.php 5 years ago mail.php 5 years ago pipe.php 5 years ago rest-api.php 5 years ago shortcodes.php 9 years ago special-mail-tags.php 5 years ago submission.php 5 years ago upgrade.php 7 years ago validation.php 7 years ago
rest-api.php
439 lines
1 <?php
2
3 add_action( 'rest_api_init', 'wpcf7_rest_api_init', 10, 0 );
4
5 function wpcf7_rest_api_init() {
6 $namespace = 'contact-form-7/v1';
7
8 register_rest_route( $namespace,
9 '/contact-forms',
10 array(
11 array(
12 'methods' => WP_REST_Server::READABLE,
13 'callback' => 'wpcf7_rest_get_contact_forms',
14 'permission_callback' => function() {
15 if ( current_user_can( 'wpcf7_read_contact_forms' ) ) {
16 return true;
17 } else {
18 return new WP_Error( 'wpcf7_forbidden',
19 __( "You are not allowed to access contact forms.", 'contact-form-7' ),
20 array( 'status' => 403 )
21 );
22 }
23 },
24 ),
25 array(
26 'methods' => WP_REST_Server::CREATABLE,
27 'callback' => 'wpcf7_rest_create_contact_form',
28 'permission_callback' => function() {
29 if ( current_user_can( 'wpcf7_edit_contact_forms' ) ) {
30 return true;
31 } else {
32 return new WP_Error( 'wpcf7_forbidden',
33 __( "You are not allowed to create a contact form.", 'contact-form-7' ),
34 array( 'status' => 403 )
35 );
36 }
37 },
38 ),
39 )
40 );
41
42 register_rest_route( $namespace,
43 '/contact-forms/(?P<id>\d+)',
44 array(
45 array(
46 'methods' => WP_REST_Server::READABLE,
47 'callback' => 'wpcf7_rest_get_contact_form',
48 'permission_callback' => function( WP_REST_Request $request ) {
49 $id = (int) $request->get_param( 'id' );
50
51 if ( current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
52 return true;
53 } else {
54 return new WP_Error( 'wpcf7_forbidden',
55 __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
56 array( 'status' => 403 )
57 );
58 }
59 },
60 ),
61 array(
62 'methods' => WP_REST_Server::EDITABLE,
63 'callback' => 'wpcf7_rest_update_contact_form',
64 'permission_callback' => function( WP_REST_Request $request ) {
65 $id = (int) $request->get_param( 'id' );
66
67 if ( current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
68 return true;
69 } else {
70 return new WP_Error( 'wpcf7_forbidden',
71 __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
72 array( 'status' => 403 )
73 );
74 }
75 },
76 ),
77 array(
78 'methods' => WP_REST_Server::DELETABLE,
79 'callback' => 'wpcf7_rest_delete_contact_form',
80 'permission_callback' => function( WP_REST_Request $request ) {
81 $id = (int) $request->get_param( 'id' );
82
83 if ( current_user_can( 'wpcf7_delete_contact_form', $id ) ) {
84 return true;
85 } else {
86 return new WP_Error( 'wpcf7_forbidden',
87 __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
88 array( 'status' => 403 )
89 );
90 }
91 },
92 ),
93 )
94 );
95
96 register_rest_route( $namespace,
97 '/contact-forms/(?P<id>\d+)/feedback',
98 array(
99 array(
100 'methods' => WP_REST_Server::CREATABLE,
101 'callback' => 'wpcf7_rest_create_feedback',
102 'permission_callback' => '__return_true',
103 ),
104 )
105 );
106
107 register_rest_route( $namespace,
108 '/contact-forms/(?P<id>\d+)/refill',
109 array(
110 array(
111 'methods' => WP_REST_Server::READABLE,
112 'callback' => 'wpcf7_rest_get_refill',
113 'permission_callback' => '__return_true',
114 ),
115 )
116 );
117 }
118
119 function wpcf7_rest_get_contact_forms( WP_REST_Request $request ) {
120 $args = array();
121
122 $per_page = $request->get_param( 'per_page' );
123
124 if ( null !== $per_page ) {
125 $args['posts_per_page'] = (int) $per_page;
126 }
127
128 $offset = $request->get_param( 'offset' );
129
130 if ( null !== $offset ) {
131 $args['offset'] = (int) $offset;
132 }
133
134 $order = $request->get_param( 'order' );
135
136 if ( null !== $order ) {
137 $args['order'] = (string) $order;
138 }
139
140 $orderby = $request->get_param( 'orderby' );
141
142 if ( null !== $orderby ) {
143 $args['orderby'] = (string) $orderby;
144 }
145
146 $search = $request->get_param( 'search' );
147
148 if ( null !== $search ) {
149 $args['s'] = (string) $search;
150 }
151
152 $items = WPCF7_ContactForm::find( $args );
153
154 $response = array();
155
156 foreach ( $items as $item ) {
157 $response[] = array(
158 'id' => $item->id(),
159 'slug' => $item->name(),
160 'title' => $item->title(),
161 'locale' => $item->locale(),
162 );
163 }
164
165 return rest_ensure_response( $response );
166 }
167
168 function wpcf7_rest_create_contact_form( WP_REST_Request $request ) {
169 $id = (int) $request->get_param( 'id' );
170
171 if ( $id ) {
172 return new WP_Error( 'wpcf7_post_exists',
173 __( "Cannot create existing contact form.", 'contact-form-7' ),
174 array( 'status' => 400 )
175 );
176 }
177
178 $args = $request->get_params();
179 $args['id'] = -1; // Create
180 $context = $request->get_param( 'context' );
181 $item = wpcf7_save_contact_form( $args, $context );
182
183 if ( ! $item ) {
184 return new WP_Error( 'wpcf7_cannot_save',
185 __( "There was an error saving the contact form.", 'contact-form-7' ),
186 array( 'status' => 500 )
187 );
188 }
189
190 $response = array(
191 'id' => $item->id(),
192 'slug' => $item->name(),
193 'title' => $item->title(),
194 'locale' => $item->locale(),
195 'properties' => wpcf7_get_properties_for_api( $item ),
196 'config_errors' => array(),
197 );
198
199 if ( wpcf7_validate_configuration() ) {
200 $config_validator = new WPCF7_ConfigValidator( $item );
201 $config_validator->validate();
202
203 $response['config_errors'] = $config_validator->collect_error_messages();
204
205 if ( 'save' == $context ) {
206 $config_validator->save();
207 }
208 }
209
210 return rest_ensure_response( $response );
211 }
212
213 function wpcf7_rest_get_contact_form( WP_REST_Request $request ) {
214 $id = (int) $request->get_param( 'id' );
215 $item = wpcf7_contact_form( $id );
216
217 if ( ! $item ) {
218 return new WP_Error( 'wpcf7_not_found',
219 __( "The requested contact form was not found.", 'contact-form-7' ),
220 array( 'status' => 404 )
221 );
222 }
223
224 $response = array(
225 'id' => $item->id(),
226 'slug' => $item->name(),
227 'title' => $item->title(),
228 'locale' => $item->locale(),
229 'properties' => wpcf7_get_properties_for_api( $item ),
230 );
231
232 return rest_ensure_response( $response );
233 }
234
235 function wpcf7_rest_update_contact_form( WP_REST_Request $request ) {
236 $id = (int) $request->get_param( 'id' );
237 $item = wpcf7_contact_form( $id );
238
239 if ( ! $item ) {
240 return new WP_Error( 'wpcf7_not_found',
241 __( "The requested contact form was not found.", 'contact-form-7' ),
242 array( 'status' => 404 )
243 );
244 }
245
246 $args = $request->get_params();
247 $context = $request->get_param( 'context' );
248 $item = wpcf7_save_contact_form( $args, $context );
249
250 if ( ! $item ) {
251 return new WP_Error( 'wpcf7_cannot_save',
252 __( "There was an error saving the contact form.", 'contact-form-7' ),
253 array( 'status' => 500 )
254 );
255 }
256
257 $response = array(
258 'id' => $item->id(),
259 'slug' => $item->name(),
260 'title' => $item->title(),
261 'locale' => $item->locale(),
262 'properties' => wpcf7_get_properties_for_api( $item ),
263 'config_errors' => array(),
264 );
265
266 if ( wpcf7_validate_configuration() ) {
267 $config_validator = new WPCF7_ConfigValidator( $item );
268 $config_validator->validate();
269
270 $response['config_errors'] = $config_validator->collect_error_messages();
271
272 if ( 'save' == $context ) {
273 $config_validator->save();
274 }
275 }
276
277 return rest_ensure_response( $response );
278 }
279
280 function wpcf7_rest_delete_contact_form( WP_REST_Request $request ) {
281 $id = (int) $request->get_param( 'id' );
282 $item = wpcf7_contact_form( $id );
283
284 if ( ! $item ) {
285 return new WP_Error( 'wpcf7_not_found',
286 __( "The requested contact form was not found.", 'contact-form-7' ),
287 array( 'status' => 404 )
288 );
289 }
290
291 $result = $item->delete();
292
293 if ( ! $result ) {
294 return new WP_Error( 'wpcf7_cannot_delete',
295 __( "There was an error deleting the contact form.", 'contact-form-7' ),
296 array( 'status' => 500 )
297 );
298 }
299
300 $response = array( 'deleted' => true );
301
302 return rest_ensure_response( $response );
303 }
304
305 function wpcf7_rest_create_feedback( WP_REST_Request $request ) {
306 $url_params = $request->get_url_params();
307
308 $item = null;
309
310 if ( ! empty( $url_params['id'] ) ) {
311 $item = wpcf7_contact_form( $url_params['id'] );
312 }
313
314 if ( ! $item ) {
315 return new WP_Error( 'wpcf7_not_found',
316 __( "The requested contact form was not found.", 'contact-form-7' ),
317 array( 'status' => 404 )
318 );
319 }
320
321 $result = $item->submit();
322
323 $unit_tag = $request->get_param( '_wpcf7_unit_tag' );
324
325 $response = array(
326 'into' => '#' . wpcf7_sanitize_unit_tag( $unit_tag ),
327 'status' => $result['status'],
328 'message' => $result['message'],
329 'posted_data_hash' => $result['posted_data_hash'],
330 );
331
332 if ( 'validation_failed' == $result['status'] ) {
333 $invalid_fields = array();
334
335 foreach ( (array) $result['invalid_fields'] as $name => $field ) {
336 $invalid_fields[] = array(
337 'into' => 'span.wpcf7-form-control-wrap.'
338 . sanitize_html_class( $name ),
339 'message' => $field['reason'],
340 'idref' => $field['idref'],
341 'error_id' => sprintf(
342 '%1$s-ve-%2$s',
343 $unit_tag,
344 $name
345 ),
346 );
347 }
348
349 $response['invalid_fields'] = $invalid_fields;
350 }
351
352 $response = wpcf7_apply_filters_deprecated(
353 'wpcf7_ajax_json_echo',
354 array( $response, $result ),
355 '5.2',
356 'wpcf7_feedback_response'
357 );
358
359 $response = apply_filters( 'wpcf7_feedback_response', $response, $result );
360
361 return rest_ensure_response( $response );
362 }
363
364 function wpcf7_rest_get_refill( WP_REST_Request $request ) {
365 $id = (int) $request->get_param( 'id' );
366 $item = wpcf7_contact_form( $id );
367
368 if ( ! $item ) {
369 return new WP_Error( 'wpcf7_not_found',
370 __( "The requested contact form was not found.", 'contact-form-7' ),
371 array( 'status' => 404 )
372 );
373 }
374
375 $response = wpcf7_apply_filters_deprecated(
376 'wpcf7_ajax_onload',
377 array( array() ),
378 '5.2',
379 'wpcf7_refill_response'
380 );
381
382 $response = apply_filters( 'wpcf7_refill_response', array() );
383
384 return rest_ensure_response( $response );
385 }
386
387 function wpcf7_get_properties_for_api( WPCF7_ContactForm $contact_form ) {
388 $properties = $contact_form->get_properties();
389
390 $properties['form'] = array(
391 'content' => (string) $properties['form'],
392 'fields' => array_map(
393 function( WPCF7_FormTag $form_tag ) {
394 return array(
395 'type' => $form_tag->type,
396 'basetype' => $form_tag->basetype,
397 'name' => $form_tag->name,
398 'options' => $form_tag->options,
399 'raw_values' => $form_tag->raw_values,
400 'labels' => $form_tag->labels,
401 'values' => $form_tag->values,
402 'pipes' => $form_tag->pipes instanceof WPCF7_Pipes
403 ? $form_tag->pipes->to_array()
404 : $form_tag->pipes,
405 'content' => $form_tag->content,
406 );
407 },
408 $contact_form->scan_form_tags()
409 ),
410 );
411
412 $properties['additional_settings'] = array(
413 'content' => (string) $properties['additional_settings'],
414 'settings' => array_filter( array_map(
415 function( $setting ) {
416 $pattern = '/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/';
417
418 if ( preg_match( $pattern, $setting, $matches ) ) {
419 $name = trim( $matches[1] );
420 $value = trim( $matches[2] );
421
422 if ( in_array( $value, array( 'on', 'true' ), true ) ) {
423 $value = true;
424 } elseif ( in_array( $value, array( 'off', 'false' ), true ) ) {
425 $value = false;
426 }
427
428 return array( $name, $value );
429 }
430
431 return false;
432 },
433 explode( "\n", $properties['additional_settings'] )
434 ) ),
435 );
436
437 return $properties;
438 }
439