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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 1 year ago config-validator 1 year ago css 2 years ago js 1 year ago swv 1 year ago capabilities.php 7 years ago contact-form-functions.php 1 year ago contact-form-template.php 1 year ago contact-form.php 1 year ago controller.php 1 year ago file.php 1 year ago form-tag.php 1 year ago form-tags-manager.php 1 year ago formatting.php 1 year ago functions.php 1 year ago html-formatter.php 1 year ago integration.php 1 year ago l10n.php 1 year ago mail-tag.php 2 years ago mail.php 1 year ago pipe.php 1 year ago pocket-holder.php 3 years ago rest-api.php 1 year ago shortcodes.php 1 year ago special-mail-tags.php 1 year ago submission.php 1 year ago upgrade.php 2 years ago validation-functions.php 1 year ago validation.php 1 year ago
rest-api.php
521 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 if ( empty( $unit_tag ) ) {
359 return new WP_Error( 'wpcf7_unit_tag_not_found',
360 __( "There is no valid unit tag.", 'contact-form-7' ),
361 array( 'status' => 400 )
362 );
363 }
364
365 $result = $item->submit();
366
367 $response = array_merge( $result, array(
368 'into' => sprintf( '#%s', $unit_tag ),
369 'invalid_fields' => array(),
370 ) );
371
372 if ( ! empty( $result['invalid_fields'] ) ) {
373 $invalid_fields = array();
374
375 foreach ( (array) $result['invalid_fields'] as $name => $field ) {
376 if ( ! wpcf7_is_name( $name ) ) {
377 continue;
378 }
379
380 $name = strtr( $name, '.', '_' );
381
382 $invalid_fields[] = array(
383 'field' => $name,
384 'message' => $field['reason'],
385 'idref' => $field['idref'],
386 'error_id' => sprintf(
387 '%1$s-ve-%2$s',
388 $unit_tag,
389 $name
390 ),
391 );
392 }
393
394 $response['invalid_fields'] = $invalid_fields;
395 }
396
397 $response = wpcf7_apply_filters_deprecated(
398 'wpcf7_ajax_json_echo',
399 array( $response, $result ),
400 '5.2',
401 'wpcf7_feedback_response'
402 );
403
404 $response = apply_filters( 'wpcf7_feedback_response', $response, $result );
405
406 return rest_ensure_response( $response );
407 }
408
409
410 public function get_schema( WP_REST_Request $request ) {
411 $url_params = $request->get_url_params();
412
413 $item = null;
414
415 if ( ! empty( $url_params['id'] ) ) {
416 $item = wpcf7_contact_form( $url_params['id'] );
417 }
418
419 if ( ! $item ) {
420 return new WP_Error( 'wpcf7_not_found',
421 __( "The requested contact form was not found.", 'contact-form-7' ),
422 array( 'status' => 404 )
423 );
424 }
425
426 $schema = $item->get_schema();
427
428 $response = isset( $schema ) ? $schema->to_array() : array();
429
430 return rest_ensure_response( $response );
431 }
432
433
434 public function get_refill( WP_REST_Request $request ) {
435 $id = (int) $request->get_param( 'id' );
436 $item = wpcf7_contact_form( $id );
437
438 if ( ! $item ) {
439 return new WP_Error( 'wpcf7_not_found',
440 __( "The requested contact form was not found.", 'contact-form-7' ),
441 array( 'status' => 404 )
442 );
443 }
444
445 $response = wpcf7_apply_filters_deprecated(
446 'wpcf7_ajax_onload',
447 array( array() ),
448 '5.2',
449 'wpcf7_refill_response'
450 );
451
452 $response = apply_filters( 'wpcf7_refill_response', array() );
453
454 return rest_ensure_response( $response );
455 }
456
457 private function get_properties( WPCF7_ContactForm $contact_form ) {
458 $properties = $contact_form->get_properties();
459
460 $properties['form'] = array(
461 'content' => (string) $properties['form'],
462 'fields' => array_map(
463 static function ( WPCF7_FormTag $form_tag ) {
464 return array(
465 'type' => $form_tag->type,
466 'basetype' => $form_tag->basetype,
467 'name' => $form_tag->name,
468 'options' => $form_tag->options,
469 'raw_values' => $form_tag->raw_values,
470 'labels' => $form_tag->labels,
471 'values' => $form_tag->values,
472 'pipes' => $form_tag->pipes instanceof WPCF7_Pipes
473 ? $form_tag->pipes->to_array()
474 : $form_tag->pipes,
475 'content' => $form_tag->content,
476 );
477 },
478 $contact_form->scan_form_tags()
479 ),
480 );
481
482 $properties['additional_settings'] = array(
483 'content' => (string) $properties['additional_settings'],
484 'settings' => array_filter( array_map(
485 static function ( $setting ) {
486 $pattern = '/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/';
487
488 if ( preg_match( $pattern, $setting, $matches ) ) {
489 $name = trim( $matches[1] );
490 $value = trim( $matches[2] );
491
492 if ( in_array( $value, array( 'on', 'true' ), true ) ) {
493 $value = true;
494 } elseif ( in_array( $value, array( 'off', 'false' ), true ) ) {
495 $value = false;
496 }
497
498 return array( $name, $value );
499 }
500
501 return false;
502 },
503 explode( "\n", $properties['additional_settings'] )
504 ) ),
505 );
506
507 return $properties;
508 }
509
510 private function get_argument_schema() {
511 return array(
512 'id' => array(
513 'description' => __( "Unique identifier for the contact form.", 'contact-form-7' ),
514 'type' => 'integer',
515 'required' => true,
516 ),
517 );
518 }
519
520 }
521