first commit

This commit is contained in:
2024-07-31 13:12:38 +07:00
commit b4e8cbe182
10213 changed files with 3125839 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
<?php
class MC4WP_Admin_Ajax
{
/**
* @var MC4WP_Admin_Tools
*/
protected $tools;
/**
* MC4WP_Admin_Ajax constructor.
*
* @param MC4WP_Admin_Tools $tools
*/
public function __construct(MC4WP_Admin_Tools $tools)
{
$this->tools = $tools;
}
/**
* Hook AJAX actions
*/
public function add_hooks()
{
add_action('wp_ajax_mc4wp_renew_mailchimp_lists', array( $this, 'refresh_mailchimp_lists' ));
add_action('wp_ajax_mc4wp_get_list_details', array( $this, 'get_list_details' ));
}
/**
* Empty lists cache & fetch lists again.
*/
public function refresh_mailchimp_lists()
{
if (! $this->tools->is_user_authorized()) {
wp_send_json_error();
return;
}
check_ajax_referer('mc4wp-ajax');
$mailchimp = new MC4WP_MailChimp();
$success = $mailchimp->refresh_lists();
wp_send_json($success);
}
/**
* Retrieve details (merge fields and interest categories) for one or multiple lists in Mailchimp
* @throws MC4WP_API_Exception
*/
public function get_list_details()
{
if (! $this->tools->is_user_authorized()) {
wp_send_json_error();
return;
}
$list_ids = (array) explode(',', $_GET['ids']);
$data = array();
$mailchimp = new MC4WP_MailChimp();
foreach ($list_ids as $list_id) {
$data[] = (object) array(
'id' => $list_id,
'merge_fields' => $mailchimp->get_list_merge_fields($list_id),
'interest_categories' => $mailchimp->get_list_interest_categories($list_id),
'marketing_permissions' => $mailchimp->get_list_marketing_permissions($list_id),
);
}
if (isset($_GET['format']) && $_GET['format'] === 'html') {
$merge_fields = $data[0]->merge_fields;
$interest_categories = $data[0]->interest_categories;
$marketing_permissions = $data[0]->marketing_permissions;
require MC4WP_PLUGIN_DIR . '/includes/views/parts/lists-overview-details.php';
} else {
wp_send_json($data);
}
exit;
}
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* Class MC4WP_Admin_Messages
*
* @ignore
* @since 3.0
*/
class MC4WP_Admin_Messages
{
/**
* @var array
*/
protected $bag;
/**
* @var bool
*/
protected $dirty = false;
/**
* Add hooks
*/
public function add_hooks()
{
add_action('admin_notices', array( $this, 'show' ));
register_shutdown_function(array( $this, 'save' ));
}
private function load()
{
if (is_null($this->bag)) {
$this->bag = get_option('mc4wp_flash_messages', array());
}
}
// empty flash bag
private function reset()
{
$this->bag = array();
$this->dirty = true;
}
/**
* Flash a message (shows on next pageload)
*
* @param $message
* @param string $type
*/
public function flash($message, $type = 'success')
{
$this->load();
$this->bag[] = array(
'text' => $message,
'type' => $type,
);
$this->dirty = true;
}
/**
* Show queued flash messages
*/
public function show()
{
$this->load();
foreach ($this->bag as $message) {
echo sprintf('<div class="notice notice-%s is-dismissible"><p>%s</p></div>', $message['type'], $message['text']);
}
$this->reset();
}
/**
* Save queued messages
*
* @hooked `shutdown`
*/
public function save()
{
if ($this->dirty) {
update_option('mc4wp_flash_messages', $this->bag, false);
}
}
}

View File

@@ -0,0 +1,104 @@
<?php
/**
* Class MC4WP_Admin_Texts
*
* @ignore
* @since 3.0
*/
class MC4WP_Admin_Texts
{
/**
* @var string
*/
protected $plugin_file;
/**
* @param string $plugin_file
*/
public function __construct($plugin_file)
{
$this->plugin_file = $plugin_file;
}
/**
* Add hooks
*/
public function add_hooks()
{
global $pagenow;
add_filter('admin_footer_text', array( $this, 'footer_text' ));
// Hooks for Plugins overview page
if ($pagenow === 'plugins.php') {
add_filter('plugin_action_links_' . $this->plugin_file, array( $this, 'add_plugin_settings_link' ), 10, 2);
add_filter('plugin_row_meta', array( $this, 'add_plugin_meta_links' ), 10, 2);
}
}
/**
* Ask for a plugin review in the WP Admin footer, if this is one of the plugin pages.
*
* @param string $text
*
* @return string
*/
public function footer_text($text)
{
if (! empty($_GET['page']) && strpos($_GET['page'], 'mailchimp-for-wp') === 0) {
$text = sprintf('If you enjoy using <strong>Mailchimp for WordPress</strong>, please <a href="%s" target="_blank">leave us a ★★★★★ plugin review on WordPress.org</a>.', 'https://wordpress.org/support/plugin/mailchimp-for-wp/reviews/#new-post');
}
return $text;
}
/**
* Add the settings link to the Plugins overview
*
* @param array $links
* @param $file
*
* @return array
*/
public function add_plugin_settings_link($links, $file)
{
if ($file !== $this->plugin_file) {
return $links;
}
$settings_link = sprintf('<a href="%s">%s</a>', admin_url('admin.php?page=mailchimp-for-wp'), esc_html__('Settings', 'mailchimp-for-wp'));
array_unshift($links, $settings_link);
return $links;
}
/**
* Adds meta links to the plugin in the WP Admin > Plugins screen
*
* @param array $links
* @param string $file
*
* @return array
*/
public function add_plugin_meta_links($links, $file)
{
if ($file !== $this->plugin_file) {
return $links;
}
$links[] = '<a href="https://www.mc4wp.com/kb/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=plugins-page">' . esc_html__('Documentation', 'mailchimp-for-wp') . '</a>';
/**
* Filters meta links shown on the Plugins overview page
*
* This takes an array of strings
*
* @since 3.0
* @param array $links
* @ignore
*/
$links = apply_filters('mc4wp_admin_plugin_meta_links', $links);
return $links;
}
}

View File

@@ -0,0 +1,67 @@
<?php
class MC4WP_Admin_Tools
{
/**
* @return string
*/
public function get_plugin_page()
{
if (empty($_GET['page'])) {
return '';
}
$prefix = 'mailchimp-for-wp';
$page = ltrim(substr($_GET['page'], strlen($prefix)), '-');
return $page;
}
/**
* @param string $page
*
* @return bool
*/
public function on_plugin_page($page = null)
{
// any settings page
if (is_null($page)) {
return isset($_GET['page']) && strpos($_GET['page'], 'mailchimp-for-wp') === 0;
}
// specific page
return $this->get_plugin_page() === $page;
}
/**
* Does the logged-in user have the required capability?
*
* @return bool
*/
public function is_user_authorized()
{
return current_user_can($this->get_required_capability());
}
/**
* Get required capability to access settings page and view dashboard widgets.
*
* @return string
*/
public function get_required_capability()
{
$capability = 'manage_options';
/**
* Filters the required user capability to access the Mailchimp for WordPress' settings pages, view the dashboard widgets.
*
* Defaults to `manage_options`
*
* @since 3.0
* @param string $capability
* @see https://codex.wordpress.org/Roles_and_Capabilities
*/
$capability = (string) apply_filters('mc4wp_admin_required_capability', $capability);
return $capability;
}
}

View File

@@ -0,0 +1,530 @@
<?php
/**
* Class MC4WP_Admin
*
* @ignore
* @access private
*/
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('<strong>%s</strong> %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 .= '<br />' . 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 .= '<br /><br />' . sprintf('<a href="%s">' . esc_html__('Here\'s some info on solving common connectivity issues.', 'mailchimp-for-wp') . '</a>', '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('<strong>%s</strong><br /> %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 '<div class="notice notice-warning mc4wp-is-dismissible">';
echo '<p>', sprintf(wp_kses(__('To get started with Mailchimp for WordPress, please <a href="%s">enter your Mailchimp API key on the settings page of the plugin</a>.', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), admin_url('admin.php?page=mailchimp-for-wp')), '</p>';
echo '<form method="post"><input type="hidden" name="_mc4wp_action" value="dismiss_api_key_notice" /><button type="submit" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></form>';
echo '</div>';
}
/**
* 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');
}
}

View File

@@ -0,0 +1,163 @@
<?php
/**
* Class MC4WP_Admin_Ads
*
* @ignore
* @access private
*/
class MC4WP_Admin_Ads
{
/**
* @return bool Adds hooks
*/
public function add_hooks()
{
// don't hook if Premium is activated
if (defined('MC4WP_PREMIUM_VERSION')) {
return false;
}
add_filter('mc4wp_admin_plugin_meta_links', array( $this, 'plugin_meta_links' ));
add_action('mc4wp_admin_form_after_behaviour_settings_rows', array( $this, 'after_form_settings_rows' ));
add_action('mc4wp_admin_form_after_appearance_settings_rows', array( $this, 'after_form_appearance_settings_rows' ));
add_action('mc4wp_admin_sidebar', array( $this, 'admin_sidebar' ));
add_action('mc4wp_admin_footer', array( $this, 'admin_footer' ));
add_action('mc4wp_admin_other_settings', array( $this, 'ecommerce' ), 90);
add_filter('mc4wp_admin_menu_items', array( $this, 'add_menu_item' ));
add_action('mc4wp_admin_after_woocommerce_integration_settings', array( $this, 'ecommerce' ));
return true;
}
public function add_menu_item($items)
{
$items['extensions'] = array(
'title' => __('Add-ons', 'mailchimp-for-wp'),
'text' => __('Add-ons', 'mailchimp-for-wp'),
'slug' => 'extensions',
'callback' => array( $this, 'show_extensions_page' ),
'position' => 100,
);
return $items;
}
/**
* Add text row to "Form > Appearance" tab.
*/
public function after_form_appearance_settings_rows()
{
echo '<tr>';
echo '<td colspan="2">';
echo '<p class="description">';
echo sprintf(__('Want to customize the style of your form? <a href="%s">Try our Styles Builder</a> & edit the look of your forms with just a few clicks.', 'mailchimp-for-wp'), 'https://www.mc4wp.com/premium-features/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=form-settings-link');
echo '</p>';
echo '</td>';
echo '</tr>';
}
/**
* Add text row to "Form > Settings" tab.
*/
public function after_form_settings_rows()
{
echo '<tr>';
echo '<td colspan="2">';
echo '<p class="description">';
if (rand(1, 2) === 1) {
echo sprintf(__('Be notified whenever someone subscribes? <a href="%s">Mailchimp for WordPress Premium</a> allows you to set up email notifications for your forms.', 'mailchimp-for-wp'), 'https://www.mc4wp.com/premium-features/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=footer-link');
} else {
echo sprintf(__('Increased conversions? <a href="%s">Mailchimp for WordPress Premium</a> submits forms without reloading the entire page, resulting in a much better experience for your visitors.', 'mailchimp-for-wp'), 'https://www.mc4wp.com/premium-features/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=form-settings-link');
}
echo '</p>';
echo '</td>';
echo '</tr>';
}
/**
* @param array $links
*
* @return array
*/
public function plugin_meta_links($links)
{
$links[] = '<a href="https://www.mc4wp.com/premium-features/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=plugins-upgrade-link">' . __('Upgrade to Premium', 'mailchimp-for-wp') . '</a>';
return $links;
}
/**
* Add several texts to admin footer.
*/
public function admin_footer()
{
if (isset($_GET['view']) && $_GET['view'] === 'edit-form') {
// WPML & Polylang specific message
if (defined('ICL_LANGUAGE_CODE')) {
echo '<p class="description">' . sprintf(__('Do you want translated forms for all of your languages? <a href="%s">Try Mailchimp for WordPress Premium</a>, which does just that plus more.', 'mailchimp-for-wp'), 'https://www.mc4wp.com/premium-features/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=footer-link') . '</p>';
return;
}
// General "edit form" message
echo '<p class="description">' . sprintf(__('Do you want to create more than one form? Our Premium add-on does just that! <a href="%s">Have a look at all Premium benefits</a>.', 'mailchimp-for-wp'), 'https://www.mc4wp.com/premium-features/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=footer-link') . '</p>';
return;
}
// General message
echo '<p class="description">' . sprintf(__('Are you enjoying this plugin? The Premium add-on unlocks several powerful features. <a href="%s">Find out about all benefits now</a>.', 'mailchimp-for-wp'), 'https://www.mc4wp.com/premium-features/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=footer-link') . '</p>';
}
/**
* Add email opt-in form to sidebar
*/
public function admin_sidebar()
{
echo '<style>.mc4wp-premium-box {
background: #fff8c5;
border: 1px solid #d4a72c66;
padding: 1em;
}</style>';
echo '<div class="mc4wp-box">';
echo '<div class="mc4wp-premium-box">';
echo '<h3>Mailchimp for WordPress Premium</h3>';
echo '<p>';
echo 'You are currently using the free version of Mailchimp for WordPress. ';
echo '</p>';
echo '<p>';
echo 'There is a Premium version of this plugin that adds several powerful features. Like multiple and improved sign-up forms, an easier way to visually enhance those forms, advanced e-commerce integration and keeping track of all sign-up attempts in your local WordPress database.';
echo '</p>';
echo '<p>You can have all those benefits for a small yearly fee. <a href="https://www.mc4wp.com/premium-features/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=upgrade-box">Take a look at Mailchimp for WordPress Premium here</a>.</p>';
echo '</div>';
echo '</div>';
}
/**
* Show notice about E-Commerce integration in Premium.
*/
public function ecommerce()
{
// detect whether WooCommerce is installed & activated.
if (! class_exists('WooCommerce')) {
return;
}
echo '<div class="mc4wp-margin-m">';
echo '<h3>Advanced WooCommerce integration for Mailchimp</h3>';
echo '<p>';
echo __('Do you want to track all WooCommerce orders in Mailchimp so you can send emails based on the purchase activity of your subscribers?', 'mailchimp-for-wp');
echo '</p>';
echo '<p>';
echo sprintf(__('<a href="%1$s">Upgrade to Mailchimp for WordPress Premium</a> or <a href="%2$s">read more about Mailchimp\'s E-Commerce features</a>.', 'mailchimp-for-wp') . '</p>', 'https://www.mc4wp.com/premium-features/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=other-settings-link', 'https://www.mc4wp.com/kb/what-is-ecommerce360/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=other-settings-link');
echo '</p>';
echo '</div>';
}
public function show_extensions_page()
{
require MC4WP_PLUGIN_DIR . '/includes/views/extensions.php';
}
}

View File

@@ -0,0 +1,98 @@
<?php
/**
* Class MC4WP_Admin_Review_Notice
*
* @ignore
*/
class MC4WP_Admin_Review_Notice
{
/**
* @var MC4WP_Admin_Tools
*/
protected $tools;
/**
* @var string
*/
protected $meta_key_dismissed = '_mc4wp_review_notice_dismissed';
/**
* MC4WP_Admin_Review_Notice constructor.
*
* @param MC4WP_Admin_Tools $tools
*/
public function __construct(MC4WP_Admin_Tools $tools)
{
$this->tools = $tools;
}
/**
* Add action & filter hooks.
*/
public function add_hooks()
{
add_action('admin_notices', array( $this, 'show' ));
add_action('mc4wp_admin_dismiss_review_notice', array( $this, 'dismiss' ));
}
/**
* Set flag in user meta so notice won't be shown.
*/
public function dismiss()
{
$user = wp_get_current_user();
update_user_meta($user->ID, $this->meta_key_dismissed, 1);
}
/**
* @return bool
*/
public function show()
{
// only show on Mailchimp for WordPress' pages.
if (! $this->tools->on_plugin_page()) {
return false;
}
// only show if 2 weeks have passed since first use.
$two_weeks_in_seconds = ( 60 * 60 * 24 * 14 );
if ($this->time_since_first_use() <= $two_weeks_in_seconds) {
return false;
}
// only show if user did not dismiss before
$user = wp_get_current_user();
if (get_user_meta($user->ID, $this->meta_key_dismissed, true)) {
return false;
}
echo '<div class="notice notice-info mc4wp-is-dismissible" id="mc4wp-review-notice">';
echo '<p>';
echo esc_html__('You\'ve been using Mailchimp for WordPress for some time now; we hope you love it!', 'mailchimp-for-wp'), ' <br />';
echo sprintf(wp_kses(__('If you do, please <a href="%s">leave us a 5★ rating on WordPress.org</a>. It would be of great help to us.', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), 'https://wordpress.org/support/view/plugin-reviews/mailchimp-for-wp?rate=5#new-post');
echo '</p>';
echo '<form method="POST" id="mc4wp-dismiss-review-form"><button type="submit" class="notice-dismiss"><span class="screen-reader-text">', esc_html__('Dismiss this notice.', 'mailchimp-for-wp'), '</span></button><input type="hidden" name="_mc4wp_action" value="dismiss_review_notice" />', wp_nonce_field('_mc4wp_action', '_wpnonce', true, false), '</form>';
echo '</div>';
return true;
}
/**
* @return int
*/
private function time_since_first_use()
{
$options = get_option('mc4wp', array());
if (! is_array($options)) {
$options = array();
}
// option was never added before, do it now.
if (empty($options['first_activated_on'])) {
$options['first_activated_on'] = time();
update_option('mc4wp', $options);
}
return time() - $options['first_activated_on'];
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* Class MC4WP_DB_Upgrader
*
* This class takes care of loading migration files from the specified migrations directory.
* Migration files should only use default WP functions and NOT use code which might not be there in the future.
*
* @ignore
*/
class MC4WP_Upgrade_Routines
{
/**
* @var float
*/
protected $version_from = 0;
/**
* @var float
*/
protected $version_to = 0;
/**
* @var string
*/
protected $migrations_dir = '';
/**
* @param float $from
* @param float $to
*/
public function __construct($from, $to, $migrations_dir)
{
$this->version_from = $from;
$this->version_to = $to;
$this->migrations_dir = $migrations_dir;
}
/**
* Run the various upgrade routines, all the way up to the latest version
*/
public function run()
{
$migrations = $this->find_migrations();
// run in sub-function for scope
array_map(array( $this, 'run_migration' ), $migrations);
}
/**
* @return array
*/
public function find_migrations()
{
$files = glob(rtrim($this->migrations_dir, '/') . '/*.php');
$migrations = array();
// return empty array when glob returns non-array value.
if (! is_array($files)) {
return $migrations;
}
foreach ($files as $file) {
$migration = basename($file);
$parts = explode('-', $migration);
$version = $parts[0];
if (version_compare($this->version_from, $version, '<')) {
$migrations[] = $file;
}
}
return $migrations;
}
/**
* Include a migration file and runs it.
*
* @param string $file
*/
protected function run_migration($file)
{
include $file;
}
}

View File

@@ -0,0 +1,88 @@
<?php
defined('ABSPATH') or exit;
// get options
$form_options = get_option('mc4wp_lite_form', array());
// bail if there are no previous options
if (empty($form_options)) {
return;
}
// bail if there are Pro forms already
$has_forms = get_posts(
array(
'post_type' => 'mc4wp-form',
'post_status' => 'publish',
'numberposts' => 1,
)
);
// There are forms already, don't continue.
if (! empty($has_forms)) {
// delete option as it apparently exists.
delete_option('mc4wp_lite_form');
return;
}
// create post type for form
$id = wp_insert_post(
array(
'post_type' => 'mc4wp-form',
'post_status' => 'publish',
'post_title' => __('Default sign-up form', 'mailchimp-for-wp'),
'post_content' => ( empty($form_options['markup']) ) ? '' : $form_options['markup'],
)
);
// set default_form_id
update_option('mc4wp_default_form_id', $id);
// set form settings
$setting_keys = array(
'css',
'custom_theme_color',
'double_optin',
'update_existing',
'replace_interests',
'send_welcome',
'redirect',
'hide_after_success',
);
$settings = array();
foreach ($setting_keys as $setting_key) {
// use isset to account for "0" settings
if (isset($form_options[ $setting_key ])) {
$settings[ $setting_key ] = $form_options[ $setting_key ];
}
}
// get only keys of lists setting
if (isset($form_options['lists'])) {
$settings['lists'] = array_keys($form_options['lists']);
}
update_post_meta($id, '_mc4wp_settings', $settings);
// set form message texts
$message_keys = array(
'text_subscribed',
'text_error',
'text_invalid_email',
'text_already_subscribed',
'text_required_field_missing',
'text_unsubscribed',
'text_not_subscribed',
);
foreach ($message_keys as $message_key) {
if (! empty($form_options[ $message_key ])) {
update_post_meta($id, $message_key, $form_options[ $message_key ]);
}
}
// delete old option
delete_option('mc4wp_lite_form');

View File

@@ -0,0 +1,65 @@
<?php
defined('ABSPATH') or exit;
$global_options = (array) get_option('mc4wp_form', array());
// find all form posts
$posts = get_posts(
array(
'post_type' => 'mc4wp-form',
'post_status' => 'publish',
'numberposts' => -1,
)
);
$css_map = array(
'default' => 'basic',
'custom' => 'styles-builder',
'light' => 'theme-light',
'dark' => 'theme-dark',
'red' => 'theme-red',
'green' => 'theme-green',
'blue' => 'theme-blue',
'custom-color' => 'theme-custom-color',
);
$stylesheets = array();
foreach ($posts as $post) {
// get form options from post meta directly
$options = (array) get_post_meta($post->ID, '_mc4wp_settings', true);
// store all global options in scoped form settings
// do this BEFORE changing css key, so we take that as well.
foreach ($global_options as $key => $value) {
if (strlen($value) > 0 && ( ! isset($options[ $key ]) || strlen($options[ $key ]) == 0 )) {
$options[ $key ] = $value;
}
}
// update "css" option value
if (isset($options['css']) && isset($css_map[ $options['css'] ])) {
$options['css'] = $css_map[ $options['css'] ];
}
// create stylesheets option
if (! empty($options['css'])) {
$stylesheet = $options['css'];
if (strpos($stylesheet, 'theme-') === 0) {
$stylesheet = 'themes';
}
if (! in_array($stylesheet, $stylesheets)) {
$stylesheets[] = $stylesheet;
}
}
update_post_meta($post->ID, '_mc4wp_settings', $options);
}
// update stylesheets option
update_option('mc4wp_form_stylesheets', $stylesheets);
// delete old options
delete_option('mc4wp_form');

View File

@@ -0,0 +1,42 @@
<?php
defined('ABSPATH') or exit;
// find all form posts
$posts = get_posts(
array(
'post_type' => 'mc4wp-form',
'post_status' => 'publish',
'numberposts' => -1,
)
);
// set form message texts
$message_keys = array(
'text_subscribed',
'text_error',
'text_invalid_email',
'text_already_subscribed',
'text_required_field_missing',
'text_unsubscribed',
'text_not_subscribed',
);
foreach ($posts as $post) {
$settings = get_post_meta($post->ID, '_mc4wp_settings', true);
foreach ($message_keys as $key) {
if (empty($settings[ $key ])) {
continue;
}
$message = $settings[ $key ];
// move message setting over to post meta
update_post_meta($post->ID, $key, $message);
unset($settings[ $key ]);
}
// update post meta with unset message keys
update_post_meta($post->ID, '_mc4wp_settings', $settings);
}

View File

@@ -0,0 +1,16 @@
<?php
defined('ABSPATH') or exit;
// transfer option
$options = (array) get_option('mc4wp_lite', array());
// merge options, with Pro options taking precedence
$pro_options = (array) get_option('mc4wp', array());
$options = array_merge($options, $pro_options);
// update options
update_option('mc4wp', $options);
// delete old option
delete_option('mc4wp_lite');

View File

@@ -0,0 +1,64 @@
<?php
defined('ABSPATH') or exit;
$old_options = get_option('mc4wp_lite_checkbox', array());
$pro_options = get_option('mc4wp_checkbox', array());
if (! empty($pro_options)) {
$old_options = array_merge($old_options, $pro_options);
}
// do we have to do something?
if (empty($old_options)) {
return;
}
// find activated integrations (show_at_xxx options)
$new_options = array();
$map = array(
'comment_form' => 'wp-comment-form',
'registration_form' => 'wp-registration-form',
'buddypress_form' => 'buddypress',
'bbpres_forms' => 'bbpress',
'woocommerce_checkout' => 'woocommerce',
'edd_checkout' => 'easy-digital-downloads',
);
$option_keys = array(
'label',
'precheck',
'css',
'lists',
'double_optin',
'update_existing',
'replace_interests',
'send_welcome',
);
foreach ($map as $old_integration_slug => $new_integration_slug) {
// check if integration is enabled using its old slug
$show_key = sprintf('show_at_%s', $old_integration_slug);
if (empty($old_options[ $show_key ])) {
continue;
}
$options = array(
'enabled' => 1,
);
foreach ($option_keys as $option_key) {
if (isset($old_options[ $option_key ])) {
$options[ $option_key ] = $old_options[ $option_key ];
}
}
// add to new options
$new_options[ $new_integration_slug ] = $options;
}
// save new settings
update_option('mc4wp_integrations', $new_options);
// delete old options
delete_option('mc4wp_lite_checkbox');
delete_option('mc4wp_checkbox');

View File

@@ -0,0 +1,41 @@
<?php
defined('ABSPATH') or exit;
// move stylebuilders file to bundle
$file = (string) get_option('mc4wp_custom_css_file', '');
if (empty($file)) {
return;
}
$uploads = wp_upload_dir();
// figure out absolute file path
$prefix = str_replace('http:', '', $uploads['baseurl']);
$relative_path = str_replace($prefix, '', $file);
// get part before ?
if (strpos($relative_path, '?') !== false) {
$parts = explode('?', $relative_path);
$relative_path = array_shift($parts);
}
// This is the absolute path to the file, he he..
$file = $uploads['basedir'] . $relative_path;
if (file_exists($file)) {
// create directory, if necessary
$dir = $uploads['basedir'] . '/mc4wp-stylesheets';
if (! file_exists($dir)) {
@mkdir($dir, 0755);
}
@chmod($dir, 0755);
// Move file to new location
$new_file = $dir . '/bundle.css';
$success = rename($file, $new_file);
}
// remove old option
delete_option('mc4wp_custom_css_file');

View File

@@ -0,0 +1,39 @@
<?php
defined('ABSPATH') or exit;
$section_widgets = get_option('sidebars_widgets', array());
$replaced = false;
foreach ($section_widgets as $section => $widgets) {
// WP has an "array_version" key that is not an array...
if (! is_array($widgets)) {
continue;
}
// loop through widget ID's
foreach ($widgets as $key => $widget_id) {
// does this widget ID start with "mc4wp_widget"?
if (strpos($widget_id, 'mc4wp_widget') === 0) {
// replace "mc4wp_widget" with "mc4wp_form_widget"
$new_widget_id = str_replace('mc4wp_widget', 'mc4wp_form_widget', $widget_id);
$section_widgets[ $section ][ $key ] = $new_widget_id;
$replaced = true;
}
}
}
// update option if we made changes
if ($replaced) {
update_option('sidebars_widgets', $section_widgets);
}
// update widget options
$options = get_option('widget_mc4wp_widget', false);
if ($options) {
update_option('widget_mc4wp_form_widget', $options);
// delete old option
delete_option('widget_mc4wp_widget');
}

View File

@@ -0,0 +1,11 @@
<?php
defined('ABSPATH') or exit;
$options = get_option('mc4wp_integrations', array());
if (! empty($options['woocommerce']) && ! empty($options['woocommerce']['position'])) {
$options['woocommerce']['position'] = sprintf('checkout_%s', $options['woocommerce']['position']);
}
update_option('mc4wp_integrations', $options);

View File

@@ -0,0 +1,112 @@
<?php
defined('ABSPATH') or exit;
/**
* @ignore
* @return object
*/
function _mc4wp_400_find_grouping_for_interest_category($groupings, $interest_category)
{
foreach ($groupings as $grouping) {
// cast to stdClass because of missing class
$grouping = (object) (array) $grouping;
if ($grouping->name === $interest_category->title) {
return $grouping;
}
}
return null;
}
/**
* @ignore
* @return object
*/
function _mc4wp_400_find_group_for_interest($groups, $interest)
{
foreach ($groups as $group_id => $group_name) {
if ($group_name === $interest->name) {
return (object) array(
'name' => $group_name,
'id' => $group_id,
);
}
}
return null;
}
// in case the migration is _very_ late to the party
if (! class_exists('MC4WP_API_V3')) {
return;
}
$options = get_option('mc4wp', array());
if (empty($options['api_key'])) {
return;
}
// get current state from transient
$lists = get_transient('mc4wp_mailchimp_lists_fallback');
if (empty($lists)) {
return;
}
@set_time_limit(600);
$api_v3 = new MC4WP_API_V3($options['api_key']);
$map = array();
foreach ($lists as $list) {
// cast to stdClass because of missing classes
$list = (object) (array) $list;
// no groupings? easy!
if (empty($list->groupings)) {
continue;
}
// fetch (new) interest categories for this list
try {
$interest_categories = $api_v3->get_list_interest_categories($list->id);
} catch (MC4WP_API_Exception $e) {
continue;
}
foreach ($interest_categories as $interest_category) {
// compare interest title with grouping name, if it matches, get new id.
$grouping = _mc4wp_400_find_grouping_for_interest_category($list->groupings, $interest_category);
if (! $grouping) {
continue;
}
$groups = array();
try {
$interests = $api_v3->get_list_interest_category_interests($list->id, $interest_category->id);
} catch (MC4WP_API_Exception $e) {
continue;
}
foreach ($interests as $interest) {
$group = _mc4wp_400_find_group_for_interest($grouping->groups, $interest);
if ($group) {
$groups[ $group->id ] = $interest->id;
$groups[ $group->name ] = $interest->id;
}
}
$map[ (string) $grouping->id ] = array(
'id' => $interest_category->id,
'groups' => $groups,
);
}
}
if (! empty($map)) {
update_option('mc4wp_groupings_map', $map);
}

View File

@@ -0,0 +1,31 @@
<?php
defined('ABSPATH') or exit;
/** @ignore */
function _mc4wp_400_replace_comma_with_pipe($matches)
{
$old = $matches[1];
$new = str_replace(',', '|', $old);
return str_replace($old, $new, $matches[0]);
}
// get all forms
$posts = get_posts(
array(
'post_type' => 'mc4wp-form',
'numberposts' => -1,
)
);
foreach ($posts as $post) {
// find hidden field values in form and pass through replace function
$old = $post->post_content;
$new = preg_replace_callback('/type="hidden" .* value="(.*)"/i', '_mc4wp_400_replace_comma_with_pipe', $old);
// update post if we replaced something
if ($new != $old) {
$post->post_content = $new;
wp_update_post($post);
}
}

View File

@@ -0,0 +1,33 @@
<?php
defined('ABSPATH') or exit;
// get old log filename
$upload_dir = wp_upload_dir(null, false);
$old_filename = trailingslashit($upload_dir['basedir']) . 'mc4wp-debug.log';
$new_filename = trailingslashit($upload_dir['basedir']) . 'mc4wp-debug-log.php';
// check if old default log file exists
if (! file_exists($old_filename)) {
return;
}
// rename to new file.
@rename($old_filename, $new_filename);
// if success, insert php exit tag as first line
if (file_exists($new_filename)) {
$handle = fopen($new_filename, 'r+');
if (is_resource($handle)) {
// make sure first line of log file is a PHP tag + exit statement (to prevent direct file access)
$line = fgets($handle);
$php_exit_string = '<?php exit; ?>';
if (strpos($line, $php_exit_string) !== 0) {
rewind($handle);
fwrite($handle, $php_exit_string . PHP_EOL . $line);
}
fclose($handle);
}
}

View File

@@ -0,0 +1,12 @@
<?php
defined('ABSPATH') or exit;
if (function_exists('mc4wp_refresh_mailchimp_lists')) {
mc4wp_refresh_mailchimp_lists();
}
delete_transient('mc4wp_mailchimp_lists_v3');
delete_option('mc4wp_mailchimp_lists_v3_fallback');
wp_schedule_event(strtotime('tomorrow 3 am'), 'daily', 'mc4wp_refresh_mailchimp_lists');

View File

@@ -0,0 +1,8 @@
<?php
defined('ABSPATH') or exit;
wp_clear_scheduled_hook('mc4wp_refresh_mailchimp_lists');
$time_string = sprintf('tomorrow %d:%d%d am', rand(1, 6), rand(0, 5), rand(0, 9));
wp_schedule_event(strtotime($time_string), 'daily', 'mc4wp_refresh_mailchimp_lists');

View File

@@ -0,0 +1,4 @@
<?php
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'mc4wp_mailchimp_list_%'");

View File

@@ -0,0 +1,18 @@
<?php
defined('ABSPATH') or exit;
// get old filename
$upload_dir = wp_upload_dir(null, false);
$old_filename = trailingslashit($upload_dir['basedir']) . 'mc4wp-debug-log.php';
// if old file exists, move it to new location
if (is_file($old_filename)) {
$new_filename = $upload_dir['basedir'] . '/mailchimp-for-wp/debug-log.php';
$dir = dirname($new_filename);
if (! is_dir($dir)) {
mkdir($dir, 0755, true);
}
rename($old_filename, $new_filename);
}