-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhubspot-wordpress-sync.php
More file actions
251 lines (199 loc) · 9.26 KB
/
hubspot-wordpress-sync.php
File metadata and controls
251 lines (199 loc) · 9.26 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* Plugin Name: HubSpot and WordPress Sync
* Plugin URI: http://archie.makuwa.co.za
* Description: HubSpot CRM and WordPress Sync Plugin
* Version: 1.0.0
* Author: Archie Makuwa (Archie @TheLobableType)
* Author URI: http://archie.makuwa.co.uk
* License: GPL v3
* Text Domain: hubspotwpsync
*/
/**
* No funny business. If this file is called directly, abort.
* @author WordPress
*
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Get WordPress User
* (currently only works when user is logged in... an be extended)
* @author Archie M
*
*/
class ManageWordPressUser {
public function __construct(){
add_action( 'plugins_loaded', array( $this, 'check_if_user_logged_in' ) );
}
public function check_if_user_logged_in(){
if ( is_user_logged_in() ){
/**
* HubSpot Tracking some shit - can definitely come in handy (not implemented here)
* @author HubSpot
*/
/*
$hubspotutk = $_COOKIE['hubspotutk']; // grab the cookie from the visitors browser.
$ipaddress = $_SERVER['REMOTE_ADDR']; // IP address.
$hs_context = array(
'hutk' => $hubspotutk,
'ipAddress' => $ipaddress,
'pageUrl' => $_SERVER['request_uri'],
'pageName' => 'Checkout'
);
$hs_context_json = json_encode( $hs_context );
*/
/**
* API key MUST be stored in a safe and location and encrypted
*/
$url = "https://api.hubapi.com/contacts/v1/contact/?hapikey=XXXXXXXXXXXXXXXXXXXXXXXXXXX";
$current_user = wp_get_current_user();
//var_dump($current_user);
$fields['properties'][] = array( "property" => "email", "value" => $current_user->user_email );
$fields['properties'][] = array( "property" => "firstname", "value" => $current_user->user_firstname );
$fields['properties'][] = array( "property" => "lastname", "value" => $current_user->user_lastname );
// You can get the company from WooCommerce's Company Field or create your very own Company meta field
$response = wp_remote_post($url,
array(
'method' => 'POST',
'timeout' => 1000,
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
),
//'headers' => 'Content-Type: application/x-www-form-urlencoded',
'httpversion' => '1.0',
'sslverify' => false,
'body' => json_encode( $fields ),
)
);
// Check response codes
$response_code = wp_remote_retrieve_response_code( $response );
$response_message = wp_remote_retrieve_response_message( $response );
// Control errors
$file = plugin_dir_path( __FILE__ ) . '/log/log.txt';
if ( 200 != $response_code && ! empty( $response_message ) ) {
//$error_message = $response->get_error_message();
$error = "Something went wrong.";
file_put_contents( $file, $error);
} else {
//echo 'Response:<pre>';
//print_r( $response );
//echo '</pre>';
$now = new DateTime();
$time_now = $now->format('Y-m-d H:i:s'); // MySQL datetime format
//echo $now->getTimestamp();
file_put_contents( $file, "Success " . $time_now );
}
}
}
}
$ManageWordPressUser_plugin = new ManageWordPressUser();
/**
* Create Users in WordPress from HubSpot CRM (if they don't exist)
* @author Archie M
*
*/
class CreateUsersFromHubSpotUsers {
public function __construct(){
add_action( 'plugins_loaded', array( $this, 'create_users' ) );
}
public function create_users() {
/**
* API key MUST be stored in a safe and location and encrypted
**/
$url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=XXXXXXXXXXXXXXXXXXXXXXXXXXX";
global $wp_version;
$args = array(
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
'blocking' => true,
//'headers' => array(),
'cookies' => array(),
'body' => null,
'compress' => false,
'decompress' => true,
'sslverify' => true,
'stream' => false,
'filename' => null
);
//$feed_response = wp_remote_get( $url, $args );
$response = wp_remote_get( $url, $args );
if( !is_wp_error( $response ) ) {
$response_body = json_decode($response['body']);
$decoded_response_body = json_decode( json_encode($response_body) );
if( isset($decoded_response_body->contacts) ) {
foreach( $decoded_response_body->contacts as $contact) {
// get user details
if( !empty($contact->properties->firstname->value) ):
$first_name = $contact->properties->firstname->value;;
else:
$fist_name = '';
endif;
if( !empty($contact->properties->lastname->value) ):
$last_name = $contact->properties->lastname->value;;
else:
$last_name = '';
endif;
if( !empty($contact->properties->company->value) ):
$company_name = $contact->properties->company->value;;
else:
$company_name = '';
endif;
$identities = $contact->{'identity-profiles'}[0]->identities;
foreach ( $identities as $key => $value ){
if ( strtolower($value->type) === 'email' ){
$contact_email = $value->value;
}
}
/**
* Create users captured in HubSpot on the WordPress CMS
* @author Archie M
*
*/
// Get useful data
$username = sanitize_text_field( wp_unslash( $contact_email ));
$email = sanitize_email( $contact_email );
$first_name = sanitize_text_field( wp_unslash( $first_name ));
$last_name = sanitize_text_field( wp_unslash( $last_name ));
$company_name = sanitize_text_field( wp_unslash( $company_name ));
$password = wp_generate_password( 10, true, true );
$hashed_pwd = wp_hash_password($password);
$user_id = username_exists( $username );
if ( !$user_id && email_exists($email) == false ) {
//$user_id = wp_create_user( $username, $password, $email );
$userdata = array(
'user_login' => $username,
'first_name' => $first_name,
'last_name' => $last_name,
'user_pass' => $hashed_pwd
);
$user_id = wp_insert_user( $userdata ) ;
// if no error
if( !is_wp_error($user_id) ) {
$user = get_user_by( 'id', $user_id );
$user->set_role( 'customer' );
// Update custom meta fields (in this case, the WooCommerce Company Name)
update_user_meta( $user_id, 'billing_company', $company_name, '' );
// mail admin
$to = "iamdrinking@thepub.co.za";
$subject = "Created users from HubSpot";
$headers = array('Content-Type: text/html; charset=UTF-8');
$message = "The following users have been created in WordPress" . "\r\n";
$message .= "First Name: " . $first_name . "\r\n";
$message .= "Last Name " . $last_name . "\r\n";
$message .= "Company Name " . $company_name . "\r\n";
$message .= "Email Address " . $contact_email . "\r\n";
$attachments = "";
wp_mail( $to, $subject, $message, $headers, $attachments );
}
}
}
}
}
}
}
$CreateUsersFromHubSpotUsers = new CreateUsersFromHubSpotUsers();