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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 2 years ago config-validator 2 years ago css 2 years ago js 2 years ago swv 2 years ago capabilities.php 7 years ago contact-form-functions.php 2 years ago contact-form-template.php 2 years ago contact-form.php 2 years ago controller.php 3 years ago file.php 3 years ago form-tag.php 2 years ago form-tags-manager.php 3 years ago formatting.php 2 years ago functions.php 2 years ago html-formatter.php 3 years ago integration.php 3 years ago l10n.php 3 years ago mail.php 2 years ago pipe.php 3 years ago pocket-holder.php 3 years ago rest-api.php 2 years ago shortcodes.php 3 years ago special-mail-tags.php 2 years ago submission.php 2 years ago upgrade.php 2 years ago validation-functions.php 2 years ago validation.php 3 years ago
rest-api.php
514 lines
1 <?php
2
3 add_action(
4 'rest_api_init',
5 static 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' => static 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' => static 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' => static 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' => static 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' => static 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 'hash' => $item->hash(),
183 'slug' => $item->name(),
184 'title' => $item->title(),
185 'locale' => $item->locale(),
186 );
187 }
188
189 return rest_ensure_response( $response );
190 }
191
192 public function create_contact_form( WP_REST_Request $request ) {
193 $id = (int) $request->get_param( 'id' );
194
195 if ( $id ) {
196 return new WP_Error( 'wpcf7_post_exists',
197 __( "Cannot create existing contact form.", 'contact-form-7' ),
198 array( 'status' => 400 )
199 );
200 }
201
202 $args = $request->get_params();
203 $args['id'] = -1; // Create
204 $context = $request->get_param( 'context' );
205 $item = wpcf7_save_contact_form( $args, $context );
206
207 if ( ! $item ) {
208 return new WP_Error( 'wpcf7_cannot_save',
209 __( "There was an error saving the contact form.", 'contact-form-7' ),
210 array( 'status' => 500 )
211 );
212 }
213
214 $response = array(
215 'id' => $item->id(),
216 'slug' => $item->name(),
217 'title' => $item->title(),
218 'locale' => $item->locale(),
219 'properties' => $this->get_properties( $item ),
220 'config_errors' => array(),
221 );
222
223 if ( wpcf7_validate_configuration() ) {
224 $config_validator = new WPCF7_ConfigValidator( $item );
225 $config_validator->validate();
226
227 $response['config_errors'] = $config_validator->collect_error_messages();
228
229 if ( 'save' == $context ) {
230 $config_validator->save();
231 }
232 }
233
234 return rest_ensure_response( $response );
235 }
236
237 public function get_contact_form( WP_REST_Request $request ) {
238 $id = (int) $request->get_param( 'id' );
239 $item = wpcf7_contact_form( $id );
240
241 if ( ! $item ) {
242 return new WP_Error( 'wpcf7_not_found',
243 __( "The requested contact form was not found.", 'contact-form-7' ),
244 array( 'status' => 404 )
245 );
246 }
247
248 $response = array(
249 'id' => $item->id(),
250 'slug' => $item->name(),
251 'title' => $item->title(),
252 'locale' => $item->locale(),
253 'properties' => $this->get_properties( $item ),
254 );
255
256 return rest_ensure_response( $response );
257 }
258
259 public function update_contact_form( WP_REST_Request $request ) {
260 $id = (int) $request->get_param( 'id' );
261 $item = wpcf7_contact_form( $id );
262
263 if ( ! $item ) {
264 return new WP_Error( 'wpcf7_not_found',
265 __( "The requested contact form was not found.", 'contact-form-7' ),
266 array( 'status' => 404 )
267 );
268 }
269
270 $args = $request->get_params();
271 $context = $request->get_param( 'context' );
272 $item = wpcf7_save_contact_form( $args, $context );
273
274 if ( ! $item ) {
275 return new WP_Error( 'wpcf7_cannot_save',
276 __( "There was an error saving the contact form.", 'contact-form-7' ),
277 array( 'status' => 500 )
278 );
279 }
280
281 $response = array(
282 'id' => $item->id(),
283 'slug' => $item->name(),
284 'title' => $item->title(),
285 'locale' => $item->locale(),
286 'properties' => $this->get_properties( $item ),
287 'config_errors' => array(),
288 );
289
290 if ( wpcf7_validate_configuration() ) {
291 $config_validator = new WPCF7_ConfigValidator( $item );
292 $config_validator->validate();
293
294 $response['config_errors'] = $config_validator->collect_error_messages();
295
296 if ( 'save' == $context ) {
297 $config_validator->save();
298 }
299 }
300
301 return rest_ensure_response( $response );
302 }
303
304 public function delete_contact_form( WP_REST_Request $request ) {
305 $id = (int) $request->get_param( 'id' );
306 $item = wpcf7_contact_form( $id );
307
308 if ( ! $item ) {
309 return new WP_Error( 'wpcf7_not_found',
310 __( "The requested contact form was not found.", 'contact-form-7' ),
311 array( 'status' => 404 )
312 );
313 }
314
315 $result = $item->delete();
316
317 if ( ! $result ) {
318 return new WP_Error( 'wpcf7_cannot_delete',
319 __( "There was an error deleting the contact form.", 'contact-form-7' ),
320 array( 'status' => 500 )
321 );
322 }
323
324 $response = array( 'deleted' => true );
325
326 return rest_ensure_response( $response );
327 }
328
329 public function create_feedback( WP_REST_Request $request ) {
330 $content_type = $request->get_header( 'Content-Type' );
331
332 if ( ! str_starts_with( $content_type, 'multipart/form-data' ) ) {
333 return new WP_Error( 'wpcf7_unsupported_media_type',
334 __( "The request payload format is not supported.", 'contact-form-7' ),
335 array( 'status' => 415 )
336 );
337 }
338
339 $url_params = $request->get_url_params();
340
341 $item = null;
342
343 if ( ! empty( $url_params['id'] ) ) {
344 $item = wpcf7_contact_form( $url_params['id'] );
345 }
346
347 if ( ! $item ) {
348 return new WP_Error( 'wpcf7_not_found',
349 __( "The requested contact form was not found.", 'contact-form-7' ),
350 array( 'status' => 404 )
351 );
352 }
353
354 $unit_tag = wpcf7_sanitize_unit_tag(
355 $request->get_param( '_wpcf7_unit_tag' )
356 );
357
358 $result = $item->submit();
359
360 $response = array_merge( $result, array(
361 'into' => sprintf( '#%s', $unit_tag ),
362 'invalid_fields' => array(),
363 ) );
364
365 if ( ! empty( $result['invalid_fields'] ) ) {
366 $invalid_fields = array();
367
368 foreach ( (array) $result['invalid_fields'] as $name => $field ) {
369 if ( ! wpcf7_is_name( $name ) ) {
370 continue;
371 }
372
373 $name = strtr( $name, '.', '_' );
374
375 $invalid_fields[] = array(
376 'field' => $name,
377 'message' => $field['reason'],
378 'idref' => $field['idref'],
379 'error_id' => sprintf(
380 '%1$s-ve-%2$s',
381 $unit_tag,
382 $name
383 ),
384 );
385 }
386
387 $response['invalid_fields'] = $invalid_fields;
388 }
389
390 $response = wpcf7_apply_filters_deprecated(
391 'wpcf7_ajax_json_echo',
392 array( $response, $result ),
393 '5.2',
394 'wpcf7_feedback_response'
395 );
396
397 $response = apply_filters( 'wpcf7_feedback_response', $response, $result );
398
399 return rest_ensure_response( $response );
400 }
401
402
403 public function get_schema( WP_REST_Request $request ) {
404 $url_params = $request->get_url_params();
405
406 $item = null;
407
408 if ( ! empty( $url_params['id'] ) ) {
409 $item = wpcf7_contact_form( $url_params['id'] );
410 }
411
412 if ( ! $item ) {
413 return new WP_Error( 'wpcf7_not_found',
414 __( "The requested contact form was not found.", 'contact-form-7' ),
415 array( 'status' => 404 )
416 );
417 }
418
419 $schema = $item->get_schema();
420
421 $response = isset( $schema ) ? $schema->to_array() : array();
422
423 return rest_ensure_response( $response );
424 }
425
426
427 public function get_refill( WP_REST_Request $request ) {
428 $id = (int) $request->get_param( 'id' );
429 $item = wpcf7_contact_form( $id );
430
431 if ( ! $item ) {
432 return new WP_Error( 'wpcf7_not_found',
433 __( "The requested contact form was not found.", 'contact-form-7' ),
434 array( 'status' => 404 )
435 );
436 }
437
438 $response = wpcf7_apply_filters_deprecated(
439 'wpcf7_ajax_onload',
440 array( array() ),
441 '5.2',
442 'wpcf7_refill_response'
443 );
444
445 $response = apply_filters( 'wpcf7_refill_response', array() );
446
447 return rest_ensure_response( $response );
448 }
449
450 private function get_properties( WPCF7_ContactForm $contact_form ) {
451 $properties = $contact_form->get_properties();
452
453 $properties['form'] = array(
454 'content' => (string) $properties['form'],
455 'fields' => array_map(
456 static function ( WPCF7_FormTag $form_tag ) {
457 return array(
458 'type' => $form_tag->type,
459 'basetype' => $form_tag->basetype,
460 'name' => $form_tag->name,
461 'options' => $form_tag->options,
462 'raw_values' => $form_tag->raw_values,
463 'labels' => $form_tag->labels,
464 'values' => $form_tag->values,
465 'pipes' => $form_tag->pipes instanceof WPCF7_Pipes
466 ? $form_tag->pipes->to_array()
467 : $form_tag->pipes,
468 'content' => $form_tag->content,
469 );
470 },
471 $contact_form->scan_form_tags()
472 ),
473 );
474
475 $properties['additional_settings'] = array(
476 'content' => (string) $properties['additional_settings'],
477 'settings' => array_filter( array_map(
478 static function ( $setting ) {
479 $pattern = '/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/';
480
481 if ( preg_match( $pattern, $setting, $matches ) ) {
482 $name = trim( $matches[1] );
483 $value = trim( $matches[2] );
484
485 if ( in_array( $value, array( 'on', 'true' ), true ) ) {
486 $value = true;
487 } elseif ( in_array( $value, array( 'off', 'false' ), true ) ) {
488 $value = false;
489 }
490
491 return array( $name, $value );
492 }
493
494 return false;
495 },
496 explode( "\n", $properties['additional_settings'] )
497 ) ),
498 );
499
500 return $properties;
501 }
502
503 private function get_argument_schema() {
504 return array(
505 'id' => array(
506 'description' => __( "Unique identifier for the contact form.", 'contact-form-7' ),
507 'type' => 'integer',
508 'required' => true,
509 ),
510 );
511 }
512
513 }
514