-
Notifications
You must be signed in to change notification settings - Fork 1
/
wps-requests.php
79 lines (64 loc) · 2.54 KB
/
wps-requests.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
<?php
require_once('wps-database.php');
require_once('wps-response.php');
/**
* {
* "funnel_id":int,
* "funnel_message":string,
* "email":string | undefinied
* "phonenumber":string | undefinied
* }
*
*
*/
function wps_rest_handle_request($wp)
{
//check if number or email is set -- then call function responsible for database
$response = new DatabaseResponse('fail', 'No email or number set');
//check if 'funnel_id' or 'funnel_message' are undefined, if so return false
if (!isset($wp['funnel_id']) || !isset($wp['funnel_message'])) {
wp_send_json(['status' => 'error', 'message' => 'No funnel id or message set']);
exit();
}
try {
//create a variable that holds the response
if (isset($wp['phonenumber']) && $wp['phonenumber'] !== '') {
$response = wps_db_submit_phone_number($wp['funnel_id'], $wp['funnel_message'], $wp['phonenumber']);
} else if (isset($wp['email']) && $wp['email'] !== '') {
$response = wps_db_submit_email($wp['funnel_id'], $wp['funnel_message'], $wp['email']);
} else {
wp_send_json(['status' => 'error', 'message' => 'No email or number set'], 500);
exit();
}
} catch (Exception $e) {
wp_send_json(['status' => 'error', 'message' => $e->getMessage()], 500);
exit();
}
wp_send_json(['status' => $response->status, 'message' => $response->message], 200);
exit();
}
function wps_get_image_path($image_id)
{
//get request, grab the current funnel element by DB_Command
$image_src = wp_get_attachment_image_src($image_id, 'large');
return $image_src;
}
function wps_rest_get_current_funnel_element($wp)
{
//get request, grab the current funnel element by DB_Command
$db_response = wps_db_get_current_funnel();
//check for errors in response, if so return
if ($db_response->status === 'error') {
wp_send_json(['status' => $db_response->status, 'data' => $db_response->message]);
exit();
}
//replace data->hero_image and data->header_icon with image paths
$db_response->message->hero_image = wps_get_image_path($db_response->message->hero_image);
$db_response->message->header_icon = wps_get_image_path($db_response->message->header_icon);
//fix text and remove backslashes
$db_response->message->header_text = FunnelObject::clear_backslashes($db_response->message->header_text);
$db_response->message->header_subtext = FunnelObject::clear_backslashes($db_response->message->header_subtext);
$db_response->message->button_text = FunnelObject::clear_backslashes($db_response->message->button_text);
wp_send_json(['status' => $db_response->status, 'data' => $db_response->message]);
exit();
}