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