diff --git a/assets/src/js/admin/notices.js b/assets/src/js/admin/notices.js index b127b237..eebac6ac 100755 --- a/assets/src/js/admin/notices.js +++ b/assets/src/js/admin/notices.js @@ -35,14 +35,14 @@ const groupingsNotice = function () { const requiredFieldsNotice = function () { const missingFields = fields.getAll().filter(f => f.forceRequired === true && !editor.containsField(f.name.toUpperCase())) - let text = 'Heads up! Your form is missing list fields that are required in Mailchimp. Either add these fields to your form or mark them as optional in Mailchimp.' + let text = 'Heads up! Your form is missing fields that are required in Mailchimp. Either add these fields to your form or mark them as optional in Mailchimp.' text += '
'; (missingFields.length > 0) ? show('required_fields_missing', text) : hide('required_fields_missing') } const mailchimpListsNotice = function () { - const text = 'Heads up! You have not yet selected a Mailchimp list to subscribe people to. Please select at least one list from the settings tab.' + const text = 'Heads up! You have not yet selected a Mailchimp audience to subscribe people to. Please select at least one audience from the settings tab.' if (settings.getSelectedLists().length > 0) { hide('no_lists_selected') diff --git a/includes/admin/class-admin.php b/includes/admin/class-admin.php index a269f0d9..632fd22a 100755 --- a/includes/admin/class-admin.php +++ b/includes/admin/class-admin.php @@ -8,523 +8,523 @@ */ class MC4WP_Admin { - /** - * @var string The relative path to the main plugin file from the plugins dir - */ - protected $plugin_file; - - /** - * @var MC4WP_Admin_Messages - */ - protected $messages; - - /** - * @var MC4WP_Admin_Ads - */ - protected $ads; - - /** - * @var MC4WP_Admin_Tools - */ - protected $tools; - - /** - * @var MC4WP_Admin_Review_Notice - */ - protected $review_notice; - - /** - * Constructor - * - * @param MC4WP_Admin_Tools $tools - * @param MC4WP_Admin_Messages $messages - */ - public function __construct(MC4WP_Admin_Tools $tools, MC4WP_Admin_Messages $messages) - { - $this->tools = $tools; - $this->messages = $messages; - $this->plugin_file = plugin_basename(MC4WP_PLUGIN_FILE); - $this->ads = new MC4WP_Admin_Ads(); - $this->review_notice = new MC4WP_Admin_Review_Notice($tools); - } - - /** - * Registers all hooks - */ - public function add_hooks() - { - - // Actions used globally throughout WP Admin - add_action('admin_menu', array( $this, 'build_menu' )); - add_action('admin_init', array( $this, 'initialize' )); - - add_action('current_screen', array( $this, 'customize_admin_texts' )); - add_action('wp_dashboard_setup', array( $this, 'register_dashboard_widgets' )); - add_action('mc4wp_admin_empty_lists_cache', array( $this, 'renew_lists_cache' )); - add_action('mc4wp_admin_empty_debug_log', array( $this, 'empty_debug_log' )); - - add_action('admin_notices', array( $this, 'show_api_key_notice' )); - add_action('mc4wp_admin_dismiss_api_key_notice', array( $this, 'dismiss_api_key_notice' )); - add_action('admin_enqueue_scripts', array( $this, 'enqueue_assets' )); - - $this->ads->add_hooks(); - $this->messages->add_hooks(); - $this->review_notice->add_hooks(); - } - - /** - * Initializes various stuff used in WP Admin - * - * - Registers settings - */ - public function initialize() - { - - // register settings - register_setting('mc4wp_settings', 'mc4wp', array( $this, 'save_general_settings' )); - - // Load upgrader - $this->init_upgrade_routines(); - - // listen for custom actions - $this->listen_for_actions(); - } - - - /** - * Listen for `_mc4wp_action` requests - */ - public function listen_for_actions() - { - // do nothing if _mc4wp_action was not in the request parameters - if (! isset($_REQUEST['_mc4wp_action'])) { - return; - } - - // check if user is authorized - if (! $this->tools->is_user_authorized()) { - return; - } - - // verify nonce - if (! isset($_REQUEST['_wpnonce']) || false === wp_verify_nonce($_REQUEST['_wpnonce'], '_mc4wp_action')) { - wp_nonce_ays('_mc4wp_action'); - exit; - } - - $action = (string) $_REQUEST['_mc4wp_action']; - - /** - * Allows you to hook into requests containing `_mc4wp_action` => action name. - * - * The dynamic portion of the hook name, `$action`, refers to the action name. - * - * By the time this hook is fired, the user is already authorized. After processing all the registered hooks, - * the request is redirected back to the referring URL. - * - * @since 3.0 - */ - do_action('mc4wp_admin_' . $action); - - // redirect back to where we came from (to prevent double submit) - if (isset($_POST['_redirect_to'])) { - $redirect_url = $_POST['_redirect_to']; - } elseif (isset($_GET['_redirect_to'])) { - $redirect_url = $_GET['_redirect_to']; - } else { - $redirect_url = remove_query_arg('_mc4wp_action'); - } - - wp_safe_redirect($redirect_url); - exit; - } - - /** - * Register dashboard widgets - */ - public function register_dashboard_widgets() - { - if (! $this->tools->is_user_authorized()) { - return; - } - - /** - * Setup dashboard widget, users are authorized by now. - * - * Use this hook to register your own dashboard widgets for users with the required capability. - * - * @since 3.0 - * @ignore - */ - do_action('mc4wp_dashboard_setup'); - } - - /** - * Upgrade routine - */ - private function init_upgrade_routines() - { - // upgrade routine for upgrade routine.... - $previous_version = get_option('mc4wp_lite_version', 0); - if ($previous_version) { - delete_option('mc4wp_lite_version'); - update_option('mc4wp_version', $previous_version); - } - - $previous_version = get_option('mc4wp_version', 0); - - // Ran upgrade routines before? - if (empty($previous_version)) { - update_option('mc4wp_version', MC4WP_VERSION); - - // if we have at least one form, we're going to run upgrade routine for v3 => v4 anyway. - $posts = get_posts( - array( - 'post_type' => 'mc4wp-form', - 'posts_per_page' => 1, - ) - ); - if (empty($posts)) { - return; - } - - $previous_version = '3.9'; - } - - // This means we're good! - if (version_compare($previous_version, MC4WP_VERSION, '>=')) { - return; - } - - define('MC4WP_DOING_UPGRADE', true); - $upgrade_routines = new MC4WP_Upgrade_Routines($previous_version, MC4WP_VERSION, __DIR__ . '/migrations'); - $upgrade_routines->run(); - update_option('mc4wp_version', MC4WP_VERSION); - } - - /** - * Renew Mailchimp lists cache - */ - public function renew_lists_cache() - { - // try getting new lists to fill cache again - $mailchimp = new MC4WP_MailChimp(); - $lists = $mailchimp->refresh_lists(); - - if (! empty($lists)) { - $this->messages->flash(esc_html__('Success! The cached configuration for your Mailchimp lists has been renewed.', 'mailchimp-for-wp')); - } - } - - /** - * Customize texts throughout WP Admin - */ - public function customize_admin_texts() - { - $texts = new MC4WP_Admin_Texts($this->plugin_file); - $texts->add_hooks(); - } - - /** - * Validates the General settings - * @param array $settings - * @return array - */ - public function save_general_settings(array $settings) - { - $current = mc4wp_get_options(); - - // merge with current settings to allow passing partial arrays to this method - $settings = array_merge($current, $settings); - - // Make sure not to use obfuscated key - if (strpos($settings['api_key'], '*') !== false) { - $settings['api_key'] = $current['api_key']; - } - - // Sanitize API key - $settings['api_key'] = sanitize_text_field($settings['api_key']); - - // if API key changed, empty Mailchimp cache - if ($settings['api_key'] !== $current['api_key']) { - delete_transient('mc4wp_mailchimp_lists'); - } - - /** - * Runs right before general settings are saved. - * - * @param array $settings The updated settings array - * @param array $current The old settings array - */ - do_action('mc4wp_save_settings', $settings, $current); - - return $settings; - } - - /** - * Load scripts and stylesheet on Mailchimp for WP Admin pages - */ - public function enqueue_assets() - { - if (! $this->tools->on_plugin_page()) { - return; - } - - $opts = mc4wp_get_options(); - $page = $this->tools->get_plugin_page(); - $mailchimp = new MC4WP_MailChimp(); - - // css - wp_register_style('mc4wp-admin', mc4wp_plugin_url('assets/css/admin.css'), array(), MC4WP_VERSION); - wp_enqueue_style('mc4wp-admin'); - - // js - wp_register_script('mc4wp-admin', mc4wp_plugin_url('assets/js/admin.js'), array(), MC4WP_VERSION, true); - wp_enqueue_script('mc4wp-admin'); - $connected = ! empty($opts['api_key']); - $mailchimp_lists = $connected ? $mailchimp->get_lists() : array(); - wp_localize_script( - 'mc4wp-admin', - 'mc4wp_vars', - array( - 'ajaxurl' => admin_url('admin-ajax.php'), - 'nonce' => wp_create_nonce('mc4wp-ajax'), - 'mailchimp' => array( - 'api_connected' => $connected, - 'lists' => $mailchimp_lists, - ), - 'countries' => MC4WP_Tools::get_countries(), - 'i18n' => array( - 'invalid_api_key' => __('The given value does not look like a valid Mailchimp API key.', 'mailchimp-for-wp'), - 'pro_only' => __('This is a premium feature. Please upgrade to Mailchimp for WordPress Premium to be able to use it.', 'mailchimp-for-wp'), - 'renew_mailchimp_lists' => __('Renew Mailchimp lists', 'mailchimp-for-wp'), - 'fetching_mailchimp_lists' => __('Fetching Mailchimp lists', 'mailchimp-for-wp'), - 'fetching_mailchimp_lists_done' => __('Done! Mailchimp lists renewed.', 'mailchimp-for-wp'), - 'fetching_mailchimp_lists_error' => __('Failed to renew your lists. An error occured.', 'mailchimp-for-wp'), - ), - ) - ); - - /** - * Hook to enqueue your own custom assets on the Mailchimp for WordPress setting pages. - * - * @since 3.0 - * - * @param string $suffix - * @param string $page - */ - do_action('mc4wp_admin_enqueue_assets', '', $page); - } - - - - /** - * Register the setting pages and their menu items - */ - public function build_menu() - { - $required_cap = $this->tools->get_required_capability(); - - $menu_items = array( - array( - 'title' => esc_html__('Mailchimp API Settings', 'mailchimp-for-wp'), - 'text' => 'Mailchimp', - 'slug' => '', - 'callback' => array( $this, 'show_generals_setting_page' ), - 'position' => 0, - ), - array( - 'title' => esc_html__('Other Settings', 'mailchimp-for-wp'), - 'text' => esc_html__('Other', 'mailchimp-for-wp'), - 'slug' => 'other', - 'callback' => array( $this, 'show_other_setting_page' ), - 'position' => 90, - ), - - ); - - /** - * Filters the menu items to appear under the main menu item. - * - * To add your own item, add an associative array in the following format. - * - * $menu_items[] = array( - * 'title' => 'Page title', - * 'text' => 'Menu text', - * 'slug' => 'Page slug', - * 'callback' => 'my_page_function', - * 'position' => 50 - * ); - * - * @param array $menu_items - * @since 3.0 - */ - $menu_items = (array) apply_filters('mc4wp_admin_menu_items', $menu_items); - - // add top menu item - $icon = file_get_contents(MC4WP_PLUGIN_DIR . '/assets/img/icon.svg'); - add_menu_page('Mailchimp for WP', 'MC4WP', $required_cap, 'mailchimp-for-wp', array( $this, 'show_generals_setting_page' ), 'data:image/svg+xml;base64,' . base64_encode($icon), '99.68491'); - - // sort submenu items by 'position' - usort($menu_items, array( $this, 'sort_menu_items_by_position' )); - - // add sub-menu items - foreach ($menu_items as $item) { - $this->add_menu_item($item); - } - } - - /** - * @param array $item - */ - public function add_menu_item(array $item) - { - - // generate menu slug - $slug = 'mailchimp-for-wp'; - if (! empty($item['slug'])) { - $slug .= '-' . $item['slug']; - } - - // provide some defaults - $parent_slug = ! empty($item['parent_slug']) ? $item['parent_slug'] : 'mailchimp-for-wp'; - $capability = ! empty($item['capability']) ? $item['capability'] : $this->tools->get_required_capability(); - - // register page - $hook = add_submenu_page($parent_slug, $item['title'] . ' - Mailchimp for WordPress', $item['text'], $capability, $slug, $item['callback']); - - // register callback for loading this page, if given - if (array_key_exists('load_callback', $item)) { - add_action('load-' . $hook, $item['load_callback']); - } - } - - /** - * Show the API Settings page - */ - public function show_generals_setting_page() - { - $opts = mc4wp_get_options(); - $api_key = mc4wp_get_api_key(); - $lists = array(); - $connected = ! empty($api_key); - - if ($connected) { - try { - $connected = $this->get_api()->is_connected(); - $mailchimp = new MC4WP_MailChimp(); - $lists = $mailchimp->get_lists(); - } catch (MC4WP_API_Connection_Exception $e) { - $message = sprintf('%s %s %s ', esc_html__('Error connecting to Mailchimp:', 'mailchimp-for-wp'), $e->getCode(), $e->getMessage()); - - if (is_object($e->response_data) && ! empty($e->response_data->ref_no)) { - $message .= '
' . sprintf(esc_html__('Looks like your server is blocked by Mailchimp\'s firewall. Please contact Mailchimp support and include the following reference number: %s', 'mailchimp-for-wp'), $e->response_data->ref_no); - } - - $message .= '

' . sprintf('' . esc_html__('Here\'s some info on solving common connectivity issues.', 'mailchimp-for-wp') . '', 'https://www.mc4wp.com/kb/solving-connectivity-issues/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=settings-notice'); - - $this->messages->flash($message, 'error'); - $connected = false; - } catch (MC4WP_API_Exception $e) { - $message = sprintf('%s
%s', esc_html__('Mailchimp returned the following error:', 'mailchimp-for-wp'), nl2br((string) $e)); - $this->messages->flash($message, 'error'); - $connected = false; - } - } - - $obfuscated_api_key = mc4wp_obfuscate_string($api_key); - require MC4WP_PLUGIN_DIR . '/includes/views/general-settings.php'; - } - - /** - * Show the Other Settings page - */ - public function show_other_setting_page() - { - $opts = mc4wp_get_options(); - $log = $this->get_log(); - $log_reader = new MC4WP_Debug_Log_Reader($log->file); - require MC4WP_PLUGIN_DIR . '/includes/views/other-settings.php'; - } - - /** - * @param $a - * @param $b - * - * @return int - */ - public function sort_menu_items_by_position($a, $b) - { - $pos_a = isset($a['position']) ? $a['position'] : 80; - $pos_b = isset($b['position']) ? $b['position'] : 90; - return $pos_a < $pos_b ? -1 : 1; - } - - /** - * Empties the log file - */ - public function empty_debug_log() - { - $log = $this->get_log(); - file_put_contents($log->file, ''); - - $this->messages->flash(esc_html__('Log successfully emptied.', 'mailchimp-for-wp')); - } - - /** - * Shows a notice when API key is not set. - */ - public function show_api_key_notice() - { - - // don't show if on settings page already - if ($this->tools->on_plugin_page('')) { - return; - } - - // only show to user with proper permissions - if (! $this->tools->is_user_authorized()) { - return; - } - - // don't show if dismissed - if (get_transient('mc4wp_api_key_notice_dismissed')) { - return; - } - - // don't show if api key is set already - $api_key = mc4wp_get_api_key(); - if (! empty($api_key)) { - return; - } - - echo '
'; - echo '

', sprintf(wp_kses(__('To get started with Mailchimp for WordPress, please enter your Mailchimp API key on the settings page of the plugin.', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), admin_url('admin.php?page=mailchimp-for-wp')), '

'; - echo '
'; - echo '
'; - } - - /** - * Dismisses the API key notice for 1 week - */ - public function dismiss_api_key_notice() - { - set_transient('mc4wp_api_key_notice_dismissed', 1, 3600 * 24 * 7); - } - - /** - * @return MC4WP_Debug_Log - */ - protected function get_log() - { - return mc4wp('log'); - } - - /** - * @return MC4WP_API_V3 - */ - protected function get_api() - { - return mc4wp('api'); - } + /** + * @var string The relative path to the main plugin file from the plugins dir + */ + protected $plugin_file; + + /** + * @var MC4WP_Admin_Messages + */ + protected $messages; + + /** + * @var MC4WP_Admin_Ads + */ + protected $ads; + + /** + * @var MC4WP_Admin_Tools + */ + protected $tools; + + /** + * @var MC4WP_Admin_Review_Notice + */ + protected $review_notice; + + /** + * Constructor + * + * @param MC4WP_Admin_Tools $tools + * @param MC4WP_Admin_Messages $messages + */ + public function __construct(MC4WP_Admin_Tools $tools, MC4WP_Admin_Messages $messages) + { + $this->tools = $tools; + $this->messages = $messages; + $this->plugin_file = plugin_basename(MC4WP_PLUGIN_FILE); + $this->ads = new MC4WP_Admin_Ads(); + $this->review_notice = new MC4WP_Admin_Review_Notice($tools); + } + + /** + * Registers all hooks + */ + public function add_hooks() + { + + // Actions used globally throughout WP Admin + add_action('admin_menu', array( $this, 'build_menu' )); + add_action('admin_init', array( $this, 'initialize' )); + + add_action('current_screen', array( $this, 'customize_admin_texts' )); + add_action('wp_dashboard_setup', array( $this, 'register_dashboard_widgets' )); + add_action('mc4wp_admin_empty_lists_cache', array( $this, 'renew_lists_cache' )); + add_action('mc4wp_admin_empty_debug_log', array( $this, 'empty_debug_log' )); + + add_action('admin_notices', array( $this, 'show_api_key_notice' )); + add_action('mc4wp_admin_dismiss_api_key_notice', array( $this, 'dismiss_api_key_notice' )); + add_action('admin_enqueue_scripts', array( $this, 'enqueue_assets' )); + + $this->ads->add_hooks(); + $this->messages->add_hooks(); + $this->review_notice->add_hooks(); + } + + /** + * Initializes various stuff used in WP Admin + * + * - Registers settings + */ + public function initialize() + { + + // register settings + register_setting('mc4wp_settings', 'mc4wp', array( $this, 'save_general_settings' )); + + // Load upgrader + $this->init_upgrade_routines(); + + // listen for custom actions + $this->listen_for_actions(); + } + + + /** + * Listen for `_mc4wp_action` requests + */ + public function listen_for_actions() + { + // do nothing if _mc4wp_action was not in the request parameters + if (! isset($_REQUEST['_mc4wp_action'])) { + return; + } + + // check if user is authorized + if (! $this->tools->is_user_authorized()) { + return; + } + + // verify nonce + if (! isset($_REQUEST['_wpnonce']) || false === wp_verify_nonce($_REQUEST['_wpnonce'], '_mc4wp_action')) { + wp_nonce_ays('_mc4wp_action'); + exit; + } + + $action = (string) $_REQUEST['_mc4wp_action']; + + /** + * Allows you to hook into requests containing `_mc4wp_action` => action name. + * + * The dynamic portion of the hook name, `$action`, refers to the action name. + * + * By the time this hook is fired, the user is already authorized. After processing all the registered hooks, + * the request is redirected back to the referring URL. + * + * @since 3.0 + */ + do_action('mc4wp_admin_' . $action); + + // redirect back to where we came from (to prevent double submit) + if (isset($_POST['_redirect_to'])) { + $redirect_url = $_POST['_redirect_to']; + } elseif (isset($_GET['_redirect_to'])) { + $redirect_url = $_GET['_redirect_to']; + } else { + $redirect_url = remove_query_arg('_mc4wp_action'); + } + + wp_safe_redirect($redirect_url); + exit; + } + + /** + * Register dashboard widgets + */ + public function register_dashboard_widgets() + { + if (! $this->tools->is_user_authorized()) { + return; + } + + /** + * Setup dashboard widget, users are authorized by now. + * + * Use this hook to register your own dashboard widgets for users with the required capability. + * + * @since 3.0 + * @ignore + */ + do_action('mc4wp_dashboard_setup'); + } + + /** + * Upgrade routine + */ + private function init_upgrade_routines() + { + // upgrade routine for upgrade routine.... + $previous_version = get_option('mc4wp_lite_version', 0); + if ($previous_version) { + delete_option('mc4wp_lite_version'); + update_option('mc4wp_version', $previous_version); + } + + $previous_version = get_option('mc4wp_version', 0); + + // Ran upgrade routines before? + if (empty($previous_version)) { + update_option('mc4wp_version', MC4WP_VERSION); + + // if we have at least one form, we're going to run upgrade routine for v3 => v4 anyway. + $posts = get_posts( + array( + 'post_type' => 'mc4wp-form', + 'posts_per_page' => 1, + ) + ); + if (empty($posts)) { + return; + } + + $previous_version = '3.9'; + } + + // This means we're good! + if (version_compare($previous_version, MC4WP_VERSION, '>=')) { + return; + } + + define('MC4WP_DOING_UPGRADE', true); + $upgrade_routines = new MC4WP_Upgrade_Routines($previous_version, MC4WP_VERSION, __DIR__ . '/migrations'); + $upgrade_routines->run(); + update_option('mc4wp_version', MC4WP_VERSION); + } + + /** + * Renew Mailchimp lists cache + */ + public function renew_lists_cache() + { + // try getting new lists to fill cache again + $mailchimp = new MC4WP_MailChimp(); + $lists = $mailchimp->refresh_lists(); + + if (! empty($lists)) { + $this->messages->flash(esc_html__('Success! The cached configuration for your Mailchimp lists has been renewed.', 'mailchimp-for-wp')); + } + } + + /** + * Customize texts throughout WP Admin + */ + public function customize_admin_texts() + { + $texts = new MC4WP_Admin_Texts($this->plugin_file); + $texts->add_hooks(); + } + + /** + * Validates the General settings + * @param array $settings + * @return array + */ + public function save_general_settings(array $settings) + { + $current = mc4wp_get_options(); + + // merge with current settings to allow passing partial arrays to this method + $settings = array_merge($current, $settings); + + // Make sure not to use obfuscated key + if (strpos($settings['api_key'], '*') !== false) { + $settings['api_key'] = $current['api_key']; + } + + // Sanitize API key + $settings['api_key'] = sanitize_text_field($settings['api_key']); + + // if API key changed, empty Mailchimp cache + if ($settings['api_key'] !== $current['api_key']) { + delete_transient('mc4wp_mailchimp_lists'); + } + + /** + * Runs right before general settings are saved. + * + * @param array $settings The updated settings array + * @param array $current The old settings array + */ + do_action('mc4wp_save_settings', $settings, $current); + + return $settings; + } + + /** + * Load scripts and stylesheet on Mailchimp for WP Admin pages + */ + public function enqueue_assets() + { + if (! $this->tools->on_plugin_page()) { + return; + } + + $opts = mc4wp_get_options(); + $page = $this->tools->get_plugin_page(); + $mailchimp = new MC4WP_MailChimp(); + + // css + wp_register_style('mc4wp-admin', mc4wp_plugin_url('assets/css/admin.css'), array(), MC4WP_VERSION); + wp_enqueue_style('mc4wp-admin'); + + // js + wp_register_script('mc4wp-admin', mc4wp_plugin_url('assets/js/admin.js'), array(), MC4WP_VERSION, true); + wp_enqueue_script('mc4wp-admin'); + $connected = ! empty($opts['api_key']); + $mailchimp_lists = $connected ? $mailchimp->get_lists() : array(); + wp_localize_script( + 'mc4wp-admin', + 'mc4wp_vars', + array( + 'ajaxurl' => admin_url('admin-ajax.php'), + 'nonce' => wp_create_nonce('mc4wp-ajax'), + 'mailchimp' => array( + 'api_connected' => $connected, + 'lists' => $mailchimp_lists, + ), + 'countries' => MC4WP_Tools::get_countries(), + 'i18n' => array( + 'invalid_api_key' => __('The given value does not look like a valid Mailchimp API key.', 'mailchimp-for-wp'), + 'pro_only' => __('This is a premium feature. Please upgrade to Mailchimp for WordPress Premium to be able to use it.', 'mailchimp-for-wp'), + 'renew_mailchimp_lists' => __('Renew Mailchimp audiences', 'mailchimp-for-wp'), + 'fetching_mailchimp_lists' => __('Fetching Mailchimp audiences', 'mailchimp-for-wp'), + 'fetching_mailchimp_lists_done' => __('Done! Mailchimp audiences renewed.', 'mailchimp-for-wp'), + 'fetching_mailchimp_lists_error' => __('Failed to renew your audiences. An error occured.', 'mailchimp-for-wp'), + ), + ) + ); + + /** + * Hook to enqueue your own custom assets on the Mailchimp for WordPress setting pages. + * + * @since 3.0 + * + * @param string $suffix + * @param string $page + */ + do_action('mc4wp_admin_enqueue_assets', '', $page); + } + + + + /** + * Register the setting pages and their menu items + */ + public function build_menu() + { + $required_cap = $this->tools->get_required_capability(); + + $menu_items = array( + array( + 'title' => esc_html__('Mailchimp API Settings', 'mailchimp-for-wp'), + 'text' => 'Mailchimp', + 'slug' => '', + 'callback' => array( $this, 'show_generals_setting_page' ), + 'position' => 0, + ), + array( + 'title' => esc_html__('Other Settings', 'mailchimp-for-wp'), + 'text' => esc_html__('Other', 'mailchimp-for-wp'), + 'slug' => 'other', + 'callback' => array( $this, 'show_other_setting_page' ), + 'position' => 90, + ), + + ); + + /** + * Filters the menu items to appear under the main menu item. + * + * To add your own item, add an associative array in the following format. + * + * $menu_items[] = array( + * 'title' => 'Page title', + * 'text' => 'Menu text', + * 'slug' => 'Page slug', + * 'callback' => 'my_page_function', + * 'position' => 50 + * ); + * + * @param array $menu_items + * @since 3.0 + */ + $menu_items = (array) apply_filters('mc4wp_admin_menu_items', $menu_items); + + // add top menu item + $icon = file_get_contents(MC4WP_PLUGIN_DIR . '/assets/img/icon.svg'); + add_menu_page('Mailchimp for WP', 'MC4WP', $required_cap, 'mailchimp-for-wp', array( $this, 'show_generals_setting_page' ), 'data:image/svg+xml;base64,' . base64_encode($icon), '99.68491'); + + // sort submenu items by 'position' + usort($menu_items, array( $this, 'sort_menu_items_by_position' )); + + // add sub-menu items + foreach ($menu_items as $item) { + $this->add_menu_item($item); + } + } + + /** + * @param array $item + */ + public function add_menu_item(array $item) + { + + // generate menu slug + $slug = 'mailchimp-for-wp'; + if (! empty($item['slug'])) { + $slug .= '-' . $item['slug']; + } + + // provide some defaults + $parent_slug = ! empty($item['parent_slug']) ? $item['parent_slug'] : 'mailchimp-for-wp'; + $capability = ! empty($item['capability']) ? $item['capability'] : $this->tools->get_required_capability(); + + // register page + $hook = add_submenu_page($parent_slug, $item['title'] . ' - Mailchimp for WordPress', $item['text'], $capability, $slug, $item['callback']); + + // register callback for loading this page, if given + if (array_key_exists('load_callback', $item)) { + add_action('load-' . $hook, $item['load_callback']); + } + } + + /** + * Show the API Settings page + */ + public function show_generals_setting_page() + { + $opts = mc4wp_get_options(); + $api_key = mc4wp_get_api_key(); + $lists = array(); + $connected = ! empty($api_key); + + if ($connected) { + try { + $connected = $this->get_api()->is_connected(); + $mailchimp = new MC4WP_MailChimp(); + $lists = $mailchimp->get_lists(); + } catch (MC4WP_API_Connection_Exception $e) { + $message = sprintf('%s %s %s ', esc_html__('Error connecting to Mailchimp:', 'mailchimp-for-wp'), $e->getCode(), $e->getMessage()); + + if (is_object($e->response_data) && ! empty($e->response_data->ref_no)) { + $message .= '
' . sprintf(esc_html__('Looks like your server is blocked by Mailchimp\'s firewall. Please contact Mailchimp support and include the following reference number: %s', 'mailchimp-for-wp'), $e->response_data->ref_no); + } + + $message .= '

' . sprintf('' . esc_html__('Here\'s some info on solving common connectivity issues.', 'mailchimp-for-wp') . '', 'https://www.mc4wp.com/kb/solving-connectivity-issues/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=settings-notice'); + + $this->messages->flash($message, 'error'); + $connected = false; + } catch (MC4WP_API_Exception $e) { + $message = sprintf('%s
%s', esc_html__('Mailchimp returned the following error:', 'mailchimp-for-wp'), nl2br((string) $e)); + $this->messages->flash($message, 'error'); + $connected = false; + } + } + + $obfuscated_api_key = mc4wp_obfuscate_string($api_key); + require MC4WP_PLUGIN_DIR . '/includes/views/general-settings.php'; + } + + /** + * Show the Other Settings page + */ + public function show_other_setting_page() + { + $opts = mc4wp_get_options(); + $log = $this->get_log(); + $log_reader = new MC4WP_Debug_Log_Reader($log->file); + require MC4WP_PLUGIN_DIR . '/includes/views/other-settings.php'; + } + + /** + * @param $a + * @param $b + * + * @return int + */ + public function sort_menu_items_by_position($a, $b) + { + $pos_a = isset($a['position']) ? $a['position'] : 80; + $pos_b = isset($b['position']) ? $b['position'] : 90; + return $pos_a < $pos_b ? -1 : 1; + } + + /** + * Empties the log file + */ + public function empty_debug_log() + { + $log = $this->get_log(); + file_put_contents($log->file, ''); + + $this->messages->flash(esc_html__('Log successfully emptied.', 'mailchimp-for-wp')); + } + + /** + * Shows a notice when API key is not set. + */ + public function show_api_key_notice() + { + + // don't show if on settings page already + if ($this->tools->on_plugin_page('')) { + return; + } + + // only show to user with proper permissions + if (! $this->tools->is_user_authorized()) { + return; + } + + // don't show if dismissed + if (get_transient('mc4wp_api_key_notice_dismissed')) { + return; + } + + // don't show if api key is set already + $api_key = mc4wp_get_api_key(); + if (! empty($api_key)) { + return; + } + + echo '
'; + echo '

', sprintf(wp_kses(__('To get started with Mailchimp for WordPress, please enter your Mailchimp API key on the settings page of the plugin.', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), admin_url('admin.php?page=mailchimp-for-wp')), '

'; + echo '
'; + echo '
'; + } + + /** + * Dismisses the API key notice for 1 week + */ + public function dismiss_api_key_notice() + { + set_transient('mc4wp_api_key_notice_dismissed', 1, 3600 * 24 * 7); + } + + /** + * @return MC4WP_Debug_Log + */ + protected function get_log() + { + return mc4wp('log'); + } + + /** + * @return MC4WP_API_V3 + */ + protected function get_api() + { + return mc4wp('api'); + } } diff --git a/includes/forms/views/add-form.php b/includes/forms/views/add-form.php index 236fe2b4..9d39b4fe 100755 --- a/includes/forms/views/add-form.php +++ b/includes/forms/views/add-form.php @@ -1,64 +1,64 @@
-
-
-

- -

-

-
-
- - -
-

- -

- -
-
-

- -

+
+
+

+ +

+

+
+ + + +
+

+ +

+ +
+
+

+ +

- -
    - -
  • - -
  • - -
- -

- connect with Mailchimp?', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), admin_url('admin.php?page=mailchimp-for-wp')); ?> -

- -
- - -
- -
-
- -
-
+ +
    + +
  • + +
  • + +
+ +

+ connect with Mailchimp?', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), admin_url('admin.php?page=mailchimp-for-wp')); ?> +

+ +
+ + +
+ +
+
+ +
+
diff --git a/includes/forms/views/parts/add-fields-help.php b/includes/forms/views/parts/add-fields-help.php index 30bc3d9d..2b666224 100755 --- a/includes/forms/views/parts/add-fields-help.php +++ b/includes/forms/views/parts/add-fields-help.php @@ -1,66 +1,66 @@
-

+

-
+
-

- -

+

+ +

-

+

-
    -
  1. -

    - -

    -
  2. -
  3. -

    - - -

    - -
  4. -
  5. -

    - -

    +
      +
    1. +

      + +

      +
    2. +
    3. +

      + + +

      + +
    4. +
    5. +

      + +

      -

      - - - -

      -
    6. -
    +

    + + + +

    +
  6. +
-
+
diff --git a/includes/forms/views/tabs/form-messages.php b/includes/forms/views/tabs/form-messages.php index 605dfd13..d9d56a5b 100755 --- a/includes/forms/views/tabs/form-messages.php +++ b/includes/forms/views/tabs/form-messages.php @@ -7,95 +7,95 @@ - + do_action('mc4wp_admin_form_before_messages_settings_rows', $opts, $form); + ?> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - 'mc4wp_form[settings][update_existing]', - 'value' => 1, - ); - ?> - - - - + 'mc4wp_form[settings][update_existing]', + 'value' => 1, + ); + ?> + + + + - + - - - - + + + +
- -

-
- -

-
- -

-
- -

-
- -

-
- -

-
- -

-
- -

-
+ +

+
+ +

+
+ +

+
+ +

+
+ +

+
+ +

+
+ +

+
+ +

+
- -

-
+ +

+
-

' . esc_html('') . ''); ?>

-
+

' . esc_html('') . ''); ?>

+
diff --git a/includes/forms/views/tabs/form-settings.php b/includes/forms/views/tabs/form-settings.php index bfa0631e..49759d1a 100755 --- a/includes/forms/views/tabs/form-settings.php +++ b/includes/forms/views/tabs/form-settings.php @@ -6,115 +6,115 @@ - - - - - - - - - - - - - - - - - - - - - - 'mc4wp_form[settings][update_existing]', - 'value' => 1, - ); - ?> - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + 'mc4wp_form[settings][update_existing]', + 'value' => 1, + ); + ?> + + + + + + + + + + +
are you connected to Mailchimp?', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), admin_url('admin.php?page=mailchimp-for-wp')); ?> - -
    - -
  • - -
  • - -
-

-
-   - -

-
-   - -

-
-   - -

- - ' . esc_html__('What does this do?', 'mailchimp-for-wp') . '', 'https://www.mc4wp.com/kb/what-does-replace-groupings-mean/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=settings-page'); ?> -

-
- -

- - -

- -
are you connected to Mailchimp?', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), admin_url('admin.php?page=mailchimp-for-wp')); ?> + +
    + +
  • + +
  • + +
+

+
+   + +

+
+   + +

+
+   + +

+ + ' . esc_html__('What does this do?', 'mailchimp-for-wp') . '', 'https://www.mc4wp.com/kb/what-does-replace-groupings-mean/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=settings-page'); ?> +

+
+ +

+ + +

+ +
@@ -124,45 +124,45 @@ - - - - - - - - - - - - + + + + + + + + + + + +
-   - -

- -

-
- -

- 0 for no redirect. Otherwise, use complete (absolute) URLs, including http://.', 'mailchimp-for-wp'), array( 'code' => array() )); ?> -

-

- -

- -
+   + +

+ +

+
+ +

+ 0 for no redirect. Otherwise, use complete (absolute) URLs, including http://.', 'mailchimp-for-wp'), array( 'code' => array() )); ?> +

+

+ +

+ +
diff --git a/includes/integrations/views/integration-settings.php b/includes/integrations/views/integration-settings.php index f774f659..c194645d 100755 --- a/includes/integrations/views/integration-settings.php +++ b/includes/integrations/views/integration-settings.php @@ -3,320 +3,320 @@ /** @var array $opts */ ?>
-

- - Mailchimp for WordPress › - › - name); ?> -

-
-
-

- name)); ?> -

+

+ + Mailchimp for WordPress › + › + name); ?> +

+
+
+

+ name)); ?> +

-

- +

+ - + -

- description); ?> -

+

+ description); ?> +

-
- + + - slug . '_integration_settings', $integration, $opts); - ?> + /** + * @ignore + */ + do_action('mc4wp_admin_before_' . $integration->slug . '_integration_settings', $integration, $opts); + ?> - - has_ui_element('enabled')) { - ?> - - - - - - - +
-   - -

name); ?>

-
+ has_ui_element('enabled')) { + ?> + + + + + + + - 'mc4wp_integrations[' . $integration->slug . '][enabled]', - 'value' => '1', - 'hide' => false, - ); - ?> - + 'mc4wp_integrations[' . $integration->slug . '][enabled]', + 'value' => '1', + 'hide' => false, + ); + ?> + - has_ui_element('implicit')) { - ?> - - - + + - - + echo sprintf( + wp_kses( + __('Warning: enabling this may affect your GDPR compliance.', 'mailchimp-for-wp'), + array( + 'a' => array( 'href' => array() ), + 'strong' => array(), + ) + ), + 'https://www.mc4wp.com/kb/gdpr-compliance/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page' + ); + ?> +

+ + + - has_ui_element('lists')) { - ?> - - - - - '; - echo '
    '; - foreach ($lists as $list) { - $checked = checked(in_array($list->id, $opts['lists'], true), true, false); - $value = esc_attr($list->id); - echo '
  • '; - } - echo '
'; + has_ui_element('lists')) { + ?> + + + + + '; + echo '
    '; + foreach ($lists as $list) { + $checked = checked(in_array($list->id, $opts['lists'], true), true, false); + $value = esc_attr($list->id); + echo '
  • '; + } + echo '
'; - echo '

'; - echo esc_html__('Select the list(s) to which people who check the checkbox should be subscribed.', 'mailchimp-for-wp'); - echo '

'; - echo ''; - } else { - echo ''; - } - ?> - - + echo '

'; + echo esc_html__('Select the audiences to which people who check the checkbox should be subscribed.', 'mailchimp-for-wp'); + echo '

'; + echo ''; + } else { + echo ''; + } + ?> + + - has_ui_element('label')) { - $config = array( - 'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]', - 'value' => 0, - ); - ?> - - - - - + has_ui_element('label')) { + $config = array( + 'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]', + 'value' => 0, + ); + ?> + + + + + - has_ui_element('precheck')) { - $config = array( - 'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]', - 'value' => 0, - ); - ?> - - - - + has_ui_element('precheck')) { + $config = array( + 'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]', + 'value' => 0, + ); + ?> + + + + - has_ui_element('css')) { - $config = array( - 'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]', - 'value' => 0, - ); - ?> - - - - - + has_ui_element('css')) { + $config = array( + 'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]', + 'value' => 0, + ); + ?> + + + + + - has_ui_element('double_optin')) { - ?> - - - - - + has_ui_element('double_optin')) { + ?> + + + + + - has_ui_element('update_existing')) { - ?> - - - - - + has_ui_element('update_existing')) { + ?> + + + + + - has_ui_element('replace_interests')) { - $config = array( - 'element' => 'mc4wp_integrations[' . $integration->slug . '][update_existing]', - 'value' => 1, - ); - ?> - - - - - + has_ui_element('replace_interests')) { + $config = array( + 'element' => 'mc4wp_integrations[' . $integration->slug . '][update_existing]', + 'value' => 1, + ); + ?> + + + + + - -
+   + +

name); ?>

+
-   - -

- '; + has_ui_element('implicit')) { + ?> +

+   + +

+ '; - echo sprintf( - wp_kses( - __('Warning: enabling this may affect your GDPR compliance.', 'mailchimp-for-wp'), - array( - 'a' => array( 'href' => array() ), - 'strong' => array(), - ) - ), - 'https://www.mc4wp.com/kb/gdpr-compliance/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page' - ); - ?> -

-
', sprintf(wp_kses(__('No lists found, are you connected to Mailchimp?', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), esc_url(admin_url('admin.php?page=mailchimp-for-wp'))), '
', sprintf(wp_kses(__('No audiences found, are you connected to Mailchimp?', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), esc_url(admin_url('admin.php?page=mailchimp-for-wp'))), '
- -

' . esc_html('') . ''); ?>

-
+ +

' . esc_html('') . ''); ?>

+
-   - -

- '; - echo sprintf( - wp_kses( - __('Warning: enabling this may affect your GDPR compliance.', 'mailchimp-for-wp'), - array( - 'a' => array( 'href' => array() ), - 'strong' => array(), - ) - ), - 'https://www.mc4wp.com/kb/gdpr-compliance/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page' - ); - ?> -

-
+   + +

+ '; + echo sprintf( + wp_kses( + __('Warning: enabling this may affect your GDPR compliance.', 'mailchimp-for-wp'), + array( + 'a' => array( 'href' => array() ), + 'strong' => array(), + ) + ), + 'https://www.mc4wp.com/kb/gdpr-compliance/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page' + ); + ?> +

+
-   - -

-
+   + +

+
-   - -

- -

-
+   + +

+ +

+
-   - -

-
+   + +

+
-   - -

- - ' . esc_html__('What does this do?', 'mailchimp-for-wp') . '', 'https://www.mc4wp.com/kb/what-does-replace-groupings-mean/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page'); ?> -

-
+   + +

+ + ' . esc_html__('What does this do?', 'mailchimp-for-wp') . '', 'https://www.mc4wp.com/kb/what-does-replace-groupings-mean/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page'); ?> +

+
+ + - slug . '_integration_settings', $integration, $opts); - ?> + /** + * @ignore + */ + do_action('mc4wp_admin_after_' . $integration->slug . '_integration_settings', $integration, $opts); + ?> - get_ui_elements()) > 0) { - submit_button(); - } - ?> + get_ui_elements()) > 0) { + submit_button(); + } + ?> -
-
+ +
-
- -
-
+
+ +
+
diff --git a/includes/views/parts/lists-overview.php b/includes/views/parts/lists-overview.php index cc9a0136..af83d379 100755 --- a/includes/views/parts/lists-overview.php +++ b/includes/views/parts/lists-overview.php @@ -1,60 +1,60 @@

-

+

-
- -

- -

-
+
+ +

+ +

+
- -

.

- ', sprintf(esc_html__('A total of %d lists were found in your Mailchimp account.', 'mailchimp-for-wp'), count($lists)), '

'; - echo ''; + +

.

+ ', sprintf(esc_html__('A total of %d audiences were found in your Mailchimp account.', 'mailchimp-for-wp'), count($lists)), '

'; + echo '
'; - $headings = array( - esc_html__('List Name', 'mailchimp-for-wp'), - esc_html__('ID', 'mailchimp-for-wp'), - esc_html__('Subscribers', 'mailchimp-for-wp'), - ); + $headings = array( + esc_html__('Audience name', 'mailchimp-for-wp'), + esc_html__('Audience ID', 'mailchimp-for-wp'), + esc_html__('# of contacts', 'mailchimp-for-wp'), + ); - echo ''; - echo ''; - foreach ($headings as $heading) { - echo ''; - } - echo ''; - echo ''; - echo ''; + echo ''; + echo ''; + foreach ($headings as $heading) { + echo ''; + } + echo ''; + echo ''; + echo ''; - foreach ($lists as $list) { - $attr_data_list_id = esc_attr($list->id); - $list_name = esc_html($list->name); - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; + foreach ($lists as $list) { + $attr_data_list_id = esc_attr($list->id); + $list_name = esc_html($list->name); + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; - echo ''; - echo ''; - echo ''; - ?> - '; - echo '
', $heading, '
', $heading, '
', $list_name, '', esc_html($list->id), '', esc_html($list->stats->member_count), '
', $list_name, '', esc_html($list->id), '', esc_html($list->stats->member_count), '
'; - } // end if empty - ?> + echo ''; + echo ''; + echo '

', esc_html__('Edit this audience in Mailchimp', 'mailchimp-for-wp'), '

'; + echo '
', esc_html__('Loading... Please wait.', 'mailchimp-for-wp'), '
'; + echo ''; + echo ''; + ?> + '; + echo ''; + } // end if empty + ?>