'mailchimpSF_options'), admin_url('options-general.php')); $settings_link = ''.__('Settings', 'mailchimp_i18n' ).''; array_unshift($links, $settings_link); return $links; } add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'mailchimpSD_plugin_action_links', 10, 1); /** * Loads the appropriate JS and CSS resources depending on * settings and context (admin or not) * * @return void */ function mailchimpSF_load_resources() { // JS if (get_option('mc_use_javascript') == 'on') { if (!is_admin()) { wp_enqueue_script('jquery_scrollto', MCSF_URL.'js/scrollTo.js', array('jquery'), MCSF_VER); wp_enqueue_script('mailchimpSF_main_js', MCSF_URL.'js/mailchimp.js', array('jquery', 'jquery-form'), MCSF_VER); // some javascript to get ajax version submitting to the proper location global $wp_scripts; $wp_scripts->localize('mailchimpSF_main_js', 'mailchimpSF', array( 'ajax_url' => trailingslashit(home_url()), )); } } if (get_option('mc_use_datepicker') == 'on' && !is_admin()) { // Datepicker theme wp_enqueue_style('flick', MCSF_URL.'/css/flick/flick.css'); // Datepicker JS wp_enqueue_script('datepicker', MCSF_URL.'/js/datepicker.js', array('jquery','jquery-ui-core')); } wp_enqueue_style('mailchimpSF_main_css', home_url('?mcsf_action=main_css&ver='.MCSF_VER)); wp_enqueue_style('mailchimpSF_ie_css', MCSF_URL.'css/ie.css'); global $wp_styles; $wp_styles->add_data( 'mailchimpSF_ie_css', 'conditional', 'IE' ); } /** * Loads resources for the MailChimp admin page * * @return void */ function mc_admin_page_load_resources() { wp_enqueue_style('mailchimpSF_admin_css', MCSF_URL.'css/admin.css'); wp_enqueue_script('mailchimpSF_admin_js', MCSF_URL.'js/admin.js'); } add_action('load-settings_page_mailchimpSF_options', 'mc_admin_page_load_resources'); /** * Loads jQuery Datepicker for the date-pick class **/ function mc_datepicker_load() { ?> .mc_error_msg { color: red; margin-bottom: 1.0em; } .mc_success_msg { color: green; margin-bottom: 1.0em; } .mc_merge_var{ padding:0; margin:0; } #mc_signup_form { padding:5px; border-width: px; border-style: ; border-color: #; color: #; background-color: #; } .mc_custom_border_hdr { border-width: px; border-style: ; border-color: #; color: #; background-color: #; padding:5px 10px; width: 100%; } #mc_signup_container {} #mc_signup_form {} #mc_signup_form .mc_var_label {} #mc_signup_form .mc_input {} #mc-indicates-required { width:100%; } #mc_display_rewards {} .mc_interests_header { font-weight:bold; } div.mc_interest{ width:100%; } #mc_signup_form input.mc_interest {} #mc_signup_form select {} #mc_signup_form label.mc_interest_label { display:inline; } .mc_signup_submit { text-align:center; } ul.mc_list { list-style-type: none; } ul.mc_list li { font-size: 12px; } .ui-datepicker-year { display: none; } #ui-datepicker-div.show .ui-datepicker-year { display: inline; padding-left: 3px } getApiUrl('authorize')); if (strpos($proxy, 'socialize-this') !== false) { $salt = mailchimpSF_auth_nonce_salt(); $id = wp_create_nonce(mailchimpSF_auth_nonce_key($salt)); $url = home_url('index.php'); $args = array( 'mcsf_action' => 'authorized', 'salt' => $salt, 'user_id' => get_current_user_id(), ); $proxy = add_query_arg(array( 'id' => $id, 'response_url' => urlencode(add_query_arg($args, $url)) ), $proxy); $proxy = apply_filters('mailchimp_proxy_url', $proxy); } wp_redirect($proxy); exit; } function mailchimpSF_authorized() { // User ID on the request? Must be set before nonce comparison $user_id = stripslashes($_GET['user_id']); if ($user_id !== null) { wp_set_current_user($user_id); } $nonce = stripslashes($_POST['id']); $salt = stripslashes($_GET['salt']); if (wp_verify_nonce($nonce, mailchimpSF_auth_nonce_key($salt)) === false) { wp_die('Cheatin’ huh?'); } $response = stripslashes_deep($_POST['response']); if (!isset($response['keys']) || !isset($response['user'])) { wp_die('Something went wrong, please try again'); } update_option('mc_sopresto_user', $response['user']); update_option('mc_sopresto_dc', $response['dc']); update_option('mc_sopresto_public_key', $response['keys']['public']); update_option('mc_sopresto_secret_key', $response['keys']['secret']); exit; } /** * Upgrades data if it needs to. Checks on admin_init * * @return void */ function mailchimpSF_upgrade() { // See if we need an upgrade if (mailchimpSF_needs_upgrade()) { // remove password option if it's set (0.5) // Update interest group data mailchimpSF_do_upgrade(); } } add_action('admin_init', 'mailchimpSF_upgrade'); /** * Creates new Sopresto API object * * @return Sopresto_MailChimp|false */ function mailchimpSF_get_api($force = false) { $public_key = get_option('mc_sopresto_public_key'); $secret_key = get_option('mc_sopresto_secret_key'); if ($public_key && $secret_key || $force) { return new Sopresto_MailChimp($public_key, $secret_key, '1.3'); } return false; } /** * Checks to see if we're storing a password, if so, we need * to upgrade to the API key * * @return bool **/ function mailchimpSF_needs_upgrade() { $igs = get_option('mc_interest_groups'); if ($igs !== false // we have an option && ( empty($igs) || // it can be an empty array (no interest groups) (is_array($igs) && isset($igs[0]['id'])) // OR it should be a populated array that's well-formed )) { return false; // no need to upgrade } else { return true; // yeah, let's do it } } /** * 1.2.4 -> 1.2.5 - Update to support multiple interest groups * MCAPIv1.2 -> MCAPIv1.3 - update interest groups * 2011-02-09 - old password upgrade code deleted as 0.5 is way old */ function mailchimpSF_do_upgrade() { # TODO: reload this } /** * Deletes all mailchimp options **/ function mailchimpSF_delete_setup() { delete_option('mc_user_id'); delete_option('mc_sopresto_user'); delete_option('mc_sopresto_public_key'); delete_option('mc_sopresto_secret_key'); delete_option('mc_rewards'); delete_option('mc_use_javascript'); delete_option('mc_use_datepicker'); delete_option('mc_use_unsub_link'); delete_option('mc_list_id'); delete_option('mc_list_name'); $igs = get_option('mc_interest_groups'); if (is_array($igs)) { foreach ($igs as $ig) { $opt = 'mc_show_interest_groups_'.$ig['id']; delete_option($opt); } } delete_option('mc_interest_groups'); $mv = get_option('mc_merge_vars'); if (is_array($mv)){ foreach($mv as $var){ $opt = 'mc_mv_'.$var['tag']; delete_option($opt); } } delete_option('mc_merge_vars'); } /** * Resets the list settings, there's only one list * that can have settings at a time, so no list_id * parameter is necessary. **/ function mailchimpSF_reset_list_settings() { delete_option('mc_list_id'); delete_option('mc_list_name'); delete_option('mc_merge_vars'); delete_option('mc_interest_groups'); delete_option('mc_use_javascript'); delete_option('mc_use_unsub_link'); delete_option('mc_use_datepicker'); delete_option('mc_header_content'); delete_option('mc_subheader_content'); delete_option('mc_submit_text'); delete_option('mc_custom_style'); delete_option('mc_header_border_width'); delete_option('mc_header_border_color'); delete_option('mc_header_background'); delete_option('mc_header_text_color'); delete_option('mc_form_border_width'); delete_option('mc_form_border_color'); delete_option('mc_form_background'); delete_option('mc_form_text_color'); $msg = '

'.esc_html(__('Successfully Reset your List selection... Now you get to pick again!', 'mailchimp_i18n')).'

'; mailchimpSF_global_msg($msg); } /** * Gets or sets a global message based on parameter passed to it * * @return string/bool depending on get/set **/ function mailchimpSF_global_msg($msg = null) { global $mcsf_msgs; // Make sure we're formed properly if (!is_array($mcsf_msgs)) { $mcsf_msgs = array(); } // See if we're getting if (is_null($msg)) { return implode('', $mcsf_msgs); } // Must be setting $mcsf_msgs[] = $msg; return true; } /** * Sets the default options for the option form **/ function mailchimpSF_set_form_defaults($list_name = '') { update_option('mc_header_content',__( 'Sign up for', 'mailchimp_i18n' ).' '.$list_name); update_option('mc_submit_text',__( 'Subscribe', 'mailchimp_i18n' )); update_option('mc_use_datepicker', 'on'); update_option('mc_custom_style','off'); update_option('mc_use_javascript','on'); update_option('mc_use_unsub_link','off'); update_option('mc_header_border_width','1'); update_option('mc_header_border_color','E3E3E3'); update_option('mc_header_background','FFFFFF'); update_option('mc_header_text_color','CC6600'); update_option('mc_form_border_width','1'); update_option('mc_form_border_color','E0E0E0'); update_option('mc_form_background','FFFFFF'); update_option('mc_form_text_color','3F3F3f'); } /** * Saves the General Form settings on the options page * * @return void **/ function mailchimpSF_save_general_form_settings() { if (MAILCHIMP_DEV_MODE == false) { if (isset($_POST['mc_rewards'])){ update_option('mc_rewards', 'on'); $msg = '

'.__('Monkey Rewards turned On!', 'mailchimp_i18n').'

'; mailchimpSF_global_msg($msg); } else if (get_option('mc_rewards')!='off') { update_option('mc_rewards', 'off'); $msg = '

'.__('Monkey Rewards turned Off!', 'mailchimp_i18n').'

'; mailchimpSF_global_msg($msg); } if (isset($_POST['mc_use_javascript'])){ update_option('mc_use_javascript', 'on'); $msg = '

'.__('Fancy Javascript submission turned On!', 'mailchimp_i18n').'

'; mailchimpSF_global_msg($msg); } else if (get_option('mc_use_javascript')!='off') { update_option('mc_use_javascript', 'off'); $msg = '

'.__('Fancy Javascript submission turned Off!', 'mailchimp_i18n').'

'; mailchimpSF_global_msg($msg); } if (isset($_POST['mc_use_datepicker'])){ update_option('mc_use_datepicker', 'on'); $msg = '

'.__('Datepicker turned On!', 'mailchimp_i18n').'

'; mailchimpSF_global_msg($msg); } else if (get_option('mc_use_datepicker')!='off') { update_option('mc_use_datepicker', 'off'); $msg = '

'.__('Datepicker turned Off!', 'mailchimp_i18n').'

'; mailchimpSF_global_msg($msg); } if (isset($_POST['mc_use_unsub_link'])){ update_option('mc_use_unsub_link', 'on'); $msg = '

'.__('Unsubscribe link turned On!', 'mailchimp_i18n').'

'; mailchimpSF_global_msg($msg); } else if (get_option('mc_use_unsub_link')!='off') { update_option('mc_use_unsub_link', 'off'); $msg = '

'.__('Unsubscribe link turned Off!', 'mailchimp_i18n').'

'; mailchimpSF_global_msg($msg); } $content = stripslashes($_POST['mc_header_content']); $content = str_replace("\r\n","
", $content); update_option('mc_header_content', $content ); $content = stripslashes($_POST['mc_subheader_content']); $content = str_replace("\r\n","
", $content); update_option('mc_subheader_content', $content ); $submit_text = stripslashes($_POST['mc_submit_text']); $submit_text = str_replace("\r\n","", $submit_text); update_option('mc_submit_text', $submit_text); } // Set Custom Style option update_option('mc_custom_style', isset($_POST['mc_custom_style']) ? 'on' : 'off'); if (MAILCHIMP_DEV_MODE == false) { //we told them not to put these things we are replacing in, but let's just make sure they are listening... update_option('mc_header_border_width',str_replace('px','',$_POST['mc_header_border_width']) ); update_option('mc_header_border_color', str_replace('#','',$_POST['mc_header_border_color'])); update_option('mc_header_background',str_replace('#','',$_POST['mc_header_background'])); update_option('mc_header_text_color', str_replace('#','',$_POST['mc_header_text_color'])); } update_option('mc_form_border_width',str_replace('px','',$_POST['mc_form_border_width']) ); update_option('mc_form_border_color', str_replace('#','',$_POST['mc_form_border_color'])); update_option('mc_form_background',str_replace('#','',$_POST['mc_form_background'])); update_option('mc_form_text_color', str_replace('#','',$_POST['mc_form_text_color'])); if (MAILCHIMP_DEV_MODE == false) { $igs = get_option('mc_interest_groups'); if (is_array($igs)) { foreach($igs as $var){ $opt = 'mc_show_interest_groups_'.$var['id']; if (isset($_POST[$opt])){ update_option($opt,'on'); } else { update_option($opt,'off'); } } } $mv = get_option('mc_merge_vars'); if (is_array($mv)) { foreach($mv as $var){ $opt = 'mc_mv_'.$var['tag']; if (isset($_POST[$opt]) || $var['req']=='Y'){ update_option($opt,'on'); } else { update_option($opt,'off'); } } } } $msg = '

'.esc_html(__('Successfully Updated your List Subscribe Form Settings!', 'mailchimp_i18n')).'

'; mailchimpSF_global_msg($msg); } /** * Sees if the user changed the list, and updates options accordingly **/ function mailchimpSF_change_list_if_necessary() { // Simple permission check before going through all this if (!current_user_can(MCSF_CAP_THRESHOLD)) { return; } $api = mailchimpSF_get_api(); if (!$api) { return; } //we *could* support paging, but few users have that many lists (and shouldn't) $lists = $api->lists(array(),0,100); $lists = $lists['data']; if (is_array($lists) && !empty($lists) && isset($_POST['mc_list_id'])) { /* If our incoming list ID (the one chosen in the select dropdown) is in our array of lists, the set it to be the active list */ foreach($lists as $key => $list) { if ($list['id'] == $_POST['mc_list_id']) { $list_id = $_POST['mc_list_id']; $list_name = $list['name']; $list_key = $key; } } $orig_list = get_option('mc_list_id'); if ($list_id != '') { update_option('mc_list_id', $list_id); update_option('mc_list_name', $list_name); update_option('mc_email_type_option', $lists[$list_key]['email_type_option']); // See if the user changed the list if ($orig_list != $list_id){ // The user changed the list, Reset the Form Defaults mailchimpSF_set_form_defaults($list_name); } // email_type_option // Grab the merge vars and interest groups $mv = $api->listMergeVars($list_id); $igs = $api->listInterestGroupings($list_id); update_option('mc_merge_vars', $mv); foreach($mv as $var){ $opt = 'mc_mv_'.$var['tag']; //turn them all on by default if ($orig_list != $list_id) { update_option($opt, 'on' ); } } update_option('mc_interest_groups', $igs); if (is_array($igs)) { foreach ($igs as $var){ $opt = 'mc_show_interest_groups_'.$var['id']; //turn them all on by default if ($orig_list != $list_id) { update_option($opt, 'on' ); } } } $igs_text = ' '; if (is_array($igs)) { $igs_text .= sprintf(__('and %s Sets of Interest Groups', 'mailchimp_i18n'), count($igs)); } $msg = '

'. sprintf( __('Success! Loaded and saved the info for %d Merge Variables', 'mailchimp_i18n').$igs_text, count($mv) ).' '. __('from your list').' "'.$list_name.'"

'. __('Now you should either Turn On the MailChimp Widget or change your options below, then turn it on.', 'mailchimp_i18n').'

'; mailchimpSF_global_msg($msg); } } } /** * Outputs the Settings/Options page */ function mailchimpSF_setup_page() { ?>

Developer mode active.

%2$s', esc_html(__("Don't have a MailChimp account?", 'mailchimp_i18n')), esc_html(__('Try one for Free!', 'mailchimp_i18n')) ); ?>

"authorize"), home_url('index.php')) ?>" class="mailchimp-login">Connect

:

lists(array(),0,100); $lists = $lists['data']; if (count($lists) == 0) { ?> MailChimp" ); ?>

:

:

Content Options


.

Custom Styling
/>
px • px!
# #
# #
# #

List Options
? id="mc_rewards" class="code" />
id="mc_use_javascript" class="code" />
id="mc_use_datepicker" class="code" />
id="mc_use_unsub_link" class="code" />

/>  — 

/>
CSS Cheat Sheet
.widget_mailchimpsf_widget This targets the entire widget container.
.widget-title This styles the title of your MailChimp widget. Modifying this class will affect your other widget titles.
#mc_signup This targets the entirity of the widget beneath the widget title.
#mc_subheader This styles the subheader text.
.mc_form_inside The guts and main container for the all of the form elements (the entirety of the widget minus the header and the sub header).
.mc_header This targets the label above the input fields.
.mc_input This attaches to the input fields.
.mc_header_address This is the label above an address group.
.mc_radio_label These are the labels associated with radio buttons.
#mc-indicates-required This targets the “Indicates Required Field” text.
#mc_signup_submit Use this to style the submit button.
$ig['id'], 'groups' => str_replace(',', '\,', stripslashes($_POST['group'][$ig['id']])), ); } break; case 'checkboxes': case 'checkbox': if (isset($_POST['group'][$ig['id']])) { foreach ($_POST['group'][$ig['id']] as $i => $value) { // Escape $groups .= str_replace(',', '\,', stripslashes($value)).','; } $groupings = array( 'id' => $ig['id'], 'groups' => $groups, ); } break; default: // Nothing break; } if (!isset($merge['GROUPINGS']) || !is_array($merge['GROUPINGS'])) { $merge['GROUPINGS'] = array(); } if (!empty($groupings)) { $merge['GROUPINGS'][] = $groupings; } } } // If we're good if ($success) { // Clear out empty merge vars foreach ($merge as $k => $v) { if (is_array($v) && empty($v)) { unset($merge[$k]); } else if (!is_array($v) && trim($v) === '') { unset($merge[$k]); } } // If we have an empty $merge, then assign empty string. if (count($merge) == 0 || $merge == '') { $merge = ''; } if (isset($_POST['email_type']) && in_array($_POST['email_type'], array('text', 'html', 'mobile'))) { $email_type = $_POST['email_type']; } else { $email_type = 'html'; } // Custom validation based on type if (is_array($merge) && !empty($merge)) { foreach ($merge as $merge_key => $merge_value) { if ($merge_key !== 'GROUPINGS') { switch ($mv_tag_keys[$merge_key]['field_type']) { case 'phone': if ($mv_tag_keys[$merge_key]['phoneformat'] == 'US') { $phone = $merge_value; if (!empty($phone)) { if (!preg_match('/[0-9]{0,3}-[0-9]{0,3}-[0-9]{0,4}/', $phone)) { $errs[] = sprintf(__("%s must consist of only numbers", 'mailchimp_i18n'), esc_html($mv_tag_keys[$merge_key]['name'])); $success = false; } } } break; default: break; } } } } if ($success) { $api = mailchimpSF_get_api(); if (!$api) { return; } $retval = $api->listSubscribe( $listId, $email, $merge, $email_type); if (!$retval) { switch($api->errorCode) { case '105' : $errs[] = __("Please try again later", 'mailchimp_i18n').'.'; break; case '214' : $msg = __("That email address is already subscribed to the list", 'mailchimp_i18n') . '.'; $account = $api->getAccountDetails(array("modules", "orders", "rewards-credits", "rewards-inspections", "rewards-referrals", "rewards-applied")); if (!$api->errorCode) { $dc = get_option('mc_sopresto_dc'); $uid = $account['user_id']; $username = preg_replace('/\s+/', '-', $account['username']); $eid = base64_encode($email); $msg .= ' ' . sprintf(__('Click here to update your profile.', 'mailchimp_i18n'), "http://$username.$dc.list-manage.com/subscribe/send-email?u=$uid&id=$listId&e=$eid"); } $errs[] = $msg; $html_errs[] = count($errs)-1; break; case '250' : list($field, $rest) = explode(' ', $api->errorMessage, 2); $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name'])); break; case '254' : list($i1, $i2, $i3, $field, $rest) = explode(' ',$api->errorMessage,5); $errs[] = sprintf(__("%s has invalid content.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name'])); break; case '270' : $errs[] = __("An invalid Interest Group was selected", 'mailchimp_i18n').'.'; break; case '502' : $errs[] = __("That email address is invalid", 'mailchimp_i18n').'.'; break; default: $errs[] = $api->errorCode.":".$api->errorMessage; break; } $success = false; } } } // If we have errors, then show them if (count($errs) > 0) { $msg = ''; foreach($errs as $error_index => $error){ if (!in_array($error_index, $html_errs)) { $error = esc_html($error); } $msg .= '» '.$error.'
'; } $msg .= '
'; } else { $msg = "".esc_html(__("Success, you've been signed up! Please look for our confirmation email!", 'mailchimp_i18n')).""; } // Set our global message mailchimpSF_global_msg($msg); return $success; } /********************** * Utility Functions * **********************/ /** * Utility function to allow placement of plugin in plugins, mu-plugins, child or parent theme's plugins folders * * This function must be ran _very early_ in the load process, as it sets up important constants for the rest of the plugin */ function mailchimpSF_where_am_i() { $locations = array( 'plugins' => array( 'dir' => WP_PLUGIN_DIR, 'url' => plugins_url() ), 'mu_plugins' => array( 'dir' => WPMU_PLUGIN_DIR, 'url' => plugins_url(), ), 'template' => array( 'dir' => trailingslashit(get_template_directory()).'plugins/', 'url' => trailingslashit(get_template_directory_uri()).'plugins/', ), 'stylesheet' => array( 'dir' => trailingslashit(get_stylesheet_directory()).'plugins/', 'url' => trailingslashit(get_stylesheet_directory_uri()).'plugins/', ), ); // Set defaults $mscf_dirbase = trailingslashit(basename(dirname(__FILE__))); // Typically wp-mailchimp/ or mailchimp/ $mscf_dir = trailingslashit(WP_PLUGIN_DIR).$mscf_dirbase; $mscf_url = trailingslashit(WP_PLUGIN_URL).$mscf_dirbase; // Try our hands at finding the real location foreach ($locations as $key => $loc) { $dir = trailingslashit($loc['dir']).$mscf_dirbase; $url = trailingslashit($loc['url']).$mscf_dirbase; if (is_file($dir.basename(__FILE__))) { $mscf_dir = $dir; $mscf_url = $url; break; } } // Define our complete filesystem path define('MCSF_DIR', $mscf_dir); /* Lang location needs to be relative *from* ABSPATH, so strip it out of our language dir location */ define('MCSF_LANG_DIR', trailingslashit(MCSF_DIR).'po/'); // Define our complete URL to the plugin folder define('MCSF_URL', $mscf_url); } ?>