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,197 @@
<?php
/**
* Class MC4WP_Integration_Admin
*
* @ignore
* @access private
*/
class MC4WP_Integration_Admin
{
/**
* @var MC4WP_Integration_Manager
*/
protected $integrations;
/**
* @var MC4WP_Admin_Messages
*/
protected $messages;
/**
* @param MC4WP_Integration_Manager $integrations
* @param MC4WP_Admin_Messages $messages
*/
public function __construct(MC4WP_Integration_Manager $integrations, MC4WP_Admin_Messages $messages)
{
$this->integrations = $integrations;
$this->messages = $messages;
}
/**
* Add hooks
*/
public function add_hooks()
{
add_action('admin_init', array( $this, 'register_setting' ));
add_action('mc4wp_admin_enqueue_assets', array( $this, 'enqueue_assets' ), 10, 2);
add_filter('mc4wp_admin_menu_items', array( $this, 'add_menu_item' ));
}
/**
* Register settings
*/
public function register_setting()
{
register_setting('mc4wp_integrations_settings', 'mc4wp_integrations', array( $this, 'save_integration_settings' ));
}
/**
* Enqueue assets
*
* @param string $suffix
* @param string $page
*
* @return void
*/
public function enqueue_assets($suffix, $page)
{
// only load on integrations pages
if ($page !== 'integrations') {
return;
}
wp_register_script('mc4wp-integrations-admin', mc4wp_plugin_url('assets/js/integrations-admin.js'), array( 'mc4wp-admin' ), MC4WP_VERSION, true);
wp_enqueue_script('mc4wp-integrations-admin');
}
/**
* @param array $items
*
* @return array
*/
public function add_menu_item($items)
{
$items[] = array(
'title' => esc_html__('Integrations', 'mailchimp-for-wp'),
'text' => esc_html__('Integrations', 'mailchimp-for-wp'),
'slug' => 'integrations',
'callback' => array( $this, 'show_integrations_page' ),
'position' => 20,
);
return $items;
}
/**
* @param array $new_settings
* @return array
*/
public function save_integration_settings(array $new_settings)
{
$integrations = $this->integrations->get_all();
$current_settings = (array) get_option('mc4wp_integrations', array());
$settings = array();
foreach ($integrations as $slug => $integration) {
$settings[ $slug ] = $this->parse_integration_settings($slug, $current_settings, $new_settings);
}
return $settings;
}
/**
* @since 3.0
* @param string $slug
* @param array $current
* @param array $new
*
* @return array
*/
protected function parse_integration_settings($slug, $current, $new)
{
$settings = array();
// start with current settings
if (! empty($current[ $slug ])) {
$settings = $current[ $slug ];
}
// if no new settings were given, return current settings.
if (empty($new[ $slug ])) {
return $settings;
}
// merge new settings with currents (to allow passing partial setting arrays)
$settings = array_merge($settings, $new[ $slug ]);
// sanitize settings
$settings = $this->sanitize_integration_settings($settings);
return $settings;
}
/**
* @param array $settings
* @return array
*/
protected function sanitize_integration_settings($settings)
{
// filter null values from lists setting
if (! empty($settings['lists'])) {
$settings['lists'] = array_filter($settings['lists']);
} else {
$settings['lists'] = array();
}
$settings['label'] = strip_tags($settings['label'], '<strong><b><br><a><script><u><em><i><span><img>');
if (! current_user_can('unfiltered_html')) {
$settings['label'] = mc4wp_kses($settings['label']);
}
return $settings;
}
/**
* Show the Integration Settings page
*
* @internal
*/
public function show_integrations_page()
{
if (! empty($_GET['integration'])) {
$this->show_integration_settings_page($_GET['integration']);
return;
}
// get all installed & enabled integrations
$enabled_integrations = $this->integrations->get_enabled_integrations();
// get all integrations but remove enabled integrations from the resulting array
$integrations = $this->integrations->get_all();
require __DIR__ . '/views/integrations.php';
}
/**
* @param string $slug
*
* @internal
*/
public function show_integration_settings_page($slug)
{
try {
$integration = $this->integrations->get($slug);
} catch (Exception $e) {
echo sprintf('<h3>Integration not found.</h3><p>No integration with slug <strong>%s</strong> was found.</p>', esc_html($slug));
return;
}
$opts = $integration->options;
$mailchimp = new MC4WP_MailChimp();
$lists = $mailchimp->get_lists();
require __DIR__ . '/views/integration-settings.php';
}
}

View File

@@ -0,0 +1,104 @@
<?php
/**
* Class MC4WP_Integration_Fixture
*
* @since 3.0
* @ignore
*/
class MC4WP_Integration_Fixture
{
/**
* @var string
*/
public $slug;
/**
* @var string
*/
public $class;
/**
* @var bool
*/
public $enabled;
/**
* @var bool
*/
public $enabled_by_default;
/**
* @var MC4WP_Integration
*/
public $instance;
/**
* @var array
*/
public $options;
/**
* @param string $slug
* @param string $class
* @param bool $enabled_by_default
* @param array $options
*/
public function __construct($slug, $class, $enabled_by_default, array $options)
{
$this->slug = $slug;
$this->class = $class;
$this->enabled_by_default = $enabled_by_default;
$this->enabled = $enabled_by_default;
$this->options = $options;
if (! empty($options['enabled'])) {
$this->enabled = true;
}
}
/**
* Returns the actual instance
*
* @return MC4WP_Integration
*/
public function load()
{
if (! $this->instance instanceof MC4WP_Integration) {
$this->instance = new $this->class($this->slug, $this->options);
}
return $this->instance;
}
/**
* Tunnel everything to MC4WP_Integration class
*
* @param string $name
* @param array $arguments
*
* @return MC4WP_Integration
*/
public function __call($name, $arguments)
{
return call_user_func_array(array( $this->load(), $name ), $arguments);
}
/**
* @param string $name
*
* @return string
*/
public function __get($name)
{
return $this->load()->$name;
}
/**
* @return string
*/
public function __toString()
{
return $this->slug;
}
}

View File

@@ -0,0 +1,188 @@
<?php
/**
* Class MC4WP_Integration_Manager
*
* @ignore
* @access private
*/
class MC4WP_Integration_Manager
{
/**
* @var MC4WP_Integration_Fixture[]
*/
protected $integrations = array();
/**
* @var MC4WP_Integration_Tags
*/
protected $tags;
/**
* Constructor
*/
public function __construct()
{
$this->tags = new MC4WP_Integration_Tags();
}
/**
* Add hooks
*/
public function add_hooks()
{
add_action('after_setup_theme', array( $this, 'initialize' ));
$this->tags->add_hooks();
}
/**
* Add hooks
*/
public function initialize()
{
/*** @var MC4WP_Integration_Fixture $integration */
$enabled_integrations = $this->get_enabled_integrations();
foreach ($enabled_integrations as $integration) {
$integration->load()->initialize();
}
}
/**
* Get an integration instance
*
* @return MC4WP_Integration_Fixture[]
* @throws Exception
*/
public function get_all()
{
return $this->integrations;
}
/**
* Get an integration instance
*
* @param string $slug
* @return MC4WP_Integration
* @throws Exception
*/
public function get($slug)
{
if (! isset($this->integrations[ $slug ])) {
throw new Exception(sprintf('No integration with slug %s has been registered.', $slug));
}
return $this->integrations[ $slug ]->load();
}
/**
* Register a new integration class
*
* @param string $slug
* @param string $class
* @param bool $enabled
*/
public function register_integration($slug, $class, $enabled = false)
{
$raw_options = $this->get_integration_options($slug);
$this->integrations[ $slug ] = new MC4WP_Integration_Fixture($slug, $class, $enabled, $raw_options);
}
/**
* Deregister an integration class
*
* @param string $slug
*/
public function deregister_integration($slug)
{
if (isset($this->integrations[ $slug ])) {
unset($this->integrations[ $slug ]);
}
}
/**
* Checks whether a certain integration is enabled (in the settings)
*
* This is decoupled from the integration class itself as checking an array is way "cheaper" than instantiating an object
*
* @param MC4WP_Integration_Fixture $integration
*
* @return bool
*/
public function is_enabled(MC4WP_Integration_Fixture $integration)
{
return $integration->enabled;
}
/**
* @param MC4WP_Integration $integration
* @return bool
*/
public function is_installed($integration)
{
return $integration->is_installed();
}
/**
* Get the integrations which are enabled
*
* - Some integrations are always enabled because they need manual work
* - Other integrations can be enabled in the settings page
* - Only returns installed integrations
*
* @return array
*/
public function get_enabled_integrations()
{
// get all enabled integrations
$enabled_integrations = array_filter($this->integrations, array( $this, 'is_enabled' ));
// remove duplicate values, for whatever reason..
$enabled_integrations = array_unique($enabled_integrations);
// filter out integrations which are not installed
$installed_enabled_integrations = array_filter($enabled_integrations, array( $this, 'is_installed' ));
return $installed_enabled_integrations;
}
/**
* Gets all integration options in a keyed array
*
* @return array
*/
private function load_options()
{
$options = (array) get_option('mc4wp_integrations', array());
/**
* Filters global integration options
*
* This array holds ALL integration settings
*
* @since 3.0
* @param array $options
* @ignore
*/
return (array) apply_filters('mc4wp_integration_options', $options);
}
/**
* Gets the raw options for an integration
*
* @param $slug
* @return array
*/
public function get_integration_options($slug)
{
static $options;
if ($options === null) {
$options = $this->load_options();
}
return isset($options[ $slug ]) ? $options[ $slug ] : array();
}
}

View File

@@ -0,0 +1,62 @@
<?php
/**
* Class MC4WP_Integration_Tags
*
* @ignore
* @access private
*/
class MC4WP_Integration_Tags extends MC4WP_Dynamic_Content_Tags
{
/**
* @var MC4WP_Integration
*/
protected $integration;
/**
* Add hooks
*/
public function add_hooks()
{
add_filter('mc4wp_integration_checkbox_label', array( $this, 'replace_in_checkbox_label' ), 10, 2);
}
/**
* Register template tags for integrations
*/
public function register()
{
parent::register();
$this->tags['subscriber_count'] = array(
'description' => __('Replaced with the number of subscribers on the selected list(s)', 'mailchimp-for-wp'),
'callback' => array( $this, 'get_subscriber_count' ),
);
}
/**
* @hooked `mc4wp_integration_checkbox_label`
* @param string $string
* @param MC4WP_Integration $integration
* @return string
*/
public function replace_in_checkbox_label($string, MC4WP_Integration $integration)
{
$this->integration = $integration;
$string = $this->replace($string, 'esc_html');
return $string;
}
/**
* Returns the number of subscribers on the selected lists (for the form context)
*
* @return int
*/
public function get_subscriber_count()
{
$mailchimp = new MC4WP_MailChimp();
$list_ids = $this->integration->get_lists();
$count = $mailchimp->get_subscriber_count($list_ids);
return number_format($count);
}
}

View File

@@ -0,0 +1,590 @@
<?php
/**
* Class MC4WP_Integration
*
* Base class for all integrations.
*
* Extend this class and implement the `add_hooks` method to get a settings page.
*
* @access public
* @since 3.0
* @abstract
*/
abstract class MC4WP_Integration
{
/**
* @var string Name of this integration.
*/
public $name = '';
/**
* @var string Description
*/
public $description = '';
/**
* @var string Slug, used as an unique identifier for this integration.
*/
public $slug = '';
/**
* @var array Array of settings
*/
public $options = array();
/**
* @var string Name attribute for the checkbox element. Will be created from slug if empty.
*/
protected $checkbox_name = '';
/**
* @var string[]
*/
public $checkbox_classes = array();
/**
* @var string[]
*/
public $wrapper_classes = array();
/**
* Constructor
*
* @param string $slug
* @param array $options
*/
public function __construct($slug, array $options)
{
$this->slug = $slug;
$this->options = $this->parse_options($options);
// if checkbox name is not set, set a good custom value
if ($this->checkbox_name === '') {
$this->checkbox_name = '_mc4wp_subscribe_' . $this->slug;
}
}
/**
* Return array of default options
*
* @return array
*/
protected function get_default_options()
{
return array(
'css' => 0,
'double_optin' => 1,
'enabled' => 0,
'implicit' => 0,
'label' => __('Sign me up for the newsletter!', 'mailchimp-for-wp'),
'lists' => array(),
'precheck' => 0,
'replace_interests' => 0,
'update_existing' => 0,
'wrap_p' => 1,
);
}
/**
* @param array $options
*
* @return array
*/
protected function parse_options(array $options)
{
$slug = $this->slug;
$default_options = $this->get_default_options();
$options = array_merge($default_options, $options);
/**
* Filters options for a specific integration
*
* The dynamic portion of the hook, `$slug`, refers to the slug of the ingration.
*
* @param array $integration_options
*/
return (array) apply_filters('mc4wp_integration_' . $slug . '_options', $options);
}
/**
* Initialize the integration
*/
public function initialize()
{
$this->add_required_hooks();
$this->add_hooks();
}
/**
* Adds the required hooks for core functionality, like adding checkbox reset CSS.
*/
protected function add_required_hooks()
{
if ($this->options['css'] && ! $this->options['implicit']) {
add_action('wp_head', array( $this, 'print_css_reset' ));
}
}
/**
* Was integration triggered?
*
* Will always return true when integration is implicit. Otherwise, will check value of checkbox.
*
* @param int $object_id Useful when overriding method. (optional)
* @return bool
*/
public function triggered($object_id = null)
{
return $this->options['implicit'] || $this->checkbox_was_checked();
}
/**
* Adds the hooks which are specific to this integration
*/
abstract protected function add_hooks();
/**
* Print CSS reset
*
* @hooked `wp_head`
*/
public function print_css_reset()
{
$css = file_get_contents(MC4WP_PLUGIN_DIR . '/assets/css/checkbox-reset.css');
// replace selector by integration specific selector so the css affects just this checkbox
$css = str_ireplace('__INTEGRATION_SLUG__', $this->slug, $css);
printf('<style>%s</style>', $css);
}
/**
* Get the text for the label element
*
* @return string
*/
public function get_label_text()
{
$integration = $this;
$label = $this->options['label'];
if (empty($label)) {
$default_options = $this->get_default_options();
$label = $default_options['label'];
}
/**
* Filters the checkbox label
*
* @since 3.0
*
* @param string $label
* @param MC4WP_Integration $integration
* @ignore
*/
$label = (string) apply_filters('mc4wp_integration_checkbox_label', $label, $integration);
return $label;
}
/**
* Was the integration checkbox checked?
*
* @return bool
*/
public function checkbox_was_checked()
{
$data = $this->get_data();
return isset($data[ $this->checkbox_name ]) && (int) $data[ $this->checkbox_name ] === 1;
}
/**
* Get a string of attributes for the HTML element wrapping the checkbox + label
*
* @return string
*/
protected function get_wrapper_attributes()
{
$html_attrs = array(
'class' => sprintf('mc4wp-checkbox mc4wp-checkbox-%s %s', $this->slug, join(' ', $this->wrapper_classes)),
);
return $this->array_to_attr_string($html_attrs);
}
/**
* Get a string of attributes for the checkbox element.
*
* @return string
*/
protected function get_checkbox_attributes()
{
$integration = $this;
$slug = $this->slug;
$attributes = array();
if ($this->options['precheck']) {
$attributes['checked'] = 'checked';
}
if (! empty($this->checkbox_classes)) {
$attributes['class'] = join(' ', $this->checkbox_classes);
}
/**
* Filters the attributes array.
*
* @param array $attributes
* @param MC4WP_Integration $integration
* @ignore
*/
$attributes = (array) apply_filters('mc4wp_integration_checkbox_attributes', $attributes, $integration);
/**
* Filters the attributes array.
*
* The dynamic portion of the hook, `$slug`, refers to the slug for this integration.
*
* @param array $attributes
* @param MC4WP_Integration $integration
* @ignore
*/
$attributes = (array) apply_filters('mc4wp_integration_' . $slug . '_checkbox_attributes', $attributes, $integration);
return $this->array_to_attr_string($attributes);
}
/**
* Outputs a checkbox
*/
public function output_checkbox()
{
echo $this->get_checkbox_html();
}
/**
* Get HTML string for the checkbox row (incl. wrapper, label, etc.)
*
* @return string
*/
public function get_checkbox_html()
{
$show_checkbox = empty($this->options['implicit']);
$integration_slug = $this->slug;
/**
* Filters whether to show the sign-up checkbox for this integration.
*
* @param bool $show_checkbox
* @param string $integration_slug
*/
$show_checkbox = (bool) apply_filters('mc4wp_integration_show_checkbox', $show_checkbox, $integration_slug);
if (! $show_checkbox) {
return '';
}
ob_start();
echo sprintf('<!-- Mailchimp for WordPress v%s - https://www.mc4wp.com/ -->', MC4WP_VERSION);
/** @ignore */
do_action('mc4wp_integration_before_checkbox_wrapper', $this);
/** @ignore */
do_action('mc4wp_integration_' . $this->slug . '_before_checkbox_wrapper', $this);
$wrapper_tag = $this->options['wrap_p'] ? 'p' : 'span';
$wrapper_attrs = $this->get_wrapper_attributes();
// Hidden field to make sure "0" is sent to server
echo sprintf('<input type="hidden" name="%s" value="0" />', esc_attr($this->checkbox_name));
echo sprintf('<%s %s>', $wrapper_tag, $wrapper_attrs);
echo '<label>';
echo sprintf('<input type="checkbox" name="%s" value="1" %s />', esc_attr($this->checkbox_name), $this->get_checkbox_attributes());
echo sprintf('<span>%s</span>', $this->get_label_text());
echo '</label>';
echo sprintf('</%s>', $wrapper_tag);
/** @ignore */
do_action('mc4wp_integration_after_checkbox_wrapper', $this);
/** @ignore */
do_action('mc4wp_integration_' . $this->slug . '_after_checkbox_wrapper', $this);
echo '<!-- / Mailchimp for WordPress -->';
$html = ob_get_clean();
return $html;
}
/**
* Get the selected Mailchimp lists
*
* @return array Array of List ID's
*/
public function get_lists()
{
$data = $this->get_data();
$integration = $this;
$slug = $this->slug;
// get checkbox lists options
$lists = $this->options['lists'];
// get lists from request, if set.
if (! empty($data['_mc4wp_lists'])) {
$lists = $data['_mc4wp_lists'];
// ensure lists is an array
if (! is_array($lists)) {
$lists = explode(',', $lists);
$lists = array_map('trim', $lists);
}
}
/**
* Allow plugins to filter final lists value. This filter is documented elsewhere.
*
* @since 2.0
* @see MC4WP_Form::get_lists
* @ignore
*/
$lists = (array) apply_filters('mc4wp_lists', $lists);
/**
* Filters the Mailchimp lists this integration should subscribe to
*
* @since 3.0
*
* @param array $lists
* @param MC4WP_Integration $integration
*/
$lists = (array) apply_filters('mc4wp_integration_lists', $lists, $integration);
/**
* Filters the Mailchimp lists a specific integration should subscribe to
*
* The dynamic portion of the hook, `$slug`, refers to the slug of the integration.
*
* @since 3.0
*
* @param array $lists
* @param MC4WP_Integration $integration
*/
$lists = (array) apply_filters('mc4wp_integration_' . $slug . '_lists', $lists, $integration);
return $lists;
}
/**
* Makes a subscription request
*
* @param array $data
* @param int $related_object_id
*
* @return boolean
*/
protected function subscribe(array $data, $related_object_id = 0)
{
$integration = $this;
$slug = $this->slug;
$mailchimp = new MC4WP_MailChimp();
$log = $this->get_log();
$list_ids = $this->get_lists();
/** @var MC4WP_MailChimp_Subscriber $subscriber */
$subscriber = null;
$result = false;
// validate lists
if (empty($list_ids)) {
$log->warning(sprintf('%s > No Mailchimp lists were selected', $this->name));
return false;
}
/**
* Filters data for integration requests.
*
* @param array $data
*/
$data = apply_filters('mc4wp_integration_data', $data);
/**
* Filters data for a specific integration request.
*
* The dynamic portion of the hook, `$slug`, refers to the integration slug.
*
* @param array $data
* @param int $related_object_id
*/
$data = apply_filters("mc4wp_integration_{$slug}_data", $data, $related_object_id);
$email_type = mc4wp_get_email_type();
$mapper = new MC4WP_List_Data_Mapper($data, $list_ids);
/** @var MC4WP_MailChimp_Subscriber[] $map */
$map = $mapper->map();
foreach ($map as $list_id => $subscriber) {
$subscriber->status = $this->options['double_optin'] ? 'pending' : 'subscribed';
$subscriber->email_type = $email_type;
$subscriber->ip_signup = mc4wp_get_request_ip_address();
/** @ignore (documented elsewhere) */
$subscriber = apply_filters('mc4wp_subscriber_data', $subscriber);
if (! $subscriber instanceof MC4WP_MailChimp_Subscriber) {
continue;
}
/**
* Filters subscriber data before it is sent to Mailchimp. Only fires for integration requests.
*
* @param MC4WP_MailChimp_Subscriber $subscriber
*/
$subscriber = apply_filters('mc4wp_integration_subscriber_data', $subscriber);
if (! $subscriber instanceof MC4WP_MailChimp_Subscriber) {
continue;
}
/**
* Filters subscriber data before it is sent to Mailchimp. Only fires for integration requests.
*
* The dynamic portion of the hook, `$slug`, refers to the integration slug.
*
* @param MC4WP_MailChimp_Subscriber $subscriber
* @param int $related_object_id
*/
$subscriber = apply_filters("mc4wp_integration_{$slug}_subscriber_data", $subscriber, $related_object_id);
if (! $subscriber instanceof MC4WP_MailChimp_Subscriber) {
continue;
}
$result = $mailchimp->list_subscribe($list_id, $subscriber->email_address, $subscriber->to_array(), $this->options['update_existing'], $this->options['replace_interests']);
}
// if result failed, show error message
if (! $result) {
// log error
if ((int) $mailchimp->get_error_code() === 214) {
$log->warning(sprintf('%s > %s is already subscribed to the selected list(s)', $this->name, $subscriber->email_address));
} else {
$log->error(sprintf('%s > Mailchimp API Error: %s', $this->name, $mailchimp->get_error_message()));
}
// bail
return false;
}
$log->info(sprintf('%s > Successfully subscribed %s', $this->name, $subscriber->email_address));
/**
* Runs right after someone is subscribed using an integration
*
* @since 3.0
*
* @param MC4WP_Integration $integration
* @param string $email_address
* @param array $merge_vars
* @param MC4WP_MailChimp_Subscriber[] $subscriber_data
* @param int $related_object_id
*/
do_action('mc4wp_integration_subscribed', $integration, $subscriber->email_address, $subscriber->merge_fields, $map, $related_object_id);
return true;
}
/**
* Are the required dependencies for this integration installed?
*
* @return bool
*/
public function is_installed()
{
return false;
}
/**
* Which UI elements should we show on the settings page for this integration?
*
* @return array
*/
public function get_ui_elements()
{
return array_keys($this->options);
}
/**
* Does integration have the given UI element?
*
* @param string $element
* @return bool
*/
public function has_ui_element($element)
{
$elements = $this->get_ui_elements();
return in_array($element, $elements, true);
}
/**
* Return a string to the admin settings page for this object (if any)
*
* @param int $object_id
* @return string
*/
public function get_object_link($object_id)
{
return '';
}
/**
* Get the data for this integration request
*
* By default, this will return a combination of all $_GET and $_POST parameters.
* Override this method if you need data from somewhere else.
*
* This data should contain the value of the checkbox (required)
* and the lists to which should be subscribed (optional)
*
* @see MC4WP_Integration::$checkbox_name
* @see MC4WP_Integration::get_lists
* @see MC4WP_Integration::checkbox_was_checked
*
* @return array
*/
public function get_data()
{
return array_merge((array) $_GET, (array) $_POST);
}
/**
* Converts an array to an attribute string (foo="bar" bar="foo") with escaped values.
*
* @param array $attrs
* @return string
*/
protected function array_to_attr_string(array $attrs)
{
$str = '';
foreach ($attrs as $key => $value) {
$str .= sprintf('%s="%s" ', $key, esc_attr($value));
}
return $str;
}
/**
* @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,58 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_User_Integration
*
* @access public
* @since 2.0
*/
abstract class MC4WP_User_Integration extends MC4WP_Integration
{
/**
* @param WP_User $user
*
* @return array
*/
protected function user_merge_vars(WP_User $user)
{
// start with user_login as name, since that's always known
$data = array(
'EMAIL' => $user->user_email,
'NAME' => $user->user_login,
);
if ('' !== $user->first_name) {
$data['NAME'] = $user->first_name;
$data['FNAME'] = $user->first_name;
}
if ('' !== $user->last_name) {
$data['LNAME'] = $user->last_name;
}
if ('' !== $user->first_name && '' !== $user->last_name) {
$data['NAME'] = sprintf('%s %s', $user->first_name, $user->last_name);
}
/**
* @use mc4wp_integration_user_data
* @since 3.0
* @deprecated 4.0
* @ignore
*/
$data = (array) apply_filters('mc4wp_user_merge_vars', $data, $user);
/**
* Filters the data for user-related integrations
* @since 4.2
* @param array $data
* @param WP_User $user
*/
$data = (array) apply_filters('mc4wp_integration_user_data', $data, $user);
return $data;
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* Gets an array of all registered integrations
*
* @since 3.0
* @access public
*
* @return MC4WP_Integration[]
*/
function mc4wp_get_integrations()
{
return mc4wp('integrations')->get_all();
}
/**
* Get an instance of a registered integration class
*
* @since 3.0
* @access public
*
* @param string $slug
*
* @return MC4WP_Integration
*/
function mc4wp_get_integration($slug)
{
return mc4wp('integrations')->get($slug);
}
/**
* Register a new integration with Mailchimp for WordPress
*
* @since 3.0
* @access public
*
* @param string $slug
* @param string $class
*
* @param bool $always_enabled
*/
function mc4wp_register_integration($slug, $class, $always_enabled = false)
{
return mc4wp('integrations')->register_integration($slug, $class, $always_enabled);
}
/**
* Deregister a previously registered integration with Mailchimp for WordPress
*
* @since 3.0
* @access public
* @param string $slug
*/
function mc4wp_deregister_integration($slug)
{
mc4wp('integrations')->deregister_integration($slug);
}

View File

@@ -0,0 +1,330 @@
<?php defined('ABSPATH') or exit;
/** @var MC4WP_Integration $integration */
/** @var array $opts */
?>
<div id="mc4wp-admin" class="wrap mc4wp-settings">
<p class="mc4wp-breadcrumbs">
<span class="prefix"><?php echo esc_html__('You are here: ', 'mailchimp-for-wp'); ?></span>
<a href="<?php echo esc_url(admin_url('admin.php?page=mailchimp-for-wp')); ?>">Mailchimp for WordPress</a> &rsaquo;
<a href="<?php echo esc_url(admin_url('admin.php?page=mailchimp-for-wp-integrations')); ?>"><?php echo esc_html__('Integrations', 'mailchimp-for-wp'); ?></a> &rsaquo;
<span class="current-crumb"><strong><?php echo esc_html($integration->name); ?></strong></span>
</p>
<div class="mc4wp-row">
<!-- Main Content -->
<div class="main-content mc4wp-col">
<h1 class="mc4wp-page-title">
<?php printf(esc_html__('%s integration', 'mailchimp-for-wp'), esc_html($integration->name)); ?>
</h1>
<h2 style="display: none;"></h2>
<?php settings_errors(); ?>
<div id="notice-additional-fields" class="notice notice-warning" style="display: none;">
<p><?php echo esc_html__('The selected Mailchimp lists require non-default fields, which may prevent this integration from working.', 'mailchimp-for-wp'); ?></p>
<p><?php echo sprintf(wp_kses(__('Please ensure you <a href="%1$s">configure the plugin to send all required fields</a> or <a href="%2$s">log into your Mailchimp account</a> and make sure only the email & name fields are marked as required fields for the selected list(s).', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), 'https://www.mc4wp.com/kb/send-additional-fields-from-integrations/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page', 'https://admin.mailchimp.com/lists/'); ?></p>
</div>
<p>
<?php echo esc_html($integration->description); ?>
</p>
<!-- Settings form -->
<form method="post" action="<?php echo admin_url('options.php'); ?>">
<?php settings_fields('mc4wp_integrations_settings'); ?>
<?php
/**
* Runs just before integration settings are outputted in admin.
*
* @since 3.0
*
* @param MC4WP_Integration $integration
* @param array $opts
* @ignore
*/
do_action('mc4wp_admin_before_integration_settings', $integration, $opts);
/**
* @ignore
*/
do_action('mc4wp_admin_before_' . $integration->slug . '_integration_settings', $integration, $opts);
?>
<table class="form-table">
<?php
if ($integration->has_ui_element('enabled')) {
?>
<tr valign="top">
<th scope="row"><?php echo esc_html__('Enabled?', 'mailchimp-for-wp'); ?></th>
<td class="nowrap integration-toggles-wrap">
<label><input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][enabled]" value="1" <?php checked($opts['enabled'], 1); ?> /> <?php echo esc_html__('Yes', 'mailchimp-for-wp'); ?></label> &nbsp;
<label><input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][enabled]" value="0" <?php checked($opts['enabled'], 0); ?> /> <?php echo esc_html__('No', 'mailchimp-for-wp'); ?></label>
<p class="description"><?php echo sprintf(esc_html__('Enable the %s integration? This will add a sign-up checkbox to the form.', 'mailchimp-for-wp'), $integration->name); ?></p>
</td>
</tr>
<?php
}
?>
<?php
$config = array(
'element' => 'mc4wp_integrations[' . $integration->slug . '][enabled]',
'value' => '1',
'hide' => false,
);
?>
<tbody class="integration-toggled-settings" data-showif="<?php echo esc_attr(json_encode($config)); ?>">
<?php
if ($integration->has_ui_element('implicit')) {
?>
<tr valign="top">
<th scope="row"><?php echo esc_html__('Implicit?', 'mailchimp-for-wp'); ?></th>
<td class="nowrap">
<label><input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][implicit]" value="1" <?php checked($opts['implicit'], 1); ?> /> <?php echo esc_html__('Yes', 'mailchimp-for-wp'); ?></label> &nbsp;
<label><input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][implicit]" value="0" <?php checked($opts['implicit'], 0); ?> /> <?php echo esc_html__('No', 'mailchimp-for-wp'); ?> <?php echo '<em>', esc_html__('(recommended)', 'mailchimp-for-wp'), '</em>'; ?>
</label>
<p class="description">
<?php
echo esc_html__('Select "yes" if you want to subscribe people without asking them explicitly.', 'mailchimp-for-wp');
echo '<br />';
echo sprintf(
wp_kses(
__('<strong>Warning: </strong> enabling this may affect your <a href="%s">GDPR compliance</a>.', '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'
);
?>
</p>
</td>
</tr>
<?php
}
?>
<?php
if ($integration->has_ui_element('lists')) {
?>
<?php // hidden input to make sure a value is sent to the server when no checkboxes were selected ?>
<input type="hidden" name="mc4wp_integrations[<?php echo $integration->slug; ?>][lists][]" value="" />
<tr valign="top">
<th scope="row"><?php echo esc_html__('Mailchimp Lists', 'mailchimp-for-wp'); ?></th>
<?php
if (! empty($lists)) {
echo '<td>';
echo '<ul style="margin-bottom: 20px; max-height: 300px; overflow-y: auto;">';
foreach ($lists as $list) {
echo '<li><label>';
echo sprintf('<input type="checkbox" name="mc4wp_integrations[%s][lists][]" value="%s" class="mc4wp-list-input" %s> ', $integration->slug, $list->id, checked(in_array($list->id, $opts['lists'], true), true, false));
echo esc_html($list->name);
echo '</label></li>';
}
echo '</ul>';
echo '<p class="description">';
echo esc_html__('Select the list(s) to which people who check the checkbox should be subscribed.', 'mailchimp-for-wp');
echo '</p>';
echo '</td>';
} else {
echo '<td>', sprintf(wp_kses(__('No lists found, <a href="%s">are you connected to Mailchimp</a>?', 'mailchimp-for-wp'), array( 'a' => array( 'href' => array() ) )), esc_url(admin_url('admin.php?page=mailchimp-for-wp'))), '</td>';
}
?>
</tr>
<?php
} // end if UI has lists
?>
<?php
if ($integration->has_ui_element('label')) {
$config = array(
'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]',
'value' => 0,
);
?>
<tr valign="top" data-showif="<?php echo esc_attr(json_encode($config)); ?>">
<th scope="row"><label for="mc4wp_checkbox_label"><?php echo esc_html__('Checkbox label text', 'mailchimp-for-wp'); ?></label></th>
<td>
<input type="text" class="widefat" id="mc4wp_checkbox_label" name="mc4wp_integrations[<?php echo $integration->slug; ?>][label]" value="<?php echo esc_attr($opts['label']); ?>" required />
<p class="description"><?php printf(esc_html__('HTML tags like %s are allowed in the label text.', 'mailchimp-for-wp'), '<code>' . esc_html('<strong><em><a>') . '</code>'); ?></p>
</td>
</tr>
<?php
} // end if UI label
?>
<?php
if ($integration->has_ui_element('precheck')) {
$config = array(
'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]',
'value' => 0,
);
?>
<tr valign="top" data-showif="<?php echo esc_attr(json_encode($config)); ?>">
<th scope="row"><?php echo esc_html__('Pre-check the checkbox?', 'mailchimp-for-wp'); ?></th>
<td class="nowrap">
<label><input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][precheck]" value="1" <?php checked($opts['precheck'], 1); ?> /> <?php echo esc_html__('Yes', 'mailchimp-for-wp'); ?></label> &nbsp;
<label><input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][precheck]" value="0" <?php checked($opts['precheck'], 0); ?> /> <?php echo esc_html__('No', 'mailchimp-for-wp'); ?> <?php echo '<em>' . __('(recommended)', 'mailchimp-for-wp') . '</em>'; ?></label>
<p class="description">
<?php
echo esc_html__('Select "yes" if the checkbox should be pre-checked.', 'mailchimp-for-wp');
echo '<br />';
echo sprintf(
wp_kses(
__('<strong>Warning: </strong> enabling this may affect your <a href="%s">GDPR compliance</a>.', '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'
);
?>
</p>
</td>
<?php
} // end if UI precheck
?>
<?php
if ($integration->has_ui_element('css')) {
$config = array(
'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]',
'value' => 0,
);
?>
<tr valign="top" data-showif="<?php echo esc_attr(json_encode($config)); ?>">
<th scope="row"><?php echo esc_html__('Load some default CSS?', 'mailchimp-for-wp'); ?></th>
<td class="nowrap">
<label><input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][css]" value="1" <?php checked($opts['css'], 1); ?> />&rlm; <?php echo esc_html__('Yes', 'mailchimp-for-wp'); ?></label> &nbsp;
<label><input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][css]" value="0" <?php checked($opts['css'], 0); ?> />&rlm; <?php echo esc_html__('No', 'mailchimp-for-wp'); ?></label>
<p class="description"><?php echo esc_html__('Select "yes" if the checkbox appears in a weird place.', 'mailchimp-for-wp'); ?></p>
</td>
</tr>
<?php
} // end if UI css
?>
<?php
if ($integration->has_ui_element('double_optin')) {
?>
<tr valign="top">
<th scope="row"><?php echo esc_html__('Double opt-in?', 'mailchimp-for-wp'); ?></th>
<td class="nowrap">
<label>
<input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][double_optin]" value="1" <?php checked($opts['double_optin'], 1); ?> />&rlm;
<?php echo esc_html__('Yes', 'mailchimp-for-wp'); ?>
</label> &nbsp;
<label>
<input type="radio" id="mc4wp_checkbox_double_optin_0" name="mc4wp_integrations[<?php echo $integration->slug; ?>][double_optin]" value="0" <?php checked($opts['double_optin'], 0); ?> />&rlm;
<?php echo esc_html__('No', 'mailchimp-for-wp'); ?>
</label>
<p class="description">
<?php echo esc_html__('Select "yes" if you want people to confirm their email address before being subscribed (recommended)', 'mailchimp-for-wp'); ?>
</p>
</td>
</tr>
<?php
} // end if UI double_optin
?>
<?php
if ($integration->has_ui_element('update_existing')) {
?>
<tr valign="top">
<th scope="row"><?php echo esc_html__('Update existing subscribers?', 'mailchimp-for-wp'); ?></th>
<td class="nowrap">
<label>
<input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][update_existing]" value="1" <?php checked($opts['update_existing'], 1); ?> />&rlm;
<?php echo esc_html__('Yes', 'mailchimp-for-wp'); ?>
</label> &nbsp;
<label>
<input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][update_existing]" value="0" <?php checked($opts['update_existing'], 0); ?> />&rlm;
<?php echo esc_html__('No', 'mailchimp-for-wp'); ?>
</label>
<p class="description"><?php echo esc_html__('Select "yes" if you want to update existing subscribers with the data that is sent.', 'mailchimp-for-wp'); ?></p>
</td>
</tr>
<?php
} // end if UI update_existing
?>
<?php
if ($integration->has_ui_element('replace_interests')) {
$config = array(
'element' => 'mc4wp_integrations[' . $integration->slug . '][update_existing]',
'value' => 1,
);
?>
<tr valign="top" data-showif="<?php echo esc_attr(json_encode($config)); ?>">
<th scope="row"><?php echo esc_html__('Replace interest groups?', 'mailchimp-for-wp'); ?></th>
<td class="nowrap">
<label>
<input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][replace_interests]" value="1" <?php checked($opts['replace_interests'], 1); ?> />&rlm;
<?php echo esc_html__('Yes', 'mailchimp-for-wp'); ?>
</label> &nbsp;
<label>
<input type="radio" name="mc4wp_integrations[<?php echo $integration->slug; ?>][replace_interests]" value="0" <?php checked($opts['replace_interests'], 0); ?> />&rlm;
<?php echo esc_html__('No', 'mailchimp-for-wp'); ?>
</label>
<p class="description">
<?php echo esc_html__('Select "no" if you want to add the selected interests to any previously selected interests when updating a subscriber.', 'mailchimp-for-wp'); ?>
<?php echo sprintf('<a href="%s" target="_blank">' . esc_html__('What does this do?', 'mailchimp-for-wp') . '</a>', 'https://www.mc4wp.com/kb/what-does-replace-groupings-mean/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page'); ?>
</p>
</td>
</tr>
<?php
} // end if UI replace_interests
?>
</tbody>
</table>
<?php
/**
* Runs right after integration settings are outputted (before the submit button).
*
* @param MC4WP_Integration $integration
* @param array $opts
* @ignore
*/
do_action('mc4wp_admin_after_integration_settings', $integration, $opts);
/**
* @ignore
*/
do_action('mc4wp_admin_after_' . $integration->slug . '_integration_settings', $integration, $opts);
?>
<?php
if (count($integration->get_ui_elements()) > 0) {
submit_button();
}
?>
</form>
</div>
<!-- Sidebar -->
<div class="mc4wp-sidebar mc4wp-col">
<?php require MC4WP_PLUGIN_DIR . '/includes/views/parts/admin-sidebar.php'; ?>
</div>
</div>
</div>

View File

@@ -0,0 +1,142 @@
<?php defined('ABSPATH') or exit;
/** @var MC4WP_Integration_Fixture[] $enabled_integrations */
/** @var MC4WP_Integration_Fixture[] $available_integrations */
/** @var MC4WP_Integration_Fixture $integration */
function _mc4wp_integrations_table_row($integration)
{
?>
<tr style="
<?php
if (! $integration->is_installed()) {
echo 'opacity: 0.6;';
}
?>
">
<!-- Integration Name -->
<td>
<?php
if ($integration->is_installed()) {
echo sprintf('<strong><a href="%s" title="%s">%s</a></strong>', esc_attr(add_query_arg(array( 'integration' => $integration->slug ))), esc_html__('Configure this integration', 'mailchimp-for-wp'), $integration->name);
} else {
echo $integration->name;
}
?>
</td>
<td class="desc">
<?php
echo esc_html($integration->description);
?>
</td>
<td>
<?php
if ($integration->enabled && $integration->is_installed()) {
echo '<span class="mc4wp-status positive">', esc_html__('Active', 'mailchimp-for-wp'), '</span>';
} elseif ($integration->is_installed()) {
echo '<span class="mc4wp-status neutral">', esc_html__('Inactive', 'mailchimp-for-wp'), '</span>';
} else {
echo '<span>', esc_html__('Not installed', 'mailchimp-for-wp'), '</span>';
}
?>
</td>
</tr>
<?php
}
/**
* Render a table with integrations
*
* @param $integrations
* @ignore
*/
function _mc4wp_integrations_table($integrations)
{
?>
<table class="mc4wp-table widefat striped">
<thead>
<tr>
<th><?php echo esc_html__('Name', 'mailchimp-for-wp'); ?></th>
<th><?php echo esc_html__('Description', 'mailchimp-for-wp'); ?></th>
<th><?php echo esc_html__('Status', 'mailchimp-for-wp'); ?></th>
</tr>
</thead>
<tbody>
<?php
// active & enabled integrations first
foreach ($integrations as $integration) {
if ($integration->is_installed() && $integration->enabled) {
_mc4wp_integrations_table_row($integration);
}
}
// active & disabled integrations next
foreach ($integrations as $integration) {
if ($integration->is_installed() && ! $integration->enabled) {
_mc4wp_integrations_table_row($integration);
}
}
// rest
foreach ($integrations as $integration) {
if (! $integration->is_installed()) {
_mc4wp_integrations_table_row($integration);
}
}
?>
</tbody>
</table>
<?php
}
?>
<div id="mc4wp-admin" class="wrap mc4wp-settings">
<p class="mc4wp-breadcrumbs">
<span class="prefix"><?php echo esc_html__('You are here: ', 'mailchimp-for-wp'); ?></span>
<a href="<?php echo admin_url('admin.php?page=mailchimp-for-wp'); ?>">Mailchimp for WordPress</a> &rsaquo;
<span class="current-crumb"><strong><?php echo esc_html__('Integrations', 'mailchimp-for-wp'); ?></strong></span>
</p>
<div class="mc4wp-row">
<!-- Main Content -->
<div class="mc4wp-col mc4wp-col-4">
<h1 class="mc4wp-page-title">Mailchimp for WordPress: <?php echo esc_html__('Integrations', 'mailchimp-for-wp'); ?></h1>
<h2 style="display: none;"></h2>
<?php settings_errors(); ?>
<p>
<?php echo esc_html__('The table below shows all available integrations.', 'mailchimp-for-wp'); ?>
<?php echo esc_html__('Click on the name of an integration to edit all settings specific to that integration.', 'mailchimp-for-wp'); ?>
</p>
<form action="<?php echo admin_url('options.php'); ?>" method="post">
<?php settings_fields('mc4wp_integrations_settings'); ?>
<h3><?php echo esc_html__('Integrations', 'mailchimp-for-wp'); ?></h3>
<?php _mc4wp_integrations_table($integrations); ?>
<p><?php echo esc_html__('Greyed out integrations will become available after installing & activating the corresponding plugin.', 'mailchimp-for-wp'); ?></p>
</form>
</div>
<!-- Sidebar -->
<div class="mc4wp-sidebar mc4wp-col">
<?php require MC4WP_PLUGIN_DIR . '/includes/views/parts/admin-sidebar.php'; ?>
</div>
</div>
</div>