-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmailchimp-sync.php
executable file
·346 lines (254 loc) · 11.3 KB
/
mailchimp-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
<?php
/*
Plugin Name: MailChimp Sync
Plugin URI: http://premium.wpmudev.org/project/mailchimp-newsletter-integration
Description: Simply integrate MailChimp with your Multisite (or regular old single user WP) site - automatically add new users to your email lists and import all your existing users
Author: WPMU DEV
Version: 1.9.7
Author URI: http://premium.wpmudev.org
Network: true
WDP ID: 73
*/
/*
Copyright 2007-2013 Incsub (http://incsub.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
the Free Software Foundation.
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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
class WPMUDEV_MailChimp_Sync {
public static $instance = null;
public static $version = '1.9.7';
public static $basename;
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
$this->set_globals();
$this->includes();
self::$basename = plugin_basename( plugin_dir_path( __FILE__ )) . '/mailchimp-sync.php';
add_action( 'plugins_loaded', array( $this, 'mailchimp_localization' ) );
add_action( 'init', array( $this, 'init' ), 1 );
add_action( 'admin_init', array( $this, 'maybe_upgrade' ), 1 );
add_action( 'wpmu_new_user', array( $this, 'mailchimp_add_user' ) );
add_action( 'user_register', array( $this, 'mailchimp_add_user' ) );
add_action( 'make_ham_user', array( $this, 'mailchimp_add_user' ) );
add_action( 'profile_update', array( $this, 'mailchimp_edit_user' ) );
add_action( 'xprofile_updated_profile', array( $this, 'mailchimp_edit_user' ) ); //for buddypress
add_action( 'make_spam_user', array( $this, 'mailchimp_user_remove' ) );
add_action( 'delete_user', array( $this, 'mailchimp_user_remove' ) );
add_action( 'wpmu_delete_user', array( $this, 'mailchimp_user_remove' ) );
add_action( 'bp_core_action_set_spammer_status', array( $this, 'mailchimp_bp_spamming' ) , 10, 2); //for buddypress
add_action( 'widgets_init', array( $this, 'mailchimp_widget_init' ) );
new WPMUDEV_MailChimp_Sync_Webhooks_30();
if ( is_admin() ) {
include_once( 'admin/class-admin.php' );
new Mailchimp_Sync_Admin();
}
}
private function set_globals() {
define( 'MAILCHIMP_MAX_LOG_LINES', 100 );
define( 'MAILCHIMP_LANG_DOMAIN', 'mailchimp' );
define( 'MAILCHIMP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'MAILCHIMP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'MAILCHIMP_ASSETS_URL', MAILCHIMP_PLUGIN_URL . 'assets/' );
define( 'MAILCHIMP_FRONT_DIR', MAILCHIMP_PLUGIN_DIR . 'front/' );
}
private function includes() {
require_once( 'deprecated.php' );
require_once( 'helpers.php' );
require_once( 'integration.php' );
require_once( 'mailchimp-api/3.0/webhooks.php' );
// WPMUDEV Dashboard class
if ( is_admin() ) {
global $wpmudev_notices;
$wpmudev_notices[] = array(
'id'=> 73,
'name'=> 'MailChimp Sync',
'screens' => array(
'settings_page_mailchimp-network'
)
);
include_once( 'externals/wpmudev-dash-notification.php' );
}
}
public function init() {
if ( ! is_multisite() || ( is_multisite() && get_site_option( 'mailchimp_allow_shortcode', false ) ) ) {
require_once( MAILCHIMP_FRONT_DIR . 'shortcode.php' );
new WPMUDEV_MailChimp_Shortcode();
}
require_once( MAILCHIMP_FRONT_DIR . 'form.class.php' );
add_action( 'wp_ajax_incsub_mailchimp_subscribe_user', array( 'WPMUDEV_MailChimp_Form', 'validate_ajax_form' ) );
add_action( 'wp_ajax_nopriv_incsub_mailchimp_subscribe_user', array( 'WPMUDEV_MailChimp_Form', 'validate_ajax_form' ) );
}
function mailchimp_localization() {
// Load up the localization file if we're using WordPress in a different language
// Place it in this plugin's "languages" folder and name it "mailchimp-[value in wp-config].mo"
load_plugin_textdomain( 'mailchimp', false, '/mailchimp-sync/languages/' );
}
function mailchimp_widget_init() {
if ( ! is_multisite() || ( is_multisite() && get_site_option( 'mailchimp_allow_widget', false ) ) ) {
require_once( MAILCHIMP_FRONT_DIR . 'widget.php' );
register_widget( 'Incsub_Mailchimp_Widget' );
}
}
function mailchimp_add_user($uid) {
if ( is_integer( $uid ) ) {
$user = get_userdata( $uid );
}
elseif ( is_array( $uid ) ) {
$user = new stdClass;
$user->spam = false;
$user->deleted = false;
$user->user_email = $uid['email'];
$user->user_firstname = $uid['first_name'];
$user->user_lastname = $uid['last_name'];
}
else {
return false;
}
// If we have recently added this email, don't do anything
$transient_key = 'mailchimp_sync_' . md5( $user->user_email );
if ( get_site_transient( $transient_key ) ) {
return false;
}
//check for spam
if ( $user->spam || $user->deleted )
return false;
//remove + sign emails
if ( get_site_option('mailchimp_ignore_plus') == 'yes' && strstr( $user->user_email, '+' ) )
return false;
$mailchimp_auto_opt_in = get_site_option('mailchimp_auto_opt_in');
$autopt = $mailchimp_auto_opt_in == 'yes' ? true : false;
$merge_vars = array( 'FNAME' => $user->user_firstname, 'LNAME' => $user->user_lastname );
$merge_vars = apply_filters( 'mailchimp_merge_vars', $merge_vars, $user );
do_action( 'mailchimp_subscribe_user', $merge_vars, $user );
$interests = mailchimp_30_get_interest_groups();
$results = mailchimp_30_subscribe_user( $user->user_email, '', array( 'interests' => $interests, 'autopt' => $autopt, 'merge_fields' => $merge_vars ) );
if ( ! is_wp_error( $results ) ) {
// There could be other plugins triggering this function twice for a single user.
// MailChimp does not refresh the list such fast.
// We'll save the subscriber user data in order to avoid that.
$transient_key = 'mailchimp_sync_' . md5( $results['email_address'] );
set_site_transient( $transient_key, true, 30 ); // Set it only to 30 seconds, should be enough for most cases
}
return $results;
}
function mailchimp_edit_user($uid) {
$user = get_userdata( $uid );
//check for spam
if ( $user->spam || $user->deleted )
return false;
$merge_vars = array( 'FNAME' => $user->user_firstname, 'LNAME' => $user->user_lastname );
$merge_vars = apply_filters('mailchimp_merge_vars', $merge_vars, $user);
do_action( 'mailchimp_update_user', $merge_vars, $user );
$result = mailchimp_30_update_user( $user->user_email, '', array( 'merge_fields' => $merge_vars ) );
return $result;
}
function mailchimp_user_remove( $uid ) {
$user = get_userdata( $uid );
if ( ! $user )
return;
$results = mailchimp_30_unsubscribe_user( $user->user_email, '', true );
}
function mailchimp_bp_spamming( $user_id, $is_spam ) {
if ($is_spam)
$this->mailchimp_user_remove( $user_id );
else
$this->mailchimp_add_user( $user_id );
}
//------------------------------------------------------------------------//
//---Page Output Functions------------------------------------------------//
//------------------------------------------------------------------------//
/**
* Log MailChimp errors
* @param Array $errors
* @return type
*/
function mailchimp_log_errors( $errors ) {
if ( ! is_array( $errors ) )
$errors = array( $errors );
$current_log = get_site_option( 'mailchimp_error_log' );
$new_log = array();
foreach ( $errors as $error ) {
$code = isset( $error['code'] ) ? $error['code'] : 0;
$message = isset( $error['message'] ) ? $error['message'] : '';
$email = isset( $error['email'] ) ? $error['email'] : '';
$date = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), current_time( 'timestamp' ) );
$new_log[] = compact( 'code', 'message', 'email', 'date' );
}
if ( $current_log ) {
$new_log = array_merge( $current_log, $new_log );
// We'll only saved the last X lines of the log
$count = count( $new_log );
if ( $count > MAILCHIMP_MAX_LOG_LINES ) {
$new_log = array_slice( $new_log, $count - $offset - 1 );
}
}
update_site_option( 'mailchimp_error_log', $new_log );
}
public function maybe_upgrade() {
$saved_version = get_site_option( 'mailchimp_sync_version', '1.8.5' );
if ( $saved_version === self::$version ) {
return;
}
if ( version_compare( $saved_version, '1.9', '<' ) ) {
// Show a notice so the user can set the groups again
$mailchimp_apikey = get_site_option('mailchimp_apikey', '');
$mailchimp_mailing_list = get_site_option('mailchimp_mailing_list');
$groups = get_site_option( 'mailchimp_groups' );
if ( $mailchimp_apikey && $mailchimp_mailing_list && $groups ) {
update_site_option( 'mailchimp_sync_set_groups_again_notice', true );
}
}
update_site_option( 'mailchimp_sync_version', self::$version );
}
}
global $mailchimp_sync;
$mailchimp_sync = WPMUDEV_MailChimp_Sync::get_instance();
function mailchimp_sync() {
return WPMUDEV_MailChimp_Sync::get_instance();
}
add_action( 'wp_ajax_mailchimp_dismiss_notice', 'mailchimp_dismiss_notice' );
function mailchimp_dismiss_notice() {
if (
( is_multisite() && current_user_can( 'manage_network' ) )
|| ( ! is_multisite() && current_user_can( 'manage_options' ) )
) {
$option = $_POST['option'];
$allowed_options = array(
'mailchimp_sync_set_groups_again_notice'
);
if ( in_array( $option, $allowed_options ) ) {
delete_site_option( $option );
}
}
}
register_uninstall_hook(__FILE__, 'mailchimp_sync_uninstall' );
function mailchimp_sync_uninstall() {
global $wpdb;
delete_site_option( 'mailchimp_apikey' );
delete_site_option( 'mailchimp_mailing_list' );
delete_site_option( 'mailchimp_auto_opt_in' );
delete_site_option( 'mailchimp_ignore_plus' );
delete_site_option( 'mailchimp_webhooks_settings' );
delete_site_option( 'mailchimp_groups' );
delete_site_option( 'mailchimp_sync_version' );
$wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '%transient_mailchimp_list_groups%'" );
$wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '%transient_timeout_mailchimp_list_groups%'" );
$wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '%transient_mailchimp_category_interests%'" );
$wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '%transient_timeout_mailchimp_category_interests%'" );
//$wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '%transient_mailchimp_list_groups%'" );
//$wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '%transient_timeout_mailchimp_list_groups%'" );
//$wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '%transient_mailchimp_list_groups%'" );
//$wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '%transient_timeout_mailchimp_list_groups%'" );
}