├── .github └── FUNDING.yml ├── .gitignore ├── assets ├── css │ └── admin-notices-style.css └── js │ └── admin-notices-script.js ├── contact-form-7-api.php ├── includes ├── class-cf7-api-admin.php ├── class.cf7-api.php ├── class.cf7-helpers.php └── class.qs-admin-notices.php ├── index.php └── readme.txt /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: kennym # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: kennymeyer 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .svn 2 | -------------------------------------------------------------------------------- /assets/css/admin-notices-style.css: -------------------------------------------------------------------------------- 1 | .cf7_row { 2 | clear: both; 3 | margin: 10px 0; 4 | } 5 | .cf7_row textarea{ 6 | width:100%; 7 | } 8 | 9 | .cf7-label-in { 10 | min-width: 80px; 11 | display: inline-block; 12 | } 13 | .xml_mailtag { 14 | margin:0 2px; 15 | } 16 | .debug-log-wrap{ 17 | display:none; 18 | } 19 | [data-qsindex]{ 20 | display: none; 21 | } 22 | -------------------------------------------------------------------------------- /assets/js/admin-notices-script.js: -------------------------------------------------------------------------------- 1 | jQuery(document).ready(function(){ 2 | //dismiss admin notice forever 3 | qs_cf7_dismiss_admin_note(); 4 | 5 | check_input_type(); 6 | 7 | toggle_debug_log(); 8 | 9 | toggle_request_type(); 10 | 11 | }); 12 | 13 | function toggle_debug_log(){ 14 | jQuery( document ).on( 'click' , '.debug-log-trigger' , function(){ 15 | jQuery( '.debug-log-wrap' ).slideToggle(); 16 | }); 17 | } 18 | function toggle_request_type(){ 19 | jQuery( document ).on( 'change' , '#wpcf7-sf-input_type', function(){ 20 | check_input_type(); 21 | }); 22 | } 23 | // Check input type on API Integration TAB 24 | function check_input_type(){ 25 | if( jQuery( '#wpcf7-sf-input_type' ).length ){ 26 | var input_type = jQuery( '#wpcf7-sf-input_type' ).val(); 27 | 28 | jQuery( '[data-qsindex]').fadeOut(); 29 | 30 | jQuery( '[data-qsindex*="'+input_type+'"]' ).fadeIn(); 31 | } 32 | } 33 | 34 | function qs_cf7_dismiss_admin_note(){ 35 | jQuery(".qs-cf7-api-dismiss-notice-forever").click(function(){ 36 | 37 | var id = jQuery( this ).attr( 'id' ); 38 | 39 | jQuery.ajax({ 40 | type: "post", 41 | url: ajaxurl, 42 | data: { 43 | action: 'qs_cf7_api_admin_dismiss_notices', 44 | id : id 45 | }, 46 | 47 | }); 48 | }); 49 | } 50 | -------------------------------------------------------------------------------- /contact-form-7-api.php: -------------------------------------------------------------------------------- 1 | version = '1.1.1'; 78 | $qs_cf7_api->plugin_basename = plugin_basename( __FILE__ ); 79 | 80 | $qs_cf7_api->init(); 81 | 82 | } 83 | 84 | qs_init_cf7_api(); 85 | -------------------------------------------------------------------------------- /includes/class-cf7-api-admin.php: -------------------------------------------------------------------------------- 1 | textdomain = 'qs-cf7-api'; 37 | 38 | $this->admin_notices = new QS_Admin_notices(); 39 | 40 | $this->api_errors = array(); 41 | 42 | $this->register_hooks(); 43 | 44 | } 45 | /** 46 | * Check if contact form 7 is active 47 | * @return [type] [description] 48 | */ 49 | public function verify_dependencies(){ 50 | if( ! is_plugin_active('contact-form-7/wp-contact-form-7.php') ){ 51 | $notice = array( 52 | 'id' => 'cf7-not-active', 53 | 'type' => 'warning', 54 | 'notice' => __( 'Contact form 7 api integrations requires CONTACT FORM 7 Plugin to be installed and active' ,$this->textdomain ), 55 | 'dismissable_forever' => false 56 | ); 57 | 58 | $this->admin_notices->wp_add_notice( $notice ); 59 | } 60 | } 61 | /** 62 | * Registers the required admin hooks 63 | * @return [type] [description] 64 | */ 65 | public function register_hooks(){ 66 | /** 67 | * Check if required plugins are active 68 | * @var [type] 69 | */ 70 | add_action( 'admin_init', array( $this, 'verify_dependencies' ) ); 71 | 72 | /*before sending email to user actions */ 73 | add_action( 'wpcf7_mail_sent', array( $this , 'qs_cf7_send_data_to_api' )); 74 | 75 | /* adds another tab to contact form 7 screen */ 76 | add_filter( "wpcf7_editor_panels" ,array( $this , "add_integrations_tab" ) , 1 , 1 ); 77 | 78 | /* actions to handle while saving the form */ 79 | add_action( "wpcf7_save_contact_form" ,array( $this , "qs_save_contact_form_details") , 10 , 1 ); 80 | 81 | add_filter( "wpcf7_contact_form_properties" ,array( $this , "add_sf_properties" ) , 10 , 2 ); 82 | add_filter( "wpcf7_pre_construct_contact_form_properties" ,array( $this , "add_sf_properties" ) , 10 , 2 ); 83 | } 84 | 85 | /** 86 | * Sets the form additional properties 87 | * @param [type] $properties [description] 88 | * @param [type] $contact_form [description] 89 | */ 90 | function add_sf_properties( $properties , $contact_form ){ 91 | 92 | //add mail tags to allowed properties 93 | $properties["wpcf7_api_data"] = isset($properties["wpcf7_api_data"]) ? $properties["wpcf7_api_data"] : array(); 94 | $properties["wpcf7_api_data_map"] = isset($properties["wpcf7_api_data_map"]) ? $properties["wpcf7_api_data_map"] : array(); 95 | $properties["template"] = isset($properties["template"]) ? $properties["template"] : ''; 96 | $properties["json_template"] = isset($properties["json_template"]) ? $properties["json_template"] : ''; 97 | 98 | return $properties; 99 | } 100 | 101 | /** 102 | * Adds a new tab on conract form 7 screen 103 | * @param [type] $panels [description] 104 | */ 105 | function add_integrations_tab($panels){ 106 | 107 | $integration_panel = array( 108 | 'title' => __( 'API Integration' , $this->textdomain ), 109 | 'callback' => array( $this, 'wpcf7_integrations' ) 110 | ); 111 | 112 | $panels["qs-cf7-api-integration"] = $integration_panel; 113 | 114 | return $panels; 115 | 116 | } 117 | /** 118 | * Collect the mail tags from the form 119 | * @return [type] [description] 120 | */ 121 | function get_mail_tags( $post ){ 122 | $tags = apply_filters( 'qs_cf7_collect_mail_tags' , $post->scan_form_tags() ); 123 | 124 | foreach ( (array) $tags as $tag ) { 125 | $type = trim( $tag['type'], ' *' ); 126 | if ( empty( $type ) || empty( $tag['name'] ) ) { 127 | continue; 128 | } elseif ( ! empty( $args['include'] ) ) { 129 | if ( ! in_array( $type, $args['include'] ) ) { 130 | continue; 131 | } 132 | } elseif ( ! empty( $args['exclude'] ) ) { 133 | if ( in_array( $type, $args['exclude'] ) ) { 134 | continue; 135 | } 136 | } 137 | $mailtags[] = $tag; 138 | } 139 | 140 | return $mailtags; 141 | } 142 | /** 143 | * The admin tab display, settings and instructions to the admin user 144 | * @param [type] $post [description] 145 | * @return [type] [description] 146 | */ 147 | function wpcf7_integrations( $post ) { 148 | 149 | $wpcf7_api_data = $post->prop( 'wpcf7_api_data' ); 150 | $wpcf7_api_data_map = $post->prop( 'wpcf7_api_data_map' ); 151 | $wpcf7_api_data_template = $post->prop( 'template' ); 152 | $wpcf7_api_json_data_template = $post->prop( 'json_template' ); 153 | $mail_tags = $this->get_mail_tags( $post ); 154 | 155 | $wpcf7_api_data["base_url"] = isset( $wpcf7_api_data["base_url"] ) ? $wpcf7_api_data["base_url"] : ''; 156 | $wpcf7_api_data["basic_auth"] = isset( $wpcf7_api_data["basic_auth"] ) ? $wpcf7_api_data["basic_auth"] : ''; 157 | $wpcf7_api_data["bearer_auth"] = isset( $wpcf7_api_data["bearer_auth"] ) ? $wpcf7_api_data["bearer_auth"] : ''; 158 | $wpcf7_api_data["send_to_api"] = isset( $wpcf7_api_data["send_to_api"] ) ? $wpcf7_api_data["send_to_api"] : ''; 159 | $wpcf7_api_data["override_message"] = isset( $wpcf7_api_data["override_message"] ) ? $wpcf7_api_data["override_message"] : ''; 160 | $wpcf7_api_data["input_type"] = isset( $wpcf7_api_data["input_type"] ) ? $wpcf7_api_data["input_type"] : 'params'; 161 | $wpcf7_api_data["method"] = isset( $wpcf7_api_data["method"] ) ? $wpcf7_api_data["method"] : 'GET'; 162 | $wpcf7_api_data["debug_log"] = true; 163 | 164 | $debug_url = get_post_meta( $post->id() , 'qs_cf7_api_debug_url' , true ); 165 | $debug_result = get_post_meta( $post->id() , 'qs_cf7_api_debug_result' , true ); 166 | $debug_params = get_post_meta( $post->id() , 'qs_cf7_api_debug_params' , true ); 167 | 168 | $error_logs = get_post_meta( $post->id() , 'api_errors' , true ); 169 | $xml_placeholder = __('*** THIS IS AN EXAMPLE ** USE YOUR XML ACCORDING TO YOUR API DOCUMENTATION ** 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | ' , ''); 181 | 182 | $json_placeholder = __('*** THIS IS AN EXAMPLE ** USE YOUR JSON ACCORDING TO YOUR API DOCUMENTATION ** 183 | { "name":"[fullname]", "age":30, "car":null } 184 | ' , ''); 185 | ?> 186 | 187 | 188 |

textdomain ) ); ?>

189 | 190 |
191 | 192 | 193 |
194 | 195 | 199 | 200 |
201 | 202 |
203 | 204 | 208 | 209 |
210 | 211 | 212 |
213 | 217 |
218 | 219 |
220 | 221 |
222 | 227 |
228 | 229 |

textdomain );?>

230 | 231 |
232 | 237 |
238 | 239 |
240 | 241 |
242 | 256 |
257 | 258 |
259 | 266 |
267 | 268 | 269 | 270 |
271 | 272 | 273 |
274 |
275 |

textdomain ) ); ?>

276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | type == 'checkbox' ):?> 286 | values as $checkbox_row ):?> 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 |
textdomain );?>textdomain );?>
name;?> ()" />
name;?>" />
302 | 303 |
304 |
305 | 306 |
307 |
308 |

textdomain ) ); ?>

309 | 310 | 311 | 312 | [name;?>] 313 | 314 | 315 | 316 | 317 |
318 |
319 | 320 |
321 |
322 |

textdomain ) ); ?>

323 | 324 | 325 | 326 | type == 'checkbox'):?> 327 | values as $checkbox_row ):?> 328 | [name;?>-] 329 | 330 | 331 | [name;?>] 332 | 333 | 334 | 335 | 336 | 337 |
338 |
339 | 340 | 341 |
342 |
343 | 346 |
347 |

textdomain );?>

348 |
349 |

textdomain );?>:

350 | 351 |

textdomain );?>:

352 | 353 |

textdomain );?>:

354 | 355 |

textdomain );?>:

356 | 357 |
358 |
359 |
360 |
361 | get_properties(); 373 | 374 | $properties['wpcf7_api_data'] = isset( $_POST["wpcf7-sf"] ) ? $_POST["wpcf7-sf"] : ''; 375 | $properties['wpcf7_api_data_map'] = isset( $_POST["qs_wpcf7_api_map"] ) ? $_POST["qs_wpcf7_api_map"] : ''; 376 | $properties['template'] = isset( $_POST["template"] ) ? $_POST["template"] : ''; 377 | $properties['json_template'] = isset( $_POST["json_template"] ) ? $_POST["json_template"] : ''; 378 | 379 | //extract custom placeholders 380 | $record_type = isset( $properties['wpcf7_api_data']['input_type'] ) ? $properties['wpcf7_api_data']['input_type'] : 'params'; 381 | if( $record_type == 'json' || $record_type == 'xml' ){ 382 | $template = $record_type == 'json' ? $properties['json_template'] : $properties['template']; 383 | preg_match_all("/\[(\w+(-\d+)?)\]/", $template, $matches, PREG_PATTERN_ORDER); 384 | $properties['wpcf7_api_data_map'] = array_merge(array_fill_keys($matches[1], ''), $properties['wpcf7_api_data_map']); 385 | } 386 | 387 | $contact_form->set_properties( $properties ); 388 | 389 | } 390 | 391 | /** 392 | * The handler that will send the data to the api 393 | * @param [type] $WPCF7_ContactForm [description] 394 | * @return [type] [description] 395 | */ 396 | public function qs_cf7_send_data_to_api( $WPCF7_ContactForm ) { 397 | 398 | $this->clear_error_log( $WPCF7_ContactForm->id() ); 399 | 400 | $submission = WPCF7_Submission::get_instance(); 401 | 402 | $url = $submission->get_meta( 'url' ); 403 | $this->post = $WPCF7_ContactForm; 404 | $qs_cf7_data = $WPCF7_ContactForm->prop( 'wpcf7_api_data' ); 405 | $qs_cf7_data_map = $WPCF7_ContactForm->prop( 'wpcf7_api_data_map' ); 406 | $qs_cf7_data_template = $WPCF7_ContactForm->prop( 'template' ); 407 | $qs_cf7_data_json_template = $WPCF7_ContactForm->prop( 'json_template' ); 408 | $qs_cf7_data['debug_log'] = true; //always save last call results for debugging 409 | 410 | 411 | /* check if the form is marked to be sent via API */ 412 | if( isset( $qs_cf7_data["send_to_api"] ) && $qs_cf7_data["send_to_api"] == "on" ){ 413 | 414 | $record_type = isset( $qs_cf7_data['input_type'] ) ? $qs_cf7_data['input_type'] : 'params'; 415 | 416 | if( $record_type == 'json' ){ 417 | $qs_cf7_data_template = $qs_cf7_data_json_template; 418 | } 419 | $record = $this->get_record( $submission , $qs_cf7_data_map , $record_type, $template = $qs_cf7_data_template ); 420 | 421 | $record["url"] = $qs_cf7_data["base_url"]; 422 | 423 | if( isset( $record["url"] ) && $record["url"] ){ 424 | 425 | do_action( 'qs_cf7_api_before_sent_to_api' , $record ); 426 | 427 | $response = $this->send_lead( $record , $qs_cf7_data['debug_log'] , $qs_cf7_data['method'] , $record_type, $qs_cf7_data["basic_auth"], $qs_cf7_data["bearer_auth"] ); 428 | $override_message = isset($qs_cf7_data["override_message"]) && $qs_cf7_data["override_message"] == "on"; 429 | 430 | if( is_wp_error( $response ) ){ 431 | if ($override_message) { 432 | $submission->set_response($response->get_error_message()); 433 | } 434 | $this->log_error( $response , $WPCF7_ContactForm->id() ); 435 | }else{ 436 | if ($override_message) { 437 | $body_string = wp_remote_retrieve_body($response); 438 | 439 | $body = json_decode($body_string); 440 | if (json_last_error() == 0) { 441 | $submission->set_response($body->message); 442 | } 443 | else { 444 | $submission->set_response($body_string); 445 | } 446 | } 447 | 448 | do_action( 'qs_cf7_api_after_sent_to_api' , $record , $response ); 449 | } 450 | } 451 | } 452 | 453 | } 454 | /** 455 | * CREATE ERROR LOG FOR RECENT API TRANSMISSION ATTEMPT 456 | * @param [type] $wp_error [description] 457 | * @param [type] $post_id [description] 458 | * @return [type] [description] 459 | */ 460 | function log_error( $wp_error , $post_id ){ 461 | //error log 462 | $this->api_errors[] = $wp_error; 463 | 464 | update_post_meta( $post_id , 'api_errors' , $this->api_errors ); 465 | } 466 | 467 | function clear_error_log( $post_id ){ 468 | delete_post_meta( $post_id , 'api_errors' ); 469 | } 470 | /** 471 | * Convert the form keys to the API keys according to the mapping instructions 472 | * @param [type] $submission [description] 473 | * @param [type] $qs_cf7_data_map [description] 474 | * @return [type] [description] 475 | */ 476 | function get_record( $submission , $qs_cf7_data_map , $type = "params", $template = "" ){ 477 | 478 | $submited_data = $submission->get_posted_data(); 479 | $uploaded_files = $submission->uploaded_files(); 480 | $record = array(); 481 | 482 | if( $type == "params" ){ 483 | 484 | foreach( $qs_cf7_data_map as $form_key => $qs_cf7_form_key){ 485 | 486 | if( $qs_cf7_form_key ){ 487 | 488 | if( is_array( $qs_cf7_form_key ) ){ 489 | //arrange checkbox arrays 490 | foreach( $submited_data[$form_key] as $value ){ 491 | if( $value ){ 492 | $record["fields"][$qs_cf7_form_key[$value]] = apply_filters( 'set_record_value' , $value , $qs_cf7_form_key ); 493 | } 494 | } 495 | }else{ 496 | $value = isset($submited_data[$form_key]) ? $submited_data[$form_key] : ""; 497 | 498 | //flatten radio 499 | if( is_array( $value ) ){ 500 | $value = reset( $value ); 501 | } 502 | 503 | // handle file input 504 | if (isset($uploaded_files[$form_key])) { 505 | // Put all files into an array 506 | $value = []; 507 | foreach ($uploaded_files[$form_key] as $path) { 508 | $image_content = file_get_contents($path); 509 | $value[] = base64_encode($image_content); 510 | } 511 | } 512 | 513 | $record["fields"][$qs_cf7_form_key] = apply_filters( 'set_record_value' , $value , $qs_cf7_form_key ); 514 | } 515 | 516 | } 517 | 518 | } 519 | 520 | } elseif( $type == "xml" || $type == "json" ){ 521 | 522 | foreach( $qs_cf7_data_map as $form_key => $qs_cf7_form_key ){ 523 | 524 | if( is_array( $qs_cf7_form_key ) ){ 525 | //arrange checkbox arrays 526 | foreach( $submited_data[$form_key] as $value ){ 527 | if( $value ){ 528 | $value = apply_filters( 'set_record_value' , $value , $qs_cf7_form_key ); 529 | 530 | $template = str_replace( "[{$form_key}-{$value}]", $value, $template ); 531 | } 532 | } 533 | }else{ 534 | $value = isset($submited_data[$form_key]) ? $submited_data[$form_key] : ""; 535 | 536 | // handle line breaks (suggested by Felix Schäfer) 537 | $value = preg_replace('/\r|\n/', '\\n', $value); 538 | $value = str_replace('\\n\\n', '\n', $value); 539 | 540 | // flatten radio 541 | if( is_array( $value ) ){ 542 | if(count($value) == 1) 543 | $value = reset( $value ); 544 | else 545 | $value = implode(";",$value); 546 | } 547 | 548 | // handle boolean acceptance fields 549 | if( $this->isAcceptanceField($form_key) ) { 550 | $value = $value == "" ? "false" : "true"; 551 | } 552 | 553 | // handle file input 554 | if (isset($uploaded_files[$form_key])) { 555 | 556 | // Put all files into an array 557 | $value = []; 558 | foreach ($uploaded_files[$form_key] as $path) { 559 | $image_content = file_get_contents($path); 560 | $value[] = base64_encode($image_content); 561 | } 562 | 563 | // replace "[$form_key]" with json array of base64 strings 564 | $template = preg_replace( "/(\")?\[{$form_key}\](\")?/", json_encode($value), $template ); 565 | } 566 | 567 | $template = str_replace( "[{$form_key}]", $value, $template ); 568 | } 569 | } 570 | 571 | //clean unchanged tags 572 | foreach( $qs_cf7_data_map as $form_key => $qs_cf7_form_key ){ 573 | if( is_array( $qs_cf7_form_key ) ){ 574 | foreach( $qs_cf7_form_key as $field_suffix=> $api_name ){ 575 | $template = str_replace( "[{$form_key}-{$field_suffix}]", '', $template ); 576 | } 577 | } 578 | 579 | } 580 | 581 | $record["fields"] = $template; 582 | 583 | } 584 | 585 | $record = apply_filters( 'cf7api_create_record', $record , $submited_data , $qs_cf7_data_map , $type , $template ); 586 | 587 | return $record; 588 | } 589 | 590 | 591 | /** 592 | * Send the lead using wp_remote 593 | * @param [type] $record [description] 594 | * @param boolean $debug [description] 595 | * @param string $method [description] 596 | * @return [type] [description] 597 | */ 598 | 599 | private function send_lead( $record , $debug = false , $method = 'GET' , $record_type = 'params', $basic_auth = null, $bearer_auth = null ){ 600 | global $wp_version; 601 | 602 | $lead = $record["fields"]; 603 | $url = $record["url"]; 604 | 605 | if( $method == 'GET' && ( $record_type == 'params' || $record_type == 'json' ) ){ 606 | $args = array( 607 | 'timeout' => 5, 608 | 'redirection' => 5, 609 | 'httpversion' => '1.0', 610 | 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(), 611 | 'blocking' => true, 612 | 'headers' => array(), 613 | 'cookies' => array(), 614 | 'body' => null, 615 | 'compress' => false, 616 | 'decompress' => true, 617 | 'sslverify' => true, 618 | 'stream' => false, 619 | 'filename' => null 620 | ); 621 | 622 | 623 | if( $record_type == "json" ){ 624 | 625 | $args['headers']['Content-Type'] = 'application/json'; 626 | 627 | $json = $this->parse_json( $lead ); 628 | 629 | if( is_wp_error( $json ) ){ 630 | return $json; 631 | }else{ 632 | $args['body'] = $json; 633 | } 634 | 635 | }else{ 636 | $lead_string = http_build_query( $lead ); 637 | 638 | $url = strpos( '?' , $url ) ? $url.'&'.$lead_string : $url.'?'.$lead_string; 639 | } 640 | 641 | $args = apply_filters( 'qs_cf7_api_get_args' , $args ); 642 | 643 | $url = apply_filters( 'qs_cf7_api_get_url' , $url, $record ); 644 | 645 | $result = wp_remote_get( $url , $args ); 646 | 647 | }else{ 648 | $args = array( 649 | 'timeout' => 5, 650 | 'redirection' => 5, 651 | 'httpversion' => '1.0', 652 | 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(), 653 | 'blocking' => true, 654 | 'headers' => array(), 655 | 'cookies' => array(), 656 | 'body' => $lead, 657 | 'compress' => false, 658 | 'decompress' => true, 659 | 'sslverify' => true, 660 | 'stream' => false, 661 | 'filename' => null 662 | ); 663 | 664 | if ( isset($basic_auth) && $basic_auth !== '' ) { 665 | $args['headers']['Authorization'] = 'Basic ' . base64_encode( $basic_auth ); 666 | } 667 | 668 | if ( isset($bearer_auth) && $bearer_auth !== '' ) { 669 | $args['headers']['Authorization'] = 'Bearer ' . $bearer_auth; 670 | } 671 | 672 | if( $record_type == "xml" ){ 673 | 674 | $args['headers']['Content-Type'] = 'text/xml'; 675 | 676 | $xml = $this->get_xml( $lead ); 677 | 678 | if( is_wp_error( $xml ) ){ 679 | return $xml; 680 | } 681 | 682 | $args['body'] = $xml->asXML(); 683 | 684 | }elseif( $record_type == "json" ){ 685 | 686 | $args['headers']['Content-Type'] = 'application/json'; 687 | 688 | $json = $this->parse_json( $lead ); 689 | 690 | if( is_wp_error( $json ) ){ 691 | return $json; 692 | }else{ 693 | $args['body'] = $json; 694 | } 695 | 696 | } 697 | 698 | $args = apply_filters( 'qs_cf7_api_get_args' , $args ); 699 | 700 | $url = apply_filters( 'qs_cf7_api_post_url' , $url ); 701 | 702 | $result = wp_remote_post( $url , $args ); 703 | 704 | 705 | } 706 | 707 | if( $debug ){ 708 | update_post_meta( $this->post->id() , 'qs_cf7_api_debug_url' , $record["url"] ); 709 | update_post_meta( $this->post->id() , 'qs_cf7_api_debug_params' , $lead ); 710 | update_post_meta( $this->post->id() , 'qs_cf7_api_debug_result' , $result ); 711 | } 712 | 713 | do_action('after_qs_cf7_api_send_lead' , $result , $record ); 714 | 715 | return $result; 716 | } 717 | 718 | private function parse_json( $string ){ 719 | 720 | $json = json_decode( $string ); 721 | 722 | if ( json_last_error() === JSON_ERROR_NONE) { 723 | return json_encode( $json ); 724 | } 725 | 726 | if ( json_last_error() === 0) { 727 | return json_encode( $json ); 728 | } 729 | 730 | return new WP_Error( 'json-error' , json_last_error() ); 731 | 732 | } 733 | 734 | private function get_xml( $lead ){ 735 | $xml = ""; 736 | if( function_exists( 'simplexml_load_string' ) ){ 737 | libxml_use_internal_errors(true); 738 | 739 | $xml = simplexml_load_string( $lead ); 740 | 741 | if( $xml == false){ 742 | $xml = new WP_Error( 743 | 'xml', 744 | __( "XML Structure is incorrect" , $this->textdomain ) 745 | ); 746 | } 747 | 748 | } 749 | 750 | return $xml; 751 | } 752 | 753 | /** 754 | * @param string $field_name 755 | * @return bool 756 | */ 757 | private function isAcceptanceField($field_name) { 758 | $field = $this->post->scan_form_tags( 759 | array( 760 | 'type' => 'acceptance', 761 | 'name' => $field_name 762 | ) 763 | ); 764 | 765 | return count($field) == 1; 766 | } 767 | } 768 | -------------------------------------------------------------------------------- /includes/class.cf7-api.php: -------------------------------------------------------------------------------- 1 | plugin_name = 'wc-cf7-api-form'; 54 | 55 | 56 | $this->load_dependencies(); 57 | 58 | /** 59 | * Create an instance of the admin class 60 | * @var QS_CF7_api_admin 61 | */ 62 | $this->admin = new QS_CF7_api_admin(); 63 | $this->admin->plugin_name = $this->plugin_name; 64 | 65 | /** 66 | * save the instance for static actions 67 | * 68 | */ 69 | self::$instance = $this; 70 | 71 | } 72 | public function init(){ 73 | 74 | } 75 | /** 76 | * Loads the required plugin files 77 | * @return [type] [description] 78 | */ 79 | public function load_dependencies(){ 80 | /** 81 | * General global plugin functions 82 | */ 83 | require_once QS_CF7_API_INCLUDES_PATH . 'class.cf7-helpers.php'; 84 | /** 85 | * admin notices class 86 | */ 87 | require_once QS_CF7_API_INCLUDES_PATH . 'class.qs-admin-notices.php'; 88 | /** 89 | * admin notices clclass 90 | */ 91 | require_once QS_CF7_API_INCLUDES_PATH . 'class-cf7-api-admin.php'; 92 | } 93 | /** 94 | * Get the current plugin instance 95 | * @return [type] [description] 96 | */ 97 | public static function get_instance() { 98 | if (self::$instance === null) { 99 | self::$instance = new self(); 100 | } 101 | return self::$instance; 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /includes/class.cf7-helpers.php: -------------------------------------------------------------------------------- 1 | register_hooks(); 21 | 22 | $this->get_plugin_options(); 23 | 24 | } 25 | 26 | /** 27 | * Registers class filters and actions 28 | * @return [null] 29 | */ 30 | public function register_hooks(){ 31 | /** 32 | * display notices hook 33 | */ 34 | add_action( 'admin_notices' , array( $this , 'qs_admin_notices' ) ); 35 | /** 36 | * catch dismiss notice action and add it to the dismissed notices array 37 | */ 38 | add_action( 'wp_ajax_qs_cf7_api_admin_dismiss_notices' , array( $this , 'qs_admin_dismiss_notices' ) ); 39 | /** 40 | * enqueue admin scripts and styles 41 | */ 42 | add_action( 'admin_enqueue_scripts', array( $this , 'load_admin_scripts' ) ); 43 | } 44 | /** 45 | * load admin scripts and styles 46 | * @return [type] [description] 47 | */ 48 | public function load_admin_scripts(){ 49 | 50 | wp_register_style( 'qs-cf7-api-admin-notices-css', QS_CF7_API_ADMIN_CSS_URL . 'admin-notices-style.css' , false , '1.0.0' ); 51 | 52 | wp_enqueue_style( 'qs-cf7-api-admin-notices-css' ); 53 | 54 | wp_register_script( 'qs-cf7-api-admin-notices-script', QS_CF7_API_ADMIN_JS_URL . 'admin-notices-script.js' , array( 'jquery' ) , '1.0.0' , true ); 55 | 56 | wp_enqueue_script( 'qs-cf7-api-admin-notices-script' ); 57 | 58 | } 59 | /** 60 | * dismiss notice and save it to the plugin options 61 | * @return [type] [description] 62 | */ 63 | public function qs_admin_dismiss_notices(){ 64 | $id = isset( $_POST['id'] ) ? sanitize_text_field( $_POST['id'] ) : ''; 65 | 66 | if( $id ){ 67 | $this->notices_options['dismiss_notices'][$id] = true; 68 | 69 | $this->update_plugin_options(); 70 | } 71 | 72 | die('updated'); 73 | } 74 | /** 75 | * get the plugin admin options 76 | * @return [type] [description] 77 | */ 78 | private function get_plugin_options(){ 79 | 80 | $this->notices_options = apply_filters( 'get_plugin_options' , get_option( 'qs_cf7_api_notices_options' ) ); 81 | 82 | } 83 | 84 | /** 85 | * save the plugin admin options 86 | * @return [type] [description] 87 | */ 88 | private function update_plugin_options(){ 89 | 90 | update_option( 'qs_cf7_api_notices_options' , $this->notices_options ); 91 | 92 | } 93 | /** 94 | * display the notices that resides in the notices collection 95 | * @return [type] [description] 96 | */ 97 | public function qs_admin_notices(){ 98 | 99 | if( $this->notices ){ 100 | foreach( $this->notices as $admin_notice ){ 101 | /** 102 | * only disply the notice if it wasnt dismiised in the past 103 | */ 104 | $classes = array( 105 | "notice notice-{$admin_notice['type']}", 106 | "is-dismissible" 107 | ); 108 | 109 | $id = $admin_notice['id']; 110 | if( ! $admin_notice['dismissable_forever'] || (! isset( $this->notices_options['dismiss_notices'][$id] ) || ! $this->notices_options['dismiss_notices'][$id]) ){ 111 | if( $admin_notice['dismissable_forever'] ){ 112 | $classes[] = 'qs-cf7-api-dismiss-notice-forever'; 113 | } 114 | echo "
115 |

{$admin_notice['notice']}

116 |
"; 117 | } 118 | 119 | } 120 | } 121 | } 122 | 123 | /** 124 | * adds notices to the class notices collection 125 | * @param array $notice an array of notice message and notice type 126 | * Types available are "error" "warning" "success" "info" 127 | */ 128 | public function wp_add_notice( $notice = "" ){ 129 | 130 | if( $notice ){ 131 | $this->notices[] = array( 132 | 'id' => $notice['id'], 133 | 'notice' => $notice['notice'], 134 | 'type' => isset( $notice['type'] ) ? $notice['type'] : 'warning', 135 | 'dismissable_forever' => isset( $notice['dismissable_forever'] ) ? $notice['dismissable_forever'] : false 136 | ); 137 | } 138 | 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kennym/cf7-to-api/10d5e86156e3eb59b889c92e78f739dbc5c8eace/index.php -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | Contact form 7 TO API + Basic Auth 2 | ================================== 3 | 4 | MAINTAINER NEEDED. Please email me if you're interested. 5 | 6 | - Tags: contact form 7 to api,contact form 7,cf7 api,cf7 get,contact 7 | form 7 post,contact form7 get,contact form 7 remote, Contact form 7 8 | crm, contact form 7 integration,contact form 7 integrations, contact 9 | form 7 rest api, 10 | - Requires at least: 4.7.0 11 | - Tested up to: 5.2.2 12 | - Stable tag: 1.4.5 13 | - License: GPLv3 or later 14 | - License URI: 15 | 16 | An addon to transmit contact form 7 entries to remote API using POST or 17 | GET. 18 | 19 | Description 20 | ----------- 21 | 22 | Adds an option to send leads to remote API's such as CRM's ERP's and 23 | other remote systems using POST/GET. 24 | NOTE: This plugin requires Contact Form 7 version 4.2 or later. 25 | 26 | - Supports XML and JSON\ 27 | - Supports Basic Auth\ 28 | - Supports Bearer Auth 29 | 30 | Usage 31 | ----- 32 | 33 | Simply go to your form settings, choose the "Redirect Settings" tab and 34 | set your required parameters, 35 | 36 | 1. Chose wether the specific form will use the API integrations 37 | 2. Type the API url 38 | 3. Select the method (POST/GET) 39 | 4. map the form fields (Each field that you use on the form will be 40 | availble on this tab after saving the form) 41 | 5. choose wether you wish to debug and save the last transmited value. 42 | 43 | Installation 44 | ------------ 45 | 46 | Installing Contact form 7 TO API can be done either by searching for 47 | "Contact form 7 TO API" via the "Plugins \> Add New" screen in your 48 | WordPress dashboard, or by using the following steps: 49 | 50 | 1. Download the plugin via WordPress.org. 51 | 2. Upload the ZIP file through the "Plugins" \> Add New \> Upload 52 | screen in your WordPress dashboard. 53 | 3. Activate the plugin through the 'Plugins' menu in WordPress 54 | 4. Visit the settings screen and configure, as desired. 55 | 56 | Frequently Asked Questions 57 | -------------------------- 58 | 59 | = How can i redirect the user after success ? = 60 | You can use another plugin for that - Contact Form 7 Redirection 61 | 62 | 63 | 64 | ### How can i set Extra parameters? 65 | 66 | You could set hidden fields for that 67 | 68 | 69 | 70 | OR 71 | 72 | simply append the constant parameters to the url 73 | For example: 74 | 75 | 76 | 77 | Changelog 78 | --------- 79 | ### 1.4.11 80 | 81 | - Support to new CF7 version 82 | 83 | ### 1.4.10 84 | 85 | - Add ability to override message with api response body (@Logikgate) 86 | 87 | ### 1.4.9 88 | 89 | - Update flattening of acceptance fields to json boolean value 90 | - Add function to check if a field is an acceptance field 91 | - Convert array to csv (@NicoP-S) 92 | - support multiple file uploads (@NicoP-S) 93 | 94 | ### 1.4.8 95 | 96 | - Revert handle single quotes 97 | 98 | ### 1.4.7 99 | 100 | - Compatibility fixes 101 | 102 | ### 1.4.5 103 | 104 | - Handle line breaks 105 | 106 | ### 1.4.4 107 | 108 | - Extract custom placeholders from template 109 | 110 | ### 1.4.3 111 | 112 | - Fixed bearer token Usage 113 | - Update WooCommerce plugin name 114 | 115 | ### 1.4.2 116 | 117 | - Bug fix 118 | 119 | ### 1.4.1 120 | 121 | - Support for WP 5 122 | 123 | ### 1.3.0 124 | 125 | - Add Basic Auth option 126 | 127 | ### 1.2.0 128 | 129 | - Added better support for checkbox arrays and radio buttons 130 | - Added record filter to override record structure 131 | "cf7api\_create\_record" 132 | 133 | ### 1.1.1 134 | 135 | - Fix version number 136 | 137 | ### 1.1.0 138 | 139 | - Added send XML option. 140 | - Added send JSON option. 141 | - Added error log. 142 | - Debug log for each form 143 | - Debug log view changed 144 | - Debug log is now saved anyway 145 | 146 | ### 1.0.1 147 | 148 | - Fix code errors and notices. 149 | 150 | ### 1.0.0 151 | 152 | - Initial release. 153 | 154 | 155 | --------------------------------------------------------------------------------