/
cf7-hook-4-amazon-sqs.php
168 lines (126 loc) · 4.27 KB
/
cf7-hook-4-amazon-sqs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
Plugin Name: CF7 hook for SQS
Plugin URI: https://www.marcorapaccini.it
Description: Amazon SQS Hook for Contact Form 7
Version: 1.0.0
Author: Marco Rapaccini
Author URI: https://www.marcorapaccini.it
License: MIT
*/
// we need composer stuff
require 'vendor/autoload.php';
// and Amazon SQS stuff
use Aws\Sqs\SqsClient;
use Aws\Exception\AwsException;
// [OPTIONAL] when user post data in the CF7 form...
add_filter('wpcf7_posted_data','add_custom_field');
// ...trigger the following function that adds a custom field with MD5 value
function add_custom_field($posted_data){
// get current title
$wpcf = WPCF7_ContactForm::get_current();
$title = $wpcf->title();
// trigger only if the CF7 form title is specified
if (strpos($title, 'SpecificCF7Title')){
// get the timestamp
$timestamp_string = date("h:i:sa");
// create a special string combining email and timestamp
$mixed_string = $posted_data['email'] . $timestamp_string;
// create and MD5 from the combined string
$md5_string = md5($mixed_string);
// pass the value in the final email that will be sent to the recipients
$posted_data['custom_unique_id_4_SQS'] = $md5_string;
// pass the form ID too
$posted_data['form_unique_id'] = $wpcf->id();
}
return $posted_data;
};
// when email is sent to receipients...
add_action('wpcf7_mail_sent','cf7_amazon_sqs');
// ...trigger the following the function that sends message to Amazon SQS
function cf7_amazon_sqs($contact_form){
// get the form title
$title = $contact_form->title();
// this is the url for the Amazon SQS
$queueUrl = 'yourAmazonSQSURL';
// if the form contains the right title
if (strpos($title, 'TitleCustomString') !== false){
// this is the submission instance
$submission = WPCF7_Submission::get_instance();
// if the user has submitted the form
if ($submission) {
// get all the submitted data
$posted_data = $submission->get_posted_data();
// fields coming from the form
$name = $posted_data['name'];
$surname = $posted_data['surname'];
$email = $posted_data['email'];
$phone = $posted_data['phone'];
$city = $posted_data['city'];
$county = $posted_data['county'];
$md5_string = $posted_data['custom_unique_id_4_SQS'];
// [OPTIONAL] you have to encode data before sending if you want to send data in the MessageBody
$data_to_send = json_encode([
'Name' => $name,
'Surname' => $surname
'Email' => $email,
'Phone' => $phone,
'City' => $city,
'County' => $county,
'Id' => $md5_string
]);
// create the SQS client - this is not safe, use environment variables instead!
$client = new SqsClient([
'region' => 'eu-central-1',
'version' => 'latest',
'credentials' => [
'key' => 'yourKeyHere',
'secret' => 'yourSecretHere'
]
]);
// parameters to send to SQS (supposing that you want to send data as MessageAttributes)
$params = [
'DelaySeconds' => 1,
'MessageAttributes' => [
"Name" => [
'DataType' => "String",
'StringValue' => $name
],
"Surname" => [
'DataType' => "String",
'StringValue' => $surname
],
"Email" => [
'DataType' => "String",
'StringValue' => $email
],
"Phone" => [
'DataType' => "String",
'StringValue' => $phone
],
"City" => [
'DataType' => "String",
'StringValue' => $city
],
"County" => [
'DataType' => "String",
'StringValue' => $county
],
"Id" => [
'DataType' => "String",
'StringValue' => $md5_string
]
],
'MessageBody' => $data_to_send, // change this value with something else if you send data through MessageAttributes
'QueueUrl' => $queueUrl
];
// send the message to the SQS
try{
$result = $client->sendMessage($params);
}catch (AwsException $e) {
error_log($e->getMessage());
}
}
}
return;
}