-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-branching-quiz.php
More file actions
236 lines (199 loc) · 9.1 KB
/
custom-branching-quiz.php
File metadata and controls
236 lines (199 loc) · 9.1 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/*
Plugin Name: Custom Branching Quiz
Description: A dynamic, branching quiz with result-based outcomes, opt-in form, and ActiveCampaign integration.
Version: 2.0
Author: Your Name
*/
define('CBQ_VERSION', '2.0');
if (!defined('ABSPATH')) exit;
// Enqueue scripts and styles
function cbq_enqueue_assets() {
wp_enqueue_style('cbq-style', plugin_dir_url(__FILE__) . 'css/quiz-style.css', array(), CBQ_VERSION);
wp_enqueue_script('cbq-script', plugin_dir_url(__FILE__) . 'js/quiz-logic.js', array('jquery'), CBQ_VERSION, true);
// Load all quiz data
$plugin_dir = plugin_dir_path(__FILE__);
$quizzes = array();
// Load quiz 1
$quiz1_file = $plugin_dir . 'data/questions.json';
if (file_exists($quiz1_file)) {
$quizzes['quiz1'] = json_decode(file_get_contents($quiz1_file));
}
// Load quiz 2
$quiz2_file = $plugin_dir . 'data/questions-quiz2.json';
if (file_exists($quiz2_file)) {
$quizzes['quiz2'] = json_decode(file_get_contents($quiz2_file));
}
// Load quiz 3
$quiz3_file = $plugin_dir . 'data/questions-quiz3.json';
if (file_exists($quiz3_file)) {
$quizzes['quiz3'] = json_decode(file_get_contents($quiz3_file));
}
wp_localize_script('cbq-script', 'cbq_ajax', [
'ajax_url' => admin_url('admin-ajax.php'),
'quizzes' => $quizzes,
'version' => CBQ_VERSION
]);
}
add_action('wp_enqueue_scripts', 'cbq_enqueue_assets');
// Shortcode to render quiz
function cbq_render_quiz($atts) {
$atts = shortcode_atts(array(
'name' => 'quiz1' // default to quiz1 if no name specified
), $atts);
$quiz_name = sanitize_text_field($atts['name']);
ob_start();
echo '<div id="cbq-quiz-container" data-quiz-name="' . esc_attr($quiz_name) . '"></div>';
// Different messages for different quizzes
if ($quiz_name === 'quiz2' || $quiz_name === 'quiz3') {
$heading = ($quiz_name === 'quiz3') ? 'Please Provide Your Information to Get Your Results!' : 'Please Enter Your Information To Get Your Results!';
echo '<div id="cbq-optin-container" style="display:none;">
<h3>' . $heading . '</h3>
<p style="text-align:center; color:#fff; font-size:14px; margin-bottom:20px;">Your information is protected by Attorney Client Privilege and SSL Website Encryption</p>
<form id="cbq-optin-form">
<input type="text" name="name" placeholder="Your Name" required />
<input type="email" name="email" placeholder="Your Email" required />
<input type="tel" name="phone" placeholder="Your Phone" required />
<button type="submit">See My Result</button>
</form>
</div>';
} else {
echo '<div id="cbq-optin-container" style="display:none;">
<h3>Done!</h3><h5>Please provide your info below to get your results!</h5>
<form id="cbq-optin-form">
<input type="text" name="name" placeholder="Your Name" required />
<input type="email" name="email" placeholder="Your Email" required />
<input type="tel" name="phone" placeholder="Your Phone" required />
<button type="submit">See My Result</button>
</form>
</div>';
}
return ob_get_clean();
}
add_shortcode('custom_quiz', 'cbq_render_quiz');
// Handle opt-in submission and redirect
function cbq_submit_results() {
$answers = $_POST['answers'];
$quiz_name = sanitize_text_field($_POST['quiz_name']);
$name = sanitize_text_field($_POST['name']);
$email = sanitize_email($_POST['email']);
$phone = sanitize_text_field($_POST['phone']);
$questions_answers = isset($_POST['questions_answers']) ? $_POST['questions_answers'] : array();
// Generate redirect based on quiz type
if ($quiz_name === 'quiz2') {
$redirect = cbq_generate_results_quiz2($answers);
$subject = 'Settlement Calculator Lead';
$ac_list = 'Settlement Calculator 2-21';
} elseif ($quiz_name === 'quiz3') {
$redirect = cbq_generate_results_quiz3($answers);
$subject = 'Good Case Calculator Lead';
$ac_list = 'Good Case Quiz';
} else {
$redirect = cbq_generate_results($answers);
$subject = 'New Case Calculator Quiz Lead';
$ac_list = 'Quiz Lead';
}
// Email admin with detailed Q&A
$to = 'calls@desalvolaw.com';
$body = "Quiz: $quiz_name\nName: $name\nEmail: $email\nPhone: $phone\n\nAnswers:\n";
// Include question and answer text if available
if (!empty($questions_answers)) {
foreach ($questions_answers as $qa) {
$body .= "Q: " . $qa['question'] . "\nA: " . $qa['answer'] . "\n\n";
}
} else {
$body .= implode("\n", array_map(function($a, $i) {
return "Q" . ($i+1) . ": $a";
}, $answers, array_keys($answers)));
}
wp_mail($to, $subject, $body);
// Send to ActiveCampaign
cbq_send_to_activecampaign($name, $email, $phone, $ac_list);
// Clean any previous output to prevent JSON corruption
if (ob_get_length()) {
ob_clean();
}
wp_send_json(['redirect' => $redirect]);
wp_die();
}
add_action('wp_ajax_cbq_submit_results', 'cbq_submit_results');
add_action('wp_ajax_nopriv_cbq_submit_results', 'cbq_submit_results');
function cbq_generate_results($answers) {
if (in_array('death', $answers) && !in_array('bad_liability', $answers)) {
return 'https://desalvolaw.com/cvr2';
}
if (in_array('bad_liability', $answers) || in_array('IC', $answers) || in_array('not_on_clock', $answers)) {
return 'https://desalvolaw.com/cvr1';
}
if (in_array('under_7500', $answers)) return 'https://desalvolaw.com/cvr6';
if (in_array('under_30k', $answers)) return 'https://desalvolaw.com/cvr5';
if (in_array('under_100', $answers)) return 'https://desalvolaw.com/cvr4';
if (in_array('under_350', $answers)) return 'https://desalvolaw.com/cvr3';
if (in_array('over_350', $answers)) return 'https://desalvolaw.com/cvr2';
return 'https://desalvolaw.com/thank-you';
}
function cbq_generate_results_quiz2($answers) {
// Priority 1: Check if answer to Q2 is "My Fault" OR answer to Q3 is "No" -> cvr1
if (in_array('my_fault', $answers) || in_array('on_clock_no', $answers)) {
return 'https://desalvolaw.com/cvr1';
}
// Priority 2: Check Q4 for "Very Serious Permanent Injury or Death" OR Q5 for "Over $350,000" -> cvr2
if (in_array('very_serious_death', $answers) || in_array('over_350000', $answers)) {
return 'https://desalvolaw.com/cvr2';
}
// Priority 3: Check Q4 for "Serious Injury with Medical Care/Injections/Surgery" OR Q5 "$100,001 to $350,000" -> cvr3
if (in_array('serious_surgery', $answers) || in_array('100001_to_350000', $answers)) {
return 'https://desalvolaw.com/cvr3';
}
// Priority 4: Check Q5 for "$30,001 to $100,000" -> cvr4
if (in_array('30001_to_100000', $answers)) {
return 'https://desalvolaw.com/cvr4';
}
// Priority 5: Check Q5 for "$7,501 to $30,000" -> cvr5
if (in_array('7501_to_30000', $answers)) {
return 'https://desalvolaw.com/cvr5';
}
// Priority 6: Check Q5 for "Under $7,500" -> cvr6
if (in_array('under_7500', $answers)) {
return 'https://desalvolaw.com/cvr6';
}
return 'https://desalvolaw.com/thank-you';
}
function cbq_generate_results_quiz3($answers) {
// Priority 1: If Q2="My Fault" OR Q3="No" OR Q5="An Unemployed Person With No Insurance" -> gcr1
if (in_array('my_fault', $answers) || in_array('on_clock_no', $answers) || in_array('no_insurance', $answers)) {
return 'https://desalvolaw.com/gcr1';
}
// Priority 2: If (Q4="Injury With Little or No Medical Care" OR Q4="Very Serious Permanent Injury or Death")
// AND (Q5="A Big Corporation" OR Q5="A Rich Person With Excellent Insurance") -> gcr2
$has_extreme_injury = in_array('injury_little_care', $answers) || in_array('very_serious_death', $answers);
$has_good_defendant = in_array('big_corporation', $answers) || in_array('rich_excellent_insurance', $answers);
if ($has_extreme_injury && $has_good_defendant) {
return 'https://desalvolaw.com/gcr2';
}
// Priority 3: If Q4="Serious Injury with Medical Care/Injections/Surgery" -> gcr4
if (in_array('serious_surgery', $answers)) {
return 'https://desalvolaw.com/gcr4';
}
// Default: Otherwise -> gcr3
return 'https://desalvolaw.com/gcr3';
}
function cbq_send_to_activecampaign($name, $email, $phone, $list_name = 'Quiz Lead') {
$api_url = 'https://desalvolaw.api-us1.com/api/3/contact/sync';
$api_key = '19a21f94a45862f38736c72a413ac15ddb2bfd1f253b33ea01d31b2f9d75b51f579da894';
$body = [
'contact' => [
'firstName' => $name,
'email' => $email,
'phone' => $phone,
'tags' => [$list_name]
]
];
$response = wp_remote_post($api_url, [
'headers' => [
'Api-Token' => $api_key,
'Content-Type' => 'application/json'
],
'body' => json_encode($body)
]);
}