Skip to content

Instantly share code, notes, and snippets.

@lucas-pelton
Created October 6, 2017 22:11
Show Gist options
  • Save lucas-pelton/03942edd0e3c65920d9a82a275016eb5 to your computer and use it in GitHub Desktop.
Save lucas-pelton/03942edd0e3c65920d9a82a275016eb5 to your computer and use it in GitHub Desktop.
Grab CF7 submission on the way by and send to MailJet via API
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @link https://lucasbalzer.com
* @since 1.0.0
* @package Wp_Loaner_Car
*
* @wordpress-plugin
* Plugin Name: Loaner Car Checkout
* Plugin URI: https://lucasbalzer.com
* Description: This is a short description of what the plugin does. It's displayed in the WordPress admin area.
* Version: 1.0.0
* Author: Lucas Balzer
* Author URI: https://lucasbalzer.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: loaner-car
* Domain Path: /languages
*/
function wplc_register_scripts() {
wp_register_script( 'wplc-script', plugin_dir_url( __FILE__ ) . 'js/wp-loaner-car.js' , '3', true );
}
add_action( 'wp_enqueue_scripts', 'wplc_register_scripts' );
function wplc_shortcode_handler( $atts ) {
$a = shortcode_atts( array(
'id' => ''
), $atts );
$id = $a['id'];
wp_enqueue_script( 'wplc-script' );
echo <<<END
<style>
.js-signature {position:relative;}
.js-signature .clear-sig {
position: absolute;
top: 10px;
right: 10px;
color: #999;
border: 1px dotted #ccc;
padding: 0 5px;
box-shadow: 0 0 19px -4px #eee;
}
/* Make Contact Form 7 Select Dropdown field responsive & full width
.wpcf7-styled select {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 3px;
margin: 0;
padding: 4px;
text-indent: 0px;
width: 100%;
z-index: 100;
} */
/* Make Contact Form 7 text field full width & responsive
.wpcf7-styled .wpcf7-text {
width: 100%;
}
.wpcf7-styled label {
font-size: 2.5em;
margin-bottom: 25px;
display: block;
line-height: 1em;
}
.wpcf7-styled input[type="checkbox"] {
zoom: 200%;
top: 4px;
position: relative;
}
.wpcf7-styled .wpcf7-submit {
margin: 40px auto;
line-height: 1em;
font-size: 2.5em;
}
.wpcf7-styled label:not(.plain) > span {
display:block;
padding-top:10px;
}
*/
</style>
END;
}
add_shortcode( 'loaner-car', 'wplc_shortcode_handler' );
add_filter('wpcf7_skip_mail','my_skip_mail');
function my_skip_mail($f){
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
if (array_key_exists ( 'skip-email' , $data )) {
return true; // DO NOT SEND E-MAIL
}
}
add_action( 'wpcf7_before_send_mail', 'ping_webhook' );
function ping_webhook( $contact_form ) {
// Get the post data and other post meta values
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
if (!array_key_exists ( 'process-webhook' , $data )) {return;}
$mj_data = '{
"Messages":[
{
"From":{
"Email":"[email protected]",
"Name":"Car Care Membership Robot"
},
"To":[
{
"Email":"'.$data["your-email"].'",
"Name":"'.$data["your-name"].'"
},
{
"Email":"[email protected]",
"Name":"Farhad Ghafarzade"
}
],
"Subject":"We\'ve received your Total Car Care upgrade request",
"Variables":{
"ip-address":"'.$_SERVER["HTTP_CF_CONNECTING_IP"].'",
"date-time":"'.gmdate("Y-m-d\TH:i:s\Z",$_SERVER["REQUEST_TIME"]).'",
"name":"'.$data["your-name"].'"
},
"TemplateID":198695,
"TemplateLanguage":true,
"InlinedAttachments": [
{
"ContentType": "image/png",
"Filename": "signature.png",
"ContentID": "thesignature",
"Base64Content":"'.$data["signature-data"].'"
}
],
"Attachments": [
{
"ContentType": "application/pdf",
"Filename": "Terms-and-Conditions.pdf",
"Base64Content": "'.base64_encode (file_get_contents('https://greendropgarage.com/wp-content/uploads/Total-Car-Care-Terms-and-Conditions.pdf')).'"
}
]
}
]
}';
// Finally send the data to webhook endpoint
$ch = curl_init("https://api.mailjet.com/v3.1/send");
curl_setopt($ch, CURLOPT_USERPWD, "XXX");
curl_setopt($ch, CURLOPT_POSTFIELDS, $mj_data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); //Optional timeout value
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //Optional timeout value
$result = curl_exec($ch);
curl_close($ch);
// var_dump($result);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment