This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-getresponse-sync.php
445 lines (341 loc) · 14.7 KB
/
woocommerce-getresponse-sync.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/*
Plugin Name: WooCommerce - GetResponse Syncing
Plugin URI: http://zentala.pl/woocommerce-getresponse-syncing/
Description: Sync customers data between WooCommerce and GetResponse.
Author: Paul Zentala
Author URI: http://zentala.pl/
Version: 1.0
License: CC BY-NC-SA 4.0
License URI: http://creativecommons.org/licenses/by-nc-sa/4.0/
Domain Path: /langs
Text Domain: wc_getres_sync
*/
// Exit if accessed directly
defined('ABSPATH') or die("Cannot access pages directly.");
class ZntlGetresponseSync {
protected $options;
protected $api;
// Construct ********************************************************************
public function __construct()
{
$this->load_files();
$this->register_hooks();
}
// Load files *******************************************************************
public function load_files()
{
if ( ! class_exists( 'GetResponse' ) ) {
require_once( plugin_dir_path( __FILE__ ) . 'GetResponseAPI.class.php' );
}
}
// Register hooks **************************************************************
public function register_hooks()
{
register_activation_hook( __FILE__, array('ZntlGetresponseSync', 'plugin_activation'));
add_action('woocommerce_order_status_completed', array($this, 'status_completed'), 90, 1);
add_action('admin_init', array($this, 'register_setting_and_fields'));
add_action('plugins_loaded', array($this, 'init_plugin'));
add_action('admin_menu', array($this, 'add_menu_page') );
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_action_links') );
set_error_handler(array($this, 'error_handler'), E_USER_ERROR);
add_action('admin_notices', array($this, 'notices'),100);
}
// Plugin activation ************************************************************
public static function plugin_activation() {
update_option('wc_getres_sync_admin_notices', array());
$notices = array();
// WC active check
if (!is_plugin_active('woocommerce/woocommerce.php')) {
$notices['woo']= array('WooCommerce is required.', 'error');
deactivate_plugins( plugin_basename( __FILE__ ));
}
// PHP version check
if ( version_compare(PHP_VERSION, '5.3', '<') ) {
$notices['php']= array('PHP 5.3 is required.', 'error');
deactivate_plugins( plugin_basename( __FILE__ ) );
}
// WP version check
if ( version_compare(get_bloginfo('version'), '4.5', '<') ) {
$notices['wp']= array('WordPress 4.5 is required.', 'error');
deactivate_plugins( plugin_basename( __FILE__ ) );
}
update_option('wc_getres_sync_admin_notices', $notices);
update_option('wc_getres_sync_old_mails', array(), 'no');
}
// Plugin init ******************************************************************
public function init_plugin() {
$this->options = get_option('wc_getres_sync_options');
$this->verify_api_key();
load_plugin_textdomain( 'wc_getres_sync', FALSE, basename( dirname( __FILE__ ) ) . '/langs/' );
}
// Add action links *************************************************************
public function add_action_links($links) {
$mylinks = array(
'<a href="' . admin_url( 'admin.php?page=wc_getres_sync' ) . '">'. esc_html__('Settings') .'</a>',
);
return array_merge( $links, $mylinks );
}
// Add menu page ****************************************************************
public function add_menu_page()
{
add_submenu_page(
'woocommerce',
__( 'GetResponce Sync', 'wc_getres_sync' ),
__( 'GetResponce Sync', 'wc_getres_sync' ),
'manage_woocommerce',
'wc_getres_sync',
array($this, 'display_options_page')
);
}
// Display options page *********************************************************
public function display_options_page()
{
?>
<div class="wrap woocomerce">
<h2><?php _e( 'GetResponce Sync', 'wc_getres_sync' ); ?></h2>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php settings_fields('wc_getres_sync_options'); ?>
<?php do_settings_sections(__FILE__); ?>
<p class="submit">
<input type="submit" class="button-primary" value="<?php esc_html_e('Save Changes'); ?>">
</p>
</form>
</div>
<?php
}
// Register setting and fields **************************************************
public function register_setting_and_fields()
{
register_setting(
'wc_getres_sync_options',
'wc_getres_sync_options'
);
add_settings_section(
'wc_getres_sync_section',
esc_html__('GetResponce Settings', 'wc_getres_sync'),
array($this, 'wc_getres_sync_section_cb'),
__FILE__
);
add_settings_field(
'wc_getres_sync_api_key',
esc_html__('GetResponse API Key', 'wc_getres_sync'),
array($this, 'wc_getres_sync_api_key_setting'),
__FILE__,
'wc_getres_sync_section'
);
add_settings_field(
'wc_getres_sync_campagins_list',
esc_html__('GetResponse Campagin', 'wc_getres_sync'),
array($this, 'wc_getres_sync_campagins_list_setting'),
__FILE__,
'wc_getres_sync_section'
);
add_settings_field(
'wc_getres_sync_products_list',
esc_html__('WooCommerce Product', 'wc_getres_sync'),
array($this, 'wc_getres_sync_products_list_setting'),
__FILE__,
'wc_getres_sync_section'
);
}
// GetResponse Section Callback *************************************************
public function wc_getres_sync_section_cb() {
// optional
}
// Verify API Key ***************************************************************
public function verify_api_key()
{
$key = $this->options['wc_getres_sync_api_key'];
$notices = get_option('wc_getres_sync_admin_notices', array());
if(!empty($key)) {
try {
$api = new GetResponse($key);
$ping = $api->ping();
if( $ping === "pong") {
$this->api = $api;
$this->add_old_email();
if ( empty( $this->options['wc_getres_sync_campagins_list'] ) ) {
$notices['ok'] = array('Connected. Choose campagin.', 'success');
}
} else {
$notices[] = array('Invalid API key.', 'error');
}
} catch (Exception $ex) {
if ( strpos($ex->getMessage(), "Could not resolve host:") !== false ) {
$this->api = 'error';
echo "Could not resolve host:"; //
$notices['cant'] = array('Can\'t connect now. Try again.', 'success');
} elseif ( strpos($ex->getMessage(), "API call failed.") !== false ) {
$this->api = 'error';
$notices['api'] = array('API call failed. Probably GetResponse API class is outdated.', 'success');
}
}
} else {
$notices['key'] = array('Enter GetResponse API key.', 'error');
}
update_option('wc_getres_sync_admin_notices', $notices);
}
// Check product ****************************************************************
public function status_completed($order_id) {
$order = wc_get_order($order_id);
if($this->options['wc_getres_sync_products_list'] == 'all') {
if (!$this->add_email_to_get_response($order)) {
$this->save_email( array( $order->billing_first_name, $order->billing_email ) );
}
} else {
$items = $order->get_items();
foreach ($items as $item) {
$product = $order->get_product_from_item($item);
if ($product->get_id() == $this->options['wc_getres_sync_products_list']) {
if (!$this->add_email_to_get_response($order)) {
$this->save_email( array( $order->billing_first_name, $order->billing_email ) );
}
break;
}
}
}
}
// Add email to GetResponse *****************************************************
public function add_email_to_get_response($order) {
if ($this->api == "error") {
return false;
} else {
try {
$api = $this->api;
$addContact = $api->addContact(
$this->options['wc_getres_sync_campagins_list'],
$order->billing_first_name,
$order->billing_email
);
return true;
} catch (Exception $ex) {
return false;
}
}
}
// Save e-mail
public function save_email($email) {
$emails = get_option('wc_getres_sync_old_mails');
array_push($emails, $email);
update_option('wc_getres_sync_old_mails', $emails, 'no');
}
// Add old emails // zamienić $i na $key =>
public function add_old_email() {
$emails = get_option('wc_getres_sync_old_mails');
$i = 0;
$order = new stdClass();
foreach ($emails as $email) {
$order->billing_first_name = $email[0];
$order->billing_email = $email[1];
if($this->add_email_to_get_response($order)){
unset($emails[$i]);
}
$i ++;
}
update_option('wc_getres_sync_old_mails', $emails, 'no');
}
// Notices **********************************************************************
public static function wc_getres_sync_notice($msg, $type)
{
?>
<div class="notice notice-<?php echo $type; ?>">
<p><?php echo( '<b>GetResponce Sync</b>: ' . __($msg, 'wc_getres_sync') ); ?></p>
</div>
<?php
}
// Notices new
public function notices() {
$notices = get_option('wc_getres_sync_admin_notices');
if ( $notices ) {
foreach ($notices as $notice) {
?>
<div class="notice notice-<?php echo $notice[1]; ?>">
<p><?php echo( '<b>GetResponce Sync</b>: ' . $notice[0] ); ?></p>
</div>
<?php
}
}
update_option('wc_getres_sync_admin_notices', array());
}
/*
*
*
*
* Inputs
*
*/
// [input] GetResponse API Key *************************************************
public function wc_getres_sync_api_key_setting()
{
echo "<input name='wc_getres_sync_options[wc_getres_sync_api_key]' type='text' value='{$this->options['wc_getres_sync_api_key']}' class='regular-text'>";
}
// [select] Campagins List Settings ********************************************
public function wc_getres_sync_campagins_list_setting()
{
if ($this->api == 'error') {
echo esc_html__('Need connection to show campagins list.', 'wc_getres_sync');
} elseif (!empty($this->api)) {
try {
$api = $this->api;
$campaigns = (array)$api->getCampaigns();
echo '<select name="wc_getres_sync_options[wc_getres_sync_campagins_list]">';
if (empty($this->options['wc_getres_sync_campagins_list'])) {
echo '<option disabled selected value> -- '. esc_html__('select campagin', 'wc_getres_sync') .' -- </option>';
}
foreach ($campaigns as $id => $campagin) {
$selected = ($this->options['wc_getres_sync_campagins_list'] === $id) ? 'selected' : '';
?>
<option value="<?php echo $id; ?>" <?php echo $selected; ?>>
<?php echo $campagin->name; ?>
(<?php echo $campagin->optin; ?> optin)
</option>
<?php
}
echo '</select>';
} catch (Exception $ex) {
echo esc_html__('Need connection to show campagins list.', 'wc_getres_sync');
}
} else {
echo esc_html__('Invalid API Key.', 'wc_getres_sync');
}
}
// [select] Procusts List Settings **********************************************
public function wc_getres_sync_products_list_setting()
{
$loop = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => 200) );
?>
<select name="wc_getres_sync_options[wc_getres_sync_products_list]">
<?php
if(empty($this->options['wc_getres_sync_products_list'])) {
echo '<option disabled selected value> -- '. esc_html__('select product', 'wc_getres_sync') .' -- </option>';
}?>
$selected = ($this->options['wc_getres_sync_products_list'] == get_the_ID()) ? 'selected' : '';
<option selected value="all" <?php echo $selected; ?>>
<?php esc_html_e('All products', 'wc_getres_sync') ?>
</option>
<?php
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$selected = ($this->options['wc_getres_sync_products_list'] == get_the_ID()) ? 'selected' : '';
?>
<option value="<?php echo get_the_ID(); ?>" <?php echo $selected; ?> >
<?php echo get_the_title(); ?>
</option>
<?php
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</select>
<?php
}
// Error handler
public function error_handler($error_code, $message, $file, $line) {
throw new ErrorException($message, $error_code, 0, $file, $line);
}
}
// Admin init
new ZntlGetresponseSync();