-
Notifications
You must be signed in to change notification settings - Fork 15
/
pmpro-aweber.php
executable file
·841 lines (735 loc) · 28 KB
/
pmpro-aweber.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
<?php
/*
Plugin Name: Paid Memberships Pro - AWeber Add On
Plugin URI: https://www.paidmembershipspro.com/add-ons/pmpro-aweber-integration/
Description: Add users and members to AWeber lists based on their membership level.
Version: 1.4
Author: Paid Memberships Pro
Author URI: https://www.paidmembershipspro.com/
*/
/*
Copyright 2011 Stranger Studios (email : jason@strangerstudios.com)
GPLv2 Full license details in license.txt
*/
/*
These keys are for the PMPro-AWeber App.
Your user keys should be entered on the settings page at Settings --> PMPro AWeber in your WP dashboard.
*/
define('PMPROAW_APPID', '7f549046');
// Load deprecated.php
require_once( dirname( __FILE__ ) . '/includes/deprecated.php' );
//init
function pmproaw_init()
{
//include AWeberAPI Class if we don't have it already
if(!class_exists("AWeberAPI"))
{
require_once(dirname(__FILE__) . "/includes/aweber_api/aweber_api.php");
}
//get options for below
$options = get_option("pmproaw_options");
//default new unsubscribe option to 'Yes (Only old lists.)'
if(empty($options)) {
$options = array('unsubscribe'=>'1');
update_option('pmproaw_options', $options);
} elseif(!isset($options['unsubscribe'])) {
$options['unsubscribe'] = 1;
update_option('pmproaw_options', $options);
}
//setup hooks for new users
if(!empty($options['users_lists']))
add_action("user_register", "pmproaw_user_register");
}
add_action("init", "pmproaw_init", 30);
/*
Load the AWeber API
*/
function pmproaw_getAccount($force = false)
{
global $pmproaw_aweber_api, $pmproaw_aweber_account;
// Ensure that the API library has been loaded
pmproaw_init();
if(empty($force) && empty($pmproaw_aweber_account)) {
$options = get_option("pmproaw_options");
if(empty($options['consumer_key']) || empty($options['consumer_secret']) || empty($options['access_key']) || empty($options['access_secret']))
return false;
try {
$pmproaw_aweber_api = new AWeberAPI($options['consumer_key'], $options['consumer_secret']);
$pmproaw_aweber_account = $pmproaw_aweber_api->getAccount($options['access_key'], $options['access_secret']);
} catch(AWeberAPIException $exc) {
global $pmproaw_exception;
$pmproaw_exception = $exc;
return false;
}
}
return $pmproaw_aweber_account;
}
//if the API is not working, show a notice and link to settings
function pmproaw_admin_notices() {
//check transient so we don't check to often
$pmproaw_api_squelch_notice = get_transient('pmproaw_api_squelch_notice');
if($pmproaw_api_squelch_notice)
return;
//don't show on the options page
if(!empty($_REQUEST['page']) && $_REQUEST['page'] == 'pmproaw_options')
return;
//haven't checked in a while so try to connect
$account = pmproaw_getAccount();
if(empty($account)) {
?>
<div class="notice notice-error is-dismissible">
<p>Error connecting to the AWeber API. <a href="<?php echo admin_url('options-general.php?page=pmproaw_options');?>">Please check your PMPro AWeber settings and reauthorize if necessary.</a></p>
</div>
<?php
} else {
//API seems okay, let's disable the check for 24 hours
set_transient('pmproaw_api_squelch_notice', true, 60*60*24);
}
}
add_action('admin_notices', 'pmproaw_admin_notices');
//subscribe users when they register
function pmproaw_user_register($user_id)
{
clean_user_cache($user_id);
$options = get_option("pmproaw_options");
//should we add them to any lists?
if(!empty($options['users_lists']) && !empty($options['access_key']) && !empty($options['access_secret']))
{
//get user info
$list_user = get_userdata($user_id);
//subscribe to each list
try {
foreach($options['users_lists'] as $list_id)
{
pmproaw_subscribe($list_id, $list_user);
}
}
catch(AWeberAPIException $exc) {
//just catching errors so users don't see them
}
}
}
/**
* Handle list changes after membership levels are changed.
*
* @since 1.3.4
*
* @param array $old_user_levels Array with user ID as key and array of old levels as value.
*/
function pmproaw_pmpro_after_all_membership_level_changes( $old_user_levels ) {
// Get options.
$options = get_option( 'pmproaw_options' );
// Loop through all the users who have had changes.
foreach ( $old_user_levels as $user_id => $old_levels ) {
// Get the lists that the user should now be subscribed to.
$new_lists = array();
$levels = pmpro_getMembershipLevelsForUser( $user_id );
if ( empty( $levels ) ) {
// User does not have a memberhsip level. Get non-member lists.
if ( ! empty( $options['users_lists'] ) ) {
$new_lists = $options['users_lists'];
}
} else {
foreach ( $levels as $level ) {
if ( ! empty( $options['level_' . $level->id . '_lists'] ) ) {
$new_lists = array_merge( $new_lists, $options['level_' . $level->id . '_lists'] );
}
}
}
// Get the lists that the user used to be subscribed to if we are going to unsubscribe.
$old_lists = array();
if ( ! empty( $options['unsubscribe'] ) ) {
// Get the lists that the user was subscribed to before the change.
$old_lists = array();
if ( empty( $old_levels ) ) {
// User did not have a membership level before. Get non-member lists.
if ( ! empty( $options['users_lists'] ) ) {
$old_lists = $options['users_lists'];
}
} else {
foreach ( $old_levels as $level ) {
if ( ! empty( $options['level_' . $level->id . '_lists'] ) ) {
$old_lists = array_merge( $old_lists, $options['level_' . $level->id . '_lists'] );
}
}
}
}
// Unique both sets of lists.
$new_lists = array_unique( $new_lists );
$old_lists = array_unique( $old_lists );
// Calculate the unsubscribe lists.
$unsubscribe_lists = array_diff( $old_lists, $new_lists );
// Process all subscriptions.
$user = get_userdata( $user_id );
foreach ( $new_lists as $list_id ) {
// Subscribe the user to the list.
try {
pmproaw_subscribe( $list_id, $user );
} catch ( AWeberAPIException $exc ) {
// Just catching errors so users don't see them.
}
}
// Process all unsubscriptions.
foreach ( $unsubscribe_lists as $list_id ) {
// Unsubscribe the user from the list.
try {
pmproaw_unsubscribe( $list_id, $user );
} catch ( AWeberAPIException $exc ) {
// Just catching errors so users don't see them.
}
}
}
}
add_action( 'pmpro_after_all_membership_level_changes', 'pmproaw_pmpro_after_all_membership_level_changes' );
function pmproaw_unsubscribe($list_id, $list_user)
{
//accept arrays as well
if(is_array($list_id))
$list_id = $list_id['id'];
//get aweber account or fail
$account = pmproaw_getAccount();
if(empty($account))
return;
//get list
$listURL = "/accounts/{$account->id}/lists/{$list_id}";
$aw_list = $account->loadFromUrl($listURL);
//find subscriber
$subscribers = $aw_list->subscribers;
$params = array('email' => $list_user->user_email);
$found_subscribers = $subscribers->find($params);
//unsubscribe
foreach($found_subscribers as $subscriber)
{
$subscriber->status = 'unsubscribed';
$subscriber->save();
}
}
function pmproaw_subscribe($list_id, $list_user)
{
//accept arrays as well
if(is_array($list_id))
$list_id = $list_id['id'];
//get aweber account or fail
$account = pmproaw_getAccount();
if(empty($account))
return;
$listURL = "/accounts/{$account->id}/lists/{$list_id}";
$list = $account->loadFromUrl($listURL);
$subscribers = $list->subscribers;
if (!$custom_fields = apply_filters("pmpro_aweber_custom_fields", array(), $list_user))
{
$new_subscriber = $subscribers->create(array(
'email' => $list_user->user_email,
'name' => trim($list_user->first_name . " " . $list_user->last_name),
'update_existing' => 'true',
'status' => 'subscribed',
));
}
else
{
$new_subscriber = $subscribers->create(array(
'email' => $list_user->user_email,
'name' => trim($list_user->first_name . " " . $list_user->last_name),
'update_existing' => 'true',
'status' => 'subscribed',
'custom_fields' => $custom_fields,
));
}
}
//change email in AWeber if a user's email is changed in WordPress
function pmproaw_profile_update($user_id, $old_user_data)
{
//get aweber account or fail
$account = pmproaw_getAccount();
if(empty($account))
return;
$new_user_data = get_userdata($user_id);
if($new_user_data->user_email != $old_user_data->user_email)
{
try {
//get all lists
$pmproaw_lists = $account->lists->data['entries'];
if(!empty($pmproaw_lists))
{
foreach($pmproaw_lists as $list)
{
//get list
$listURL = "/accounts/{$account->id}/lists/{$list['id']}";
$aw_list = $account->loadFromUrl($listURL);
//find subscriber
$subscribers = $aw_list->subscribers;
$params = array(
'status' => 'subscribed',
'email' => $old_user_data->user_email,
);
$found_subscribers = $subscribers->find($params);
if ( count( $found_subscribers ) ) {
// Subscriber found. Change email.
$subscriber = $found_subscribers[0];
$subscriber->email = $new_user_data->user_email;
$subscriber->save();
}
}
}
}
catch(AWeberAPIException $exc) {
//just catching errors to hide them from users
}
}
}
add_action("profile_update", "pmproaw_profile_update", 10, 2);
//admin init. registers settings
function pmproaw_admin_init()
{
// setup settings
register_setting('pmproaw_options', 'pmproaw_options', 'pmproaw_options_validate');
add_settings_section('pmproaw_section_general', 'General Settings', 'pmproaw_section_general', 'pmproaw_options');
add_settings_field('pmproaw_option_authorization_code', 'AWeber Authorization Code', 'pmproaw_option_authorization_code', 'pmproaw_options', 'pmproaw_section_general');
add_settings_field('pmproaw_option_consumer_key', 'AWeber Consumer Key', 'pmproaw_option_consumer_key', 'pmproaw_options', 'pmproaw_section_general');
add_settings_field('pmproaw_option_consumer_secret', 'AWeber Consumer Secret', 'pmproaw_option_consumer_secret', 'pmproaw_options', 'pmproaw_section_general');
add_settings_field('pmproaw_option_access_key', 'AWeber Access Key', 'pmproaw_option_access_key', 'pmproaw_options', 'pmproaw_section_general');
add_settings_field('pmproaw_option_access_secret', 'AWeber Access Secret', 'pmproaw_option_access_secret', 'pmproaw_options', 'pmproaw_section_general');
add_settings_field('pmproaw_option_users_lists', 'Non-Members Lists', 'pmproaw_option_users_lists', 'pmproaw_options', 'pmproaw_section_general');
// add_settings_field('pmproaw_option_double_opt_in', 'Require Double Opt-in?', 'pmproaw_option_double_opt_in', 'pmproaw_options', 'pmproaw_section_general');
add_settings_field('pmproaw_option_unsubscribe', 'Unsubscribe on Level Change?', 'pmproaw_option_unsubscribe', 'pmproaw_options', 'pmproaw_section_general');
// pmpro-related options
add_settings_section('pmproaw_section_levels', 'Membership Levels and Lists', 'pmproaw_section_levels', 'pmproaw_options');
//add options for levels
$pmproaw_levels = pmproaw_getPMProLevels();
if(!empty($pmproaw_levels))
{
foreach($pmproaw_levels as $level)
{
add_settings_field('pmproaw_option_memberships_lists_' . $level->id, $level->name, 'pmproaw_option_memberships_lists', 'pmproaw_options', 'pmproaw_section_levels', array($level));
}
}
}
add_action("admin_init", "pmproaw_admin_init");
/**
* Get all PMPro levels, including hidden levels.
* The pmpro_getAllLevels function is good enough to use now.
* But PMPro might not be installed, so we need this wrapper.
*/
function pmproaw_getPMProLevels()
{
if ( function_exists( 'pmpro_getAllLevels' ) ) {
$pmproaw_levels = pmpro_getAllLevels( true, true );
} else {
$pmproaw_levels = array();
}
return $pmproaw_levels;
}
//options sections
function pmproaw_section_general()
{
?>
<p></p>
<?php
}
//options sections
function pmproaw_section_levels()
{
global $wpdb;
$pmproaw_levels = pmproaw_getPMProLevels();
//do we have PMPro installed?
if(class_exists("MemberOrder"))
{
?>
<p>PMPro is installed.</p>
<?php
//do we have levels?
if(empty($pmproaw_levels))
{
?>
<p>Once you've <a href="admin.php?page=pmpro-membershiplevels">created some levels in Paid Memberships Pro</a>, you will be able to assign AWeber lists to them here.</p>
<?php
}
else
{
?>
<p>For each level below, choose the lists which should be subscribed to when a new user registers.</p>
<?php
}
}
else
{
//just deactivated or needs to be installed?
if(file_exists(dirname(__FILE__) . "/../paid-memberships-pro/paid-memberships-pro.php"))
{
//just deactivated
?>
<p><a href="plugins.php?plugin_status=inactive">Activate Paid Memberships Pro</a> to add membership functionality to your site and finer control over your AWeber lists.</p>
<?php
}
else
{
//needs to be installed
?>
<p><a href="plugin-install.php?tab=search&type=term&s=paid+memberships+pro&plugin-search-input=Search+Plugins">Install Paid Memberships Pro</a> to add membership functionality to your site and finer control over your AWeber lists.</p>
<?php
}
}
}
//options code
function pmproaw_option_authorization_code()
{
global $pmproaw_exception;
$options = get_option('pmproaw_options');
if(isset($options['authorization_code']))
$authorization_code = $options['authorization_code'];
else
$authorization_code = "";
echo "<textarea name='pmproaw_options[authorization_code]' cols='60' rows='3'>" . esc_textarea($authorization_code) . "</textarea>";
if(empty($authorization_code)){
echo "<div class='updated inline'><p>To get started, you must authorize this website to access your AWeber account.<br /><a target='_blank' href='?page=pmproaw_options&oauth=1'>Click here to authorize this site to access your AWeber account</a>.<br />Then copy the 'authorization code' given into the field above and click 'Save Settings' to continue.</p></div>";
} elseif(!empty($pmproaw_exception)) {
echo "<div class='notice notice-error inline'><p>You must reauthorize the PMPro Aweber App at AWeber.<br /><a target='_blank' href='?page=pmproaw_options&oauth=1'>Click here to authorize this site to access your AWeber account</a>.<br />Then copy the new 'authorization code' given and replace it into the field above and click 'Save Settings' to continue.</p></div>";
}
//show an extra save settings button sometimes
if(empty($authorization_code) || !empty($pmproaw_exception)) {
?>
<input type="submit" name="submit" class="button-primary" value="Save Settings">
<?php
}
}
function pmproaw_option_consumer_key()
{
$options = get_option('pmproaw_options');
if(isset($options['consumer_key']))
$consumer_key = $options['consumer_key'];
else
$consumer_key = "";
?>
<input id='pmproaw_consumer_key' name='pmproaw_options[consumer_key]' size='80' type='text' value='<?php echo esc_attr($consumer_key); ?>' readonly='readonly'/>
<br /><small>This value is automatically generated when the Authorization Code above is set.</small>
<script>
//hide this row in the table
jQuery('#pmproaw_consumer_key').closest('tr').hide();
</script>
<?php
}
function pmproaw_option_consumer_secret()
{
$options = get_option('pmproaw_options');
if(isset($options['consumer_secret']))
$consumer_secret = $options['consumer_secret'];
else
$consumer_secret = "";
?>
<input id='pmproaw_consumer_secret' name='pmproaw_options[consumer_secret]' size='80' type='text' value='<?php echo esc_attr($consumer_secret); ?>' readonly='readonly'/>
<br /><small>This value is automatically generated when the Authorization Code above is set.</small>
<script>
//hide this row in the table
jQuery('#pmproaw_consumer_secret').closest('tr').hide();
</script>
<?php
}
function pmproaw_option_access_key()
{
$options = get_option('pmproaw_options');
if(isset($options['access_key']))
$access_key = $options['access_key'];
else
$access_key = "";
?>
<input id='pmproaw_access_key' name='pmproaw_options[access_key]' size='80' type='text' value='<?php echo esc_attr($access_key); ?>' readonly='readonly'/>
<br /><small>This value is automatically generated when the Authorization Code above is set.</small>
<script>
//hide this row in the table
jQuery('#pmproaw_access_key').closest('tr').hide();
</script>
<?php
}
function pmproaw_option_access_secret()
{
$options = get_option('pmproaw_options');
if(isset($options['access_secret']))
$access_secret = $options['access_secret'];
else
$access_secret = "";
?>
<input id='pmproaw_access_secret' name='pmproaw_options[access_secret]' size='80' type='text' value='<?php echo esc_attr($access_secret); ?>' readonly='readonly'/>
<br /><small>This value is automatically generated when the Authorization Code above is set.</small>
<script>
//hide this row in the table
jQuery('#pmproaw_access_secret').closest('tr').hide();
</script>
<?php
}
function pmproaw_option_users_lists()
{
global $pmproaw_lists;
$options = get_option('pmproaw_options');
if(isset($options['users_lists']) && is_array($options['users_lists']))
$selected_lists = $options['users_lists'];
else
$selected_lists = array();
if(!empty($pmproaw_lists))
{
echo "<select multiple='yes' name=\"pmproaw_options[users_lists][]\">";
foreach($pmproaw_lists as $list)
{
echo "<option value='" . $list['id'] . "' ";
if(in_array($list['id'], $selected_lists))
echo "selected='selected'";
echo ">" . $list['name'] . "</option>";
}
echo "</select>";
}
else
{
echo "No lists found.";
}
}
function pmproaw_option_double_opt_in()
{
$options = get_option('pmproaw_options');
?>
<select name="pmproaw_options[double_opt_in]">
<option value="0" <?php selected($options['double_opt_in'], 0);?>>No</option>
<option value="1" <?php selected($options['double_opt_in'], 1);?>>Yes</option>
</select>
<?php
}
function pmproaw_option_memberships_lists($level)
{
global $pmproaw_lists;
$options = get_option('pmproaw_options');
$level = $level[0]; //WP stores this in the first element of an array
if(isset($options['level_' . $level->id . '_lists']) && is_array($options['level_' . $level->id . '_lists']))
$selected_lists = $options['level_' . $level->id . '_lists'];
else
$selected_lists = array();
if(!empty($pmproaw_lists))
{
echo "<select multiple='yes' name=\"pmproaw_options[level_" . $level->id . "_lists][]\">";
foreach($pmproaw_lists as $list)
{
echo "<option value='" . $list['id'] . "' ";
if(in_array($list['id'], $selected_lists))
echo "selected='selected'";
echo ">" . $list['name'] . "</option>";
}
echo "</select>";
}
else
{
echo "No lists found.";
}
}
// validate our options
function pmproaw_options_validate($input)
{
$pmproaw_levels = pmproaw_getPMProLevels();
$options = get_option('pmproaw_options');
//api key
$newinput['authorization_code'] = trim(preg_replace("[^a-zA-Z0-9\-\|]", "", $input['authorization_code']));
$newinput['consumer_key'] = trim(preg_replace("[^a-zA-Z0-9\-]", "", $input['consumer_key']));
$newinput['consumer_secret'] = trim(preg_replace("[^a-zA-Z0-9\-]", "", $input['consumer_secret']));
$newinput['access_key'] = trim(preg_replace("[^a-zA-Z0-9\-]", "", $input['access_key']));
$newinput['access_secret'] = trim(preg_replace("[^a-zA-Z0-9\-]", "", $input['access_secret']));
//clear access key and secret if authorization code was updated
if($options['authorization_code'] != $newinput['authorization_code']) {
$newinput['consumer_key'] = '';
$newinput['consumer_secret'] = '';
$newinput['access_key'] = '';
$newinput['access_secret'] = '';
$reauthorizing = true;
} else {
$reauthorizing = false;
}
//other settings
//$newinput['double_opt_in'] = intval($input['double_opt_in']);
$newinput['unsubscribe'] = preg_replace("[^a-zA-Z0-9\-]", "", $input['unsubscribe']);
//user lists
if( $reauthorizing ) {
//no lists, probably needs to authenticate, keep old settings
$newinput['users_lists'] = $options['users_lists'];
} else {
//grab data from input
if(!empty($input['users_lists']) && is_array($input['users_lists']))
{
$count = count($input['users_lists']);
for($i = 0; $i < $count; $i++)
$newinput['users_lists'][] = trim(preg_replace("[^a-zA-Z0-9\-]", "", $input['users_lists'][$i]));
}
}
//membership lists
if(!empty($pmproaw_levels)) {
foreach($pmproaw_levels as $level) {
if( $reauthorizing ) {
//no lists, probably needs to authenticate, keep old settings
$newinput['level_' . $level->id . '_lists'] = $options['level_' . $level->id . '_lists'];
} else {
//grab data from input
if(!empty($input['level_' . $level->id . '_lists']) && is_array($input['level_' . $level->id . '_lists'])) {
$count = count($input['level_' . $level->id . '_lists']);
for($i = 0; $i < $count; $i++)
$newinput['level_' . $level->id . '_lists'][] = trim(preg_replace("[^a-zA-Z0-9\-]", "", $input['level_' . $level->id . '_lists'][$i]));
}
}
}
}
return $newinput;
}
// add the admin options page
function pmproaw_admin_add_page()
{
add_options_page('PMPro AWeber Options', 'PMPro AWeber', 'manage_options', 'pmproaw_options', 'pmproaw_options_page');
}
add_action('admin_menu', 'pmproaw_admin_add_page');
//oauth
function pmproaw_init_oauth()
{
if(is_admin())
{
if(!empty($_REQUEST['page']) && $_REQUEST['page'] == "pmproaw_options" && !empty($_REQUEST['oauth']))
{
// Redirect your user to the distributed app authorization URL
$authorizationURL = "https://auth.aweber.com/1.0/oauth/authorize_app/" . PMPROAW_APPID;
header("Location: $authorizationURL");
exit();
}
}
}
add_action("init", "pmproaw_init_oauth");
//show exception error
function pmproaw_printAWeberAPIException($exc) {
//don't print unless we're on the settings page
if(!current_user_can('manage_options') || (is_admin() && !empty($_REQUEST['page']) && $_REQUEST['page'] != 'pmproaw_options'))
return;
print "<div class='notice notice-error'><h3>AWeberAPIException:</h3>";
print " <li> Type: $exc->type <br>";
print " <li> Msg : $exc->message <br>";
print " <li> Docs: $exc->documentation_url <br>";
print "</div>";
}
//html for options page
function pmproaw_options_page()
{
global $pmproaw_lists;
//check for a valid API key and get lists
$options = get_option("pmproaw_options");
//get token if needed
if(!empty($options['authorization_code']) && (empty($options['consumer_key']) || empty($options['consumer_secret']) || empty($options['access_key']) || empty($options['access_secret'])))
{
try {
# set $authorization_code to the code that is given to you from
# https://auth.aweber.com/1.0/oauth/authorize_app/YOUR_APP_ID
$auth = AWeberAPI::getDataFromAweberID($options['authorization_code']);
list($consumerKey, $consumerSecret, $accessKey, $accessSecret) = $auth;
# Store the Consumer key/secret, as well as the AccessToken key/secret
# in your app, these are the credentials you need to access the API.
$options['consumer_key'] = $consumerKey;
$options['consumer_secret'] = $consumerSecret;
$options['access_key'] = $accessKey;
$options['access_secret'] = $accessSecret;
update_option('pmproaw_options', $options);
}
catch(AWeberAPIException $exc) {
global $pmproaw_exception;
$pmproaw_exception = $exc;
pmproaw_printAWeberAPIException($exc);
}
}
//get lists
if(!empty($options['consumer_key']) && !empty($options['consumer_secret']) && !empty($options['access_key']) && !empty($options['access_secret']))
{
//get aweber account or fail
$account = pmproaw_getAccount();
if(!empty($account)) {
try {
if($account->lists->data['total_size'] > 99) {
//need to grab lists this way to get > 100
$pmproaw_lists = array();
foreach($account->lists as $somelist) {
$pmproaw_lists[] = $somelist->data;
}
} else {
//just get the entries array which has first 100
$pmproaw_lists = $account->lists->data['entries'];
}
$all_lists = array();
//save all lists in an option
$i = 0;
foreach ( $pmproaw_lists as $list ) {
$all_lists[$i]['id'] = $list['id'];
$all_lists[$i]['account_id'] = $account->id;
$all_lists[$i]['name'] = $list['name'];
$i++;
}
/** Save all of our new data */
update_option( "pmproaw_all_lists", $all_lists);
} catch(AWeberAPIException $exc) {
global $pmproaw_exception;
$pmproaw_exception = $exc;
pmproaw_printAWeberAPIException($exc);
}
} else {
global $pmproaw_exception;
if(!empty($pmproaw_exception)) {
pmproaw_printAWeberAPIException($pmproaw_exception);
}
}
}
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br></div>
<h2>PMPro AWeber Integration Options</h2>
<?php if(!empty($msg)) { ?>
<div class="message <?php echo $msgt; ?>"><p><?php echo $msg; ?></p></div>
<?php } ?>
<form action="options.php" method="post">
<p>This plugin will integrate your site with AWeber. You can choose one or more AWeber lists to have users subscribed to when they signup for your site.</p>
<p>If you have <a href="http://www.paidmembershipspro.com">Paid Memberships Pro</a> installed, you can also choose one or more AWeber lists to have members subscribed to for each membership level.</p>
<p>Don't have a AWeber account? <a href="http://www.aweber.com/?422729" target="_blank">Get one here</a>.</p>
<?php
settings_fields('pmproaw_options');
do_settings_sections('pmproaw_options');
?>
<p><br /></p>
<div class="bottom-buttons">
<input type="hidden" name="pmprot_options[set]" value="1" />
<input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e('Save Settings'); ?>">
</div>
</form>
</div>
<?php
}
/*
Function to add links to the plugin action links
*/
function pmproaw_add_action_links($links) {
$new_links = array(
'<a href="' . get_admin_url(NULL, 'options-general.php?page=pmproaw_options') . '">Settings</a>',
);
return array_merge($new_links, $links);
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'pmproaw_add_action_links');
/*
Function to add links to the plugin row meta
*/
function pmproaw_plugin_row_meta($links, $file) {
if(strpos($file, 'pmpro-aweber.php') !== false)
{
$new_links = array(
'<a href="' . esc_url('https://www.paidmembershipspro.com/add-ons/pmpro-aweber-integration/') . '" title="' . esc_attr( __( 'View Documentation', 'pmpro' ) ) . '">' . __( 'Docs', 'pmpro' ) . '</a>',
'<a href="' . esc_url('https://www.paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro' ) ) . '">' . __( 'Support', 'pmpro' ) . '</a>',
);
$links = array_merge($links, $new_links);
}
return $links;
}
add_filter('plugin_row_meta', 'pmproaw_plugin_row_meta', 10, 2);
function pmproaw_option_unsubscribe()
{
$options = get_option('pmproaw_options');
?>
<select name="pmproaw_options[unsubscribe]">
<option value="0" <?php selected($options['unsubscribe'], 0);?>>No</option>
<option value="1" <?php selected($options['unsubscribe'], 1);?>>Yes (Only old level lists.)</option>
<option value="all" <?php selected($options['unsubscribe'], "all");?>>Yes (All other lists.)</option>
</select>
<small>Recommended: Yes. However, if you manage multiple lists in Aweber and have users subscribe outside of WordPress, you may want to choose No so contacts aren't unsubscribed from other lists when they register on your site.</small>
<?php
}