-
Notifications
You must be signed in to change notification settings - Fork 0
/
edd-activecampaign.php
667 lines (587 loc) · 17.4 KB
/
edd-activecampaign.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<?php
/**
* Plugin Name: Easy Digital Downloads - ActiveCampaign
* Plugin URL: http://easydigitaldownloads.com/extension/activecampaign
* Description: Include a ActiveCampaign signup option with your Easy Digital Downloads checkout.
* Author: Easy Digital Downloads
* Author URI: https://easydigitaldownloads.com
* Version: 1.1
* Text Domain: edd-activecampaign
* Domain Path: languages
*
* @package EDD_ActiveCampaign
* @version 1.1
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'EDD_ActiveCampaign' ) ) :
/**
* EDD_ActiveCampaign Class
*
* @since 1.1
*/
final class EDD_ActiveCampaign {
/**
* Holds the instance.
*
* Ensures that only one instance of EDD_ActiveCampaign exists in memory at any one
* time and it also prevents needing to define globals all over the place.
*
* TL;DR This is a static property property that holds the singleton instance.
*
* @var object
* @static
* @since 1.1
*/
private static $instance;
/**
* EDD ActiveCampaign uses many variables, several of which can be filtered to
* customize the way it operates. Most of these variables are stored in a
* private array that gets updated with the help of PHP magic methods.
*
* @var array
* @see EDD_ActiveCampaign::setup_globals()
* @since 1.1
*/
private $data;
/**
* Get active object instance.
*
* @since 1.1
*
* @access public
* @static
* @return object
*/
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof EDD_ActiveCampaign ) ) {
self::$instance = new EDD_ActiveCampaign();
self::$instance->setup_globals();
self::$instance->load_classes();
self::$instance->hooks();
self::$instance->updater();
}
return self::$instance;
}
/**
* Class constructor. Includes constants, includes and init method.
*
* @access private
* @since 1.1
*/
private function __construct() {
if ( ! class_exists( 'Easy_Digital_Downloads' ) ) {
return;
}
self::$instance = $this;
add_action( 'init', array( $this, 'init' ) );
register_activation_hook( __FILE__, array( $this, 'activation' ) );
}
/**
* Sets up the constants/globals used.
*
* @access public
* @since 1.1
*/
private function setup_globals() {
$this->version = '1.1';
// File Path and URL Information
$this->file = __FILE__;
$this->basename = apply_filters( 'edd_activecampaign_plugin_basenname', plugin_basename( $this->file ) );
$this->plugin_url = plugin_dir_url( __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->lang_dir = apply_filters( 'edd_activecampaign_lang_dir', trailingslashit( $this->plugin_path . 'languages' ) );
// Classes
$this->includes_dir = apply_filters( 'edd_activecampaign_includes_dir', trailingslashit( $this->plugin_path . 'includes' ) );
$this->includes_url = apply_filters( 'edd_activecampaign_includes_url', trailingslashit( $this->plugin_url . 'includes' ) );
}
/**
* Throw error on object clone.
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @since 1.1
* @access protected
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'edd-activecampaign' ), '1.0' );
}
/**
* Disable unserializing of the class.
*
* @access protected
* @since 1.1
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'edd-activecampaign' ), '1.0' );
}
/**
* Magic method for checking if custom variables have been set.
*
* @access protected
* @since 1.0
*
* @param string $key Variable name.
*
* @return void
*/
public function __isset( $key ) {
return isset( $this->data[ $key ] );
}
/**
* Magic method for getting variables.
*
* @access protected
* @since 1.1
*
* @param string $key Variable name.
*
* @return void
*/
public function __get( $key ) {
return isset( $this->data[ $key ] ) ? $this->data[ $key ] : null;
}
/**
* Magic method for setting variables.
*
* @since 1.1
* @access protected
*
* @param string $key Variable name.
* @param string $value Variable value.
*
* @return void
*/
public function __set( $key, $value ) {
$this->data[ $key ] = $value;
}
/**
* Magic method for unsetting variables
*
* @access protected
* @since 1.1
*
* @param string $key Variable name.
*
* @return void
*/
public function __unset( $key ) {
if ( isset( $this->data[ $key ] ) ) {
unset( $this->data[ $key ] );
}
}
/**
* Magic method to prevent notices and errors from invalid method calls.
*
* @access public
* @since 1.1
*
* @param string $name
* @param array $args
*
* @return void
*/
public function __call( $name = '', $args = array() ) {
unset( $name, $args );
return null;
}
/**
* Reset the instance of the class.
*
* @access public
* @since 1.1
* @static
*/
public static function reset() {
self::$instance = null;
}
/**
* Function fired on `init`.
*
* This function is called on WordPress `init`. It's triggered from the
* constructor function.
*
* @access public
* @since 1.1
* @return void
*/
public function init() {
do_action( 'edd_activecampaign_before_init' );
$this->load_textdomain();
do_action( 'edd_activecampaign_after_init' );
}
/**
* Loads classes.
*
* @access private
* @since 1.1
* @return void
*/
private function load_classes() {
}
/**
* Load Plugin Textdomain
*
* Looks for the plugin translation files in certain directories and loads
* them to allow the plugin to be localised
*
* @since 1.0
* @access public
* @return bool True on success, false on failure
*/
public function load_plugin_textdomain() {
// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), 'edd-activecampaign' );
$mofile = sprintf( '%1$s-%2$s.mo', 'edd-activecampaign', $locale );
// Setup paths to current locale file
$mofile_local = $this->lang_dir . $mofile;
if ( file_exists( $mofile_local ) ) {
// Look in the /wp-content/plugins/edd-reviews/languages/ folder
load_textdomain( 'edd-activecampaign', $mofile_local );
} else {
// Load the default language files
load_plugin_textdomain( 'edd-activecampaign', false, $this->lang_dir );
}
return false;
}
/**
* Activation function fires when the plugin is activated.
*
* This function is fired when the activation hook is called by WordPress,
* it disables the plugin if EDD isn't active and throws an error.
*
* @access public
* @since 1.1
* @return void
*/
public function activation() {
global $wpdb;
edd_activecampaign();
if ( ! class_exists( 'Easy_Digital_Downloads' ) ) {
if ( is_plugin_active( $this->basename ) ) {
deactivate_plugins( $this->basename );
unset( $_GET['activate'] );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
}
}
}
/**
* Adds all the hooks/filters.
*
* The plugin relies heavily on the use of hooks and filters and modifies
* default WordPress behavior by the use of actions and filters which are
* provided by WordPress.
*
* Actions are provided to hook on this function, before the hooks and filters
* are added and after they are added. The class object is passed via the action.
*
* @access public
* @since 1.1
* @return void
*/
public function hooks() {
do_action_ref_array( 'edd_activecampaign_before_setup_actions', array( &$this ) );
/* Actions */
add_action( 'edd_checkout_before_gateway', array( $this, 'check_for_email_signup' ), 10, 2 );
add_action( 'edd_purchase_form_before_submit', array( $this, 'display_checkout_fields' ), 100 );
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'edd_complete_download_purchase', array( $this, 'completed_download_purchase_signup' ), 10, 3 );
/* Filters */
add_filter( 'edd_settings_sections_extensions', array( $this, 'settings_section' ) );
add_filter( 'edd_settings_extensions', array( $this, 'register_settings' ) );
add_filter( 'edd_metabox_fields_save', array( $this, 'save_metabox' ) );
do_action_ref_array( 'edd_activecampaign_after_setup_actions', array( &$this ) );
}
/**
* Handles the displaying of any notices in the admin area.
*
* @access public
* @since 1.1
* @return void
*/
public function admin_notices() {
echo '<div class="error"><p>' . sprintf( __( 'You must install %sEasy Digital Downloads%s for the ActiveCampaign Add-On to work.', 'edd-activecampaign' ), '<a href="http://easydigitaldownloads.com" title="Easy Digital Downloads">', '</a>' ) . '</p></div>';
}
/**
* Checks whether a user should be signed up for the ActiveCampaign list.
*
* @access public
* @since 1.0
*
* @param array $data Checkout data.
* @param array $user_info User details.
*
* @return void
*/
public function check_for_email_signup( $data, $user_info ) {
if ( $data['eddactivecampaign_activecampaign_signup'] ) {
$email = $user_info['email'];
$this->subscribe_email( $email, $user_info['first_name'], $user_info['last_name'] );
}
}
/**
* Add an email address to the ActiveCampaign list.
*
* @access public
* @since 1.0
* @since 1.1 - Added param $list
*
* @param string $email Email address.
* @param string $first_name First name.
* @param string $last_name Last name.
* @param int $list List ID.
*
* @return bool
*/
public function subscribe_email( $email, $first_name = '', $last_name = '', $list = 0 ) {
if ( edd_get_option( 'eddactivecampaign_api' ) ) {
if ( 0 == $list ) {
$list = edd_get_option( 'eddactivecampaign_list', false );
}
if ( ! $list ) {
return false;
}
// Load ActiveCampaign API
require_once( 'vendor/ActiveCampaign.class.php' );
$ac = new ActiveCampaign( edd_get_option( 'eddactivecampaign_apiurl' ), edd_get_option( 'eddactivecampaign_api' ) );
$subscriber = array(
"email" => "$email",
"first_name" => "$first_name",
"last_name" => "$last_name",
"p[{$list}]" => $list,
"status[{$list}]" => 1,
);
$ac->api( "contact/add", $subscriber );
}
return false;
}
/**
* Display checkout fields.
*
* @access public
* @since 1.0
*/
public function display_checkout_fields() {
global $edd_options;
ob_start();
if ( isset( $edd_options['eddactivecampaign_api'] ) && strlen( trim( $edd_options['eddactivecampaign_api'] ) ) > 0 ) { ?>
<p>
<input name="eddactivecampaign_activecampaign_signup" id="eddactivecampaign_activecampaign_signup" type="checkbox" checked="checked" />
<label for="eddactivecampaign_activecampaign_signup"><?php echo isset( $edd_options['eddactivecampaign_label'] ) ? $edd_options['eddactivecampaign_label'] : __( 'Sign up for our mailing list', 'edd-activecampaign' ); ?></label>
</p>
<?php
}
echo ob_get_clean();
}
/**
* Registers the subsection for EDD Settings.
*
* @access public
* @since 1.1
*
* @param array $sections Settings Sections.
*
* @return array Sections with ActiveCampaign added.
*/
public function settings_section( $sections ) {
$sections['activecampaign'] = __( 'ActiveCampaign', 'edd-activecampaign' );
return $sections;
}
/**
* Register settings.
*
* @access public
* @since 1.0
*
* @param array $settings Settings.
*
* @return array $settings Updated settings.
*/
public function register_settings( $settings ) {
$activecampaign_settings = array(
array(
'id' => 'eddactivecampaign_settings',
'name' => '<strong>' . __( 'ActiveCampaign Settings', 'edd-activecampaign' ) . '</strong>',
'desc' => '',
'type' => 'header',
),
array(
'id' => 'eddactivecampaign_apiurl',
'name' => __( 'API URL', 'edd-activecampaign' ),
'desc' => __( 'Enter your ActiveCampaign API URL. It is located in the Settings --> API area of your ActiveCampaign account.', 'edd-activecampaign' ),
'type' => 'text',
'size' => 'regular',
),
array(
'id' => 'eddactivecampaign_api',
'name' => __( 'API Key', 'edd-activecampaign' ),
'desc' => __( 'Enter your ActiveCampaign API Key. It is located in the Settings --> API area of your ActiveCampaign account.', 'edd-activecampaign' ),
'type' => 'text',
'size' => 'regular',
),
array(
'id' => 'eddactivecampaign_list',
'name' => __( 'Choose a list', 'edd-activecampaign' ),
'desc' => __( 'Select the list you wish to subscribe buyers to.', 'edd-activecampaign' ),
'type' => 'select',
'options' => $this->get_lists()
),
array(
'id' => 'eddactivecampaign_label',
'name' => __( 'Checkout Label', 'edd-activecampaign' ),
'desc' => __( 'This is the text shown next to the signup option', 'edd-activecampaign' ),
'type' => 'text',
'size' => 'regular',
),
);
if ( version_compare( EDD_VERSION, 2.5, '>=' ) ) {
$activecampaign_settings = array( 'activecampaign' => $activecampaign_settings );
}
return array_merge( $settings, $activecampaign_settings );
}
/**
* Instantiates the EDD_License class and passes the plugin data to enable
* remote license checks with EDD server.
*
* @since 1.1
* @access public
* @return void
*/
public function updater() {
if ( class_exists( 'EDD_License' ) ) {
$license = new EDD_License( $this->file, 'ActiveCampaign', $this->version, 'EDD Team', 'edd_activecampaign_license_key' );
}
}
/**
* Retrieve the lists set up in ActiveCampaign.
*
* @since 1.1
* @access public
* @return array $lists ActiveCampaign Lists.
*/
public function get_lists() {
if ( ! edd_get_option( 'eddactivecampaign_apiurl', false ) || ! edd_get_option( 'eddactivecampaign_api', false ) ) {
return array();
}
// Load ActiveCampaign API
require_once( 'vendor/ActiveCampaign.class.php' );
$ac = new ActiveCampaign( edd_get_option( 'eddactivecampaign_apiurl' ), edd_get_option( 'eddactivecampaign_api' ) );
$lists = $ac->api( 'list/list', array( 'ids' => 'all' ) );
// var_dump($lists);
if ( (int) $lists->success ) {
// We need to cast the object to an array because ActiveCampaign returns invalid JSON.
$lists = (array) $lists;
$output = array();
foreach ( $lists as $key => $list ) {
if ( ! is_numeric( $key ) ) {
continue;
}
$output[ $list->id ] = $list->name;
}
return $output;
} else {
return array();
}
}
/**
* Add metabox to Download edit screen.
*
* @since 1.1
* @access public
*/
public function add_metabox() {
if ( current_user_can( 'edit_product', get_the_ID() ) ) {
add_meta_box( 'edd_activecampaign', 'ActiveCampaign', array( $this, 'render_metabox' ), 'download', 'side' );
}
}
/**
* Render the metabox displayed on the Download edit screen.
*
* @since 1.1
* @access public
*/
public function render_metabox() {
global $post;
echo '<p>' . __( 'Select the lists you wish buyers to be subscribed to when purchasing.', 'edd-activecampaign' ) . '</p>';
$checked = (array) get_post_meta( $post->ID, '_edd_activecampaign', true );
foreach ( $this->get_lists() as $list_id => $list_name ) {
echo '<label>';
echo '<input type="checkbox" name="_edd_activecampaign[]" value="' . esc_attr( $list_id ) . '"' . checked( true, in_array( $list_id, $checked ), false ) . '>';
echo ' ' . $list_name;
echo '</label><br/>';
}
}
/**
* Save metabox data.
*
* @since 1.1
* @access public
*
* @param array $fields Metabox fields.
*
* @return array $fields.
*/
public function save_metabox( $fields ) {
$fields[] = '_edd_activecampaign';
return $fields;
}
/**
* Check if a customer needs to be subscribed on completed purchase of specific products.
*
* @since 1.1
* @access public
*
* @param int $download_id Download ID.
* @param int $payment_id Payment ID.
* @param string $download_type Download type (default/bundle).
*/
public function completed_download_purchase_signup( $download_id = 0, $payment_id = 0, $download_type = 'default' ) {
$user_info = edd_get_payment_meta_user_info( $payment_id );
$lists = get_post_meta( $download_id, '_edd_activecampaign', true );
if ( 'bundle' == $download_type ) {
$downloads = edd_get_bundled_products( $download_id );
if ( $downloads ) {
foreach( $downloads as $id ) {
$download_lists = get_post_meta( $id, '_edd_activecampaign', true );
if ( is_array( $download_lists ) ) {
$lists = array_merge( $download_lists, (array) $lists );
}
}
}
}
if ( empty ( $lists ) ) {
return;
}
$lists = array_unique( $lists );
foreach ( $lists as $list ) {
$this->subscribe_email( $user_info['email'], $user_info['first_name'], $user_info['last_name'], $list );
}
}
}
endif;
/**
* The main function responsible for returning the one true EDD_ActiveCampaign
* Instance to functions everywhere.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* Example: <?php $edd_activecampaign = edd_activecampaign(); ?>
*
* @since 1.1
* @return object|null The one true EDD_ActiveCampaign Instance.
*/
function edd_activecampaign() {
if ( ! class_exists( 'Easy_Digital_Downloads' ) ) {
return null;
}
return EDD_ActiveCampaign::get_instance();
}
add_action( 'plugins_loaded', 'edd_activecampaign', 10 );