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