PluginProbe
Mailchimp List Subscribe Form / 1.4.3
Mailchimp List Subscribe Form v1.4.3
1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 All 55 releases
mailchimp / mailchimp.php

mailchimp.php in Mailchimp List Subscribe Form 1.4.3, at mailchimp.php

1,593 lines 50.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: MailChimp
4 Plugin URI: https://connect.mailchimp.com/integrations/wordpress-list-subscribe-form/
5 Description: The MailChimp plugin allows you to quickly and easily add a signup form for your MailChimp list.
6 Version: 1.4.3
7 Author: MailChimp
8 Author URI: http://mailchimp.com/
9 */
10 /* Copyright 2008-2012 MailChimp.com (email : api@mailchimp.com)
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 // Version constant for easy CSS refreshes
28 define('MCSF_VER', '1.4.2');
29
30 // What's our permission (capability) threshold
31 define('MCSF_CAP_THRESHOLD', 'manage_options');
32
33 // If Developer mode not defined, make it false
34 if (! defined('MAILCHIMP_DEV_MODE') ) {
35 define('MAILCHIMP_DEV_MODE', false);
36 }
37
38 // Define our location constants, both MCSF_DIR and MCSF_URL
39 mailchimpSF_where_am_i();
40
41 // Get our MailChimp API class in scope
42 if (!class_exists('Sopresto_MailChimp')) {
43 require_once(MCSF_DIR.'lib/sopresto/sopresto.php');
44 }
45
46 // includes the widget code so it can be easily called either normally or via ajax
47 include_once('mailchimp_widget.php');
48
49 // includes the backwards compatibility functions
50 include_once('mailchimp_compat.php');
51
52 /**
53 * Do the following plugin setup steps here
54 *
55 * Internationalization
56 * Resource (JS & CSS) enqueuing
57 *
58 * @return void
59 */
60 function mailchimpSF_plugin_init() {
61 // Internationalize the plugin
62 $textdomain = 'mailchimp_i18n';
63 $locale = apply_filters( 'plugin_locale', get_locale(), $textdomain);
64 load_textdomain('mailchimp_i18n', MCSF_LANG_DIR.$textdomain.'-'.$locale.'.mo');
65
66 // Bring in our appropriate JS and CSS resources
67 mailchimpSF_load_resources();
68 }
69 add_action( 'init', 'mailchimpSF_plugin_init' );
70
71
72 /**
73 * Add the settings link to the MailChimp plugin row
74 *
75 * @param array $links - Links for the plugin
76 * @return array - Links
77 */
78 function mailchimpSD_plugin_action_links($links) {
79 $settings_page = add_query_arg(array('page' => 'mailchimpSF_options'), admin_url('options-general.php'));
80 $settings_link = '<a href="'.esc_url($settings_page).'">'.__('Settings', 'mailchimp_i18n' ).'</a>';
81 array_unshift($links, $settings_link);
82 return $links;
83 }
84 add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'mailchimpSD_plugin_action_links', 10, 1);
85
86 /**
87 * Loads the appropriate JS and CSS resources depending on
88 * settings and context (admin or not)
89 *
90 * @return void
91 */
92 function mailchimpSF_load_resources() {
93 // JS
94 if (get_option('mc_use_javascript') == 'on') {
95 if (!is_admin()) {
96 wp_enqueue_script('jquery_scrollto', MCSF_URL.'js/scrollTo.js', array('jquery'), MCSF_VER);
97 wp_enqueue_script('mailchimpSF_main_js', MCSF_URL.'js/mailchimp.js', array('jquery', 'jquery-form'), MCSF_VER);
98 // some javascript to get ajax version submitting to the proper location
99 global $wp_scripts;
100 $wp_scripts->localize('mailchimpSF_main_js', 'mailchimpSF', array(
101 'ajax_url' => trailingslashit(home_url()),
102 ));
103 }
104 }
105
106 if (get_option('mc_use_datepicker') == 'on' && !is_admin()) {
107 // Datepicker theme
108 wp_enqueue_style('flick', MCSF_URL.'/css/flick/flick.css');
109 // Datepicker JS
110 wp_enqueue_script('datepicker', MCSF_URL.'/js/datepicker.js', array('jquery','jquery-ui-core'));
111 }
112
113 wp_enqueue_style('mailchimpSF_main_css', home_url('?mcsf_action=main_css&ver='.MCSF_VER));
114 wp_enqueue_style('mailchimpSF_ie_css', MCSF_URL.'css/ie.css');
115 global $wp_styles;
116 $wp_styles->add_data( 'mailchimpSF_ie_css', 'conditional', 'IE' );
117 }
118
119
120 /**
121 * Loads resources for the MailChimp admin page
122 *
123 * @return void
124 */
125 function mc_admin_page_load_resources() {
126 wp_enqueue_style('mailchimpSF_admin_css', MCSF_URL.'css/admin.css');
127 wp_enqueue_script('mailchimpSF_admin_js', MCSF_URL.'js/admin.js');
128 }
129 add_action('load-settings_page_mailchimpSF_options', 'mc_admin_page_load_resources');
130
131
132 /**
133 * Loads jQuery Datepicker for the date-pick class
134 **/
135 function mc_datepicker_load() {
136 ?>
137 <script type="text/javascript">
138 jQuery(function($) {
139 $('.date-pick').each(function() {
140 var format = $(this).data('format') || 'mm/dd/yyyy';
141 format = format.replace(/yyyy/i, 'yy');
142 $(this).datepicker({
143 autoFocusNextInput: true,
144 constrainInput: false,
145 changeMonth: true,
146 changeYear: true,
147 beforeShow: function(input, inst) { $('#ui-datepicker-div').addClass('show'); },
148 dateFormat: format.toLowerCase(),
149 });
150 });
151 d = new Date();
152 $('.birthdate-pick').each(function() {
153 var format = $(this).data('format') || 'mm/dd';
154 format = format.replace(/yyyy/i, 'yy');
155 $(this).datepicker({
156 autoFocusNextInput: true,
157 constrainInput: false,
158 changeMonth: true,
159 changeYear: false,
160 minDate: new Date(d.getFullYear(), 1-1, 1),
161 maxDate: new Date(d.getFullYear(), 12-1, 31),
162 beforeShow: function(input, inst) { $('#ui-datepicker-div').removeClass('show'); },
163 dateFormat: format.toLowerCase(),
164 });
165
166 });
167
168 });
169 </script>
170 <?php
171 }
172 if (get_option('mc_use_datepicker') == 'on' && !is_admin()) {
173 add_action('wp_head', 'mc_datepicker_load');
174 }
175
176 /**
177 * Handles requests that as light-weight a load as possible.
178 * typically, JS or CSS
179 **/
180 function mailchimpSF_early_request_handler() {
181 if (isset($_GET['mcsf_action'])) {
182 switch ($_GET['mcsf_action']) {
183 case 'main_css':
184 header("Content-type: text/css");
185 mailchimpSF_main_css();
186 exit;
187 case 'authorize':
188 mailchimpSF_authorize();
189 break;
190 case 'authorized':
191 mailchimpSF_authorized();
192 break;
193 }
194 }
195 }
196 add_action('init', 'mailchimpSF_early_request_handler', 0);
197
198 /**
199 * Outputs the front-end CSS. This checks several options, so it
200 * was best to put it in a Request-handled script, as opposed to
201 * a static file.
202 */
203 function mailchimpSF_main_css() {
204 ?>
205 .mc_error_msg {
206 color: red;
207 margin-bottom: 1.0em;
208 }
209 .mc_success_msg {
210 color: green;
211 margin-bottom: 1.0em;
212 }
213 .mc_merge_var{
214 padding:0;
215 margin:0;
216 }
217 <?php
218 // If we're utilizing custom styles
219 if (get_option('mc_custom_style')=='on'){
220 ?>
221 #mc_signup_form {
222 padding:5px;
223 border-width: <?php echo get_option('mc_form_border_width'); ?>px;
224 border-style: <?php echo (get_option('mc_form_border_width')==0) ? 'none' : 'solid'; ?>;
225 border-color: #<?php echo get_option('mc_form_border_color'); ?>;
226 color: #<?php echo get_option('mc_form_text_color'); ?>;
227 background-color: #<?php echo get_option('mc_form_background'); ?>;
228 }
229
230
231 .mc_custom_border_hdr {
232 border-width: <?php echo get_option('mc_header_border_width'); ?>px;
233 border-style: <?php echo (get_option('mc_header_border_width')==0) ? 'none' : 'solid'; ?>;
234 border-color: #<?php echo get_option('mc_header_border_color'); ?>;
235 color: #<?php echo get_option('mc_header_text_color'); ?>;
236 background-color: #<?php echo get_option('mc_header_background'); ?>;
237 <!-- font-size: 1.2em;-->
238 padding:5px 10px;
239 width: 100%;
240 }
241 <?php
242 }
243 ?>
244 #mc_signup_container {}
245 #mc_signup_form {}
246 #mc_signup_form .mc_var_label {}
247 #mc_signup_form .mc_input {}
248 #mc-indicates-required {
249 width:100%;
250 }
251 #mc_display_rewards {}
252 .mc_interests_header {
253 font-weight:bold;
254 }
255 div.mc_interest{
256 width:100%;
257 }
258 #mc_signup_form input.mc_interest {}
259 #mc_signup_form select {}
260 #mc_signup_form label.mc_interest_label {
261 display:inline;
262 }
263 .mc_signup_submit {
264 text-align:center;
265 }
266 ul.mc_list {
267 list-style-type: none;
268 }
269 ul.mc_list li {
270 font-size: 12px;
271 }
272 #ui-datepicker-div .ui-datepicker-year {
273 display: none;
274 }
275 #ui-datepicker-div.show .ui-datepicker-year {
276 display: inline;
277 padding-left: 3px
278 }
279 <?php
280 }
281
282
283 /**
284 * Add our settings page to the admin menu
285 *
286 * @return void
287 */
288 function mailchimpSF_add_pages(){
289 // Add settings page for users who can edit plugins
290 add_options_page( __( 'MailChimp Setup', 'mailchimp_i18n' ), __( 'MailChimp Setup', 'mailchimp_i18n' ), MCSF_CAP_THRESHOLD, 'mailchimpSF_options', 'mailchimpSF_setup_page');
291 }
292 add_action('admin_menu', 'mailchimpSF_add_pages');
293
294 function mailchimpSF_request_handler() {
295 if (isset($_POST['mcsf_action'])) {
296 switch ($_POST['mcsf_action']) {
297 case 'logout':
298 // Check capability & Verify nonce
299 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'mc_logout')) {
300 wp_die('Cheatin&rsquo; huh?');
301 }
302
303 // erase auth information
304 delete_option('mc_sopresto_user');
305 delete_option('mc_sopresto_public_key');
306 delete_option('mc_sopresto_secret_key');
307 break;
308 case 'reset_list':
309 // Check capability & Verify nonce
310 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'reset_mailchimp_list')) {
311 wp_die('Cheatin&rsquo; huh?');
312 }
313
314 mailchimpSF_reset_list_settings();
315 break;
316 case 'change_form_settings':
317 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'update_general_form_settings')) {
318 wp_die('Cheatin&rsquo; huh?');
319 }
320
321 // Update the form settings
322 mailchimpSF_save_general_form_settings();
323 break;
324 case 'mc_submit_signup_form':
325 // Validate nonce
326 if (!wp_verify_nonce($_POST['_mc_submit_signup_form_nonce'], 'mc_submit_signup_form')) {
327 wp_die('Cheatin&rsquo; huh?');
328 }
329
330 // Attempt the signup
331 mailchimpSF_signup_submit();
332
333 // Do a different action for html vs. js
334 switch ($_POST['mc_submit_type']) {
335 case 'html':
336 /* Allow to fall through. The widget will pick up the
337 * global message left over from the signup_submit function */
338 break;
339 case 'js':
340 if (!headers_sent()){ //just in case...
341 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT', true, 200);
342 }
343 echo mailchimpSF_global_msg(); // Don't esc_html this, b/c we've already escaped it
344 exit;
345 }
346 }
347 }
348 }
349 add_action('init', 'mailchimpSF_request_handler');
350
351
352 function mailchimpSF_auth_nonce_key($salt = null) {
353 if (is_null($salt)) {
354 $salt = mailchimpSF_auth_nonce_salt();
355 }
356 return 'social_authentication' . md5( AUTH_KEY . $salt );
357 }
358
359 function mailchimpSF_auth_nonce_salt() {
360 return md5(microtime().$_SERVER['SERVER_ADDR']);
361 }
362
363 function mailchimpSF_authorize() {
364 $api = mailchimpSF_get_api(true);
365 $proxy = apply_filters('mailchimp_authorize_url', $api->getApiUrl('authorize'));
366 if (strpos($proxy, 'socialize-this') !== false) {
367 $salt = mailchimpSF_auth_nonce_salt();
368 $id = mailchimpSF_create_nonce( mailchimpSF_auth_nonce_key( $salt ) );
369
370 $url = home_url('index.php');
371 $args = array(
372 'mcsf_action' => 'authorized',
373 'salt' => $salt,
374 'user_id' => get_current_user_id(),
375 );
376
377 $proxy = add_query_arg(array(
378 'id' => $id,
379 'response_url' => urlencode(add_query_arg($args, $url))
380 ), $proxy);
381
382 $proxy = apply_filters('mailchimp_proxy_url', $proxy);
383 }
384
385 wp_redirect($proxy);
386 exit;
387 }
388
389 function mailchimpSF_authorized() {
390 // User ID on the request? Must be set before nonce comparison
391 $user_id = stripslashes($_GET['user_id']);
392 if ($user_id !== null) {
393 wp_set_current_user($user_id);
394 }
395
396 $nonce = stripslashes($_POST['id']);
397 $salt = stripslashes($_GET['salt']);
398
399 if (mailchimpSF_verify_nonce( $nonce, mailchimpSF_auth_nonce_key( $salt ) ) === false) {
400 wp_die('Cheatin&rsquo; huh?');
401 }
402
403 $response = stripslashes_deep($_POST['response']);
404
405 if (!isset($response['keys']) || !isset($response['user'])) {
406 wp_die('Something went wrong, please try again');
407 }
408
409 update_option('mc_sopresto_user', $response['user']);
410 update_option('mc_sopresto_dc', $response['dc']);
411 update_option('mc_sopresto_public_key', $response['keys']['public']);
412 update_option('mc_sopresto_secret_key', $response['keys']['secret']);
413 exit;
414 }
415
416 /**
417 * Upgrades data if it needs to. Checks on admin_init
418 *
419 * @return void
420 */
421 function mailchimpSF_upgrade() {
422 // See if we need an upgrade
423 if (mailchimpSF_needs_upgrade()) {
424 // remove password option if it's set (0.5)
425 // Update interest group data
426 mailchimpSF_do_upgrade();
427 }
428 }
429 add_action('admin_init', 'mailchimpSF_upgrade');
430
431 /**
432 * Creates new Sopresto API object
433 *
434 * @return Sopresto_MailChimp|false
435 */
436 function mailchimpSF_get_api($force = false) {
437 $public_key = get_option('mc_sopresto_public_key');
438 $secret_key = get_option('mc_sopresto_secret_key');
439
440 if ($public_key && $secret_key || $force) {
441 return new Sopresto_MailChimp($public_key, $secret_key, '1.3');
442 }
443
444 return false;
445 }
446
447 /**
448 * Checks to see if we're storing a password, if so, we need
449 * to upgrade to the API key
450 *
451 * @return bool
452 **/
453 function mailchimpSF_needs_upgrade() {
454 $igs = get_option('mc_interest_groups');
455
456 if ($igs !== false // we have an option
457 && (
458 empty($igs) || // it can be an empty array (no interest groups)
459 (is_array($igs) && isset($igs[0]['id'])) // OR it should be a populated array that's well-formed
460 )) {
461 return false; // no need to upgrade
462 }
463 else {
464 return true; // yeah, let's do it
465 }
466 }
467
468 /**
469 * 1.2.4 -> 1.2.5 - Update to support multiple interest groups
470 * MCAPIv1.2 -> MCAPIv1.3 - update interest groups
471 * 2011-02-09 - old password upgrade code deleted as 0.5 is way old
472 */
473 function mailchimpSF_do_upgrade() {
474 # TODO: reload this
475 }
476
477 /**
478 * Deletes all mailchimp options
479 **/
480 function mailchimpSF_delete_setup() {
481 delete_option('mc_user_id');
482 delete_option('mc_sopresto_user');
483 delete_option('mc_sopresto_public_key');
484 delete_option('mc_sopresto_secret_key');
485 delete_option('mc_rewards');
486 delete_option('mc_use_javascript');
487 delete_option('mc_use_datepicker');
488 delete_option('mc_use_unsub_link');
489 delete_option('mc_list_id');
490 delete_option('mc_list_name');
491 $igs = get_option('mc_interest_groups');
492 if (is_array($igs)) {
493 foreach ($igs as $ig) {
494 $opt = 'mc_show_interest_groups_'.$ig['id'];
495 delete_option($opt);
496 }
497 }
498 delete_option('mc_interest_groups');
499 $mv = get_option('mc_merge_vars');
500 if (is_array($mv)){
501 foreach($mv as $var){
502 $opt = 'mc_mv_'.$var['tag'];
503 delete_option($opt);
504 }
505 }
506 delete_option('mc_merge_vars');
507 }
508
509 /**
510 * Resets the list settings, there's only one list
511 * that can have settings at a time, so no list_id
512 * parameter is necessary.
513 **/
514 function mailchimpSF_reset_list_settings() {
515
516 delete_option('mc_list_id');
517 delete_option('mc_list_name');
518 delete_option('mc_merge_vars');
519 delete_option('mc_interest_groups');
520
521 delete_option('mc_use_javascript');
522 delete_option('mc_use_unsub_link');
523 delete_option('mc_use_datepicker');
524
525 delete_option('mc_header_content');
526 delete_option('mc_subheader_content');
527 delete_option('mc_submit_text');
528
529 delete_option('mc_custom_style');
530
531 delete_option('mc_header_border_width');
532 delete_option('mc_header_border_color');
533 delete_option('mc_header_background');
534 delete_option('mc_header_text_color');
535
536 delete_option('mc_form_border_width');
537 delete_option('mc_form_border_color');
538 delete_option('mc_form_background');
539 delete_option('mc_form_text_color');
540
541 $msg = '<p class="success_msg">'.esc_html(__('Successfully Reset your List selection... Now you get to pick again!', 'mailchimp_i18n')).'</p>';
542 mailchimpSF_global_msg($msg);
543 }
544
545 /**
546 * Gets or sets a global message based on parameter passed to it
547 *
548 * @return string/bool depending on get/set
549 **/
550 function mailchimpSF_global_msg($msg = null) {
551 global $mcsf_msgs;
552
553 // Make sure we're formed properly
554 if (!is_array($mcsf_msgs)) {
555 $mcsf_msgs = array();
556 }
557
558 // See if we're getting
559 if (is_null($msg)) {
560 return implode('', $mcsf_msgs);
561 }
562
563 // Must be setting
564 $mcsf_msgs[] = $msg;
565 return true;
566 }
567
568 /**
569 * Sets the default options for the option form
570 **/
571 function mailchimpSF_set_form_defaults($list_name = '') {
572 update_option('mc_header_content',__( 'Sign up for', 'mailchimp_i18n' ).' '.$list_name);
573 update_option('mc_submit_text',__( 'Subscribe', 'mailchimp_i18n' ));
574
575 update_option('mc_use_datepicker', 'on');
576 update_option('mc_custom_style','off');
577 update_option('mc_use_javascript','on');
578 update_option('mc_use_unsub_link','off');
579 update_option('mc_header_border_width','1');
580 update_option('mc_header_border_color','E3E3E3');
581 update_option('mc_header_background','FFFFFF');
582 update_option('mc_header_text_color','CC6600');
583
584 update_option('mc_form_border_width','1');
585 update_option('mc_form_border_color','E0E0E0');
586 update_option('mc_form_background','FFFFFF');
587 update_option('mc_form_text_color','3F3F3f');
588 }
589
590 /**
591 * Saves the General Form settings on the options page
592 *
593 * @return void
594 **/
595 function mailchimpSF_save_general_form_settings() {
596
597 if (MAILCHIMP_DEV_MODE == false) {
598 if (isset($_POST['mc_rewards'])){
599 update_option('mc_rewards', 'on');
600 $msg = '<p class="success_msg">'.__('Monkey Rewards turned On!', 'mailchimp_i18n').'</p>';
601 mailchimpSF_global_msg($msg);
602 } else if (get_option('mc_rewards')!='off') {
603 update_option('mc_rewards', 'off');
604 $msg = '<p class="success_msg">'.__('Monkey Rewards turned Off!', 'mailchimp_i18n').'</p>';
605 mailchimpSF_global_msg($msg);
606 }
607 if (isset($_POST['mc_use_javascript'])){
608 update_option('mc_use_javascript', 'on');
609 $msg = '<p class="success_msg">'.__('Fancy Javascript submission turned On!', 'mailchimp_i18n').'</p>';
610 mailchimpSF_global_msg($msg);
611 } else if (get_option('mc_use_javascript')!='off') {
612 update_option('mc_use_javascript', 'off');
613 $msg = '<p class="success_msg">'.__('Fancy Javascript submission turned Off!', 'mailchimp_i18n').'</p>';
614 mailchimpSF_global_msg($msg);
615 }
616
617 if (isset($_POST['mc_use_datepicker'])){
618 update_option('mc_use_datepicker', 'on');
619 $msg = '<p class="success_msg">'.__('Datepicker turned On!', 'mailchimp_i18n').'</p>';
620 mailchimpSF_global_msg($msg);
621 } else if (get_option('mc_use_datepicker')!='off') {
622 update_option('mc_use_datepicker', 'off');
623 $msg = '<p class="success_msg">'.__('Datepicker turned Off!', 'mailchimp_i18n').'</p>';
624 mailchimpSF_global_msg($msg);
625 }
626
627 if (isset($_POST['mc_use_unsub_link'])){
628 update_option('mc_use_unsub_link', 'on');
629 $msg = '<p class="success_msg">'.__('Unsubscribe link turned On!', 'mailchimp_i18n').'</p>';
630 mailchimpSF_global_msg($msg);
631 } else if (get_option('mc_use_unsub_link')!='off') {
632 update_option('mc_use_unsub_link', 'off');
633 $msg = '<p class="success_msg">'.__('Unsubscribe link turned Off!', 'mailchimp_i18n').'</p>';
634 mailchimpSF_global_msg($msg);
635 }
636
637 $content = stripslashes($_POST['mc_header_content']);
638 $content = str_replace("\r\n","<br/>", $content);
639 update_option('mc_header_content', $content );
640
641 $content = stripslashes($_POST['mc_subheader_content']);
642 $content = str_replace("\r\n","<br/>", $content);
643 update_option('mc_subheader_content', $content );
644
645
646 $submit_text = stripslashes($_POST['mc_submit_text']);
647 $submit_text = str_replace("\r\n","", $submit_text);
648 update_option('mc_submit_text', $submit_text);
649 }
650
651 // Set Custom Style option
652 update_option('mc_custom_style', isset($_POST['mc_custom_style']) ? 'on' : 'off');
653
654 if (MAILCHIMP_DEV_MODE == false) {
655 //we told them not to put these things we are replacing in, but let's just make sure they are listening...
656 update_option('mc_header_border_width',str_replace('px','',$_POST['mc_header_border_width']) );
657 update_option('mc_header_border_color', str_replace('#','',$_POST['mc_header_border_color']));
658 update_option('mc_header_background',str_replace('#','',$_POST['mc_header_background']));
659 update_option('mc_header_text_color', str_replace('#','',$_POST['mc_header_text_color']));
660 }
661
662 update_option('mc_form_border_width',str_replace('px','',$_POST['mc_form_border_width']) );
663 update_option('mc_form_border_color', str_replace('#','',$_POST['mc_form_border_color']));
664 update_option('mc_form_background',str_replace('#','',$_POST['mc_form_background']));
665 update_option('mc_form_text_color', str_replace('#','',$_POST['mc_form_text_color']));
666
667 if (MAILCHIMP_DEV_MODE == false) {
668 $igs = get_option('mc_interest_groups');
669 if (is_array($igs)) {
670 foreach($igs as $var){
671 $opt = 'mc_show_interest_groups_'.$var['id'];
672 if (isset($_POST[$opt])){
673 update_option($opt,'on');
674 } else {
675 update_option($opt,'off');
676 }
677 }
678 }
679
680 $mv = get_option('mc_merge_vars');
681 if (is_array($mv)) {
682 foreach($mv as $var){
683 $opt = 'mc_mv_'.$var['tag'];
684 if (isset($_POST[$opt]) || $var['req']=='Y'){
685 update_option($opt,'on');
686 } else {
687 update_option($opt,'off');
688 }
689 }
690 }
691 }
692 $msg = '<p class="success_msg">'.esc_html(__('Successfully Updated your List Subscribe Form Settings!', 'mailchimp_i18n')).'</p>';
693 mailchimpSF_global_msg($msg);
694 }
695
696 /**
697 * Sees if the user changed the list, and updates options accordingly
698 **/
699 function mailchimpSF_change_list_if_necessary() {
700 // Simple permission check before going through all this
701 if (!current_user_can(MCSF_CAP_THRESHOLD)) { return; }
702
703 $api = mailchimpSF_get_api();
704 if (!$api) { return; }
705
706 //we *could* support paging, but few users have that many lists (and shouldn't)
707 $lists = $api->lists(array(),0,100);
708 $lists = $lists['data'];
709
710 if (is_array($lists) && !empty($lists) && isset($_POST['mc_list_id'])) {
711
712 /* If our incoming list ID (the one chosen in the select dropdown)
713 is in our array of lists, the set it to be the active list */
714 foreach($lists as $key => $list) {
715 if ($list['id'] == $_POST['mc_list_id']) {
716 $list_id = $_POST['mc_list_id'];
717 $list_name = $list['name'];
718 $list_key = $key;
719 }
720 }
721
722 $orig_list = get_option('mc_list_id');
723 if ($list_id != '') {
724 update_option('mc_list_id', $list_id);
725 update_option('mc_list_name', $list_name);
726 update_option('mc_email_type_option', $lists[$list_key]['email_type_option']);
727
728
729 // See if the user changed the list
730 if ($orig_list != $list_id){
731 // The user changed the list, Reset the Form Defaults
732 mailchimpSF_set_form_defaults($list_name);
733 }
734 // email_type_option
735
736 // Grab the merge vars and interest groups
737 $mv = $api->listMergeVars($list_id);
738 $igs = $api->listInterestGroupings($list_id);
739
740 update_option('mc_merge_vars', $mv);
741 foreach($mv as $var){
742 $opt = 'mc_mv_'.$var['tag'];
743 //turn them all on by default
744 if ($orig_list != $list_id) {
745 update_option($opt, 'on' );
746 }
747 }
748 update_option('mc_interest_groups', $igs);
749 if (is_array($igs)) {
750 foreach ($igs as $var){
751 $opt = 'mc_show_interest_groups_'.$var['id'];
752 //turn them all on by default
753 if ($orig_list != $list_id) {
754 update_option($opt, 'on' );
755 }
756 }
757 }
758 $igs_text = ' ';
759 if (is_array($igs)) {
760 $igs_text .= sprintf(__('and %s Sets of Interest Groups', 'mailchimp_i18n'), count($igs));
761 }
762
763 $msg = '<p class="success_msg">'.
764 sprintf(
765 __('<b>Success!</b> Loaded and saved the info for %d Merge Variables', 'mailchimp_i18n').$igs_text,
766 count($mv)
767 ).' '.
768 __('from your list').' "'.$list_name.'"<br/><br/>'.
769 __('Now you should either Turn On the MailChimp Widget or change your options below, then turn it on.', 'mailchimp_i18n').'</p>';
770 mailchimpSF_global_msg($msg);
771 }
772 }
773 }
774
775
776 /**
777 * Outputs the Settings/Options page
778 */
779 function mailchimpSF_setup_page() {
780 ?>
781 <div class="wrap">
782
783 <div class="mailchimp-header">
784 <h2><?php esc_html_e('MailChimp List Setup', 'mailchimp_i18n');?> </h2>
785 </div>
786 <?php
787
788 // Display Developer mode active.
789 if (MAILCHIMP_DEV_MODE == true) { ?>
790 <p class="error_msg">Developer mode active.</p>
791 <?php }
792
793 $user = get_option('mc_sopresto_user');
794
795 // If we have an API Key, see if we need to change the lists and its options
796 mailchimpSF_change_list_if_necessary();
797
798 // Display our success/error message(s) if have them
799 if (mailchimpSF_global_msg() != ''){
800 // Message has already been html escaped, so we don't want to 2x escape it here
801 ?>
802 <div id="mc_message" class=""><?php echo mailchimpSF_global_msg(); ?></div>
803 <?php
804 }
805
806 // If we don't have an API Key, do a login form
807 if (!$user && MAILCHIMP_DEV_MODE == false) {
808 ?>
809 <div>
810 <h3 class="mc-h2"><?php esc_html_e('Log In', 'mailchimp_i18n');?></h3>
811 <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>
812 <p class="mc-a">
813 <?php
814 echo sprintf(
815 '%1$s <a href="http://www.mailchimp.com/signup/" target="_blank">%2$s</a>',
816 esc_html(__("Don't have a MailChimp account?", 'mailchimp_i18n')),
817 esc_html(__('Try one for Free!', 'mailchimp_i18n'))
818 );
819 ?>
820 </p>
821
822 <div style="width: 900px;">
823 <table class="widefat mc-widefat mc-api">
824 <tr valign="top">
825 <th scope="row" class="mailchimp-connect"><?php esc_html_e('Connect to MailChimp', 'mailchimp_i18n'); ?></th>
826 <td>
827 <a href="<?php echo add_query_arg(array("mcsf_action" => "authorize"), home_url('index.php')) ?>" class="mailchimp-login">Connect</a>
828 </td>
829 </tr>
830 </table>
831 </div>
832 </div>
833
834 <br/>
835 <?php
836 if ($user && $user['username'] != '') {
837 ?>
838 <!--<div class="notes_msg">
839 <strong><?php esc_html_e('Notes', 'mailchimp_i18n'); ?>:</strong>
840 <ul>
841 <li><?php esc_html_e('Changing your settings at MailChimp.com may cause this to stop working.', 'mailchimp_i18n'); ?></li>
842 <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>
843 <li><?php esc_html_e('If any of that happens, no biggie - just reconfigure your login and the items below...', 'mailchimp_i18n'); ?></li>
844 </ul>
845 </div>-->
846 <?php
847 }
848 } // End of login form
849
850 // Start logout form
851 elseif (MAILCHIMP_DEV_MODE == false) {
852 ?>
853 <table style="min-width:400px;" class="mc-user" cellspacing="0">
854 <tr>
855 <td><h3><?php esc_html_e('Logged in as', 'mailchimp_i18n');?>: <?php echo esc_html($user['username']); ?></h3>
856 </td>
857 <td>
858 <form method="post" action="options-general.php?page=mailchimpSF_options">
859 <input type="hidden" name="mcsf_action" value="logout"/>
860 <input type="submit" name="Submit" value="<?php esc_attr_e('Logout', 'mailchimp_i18n');?>" class="button" />
861 <?php wp_nonce_field('mc_logout', '_mcsf_nonce_action'); ?>
862 </form>
863 </td>
864 </tr>
865 </table>
866 <?php
867 } // End Logout form
868
869 //Just get out if nothing else matters...
870 $api = mailchimpSF_get_api();
871 if (!$api && MAILCHIMP_DEV_MODE == false) { return; }
872
873 if ($api){
874 ?>
875 <h3 class="mc-h2"><?php esc_html_e('Your Lists', 'mailchimp_i18n'); ?></h3>
876
877 <div>
878
879 <p class="mc-p"><?php esc_html_e('Please select the List you wish to create a Signup Form for.', 'mailchimp_i18n'); ?></p>
880 <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>
881
882 <form method="post" action="options-general.php?page=mailchimpSF_options">
883 <?php
884 //we *could* support paging, but few users have that many lists (and shouldn't)
885 $lists = $api->lists(array(),0,100);
886 $lists = $lists['data'];
887
888 if (count($lists) == 0) {
889 ?>
890 <span class='error_msg'>
891 <?php
892 echo sprintf(
893 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')),
894 "<a href='http://www.mailchimp.com/'>MailChimp</a>"
895 );
896 ?>
897 </span>
898 <?php
899 }
900 else {
901 ?>
902 <table style="min-width:400px" class="mc-list-select" cellspacing="0">
903 <tr class="mc-list-row">
904 <td>
905 <select name="mc_list_id" style="min-width:200px;">
906 <option value=""> &mdash; <?php esc_html_e('Select A List','mailchimp_i18n'); ?> &mdash; </option>
907 <?php
908 foreach ($lists as $list) {
909 $option = get_option('mc_list_id');
910 ?>
911 <option value="<?php echo esc_attr($list['id']); ?>"<?php selected($list['id'], $option); ?>><?php echo esc_html($list['name']); ?></option>
912 <?php
913 }
914 ?>
915 </select>
916 </td>
917 <td>
918 <input type="hidden" name="mcsf_action" value="update_mc_list_id" />
919 <input type="submit" name="Submit" value="<?php esc_attr_e('Update List', 'mailchimp_i18n'); ?>" class="button" />
920 </td>
921 </tr>
922 </table>
923 <?php
924 } //end select list
925 ?>
926 </form>
927 </div>
928
929 <br/>
930
931 <?php
932 }
933 elseif (MAILCHIMP_DEV_MODE == false) {
934 //display the selected list...
935 ?>
936
937 <p class="submit">
938 <form method="post" action="options-general.php?page=mailchimpSF_options">
939 <input type="hidden" name="mcsf_action" value="reset_list" />
940 <input type="submit" name="reset_list" value="<?php esc_attr_e('Reset List Options and Select again', 'mailchimp_i18n'); ?>" class="button" />
941 <?php wp_nonce_field('reset_mailchimp_list', '_mcsf_nonce_action'); ?>
942 </form>
943 </p>
944 <h3><?php esc_html_e('Subscribe Form Widget Settings for this List', 'mailchimp_i18n'); ?>:</h3>
945 <h4><?php esc_html_e('Selected MailChimp List', 'mailchimp_i18n'); ?>: <?php echo esc_html(get_option('mc_list_name')); ?></h4>
946 <?php
947 }
948
949 //Just get out if nothing else matters...
950 if (get_option('mc_list_id') == '' && MAILCHIMP_DEV_MODE == false) return;
951
952
953 // The main Settings form
954 ?>
955
956 <div>
957 <form method="post" action="options-general.php?page=mailchimpSF_options">
958 <div style="width:900px;">
959 <input type="hidden" name="mcsf_action" value="change_form_settings">
960 <?php wp_nonce_field('update_general_form_settings', '_mcsf_nonce_action'); ?>
961
962 <?php if (MAILCHIMP_DEV_MODE == false) { ?>
963 <!--<input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button" />-->
964 <table class="widefat mc-widefat mc-label-options">
965 <tr><th colspan="2">Content Options</th></tr>
966 <tr valign="top">
967 <th scope="row"><?php esc_html_e('Header', 'mailchimp_i18n'); ?></th>
968 <td>
969 <textarea name="mc_header_content" rows="2" cols="70"><?php echo esc_html(get_option('mc_header_content')); ?></textarea><br/>
970 <?php esc_html_e('You can fill this with your own Text, HTML markup (including image links), or Nothing!', 'mailchimp_i18n'); ?>
971 </td>
972 </tr>
973
974 <tr valign="top">
975 <th scope="row"><?php esc_html_e('Sub-header', 'mailchimp_i18n'); ?></th>
976 <td>
977 <textarea name="mc_subheader_content" rows="2" cols="70"><?php echo esc_html(get_option('mc_subheader_content')); ?></textarea><br/>
978 <?php esc_html_e('You can fill this with your own Text, HTML markup (including image links), or Nothing!', 'mailchimp_i18n'); ?>.<br/>
979 <?php esc_html_e('This will be displayed under the heading and above the form.', 'mailchimp_i18n'); ?>
980 </td>
981 </tr>
982
983 <tr valign="top" class="last-row">
984 <th scope="row"><?php esc_html_e('Submit Button', 'mailchimp_i18n'); ?></th>
985 <td>
986 <input type="text" name="mc_submit_text" size="70" value="<?php echo esc_attr(get_option('mc_submit_text')); ?>"/>
987 </td>
988 </tr>
989 </table>
990
991 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button mc-submit" /><br/>
992 <?php } ?>
993
994 <table class="widefat mc-widefat mc-custom-styling">
995 <tr><th colspan="2">Custom Styling</th></tr>
996 <tr class="mc-turned-on"><th><label for="mc_custom_style"><?php esc_html_e('Enabled?', 'mailchimp_i18n'); ?></label></th><td><span class="mc-pre-input"></span><input type="checkbox" name="mc_custom_style" id="mc_custom_style"<?php checked(get_option('mc_custom_style'), 'on'); ?> /></td></tr>
997
998 <tr class="mc-internal-heading"><th colspan="2"><?php esc_html_e('Form Settings', 'mailchimp_i18n'); ?></th></tr>
999 <tr><th><?php esc_html_e('Border Width', 'mailchimp_i18n'); ?></th><td><span class="mc-pre-input"></span><input type="text" name="mc_form_border_width" size="3" maxlength="3" value="<?php echo esc_attr(get_option('mc_form_border_width')); ?>"/>
1000 <em>px <?php esc_html_e('Set to 0 for no border, do not enter', 'mailchimp_i18n'); ?> <strong>px</strong>!</em>
1001 </td></tr>
1002 <tr><th><?php esc_html_e('Border Color', 'mailchimp_i18n'); ?></th><td><span class="mc-pre-input">#</span><input type="text" name="mc_form_border_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_form_border_color')); ?>"/>
1003 <em><?php esc_html_e('Do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
1004 </td></tr>
1005 <tr><th><?php esc_html_e('Text Color', 'mailchimp_i18n'); ?></th><td><span class="mc-pre-input">#</span><input type="text" name="mc_form_text_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_form_text_color')); ?>"/>
1006 <em><?php esc_html_e('Do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
1007 </td></tr>
1008 <tr class="last-row"><th><?php esc_html_e('Background Color', 'mailchimp_i18n'); ?></th><td><span class="mc-pre-input">#</span><input type="text" name="mc_form_background" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_form_background')); ?>"/>
1009 <em><?php esc_html_e('Do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
1010 </td></tr>
1011 </table>
1012
1013 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button mc-submit" /><br/>
1014
1015 <?php if (MAILCHIMP_DEV_MODE == false) { ?>
1016 <table class="widefat mc-widefat">
1017 <tr><th colspan="2">List Options</th></tr>
1018 <tr valign="top">
1019 <th scope="row"><?php esc_html_e('Monkey Rewards', 'mailchimp_i18n'); ?>?</th>
1020 <td><input name="mc_rewards" type="checkbox"<?php if (get_option('mc_rewards')=='on' || get_option('mc_rewards')=='' ) { echo ' checked="checked"'; } ?> id="mc_rewards" class="code" />
1021 <em><label for="mc_rewards"><?php esc_html_e('Turning this on will place a "powered by MailChimp" link in your form that will earn you credits with us. It is optional and can be turned on or off at any time.', 'mailchimp_i18n'); ?></label></em>
1022 </td>
1023 </tr>
1024
1025 <tr valign="top">
1026 <th scope="row"><?php esc_html_e('Use Javascript Support?', 'mailchimp_i18n'); ?></th>
1027 <td><input name="mc_use_javascript" type="checkbox" <?php checked(get_option('mc_use_javascript'), 'on'); ?> id="mc_use_javascript" class="code" />
1028 <em><label for="mc_use_javascript"><?php esc_html_e('Turning this on will use fancy javascript submission and should degrade gracefully for users not using javascript. It is optional and can be turned on or off at any time.', 'mailchimp_i18n'); ?></label></em>
1029 </td>
1030 </tr>
1031
1032 <tr valign="top">
1033 <th scope="row"><?php esc_html_e('Use Javascript Datepicker?', 'mailchimp_i18n'); ?></th>
1034 <td><input name="mc_use_datepicker" type="checkbox" <?php checked(get_option('mc_use_datepicker'), 'on'); ?> id="mc_use_datepicker" class="code" />
1035 <em><label for="mc_use_datepicker"><?php esc_html_e('Turning this on will use the jQuery UI Datepicker for dates.', 'mailchimp_i18n'); ?></label></em>
1036 </td>
1037 </tr>
1038 <tr valign="top" class="last-row">
1039 <th scope="row"><?php esc_html_e('Include Unsubscribe link?', 'mailchimp_i18n'); ?></th>
1040 <td><input name="mc_use_unsub_link" type="checkbox"<?php checked(get_option('mc_use_unsub_link'), 'on'); ?> id="mc_use_unsub_link" class="code" />
1041 <em><label for="mc_use_unsub_link"><?php esc_html_e('Turning this on will add a link to your host unsubscribe form', 'mailchimp_i18n'); ?></label></em>
1042 </td>
1043 </tr>
1044 </table>
1045
1046 <?php } ?>
1047
1048 </div>
1049
1050 <?php
1051 // Merge Variables Table
1052 if (MAILCHIMP_DEV_MODE == false) { ?>
1053 <div style="width:900px;">
1054
1055 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button mc-submit" /><br/>
1056
1057 <table class='widefat mc-widefat'>
1058 <tr>
1059 <th colspan="4">
1060 <?php esc_html_e('Merge Variables Included', 'mailchimp_i18n'); ?>
1061
1062 <?php
1063 $mv = get_option('mc_merge_vars');
1064
1065 if (count($mv) == 0 || !is_array($mv)){
1066 ?>
1067 <em><?php esc_html_e('No Merge Variables found.', 'mailchimp_i18n'); ?></em>
1068 <?php
1069 } else {
1070 ?>
1071 </th>
1072 </tr>
1073 <tr valign="top">
1074 <th><?php esc_html_e('Name', 'mailchimp_i18n');?></th>
1075 <th><?php esc_html_e('Tag', 'mailchimp_i18n');?></th>
1076 <th><?php esc_html_e('Required?', 'mailchimp_i18n');?></th>
1077 <th><?php esc_html_e('Include?', 'mailchimp_i18n');?></th>
1078 </tr>
1079 <?php
1080 foreach($mv as $var){
1081 ?>
1082 <tr valign="top">
1083 <td><?php echo esc_html($var['name']); ?></td>
1084 <td><?php echo esc_html($var['tag']); ?></td>
1085 <td><?php echo esc_html(($var['req'] == 1) ? 'Y' : 'N'); ?></td>
1086 <td>
1087 <?php
1088 if (!$var['req']){
1089 $opt = 'mc_mv_'.$var['tag'];
1090 ?>
1091 <input name="<?php echo esc_attr($opt); ?>" type="checkbox" id="<?php echo esc_attr($opt); ?>" class="code"<?php checked(get_option($opt), 'on'); ?> />
1092 <?php
1093 } else {
1094 ?>
1095 &nbsp;&mdash;&nbsp;
1096 <?php
1097 }
1098 ?>
1099 </td>
1100 </tr>
1101 <?php
1102 }
1103 ?>
1104 </table>
1105 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button mc-submit" /><br/>
1106 </div>
1107 <?php
1108 }
1109 ?>
1110
1111
1112
1113 <?php
1114 // Interest Groups Table
1115 $igs = get_option('mc_interest_groups');
1116 if (is_array($igs) && !isset($igs['id'])) { ?>
1117 <h3 class="mc-h3"><?php esc_html_e('Group Settings', 'mailchimp_i18n'); ?></h3> <?php
1118 // Determines whether or not to continue processing. Only false if there was an error.
1119 $continue = true;
1120 foreach ($igs as $ig) {
1121 if ($continue) {
1122 if (!is_array($ig) || empty($ig) || $ig == 'N' ) {
1123 ?>
1124 <em><?php esc_html_e('No Interest Groups Setup for this List', 'mailchimp_i18n'); ?></em>
1125 <?php
1126 $continue = false;
1127 }
1128 else {
1129 ?>
1130 <table class='mc-widefat mc-blue' width="450px" cellspacing="0">
1131 <tr valign="top">
1132 <th colspan="2"><?php echo esc_html($ig['name']); ?></th>
1133 </tr>
1134 <tr valign="top">
1135 <th>
1136 <label for="<?php echo esc_attr('mc_show_interest_groups_'.$ig['id']); ?>"><?php esc_html_e('Show?', 'mailchimp_i18n'); ?></label>
1137 </th>
1138 <th>
1139 <input name="<?php echo esc_attr('mc_show_interest_groups_'.$ig['id']); ?>" id="<?php echo esc_attr('mc_show_interest_groups_'.$ig['id']); ?>" type="checkbox" class="code"<?php checked('on', get_option('mc_show_interest_groups_'.$ig['id'])); ?> />
1140 </th>
1141 </tr>
1142 <tr valign="top">
1143 <th><?php esc_html_e('Input Type', 'mailchimp_i18n'); ?></th>
1144 <td><?php echo esc_html($ig['form_field']); ?></td>
1145 </tr>
1146 <tr valign="top" class="last-row">
1147 <th><?php esc_html_e('Options', 'mailchimp_i18n'); ?></th>
1148 <td>
1149 <ul>
1150 <?php
1151 foreach($ig['groups'] as $interest){
1152 ?>
1153 <li><?php echo esc_html($interest['name']); ?></li>
1154 <?php
1155 }
1156 ?>
1157 </ul>
1158 </td>
1159 </tr>
1160 </table>
1161 <?php
1162 }
1163 }
1164 }
1165 }
1166 } // end dev mode check
1167 ?>
1168 <div style="width: 900px; margin-top: 35px;">
1169 <table class="widefat mc-widefat mc-yellow">
1170 <tr><th colspan="2">CSS Cheat Sheet</th></tr>
1171 <tr valign="top">
1172 <th scope="row">.widget_mailchimpsf_widget </th>
1173 <td>This targets the entire widget container.</td>
1174 </tr>
1175 <tr valign="top">
1176 <th scope="row">.widget-title</th>
1177 <td>This styles the title of your MailChimp widget. <i>Modifying this class will affect your other widget titles.</i></td>
1178 </tr>
1179 <tr valign="top">
1180 <th scope="row">#mc_signup</th>
1181 <td>This targets the entirity of the widget beneath the widget title.</td>
1182 </tr>
1183 <tr valign="top">
1184 <th scope="row">#mc_subheader</th>
1185 <td>This styles the subheader text.</td>
1186 </tr>
1187 <tr valign="top">
1188 <th scope="row">.mc_form_inside</th>
1189 <td>The guts and main container for the all of the form elements (the entirety of the widget minus the header and the sub header).</td>
1190 </tr>
1191 <tr valign="top">
1192 <th scope="row">.mc_header</th>
1193 <td>This targets the label above the input fields.</td>
1194 </tr>
1195 <tr valign="top">
1196 <th scope="row">.mc_input</th>
1197 <td>This attaches to the input fields.</td>
1198 </tr>
1199 <tr valign="top">
1200 <th scope="row">.mc_header_address</th>
1201 <td>This is the label above an address group.</td>
1202 </tr>
1203 <tr valign="top">
1204 <th scope="row">.mc_radio_label</th>
1205 <td>These are the labels associated with radio buttons.</td>
1206 </tr>
1207 <tr valign="top">
1208 <th scope="row">#mc-indicates-required</th>
1209 <td>This targets the “Indicates Required Field” text.</td>
1210 </tr>
1211 <tr valign="top">
1212 <th scope="row">#mc_signup_submit</th>
1213 <td>Use this to style the submit button.</td>
1214 </tr>
1215 </table>
1216 </div>
1217
1218 </form>
1219 <?php
1220 }//mailchimpSF_setup_page()
1221
1222
1223 function mailchimpSF_register_widgets() {
1224 if (mailchimpSF_get_api() || MAILCHIMP_DEV_MODE == true) {
1225 register_widget('mailchimpSF_Widget');
1226 }
1227 }
1228 add_action('widgets_init', 'mailchimpSF_register_widgets');
1229
1230 function mailchimpSF_shortcode($atts){
1231 ob_start();
1232 mailchimpSF_signup_form();
1233 return ob_get_clean();
1234 }
1235 add_shortcode('mailchimpsf_form', 'mailchimpSF_shortcode');
1236
1237 /**
1238 * Attempts to signup a user, per the $_POST args.
1239 *
1240 * This sets a global message, that is then used in the widget
1241 * output to retrieve and display that message.
1242 *
1243 * @return bool
1244 */
1245 function mailchimpSF_signup_submit() {
1246 $mv = get_option('mc_merge_vars', array());
1247 $mv_tag_keys = array();
1248
1249 $igs = get_option('mc_interest_groups', array());
1250
1251 $success = true;
1252 $listId = get_option('mc_list_id');
1253 $email = isset($_POST['mc_mv_EMAIL']) ? strip_tags(stripslashes($_POST['mc_mv_EMAIL'])) : '';
1254 $merge = $errs = $html_errs = array(); // Set up some vars
1255
1256 // Loop through our Merge Vars, and if they're empty, but required, then print an error, and mark as failed
1257 foreach($mv as $var) {
1258 $opt = 'mc_mv_'.$var['tag'];
1259
1260 $opt_val = isset($_POST[$opt]) ? stripslashes($_POST[$opt]) : '';
1261
1262 if (is_array($opt_val) && isset($opt_val['area'])) {
1263 // This filters out all 'falsey' elements
1264 $opt_val = array_filter($opt_val);
1265
1266 // If they weren't all empty
1267 if ($opt_val) {
1268 $opt_val = implode('-', $opt_val);
1269 if (strlen($opt_val) < 12) {
1270 $opt_val = '';
1271 }
1272 }
1273 else {
1274 $opt_val = '';
1275 }
1276 }
1277 else if (is_array($opt_val) && $var['field_type'] == 'address') {
1278 if ($var['req'] == 'Y') {
1279 if (empty($opt_val['addr1']) || empty($opt_val['city'])) {
1280 $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($var['name']));
1281 $success = false;
1282 }
1283 }
1284 $merge[$var['tag']] = $opt_val;
1285 continue;
1286 }
1287 else if (is_array($opt_val)) {
1288 $opt_val = implode($opt_val);
1289 }
1290
1291 if ($var['req'] == 'Y' && trim($opt_val) == '') {
1292 $success = false;
1293 $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($var['name']));
1294 }
1295 else {
1296 if ($var['tag'] != 'EMAIL') {
1297 $merge[$var['tag']] = $opt_val;
1298 }
1299 }
1300
1301 // We also want to create an array where the keys are the tags for easier validation later
1302 $mv_tag_keys[$var['tag']] = $var;
1303
1304 }
1305
1306 // Head back to the beginning of the merge vars array
1307 reset($mv);
1308
1309 // Ensure we have an array
1310 $igs = !is_array($igs) ? array() : $igs;
1311 foreach ($igs as $ig) {
1312 $groups = '';
1313 if (get_option('mc_show_interest_groups_'.$ig['id']) == 'on') {
1314 $groupings = array();
1315 switch ($ig['form_field']) {
1316 case 'select':
1317 case 'dropdown':
1318 case 'radio':
1319 if (isset($_POST['group'][$ig['id']])) {
1320 $groupings = array(
1321 'id' => $ig['id'],
1322 'groups' => str_replace(',', '\,', stripslashes($_POST['group'][$ig['id']])),
1323 );
1324 }
1325 break;
1326 case 'checkboxes':
1327 case 'checkbox':
1328 if (isset($_POST['group'][$ig['id']])) {
1329 foreach ($_POST['group'][$ig['id']] as $i => $value) {
1330 // Escape
1331 $groups .= str_replace(',', '\,', stripslashes($value)).',';
1332 }
1333 $groupings = array(
1334 'id' => $ig['id'],
1335 'groups' => $groups,
1336 );
1337 }
1338 break;
1339 default:
1340 // Nothing
1341 break;
1342 }
1343 if (!isset($merge['GROUPINGS']) || !is_array($merge['GROUPINGS'])) {
1344 $merge['GROUPINGS'] = array();
1345 }
1346 if (!empty($groupings)) {
1347 $merge['GROUPINGS'][] = $groupings;
1348 }
1349 }
1350 }
1351
1352 // If we're good
1353 if ($success) {
1354 // Clear out empty merge vars
1355 foreach ($merge as $k => $v) {
1356 if (is_array($v) && empty($v)) {
1357 unset($merge[$k]);
1358 }
1359 else if (!is_array($v) && trim($v) === '') {
1360 unset($merge[$k]);
1361 }
1362 }
1363
1364 // If we have an empty $merge, then assign empty string.
1365 if (count($merge) == 0 || $merge == '') {
1366 $merge = '';
1367 }
1368
1369 if (isset($_POST['email_type']) && in_array($_POST['email_type'], array('text', 'html', 'mobile'))) {
1370 $email_type = $_POST['email_type'];
1371 }
1372 else {
1373 $email_type = 'html';
1374 }
1375
1376 // Custom validation based on type
1377 if (is_array($merge) && !empty($merge)) {
1378 foreach ($merge as $merge_key => $merge_value) {
1379 if ($merge_key !== 'GROUPINGS') {
1380 switch ($mv_tag_keys[$merge_key]['field_type']) {
1381 case 'phone':
1382 if ($mv_tag_keys[$merge_key]['phoneformat'] == 'US') {
1383 $phone = $merge_value;
1384 if (!empty($phone)) {
1385 if (!preg_match('/[0-9]{0,3}-[0-9]{0,3}-[0-9]{0,4}/', $phone)) {
1386 $errs[] = sprintf(__("%s must consist of only numbers", 'mailchimp_i18n'), esc_html($mv_tag_keys[$merge_key]['name']));
1387 $success = false;
1388 }
1389 }
1390 }
1391 break;
1392
1393 default:
1394 break;
1395 }
1396 }
1397 }
1398 }
1399 if ($success) {
1400 $api = mailchimpSF_get_api();
1401 if (!$api) { return; }
1402
1403 $retval = $api->listSubscribe( $listId, $email, $merge, $email_type);
1404 if (!$retval) {
1405 switch($api->errorCode) {
1406 case '105' :
1407 $errs[] = __("Please try again later", 'mailchimp_i18n').'.';
1408 break;
1409 case '214' :
1410 $msg = __("That email address is already subscribed to the list", 'mailchimp_i18n') . '.';
1411
1412 $account = $api->getAccountDetails(array("modules", "orders", "rewards-credits", "rewards-inspections", "rewards-referrals", "rewards-applied"));
1413 if (!$api->errorCode) {
1414 $dc = get_option('mc_sopresto_dc');
1415 $uid = $account['user_id'];
1416 $username = preg_replace('/\s+/', '-', $account['username']);
1417 $eid = base64_encode($email);
1418 $msg .= ' ' . sprintf(__('<a href="%s">Click here to update your profile.</a>', 'mailchimp_i18n'), "http://$dc.list-manage.com/subscribe/send-email?u=$uid&id=$listId&e=$eid");
1419 }
1420
1421 $errs[] = $msg;
1422 $html_errs[] = count($errs)-1;
1423 break;
1424 case '250' :
1425 list($field, $rest) = explode(' ', $api->errorMessage, 2);
1426 $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name']));
1427 break;
1428 case '254' :
1429 list($i1, $i2, $i3, $field, $rest) = explode(' ',$api->errorMessage,5);
1430 $errs[] = sprintf(__("%s has invalid content.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name']));
1431 break;
1432 case '270' :
1433 $errs[] = __("An invalid Interest Group was selected", 'mailchimp_i18n').'.';
1434 break;
1435 case '502' :
1436 $errs[] = __("That email address is invalid", 'mailchimp_i18n').'.';
1437 break;
1438 default:
1439 $errs[] = $api->errorCode.":".$api->errorMessage;
1440 break;
1441 }
1442 $success = false;
1443 }
1444 }
1445 }
1446
1447 // If we have errors, then show them
1448 if (count($errs) > 0) {
1449 $msg = '<span class="mc_error_msg">';
1450 foreach($errs as $error_index => $error){
1451 if (!in_array($error_index, $html_errs)) {
1452 $error = esc_html($error);
1453 }
1454 $msg .= '&raquo; '.$error.'<br />';
1455 }
1456 $msg .= '</span>';
1457 }
1458 else {
1459 $msg = "<strong class='mc_success_msg'>".esc_html(__("Success, you've been signed up! Please look for our confirmation email!", 'mailchimp_i18n'))."</strong>";
1460 }
1461
1462 // Set our global message
1463 mailchimpSF_global_msg($msg);
1464
1465 return $success;
1466 }
1467
1468
1469
1470 /**********************
1471 * Utility Functions *
1472 **********************/
1473 /**
1474 * Utility function to allow placement of plugin in plugins, mu-plugins, child or parent theme's plugins folders
1475 *
1476 * This function must be ran _very early_ in the load process, as it sets up important constants for the rest of the plugin
1477 */
1478 function mailchimpSF_where_am_i() {
1479 $locations = array(
1480 'plugins' => array(
1481 'dir' => WP_PLUGIN_DIR,
1482 'url' => plugins_url()
1483 ),
1484 'mu_plugins' => array(
1485 'dir' => WPMU_PLUGIN_DIR,
1486 'url' => plugins_url(),
1487 ),
1488 'template' => array(
1489 'dir' => trailingslashit(get_template_directory()).'plugins/',
1490 'url' => trailingslashit(get_template_directory_uri()).'plugins/',
1491 ),
1492 'stylesheet' => array(
1493 'dir' => trailingslashit(get_stylesheet_directory()).'plugins/',
1494 'url' => trailingslashit(get_stylesheet_directory_uri()).'plugins/',
1495 ),
1496 );
1497
1498 // Set defaults
1499 $mscf_dirbase = trailingslashit(basename(dirname(__FILE__))); // Typically wp-mailchimp/ or mailchimp/
1500 $mscf_dir = trailingslashit(WP_PLUGIN_DIR).$mscf_dirbase;
1501 $mscf_url = trailingslashit(WP_PLUGIN_URL).$mscf_dirbase;
1502
1503 // Try our hands at finding the real location
1504 foreach ($locations as $key => $loc) {
1505 $dir = trailingslashit($loc['dir']).$mscf_dirbase;
1506 $url = trailingslashit($loc['url']).$mscf_dirbase;
1507 if (is_file($dir.basename(__FILE__))) {
1508 $mscf_dir = $dir;
1509 $mscf_url = $url;
1510 break;
1511 }
1512 }
1513
1514 // Define our complete filesystem path
1515 define('MCSF_DIR', $mscf_dir);
1516
1517 /* Lang location needs to be relative *from* ABSPATH,
1518 so strip it out of our language dir location */
1519 define('MCSF_LANG_DIR', trailingslashit(MCSF_DIR).'po/');
1520
1521 // Define our complete URL to the plugin folder
1522 define('MCSF_URL', $mscf_url);
1523 }
1524
1525
1526 /**
1527 * MODIFIED VERSION of wp_verify_nonce from WP Core. Core was not overridden to prevent problems when replacing
1528 * something universally.
1529 *
1530 * Verify that correct nonce was used with time limit.
1531 *
1532 * The user is given an amount of time to use the token, so therefore, since the
1533 * UID and $action remain the same, the independent variable is the time.
1534 *
1535 * @param string $nonce Nonce that was used in the form to verify
1536 * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
1537 * @return bool Whether the nonce check passed or failed.
1538 */
1539 function mailchimpSF_verify_nonce($nonce, $action = -1) {
1540 $user = wp_get_current_user();
1541 $uid = (int) $user->ID;
1542 if ( ! $uid ) {
1543 $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
1544 }
1545
1546 if ( empty( $nonce ) ) {
1547 return false;
1548 }
1549
1550 $token = 'MAILCHIMP';
1551 $i = wp_nonce_tick();
1552
1553 // Nonce generated 0-12 hours ago
1554 $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce'), -12, 10 );
1555 if ( hash_equals( $expected, $nonce ) ) {
1556 return 1;
1557 }
1558
1559 // Nonce generated 12-24 hours ago
1560 $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
1561 if ( hash_equals( $expected, $nonce ) ) {
1562 return 2;
1563 }
1564
1565 // Invalid nonce
1566 return false;
1567 }
1568
1569
1570 /**
1571 * MODIFIED VERSION of wp_create_nonce from WP Core. Core was not overridden to prevent problems when replacing
1572 * something universally.
1573 *
1574 * Creates a cryptographic token tied to a specific action, user, and window of time.
1575 *
1576 * @param string $action Scalar value to add context to the nonce.
1577 * @return string The token.
1578 */
1579 function mailchimpSF_create_nonce($action = -1) {
1580 $user = wp_get_current_user();
1581 $uid = (int) $user->ID;
1582 if ( ! $uid ) {
1583 /** This filter is documented in wp-includes/pluggable.php */
1584 $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
1585 }
1586
1587 $token = 'MAILCHIMP';
1588 $i = wp_nonce_tick();
1589
1590 return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
1591 }
1592
1593