-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.e20r-mailchimp-for-membership-plugins.php
executable file
·487 lines (380 loc) · 16.9 KB
/
class.e20r-mailchimp-for-membership-plugins.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
<?php
/*
Plugin Name: E20R MailChimp Interest Groups for Paid Memberships Pro (and WooCommerce)
Plugin URI: https://eighty20results.com/wordpress-plugins/e20r-mailchimp-for-membership-plugins/
Description: Use MailChimp Interest Groups and Merge Fields when adding members to your MailChimp.com list(s) when they purchase, sign up, or register to get access your site/products. Segment users with Merge Tags and/or MailChimp Interest Groups. Include custom user meta data in the merge tags/merge fields. Supports <a href="https://wordpress.org/plugins/paid-memberships-pro/">Paid Memberships Pro</a> and <a href="https://wordpress.org/plugins/woocommerce/">WooCommerce</a>
Version: 5.0.1
WC requires at least: 3.3
WC tested up to: 4.0
Requires at least: 4.5
Tested up to: 5.4
Author: Eighty/20 Results <thomas@eighty20results.com>
Author URI: https://eighty20results.com/thomas-sjolshagen/
Developer: Thomas Sjolshagen <thomas@eighty20results.com>
Developer URI: https://eighty20results.com/thomas-sjolshagen/
Text Domain: e20r-mailchimp-for-membership-plugins
Domain Path: /languages
License: GPLv2
* Copyright (c) 2017-2020 - Eighty / 20 Results by Wicked Strong Chicks.
* ALL RIGHTS RESERVED
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace E20R\MailChimp;
use E20R\Utilities\GDPR_Enablement;
use E20R\Utilities\Licensing\Licensing;
use E20R\Utilities\Licensing\Mailchimp_License;
use E20R\Utilities\Utilities;
use E20R_Email_Memberships\Admin_Setup;
/**
* Deny TESTING the "GROUPINGS" entry in the `e20r_mailchimp_merge_fields` supplied array of merge fields
*/
if ( ! defined( 'E20R_MC_TESTING' ) ) {
define( 'E20R_MC_TESTING', false );
}
if ( ! defined( 'E20R_MAILCHIMP_VERSION' ) ) {
define( 'E20R_MAILCHIMP_VERSION', '5.0.1' );
}
if ( ! defined( 'E20R_MAILCHIMP_DIR' ) ) {
define( 'E20R_MAILCHIMP_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'E20R_MAILCHIMP_URL' ) ) {
define( 'E20R_MAILCHIMP_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'E20R_MAILCHIMP_NA' ) ) {
define( 'E20R_MAILCHIMP_NA', - 1 );
}
if ( ! defined( 'E20R_MAILCHIMP_CURRENT_USER' ) ) {
define( 'E20R_MAILCHIMP_CURRENT_USER', 0 );
}
if ( ! defined( 'E20R_MAILCHIMP_BILLING_USER' ) ) {
define( 'E20R_MAILCHIMP_BILLING_USER', 1 );
}
if ( ! class_exists( 'E20R\MailChimp\Controller' ) ) {
class Controller {
/**
* Name of plugin directory (plugin slug)
*/
const plugin_slug = 'e20r-mailchimp-for-membership-plugins';
/**
* @var null|Controller
*/
private static $instance = null;
/**
* @var string $plugin - Name of the plugin (for hooks)
*/
public static $plugin;
/**
* Controller constructor.
*/
private function __construct() {
add_filter( 'e20r-licensing-text-domain', array( $this, 'get_plugin_name' ) );
add_action( 'init', array( Mailchimp_License::get_instance(), 'load_hooks' ), 99 );
}
/**
* Return the name of this plugin
*
* @return string
*/
public function get_plugin_name() {
return Controller::plugin_slug;
}
/**
* Returns an instance of the Controller class (or null)
*
* @return Controller|null
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* The plugins_loader hook handler for the E20R MailChimp for PMPro plugin
*/
public function plugins_loaded() {
Utilities::get_instance()->log( "Loading action handlers for member plugins" );
self::$plugin = plugin_basename( __FILE__ );
add_action( 'plugins_loaded', array( Admin_Setup::get_instance(), 'load_hooks' ) );
add_action( 'plugins_loaded', array( Licensing::get_instance(), 'load_hooks' ), 11 );
add_action( "plugins_loaded", array( Member_Handler::get_instance(), "load_plugin" ), 12 );
add_action( 'plugins_loaded', array( GDPR_Enablement::get_instance(), 'load_hooks' ), 98 );
add_action( 'plugins_loaded', array( MC_Settings::get_instance(), 'load_actions' ), 99 );
add_action( 'init', array( User_Handler::get_instance(), 'load_actions' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'load_frontend_styles' ), 999 );
}
/**
* Load CSS and Javascript to /wp-admin/
*/
public function load_admin_styles() {
if ( is_admin() ) {
wp_enqueue_style( 'e20r-mc-admin', E20R_MAILCHIMP_URL . 'css/e20r-mailchimp-for-membership-plugins-admin.css', array(), E20R_MAILCHIMP_VERSION );
}
}
/**
* Are we currently on the login or registration page?
* (Includes support for Theme My Logins)
*
* @return bool
*/
public static function on_login_page() {
global $post;
$on_login_page = ( $GLOBALS['pagenow'] === 'wp-login.php' && ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === 'register' );
$on_login_page = $on_login_page || ( isset( $post->post_content ) ? has_shortcode( $post->post_content, 'theme-my-login' ) : false );
return $on_login_page;
}
/**
* Load style(s) for frontend
*
* @since 2.2 - ENHANCEMENT: Account for PMPro styles
*/
public function load_frontend_styles() {
if ( wp_style_is( 'pmpro_frontend', 'enqueued' ) ) {
$deps = array( 'pmpro_frontend' );
} else {
$deps = null;
}
wp_enqueue_style( 'e20r-mc', E20R_MAILCHIMP_URL . "css/e20r-mailchimp-for-membership-plugins.css", $deps, E20R_MAILCHIMP_VERSION );
}
/**
* Set Default options when activating plugin
*/
public function activation() {
//get options
$options = get_option( "e20r_mc_settings", array() );
//defaults
if ( empty( $options ) ) {
$options = array(
"api_key" => "",
"double_opt_in" => 0,
"unsubscribe" => 2,
"members_list" => array(),
"additional_lists" => array(),
"level_merge_field" => "",
);
update_option( "e20r_mc_settings", $options );
} else if ( ! empty( $options ) && ! isset( $options['unsubscribe'] ) ) {
$options['unsubscribe'] = 2;
update_option( "e20r_mc_settings", $options );
}
do_action( 'e20r-mailchimp-plugin-activation' );
}
/**
* Unsubscribe a user from a specific list
*
* @param string $list_id - the List ID
* @param \WP_User $user - The WP_User object for the user
* @param array|null $merge_fields - The Merge Fields to use for the user/list ID
* @param array|null $interests - The Interests to use for the user/list ID
*
* @return bool
*/
public function unsubscribe( $list_id, $user, $merge_fields = null, $interests = null ) {
//make sure user has an email address
if ( empty( $user->user_email ) ) {
return false;
}
$mc_api = MailChimp_API::get_instance();
$utils = Utilities::get_instance();
$unsub_setting = $mc_api->get_option( 'unsubscribe' );
if ( ! empty( $mc_api ) ) {
switch ( $unsub_setting ) {
case 1:
case 'all':
$utils->log( "Actually removing the user {$user->ID} from the {$list_id} list" );
return $mc_api->delete( $list_id, $user, $merge_fields, $interests );
break;
case 2:
// We're only updating the user/member's list settings
$utils->log( "Updating the member list settings (merge fields and Interests) for {$user->ID} on {$list_id}" );
return $mc_api->subscribe( $list_id, $user, $merge_fields, $interests );
break;
default:
return false;
}
} else {
wp_die( __( 'Error during unsubscribe operation. Please report this error to the administrator', Controller::plugin_slug ) );
}
}
/**
* Subscribe a user to a specific list
*
* @param string $list_id - the List ID
* @param \WP_User $user - The WP_User object for the user
* @param int $level_id - The membership level (ID) the user is being added/subscribed to
*
* @return bool
*/
public function subscribe( $list_id, $user, $level_id ) {
$utils = Utilities::get_instance();
$mc_api = MailChimp_API::get_instance();
$ig_controller = Interest_Groups::get_instance();
$mf_controller = Merge_Fields::get_instance();
$level_ids = array();
//make sure user has an email address
if ( empty( $user->user_email ) ) {
$utils->log( "No user info??? " . print_r( $user, true ) );
return false;
}
$utils->log( "Subscribe operation for {$user->user_email} for Level ID: {$level_id}" );
if ( ! is_null( $level_id ) ) {
$level_ids = array( $level_id );
}
$merge_fields = $mf_controller->populate( $list_id, $user, $level_ids );
$interests = $ig_controller->populate( $list_id, $user, $level_ids );
$opt_in = $mc_api->get_option( 'double_opt_in' );
$email_type = apply_filters( 'e20r_mailchimp_default_mail_type', 'html' );
$has_consent = apply_filters( 'e20r-mailchimp-user-consent-provided', true, $user->ID );
if ( true === $has_consent ) {
$utils->log( "Trying to subscribe {$user->ID} to list {$list_id} with double opt-in ({$opt_in}), GDPR consent ({$has_consent}) and type {$email_type}" );
return $mc_api->subscribe( $list_id, $user, $merge_fields, $interests, $email_type, $opt_in );
}
do_action( 'e20r-mailchimp-process-consent', $has_consent, $user->ID );
}
/**
* Subscribe a user to any additional opt-in lists selected
*
* @param int $user_id
*/
public function subscribe_to_additional_lists( $user_id, $level_id ) {
$utils = Utilities::get_instance();
$additional_lists = $utils->get_variable( 'additional_lists', null );
$has_consent = apply_filters( 'e20r-mailchimp-user-consent-provided', true, $user_id );
if ( ! empty( $additional_lists ) ) {
update_user_meta( $user_id, 'e20r_mc_additional_lists', $additional_lists );
$list_user = get_userdata( $user_id );
foreach ( $additional_lists as $list ) {
//subscribe them
if ( true === $has_consent ) {
$this->subscribe( $list, $list_user, $level_id );
}
}
}
do_action( 'e20r-mailchimp-process-consent', $has_consent, $user_id );
}
/**
* Class auto-loader for the Mailchimp for PMPro plugin
*
* @param string $class_name Name of the class to auto-load
*
* @since 1.0
* @access public static
*/
public static function auto_loader( $class_name ) {
if ( false === stripos( $class_name, 'e20r' ) ) {
return;
}
$parts = explode( '\\', $class_name );
$c_name = strtolower( preg_replace( '/_/', '-', $parts[ ( count( $parts ) - 1 ) ] ) );
$base_paths = apply_filters( 'e20r-mailchimp-autoloader-paths', array( plugin_dir_path( __FILE__ ) . 'classes/' ) );
if ( file_exists( plugin_dir_path( __FILE__ ) . 'class/' ) ) {
$base_paths = apply_filters( 'e20r-mailchimp-autoloader-paths', array( plugin_dir_path( __FILE__ ) . 'class/' ) );
}
$filename = "class.{$c_name}.php";
foreach ( $base_paths as $base_path ) {
$iterator = new \RecursiveDirectoryIterator( $base_path, \RecursiveDirectoryIterator::SKIP_DOTS | \RecursiveIteratorIterator::SELF_FIRST | \RecursiveIteratorIterator::CATCH_GET_CHILD | \RecursiveDirectoryIterator::FOLLOW_SYMLINKS );
/**
* Load class member files, recursively
*/
$filter = new \RecursiveCallbackFilterIterator( $iterator, function ( $current, $key, $iterator ) use ( $filename ) {
$file_name = $current->getFilename();
// Skip hidden files and directories.
if ( $file_name[0] == '.' || $file_name == '..' ) {
return false;
}
if ( $current->isDir() ) {
// Only recurse into intended subdirectories.
return $file_name() === $filename;
} else {
// Only consume files of interest.
return strpos( $file_name, $filename ) === 0;
}
} );
foreach ( new \ RecursiveIteratorIterator( $iterator ) as $f_filename => $f_file ) {
$class_path = $f_file->getPath() . "/" . $f_file->getFilename();
if ( $f_file->isFile() && false !== strpos( $class_path, $filename ) ) {
require_once( $class_path );
}
}
}
}
}
}
/**
* Filter to test the Groupings functionality.
*
* @param array $fields
* @param \WP_User $user
* @param string $list_id
*
* @return array
*/
function test_e20rmc_listsubscribe_fields( $fields, $user = null, $list_id = null ) {
if ( defined( 'E20R_MC_TESTING' ) && false !== E20R_MC_TESTING ) {
if ( WP_DEBUG ) {
error_log( "PMPROMC: Loading test filter for listsubscribe fields" );
}
if ( is_null( $user ) ) {
$user = get_current_user();
}
$new_fields = array(
"FNAME" => 'Thomas',
"LNAME" => 'PMPro',
'GROUPINGS' => array(
array(
'name' => "Category",
'groups' => array( "Members" ),
),
),
"JOINDATE" => date( 'Y-m-d', current_time( 'timestamp' ) ),
);
$fields = array_merge( $fields, $new_fields );
}
return $fields;
}
global $e20r_mailchimp_plugins;
/**
* @var string[] $e20r_mailchimp_plugins
*
* Format:
* array( 'plugin_slug' => '', 'class_name' => '', 'label' => '' )
*/
if ( empty( $e20r_mailchimp_plugins ) ) {
$e20r_mailchimp_plugins = array();
}
$e20r_mailchimp_plugins['pmpro'] = array(
'plugin_slug' => 'pmpro',
'class_name' => 'PMPro',
'label' => __( 'Level:', Controller::plugin_slug ),
);
$e20r_mailchimp_plugins['woocommerce'] = array(
'plugin_slug' => 'woocommerce',
'class_name' => 'WooCommerce',
'label' => __( 'Cat:', Controller::plugin_slug ),
);
try {
spl_autoload_register( 'E20R\MailChimp\Controller::auto_loader' );
} catch ( \Exception $e ) {
wp_die( "E20R-MC-Loader-Error: " . $e->getMessage() );
}
register_activation_hook( __FILE__, array( Controller::get_instance(), "activation" ) );
/** One-Click update support **/
// Load one-click update support for v3.x BETA from custom repository
Utilities::configureUpdateServerV4( 'e20r-mailchimp-for-membership-plugins', plugin_dir_path( __FILE__ ) . 'class.e20r-mailchimp-for-membership-plugins.php' );
/** End of One-Click update support **/
$GLOBALS['e20r_mc_error_msg'] = null;
add_action( 'plugins_loaded', array( Controller::get_instance(), 'plugins_loaded' ), - 1 );