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 +827 -899 1.5trunk View file →
@@ -1,88 +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.5
7 -Author: MailChimp
8 -Author URI: https://mailchimp.com/
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.5');
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 75 // Define our location constants, both MCSF_DIR and MCSF_URL
34 -mailchimpSF_where_am_i();
76 +mailchimp_sf_where_am_i();
35 77
36 -// Get our MailChimp API class in scope
37 -if (!class_exists('MailChimp_API')) {
38 - $path = plugin_dir_path(__FILE__);
39 - require_once($path . 'lib/mailchimp/mailchimp.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';
40 82 }
41 83
84 +// Encryption utility class.
85 +require_once plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-data-encryption.php';
86 +
42 87 // includes the widget code so it can be easily called either normally or via ajax
43 -include_once('mailchimp_widget.php');
88 +require_once 'mailchimp_widget.php';
44 89
45 90 // includes the backwards compatibility functions
46 -include_once('mailchimp_compat.php');
91 +require_once 'mailchimp_compat.php';
47 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 +
48 140 /**
49 141 * Do the following plugin setup steps here
50 142 *
51 - * Internationalization
52 143 * Resource (JS & CSS) enqueuing
53 144 *
54 145 * @return void
55 146 */
56 -function mailchimpSF_plugin_init() {
57 - // Internationalize the plugin
58 - $textdomain = 'mailchimp_i18n';
59 - $locale = apply_filters( 'plugin_locale', get_locale(), $textdomain);
60 - load_textdomain('mailchimp_i18n', MCSF_LANG_DIR.$textdomain.'-'.$locale.'.mo');
147 +function mailchimp_sf_plugin_init() {
61 148
62 - // Check for mc_api_key or sopresto key and continue if neither
63 - mailchimpSF_migrate_sopresto();
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 + }
64 152
65 - // Bring in our appropriate JS and CSS resources
66 - mailchimpSF_load_resources();
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 + }
67 157 }
68 -add_action( 'init', 'mailchimpSF_plugin_init' );
69 158
159 +add_action( 'init', 'mailchimp_sf_plugin_init' );
70 160
71 161 /**
72 - * Add the settings link to the MailChimp plugin row
162 + * Add the settings link to the Mailchimp plugin row
73 163 *
74 164 * @param array $links - Links for the plugin
75 165 * @return array - Links
76 166 */
77 -function mailchimpSD_plugin_action_links($links) {
78 - $settings_page = add_query_arg(array('page' => 'mailchimpSF_options'), admin_url('options-general.php'));
79 - $settings_link = '<a href="'.esc_url($settings_page).'">'.__('Settings', 'mailchimp_i18n' ).'</a>';
80 - array_unshift($links, $settings_link);
81 - return $links;
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 );
171 + return $links;
82 172 }
83 -add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'mailchimpSD_plugin_action_links', 10, 1);
84 173
174 +add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'mailchimp_sf_plugin_action_links', 10, 1 );
175 +
85 176 /**
86 177 * Loads the appropriate JS and CSS resources depending on
87 178 * settings and context (admin or not)
88 179 *
@@ -87,243 +178,158 @@
87 178 * settings and context (admin or not)
88 179 *
89 180 * @return void
90 181 */
91 -function mailchimpSF_load_resources() {
92 - // JS
93 - if (get_option('mc_use_javascript') == 'on') {
94 - if (!is_admin()) {
95 - wp_enqueue_script('jquery_scrollto', MCSF_URL.'js/scrollTo.js', array('jquery'), MCSF_VER);
96 - wp_enqueue_script('mailchimpSF_main_js', MCSF_URL.'js/mailchimp.js', array('jquery', 'jquery-form'), MCSF_VER);
97 - // some javascript to get ajax version submitting to the proper location
98 - global $wp_scripts;
99 - $wp_scripts->localize('mailchimpSF_main_js', 'mailchimpSF', array(
100 - 'ajax_url' => trailingslashit(home_url()),
101 - ));
102 - }
103 - }
182 +function mailchimp_sf_load_resources() {
183 + // JS
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 + );
104 191
105 - if (get_option('mc_use_datepicker') == 'on' && !is_admin()) {
106 - // Datepicker theme
107 - wp_enqueue_style('flick', MCSF_URL.'/css/flick/flick.css');
108 - // Datepicker JS
109 - wp_enqueue_script('datepicker', MCSF_URL.'/js/datepicker.js', array('jquery','jquery-ui-core'));
110 - }
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' );
195 + }
111 196
112 - if(get_option('mc_nuke_all_styles') !== true) {
113 - wp_enqueue_style('mailchimpSF_main_css', home_url('?mcsf_action=main_css&ver='.MCSF_VER, 'relative'));
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 + );
202 +
203 + // Datepicker theme
204 + wp_enqueue_style( 'flick', MCSF_URL . 'assets/css/flick/flick.css', array(), MCSF_VER );
205 +
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 );
209 +
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 );
214 + }
215 + }
118 216 }
119 217
218 +/**
219 + * Custom styles CSS
220 + *
221 + * @return string
222 + */
223 +function mailchimp_sf_custom_style_css() {
224 + ob_start();
225 + ?>
226 + .mc_signup_form {
227 + padding:5px;
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' ) ); ?>;
233 + }
234 + <?php
235 + return ob_get_clean();
236 +}
120 237
121 238 /**
122 - * Loads resources for the MailChimp admin page
239 + * Request handler
123 240 *
124 241 * @return void
125 242 */
126 -function mc_admin_page_load_resources() {
127 - wp_enqueue_style('mailchimpSF_admin_css', MCSF_URL.'css/admin.css');
128 - wp_enqueue_script('mailchimpSF_admin_js', MCSF_URL.'js/admin.js');
129 -}
130 -add_action('load-settings_page_mailchimpSF_options', 'mc_admin_page_load_resources');
243 +function mailchimp_sf_request_handler() {
244 + if ( isset( $_POST['mcsf_action'] ) ) {
245 + switch ( $_POST['mcsf_action'] ) {
246 + case 'logout':
247 + // Check capability & Verify nonce
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?' );
254 + }
131 255
256 + // erase auth information
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 );
259 + break;
260 + case 'change_form_settings':
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?' );
267 + }
132 268
133 -/**
134 - * Loads jQuery Datepicker for the date-pick class
135 - **/
136 -function mc_datepicker_load() {
137 - require_once(MCSF_DIR . '/views/datepicker.php');
269 + // Update the form settings
270 + mailchimp_sf_save_general_form_settings();
271 + break;
272 + }
273 + }
138 274 }
139 -if (get_option('mc_use_datepicker') == 'on' && !is_admin()) {
140 - add_action('wp_head', 'mc_datepicker_load');
141 -}
275 +add_action( 'init', 'mailchimp_sf_request_handler' );
142 276
143 277 /**
144 - * Handles requests that as light-weight a load as possible.
145 - * typically, JS or CSS
146 - **/
147 -function mailchimpSF_early_request_handler() {
148 - if (isset($_GET['mcsf_action'])) {
149 - switch ($_GET['mcsf_action']) {
150 - case 'main_css':
151 - header("Content-type: text/css");
152 - mailchimpSF_main_css();
153 - exit;
154 - }
155 - }
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 );
156 286 }
157 -add_action('init', 'mailchimpSF_early_request_handler', 0);
158 287
159 288 /**
160 - * Outputs the front-end CSS. This checks several options, so it
161 - * was best to put it in a Request-handled script, as opposed to
162 - * a static file.
289 + * Get auth key
290 + *
291 + * @param mixed $salt Salt
292 + * @return string
163 293 */
164 -function mailchimpSF_main_css() {
165 - require_once(MCSF_DIR . '/views/css/frontend.php');
294 +function mailchimp_sf_auth_nonce_key( $salt = null ) {
295 + if ( is_null( $salt ) ) {
296 + $salt = mailchimp_sf_auth_nonce_salt();
297 + }
298 + return 'social_authentication' . md5( AUTH_KEY . $salt );
166 299 }
167 300
168 -
169 301 /**
170 - * Add our settings page to the admin menu
302 + * Return auth nonce salt
171 303 *
172 - * @return void
304 + * @return string
173 305 */
174 -function mailchimpSF_add_pages(){
175 - // Add settings page for users who can edit plugins
176 - add_options_page( __( 'MailChimp Setup', 'mailchimp_i18n' ), __( 'MailChimp Setup', 'mailchimp_i18n' ), MCSF_CAP_THRESHOLD, 'mailchimpSF_options', 'mailchimpSF_setup_page');
306 +function mailchimp_sf_auth_nonce_salt() {
307 + return md5( microtime() . isset( $_SERVER['SERVER_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) ) : '' );
177 308 }
178 -add_action('admin_menu', 'mailchimpSF_add_pages');
179 309
180 -function mailchimpSF_request_handler() {
181 - if (isset($_POST['mcsf_action'])) {
182 - switch ($_POST['mcsf_action']) {
183 - case 'login':
184 - $key = trim($_POST['mailchimpSF_api_key']);
185 -
186 - try {
187 - $api = new MailChimp_API($key);
188 - } catch (Exception $e) {
189 - $msg = "<strong class='mc_error_msg'>" . $e->getMessage() . "</strong>";
190 - mailchimpSF_global_msg($msg);
191 - break;
192 - }
193 -
194 - $key = mailchimpSF_verify_key($api);
195 - if(is_wp_error($key)) {
196 - $msg = "<strong class='mc_error_msg'>" . $key->get_error_message() . "</strong>";
197 - mailchimpSF_global_msg($msg);
198 - }
199 -
200 - break;
201 - case 'logout':
202 - // Check capability & Verify nonce
203 - if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'mc_logout')) {
204 - wp_die('Cheatin&rsquo; huh?');
205 - }
206 -
207 - // erase auth information
208 - $options = array('mc_api_key', 'mc_sopresto_user', 'mc_sopresto_public_key', 'mc_sopresto_secret_key');
209 - mailchimpSF_delete_options($options);
210 - break;
211 - case 'change_form_settings':
212 - if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'update_general_form_settings')) {
213 - wp_die('Cheatin&rsquo; huh?');
214 - }
215 -
216 - // Update the form settings
217 - mailchimpSF_save_general_form_settings();
218 - break;
219 - case 'mc_submit_signup_form':
220 - // Validate nonce
221 - if (!wp_verify_nonce($_POST['_mc_submit_signup_form_nonce'], 'mc_submit_signup_form')) {
222 - wp_die('Cheatin&rsquo; huh?');
223 - }
224 -
225 - // Attempt the signup
226 - mailchimpSF_signup_submit();
227 -
228 - // Do a different action for html vs. js
229 - switch ($_POST['mc_submit_type']) {
230 - case 'html':
231 - /* Allow to fall through. The widget will pick up the
232 - * global message left over from the signup_submit function */
233 - break;
234 - case 'js':
235 - if (!headers_sent()){ //just in case...
236 - header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT', true, 200);
237 - }
238 - echo mailchimpSF_global_msg(); // Don't esc_html this, b/c we've already escaped it
239 - exit;
240 - }
241 - }
242 - }
243 -}
244 -add_action('init', 'mailchimpSF_request_handler');
245 -
246 -function mailchimpSF_migrate_sopresto() {
247 - $sopresto = get_option('mc_sopresto_secret_key');
248 - if(!$sopresto) {
249 - return;
250 - }
251 -
252 - // Talk to Sopresto, make exchange, delete old sopresto things.
253 - $body = array(
254 - 'public_key' => get_option('mc_sopresto_public_key'),
255 - 'hash' => sha1(get_option('mc_sopresto_public_key').get_option('mc_sopresto_secret_key'))
256 - );
257 -
258 - $url = 'https://sopresto.socialize-this.com/mailchimp/exchange';
259 - $args = array(
260 - 'method' => 'POST',
261 - 'timeout' => 500,
262 - 'redirection' => 5,
263 - 'httpversion' => '1.0',
264 - 'user-agent' => 'MailChimp WordPress Plugin/' . get_bloginfo( 'url' ),
265 - 'body' => $body
266 - );
267 -
268 - //post to sopresto
269 - $key = wp_remote_post($url, $args);
270 - if(!is_wp_error($key) && $key['response']['code'] == 200) {
271 - $key = json_decode($key['body']);
272 - try {
273 - $api = new MailChimp_API($key->response);
274 - } catch (Exception $e) {
275 - $msg = "<strong class='mc_error_msg'>" . $e->getMessage() . "</strong>";
276 - mailchimpSF_global_msg($msg);
277 - return;
278 - }
279 -
280 - $verify = mailchimpSF_verify_key($api);
281 -
282 - //something went wrong with the key that we had
283 - if(is_wp_error($verify)) {
284 - return;
285 - }
286 -
287 - delete_option('mc_sopresto_public_key');
288 - delete_option('mc_sopresto_secret_key');
289 - delete_option('mc_sopresto_user');
290 -
291 - return;
292 - }
293 -
294 - // Nothing to do here.
295 - return;
296 -}
297 -
298 -function mailchimpSF_auth_nonce_key($salt = null) {
299 - if (is_null($salt)) {
300 - $salt = mailchimpSF_auth_nonce_salt();
301 - }
302 - return 'social_authentication' . md5( AUTH_KEY . $salt );
303 -}
304 -
305 -function mailchimpSF_auth_nonce_salt() {
306 - return md5(microtime().$_SERVER['SERVER_ADDR']);
307 -}
308 -
309 310 /**
310 - * Creates new MailChimp API v3 object
311 + * Creates new Mailchimp API v3 object
311 312 *
312 313 * @return MailChimp_API | false
313 314 */
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 + }
314 322
315 -function mailchimpSF_get_api($force = false) {
316 - $key = get_option('mc_api_key');
317 - if($key) {
318 - return new MailChimp_API($key);
319 - }
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 );
327 + }
320 328
321 - return false;
329 + return false;
322 330 }
323 331
324 -
325 -
326 332 /**
327 333 * Checks to see if we're storing a password, if so, we need
328 334 * to upgrade to the API key
329 335 *
@@ -328,92 +334,117 @@
328 334 * to upgrade to the API key
329 335 *
330 336 * @return bool
331 337 **/
332 -function mailchimpSF_needs_upgrade() {
333 - $igs = get_option('mc_interest_groups');
338 +function mailchimp_sf_needs_upgrade() {
339 + $igs = get_option( 'mc_interest_groups' );
334 340
335 - if ($igs !== false // we have an option
336 - && (
337 - empty($igs) || // it can be an empty array (no interest groups)
338 - (is_array($igs) && isset($igs[0]['id'])) // OR it should be a populated array that's well-formed
339 - )) {
340 - return false; // no need to upgrade
341 - }
342 - else {
343 - return true; // yeah, let's do it
344 - }
341 + if ( false !== $igs // we have an option
342 + && (
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 + ) {
347 + return false; // no need to upgrade
348 + } else {
349 + return true; // yeah, let's do it
350 + }
345 351 }
346 352
347 353 /**
348 - * Deletes all mailchimp options
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
349 358 **/
350 -function mailchimpSF_delete_setup() {
351 - $options = array('mc_user_id', 'mc_sopresto_user', 'mc_sopresto_public_key', 'mc_sopresto_secret_key', 'mc_rewards', 'mc_use_javascript', 'mc_use_datepicker', 'mc_use_unsub_link', 'mc_list_id', 'mc_list_name', 'mc_interest_groups', 'mc_merge_vars');
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 + );
352 368
353 - $igs = get_option('mc_interest_groups');
354 - if (is_array($igs)) {
355 - foreach ($igs as $ig) {
356 - $opt = 'mc_show_interest_groups_'.$ig['id'];
357 - $options[] = $opt;
358 - }
359 - }
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;
374 + }
375 + }
360 376
361 - $mv = get_option('mc_merge_vars');
362 - if (is_array($mv)){
363 - foreach($mv as $var){
364 - $opt = 'mc_mv_'.$var['tag'];
365 - $options[] = $opt;
366 - }
367 - }
368 -
369 - mailchimpSF_delete_options($options);
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;
382 + }
383 + }
384 +
385 + mailchimp_sf_delete_options( $options );
370 386 }
371 387
372 388 /**
373 - * 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
374 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
375 397 * @return string/bool depending on get/set
376 - **/
377 -function mailchimpSF_global_msg($msg = null) {
378 - global $mcsf_msgs;
398 + */
399 +function mailchimp_sf_frontend_msg( $msg = null ) {
400 + global $mcsf_msgs;
379 401
380 - // Make sure we're formed properly
381 - if (!is_array($mcsf_msgs)) {
382 - $mcsf_msgs = array();
383 - }
402 + // Make sure we're formed properly
403 + if ( ! is_array( $mcsf_msgs ) ) {
404 + $mcsf_msgs = array();
405 + }
384 406
385 - // See if we're getting
386 - if (is_null($msg)) {
387 - return implode('', $mcsf_msgs);
388 - }
407 + // See if we're getting
408 + if ( is_null( $msg ) ) {
409 + return implode( '', $mcsf_msgs );
410 + }
389 411
390 - // Must be setting
391 - $mcsf_msgs[] = $msg;
392 - return true;
412 + // Must be setting
413 + $mcsf_msgs[] = $msg;
414 + return true;
393 415 }
394 416
395 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 +/**
396 430 * Sets the default options for the option form
397 - **/
398 -function mailchimpSF_set_form_defaults($list_name = '') {
399 - update_option('mc_header_content',__( 'Sign up for', 'mailchimp_i18n' ).' '.$list_name);
400 - 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' ) );
401 438
402 - update_option('mc_use_datepicker', 'on');
403 - update_option('mc_custom_style','off');
404 - update_option('mc_use_javascript','on');
405 - update_option('mc_double_optin', true);
406 - update_option('mc_use_unsub_link','off');
407 - update_option('mc_header_border_width','1');
408 - update_option('mc_header_border_color','E3E3E3');
409 - update_option('mc_header_background','FFFFFF');
410 - 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' );
411 442
412 - update_option('mc_form_border_width','1');
413 - update_option('mc_form_border_color','E0E0E0');
414 - update_option('mc_form_background','FFFFFF');
415 - 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' );
416 447 }
417 448
418 449 /**
419 450 * Saves the General Form settings on the options page
@@ -419,644 +450,484 @@
419 450 * Saves the General Form settings on the options page
420 451 *
421 452 * @return void
422 453 **/
423 -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 + }
424 466
425 - // IF NOT DEV MODE
426 - if (isset($_POST['mc_rewards'])){
427 - update_option('mc_rewards', 'on');
428 - $msg = '<p class="success_msg">'.__('Monkey Rewards turned On!', 'mailchimp_i18n').'</p>';
429 - mailchimpSF_global_msg($msg);
430 - } else if (get_option('mc_rewards')!='off') {
431 - update_option('mc_rewards', 'off');
432 - $msg = '<p class="success_msg">'.__('Monkey Rewards turned Off!', 'mailchimp_i18n').'</p>';
433 - mailchimpSF_global_msg($msg);
434 - }
435 - if (isset($_POST['mc_use_javascript'])){
436 - update_option('mc_use_javascript', 'on');
437 - $msg = '<p class="success_msg">'.__('Fancy Javascript submission turned On!', 'mailchimp_i18n').'</p>';
438 - mailchimpSF_global_msg($msg);
439 - } else if (get_option('mc_use_javascript')!='off') {
440 - update_option('mc_use_javascript', 'off');
441 - $msg = '<p class="success_msg">'.__('Fancy Javascript submission turned Off!', 'mailchimp_i18n').'</p>';
442 - mailchimpSF_global_msg($msg);
443 - }
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 + }
444 477
445 - if (isset($_POST['mc_use_datepicker'])){
446 - update_option('mc_use_datepicker', 'on');
447 - $msg = '<p class="success_msg">'.__('Datepicker turned On!', 'mailchimp_i18n').'</p>';
448 - mailchimpSF_global_msg($msg);
449 - } else if (get_option('mc_use_datepicker')!='off') {
450 - update_option('mc_use_datepicker', 'off');
451 - $msg = '<p class="success_msg">'.__('Datepicker turned Off!', 'mailchimp_i18n').'</p>';
452 - mailchimpSF_global_msg($msg);
453 - }
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 + }
454 488
455 - /*Enable double optin toggle*/
456 -
457 - if(isset($_POST['mc_double_optin'])) {
458 - update_option('mc_double_optin', true);
459 - $msg = '<p class="success_msg">'.__('Double opt-in turned On!', 'mailchimp_i18n').'</p>';
460 - mailchimpSF_global_msg($msg);
461 - } else if (get_option('mc_double_optin') != false) {
462 - update_option('mc_double_optin', false);
463 - $msg = '<p class="success_msg">'.__('Double opt-in turned Off!', 'mailchimp_i18n').'</p>';
464 - mailchimpSF_global_msg($msg);
465 - }
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 + }
466 498
467 - /* NUKE the CSS! */
468 - if(isset($_POST['mc_nuke_all_styles'])) {
469 - update_option('mc_nuke_all_styles', true);
470 - $msg = '<p class="success_msg">'.__('MailChimp CSS turned Off!', 'mailchimp_i18n').'</p>';
471 - mailchimpSF_global_msg($msg);
472 - }elseif (get_option('mc_nuke_all_styles') !== false) {
473 - update_option('mc_nuke_all_styles', false);
474 - $msg = '<p class="success_msg">'.__('MailChimp CSS turned On!', 'mailchimp_i18n').'</p>';
475 - mailchimpSF_global_msg($msg);
476 - }
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 );
477 502
478 - if (isset($_POST['mc_use_unsub_link'])){
479 - update_option('mc_use_unsub_link', 'on');
480 - $msg = '<p class="success_msg">'.__('Unsubscribe link turned On!', 'mailchimp_i18n').'</p>';
481 - mailchimpSF_global_msg($msg);
482 - }
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 );
483 506
484 - elseif (get_option('mc_use_unsub_link')!='off') {
485 - update_option('mc_use_unsub_link', 'off');
486 - $msg = '<p class="success_msg">'.__('Unsubscribe link turned Off!', 'mailchimp_i18n').'</p>';
487 - mailchimpSF_global_msg($msg);
488 - }
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 );
489 510
490 - $content = stripslashes($_POST['mc_header_content']);
491 - $content = str_replace("\r\n","<br/>", $content);
492 - update_option('mc_header_content', $content );
511 + // Set Custom Style option
512 + update_option( 'mc_custom_style', isset( $_POST['mc_custom_style'] ) ? 'on' : 'off' );
493 513
494 - $content = stripslashes($_POST['mc_subheader_content']);
495 - $content = str_replace("\r\n","<br/>", $content);
496 - update_option('mc_subheader_content', $content );
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'] ) ) );
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 + }
497 527
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' );
537 + }
538 + }
539 + }
498 540
499 - $submit_text = stripslashes($_POST['mc_submit_text']);
500 - $submit_text = str_replace("\r\n","", $submit_text);
501 - update_option('mc_submit_text', $submit_text);
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' );
549 + }
550 + }
551 + }
502 552
503 - // Set Custom Style option
504 - update_option('mc_custom_style', isset($_POST['mc_custom_style']) ? 'on' : 'off');
505 -
506 - //we told them not to put these things we are replacing in, but let's just make sure they are listening...
507 - if(isset($_POST['mc_form_border_width'])) {
508 - update_option('mc_form_border_width',str_replace('px', '', $_POST['mc_form_border_width']) );
509 - }
510 - if(isset($_POST['mc_form_border_color'])) {
511 - update_option('mc_form_border_color', str_replace('#', '', $_POST['mc_form_border_color']));
512 - }
513 - if(isset($_POST['mc_form_background'])){
514 - update_option('mc_form_background',str_replace('#', '', $_POST['mc_form_background']));
515 - }
516 - if(isset($_POST['mc_form_text_color'])) {
517 - update_option('mc_form_text_color', str_replace('#', '', $_POST['mc_form_text_color']));
518 - }
519 -
520 -
521 - // IF NOT DEV MODE
522 - $igs = get_option('mc_interest_groups');
523 - if (is_array($igs)) {
524 - foreach($igs as $var){
525 - $opt = 'mc_show_interest_groups_'.$var['id'];
526 - if (isset($_POST[$opt])){
527 - update_option($opt,'on');
528 - } else {
529 - update_option($opt,'off');
530 - }
531 - }
532 - }
533 -
534 - $mv = get_option('mc_merge_vars');
535 - if (is_array($mv)) {
536 - foreach($mv as $var){
537 - $opt = 'mc_mv_'.$var['tag'];
538 - if (isset($_POST[$opt]) || $var['required']=='Y'){
539 - update_option($opt,'on');
540 - } else {
541 - update_option($opt,'off');
542 - }
543 - }
544 - }
545 -
546 - $msg = '<p class="success_msg">'.esc_html(__('Successfully Updated your List Subscribe Form Settings!', 'mailchimp_i18n')).'</p>';
547 - mailchimpSF_global_msg($msg);
553 + $msg = esc_html__( 'Successfully Updated your List Subscribe Form Settings!', 'mailchimp' );
554 + admin_notice_success( $msg );
555 + // phpcs:enable WordPress.Security.NonceVerification.Missing
548 556 }
549 557
550 558 /**
551 559 * Sees if the user changed the list, and updates options accordingly
552 560 **/
553 -function mailchimpSF_change_list_if_necessary() {
554 - // Simple permission check before going through all this
555 - 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 + }
556 565
557 - $api = mailchimpSF_get_api();
558 - 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 + }
559 573
560 - //we *could* support paging, but few users have that many lists (and shouldn't)
561 - $lists = $api->get('lists',100);
562 - $lists = $lists['lists'];
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 + }
563 579
564 - 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 + }
565 584
566 - /* If our incoming list ID (the one chosen in the select dropdown)
567 - is in our array of lists, the set it to be the active list */
568 - foreach($lists as $key => $list) {
569 - if ($list['id'] == $_POST['mc_list_id']) {
570 - $list_id = $_POST['mc_list_id'];
571 - $list_name = $list['name'];
572 - $list_key = $key;
573 - }
574 - }
585 + $lists = $lists['lists'];
575 586
576 - $orig_list = get_option('mc_list_id');
577 - if ($list_id != '') {
578 - update_option('mc_list_id', $list_id);
579 - update_option('mc_list_name', $list_name);
580 - update_option('mc_email_type_option', $lists[$list_key]['email_type_option']);
587 + if ( is_array( $lists ) && ! empty( $lists ) ) {
581 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'] ) );
596 + $list_name = $list['name'];
597 + $list_key = $key;
598 + }
582 599
583 - // See if the user changed the list
584 - $new_list = false;
585 - if ($orig_list != $list_id){
586 - // The user changed the list, Reset the Form Defaults
587 - mailchimpSF_set_form_defaults($list_name);
588 -
589 - $new_list = true;
590 - }
591 - // email_type_option
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 + }
608 + }
592 609
593 - // Grab the merge vars and interest groups
594 - $mv = mailchimpSF_get_merge_vars($list_id, $new_list);
595 - $igs = mailchimpSF_get_interest_categories($list_id, $new_list);
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'] );
596 615
597 - $igs_text = ' ';
598 - if (is_array($igs)) {
599 - $igs_text .= sprintf(__('and %s Sets of Interest Groups', 'mailchimp_i18n'), count($igs));
600 - }
616 + // See if the user changed the list
617 + $new_list = false;
618 + if ( $orig_list !== $list_id ) {
619 + // The user changed the list, Reset the Form Defaults
620 + mailchimp_sf_set_form_defaults( $list_name );
601 621
602 - $msg = '<p class="success_msg">'.
603 - sprintf(
604 - __('<b>Success!</b> Loaded and saved the info for %d Merge Variables', 'mailchimp_i18n').$igs_text,
605 - count($mv)
606 - ).' '.
607 - __('from your list').' "'.$list_name.'"<br/><br/>'.
608 - __('Now you should either Turn On the MailChimp Widget or change your options below, then turn it on.', 'mailchimp_i18n').'</p>';
609 - mailchimpSF_global_msg($msg);
610 - }
611 - }
612 -}
622 + $new_list = true;
623 + }
613 624
614 -function mailchimpSF_get_merge_vars($list_id, $new_list) {
615 - $api = mailchimpSF_get_api();
616 - $mv = $api->get('lists/' . $list_id . '/merge-fields', 80);
617 - $mv['merge_fields'] = mailchimpSF_add_email_field($mv['merge_fields']);
618 - update_option('mc_merge_vars', $mv['merge_fields']);
619 - foreach($mv['merge_fields'] as $var){
620 - $opt = 'mc_mv_'.$var['tag'];
621 - //turn them all on by default
622 - if ($new_list) {
623 - update_option($opt, 'on' );
624 - }
625 - }
626 - return $mv['merge_fields'];
627 -}
625 + // Grab the merge vars and interest groups
626 + $mv = mailchimp_sf_get_merge_vars( $list_id, $new_list );
627 + $igs = mailchimp_sf_get_interest_categories( $list_id, $new_list );
628 628
629 -function mailchimpSF_add_email_field($merge) {
630 -
631 - $email = array(
632 - 'tag' => 'EMAIL',
633 - 'name' => __('Email Address', 'mailchimp_i18n'),
634 - 'type' => 'email',
635 - 'required' => true,
636 - 'public' => true,
637 - 'display_order' => 1,
638 - 'default_value' => null
639 - );
640 - array_unshift($merge, $email);
641 - return $merge;
642 -}
629 + $igs_text = ' ';
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 ) );
633 + }
643 634
644 -function mailchimpSF_get_interest_categories($list_id, $new_list) {
645 - $api = mailchimpSF_get_api();
646 - $igs = $api->get('lists/' . $list_id . '/interest-categories', 60);
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' );
647 642
648 - if (is_array($igs)) {
649 - $key = 0;
650 - foreach($igs['categories'] as $ig) {
651 - $groups = $api->get('lists/' . $list_id . '/interest-categories/' . $ig['id'] . '/interests', 60);
652 - $igs['categories'][$key]['groups'] = $groups['interests'];
653 - $opt = 'mc_show_interest_groups_'.$ig['id'];
643 + admin_notice_success( $msg );
644 + }
654 645
655 - //turn them all on by default
656 - if ($new_list) {
657 - update_option($opt, 'on' );
658 - }
659 - $key++;
660 - }
661 - }
662 - update_option('mc_interest_groups', $igs['categories']);
663 - return $igs['categories'];
646 + // Update the lists option.
647 + update_option( 'mailchimp_sf_lists', $lists );
648 + }
664 649 }
665 650
666 -
667 651 /**
668 - * 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
669 658 */
670 -function mailchimpSF_setup_page() {
671 - $path = plugin_dir_path(__FILE__);
672 - wp_enqueue_script('showMe', MCSF_URL.'js/hidecss.js', array('jquery'), MCSF_VER);
673 - require_once($path.'/views/setup_page.php');
674 -}//mailchimpSF_setup_page()
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 );
675 662
663 + // if we get an error back from the api, exit this process.
664 + if ( is_wp_error( $mv ) ) {
665 + return;
666 + }
676 667
677 -function mailchimpSF_register_widgets() {
678 - if (mailchimpSF_get_api()) {
679 - register_widget('mailchimpSF_Widget');
680 - }
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'] );
671 + }
672 +
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 + }
684 + }
685 + }
686 + return $mv['merge_fields'];
681 687 }
682 -add_action('widgets_init', 'mailchimpSF_register_widgets');
683 688
684 -function mailchimpSF_shortcode($atts){
685 - ob_start();
686 - mailchimpSF_signup_form();
687 - return ob_get_clean();
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;
688 707 }
689 -add_shortcode('mailchimpsf_form', 'mailchimpSF_shortcode');
690 708
691 709 /**
692 - * Attempts to signup a user, per the $_POST args.
710 + * Get interest categories
693 711 *
694 - * This sets a global message, that is then used in the widget
695 - * output to retrieve and display that message.
696 - *
697 - * @return bool
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
698 716 */
699 -function mailchimpSF_signup_submit() {
700 - $mv = get_option('mc_merge_vars', array());
701 - $mv_tag_keys = array();
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 );
702 720
703 - $igs = get_option('mc_interest_groups', array());
721 + // if we get an error back from the api, exis
722 + if ( is_wp_error( $igs ) ) {
723 + return;
724 + }
704 725
705 - $listId = get_option('mc_list_id');
706 - $email = isset($_POST['mc_mv_EMAIL']) ? strip_tags(stripslashes($_POST['mc_mv_EMAIL'])) : '';
707 - $merge = $errs = $html_errs = array(); // Set up some vars
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'];
708 732
709 - $merge = mailchimpSF_merge_submit($mv);
710 -
711 - //Catch errors and fail early.
712 - if(is_wp_error($merge)) {
713 - $msg = "<strong class='mc_error_msg'>" . $merge->get_error_message() . "</strong>";
714 - mailchimpSF_global_msg($msg);
733 + // turn them all on by default
734 + if ( $new_list ) {
735 + update_option( $opt, 'on' );
736 + }
737 + ++$key;
738 + }
739 + }
715 740
716 - return false;
717 - }
741 + if ( $update_option ) {
742 + update_option( 'mc_interest_groups', $igs['categories'] );
743 + }
718 744
719 - // Head back to the beginning of the merge vars array
720 - reset($mv);
721 - // Ensure we have an array
722 - $igs = !is_array($igs) ? array() : $igs;
723 - $igs = mailchimpSF_groups_submit($igs);
724 -
725 - // Clear out empty merge vars
726 - $merge = mailchimpSF_merge_remove_empty($merge);
727 - if (isset($_POST['email_type']) && in_array($_POST['email_type'], array('text', 'html', 'mobile'))) {
728 - $email_type = $_POST['email_type'];
729 - }
730 - else {
731 - $email_type = 'html';
732 - }
733 -
734 - $api = mailchimpSF_get_api();
735 - if (!$api) {
736 - $url = mailchimpSF_signup_form_url();
737 - $error = '<strong class="mc_error_msg">'. __('We encountered a problem adding ' . $email . ' to the list. Please <a href="' . $url . '">sign up here.</a>') . '</strong>';
738 - mailchimpSF_global_msg($error);
739 - return false;
740 - }
741 -
742 - $url = 'lists/'. $listId . '/members/' . md5(strtolower($email));
743 - $status = mailchimpSF_check_status($url);
744 - $body = mailchimpSF_subscribe_body($merge, $igs, $email_type, $email, $status, get_option('mc_double_optin'));
745 - $retval = $api->post($url, $body, 'PUT');
746 -
747 - // If we have errors, then show them
748 - if(is_wp_error($retval)) {
749 - $msg = "<strong class='mc_error_msg'>" . $retval->get_error_message() . "</strong>";
750 - mailchimpSF_global_msg($msg);
751 - return false;
752 - }
753 -
754 - if($retval['status'] == 'subscribed') {
755 - $esc = __("Success, you've been signed up.", 'mailchimp_i18n');
756 - $msg = "<strong class='mc_success_msg'>{$esc}</strong>";
757 - } else {
758 - $esc = __("Success, you've been signed up! Please look for our confirmation email.", 'mailchimp_i18n');
759 - $msg = "<strong class='mc_success_msg'>{$esc}</strong>";
760 - }
761 -
762 - // Set our global message
763 - mailchimpSF_global_msg($msg);
764 -
765 - return true;
745 + return $igs['categories'];
766 746 }
767 747
768 - /*
769 - Cleans up merge fields and interests to make them
770 - API 3.0-friendly.
771 - */
772 -
773 -function mailchimpSF_subscribe_body($merge, $igs, $email_type, $email, $status, $double_optin) {
774 - $body = new stdClass();
775 - $body->email_address = $email;
776 - $body->email_type = $email_type;
777 - $body->merge_fields = $merge;
778 - if (!empty($igs)) {
779 - $body->interests = $igs;
780 - }
781 -
782 - // single opt-in that covers new subscribers
783 - if (!$status && $double_optin == false) {
784 - $body->status = 'subscribed';
785 - }
786 - // anyone else
787 - else {
788 - $body->status = 'pending';
789 - }
790 -
791 -
792 - return $body;
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' );
756 + }
793 757 }
758 +add_action( 'widgets_init', 'mailchimp_sf_register_widgets' );
794 759
795 -function mailchimpSF_check_status($endpoint) {
796 - $endpoint .= '?fields=status';
797 - $api = mailchimpSF_get_api();
798 - $subscriber = $api->get($endpoint, null);
799 - if(is_wp_error($subscriber)) {
800 - return false;
801 - }
802 - return $subscriber['status'];
803 - }
804 -
805 -function mailchimpSF_merge_submit($mv) {
806 - // Loop through our Merge Vars, and if they're empty, but required, then print an error, and mark as failed
807 - $merge = new stdClass();
808 - foreach($mv as $var) {
809 - // We also want to create an array where the keys are the tags for easier validation later
810 - $mv_tag_keys[$var['tag']] = $var;
811 -
812 - $opt = 'mc_mv_'.$var['tag'];
813 -
814 - $opt_val = isset($_POST[$opt]) ? stripslashes_deep($_POST[$opt]) : '';
815 -
816 - // Handle phone number logic
817 - if ($var['type'] === 'phone' && $var['options']['phone_format'] === 'US') {
818 - $opt_val = mailchimpSF_merge_validate_phone($opt_val, $var);
819 - if(is_wp_error($opt_val)) {
820 - return $opt_val;
821 - }
822 - }
823 - // Handle address logic
824 - else if (is_array($opt_val) && $var['type'] == 'address') {
825 - $validate = mailchimpSF_merge_validate_address($opt_val, $var);
826 - if(is_wp_error($validate)) {
827 - return $validate;
828 - }
829 -
830 - if($validate) {
831 - $merge->$var['tag'] = $validate;
832 - }
833 - continue;
834 -
835 - }
836 - else if (is_array($opt_val)) {
837 - $keys = array_keys($opt_val);
838 - $val = new stdClass();
839 - foreach($keys as $key) {
840 - $val->$key = $opt_val[$key];
841 - }
842 - $opt_val = $val;
843 - }
844 -
845 - if ($var['required'] == 'Y' && trim($opt_val) == '') {
846 - $message = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($var['name']));
847 - $error = new WP_Error('missing_required_field', $message);
848 - return $error;
849 - }
850 - else {
851 - if ($var['tag'] != 'EMAIL') {
852 - $merge->$var['tag'] = $opt_val;
853 - }
854 - }
855 - }
856 - return $merge;
760 +/**
761 + * Add shortcode
762 + *
763 + * @return string
764 + */
765 +function mailchimp_sf_shortcode() {
766 + ob_start();
767 + mailchimp_sf_signup_form();
768 + return ob_get_clean();
857 769 }
770 +add_shortcode( 'mailchimpsf_form', 'mailchimp_sf_shortcode' );
858 771
859 -function mailchimpSF_merge_validate_phone($opt_val, $var) {
860 - // This filters out all 'falsey' elements
861 - $opt_val = array_filter($opt_val);
862 - // If they weren't all empty
863 - if (!$opt_val) {
864 - return false;
865 - }
772 +/**
773 + * Cleans up merge fields and interests to make them
774 + * API 3.0-friendly.
775 + *
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
783 + */
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;
866 789
867 - $opt_val = implode('-', $opt_val);
868 - if (strlen($opt_val) < 12) {
869 - $opt_val = '';
870 - }
790 + if ( ! empty( $igs ) ) {
791 + $body->interests = $igs;
792 + }
871 793
794 + // Early return for already subscribed users
795 + if ( 'subscribed' === $status ) {
796 + return $body;
797 + }
872 798
873 - if (!preg_match('/[0-9]{0,3}-[0-9]{0,3}-[0-9]{0,4}/A', $opt_val)) {
874 - $message = sprintf(__("%s must consist of only numbers", 'mailchimp_i18n'), esc_html($var['name']));
875 - $error = new WP_Error('mc_phone_validation', $message);
876 - return $error;
877 - }
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';
878 803
879 - return $opt_val;
804 + return $body;
880 805 }
881 806
882 -function mailchimpSF_merge_validate_address($opt_val, $var) {
883 - if ($var['required'] == 'Y') {
884 - if (empty($opt_val['addr1']) || empty($opt_val['city'])) {
885 - $message = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($var['name']));
886 - $error = new WP_Error('invalid_address_merge', $message);
887 - return $error;
888 - }
889 - } else {
890 - if (empty($opt_val['addr1']) || empty($opt_val['city'])) {
891 - return false;
892 - }
893 - $merge = new stdClass();
894 - $merge->addr1 = $opt_val['addr1'];
895 - $merge->addr2 = $opt_val['addr2'];
896 - $merge->city = $opt_val['city'];
897 - $merge->state = $opt_val['state'];
898 - $merge->zip = $opt_val['zip'];
899 - $merge->country = $opt_val['country'];
900 - return $merge;
901 - }
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;
819 + }
820 + return $subscriber['status'];
902 821 }
903 822
904 -function mailchimpSF_merge_remove_empty($merge) {
905 - foreach ($merge as $k => $v) {
906 - if (is_object($v) && empty($v)) {
907 - unset($merge->$k);
908 - }
909 - else if (!is_object($v) && trim($v) === '') {
910 - unset($merge->$k);
911 - }
912 - }
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;
833 + }
913 834
914 - // If we have an empty $merge, then assign empty string.
915 - if (count($merge) == 0 || $merge == '') {
916 - $merge = '';
917 - }
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 );
918 841
919 - return $merge;
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 + }
920 846 }
921 847
922 -
923 -function mailchimpSF_groups_submit($igs) {
924 - if(empty($igs)) {
925 - return new StdClass();
926 - }
927 -
928 - $groups = new stdClass();
929 -
930 - foreach ($igs as $ig) {
931 - if (get_option('mc_show_interest_groups_'.$ig['id']) == 'on' && $ig['type'] !== 'hidden') {
932 - switch ($ig['type']) {
933 - case 'dropdown':
934 - case 'radio':
935 - // there can only be one value submitted for radio/dropdowns, so use that at the group id.
936 - if (isset($_POST['group'][$ig['id']])){
937 - $value = $_POST['group'][$ig['id']];
938 - $groups->$value = true;
939 - }
940 - break;
941 - case 'checkboxes':
942 - if (isset($_POST['group'][$ig['id']])) {
943 - foreach ($_POST['group'][$ig['id']] as $id => $value) {
944 - $groups->$id = true;
945 - }
946 - }
947 - break;
948 - default:
949 - // Nothing
950 - break;
951 - }
952 - }
953 - }
954 - return $groups;
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;
955 862 }
956 863
957 -function mailchimpSF_verify_key($api) {
958 - $user = $api->get('');
959 - if (is_wp_error($user)) {
960 - return $user;
961 - }
962 -
963 - //Might as well set this data if we have it already.
964 - $valid_roles = array('owner', 'admin', 'manager');
965 - if(in_array($user['role'], $valid_roles)) {
966 - update_option('mc_api_key', $api->key);
967 - update_option('mc_user', $user);
968 - update_option('mc_datacenter', $api->datacenter);
969 -
970 - } else {
971 - $msg = __('API Key must belong to "Owner", "Admin", or "Manager."', 'mailchimp_i18n');
972 - return new WP_Error('mc-invalid-role', $msg);
973 - }
974 - return;
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 + }
975 874 }
976 875
977 -function mailchimpSF_update_profile_url($email) {
978 - $dc = get_option('mc_datacenter');
979 - $eid = base64_encode($email);
980 - $user = get_option('mc_user');
981 - $list_id = get_option('mc_list_id');
982 - $url = 'http://' . $dc . '.list-manage.com/subscribe/send-email?u=' . $user['account_id'] . '&id=' . $list_id . '&e=' . $eid;
983 - return $url;
984 -}
985 -
986 -function mailchimpSF_signup_form_url() {
987 - $dc = get_option('mc_datacenter');
988 - $user = get_option('mc_user');
989 - $list_id = get_option('mc_list_id');
990 - $url = 'http://' . $dc . '.list-manage.com/subscribe?u=' . $user['account_id'] . '&id=' . $list_id;
991 - return $url;
992 -}
993 -
994 -function mailchimpSF_delete_options($options = array()) {
995 - foreach($options as $option) {
996 - delete_option($option);
997 - }
998 -}
999 -
1000 -
1001 876 /**********************
1002 877 * Utility Functions *
1003 -**********************/
878 + **********************/
1004 879 /**
1005 880 * Utility function to allow placement of plugin in plugins, mu-plugins, child or parent theme's plugins folders
1006 881 *
1007 882 * This function must be ran _very early_ in the load process, as it sets up important constants for the rest of the plugin
1008 883 */
1009 -function mailchimpSF_where_am_i() {
1010 - $locations = array(
1011 - 'plugins' => array(
1012 - 'dir' => plugin_dir_path(__FILE__),
1013 - 'url' => plugins_url()
1014 - ),
1015 - 'mu_plugins' => array(
1016 - 'dir' => plugin_dir_path(__FILE__),
1017 - 'url' => plugins_url(),
1018 - ),
1019 - 'template' => array(
1020 - 'dir' => trailingslashit(get_template_directory()).'plugins/',
1021 - 'url' => trailingslashit(get_template_directory_uri()).'plugins/',
1022 - ),
1023 - 'stylesheet' => array(
1024 - 'dir' => trailingslashit(get_stylesheet_directory()).'plugins/',
1025 - 'url' => trailingslashit(get_stylesheet_directory_uri()).'plugins/',
1026 - ),
1027 - );
884 +function mailchimp_sf_where_am_i() {
885 + $locations = array(
886 + 'plugins' => array(
887 + 'dir' => plugin_dir_path( __FILE__ ),
888 + 'url' => plugins_url(),
889 + ),
890 + 'mu_plugins' => array(
891 + 'dir' => plugin_dir_path( __FILE__ ),
892 + 'url' => plugins_url(),
893 + ),
894 + 'template' => array(
895 + 'dir' => trailingslashit( get_template_directory() ) . 'plugins/',
896 + 'url' => trailingslashit( get_template_directory_uri() ) . 'plugins/',
897 + ),
898 + 'stylesheet' => array(
899 + 'dir' => trailingslashit( get_stylesheet_directory() ) . 'plugins/',
900 + 'url' => trailingslashit( get_stylesheet_directory_uri() ) . 'plugins/',
901 + ),
902 + );
1028 903
1029 - // Set defaults
1030 - $mscf_dirbase = trailingslashit(basename(dirname(__FILE__))); // Typically wp-mailchimp/ or mailchimp/
1031 - $mscf_dir = trailingslashit(plugin_dir_path(__FILE__));
1032 - $mscf_url = trailingslashit(plugins_url(null, __FILE__));
904 + // Set defaults
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__ ) );
1033 908
1034 - // Try our hands at finding the real location
1035 - foreach ($locations as $key => $loc) {
1036 - $dir = trailingslashit($loc['dir']).$mscf_dirbase;
1037 - $url = trailingslashit($loc['url']).$mscf_dirbase;
1038 - if (is_file($dir.basename(__FILE__))) {
1039 - $mscf_dir = $dir;
1040 - $mscf_url = $url;
1041 - break;
1042 - }
1043 - }
909 + // Try our hands at finding the real location
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__ ) ) ) {
914 + $mscf_dir = $dir;
915 + $mscf_url = $url;
916 + break;
917 + }
918 + }
1044 919
1045 - // Define our complete filesystem path
1046 - define('MCSF_DIR', $mscf_dir);
920 + // Define our complete filesystem path
921 + define( 'MCSF_DIR', $mscf_dir );
1047 922
1048 - /* Lang location needs to be relative *from* ABSPATH,
1049 - so strip it out of our language dir location */
1050 - define('MCSF_LANG_DIR', trailingslashit(MCSF_DIR).'po/');
1051 -
1052 - // Define our complete URL to the plugin folder
1053 - define('MCSF_URL', $mscf_url);
923 + // Define our complete URL to the plugin folder
924 + define( 'MCSF_URL', $mscf_url );
1054 925 }
1055 926
1056 927
1057 928 /**
1058 - * 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
1059 930 * something universally.
1060 931 *
1061 932 * Verify that correct nonce was used with time limit.
1062 933 *
@@ -1062,45 +933,45 @@
1062 933 *
1063 934 * The user is given an amount of time to use the token, so therefore, since the
1064 935 * UID and $action remain the same, the independent variable is the time.
1065 936 *
1066 - * @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
1067 938 * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
1068 939 * @return bool Whether the nonce check passed or failed.
1069 940 */
1070 -function mailchimpSF_verify_nonce($nonce, $action = -1) {
1071 - $user = wp_get_current_user();
1072 - $uid = (int) $user->ID;
1073 - if ( ! $uid ) {
1074 - $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
1075 - }
941 +function mailchimp_sf_verify_nonce( $nonce, $action = -1 ) {
942 + $user = wp_get_current_user();
943 + $uid = (int) $user->ID;
944 + if ( ! $uid ) {
945 + $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
946 + }
1076 947
1077 - if ( empty( $nonce ) ) {
1078 - return false;
1079 - }
948 + if ( empty( $nonce ) ) {
949 + return false;
950 + }
1080 951
1081 - $token = 'MAILCHIMP';
1082 - $i = wp_nonce_tick();
952 + $token = 'MAILCHIMP';
953 + $i = wp_nonce_tick();
1083 954
1084 - // Nonce generated 0-12 hours ago
1085 - $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce'), -12, 10 );
1086 - if ( hash_equals( $expected, $nonce ) ) {
1087 - return 1;
1088 - }
955 + // Nonce generated 0-12 hours ago
956 + $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
957 + if ( hash_equals( $expected, $nonce ) ) {
958 + return 1;
959 + }
1089 960
1090 - // Nonce generated 12-24 hours ago
1091 - $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
1092 - if ( hash_equals( $expected, $nonce ) ) {
1093 - return 2;
1094 - }
961 + // Nonce generated 12-24 hours ago
962 + $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
963 + if ( hash_equals( $expected, $nonce ) ) {
964 + return 2;
965 + }
1095 966
1096 - // Invalid nonce
1097 - return false;
967 + // Invalid nonce
968 + return false;
1098 969 }
1099 970
1100 971
1101 972 /**
1102 - * 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
1103 974 * something universally.
1104 975 *
1105 976 * Creates a cryptographic token tied to a specific action, user, and window of time.
1106 977 *
@@ -1106,17 +977,74 @@
1106 977 *
1107 978 * @param string $action Scalar value to add context to the nonce.
1108 979 * @return string The token.
1109 980 */
1110 -function mailchimpSF_create_nonce($action = -1) {
1111 - $user = wp_get_current_user();
1112 - $uid = (int) $user->ID;
1113 - if ( ! $uid ) {
1114 - /** This filter is documented in wp-includes/pluggable.php */
1115 - $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
1116 - }
981 +function mailchimp_sf_create_nonce( $action = -1 ) {
982 + $user = wp_get_current_user();
983 + $uid = (int) $user->ID;
984 + if ( ! $uid ) {
985 + /** This filter is documented in wp-includes/pluggable.php */
986 + $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
987 + }
1117 988
1118 - $token = 'MAILCHIMP';
1119 - $i = wp_nonce_tick();
989 + $token = 'MAILCHIMP';
990 + $i = wp_nonce_tick();
1120 991
1121 - return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
1122 -}
992 + return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
993 +}
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 +}