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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 3 years ago css 3 years ago js 3 years ago swv 3 years ago capabilities.php 7 years ago config-validator.php 3 years ago contact-form-functions.php 3 years ago contact-form-template.php 3 years ago contact-form.php 3 years ago controller.php 3 years ago file.php 3 years ago form-tag.php 3 years ago form-tags-manager.php 3 years ago formatting.php 3 years ago functions.php 3 years ago html-formatter.php 3 years ago integration.php 3 years ago l10n.php 3 years ago mail.php 3 years ago pipe.php 4 years ago pocket-holder.php 3 years ago rest-api.php 4 years ago shortcodes.php 3 years ago special-mail-tags.php 3 years ago submission.php 3 years ago upgrade.php 7 years ago validation-functions.php 3 years ago validation.php 3 years ago
rest-api.php
513 lines
1 <?php
2
3 add_action(
4 'rest_api_init',
5 function () {
6 $controller = new WPCF7_REST_Controller;
7 $controller->register_routes();
8 },
9 10, 0
10 );
11
12
13 class WPCF7_REST_Controller {
14
15 const route_namespace = 'contact-form-7/v1';
16
17 public function register_routes() {
18
19 register_rest_route( self::route_namespace,
20 '/contact-forms',
21 array(
22 array(
23 'methods' => WP_REST_Server::READABLE,
24 'callback' => array( $this, 'get_contact_forms' ),
25 'permission_callback' => function () {
26 if ( current_user_can( 'wpcf7_read_contact_forms' ) ) {
27 return true;
28 } else {
29 return new WP_Error( 'wpcf7_forbidden',
30 __( "You are not allowed to access contact forms.", 'contact-form-7' ),
31 array( 'status' => 403 )
32 );
33 }
34 },
35 ),
36 array(
37 'methods' => WP_REST_Server::CREATABLE,
38 'callback' => array( $this, 'create_contact_form' ),
39 'permission_callback' => function () {
40 if ( current_user_can( 'wpcf7_edit_contact_forms' ) ) {
41 return true;
42 } else {
43 return new WP_Error( 'wpcf7_forbidden',
44 __( "You are not allowed to create a contact form.", 'contact-form-7' ),
45 array( 'status' => 403 )
46 );
47 }
48 },
49 ),
50 )
51 );
52
53 register_rest_route( self::route_namespace,
54 '/contact-forms/(?P<id>\d+)',
55 array(
56 array(
57 'methods' => WP_REST_Server::READABLE,
58 'callback' => array( $this, 'get_contact_form' ),
59 'permission_callback' => function ( WP_REST_Request $request ) {
60 $id = (int) $request->get_param( 'id' );
61
62 if ( current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
63 return true;
64 } else {
65 return new WP_Error( 'wpcf7_forbidden',
66 __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
67 array( 'status' => 403 )
68 );
69 }
70 },
71 ),
72 array(
73 'methods' => WP_REST_Server::EDITABLE,
74 'callback' => array( $this, 'update_contact_form' ),
75 'permission_callback' => function ( WP_REST_Request $request ) {
76 $id = (int) $request->get_param( 'id' );
77
78 if ( current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
79 return true;
80 } else {
81 return new WP_Error( 'wpcf7_forbidden',
82 __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
83 array( 'status' => 403 )
84 );
85 }
86 },
87 ),
88 array(
89 'methods' => WP_REST_Server::DELETABLE,
90 'callback' => array( $this, 'delete_contact_form' ),
91 'permission_callback' => function ( WP_REST_Request $request ) {
92 $id = (int) $request->get_param( 'id' );
93
94 if ( current_user_can( 'wpcf7_delete_contact_form', $id ) ) {
95 return true;
96 } else {
97 return new WP_Error( 'wpcf7_forbidden',
98 __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
99 array( 'status' => 403 )
100 );
101 }
102 },
103 ),
104 )
105 );
106
107 register_rest_route( self::route_namespace,
108 '/contact-forms/(?P<id>\d+)/feedback',
109 array(
110 array(
111 'methods' => WP_REST_Server::CREATABLE,
112 'callback' => array( $this, 'create_feedback' ),
113 'permission_callback' => '__return_true',
114 ),
115 )
116 );
117
118 register_rest_route( self::route_namespace,
119 '/contact-forms/(?P<id>\d+)/feedback/schema',
120 array(
121 array(
122 'methods' => WP_REST_Server::READABLE,
123 'callback' => array( $this, 'get_schema' ),
124 'permission_callback' => '__return_true',
125 ),
126 'schema' => 'wpcf7_swv_get_meta_schema',
127 )
128 );
129
130 register_rest_route( self::route_namespace,
131 '/contact-forms/(?P<id>\d+)/refill',
132 array(
133 array(
134 'methods' => WP_REST_Server::READABLE,
135 'callback' => array( $this, 'get_refill' ),
136 'permission_callback' => '__return_true',
137 ),
138 )
139 );
140 }
141
142 public function get_contact_forms( WP_REST_Request $request ) {
143 $args = array();
144
145 $per_page = $request->get_param( 'per_page' );
146
147 if ( null !== $per_page ) {
148 $args['posts_per_page'] = (int) $per_page;
149 }
150
151 $offset = $request->get_param( 'offset' );
152
153 if ( null !== $offset ) {
154 $args['offset'] = (int) $offset;
155 }
156
157 $order = $request->get_param( 'order' );
158
159 if ( null !== $order ) {
160 $args['order'] = (string) $order;
161 }
162
163 $orderby = $request->get_param( 'orderby' );
164
165 if ( null !== $orderby ) {
166 $args['orderby'] = (string) $orderby;
167 }
168
169 $search = $request->get_param( 'search' );
170
171 if ( null !== $search ) {
172 $args['s'] = (string) $search;
173 }
174
175 $items = WPCF7_ContactForm::find( $args );
176
177 $response = array();
178
179 foreach ( $items as $item ) {
180 $response[] = array(
181 'id' => $item->id(),
182 'slug' => $item->name(),
183 'title' => $item->title(),
184 'locale' => $item->locale(),
185 );
186 }
187
188 return rest_ensure_response( $response );
189 }
190
191 public function create_contact_form( WP_REST_Request $request ) {
192 $id = (int) $request->get_param( 'id' );
193
194 if ( $id ) {
195 return new WP_Error( 'wpcf7_post_exists',
196 __( "Cannot create existing contact form.", 'contact-form-7' ),
197 array( 'status' => 400 )
198 );
199 }
200
201 $args = $request->get_params();
202 $args['id'] = -1; // Create
203 $context = $request->get_param( 'context' );
204 $item = wpcf7_save_contact_form( $args, $context );
205
206 if ( ! $item ) {
207 return new WP_Error( 'wpcf7_cannot_save',
208 __( "There was an error saving the contact form.", 'contact-form-7' ),
209 array( 'status' => 500 )
210 );
211 }
212
213 $response = array(
214 'id' => $item->id(),
215 'slug' => $item->name(),
216 'title' => $item->title(),
217 'locale' => $item->locale(),
218 'properties' => $this->get_properties( $item ),
219 'config_errors' => array(),
220 );
221
222 if ( wpcf7_validate_configuration() ) {
223 $config_validator = new WPCF7_ConfigValidator( $item );
224 $config_validator->validate();
225
226 $response['config_errors'] = $config_validator->collect_error_messages();
227
228 if ( 'save' == $context ) {
229 $config_validator->save();
230 }
231 }
232
233 return rest_ensure_response( $response );
234 }
235
236 public function get_contact_form( WP_REST_Request $request ) {
237 $id = (int) $request->get_param( 'id' );
238 $item = wpcf7_contact_form( $id );
239
240 if ( ! $item ) {
241 return new WP_Error( 'wpcf7_not_found',
242 __( "The requested contact form was not found.", 'contact-form-7' ),
243 array( 'status' => 404 )
244 );
245 }
246
247 $response = array(
248 'id' => $item->id(),
249 'slug' => $item->name(),
250 'title' => $item->title(),
251 'locale' => $item->locale(),
252 'properties' => $this->get_properties( $item ),
253 );
254
255 return rest_ensure_response( $response );
256 }
257
258 public function update_contact_form( WP_REST_Request $request ) {
259 $id = (int) $request->get_param( 'id' );
260 $item = wpcf7_contact_form( $id );
261
262 if ( ! $item ) {
263 return new WP_Error( 'wpcf7_not_found',
264 __( "The requested contact form was not found.", 'contact-form-7' ),
265 array( 'status' => 404 )
266 );
267 }
268
269 $args = $request->get_params();
270 $context = $request->get_param( 'context' );
271 $item = wpcf7_save_contact_form( $args, $context );
272
273 if ( ! $item ) {
274 return new WP_Error( 'wpcf7_cannot_save',
275 __( "There was an error saving the contact form.", 'contact-form-7' ),
276 array( 'status' => 500 )
277 );
278 }
279
280 $response = array(
281 'id' => $item->id(),
282 'slug' => $item->name(),
283 'title' => $item->title(),
284 'locale' => $item->locale(),
285 'properties' => $this->get_properties( $item ),
286 'config_errors' => array(),
287 );
288
289 if ( wpcf7_validate_configuration() ) {
290 $config_validator = new WPCF7_ConfigValidator( $item );
291 $config_validator->validate();
292
293 $response['config_errors'] = $config_validator->collect_error_messages();
294
295 if ( 'save' == $context ) {
296 $config_validator->save();
297 }
298 }
299
300 return rest_ensure_response( $response );
301 }
302
303 public function delete_contact_form( WP_REST_Request $request ) {
304 $id = (int) $request->get_param( 'id' );
305 $item = wpcf7_contact_form( $id );
306
307 if ( ! $item ) {
308 return new WP_Error( 'wpcf7_not_found',
309 __( "The requested contact form was not found.", 'contact-form-7' ),
310 array( 'status' => 404 )
311 );
312 }
313
314 $result = $item->delete();
315
316 if ( ! $result ) {
317 return new WP_Error( 'wpcf7_cannot_delete',
318 __( "There was an error deleting the contact form.", 'contact-form-7' ),
319 array( 'status' => 500 )
320 );
321 }
322
323 $response = array( 'deleted' => true );
324
325 return rest_ensure_response( $response );
326 }
327
328 public function create_feedback( WP_REST_Request $request ) {
329 $content_type = $request->get_header( 'Content-Type' );
330
331 if ( ! str_starts_with( $content_type, 'multipart/form-data' ) ) {
332 return new WP_Error( 'wpcf7_unsupported_media_type',
333 __( "The request payload format is not supported.", 'contact-form-7' ),
334 array( 'status' => 415 )
335 );
336 }
337
338 $url_params = $request->get_url_params();
339
340 $item = null;
341
342 if ( ! empty( $url_params['id'] ) ) {
343 $item = wpcf7_contact_form( $url_params['id'] );
344 }
345
346 if ( ! $item ) {
347 return new WP_Error( 'wpcf7_not_found',
348 __( "The requested contact form was not found.", 'contact-form-7' ),
349 array( 'status' => 404 )
350 );
351 }
352
353 $unit_tag = wpcf7_sanitize_unit_tag(
354 $request->get_param( '_wpcf7_unit_tag' )
355 );
356
357 $result = $item->submit();
358
359 $response = array_merge( $result, array(
360 'into' => sprintf( '#%s', $unit_tag ),
361 'invalid_fields' => array(),
362 ) );
363
364 if ( ! empty( $result['invalid_fields'] ) ) {
365 $invalid_fields = array();
366
367 foreach ( (array) $result['invalid_fields'] as $name => $field ) {
368 if ( ! wpcf7_is_name( $name ) ) {
369 continue;
370 }
371
372 $name = strtr( $name, '.', '_' );
373
374 $invalid_fields[] = array(
375 'field' => $name,
376 'message' => $field['reason'],
377 'idref' => $field['idref'],
378 'error_id' => sprintf(
379 '%1$s-ve-%2$s',
380 $unit_tag,
381 $name
382 ),
383 );
384 }
385
386 $response['invalid_fields'] = $invalid_fields;
387 }
388
389 $response = wpcf7_apply_filters_deprecated(
390 'wpcf7_ajax_json_echo',
391 array( $response, $result ),
392 '5.2',
393 'wpcf7_feedback_response'
394 );
395
396 $response = apply_filters( 'wpcf7_feedback_response', $response, $result );
397
398 return rest_ensure_response( $response );
399 }
400
401
402 public function get_schema( WP_REST_Request $request ) {
403 $url_params = $request->get_url_params();
404
405 $item = null;
406
407 if ( ! empty( $url_params['id'] ) ) {
408 $item = wpcf7_contact_form( $url_params['id'] );
409 }
410
411 if ( ! $item ) {
412 return new WP_Error( 'wpcf7_not_found',
413 __( "The requested contact form was not found.", 'contact-form-7' ),
414 array( 'status' => 404 )
415 );
416 }
417
418 $schema = $item->get_schema();
419
420 $response = isset( $schema ) ? $schema->to_array() : array();
421
422 return rest_ensure_response( $response );
423 }
424
425
426 public function get_refill( WP_REST_Request $request ) {
427 $id = (int) $request->get_param( 'id' );
428 $item = wpcf7_contact_form( $id );
429
430 if ( ! $item ) {
431 return new WP_Error( 'wpcf7_not_found',
432 __( "The requested contact form was not found.", 'contact-form-7' ),
433 array( 'status' => 404 )
434 );
435 }
436
437 $response = wpcf7_apply_filters_deprecated(
438 'wpcf7_ajax_onload',
439 array( array() ),
440 '5.2',
441 'wpcf7_refill_response'
442 );
443
444 $response = apply_filters( 'wpcf7_refill_response', array() );
445
446 return rest_ensure_response( $response );
447 }
448
449 private function get_properties( WPCF7_ContactForm $contact_form ) {
450 $properties = $contact_form->get_properties();
451
452 $properties['form'] = array(
453 'content' => (string) $properties['form'],
454 'fields' => array_map(
455 function ( WPCF7_FormTag $form_tag ) {
456 return array(
457 'type' => $form_tag->type,
458 'basetype' => $form_tag->basetype,
459 'name' => $form_tag->name,
460 'options' => $form_tag->options,
461 'raw_values' => $form_tag->raw_values,
462 'labels' => $form_tag->labels,
463 'values' => $form_tag->values,
464 'pipes' => $form_tag->pipes instanceof WPCF7_Pipes
465 ? $form_tag->pipes->to_array()
466 : $form_tag->pipes,
467 'content' => $form_tag->content,
468 );
469 },
470 $contact_form->scan_form_tags()
471 ),
472 );
473
474 $properties['additional_settings'] = array(
475 'content' => (string) $properties['additional_settings'],
476 'settings' => array_filter( array_map(
477 function ( $setting ) {
478 $pattern = '/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/';
479
480 if ( preg_match( $pattern, $setting, $matches ) ) {
481 $name = trim( $matches[1] );
482 $value = trim( $matches[2] );
483
484 if ( in_array( $value, array( 'on', 'true' ), true ) ) {
485 $value = true;
486 } elseif ( in_array( $value, array( 'off', 'false' ), true ) ) {
487 $value = false;
488 }
489
490 return array( $name, $value );
491 }
492
493 return false;
494 },
495 explode( "\n", $properties['additional_settings'] )
496 ) ),
497 );
498
499 return $properties;
500 }
501
502 private function get_argument_schema() {
503 return array(
504 'id' => array(
505 'description' => __( "Unique identifier for the contact form.", 'contact-form-7' ),
506 'type' => 'integer',
507 'required' => true,
508 ),
509 );
510 }
511
512 }
513