-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgive-activecampaign.php
508 lines (427 loc) · 13.8 KB
/
give-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
<?php
/**
* Plugin Name: Give - ActiveCampaign
* Plugin URI: https://givewp.com/addons/activecampaign/
* Description: Easily display an ActiveCampaign opt-in option within your donation forms.
* Author: GiveWP
* Author URI: https://givewp.com/
* Version: 2.0.0
* Requires PHP: 7.2
* Requires at least: 6.3
* Text Domain: give-activecampaign
* Domain Path: languages
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Plugin constants.
if ( ! defined( 'GIVE_ACTIVECAMPAIGN_VERSION' ) ) {
define( 'GIVE_ACTIVECAMPAIGN_VERSION', '2.0.0' );
}
if ( ! defined( 'GIVE_ACTIVECAMPAIGN_MIN_GIVE_VER' ) ) {
define( 'GIVE_ACTIVECAMPAIGN_MIN_GIVE_VER', '3.11.0' );
}
if ( ! defined( 'GIVE_ACTIVECAMPAIGN_FILE' ) ) {
define( 'GIVE_ACTIVECAMPAIGN_FILE', __FILE__ );
}
if ( ! defined( 'GIVE_ACTIVECAMPAIGN_PATH' ) ) {
define( 'GIVE_ACTIVECAMPAIGN_PATH', dirname( GIVE_ACTIVECAMPAIGN_FILE ) );
}
if ( ! defined( 'GIVE_ACTIVECAMPAIGN_URL' ) ) {
define( 'GIVE_ACTIVECAMPAIGN_URL', plugin_dir_url( GIVE_ACTIVECAMPAIGN_FILE ) );
}
if ( ! defined( 'GIVE_ACTIVECAMPAIGN_BASENAME' ) ) {
define( 'GIVE_ACTIVECAMPAIGN_BASENAME', plugin_basename( GIVE_ACTIVECAMPAIGN_FILE ) );
}
if ( ! defined( 'GIVE_ACTIVECAMPAIGN_DIR' ) ) {
define( 'GIVE_ACTIVECAMPAIGN_DIR', plugin_dir_path( GIVE_ACTIVECAMPAIGN_FILE ) );
}
if ( ! class_exists( 'Give_ActiveCampaign' ) ) {
/**
* Class Give_ActiveCampaign
*
* @since 1.0.0
*/
class Give_ActiveCampaign {
/**
* @since 1.0.0
*
* @var Give_ActiveCampaign The reference the singleton instance of this class.
*/
private static $instance;
/**
* Notices (array)
*
* @since 1.0.0
*
* @var array
*/
public $notices = array();
/**
* @since 2.0.0
* @var array
*/
private $serviceProviders = [
\GiveActiveCampaign\FormExtension\ServiceProvider::class,
];
/**
* Returns the singleton instance of this class.
*
* @return Give_ActiveCampaign The singleton instance.
* @since 1.0.0
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
self::$instance->setup();
}
return self::$instance;
}
/**
* Setup Give ActiveCampaign.
*
* @since 1.0.0
* @access private
*/
private function setup() {
require_once GIVE_ACTIVECAMPAIGN_PATH . '/vendor/autoload.php';
// Load service providers.
add_action('before_give_init', [$this, 'registerServiceProviders']);
add_action( 'give_init', array( $this, 'init' ), 10 );
add_action( 'admin_init', array( $this, 'check_environment' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
add_action( 'give_add_email_tags', array( $this, 'add_email_tags' ), 9999999 );
add_action( 'give_insert_payment', array( $this, 'completed_donation_optin' ), 10, 2 );
add_filter( 'give-settings_get_settings_pages', array( $this, 'global_settings' ), 10, 1 );
}
/**
* Register service providers
*
* @since 2.0.0
*/
public function registerServiceProviders()
{
if ( ! $this->get_environment_warning() ) {
return;
}
foreach ($this->serviceProviders as $className) {
give()->registerServiceProvider($className);
}
}
/**
* Init the plugin after plugins_loaded so environment variables are set.
*
* @since 1.0.0
*/
public function init() {
if ( ! $this->get_environment_warning() ) {
return false;
}
$this->licensing();
$this->textdomain();
$this->activation_banner();
require_once GIVE_ACTIVECAMPAIGN_PATH . '/includes/activation.php';
if ( ! class_exists( 'Give' ) ) {
return false;
}
require_once GIVE_ACTIVECAMPAIGN_PATH . '/includes/helpers.php';
require_once GIVE_ACTIVECAMPAIGN_PATH . '/includes/metabox.php';
}
/**
* Load the plugin's textdomain
*
* @since 1.0.0
*/
public function textdomain() {
// Set filter for language directory.
$lang_dir = GIVE_ACTIVECAMPAIGN_DIR . '/languages/';
$lang_dir = apply_filters( 'give_activecampaign_languages_directory', $lang_dir );
// Traditional WordPress plugin locale filter.
$locale = apply_filters( 'plugin_locale', get_locale(), 'give-activecampaign' );
$mofile = sprintf( '%1$s-%2$s.mo', 'give-activecampaign', $locale );
// Setup paths to current locale file.
$mofile_local = $lang_dir . $mofile;
$mofile_global = WP_LANG_DIR . '/give-activecampaign/' . $mofile;
if ( file_exists( $mofile_global ) ) {
// Look in global /wp-content/languages/give-activecampaign/ folder.
load_textdomain( 'give-activecampaign', $mofile_global );
} elseif ( file_exists( $mofile_local ) ) {
// Look in local /wp-content/plugins/give-activecampaign/languages/ folder.
load_textdomain( 'give-activecampaign', $mofile_local );
} else {
// Load the default language files.
load_plugin_textdomain( 'give-activecampaign', false, $lang_dir );
}
// Load the translations
load_plugin_textdomain( 'give-activecampaign', false, GIVE_ACTIVECAMPAIGN_PATH . '/languages/' );
}
/**
* Check plugin environment.
*
* @return bool
* @since 1.0.0
* @access public
*/
public function check_environment() {
// Flag to check whether plugin file is loaded or not.
$is_working = true;
// Load plugin helper functions.
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
// Check to see if Give is activated, if it isn't deactivate and show a banner.
$is_give_active = defined( 'GIVE_PLUGIN_BASENAME' ) ? is_plugin_active( GIVE_PLUGIN_BASENAME ) : false;
if ( empty( $is_give_active ) ) {
// Show admin notice.
$this->add_admin_notice( 'prompt_give_activate', 'error',
sprintf( __( '<strong>Activation Error:</strong> You must have the <a href="%s" target="_blank">Give</a> plugin installed and activated for Give - ActiveCampaign to activate.',
'give-activecampaign' ), 'https://givewp.com' ) );
$is_working = false;
}
return $is_working;
}
/**
* Check plugin for Give environment.
*
* @return bool
* @since 1.0.0
* @access public
*/
public function get_environment_warning() {
// Flag to check whether plugin file is loaded or not.
$is_working = true;
// Verify dependency cases.
if (
defined( 'GIVE_VERSION' )
&& version_compare( GIVE_VERSION, GIVE_ACTIVECAMPAIGN_MIN_GIVE_VER, '<' )
) {
/*
Min. Give. plugin version. */
// Show admin notice.
$this->add_admin_notice( 'prompt_give_incompatible', 'error',
sprintf( __( '<strong>Activation Error:</strong> You must have the <a href="%1$s" target="_blank">Give</a> core version %2$s for the Give - ActiveCampaign add-on to activate.',
'give-activecampaign' ), 'https://givewp.com', GIVE_ACTIVECAMPAIGN_MIN_GIVE_VER ) );
$is_working = false;
}
return $is_working;
}
/**
* Implement Give Licensing for Give MailChimp Add On.
*
* @since 1.0.0
* @access private
*/
private function licensing() {
if ( class_exists( 'Give_License' ) ) {
new Give_License( GIVE_ACTIVECAMPAIGN_FILE, 'ActiveCampaign', GIVE_ACTIVECAMPAIGN_VERSION, 'WordImpress' );
}
}
/**
* Show activation banner for this add-on.
*
* @return bool
* @since 1.0.0
*
*/
public function activation_banner() {
// Check for activation banner inclusion.
if (
! class_exists( 'Give_Addon_Activation_Banner' )
&& file_exists( GIVE_PLUGIN_DIR . 'includes/admin/class-addon-activation-banner.php' )
) {
include GIVE_PLUGIN_DIR . 'includes/admin/class-addon-activation-banner.php';
}
// Initialize activation welcome banner.
if ( class_exists( 'Give_Addon_Activation_Banner' ) ) {
// Only runs on admin.
$args = array(
'file' => GIVE_ACTIVECAMPAIGN_FILE,
'name' => esc_html__( 'ActiveCampaign', 'give-activecampaign' ),
'version' => GIVE_ACTIVECAMPAIGN_VERSION,
'settings_url' => admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=activecampaign' ),
'documentation_url' => 'http://docs.givewp.com/addon-activecampaign',
'support_url' => 'https://givewp.com/support/',
'testing' => false, // Never leave true.
);
new Give_Addon_Activation_Banner( $args );
}
return true;
}
/**
* Allow this class and other classes to add notices.
*
* @param $slug
* @param $class
* @param $message
*
* @since 1.0.0
*/
public function add_admin_notice( $slug, $class, $message ) {
$this->notices[ $slug ] = array(
'class' => $class,
'message' => $message,
);
}
/**
* Display admin notices.
*
* @since 1.0.0
*/
public function admin_notices() {
$allowed_tags = array(
'a' => array(
'href' => array(),
'title' => array(),
'class' => array(),
'id' => array(),
),
'br' => array(),
'em' => array(),
'span' => array(
'class' => array(),
),
'strong' => array(),
);
foreach ( (array) $this->notices as $notice_key => $notice ) {
echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>";
echo wp_kses( $notice['message'], $allowed_tags );
echo '</p></div>';
}
}
/**
* Add plugin setting.
*
* @param array $settings Give Settings.
*
* @return array $settings Give Settings.
* @since 1.0.0
* @access public
*
*/
public function global_settings( $settings ) {
$settings[] = require_once GIVE_ACTIVECAMPAIGN_PATH . '/includes/settings.php';
new Give_ActiveCampaign_Settings();
return $settings;
}
/**
* Check if a donor needs to be subscribed on completed donation.
*
* @param $payment_id int
* @param $payment_data array
*
* @return bool
* @since 1.0.0
* @access public
*
*/
public function completed_donation_optin( $payment_id, $payment_data ) {
// Check to see if the user has elected to subscribe.
if ( ! isset( $_POST['give_activecampaign_signup'] ) || $_POST['give_activecampaign_signup'] !== 'on' ) {
return false;
}
$form_id = $payment_data['give_form_id'];
$form_display_option = give_get_meta( $form_id, 'activecampaign_per_form_options', true );
// Is AC disabled for this payment's form?
if ( 'disabled' === $form_display_option ) {
return false;
}
// Check if lists / tags is set for customized form if not use global list(s).
if ( 'customized' === $form_display_option ) {
$lists = give_get_meta( $form_id, 'give_activecampaign_lists', true, array() );
$tags = give_get_meta( $form_id, 'give_activecampaign_tags', true, array() );
} else {
// Get lists / tags.
$lists = give_get_option( 'give_activecampaign_lists', array() );
$tags = give_get_option( 'give_activecampaign_tags', array() );
}
// Subscribe email.
$this->subscribe_email( $payment_data['user_info'], $lists, $tags );
// Add meta to the donation post that this donation opted-in to ActiveCampaign.
if ( ! empty( $tags ) || ! empty( $lists ) ) {
add_post_meta( $payment_id, '_give_activecampaign_donation_optin_status', 'true' );
}
}
/**
* Add an email address to the ActiveCampaign list.
*
* @access public
*
* @param array $user_info Last name.
*
* @return bool
* @since 1.0.0
*/
public function subscribe_email( $user_info = [], $lists = [], $tags = [] ) {
$api_url = give_get_option( 'give_activecampaign_apiurl', false );
$api_key = give_get_option( 'give_activecampaign_api', false );
if ( $api_url && $api_key ) {
$ac = new ActiveCampaign( $api_url, $api_key );
$subscriber = [
"email" => $user_info['email'],
"first_name" => $user_info['first_name'],
"last_name" => $user_info['last_name'],
"tags" => implode( ', ', (array)$tags ),
];
foreach ( $lists as $list ) {
$subscriber["p[$list]"] = $list;
}
$response = $ac->api( "contact/add", $subscriber );
}
return false;
}
/**
* Register Email Tag
*
* @since 1.0.0
*/
public function add_email_tags() {
// Adds an email tag called {give_activecampaign_status} to indicate whether the donor opted in or not.
give_add_email_tag(
array(
'tag' => 'give_activecampaign_status', // The tag name.
'desc' => esc_html__( 'This outputs whether the donor opted-in to the ActiveCampaign', 'give-activecampaign' ), // For admins.
'func' => [ $this, 'status_email_tag' ], // Callback to function below.
'context' => 'donation',
'is_admin' => false, // default is false. This is here to simply display it as an option.
)
);
}
/**
* Render give_activecampaign_status email tag
*
* @param array $tag_args Array of arguments
*
* @return string
* @since 1.0.0
*/
public function status_email_tag( $tag_args ) {
$opt_in_meta = give_get_meta( $tag_args['payment_id'], '_give_activecampaign_donation_optin_status', true, false );
if ( ! empty( $opt_in_meta ) ) {
$output = esc_html__( 'Subscribed to newsletter', 'give-activecampaign' );
} else {
$output = esc_html__( 'Did not subscribe to newsletter', 'give-activecampaign' );
}
/**
* Filter is used change outputs whether the donor opted-in to the Newsletter.
*
* @param string $output string whether the donor opted-in to the Newsletter.
*
* @since 1.0.0
*
*/
return apply_filters( 'give_email_tag_give_activecampaign_status', $output, $tag_args );
}
}
/**
* Returns class object instance.
*
* @return Give_ActiveCampaign bool|object
* @since 1.0.0
*
*/
function Give_ActiveCampaign() {
return Give_ActiveCampaign::get_instance();
}
Give_ActiveCampaign();
}