-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
146 lines (128 loc) · 3.67 KB
/
Copy pathindex.php
File metadata and controls
146 lines (128 loc) · 3.67 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
<?php
/**
* Endpoints for Sharpspring
*
* @package RelativeMarketing\EndpointsForSharpspring
* @author Daniel Gregory
* @copyright 2018 Relative Marketing
*
* @wordpress-plugin
* Plugin Name: Endpoints For Sharpspring
* Plugin URI: https://relativemarketing.co.uk
* Description: Adds endpoints to your wordpress site that allows you to communicate with the Sharpspring API
* Version: 1.0.0
* Author: Relative Marketing
* Author URI: https://relativemarketing.co.uk
* Text Domain: endpoints-for-sharpring
*/
namespace RelativeMarketing\EndpointsForSharpspring;
defined('ABSPATH') or die();
include 'inc/class-sharpspring-request.php';
/**
* Registers a new endpoint to allow us to connect to sharsprings
* serverside and keep our secret key secret
*
* url should take the form of:
*
* www.example.com/wp-json/relativemarketing/sharpspring/v1/addLead/email/$email/name/$name
*
*/
add_action( 'rest_api_init', __NAMESPACE__ . '\\init_endpoint' );
function init_endpoint() {
register_rest_route(
'relativemarketing/sharpspring/v1',
'/addLead/',
[
'methods' => 'POST',
'callback' => __NAMESPACE__ . '\\handle_newsletter_subscribe',
'args' => [
'name' => [
'validate_callback' => __NAMESPACE__ . '\\validate_name_string'
],
'email' => [
'validate_callback' => function( $param ) {
return is_email( $param );
}
],
],
]
);
register_rest_route(
'relativemarketing/sharpspring/v1',
'/updateLeadTracking/',
[
'methods' => 'POST',
'callback' => __NAMESPACE__ . '\\update_lead_tracking',
'args' => [
'email' => [
'validate_callback' => function( $param ) {
return is_email( $param );
}
],
],
]
);
}
function update_lead_tracking( $params ) {
// Setup the data for the request
$data = [
'emailAddress' => $params['email'],
'trackingID' => get_sharpspring_tracking_id(),
];
$request = new Sharpspring_Request( 'updateLeadsV2', $data );
return rest_ensure_response( $request->make_request() );
}
/**
* Attempt to add the email subscription to sharspring as a new lead
*/
function handle_newsletter_subscribe( $params ) {
// Sort out name and email params
$split_name = explode( '%20', $params->get_param( 'name' ) );
$first_name = $split_name[0];
$last_name = $split_name[ count( $split_name ) - 1 ];
$email = $params->get_param('email');
$method = 'createLeads';
// If we have sharsprings(ss) tracking cookie then make sure it is attached
// to the lead when the lead is created
$tracking_id = get_sharpspring_tracking_id();
$campaign_id = $params['campaignId'];
// Format the params according to ss api requirements
$data = [
"objects" => [
[
"firstName" => $first_name,
"lastName" => $last_name,
"emailAddress" => $email,
"trackingID" => $tracking_id,
"campaignID" => $campaign_id,
]
]
];
$request = new Sharpspring_Request( $method, $data );
return rest_ensure_response( $request->make_request() );
}
/**
* Validates a given full name string
*/
function validate_name_string( $name ) {
/**
* ^ - Beginning of the line
* [a-zA-Z- ] - Allowed characters
* + - At least one or more of the preceding
* $ - End of line
*
* Everything must match from the start and end to be true
*/
return (bool) preg_match( "/^[a-zA-Z- ]+$/", urldecode( $name ) );
}
/**
* Get the users sharpspring tracking id
*/
function get_sharpspring_tracking_id() {
return array_key_exists( '__ss_tk', $_COOKIE ) ? $_COOKIE['__ss_tk'] : '';
}
/**
* Add the options page to allow users to setup the plugin
*/
require plugin_dir_path( __FILE__ ) . 'inc/class-options-page.php';
Options_Page::get_instance();