-
Notifications
You must be signed in to change notification settings - Fork 48
/
mailchimp.php
1601 lines (1415 loc) · 50.7 KB
/
mailchimp.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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: MailChimp
Plugin URI: http://www.mailchimp.com/plugins/mailchimp-wordpress-plugin/
Description: The MailChimp plugin allows you to quickly and easily add a signup form for your MailChimp list.
Version: 1.4.3
Author: MailChimp and Crowd Favorite
Author URI: http://mailchimp.com/api/
*/
/* Copyright 2008-2012 MailChimp.com (email : [email protected])
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Version constant for easy CSS refreshes
define('MCSF_VER', '1.4.3');
// What's our permission (capability) threshold
define('MCSF_CAP_THRESHOLD', 'manage_options');
// If Developer mode not defined, make it false
if (! defined('MAILCHIMP_DEV_MODE') ) {
define('MAILCHIMP_DEV_MODE', false);
}
// Define our location constants, both MCSF_DIR and MCSF_URL
mailchimpSF_where_am_i();
// Get our MailChimp API class in scope
if (!class_exists('Sopresto_MailChimp')) {
require_once(MCSF_DIR.'lib/sopresto/sopresto.php');
}
// includes the widget code so it can be easily called either normally or via ajax
include_once('mailchimp_widget.php');
// includes the backwards compatibility functions
include_once('mailchimp_compat.php');
/**
* Do the following plugin setup steps here
*
* Internationalization
* Resource (JS & CSS) enqueuing
*
* @return void
*/
function mailchimpSF_plugin_init() {
// Internationalize the plugin
$textdomain = 'mailchimp_i18n';
$locale = apply_filters( 'plugin_locale', get_locale(), $textdomain);
load_textdomain('mailchimp_i18n', MCSF_LANG_DIR.$textdomain.'-'.$locale.'.mo');
// Bring in our appropriate JS and CSS resources
mailchimpSF_load_resources();
}
add_action( 'init', 'mailchimpSF_plugin_init' );
/**
* Add the settings link to the MailChimp plugin row
*
* @param array $links - Links for the plugin
* @return array - Links
*/
function mailchimpSD_plugin_action_links($links) {
$settings_page = add_query_arg(array('page' => 'mailchimpSF_options'), admin_url('options-general.php'));
$settings_link = '<a href="'.esc_url($settings_page).'">'.__('Settings', 'mailchimp_i18n' ).'</a>';
array_unshift($links, $settings_link);
return $links;
}
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'mailchimpSD_plugin_action_links', 10, 1);
/**
* Loads the appropriate JS and CSS resources depending on
* settings and context (admin or not)
*
* @return void
*/
function mailchimpSF_load_resources() {
// JS
if ( get_option( 'mc_use_javascript' ) == 'on' ) {
if ( ! is_admin() ) {
wp_enqueue_script( 'jquery_scrollto', MCSF_URL.'js/scrollTo.js', array( 'jquery' ), MCSF_VER );
wp_enqueue_script( 'mailchimpSF_main_js', MCSF_URL.'js/mailchimp.js', array( 'jquery' , 'jquery-form' ), MCSF_VER );
// some javascript to get ajax version submitting to the proper location
global $wp_scripts;
$wp_scripts->localize( 'mailchimpSF_main_js', 'mailchimpSF', array(
'ajax_url' => trailingslashit( home_url() ),
));
}
}
if ( get_option( 'mc_use_datepicker' ) == 'on' && ! is_admin() ) {
// Datepicker theme
wp_enqueue_style( 'flick', MCSF_URL . 'css/flick/flick.css' );
// Datepicker JS
wp_enqueue_script( 'datepicker', MCSF_URL . 'js/datepicker.js', array( 'jquery','jquery-ui-core' ) );
}
wp_enqueue_style( 'mailchimpSF_main_css', home_url( '?mcsf_action=main_css&ver='.MCSF_VER ) );
wp_enqueue_style( 'mailchimpSF_ie_css', MCSF_URL.'css/ie.css');
global $wp_styles;
$wp_styles->add_data( 'mailchimpSF_ie_css', 'conditional', 'IE' );
}
/**
* Loads resources for the MailChimp admin page
*
* @return void
*/
function mc_admin_page_load_resources() {
wp_enqueue_style('mailchimpSF_admin_css', MCSF_URL.'css/admin.css');
wp_enqueue_script('mailchimpSF_admin_js', MCSF_URL.'js/admin.js');
}
add_action('load-settings_page_mailchimpSF_options', 'mc_admin_page_load_resources');
/**
* Loads jQuery Datepicker for the date-pick class
**/
function mc_datepicker_load() {
?>
<script type="text/javascript">
jQuery(function($) {
$('.date-pick').each(function() {
var format = $(this).data('format') || 'mm/dd/yyyy';
format = format.replace(/yyyy/i, 'yy');
$(this).datepicker({
autoFocusNextInput: true,
constrainInput: false,
changeMonth: true,
changeYear: true,
beforeShow: function(input, inst) { $('#ui-datepicker-div').addClass('show'); },
dateFormat: format.toLowerCase(),
});
});
d = new Date();
$('.birthdate-pick').each(function() {
var format = $(this).data('format') || 'mm/dd';
format = format.replace(/yyyy/i, 'yy');
$(this).datepicker({
autoFocusNextInput: true,
constrainInput: false,
changeMonth: true,
changeYear: false,
minDate: new Date(d.getFullYear(), 1-1, 1),
maxDate: new Date(d.getFullYear(), 12-1, 31),
beforeShow: function(input, inst) { $('#ui-datepicker-div').removeClass('show'); },
dateFormat: format.toLowerCase(),
});
});
});
</script>
<?php
}
if (get_option('mc_use_datepicker') == 'on' && !is_admin()) {
add_action('wp_head', 'mc_datepicker_load');
}
/**
* Handles requests that as light-weight a load as possible.
* typically, JS or CSS
**/
function mailchimpSF_early_request_handler() {
if (isset($_GET['mcsf_action'])) {
switch ($_GET['mcsf_action']) {
case 'main_css':
header("Content-type: text/css");
mailchimpSF_main_css();
exit;
case 'authorize':
mailchimpSF_authorize();
break;
case 'authorized':
mailchimpSF_authorized();
break;
}
}
}
add_action('init', 'mailchimpSF_early_request_handler', 0);
/**
* Outputs the front-end CSS. This checks several options, so it
* was best to put it in a Request-handled script, as opposed to
* a static file.
*/
function mailchimpSF_main_css() {
?>
.mc_error_msg {
color: red;
margin-bottom: 1.0em;
}
.mc_success_msg {
color: green;
margin-bottom: 1.0em;
}
.mc_merge_var{
padding:0;
margin:0;
}
<?php
// If we're utilizing custom styles
if (get_option('mc_custom_style')=='on'){
?>
#mc_signup_form {
padding:5px;
border-width: <?php echo get_option('mc_form_border_width'); ?>px;
border-style: <?php echo (get_option('mc_form_border_width')==0) ? 'none' : 'solid'; ?>;
border-color: #<?php echo get_option('mc_form_border_color'); ?>;
color: #<?php echo get_option('mc_form_text_color'); ?>;
background-color: #<?php echo get_option('mc_form_background'); ?>;
}
.mc_custom_border_hdr {
border-width: <?php echo get_option('mc_header_border_width'); ?>px;
border-style: <?php echo (get_option('mc_header_border_width')==0) ? 'none' : 'solid'; ?>;
border-color: #<?php echo get_option('mc_header_border_color'); ?>;
color: #<?php echo get_option('mc_header_text_color'); ?>;
background-color: #<?php echo get_option('mc_header_background'); ?>;
<!-- font-size: 1.2em;-->
padding:5px 10px;
width: 100%;
}
<?php
}
?>
#mc_signup_container {}
#mc_signup_form {}
#mc_signup_form .mc_var_label {}
#mc_signup_form .mc_input {}
#mc-indicates-required {
width:100%;
}
#mc_display_rewards {}
.mc_interests_header {
font-weight:bold;
}
div.mc_interest{
width:100%;
}
#mc_signup_form input.mc_interest {}
#mc_signup_form select {}
#mc_signup_form label.mc_interest_label {
display:inline;
}
.mc_signup_submit {
text-align:center;
}
ul.mc_list {
list-style-type: none;
}
ul.mc_list li {
font-size: 12px;
}
#ui-datepicker-div .ui-datepicker-year {
display: none;
}
#ui-datepicker-div.show .ui-datepicker-year {
display: inline;
padding-left: 3px
}
<?php
}
/**
* Add our settings page to the admin menu
*
* @return void
*/
function mailchimpSF_add_pages(){
// Add settings page for users who can edit plugins
add_options_page( __( 'MailChimp Setup', 'mailchimp_i18n' ), __( 'MailChimp Setup', 'mailchimp_i18n' ), MCSF_CAP_THRESHOLD, 'mailchimpSF_options', 'mailchimpSF_setup_page');
}
add_action('admin_menu', 'mailchimpSF_add_pages');
function mailchimpSF_request_handler() {
if (isset($_POST['mcsf_action'])) {
switch ($_POST['mcsf_action']) {
case 'logout':
// Check capability & Verify nonce
if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'mc_logout')) {
wp_die('Cheatin’ huh?');
}
// erase auth information
delete_option('mc_sopresto_user');
delete_option('mc_sopresto_public_key');
delete_option('mc_sopresto_secret_key');
break;
case 'reset_list':
// Check capability & Verify nonce
if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'reset_mailchimp_list')) {
wp_die('Cheatin’ huh?');
}
mailchimpSF_reset_list_settings();
break;
case 'change_form_settings':
if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'update_general_form_settings')) {
wp_die('Cheatin’ huh?');
}
// Update the form settings
mailchimpSF_save_general_form_settings();
break;
case 'mc_submit_signup_form':
// Validate nonce
if (!wp_verify_nonce($_POST['_mc_submit_signup_form_nonce'], 'mc_submit_signup_form')) {
wp_die('Cheatin’ huh?');
}
// Attempt the signup
mailchimpSF_signup_submit();
// Do a different action for html vs. js
switch ($_POST['mc_submit_type']) {
case 'html':
/* Allow to fall through. The widget will pick up the
* global message left over from the signup_submit function */
break;
case 'js':
if (!headers_sent()){ //just in case...
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT', true, 200);
}
echo mailchimpSF_global_msg(); // Don't esc_html this, b/c we've already escaped it
exit;
}
}
}
}
add_action('init', 'mailchimpSF_request_handler');
function mailchimpSF_auth_nonce_key($salt = null) {
if (is_null($salt)) {
$salt = mailchimpSF_auth_nonce_salt();
}
return 'social_authentication' . md5( AUTH_KEY . $salt );
}
function mailchimpSF_auth_nonce_salt() {
return md5(microtime().$_SERVER['SERVER_ADDR']);
}
function mailchimpSF_authorize() {
$api = mailchimpSF_get_api( true );
$proxy = apply_filters( 'mailchimp_authorize_url', $api->getApiUrl( 'authorize' ) );
if ( strpos( $proxy, 'socialize-this' ) !== false ) {
$salt = mailchimpSF_auth_nonce_salt();
$id = mailchimpSF_create_nonce( mailchimpSF_auth_nonce_key( $salt ) );
$url = admin_url( 'index.php' );
$args = array(
'mcsf_action' => 'authorized',
'salt' => $salt,
'user_id' => get_current_user_id(),
);
$proxy = add_query_arg( array(
'id' => $id,
'response_url' => urlencode( add_query_arg( $args, $url ) )
), $proxy );
$proxy = apply_filters( 'mailchimp_proxy_url', $proxy );
}
wp_redirect( $proxy );
exit;
}
function mailchimpSF_authorized() {
// User ID on the request? Must be set before nonce comparison
$user_id = stripslashes($_GET['user_id']);
if ($user_id !== null) {
wp_set_current_user($user_id);
}
$nonce = stripslashes($_POST['id']);
$salt = stripslashes($_GET['salt']);
if (mailchimpSF_verify_nonce( $nonce, mailchimpSF_auth_nonce_key( $salt ) ) === false) {
wp_die('Cheatin’ huh?');
}
$response = stripslashes_deep($_POST['response']);
if (!isset($response['keys']) || !isset($response['user'])) {
wp_die('Something went wrong, please try again');
}
update_option('mc_sopresto_user', $response['user']);
update_option('mc_sopresto_dc', $response['dc']);
update_option('mc_sopresto_public_key', $response['keys']['public']);
update_option('mc_sopresto_secret_key', $response['keys']['secret']);
exit;
}
/**
* Upgrades data if it needs to. Checks on admin_init
*
* @return void
*/
function mailchimpSF_upgrade() {
// See if we need an upgrade
if (mailchimpSF_needs_upgrade()) {
// remove password option if it's set (0.5)
// Update interest group data
mailchimpSF_do_upgrade();
}
}
add_action('admin_init', 'mailchimpSF_upgrade');
/**
* Creates new Sopresto API object
*
* @return Sopresto_MailChimp|false
*/
function mailchimpSF_get_api($force = false) {
$public_key = get_option('mc_sopresto_public_key');
$secret_key = get_option('mc_sopresto_secret_key');
if ($public_key && $secret_key || $force) {
return new Sopresto_MailChimp($public_key, $secret_key, '1.3');
}
return false;
}
/**
* Checks to see if we're storing a password, if so, we need
* to upgrade to the API key
*
* @return bool
**/
function mailchimpSF_needs_upgrade() {
$igs = get_option('mc_interest_groups');
if ($igs !== false // we have an option
&& (
empty($igs) || // it can be an empty array (no interest groups)
(is_array($igs) && isset($igs[0]['id'])) // OR it should be a populated array that's well-formed
)) {
return false; // no need to upgrade
}
else {
return true; // yeah, let's do it
}
}
/**
* 1.2.4 -> 1.2.5 - Update to support multiple interest groups
* MCAPIv1.2 -> MCAPIv1.3 - update interest groups
* 2011-02-09 - old password upgrade code deleted as 0.5 is way old
*/
function mailchimpSF_do_upgrade() {
# TODO: reload this
}
/**
* Deletes all mailchimp options
**/
function mailchimpSF_delete_setup() {
delete_option('mc_user_id');
delete_option('mc_sopresto_user');
delete_option('mc_sopresto_public_key');
delete_option('mc_sopresto_secret_key');
delete_option('mc_rewards');
delete_option('mc_use_javascript');
delete_option('mc_use_datepicker');
delete_option('mc_use_unsub_link');
delete_option('mc_list_id');
delete_option('mc_list_name');
$igs = get_option('mc_interest_groups');
if (is_array($igs)) {
foreach ($igs as $ig) {
$opt = 'mc_show_interest_groups_'.$ig['id'];
delete_option($opt);
}
}
delete_option('mc_interest_groups');
$mv = get_option('mc_merge_vars');
if (is_array($mv)){
foreach($mv as $var){
$opt = 'mc_mv_'.$var['tag'];
delete_option($opt);
}
}
delete_option('mc_merge_vars');
}
/**
* Resets the list settings, there's only one list
* that can have settings at a time, so no list_id
* parameter is necessary.
**/
function mailchimpSF_reset_list_settings() {
delete_option('mc_list_id');
delete_option('mc_list_name');
delete_option('mc_merge_vars');
delete_option('mc_interest_groups');
delete_option('mc_use_javascript');
delete_option('mc_use_unsub_link');
delete_option('mc_use_datepicker');
delete_option('mc_header_content');
delete_option('mc_subheader_content');
delete_option('mc_submit_text');
delete_option('mc_custom_style');
delete_option('mc_header_border_width');
delete_option('mc_header_border_color');
delete_option('mc_header_background');
delete_option('mc_header_text_color');
delete_option('mc_form_border_width');
delete_option('mc_form_border_color');
delete_option('mc_form_background');
delete_option('mc_form_text_color');
$msg = '<p class="success_msg">'.esc_html(__('Successfully Reset your List selection... Now you get to pick again!', 'mailchimp_i18n')).'</p>';
mailchimpSF_global_msg($msg);
}
/**
* Gets or sets a global message based on parameter passed to it
*
* @return string/bool depending on get/set
**/
function mailchimpSF_global_msg($msg = null) {
global $mcsf_msgs;
// Make sure we're formed properly
if (!is_array($mcsf_msgs)) {
$mcsf_msgs = array();
}
// See if we're getting
if (is_null($msg)) {
return implode('', $mcsf_msgs);
}
// Must be setting
$mcsf_msgs[] = $msg;
return true;
}
/**
* Sets the default options for the option form
**/
function mailchimpSF_set_form_defaults($list_name = '') {
update_option('mc_header_content',__( 'Sign up for', 'mailchimp_i18n' ).' '.$list_name);
update_option('mc_submit_text',__( 'Subscribe', 'mailchimp_i18n' ));
update_option('mc_use_datepicker', 'on');
update_option('mc_custom_style','off');
update_option('mc_use_javascript','on');
update_option('mc_use_unsub_link','off');
update_option('mc_header_border_width','1');
update_option('mc_header_border_color','E3E3E3');
update_option('mc_header_background','FFFFFF');
update_option('mc_header_text_color','CC6600');
update_option('mc_form_border_width','1');
update_option('mc_form_border_color','E0E0E0');
update_option('mc_form_background','FFFFFF');
update_option('mc_form_text_color','3F3F3f');
}
/**
* Saves the General Form settings on the options page
*
* @return void
**/
function mailchimpSF_save_general_form_settings() {
if (MAILCHIMP_DEV_MODE == false) {
if (isset($_POST['mc_rewards'])){
update_option('mc_rewards', 'on');
$msg = '<p class="success_msg">'.__('Monkey Rewards turned On!', 'mailchimp_i18n').'</p>';
mailchimpSF_global_msg($msg);
} else if (get_option('mc_rewards')!='off') {
update_option('mc_rewards', 'off');
$msg = '<p class="success_msg">'.__('Monkey Rewards turned Off!', 'mailchimp_i18n').'</p>';
mailchimpSF_global_msg($msg);
}
if (isset($_POST['mc_use_javascript'])){
update_option('mc_use_javascript', 'on');
$msg = '<p class="success_msg">'.__('Fancy Javascript submission turned On!', 'mailchimp_i18n').'</p>';
mailchimpSF_global_msg($msg);
} else if (get_option('mc_use_javascript')!='off') {
update_option('mc_use_javascript', 'off');
$msg = '<p class="success_msg">'.__('Fancy Javascript submission turned Off!', 'mailchimp_i18n').'</p>';
mailchimpSF_global_msg($msg);
}
if (isset($_POST['mc_use_datepicker'])){
update_option('mc_use_datepicker', 'on');
$msg = '<p class="success_msg">'.__('Datepicker turned On!', 'mailchimp_i18n').'</p>';
mailchimpSF_global_msg($msg);
} else if (get_option('mc_use_datepicker')!='off') {
update_option('mc_use_datepicker', 'off');
$msg = '<p class="success_msg">'.__('Datepicker turned Off!', 'mailchimp_i18n').'</p>';
mailchimpSF_global_msg($msg);
}
if (isset($_POST['mc_use_unsub_link'])){
update_option('mc_use_unsub_link', 'on');
$msg = '<p class="success_msg">'.__('Unsubscribe link turned On!', 'mailchimp_i18n').'</p>';
mailchimpSF_global_msg($msg);
} else if (get_option('mc_use_unsub_link')!='off') {
update_option('mc_use_unsub_link', 'off');
$msg = '<p class="success_msg">'.__('Unsubscribe link turned Off!', 'mailchimp_i18n').'</p>';
mailchimpSF_global_msg($msg);
}
$content = stripslashes($_POST['mc_header_content']);
$content = str_replace("\r\n","<br/>", $content);
update_option('mc_header_content', $content );
$content = stripslashes($_POST['mc_subheader_content']);
$content = str_replace("\r\n","<br/>", $content);
update_option('mc_subheader_content', $content );
$submit_text = stripslashes($_POST['mc_submit_text']);
$submit_text = str_replace("\r\n","", $submit_text);
update_option('mc_submit_text', $submit_text);
}
// Set Custom Style option
update_option('mc_custom_style', isset($_POST['mc_custom_style']) ? 'on' : 'off');
if (MAILCHIMP_DEV_MODE == false) {
//we told them not to put these things we are replacing in, but let's just make sure they are listening...
$header_keys = array(
'mc_header_border_width' => 'px',
'mc_header_border_color' => '#',
'mc_header_background' => '#',
'mc_header_text_color' => '#',
);
// Prevent WP_DEBUG warnings and updating when they are not set
foreach ( $header_keys as $header_key => $header_replacement) {
if ( isset( $_POST[ $header_key ] ) ) {
update_option( $header_key, str_replace( $header_replacement, '', $_POST[ $header_key ] ) );
}
}
}
update_option('mc_form_border_width',str_replace('px','',$_POST['mc_form_border_width']) );
update_option('mc_form_border_color', str_replace('#','',$_POST['mc_form_border_color']));
update_option('mc_form_background',str_replace('#','',$_POST['mc_form_background']));
update_option('mc_form_text_color', str_replace('#','',$_POST['mc_form_text_color']));
if (MAILCHIMP_DEV_MODE == false) {
$igs = get_option('mc_interest_groups');
if (is_array($igs)) {
foreach($igs as $var){
$opt = 'mc_show_interest_groups_'.$var['id'];
if (isset($_POST[$opt])){
update_option($opt,'on');
} else {
update_option($opt,'off');
}
}
}
$mv = get_option('mc_merge_vars');
if (is_array($mv)) {
foreach($mv as $var){
$opt = 'mc_mv_'.$var['tag'];
if (isset($_POST[$opt]) || $var['req']=='Y'){
update_option($opt,'on');
} else {
update_option($opt,'off');
}
}
}
}
$msg = '<p class="success_msg">'.esc_html(__('Successfully Updated your List Subscribe Form Settings!', 'mailchimp_i18n')).'</p>';
mailchimpSF_global_msg($msg);
}
/**
* Sees if the user changed the list, and updates options accordingly
**/
function mailchimpSF_change_list_if_necessary() {
// Simple permission check before going through all this
if (!current_user_can(MCSF_CAP_THRESHOLD)) { return; }
$api = mailchimpSF_get_api();
if (!$api) { return; }
//we *could* support paging, but few users have that many lists (and shouldn't)
$lists = $api->lists(array(),0,100);
$lists = $lists['data'];
if (is_array($lists) && !empty($lists) && isset($_POST['mc_list_id'])) {
/* If our incoming list ID (the one chosen in the select dropdown)
is in our array of lists, the set it to be the active list */
foreach($lists as $key => $list) {
if ($list['id'] == $_POST['mc_list_id']) {
$list_id = $_POST['mc_list_id'];
$list_name = $list['name'];
$list_key = $key;
}
}
$orig_list = get_option('mc_list_id');
if ( ! empty( $list_id ) ) {
update_option('mc_list_id', $list_id);
update_option('mc_list_name', $list_name);
update_option('mc_email_type_option', $lists[$list_key]['email_type_option']);
// See if the user changed the list
if ($orig_list != $list_id){
// The user changed the list, Reset the Form Defaults
mailchimpSF_set_form_defaults($list_name);
}
// email_type_option
// Grab the merge vars and interest groups
$mv = $api->listMergeVars($list_id);
$igs = $api->listInterestGroupings($list_id);
update_option('mc_merge_vars', $mv);
foreach($mv as $var){
$opt = 'mc_mv_'.$var['tag'];
//turn them all on by default
if ($orig_list != $list_id) {
update_option($opt, 'on' );
}
}
update_option('mc_interest_groups', $igs);
if (is_array($igs)) {
foreach ($igs as $var){
$opt = 'mc_show_interest_groups_'.$var['id'];
//turn them all on by default
if ($orig_list != $list_id) {
update_option($opt, 'on' );
}
}
}
$igs_text = ' ';
if (is_array($igs)) {
$igs_text .= sprintf(__('and %s Sets of Interest Groups', 'mailchimp_i18n'), count($igs));
}
$msg = '<p class="success_msg">'.
sprintf(
__('<b>Success!</b> Loaded and saved the info for %d Merge Variables', 'mailchimp_i18n').$igs_text,
count($mv)
).' '.
__('from your list').' "'.$list_name.'"<br/><br/>'.
__('Now you should either Turn On the MailChimp Widget or change your options below, then turn it on.', 'mailchimp_i18n').'</p>';
mailchimpSF_global_msg($msg);
}
}
}
/**
* Outputs the Settings/Options page
*/
function mailchimpSF_setup_page() {
?>
<div class="wrap">
<h2 class="mailchimp-header">
<span class="header-content"> <?php esc_html_e( 'MailChimp List Setup', 'mailchimp_i18n' ); ?> </span>
</h2>
<?php
// Display Developer mode active.
if (MAILCHIMP_DEV_MODE == true) { ?>
<p class="error_msg">Developer mode active.</p>
<?php }
$user = get_option('mc_sopresto_user');
// If we have an API Key, see if we need to change the lists and its options
mailchimpSF_change_list_if_necessary();
// Display our success/error message(s) if have them
if (mailchimpSF_global_msg() != ''){
// Message has already been html escaped, so we don't want to 2x escape it here
?>
<div id="mc_message" class=""><?php echo mailchimpSF_global_msg(); ?></div>
<?php
}
// If we don't have an API Key, do a login form
if (!$user && MAILCHIMP_DEV_MODE == false) {
?>
<div>
<h3 class="mc-h2"><?php esc_html_e('Log In', 'mailchimp_i18n');?></h3>
<p class="mc-p" style="width: 40%;line-height: 21px;"><?php esc_html_e('To start using the MailChimp plugin, we first need to connect your MailChimp account. Click login below to connect.', 'mailchimp_i18n'); ?></p>
<p class="mc-a">
<?php
echo sprintf(
'%1$s <a href="http://www.mailchimp.com/signup/" target="_blank">%2$s</a>',
esc_html(__("Don't have a MailChimp account?", 'mailchimp_i18n')),
esc_html(__('Try one for Free!', 'mailchimp_i18n'))
);
?>
</p>
<div style="width: 900px;">
<table class="widefat mc-widefat mc-api">
<tr valign="top">
<th scope="row" class="mailchimp-connect"><?php esc_html_e('Connect to MailChimp', 'mailchimp_i18n'); ?></th>
<td>
<a href="<?php echo add_query_arg(array("mcsf_action" => "authorize"), admin_url('index.php')) ?>" class="mailchimp-login">Connect</a>
</td>
</tr>
</table>
</div>
</div>
<br/>
<?php
if ($user && $user['username'] != '') {
?>
<!--<div class="notes_msg">
<strong><?php esc_html_e('Notes', 'mailchimp_i18n'); ?>:</strong>
<ul>
<li><?php esc_html_e('Changing your settings at MailChimp.com may cause this to stop working.', 'mailchimp_i18n'); ?></li>
<li><?php esc_html_e('If you change your login to a different account, the info you have setup below will be erased.', 'mailchimp_i18n'); ?></li>
<li><?php esc_html_e('If any of that happens, no biggie - just reconfigure your login and the items below...', 'mailchimp_i18n'); ?></li>
</ul>
</div>-->
<?php
}
} // End of login form
// Start logout form
elseif (MAILCHIMP_DEV_MODE == false) {
?>
<table style="min-width:400px;" class="mc-user" cellspacing="0">
<tr>
<td><h3><?php esc_html_e('Logged in as', 'mailchimp_i18n');?>: <?php echo esc_html($user['username']); ?></h3>
</td>
<td>
<form method="post" action="options-general.php?page=mailchimpSF_options">
<input type="hidden" name="mcsf_action" value="logout"/>
<input type="submit" name="Submit" value="<?php esc_attr_e('Logout', 'mailchimp_i18n');?>" class="button" />
<?php wp_nonce_field('mc_logout', '_mcsf_nonce_action'); ?>
</form>
</td>
</tr>
</table>
<?php
} // End Logout form
//Just get out if nothing else matters...
$api = mailchimpSF_get_api();
if (!$api && MAILCHIMP_DEV_MODE == false) { return; }
if ($api){
?>
<h3 class="mc-h2"><?php esc_html_e('Your Lists', 'mailchimp_i18n'); ?></h3>
<div>
<p class="mc-p"><?php esc_html_e('Please select the List you wish to create a Signup Form for.', 'mailchimp_i18n'); ?></p>
<p class="mc-list-note"><strong><?php esc_html_e('Note:', 'mailchimp_i18n'); ?></strong> <?php esc_html_e('Updating your list will not cause settings below to be lost. Changing to a new list will.', 'mailchimp_i18n'); ?></p>
<form method="post" action="options-general.php?page=mailchimpSF_options">
<?php
//we *could* support paging, but few users have that many lists (and shouldn't)
$lists = $api->lists(array(),0,100);
$lists = $lists['data'];
if (count($lists) == 0) {
?>
<span class='error_msg'>
<?php
echo sprintf(
esc_html(__("Uh-oh, you don't have any lists defined! Please visit %s, login, and setup a list before using this tool!", 'mailchimp_i18n')),
"<a href='http://www.mailchimp.com/'>MailChimp</a>"
);
?>
</span>
<?php
}
else {
?>
<table style="min-width:400px" class="mc-list-select" cellspacing="0">
<tr class="mc-list-row">
<td>
<select name="mc_list_id" style="min-width:200px;">
<option value=""> — <?php esc_html_e('Select A List','mailchimp_i18n'); ?> — </option>
<?php
foreach ($lists as $list) {
$option = get_option('mc_list_id');
?>
<option value="<?php echo esc_attr($list['id']); ?>"<?php selected($list['id'], $option); ?>><?php echo esc_html($list['name']); ?></option>
<?php
}
?>
</select>
</td>
<td>
<input type="hidden" name="mcsf_action" value="update_mc_list_id" />
<input type="submit" name="Submit" value="<?php esc_attr_e('Update List', 'mailchimp_i18n'); ?>" class="button" />
</td>
</tr>
</table>
<?php
} //end select list
?>
</form>
</div>
<br/>
<?php
}
elseif (MAILCHIMP_DEV_MODE == false) {
//display the selected list...
?>
<p class="submit">
<form method="post" action="options-general.php?page=mailchimpSF_options">
<input type="hidden" name="mcsf_action" value="reset_list" />
<input type="submit" name="reset_list" value="<?php esc_attr_e('Reset List Options and Select again', 'mailchimp_i18n'); ?>" class="button" />
<?php wp_nonce_field('reset_mailchimp_list', '_mcsf_nonce_action'); ?>
</form>
</p>
<h3><?php esc_html_e('Subscribe Form Widget Settings for this List', 'mailchimp_i18n'); ?>:</h3>
<h4><?php esc_html_e('Selected MailChimp List', 'mailchimp_i18n'); ?>: <?php echo esc_html(get_option('mc_list_name')); ?></h4>
<?php
}
//Just get out if nothing else matters...
if (get_option('mc_list_id') == '' && MAILCHIMP_DEV_MODE == false) return;
// The main Settings form
?>
<div>
<form method="post" action="options-general.php?page=mailchimpSF_options">
<div style="width:900px;">
<input type="hidden" name="mcsf_action" value="change_form_settings">
<?php wp_nonce_field('update_general_form_settings', '_mcsf_nonce_action'); ?>
<?php if (MAILCHIMP_DEV_MODE == false) { ?>
<!--<input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button" />-->
<table class="widefat mc-widefat mc-label-options">
<tr><th colspan="2">Content Options</th></tr>
<tr valign="top">
<th scope="row"><?php esc_html_e('Header', 'mailchimp_i18n'); ?></th>
<td>
<textarea name="mc_header_content" rows="2" cols="70"><?php echo esc_html(get_option('mc_header_content')); ?></textarea><br/>
<?php esc_html_e('You can fill this with your own Text, HTML markup (including image links), or Nothing!', 'mailchimp_i18n'); ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e('Sub-header', 'mailchimp_i18n'); ?></th>
<td>
<textarea name="mc_subheader_content" rows="2" cols="70"><?php echo esc_html(get_option('mc_subheader_content')); ?></textarea><br/>
<?php esc_html_e('You can fill this with your own Text, HTML markup (including image links), or Nothing!', 'mailchimp_i18n'); ?>.<br/>
<?php esc_html_e('This will be displayed under the heading and above the form.', 'mailchimp_i18n'); ?>
</td>
</tr>
<tr valign="top" class="last-row">
<th scope="row"><?php esc_html_e('Submit Button', 'mailchimp_i18n'); ?></th>
<td>
<input type="text" name="mc_submit_text" size="70" value="<?php echo esc_attr(get_option('mc_submit_text')); ?>"/>
</td>
</tr>
</table>
<input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button mc-submit" /><br/>
<?php } ?>
<table class="widefat mc-widefat mc-custom-styling">