PluginProbe
Mailchimp List Subscribe Form / 1.2.5
Mailchimp List Subscribe Form v1.2.5
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.2.5, at mailchimp.php

1,385 lines 44.7 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: http://www.mailchimp.com/plugins/mailchimp-wordpress-plugin/
5 Description: The MailChimp plugin allows you to quickly and easily add a signup form for your MailChimp list.
6 Version: 1.2.5
7 Author: MailChimp and Crowd Favorite
8 Author URI: http://mailchimp.com/api/
9 */
10 /* Copyright 2008 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.2.5');
29
30 // What's our permission (capability) threshold
31 define('MCSF_CAP_THRESHOLD', 'edit_plugins');
32
33 // Define our location constants, both MCSF_DIR and MCSF_URL
34 mailchimpSF_where_am_i();
35
36 // Get our MailChimp API class in scope
37 if (!class_exists('mailchimpSF_MCAPI')) {
38 require_once('miniMCAPI.class.php');
39 }
40
41 // includes the widget code so it can be easily called either normally or via ajax
42 include_once('mailchimp_widget.php');
43
44 // includes the backwards compatibility functions
45 include_once('mailchimp_compat.php');
46
47 /**
48 * Do the following plugin setup steps here
49 *
50 * Internationalization
51 * Resource (JS & CSS) enqueuing
52 *
53 * @return void
54 */
55 function mailchimpSF_plugin_init() {
56 // Internationalize the plugin
57 $textdomain = 'mailchimp_i18n';
58 $locale = apply_filters( 'plugin_locale', get_locale(), $textdomain);
59 load_textdomain('mailchimp_i18n', MCSF_LANG_DIR.$textdomain.'-'.$locale.'.mo');
60
61 // Bring in our appropriate JS and CSS resources
62 mailchimpSF_load_resources();
63 }
64 add_action( 'init', 'mailchimpSF_plugin_init' );
65
66 function mailchimpSD_plugin_action_links($links) {
67 $settings_link = '<a href="'. admin_url('options-general.php?page=mailchimpSF_options').'">'.__('Settings', 'mailchimp_i18n' ).'</a>';
68 array_unshift($links, $settings_link);
69 return $links;
70 }
71 add_filter('plugin_action_links', 'mailchimpSD_plugin_action_links', 10, 1);
72
73 /**
74 * Loads the appropriate JS and CSS resources depending on
75 * settings and context (admin or not)
76 *
77 * @return void
78 */
79 function mailchimpSF_load_resources() {
80 // JS
81 if (get_option('mc_use_javascript') == 'on') {
82 if (!is_admin()) {
83 wp_enqueue_script('jquery_scrollto', MCSF_URL.'js/scrollTo.js', array('jquery'), MCSF_VER);
84 wp_enqueue_script('mailchimpSF_main_js', MCSF_URL.'js/mailchimp.js', array('jquery', 'jquery-form'), MCSF_VER);
85 // some javascript to get ajax version submitting to the proper location
86 global $wp_scripts;
87 $wp_scripts->localize('mailchimpSF_main_js', 'mailchimpSF', array(
88 'ajax_url' => trailingslashit(home_url()),
89 ));
90 }
91 }
92
93 if (get_option('mc_use_datepicker') == 'on' && !is_admin()) {
94 // Datepicker theme
95 wp_enqueue_style('flick', MCSF_URL.'/css/flick/flick.css');
96 // Datepicker JS
97 wp_enqueue_script('datepicker', MCSF_URL.'/js/datepicker.js', array('jquery','jquery-ui-core'));
98 }
99
100 // CSS
101 else {
102 wp_enqueue_style('mailchimpSF_main_css', home_url('?mcsf_action=main_css&ver='.MCSF_VER));
103 wp_enqueue_style('mailchimpSF_ie_css', MCSF_URL.'css/ie.css');
104 global $wp_styles;
105 $wp_styles->add_data( 'mailchimpSF_ie_css', 'conditional', 'IE' );
106 }
107 }
108
109
110 /**
111 * Loads resources for the MailChimp admin page
112 *
113 * @return void
114 */
115 function mc_admin_page_load_resources() {
116 wp_enqueue_style('mailchimpSF_admin_css', MCSF_URL.'css/admin.css');
117 }
118 add_action('load-settings_page_mailchimpSF_options', 'mc_admin_page_load_resources');
119
120
121 /**
122 * Loads jQuery Datepicker for the date-pick class
123 **/
124 function mc_datepicker_load() {
125 ?>
126 <script type="text/javascript">
127 jQuery(function($) {
128 $('.date-pick').datepicker({
129 autoFocusNextInput: true,
130 constrainInput: false,
131 changeMonth: true,
132 changeYear: true,
133 beforeShow: function(input, inst) { $('#ui-datepicker-div').addClass('show'); },
134 dateFormat: 'yy/mm/dd',
135 });
136
137 d = new Date();
138 $('.birthdate-pick').datepicker({
139 autoFocusNextInput: true,
140 constrainInput: false,
141 changeMonth: true,
142 changeYear: false,
143 minDate: new Date(d.getFullYear(), 1-1, 1),
144 maxDate: new Date(d.getFullYear(), 12-1, 31),
145 beforeShow: function(input, inst) { $('#ui-datepicker-div').removeClass('show'); },
146 dateFormat: 'mm/dd',
147 });
148
149
150 });
151 </script>
152 <?php
153 }
154 if (get_option('mc_use_datepicker') == 'on' && !is_admin()) {
155 add_action('wp_head', 'mc_datepicker_load');
156 }
157
158 /**
159 * Handles requests that as light-weight a load as possible.
160 * typically, JS or CSS
161 **/
162 function mailchimpSF_early_request_handler() {
163 if (isset($_GET['mcsf_action'])) {
164 switch ($_GET['mcsf_action']) {
165 case 'main_css':
166 header("Content-type: text/css");
167 mailchimpSF_main_css();
168 exit;
169 }
170 }
171 }
172 add_action('init', 'mailchimpSF_early_request_handler', 0);
173
174 /**
175 * Outputs the front-end CSS. This checks several options, so it
176 * was best to put it in a Request-handled script, as opposed to
177 * a static file.
178 */
179 function mailchimpSF_main_css() {
180 ?>
181 .mc_error_msg {
182 color: red;
183 }
184 .mc_success_msg {
185 color: green;
186 }
187 .mc_merge_var{
188 padding:0;
189 margin:0;
190 }
191 <?php
192 // If we're utilizing custom styles
193 if (get_option('mc_custom_style')=='on'){
194 ?>
195 #mc_signup_form {
196 padding:5px;
197 border-width: <?php echo get_option('mc_form_border_width'); ?>px;
198 border-style: <?php echo (get_option('mc_form_border_width')==0) ? 'none' : 'solid'; ?>;
199 border-color: #<?php echo get_option('mc_form_border_color'); ?>;
200 color: #<?php echo get_option('mc_form_text_color'); ?>;
201 background-color: #<?php echo get_option('mc_form_background'); ?>;
202 }
203
204
205 .mc_custom_border_hdr {
206 border-width: <?php echo get_option('mc_header_border_width'); ?>px;
207 border-style: <?php echo (get_option('mc_header_border_width')==0) ? 'none' : 'solid'; ?>;
208 border-color: #<?php echo get_option('mc_header_border_color'); ?>;
209 color: #<?php echo get_option('mc_header_text_color'); ?>;
210 background-color: #<?php echo get_option('mc_header_background'); ?>;
211 font-size: 1.2em;
212 padding:5px 10px;
213 width: 100%;
214 }
215 <?php
216 }
217 ?>
218 #mc_signup_container {}
219 #mc_signup_form {}
220 #mc_signup_form .mc_var_label {}
221 #mc_signup_form .mc_input {}
222 #mc-indicates-required {
223 width:100%;
224 }
225 #mc_display_rewards {}
226 .mc_interests_header {
227 font-weight:bold;
228 }
229 div.mc_interest{
230 width:100%;
231 }
232 #mc_signup_form input.mc_interest {}
233 #mc_signup_form select {}
234 #mc_signup_form label.mc_interest_label {
235 display:inline;
236 }
237 .mc_signup_submit {
238 text-align:center;
239 }
240 ul.mc_list {
241 list-style-type: none;
242 }
243 ul.mc_list li {
244 font-size: 12px;
245 }
246 .ui-datepicker-year {
247 display: none;
248 }
249 #ui-datepicker-div.show .ui-datepicker-year {
250 display: inline;
251 padding-left: 3px
252 }
253 <?php
254 }
255
256
257 /**
258 * Add our settings page to the admin menu
259 *
260 * @return void
261 */
262 function mailchimpSF_add_pages(){
263 // Add settings page for users who can edit plugins
264 add_options_page( __( 'MailChimp Setup', 'mailchimp_i18n' ), __( 'MailChimp Setup', 'mailchimp_i18n' ), MCSF_CAP_THRESHOLD, 'mailchimpSF_options', 'mailchimpSF_setup_page');
265 }
266 add_action('admin_menu', 'mailchimpSF_add_pages');
267
268 function mailchimpSF_request_handler() {
269 if (isset($_POST['mcsf_action'])) {
270 switch ($_POST['mcsf_action']) {
271 case 'logout':
272 // Check capability & Verify nonce
273 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'mc_logout')) {
274 wp_die('Cheatin&rsquo; huh?');
275 }
276
277 // erase API Key
278 update_option('mc_apikey', '');
279 break;
280 case 'update_mc_apikey':
281 // Check capability & Verify nonce
282 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'update_mc_api_key')) {
283 wp_die('Cheatin&rsquo; huh?');
284 }
285
286 mailchimpSF_set_api_key(strip_tags(stripslashes($_POST['mc_apikey'])));
287 break;
288 case 'reset_list':
289 // Check capability & Verify nonce
290 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'reset_mailchimp_list')) {
291 wp_die('Cheatin&rsquo; huh?');
292 }
293
294 mailchimpSF_reset_list_settings();
295 break;
296 case 'change_form_settings':
297 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'update_general_form_settings')) {
298 wp_die('Cheatin&rsquo; huh?');
299 }
300
301 // Update the form settings
302 mailchimpSF_save_general_form_settings();
303 break;
304 case 'mc_submit_signup_form':
305 // Validate nonce
306 if (!wp_verify_nonce($_POST['_mc_submit_signup_form_nonce'], 'mc_submit_signup_form')) {
307 wp_die('Cheatin&rsquo; huh?');
308 }
309
310 // Attempt the signup
311 mailchimpSF_signup_submit();
312
313 // Do a different action for html vs. js
314 switch ($_POST['mc_submit_type']) {
315 case 'html':
316 /* Allow to fall through. The widget will pick up the
317 * global message left over from the signup_submit function */
318 break;
319 case 'js':
320 if (!headers_sent()){ //just in case...
321 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT', true, 200);
322 }
323 echo mailchimpSF_global_msg(); // Don't esc_html this, b/c we've already escaped it
324 exit;
325 }
326 }
327 }
328 }
329 add_action('init', 'mailchimpSF_request_handler');
330
331 /**
332 * Upgrades data if it needs to. Checks on admin_init
333 *
334 * @return void
335 */
336 function mailchimpSF_upgrade() {
337 // See if we need an upgrade
338 if (mailchimpSF_needs_upgrade()) {
339 // remove password option if it's set (0.5)
340 // Update interest group data
341 mailchimpSF_do_upgrade();
342 }
343 }
344 add_action('admin_init', 'mailchimpSF_upgrade');
345
346 /**
347 * Checks to see if we're storing a password, if so, we need
348 * to upgrade to the API key
349 *
350 * @return bool
351 **/
352 function mailchimpSF_needs_upgrade() {
353 $igs = get_option('mc_interest_groups');
354
355 /* True if:
356 * The option is set in the database AND
357 * The option is empty (no interest groups) OR the option is an array and the first element has an 'id' element in it
358 * False otherwise
359 */
360 if ($igs !== false && (empty($igs) || (is_array($igs) && isset($igs[0]['id']))) ) {
361 return false;
362 }
363 else {
364 return true;
365 }
366 }
367
368 /**
369 * 1.2.4 -> 1.2.5 - Update to support multiple interest groups
370 * MCAPIv1.2 -> MCAPIv1.3 - update interest groups
371 * 2011-02-09 - old password upgrade code deleted as 0.5 is way old
372 */
373 function mailchimpSF_do_upgrade() {
374 //left in just for good measure
375 delete_option('mc_password');
376 $api = new mailchimpSF_MCAPI(get_option('mc_apikey'));
377 $igs = $api->listInterestGroupings(get_option('mc_list_id'));
378 update_option('mc_interest_groups', $igs);
379 }
380
381 /**
382 * Sets the API Key to whatever value was passed to this func
383 *
384 * @return array of vars
385 **/
386 function mailchimpSF_set_api_key($api_key = '') {
387 $delete_setup = false;
388 $api = new mailchimpSF_MCAPI($api_key);
389 $api->ping();
390 if (empty($api->errorCode)) {
391 $msg = "<p class='success_msg'>".esc_html(__("Success! We were able to verify your API Key! Let's continue, shall we?", 'mailchimp_i18n'))."</p>";
392 update_option('mc_apikey', $api_key);
393 $req = $api->getAccountDetails();
394 update_option('mc_username', $req['username']);
395 update_option('mc_user_id', $req['user_id']);
396 $cur_list_id = get_option('mc_list_id');
397 if (!empty($cur_list_id)) {
398 //we *could* support paging, but few users have that many lists (and shouldn't)
399 $lists = $api->lists(array(),0,100);
400 $lists = $lists['data'];
401 //but don't delete if the list still exists...
402 $delete_setup = true;
403 foreach($lists as $list) {
404 if ($list['id'] == $cur_list_id) {
405 $list_id = isset($_POST['mc_list_id']) ? $_POST['mc_list_id'] : '';
406 $delete_setup = false;
407 }
408 }
409 }
410 } else {
411 $msg = "<p class='error_msg'>".esc_html(__('Uh-oh, we were unable to verify your API Key. Please check them and try again!', 'mailchimp_i18n'))."<br/>";
412 $msg .= __('The server said:', 'mailchimp_i18n')."<em>".esc_html($api->errorMessage)."</em></p>";
413 $username = get_option('mc_username');
414 if (empty($username)) {
415 $delete_setup = true;
416 }
417 }
418
419 // Set a global message
420 mailchimpSF_global_msg($msg);
421
422 // If we need to delete our setup, do it
423 if ($delete_setup){
424 mailchimpSF_delete_setup();
425 }
426 }
427
428 /**
429 * Deletes all mailchimp options
430 **/
431 function mailchimpSF_delete_setup() {
432 delete_option('mc_user_id');
433 delete_option('mc_rewards');
434 delete_option('mc_use_javascript');
435 delete_option('mc_use_datepicker');
436 delete_option('mc_use_unsub_link');
437 delete_option('mc_list_id');
438 delete_option('mc_list_name');
439 $igs = get_option('mc_interest_groups');
440 if (is_array($igs)) {
441 foreach ($igs as $ig) {
442 $opt = 'mc_show_interest_groups_'.$ig['id'];
443 delete_option($opt);
444 }
445 }
446 delete_option('mc_interest_groups');
447 $mv = get_option('mc_merge_vars');
448 if (is_array($mv)){
449 foreach($mv as $var){
450 $opt = 'mc_mv_'.$var['tag'];
451 delete_option($opt);
452 }
453 }
454 delete_option('mc_merge_vars');
455 }
456
457 /**
458 * Resets the list settings, there's only one list
459 * that can have settings at a time, so no list_id
460 * parameter is necessary.
461 **/
462 function mailchimpSF_reset_list_settings() {
463
464 delete_option('mc_list_id');
465 delete_option('mc_list_name');
466 delete_option('mc_merge_vars');
467 delete_option('mc_interest_groups');
468
469 delete_option('mc_use_javascript');
470 delete_option('mc_use_unsub_link');
471 delete_option('mc_use_datepicker');
472
473 delete_option('mc_header_content');
474 delete_option('mc_subheader_content');
475 delete_option('mc_submit_text');
476
477 delete_option('mc_custom_style');
478
479 delete_option('mc_header_border_width');
480 delete_option('mc_header_border_color');
481 delete_option('mc_header_background');
482 delete_option('mc_header_text_color');
483
484 delete_option('mc_form_border_width');
485 delete_option('mc_form_border_color');
486 delete_option('mc_form_background');
487 delete_option('mc_form_text_color');
488
489 $msg = '<p class="success_msg">'.esc_html(__('Successfully Reset your List selection... Now you get to pick again!', 'mailchimp_i18n')).'</p>';
490 mailchimpSF_global_msg($msg);
491 }
492
493 /**
494 * Gets or sets a global message based on parameter passed to it
495 *
496 * @return string/bool depending on get/set
497 **/
498 function mailchimpSF_global_msg($msg = null) {
499 global $mcsf_msgs;
500
501 // Make sure we're formed properly
502 if (!is_array($mcsf_msgs)) {
503 $mcsf_msgs = array();
504 }
505
506 // See if we're getting
507 if (is_null($msg)) {
508 return implode('', $mcsf_msgs);
509 }
510
511 // Must be setting
512 $mcsf_msgs[] = $msg;
513 return true;
514 }
515
516 /**
517 * Sets the default options for the option form
518 **/
519 function mailchimpSF_set_form_defaults($list_name = '') {
520 update_option('mc_header_content',__( 'Sign up for', 'mailchimp_i18n' ).' '.$list_name);
521 update_option('mc_submit_text',__( 'Subscribe', 'mailchimp_i18n' ));
522
523 update_option('mc_use_datepicker', 'on');
524 update_option('mc_custom_style','on');
525 update_option('mc_use_javascript','on');
526 update_option('mc_use_unsub_link','off');
527 update_option('mc_header_border_width','1');
528 update_option('mc_header_border_color','E3E3E3');
529 update_option('mc_header_background','FFFFFF');
530 update_option('mc_header_text_color','CC6600');
531
532 update_option('mc_form_border_width','1');
533 update_option('mc_form_border_color','C4D3EA');
534 update_option('mc_form_background','EEF3F8');
535 update_option('mc_form_text_color','555555');
536 }
537
538 /**
539 * Saves the General Form settings on the options page
540 *
541 * @return void
542 **/
543 function mailchimpSF_save_general_form_settings() {
544 if (isset($_POST['mc_rewards'])){
545 update_option('mc_rewards', 'on');
546 $msg = '<p class="success_msg">'.__('Monkey Rewards turned On!', 'mailchimp_i18n').'</p>';
547 mailchimpSF_global_msg($msg);
548 } else if (get_option('mc_rewards')!='off') {
549 update_option('mc_rewards', 'off');
550 $msg = '<p class="success_msg">'.__('Monkey Rewards turned Off!', 'mailchimp_i18n').'</p>';
551 mailchimpSF_global_msg($msg);
552 }
553 if (isset($_POST['mc_use_javascript'])){
554 update_option('mc_use_javascript', 'on');
555 $msg = '<p class="success_msg">'.__('Fancy Javascript submission turned On!', 'mailchimp_i18n').'</p>';
556 mailchimpSF_global_msg($msg);
557 } else if (get_option('mc_use_javascript')!='off') {
558 update_option('mc_use_javascript', 'off');
559 $msg = '<p class="success_msg">'.__('Fancy Javascript submission turned Off!', 'mailchimp_i18n').'</p>';
560 mailchimpSF_global_msg($msg);
561 }
562
563 if (isset($_POST['mc_use_datepicker'])){
564 update_option('mc_use_datepicker', 'on');
565 $msg = '<p class="success_msg">'.__('Datepicker turned On!', 'mailchimp_i18n').'</p>';
566 mailchimpSF_global_msg($msg);
567 } else if (get_option('mc_use_datepicker')!='off') {
568 update_option('mc_use_datepicker', 'off');
569 $msg = '<p class="success_msg">'.__('Datepicker turned Off!', 'mailchimp_i18n').'</p>';
570 mailchimpSF_global_msg($msg);
571 }
572
573 if (isset($_POST['mc_use_unsub_link'])){
574 update_option('mc_use_unsub_link', 'on');
575 $msg = '<p class="success_msg">'.__('Unsubscribe link turned On!', 'mailchimp_i18n').'</p>';
576 mailchimpSF_global_msg($msg);
577 } else if (get_option('mc_use_unsub_link')!='off') {
578 update_option('mc_use_unsub_link', 'off');
579 $msg = '<p class="success_msg">'.__('Unsubscribe link turned Off!', 'mailchimp_i18n').'</p>';
580 mailchimpSF_global_msg($msg);
581 }
582
583 $content = stripslashes($_POST['mc_header_content']);
584 $content = str_replace("\r\n","<br/>", $content);
585 update_option('mc_header_content', $content );
586
587 $content = stripslashes($_POST['mc_subheader_content']);
588 $content = str_replace("\r\n","<br/>", $content);
589 update_option('mc_subheader_content', $content );
590
591
592 $submit_text = stripslashes($_POST['mc_submit_text']);
593 $submit_text = str_replace("\r\n","", $submit_text);
594 update_option('mc_submit_text', $submit_text);
595
596 // Set Custom Style option
597 update_option('mc_custom_style', isset($_POST['mc_custom_style']) ? 'on' : 'off');
598
599 //we told them not to put these things we are replacing in, but let's just make sure they are listening...
600 update_option('mc_header_border_width',str_replace('px','',$_POST['mc_header_border_width']) );
601 update_option('mc_header_border_color', str_replace('#','',$_POST['mc_header_border_color']));
602 update_option('mc_header_background',str_replace('#','',$_POST['mc_header_background']));
603 update_option('mc_header_text_color', str_replace('#','',$_POST['mc_header_text_color']));
604
605 update_option('mc_form_border_width',str_replace('px','',$_POST['mc_form_border_width']) );
606 update_option('mc_form_border_color', str_replace('#','',$_POST['mc_form_border_color']));
607 update_option('mc_form_background',str_replace('#','',$_POST['mc_form_background']));
608 update_option('mc_form_text_color', str_replace('#','',$_POST['mc_form_text_color']));
609
610 $igs = get_option('mc_interest_groups');
611 if (is_array($igs)) {
612 foreach($igs as $var){
613 $opt = 'mc_show_interest_groups_'.$var['id'];
614 if (isset($_POST[$opt])){
615 update_option($opt,'on');
616 } else {
617 update_option($opt,'off');
618 }
619 }
620 }
621
622 $mv = get_option('mc_merge_vars');
623 if (is_array($mv)) {
624 foreach($mv as $var){
625 $opt = 'mc_mv_'.$var['tag'];
626 if (isset($_POST[$opt]) || $var['req']=='Y'){
627 update_option($opt,'on');
628 } else {
629 update_option($opt,'off');
630 }
631 }
632 }
633 $msg = '<p class="success_msg">'.esc_html(__('Successfully Updated your List Subscribe Form Settings!', 'mailchimp_i18n')).'</p>';
634 mailchimpSF_global_msg($msg);
635 }
636
637 /**
638 * Sees if the user changed the list, and updates options accordingly
639 **/
640 function mailchimpSF_change_list_if_necessary($api_key) {
641 // Simple permission check before going through all this
642 if (!current_user_can(MCSF_CAP_THRESHOLD)) { return; }
643
644 $api = new mailchimpSF_MCAPI($api_key);
645 //we *could* support paging, but few users have that many lists (and shouldn't)
646 $lists = $api->lists(array(),0,100);
647 $lists = $lists['data'];
648
649 if (is_array($lists) && !empty($lists) && isset($_POST['mc_list_id'])) {
650
651 /* If our incoming list ID (the one chosen in the select dropdown)
652 is in our array of lists, the set it to be the active list */
653 foreach($lists as $key => $list) {
654 if ($list['id'] == $_POST['mc_list_id']) {
655 $list_id = $_POST['mc_list_id'];
656 $list_name = $list['name'];
657 $list_key = $key;
658 }
659 }
660
661 $orig_list = get_option('mc_list_id');
662 if ($list_id != '') {
663 update_option('mc_list_id', $list_id);
664 update_option('mc_list_name', $list_name);
665 update_option('mc_email_type_option', $lists[$list_key]['email_type_option']);
666
667
668 // See if the user changed the list
669 if ($orig_list != $list_id){
670 // The user changed the list, Reset the Form Defaults
671 mailchimpSF_set_form_defaults($list_name);
672 }
673 // email_type_option
674
675 // Grab the merge vars and interest groups
676 $mv = $api->listMergeVars($list_id);
677 $igs = $api->listInterestGroupings($list_id);
678
679 update_option('mc_merge_vars', $mv);
680 foreach($mv as $var){
681 $opt = 'mc_mv_'.$var['tag'];
682 //turn them all on by default
683 if ($orig_list != $list_id) {
684 update_option($opt, 'on' );
685 }
686 }
687 update_option('mc_interest_groups', $igs);
688 if (is_array($igs)) {
689 foreach ($igs as $var){
690 $opt = 'mc_show_interest_groups_'.$var['id'];
691 //turn them all on by default
692 if ($orig_list != $list_id) {
693 update_option($opt, 'on' );
694 }
695 }
696 }
697 $igs_text = ' ';
698 if (is_array($igs)) {
699 $igs_text .= sprintf(__('and %s Sets of Interest Groups', 'mailchimp_i18n'), count($igs));
700 }
701
702 $msg = '<p class="success_msg">'.
703 sprintf(
704 __('Success! Loaded and saved the info for %d Merge Variables', 'mailchimp_i18n').$igs_text,
705 count($mv)
706 ).' '.
707 __('from your list').' "'.$list_name.'"<br/><br/>'.
708 __('Now you should either Turn On the MailChimp Widget or change your options below, then turn it on.', 'mailchimp_i18n').'</p>';
709 mailchimpSF_global_msg($msg);
710 }
711 }
712 }
713
714 /**
715 * Outputs the Settings/Options page
716 */
717 function mailchimpSF_setup_page() {
718 ?>
719 <div class="wrap">
720
721 <h2><?php esc_html_e('MailChimp List Setup', 'mailchimp_i18n');?> </h2>
722
723 <?php
724
725 $user = get_option('mc_username');
726 $api_key = get_option('mc_apikey');
727
728 // If we have an API Key, see if we need to change the lists and its options
729 if (!empty($api_key)){
730 mailchimpSF_change_list_if_necessary($api_key);
731 }
732
733
734
735 // Display our success/error message(s) if have them
736 if (mailchimpSF_global_msg() != ''){
737 // Message has already been html escaped, so we don't want to 2x escape it here
738 ?>
739 <div id="mc_message" class=""><?php echo mailchimpSF_global_msg(); ?></div>
740 <?php
741 }
742
743
744 // If we don't have an API Key, do a login form
745 if (get_option('mc_apikey') == '') {
746 ?>
747 <div>
748 <form method="post" action="options-general.php?page=mailchimpSF_options">
749 <h3><?php esc_html_e('Login Info', 'mailchimp_i18n');?></h3>
750 <?php esc_html_e('To start using the MailChimp plugin, we first need to login and get your API Key. Please enter your MailChimp API Key below.', 'mailchimp_i18n'); ?>
751
752 <br/>
753
754 <?php
755 echo sprintf(
756 '%1$s <a href="http://www.mailchimp.com/signup/" target="_blank">%2$s</a>',
757 esc_html(__("Don't have a MailChimp account?", 'mailchimp_i18n')),
758 esc_html(__('Try one for Free!', 'mailchimp_i18n'))
759 );
760 ?>
761
762 <br/>
763
764 <table class="form-table">
765 <tr valign="top">
766 <th scope="row"><?php esc_html_e('API Key', 'mailchimp_i18n'); ?>:</th>
767 <td>
768 <input name="mc_apikey" type="text" id="mc_apikey" class="code" value="<?php echo esc_attr($api_key); ?>" size="32" />
769 <br/>
770 <a href="http://admin.mailchimp.com/account/api-key-popup" target="_blank">get your API Key here</a>
771 </td>
772 </tr>
773 </table>
774
775 <input type="hidden" name="mcsf_action" value="update_mc_apikey"/>
776 <input type="submit" name="Submit" value="<?php esc_attr_e('Save & Check', 'mailchimp_i18n');?>" class="button" />
777 <?php wp_nonce_field('update_mc_api_key', '_mcsf_nonce_action'); ?>
778 </form>
779 </div>
780
781 <?php
782 if (get_option('mc_username')!=''){
783 ?>
784 <strong><?php esc_html_e('Notes', 'mailchimp_i18n'); ?>:</strong>
785 <ul>
786 <li><em><?php esc_html_e('Changing your settings at MailChimp.com may cause this to stop working.', 'mailchimp_i18n'); ?></em></li>
787 <li><em><?php esc_html_e('If you change your login to a different account, the info you have setup below will be erased.', 'mailchimp_i18n'); ?></em></li>
788 <li><em><?php esc_html_e('If any of that happens, no biggie - just reconfigure your login and the items below...', 'mailchimp_i18n'); ?></em></li>
789 </ul>
790 <br/>
791 <?php
792 }
793 } // End of login form
794
795 // Start logout form
796 else {
797 ?>
798 <table style="min-width:400px;">
799 <tr>
800 <td><h3><?php esc_html_e('Logged in as', 'mailchimp_i18n');?>: <?php echo esc_html(get_option('mc_username')); ?></h3>
801 </td>
802 <td>
803 <form method="post" action="options-general.php?page=mailchimpSF_options">
804 <input type="hidden" name="mcsf_action" value="logout"/>
805 <input type="submit" name="Submit" value="<?php esc_attr_e('Logout', 'mailchimp_i18n');?>" class="button" />
806 <?php wp_nonce_field('mc_logout', '_mcsf_nonce_action'); ?>
807 </form>
808 </td>
809 </tr>
810 </table>
811 <?php
812 } // End Logout form
813
814
815
816 //Just get out if nothing else matters...
817 if (get_option('mc_apikey') == '') return;
818
819 if (get_option('mc_apikey')!=''){
820 ?>
821 <h3><?php esc_html_e('Your Lists', 'mailchimp_i18n'); ?></h3>
822
823 <div>
824
825 <p><?php esc_html_e('Please select the List you wish to create a Signup Form for.', 'mailchimp_i18n'); ?></p>
826
827 <form method="post" action="options-general.php?page=mailchimpSF_options">
828 <?php
829 $api = new mailchimpSF_MCAPI(get_option('mc_apikey'));
830 //we *could* support paging, but few users have that many lists (and shouldn't)
831 $lists = $api->lists(array(),0,100);
832 $lists = $lists['data'];
833
834 if (count($lists) == 0) {
835 ?>
836 <span class='error_msg'>
837 <?php
838 echo sprintf(
839 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')),
840 "<a href='http://www.mailchimp.com/'>MailChimp</a>"
841 );
842 ?>
843 </span>
844 <?php
845 }
846 else {
847 ?>
848 <table style="min-width:400px">
849 <tr>
850 <td>
851 <select name="mc_list_id" style="min-width:200px;">
852 <option value=""> &mdash; <?php esc_html_e('Select A List','mailchimp_i18n'); ?> &mdash; </option>
853 <?php
854 foreach ($lists as $list) {
855 $option = get_option('mc_list_id');
856 ?>
857 <option value="<?php echo esc_attr($list['id']); ?>"<?php selected($list['id'], $option); ?>><?php echo esc_html($list['name']); ?></option>
858 <?php
859 }
860 ?>
861 </select>
862 </td>
863 <td>
864 <input type="hidden" name="mcsf_action" value="update_mc_list_id" />
865 <input type="submit" name="Submit" value="<?php esc_attr_e('Update List', 'mailchimp_i18n'); ?>" class="button" />
866 </td>
867 </tr>
868 <tr>
869 <td colspan="2">
870 <strong><?php esc_html_e('Note:', 'mailchimp_i18n'); ?></strong> <em><?php esc_html_e('Updating your list will not cause settings below to be lost. Changing to a new list will.', 'mailchimp_i18n'); ?></em>
871 </td>
872 </tr>
873 </table>
874 <?php
875 } //end select list
876 ?>
877 </form>
878 </div>
879
880 <br/>
881
882 <?php
883 }
884 else {
885 //display the selected list...
886 ?>
887
888 <p class="submit">
889 <form method="post" action="options-general.php?page=mailchimpSF_options">
890 <input type="hidden" name="mcsf_action" value="reset_list" />
891 <input type="submit" name="reset_list" value="<?php esc_attr_e('Reset List Options and Select again', 'mailchimp_i18n'); ?>" class="button" />
892 <?php wp_nonce_field('reset_mailchimp_list', '_mcsf_nonce_action'); ?>
893 </form>
894 </p>
895 <h3><?php esc_html_e('Subscribe Form Widget Settings for this List', 'mailchimp_i18n'); ?>:</h3>
896 <h4><?php esc_html_e('Selected MailChimp List', 'mailchimp_i18n'); ?>: <?php echo esc_html(get_option('mc_list_name')); ?></h4>
897 <?php
898 }
899 //Just get out if nothing else matters...
900 if (get_option('mc_list_id') == '') return;
901
902
903 // The main Settings form
904 ?>
905
906 <div>
907 <form method="post" action="options-general.php?page=mailchimpSF_options">
908 <div style="width:600px;">
909 <input type="hidden" name="mcsf_action" value="change_form_settings">
910 <?php wp_nonce_field('update_general_form_settings', '_mcsf_nonce_action'); ?>
911 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button" />
912 <table class="widefat">
913 <tr valign="top">
914 <th scope="row"><?php esc_html_e('Monkey Rewards', 'mailchimp_i18n'); ?>:</th>
915 <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" />
916 <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>
917 </td>
918 </tr>
919 <tr valign="top">
920 <th scope="row"><?php esc_html_e('Use Javascript Support?', 'mailchimp_i18n'); ?>:</th>
921 <td><input name="mc_use_javascript" type="checkbox" <?php checked(get_option('mc_use_javascript'), 'on'); ?> id="mc_use_javascript" class="code" />
922 <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>
923 </td>
924 </tr>
925 <tr valign="top">
926 <th scope="row"><?php esc_html_e('Use Javascript Datepicker?', 'mailchimp_i18n'); ?>:</th>
927 <td><input name="mc_use_datepicker" type="checkbox" <?php checked(get_option('mc_use_datepicker'), 'on'); ?> id="mc_use_datepicker" class="code" />
928 <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>
929 </td>
930 </tr>
931 <tr valign="top">
932 <th scope="row"><?php esc_html_e('Include Unsubscribe link?', 'mailchimp_i18n'); ?>:</th>
933 <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" />
934 <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>
935 </td>
936 </tr>
937 <tr valign="top">
938 <th scope="row"><?php esc_html_e('Header content', 'mailchimp_i18n'); ?>:</th>
939 <td>
940 <textarea name="mc_header_content" rows="2" cols="50"><?php echo esc_html(get_option('mc_header_content')); ?></textarea><br/>
941 <em><?php esc_html_e('You can fill this with your own Text, HTML markup (including image links), or Nothing!', 'mailchimp_i18n'); ?></em>
942 </td>
943 </tr>
944
945 <tr valign="top">
946 <th scope="row"><?php esc_html_e('Sub-header content', 'mailchimp_i18n'); ?>:</th>
947 <td>
948 <textarea name="mc_subheader_content" rows="2" cols="50"><?php echo esc_html(get_option('mc_subheader_content')); ?></textarea><br/>
949 <em><?php esc_html_e('You can fill this with your own Text, HTML markup (including image links), or Nothing!', 'mailchimp_i18n'); ?></em>.
950 <?php esc_html_e('This will be displayed under the heading and above the form.', 'mailchimp_i18n'); ?>
951 </td>
952 </tr>
953
954
955 <tr valign="top">
956 <th scope="row"><?php esc_html_e('Submit Button text', 'mailchimp_i18n'); ?>:</th>
957 <td>
958 <input type="text" name="mc_submit_text" size="30" value="<?php echo esc_attr(get_option('mc_submit_text')); ?>"/>
959 </td>
960 </tr>
961
962 <tr valign="top">
963 <th scope="row"><?php esc_html_e('Custom Styling', 'mailchimp_i18n'); ?>:</th>
964 <td>
965 <table class="widefat">
966
967 <tr><th><label for="mc_custom_style"><?php esc_html_e('Turned On?', 'mailchimp_i18n'); ?></label></th><td><input type="checkbox" name="mc_custom_style" id="mc_custom_style"<?php checked(get_option('mc_custom_style'), 'on'); ?> /></td></tr>
968 <tr><th colspan="2"><?php esc_html_e('Header Settings (only applies if there are no HTML tags in the Header Content area above)', 'mailchimp_i18n'); ?>:</th></tr>
969 <tr><th><?php esc_html_e('Border Width', 'mailchimp_i18n'); ?>:</th><td><input type="text" name="mc_header_border_width" size="3" maxlength="3" value="<?php echo esc_attr(get_option('mc_header_border_width')); ?>"/> px<br/>
970 <em><?php esc_html_e('Set to 0 for no border, do not enter', 'mailchimp_i18n'); ?> <strong>px</strong>!</em>
971 </td></tr>
972 <tr><th><?php esc_html_e('Border Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_header_border_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_header_border_color')); ?>"/><br/>
973 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
974 </td></tr>
975 <tr><th><?php esc_html_e('Text Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_header_text_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_header_text_color')); ?>"/><br/>
976 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
977 </td></tr>
978 <tr><th><?php esc_html_e('Background Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_header_background" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_header_background')); ?>"/><br/>
979 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
980 </td></tr>
981
982 <tr><th colspan="2"><?php esc_html_e('Form Settings', 'mailchimp_i18n'); ?>:</th></tr>
983 <tr><th><?php esc_html_e('Border Width', 'mailchimp_i18n'); ?>:</th><td><input type="text" name="mc_form_border_width" size="3" maxlength="3" value="<?php echo esc_attr(get_option('mc_form_border_width')); ?>"/> px<br/>
984 <em><?php esc_html_e('Set to 0 for no border, do not enter', 'mailchimp_i18n'); ?> <strong>px</strong>!</em>
985 </td></tr>
986 <tr><th><?php esc_html_e('Border Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_form_border_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_form_border_color')); ?>"/><br/>
987 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
988 </td></tr>
989 <tr><th><?php esc_html_e('Text Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_form_text_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_form_text_color')); ?>"/><br/>
990 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
991 </td></tr>
992 <tr><th><?php esc_html_e('Background Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_form_background" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_form_background')); ?>"/><br/>
993 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
994 </td></tr>
995 </table>
996 </td>
997 </tr>
998 </table>
999 </div>
1000 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button" />
1001
1002 <?php
1003 // Merge Variables Table
1004 ?>
1005 <div style="width:400px;">
1006
1007 <h4><?php esc_html_e('Merge Variables Included', 'mailchimp_i18n'); ?></h4>
1008
1009 <?php
1010 $mv = get_option('mc_merge_vars');
1011
1012 if (count($mv) == 0 || !is_array($mv)){
1013 ?>
1014 <em><?php esc_html_e('No Merge Variables found.', 'mailchimp_i18n'); ?></em>
1015 <?php
1016 } else {
1017 ?>
1018
1019 <table class='widefat'>
1020 <tr valign="top">
1021 <th><?php esc_html_e('Name', 'mailchimp_i18n');?></th>
1022 <th><?php esc_html_e('Tag', 'mailchimp_i18n');?></th>
1023 <th><?php esc_html_e('Required?', 'mailchimp_i18n');?></th>
1024 <th><?php esc_html_e('Include?', 'mailchimp_i18n');?></th>
1025 </tr>
1026 <?php
1027 foreach($mv as $var){
1028 ?>
1029 <tr valign="top">
1030 <td><?php echo esc_html($var['name']); ?></td>
1031 <td><?php echo esc_html($var['tag']); ?></td>
1032 <td><?php echo esc_html(($var['req'] == 1) ? 'Y' : 'N'); ?></td>
1033 <td>
1034 <?php
1035 if (!$var['req']){
1036 $opt = 'mc_mv_'.$var['tag'];
1037 ?>
1038 <input name="<?php echo esc_attr($opt); ?>" type="checkbox" id="<?php echo esc_attr($opt); ?>" class="code"<?php checked(get_option($opt), 'on'); ?> />
1039 <?php
1040 } else {
1041 ?>
1042 &nbsp;&mdash;&nbsp;
1043 <?php
1044 }
1045 ?>
1046 </td>
1047 </tr>
1048 <?php
1049 }
1050 ?>
1051 </table>
1052 <?php
1053 }
1054
1055 ?>
1056
1057 <h4><?php esc_html_e('Interest Groups', 'mailchimp_i18n'); ?></h4>
1058
1059 <?php
1060 // Interest Groups Table
1061 $igs = get_option('mc_interest_groups');
1062 if (is_array($igs) && !isset($igs['id'])) {
1063 // Determines whether or not to continue processing. Only false if there was an error.
1064 $continue = true;
1065 foreach ($igs as $ig) {
1066 if ($continue) {
1067 if (!is_array($ig) || empty($ig) || $ig == 'N' ) {
1068 ?>
1069 <em><?php esc_html_e('No Interest Groups Setup for this List', 'mailchimp_i18n'); ?></em>
1070 <?php
1071 $continue = false;
1072 }
1073 else {
1074 ?>
1075 <table class='widefat'>
1076 <tr valign="top">
1077 <th width="75px">
1078 <label for="<?php echo esc_attr('mc_show_interest_groups_'.$ig['id']); ?>"><?php esc_html_e('Show?', 'mailchimp_i18n'); ?></label>
1079 </th>
1080 <th>
1081 <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'])); ?> />
1082 </th>
1083 </tr>
1084 <tr valign="top">
1085 <th><?php esc_html_e('Name', 'mailchimp_i18n'); ?>:</th>
1086 <th><?php echo esc_html($ig['name']); ?></th>
1087 </tr>
1088 <tr valign="top">
1089 <th><?php esc_html_e('Input Type', 'mailchimp_i18n'); ?>:</th>
1090 <td><?php echo esc_html($ig['form_field']); ?></td>
1091 </tr>
1092 <tr valign="top">
1093 <th><?php esc_html_e('Options', 'mailchimp_i18n'); ?>:</th>
1094 <td>
1095 <ul>
1096 <?php
1097 foreach($ig['groups'] as $interest){
1098 ?>
1099 <li><?php echo esc_html($interest['name']); ?></li>
1100 <?php
1101 }
1102 ?>
1103 </ul>
1104 </td>
1105 </tr>
1106 </table>
1107 <?php
1108 }
1109 }
1110 }
1111 }
1112 else {
1113 ?>
1114 <em><?php esc_html_e('Error retrieving interest groups. Please re-import the list.', 'mailchimp_i18n'); ?></em>
1115 <?php
1116 }
1117
1118 ?>
1119 <p class="submit">
1120 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button" />
1121 </p>
1122 </div>
1123 </form>
1124 </div>
1125 </div><!--wrap-->
1126 <?php
1127 }//mailchimpSF_setup_page()
1128
1129
1130 function mailchimpSF_register_widgets() {
1131 register_widget('mailchimpSF_Widget');
1132 }
1133 add_action('widgets_init', 'mailchimpSF_register_widgets');
1134
1135 function mailchimpSF_shortcode($atts){
1136 ob_start();
1137 mailchimpSF_signup_form();
1138 return ob_get_clean();
1139 }
1140 add_shortcode('mailchimpsf_form', 'mailchimpSF_shortcode');
1141
1142 /**
1143 * Attempts to signup a user, per the $_POST args.
1144 *
1145 * This sets a global message, that is then used in the widget
1146 * output to retrieve and display that message.
1147 *
1148 * @return bool
1149 */
1150 function mailchimpSF_signup_submit() {
1151 $mv = get_option('mc_merge_vars', array());
1152 $mv_tag_keys = array();
1153
1154 $igs = get_option('mc_interest_groups', array());
1155
1156 $success = true;
1157 $listId = get_option('mc_list_id');
1158 $email = isset($_POST['mc_mv_EMAIL']) ? strip_tags(stripslashes($_POST['mc_mv_EMAIL'])) : '';
1159 $merge = $errs = array(); // Set up some vars
1160
1161 // Loop through our Merge Vars, and if they're empty, but required, then print an error, and mark as failed
1162 foreach($mv as $var) {
1163 $opt = 'mc_mv_'.$var['tag'];
1164
1165 $opt_val = isset($_POST[$opt]) ? $_POST[$opt] : '';
1166
1167 if (is_array($opt_val)) {
1168 $opt_val = implode('', $opt_val);
1169 }
1170
1171 if ($var['req'] == 'Y' && trim($opt_val) == '') {
1172 $success = false;
1173 $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($var['name']));
1174 }
1175 else {
1176 if ($var['tag'] != 'EMAIL') {
1177 $merge[$var['tag']] = $opt_val;
1178 }
1179 }
1180
1181 // We also want to create an array where the keys are the tags for easier validation later
1182 $mv_tag_keys[$var['tag']] = $var;
1183
1184 }
1185
1186 // Head back to the beginning of the merge vars array
1187 reset($mv);
1188
1189 foreach ($igs as $ig) {
1190 if (get_option('mc_show_interest_groups_'.$ig['id']) == 'on') {
1191 $groupings = array();
1192 switch ($ig['form_field']) {
1193 case 'select':
1194 case 'dropdown':
1195 case 'radio':
1196 if (isset($_POST['group'][$ig['id']])) {
1197 $groupings = array(
1198 'id' => $ig['id'],
1199 'groups' => str_replace(',', '\,', $_POST['group'][$ig['id']]),
1200 );
1201 }
1202 break;
1203 case 'checkboxes':
1204 case 'checkbox':
1205 if (isset($_POST['group'][$ig['id']])) {
1206 foreach ($_POST['group'][$ig['id']] as $i => $value) {
1207 $groups .= str_replace(',', '\,', $value).',';
1208
1209 }
1210 $groupings = array(
1211 'id' => $ig['id'],
1212 'groups' => $groups,
1213 );
1214 }
1215 break;
1216 default:
1217 // Nothing
1218 break;
1219 }
1220 if (!isset($merge['GROUPINGS']) || !is_array($merge['GROUPINGS'])) {
1221 $merge['GROUPINGS'] = array();
1222 }
1223 if (!empty($groupings)) {
1224 $merge['GROUPINGS'][] = $groupings;
1225 }
1226 }
1227 }
1228
1229 // If we're good
1230 if ($success) {
1231 // Clear out empty merge vars
1232
1233 foreach ($merge as $k => $v) {
1234 if (is_array($v) && empty($v)) {
1235 unset($merge[$k]);
1236 }
1237 else if (!is_array($v) && trim($v) === '') {
1238 unset($merge[$k]);
1239 }
1240 }
1241
1242 // If we have an empty $merge, then assign empty string.
1243 if (count($merge) == 0 || $merge == '') {
1244 $merge = '';
1245 }
1246
1247 if (isset($_POST['email_type']) && in_array($_POST['email_type'], array('text', 'html', 'mobile'))) {
1248 $email_type = $_POST['email_type'];
1249 }
1250 else {
1251 $email_type = 'html';
1252 }
1253
1254 // Custom validation based on type
1255 if (is_array($merge) && !empty($merge)) {
1256 foreach ($merge as $merge_key => $merge_value) {
1257 if ($merge_key !== 'GROUPINGS') {
1258 switch ($mv_tag_keys[$merge_key]['field_type']) {
1259 case 'phone':
1260 $phone = implode($merge_value, '');
1261 if (!empty($phone)) {
1262 if (preg_match('/[^0-9]/', $phone)) {
1263 $errs[] = sprintf(__("%s must consist of only numbers", 'mailchimp_i18n'), esc_html($mv_tag_keys[$merge_key]['name']));
1264 $success = false;
1265 }
1266 }
1267 break;
1268
1269 default:
1270 break;
1271 }
1272 }
1273 }
1274 }
1275 if ($success) {
1276 $api = new mailchimpSF_MCAPI(get_option('mc_apikey'));
1277 $retval = $api->listSubscribe( $listId, $email, $merge, $email_type);
1278 if (!$retval) {
1279 switch($api->errorCode) {
1280 case '105' :
1281 $errs[] = __("Please try again later", 'mailchimp_i18n').'.';
1282 break;
1283 case '214' :
1284 $errs[] = __("That email address is already subscribed to the list", 'mailchimp_i18n').'.';
1285 break;
1286 case '250' :
1287 list($field, $rest) = explode(' ', $api->errorMessage, 2);
1288 $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name']));
1289 break;
1290 case '254' :
1291 list($i1, $i2, $i3, $field, $rest) = explode(' ',$api->errorMessage,5);
1292 $errs[] = sprintf(__("%s has invalid content.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name']));
1293 break;
1294 case '270' :
1295 $errs[] = __("An invalid Interest Group was selected", 'mailchimp_i18n').'.';
1296 break;
1297 case '502' :
1298 $errs[] = __("That email address is invalid", 'mailchimp_i18n').'.';
1299 break;
1300 default:
1301 $errs[] = $api->errorCode.":".$api->errorMessage;
1302 break;
1303 }
1304 $success = false;
1305 }
1306 }
1307 }
1308
1309 // If we have errors, then show them
1310 if (count($errs) > 0) {
1311 $msg = '<span class="mc_error_msg">';
1312 foreach($errs as $error){
1313 $msg .= '&raquo; '.esc_html($error).'<br />';
1314 }
1315 $msg .= '</span>';
1316 }
1317 else {
1318 $msg = "<strong class='mc_success_msg'>".esc_html(__("Success, you've been signed up! Please look for our confirmation email!", 'mailchimp_i18n'))."</strong>";
1319 }
1320
1321 // Set our global message
1322 mailchimpSF_global_msg($msg);
1323
1324 return $success;
1325 }
1326
1327
1328
1329 /**********************
1330 * Utility Functions *
1331 **********************/
1332 /**
1333 * Utility function to allow placement of plugin in plugins, mu-plugins, child or parent theme's plugins folders
1334 *
1335 * This function must be ran _very early_ in the load process, as it sets up important constants for the rest of the plugin
1336 */
1337 function mailchimpSF_where_am_i() {
1338 $locations = array(
1339 'plugins' => array(
1340 'dir' => WP_PLUGIN_DIR,
1341 'url' => plugins_url()
1342 ),
1343 'mu_plugins' => array(
1344 'dir' => WPMU_PLUGIN_DIR,
1345 'url' => plugins_url(),
1346 ),
1347 'template' => array(
1348 'dir' => trailingslashit(get_template_directory()).'plugins/',
1349 'url' => trailingslashit(get_template_directory_uri()).'plugins/',
1350 ),
1351 'stylesheet' => array(
1352 'dir' => trailingslashit(get_stylesheet_directory()).'plugins/',
1353 'url' => trailingslashit(get_stylesheet_directory_uri()).'plugins/',
1354 ),
1355 );
1356
1357 // Set defaults
1358 $mscf_dir = trailingslashit(WP_PLUGIN_DIR).'mailchimp/';
1359 $mscf_url = trailingslashit(WP_PLUGIN_URL).'mailchimp/';
1360
1361 // Try our hands at finding the real location
1362 foreach ($locations as $key => $loc) {
1363 $dir = trailingslashit($loc['dir']).'mailchimp/';
1364 $url = trailingslashit($loc['url']).'mailchimp/';
1365 if (is_file($dir.basename(__FILE__))) {
1366 $mscf_dir = $dir;
1367 $mscf_url = $url;
1368 break;
1369 }
1370 }
1371
1372 // Define our complete filesystem path
1373 define('MCSF_DIR', $mscf_dir);
1374
1375 /* Lang location needs to be relative *from* ABSPATH,
1376 so strip it out of our language dir location */
1377 define('MCSF_LANG_DIR', trailingslashit(MCSF_DIR).'po/');
1378
1379 // Define our complete URL to the plugin folder
1380 define('MCSF_URL', $mscf_url);
1381 }
1382
1383
1384 ?>
1385