Changed source root directory

This commit is contained in:
2026-03-05 16:30:11 +01:00
parent dc85447ee1
commit 538f85d7a2
5868 changed files with 749734 additions and 99 deletions

View File

@@ -0,0 +1,88 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_AffiliateWP_Integration
*
* @ignore
*/
class MC4WP_AffiliateWP_Integration extends MC4WP_User_Integration
{
/**
* @var string
*/
public $name = 'AffiliateWP';
/**
* @var string
*/
public $description = 'Subscribes people from your AffiliateWP registration form.';
/**
* @var bool
*/
public $shown = false;
/**
* Add hooks
*/
public function add_hooks()
{
if (! $this->options['implicit']) {
add_action('affwp_register_fields_before_tos', [ $this, 'maybe_output_checkbox' ], 20);
}
add_action('affwp_register_user', [ $this, 'subscribe_from_registration' ], 90, 1);
}
/**
* Output checkbox, once.
*/
public function maybe_output_checkbox()
{
if (! $this->shown) {
$this->output_checkbox();
$this->shown = true;
}
}
/**
* Subscribes from WP Registration Form
*
* @param int $affiliate_id
*
* @return bool|string
*/
public function subscribe_from_registration($affiliate_id)
{
// was sign-up checkbox checked?
if (! $this->triggered()) {
return false;
}
// gather emailadress from user who WordPress registered
$user_id = affwp_get_affiliate_user_id($affiliate_id);
$user = get_userdata($user_id);
// was a user found with the given ID?
if (! $user instanceof WP_User) {
return false;
}
$data = $this->user_merge_vars($user);
return $this->subscribe($data, $user_id);
}
/* End registration form functions */
/**
* @return bool
*/
public function is_installed()
{
return class_exists('Affiliate_WP');
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* Try to include a file before each integration's settings page
*
* @param MC4WP_Integration $integration
* @param array $opts
* @ignore
*/
function mc4wp_admin_before_integration_settings(MC4WP_Integration $integration, $opts)
{
$file = __DIR__ . '/' . $integration->slug . '/admin-before.php';
if (file_exists($file)) {
include $file;
}
}
/**
* Try to include a file before each integration's settings page
*
* @param MC4WP_Integration $integration
* @param array $opts
* @ignore
*/
function mc4wp_admin_after_integration_settings(MC4WP_Integration $integration, $opts)
{
$file = __DIR__ . '/' . $integration->slug . '/admin-after.php';
if (file_exists($file)) {
include $file;
}
}
add_action('mc4wp_admin_before_integration_settings', 'mc4wp_admin_before_integration_settings', 30, 2);
add_action('mc4wp_admin_after_integration_settings', 'mc4wp_admin_after_integration_settings', 30, 2);
// Register core integrations
mc4wp_register_integration('wp-comment-form', 'MC4WP_Comment_Form_Integration');
mc4wp_register_integration('wp-registration-form', 'MC4WP_Registration_Form_Integration');
mc4wp_register_integration('buddypress', 'MC4WP_BuddyPress_Integration');
mc4wp_register_integration('easy-digital-downloads', 'MC4WP_Easy_Digital_Downloads_Integration');
mc4wp_register_integration('contact-form-7', 'MC4WP_Contact_Form_7_Integration', true);
mc4wp_register_integration('events-manager', 'MC4WP_Events_Manager_Integration');
mc4wp_register_integration('memberpress', 'MC4WP_MemberPress_Integration');
mc4wp_register_integration('affiliatewp', 'MC4WP_AffiliateWP_Integration');
mc4wp_register_integration('give', 'MC4WP_Give_Integration');
mc4wp_register_integration('custom', 'MC4WP_Custom_Integration', true);
mc4wp_register_integration('woocommerce', 'MC4WP_WooCommerce_Integration');
require __DIR__ . '/prosopo-procaptcha/bootstrap.php';
require __DIR__ . '/wpforms/bootstrap.php';
require __DIR__ . '/gravity-forms/bootstrap.php';
require __DIR__ . '/ninja-forms/bootstrap.php';

View File

@@ -0,0 +1,183 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_BuddyPress_Integration
*
* @ignore
*/
class MC4WP_BuddyPress_Integration extends MC4WP_User_Integration
{
/**
* @var string
*/
public $name = 'BuddyPress';
/**
* @var string
*/
public $description = 'Subscribes users from BuddyPress registration forms.';
/**
* Add hooks
*/
public function add_hooks()
{
if (! $this->options['implicit']) {
add_action('bp_before_registration_submit_buttons', [ $this, 'output_checkbox' ], 20);
}
if (is_multisite()) {
/**
* Multisite signups are a two-stage process - the data is first added to
* the 'signups' table and then converted into an actual user during the
* activation process.
*
* To avoid all signups being subscribed to the Mailchimp list until they
* have responded to the activation email, a value is stored in the signup
* usermeta data which is retrieved on activation and acted upon.
*/
add_filter('bp_signup_usermeta', [ $this, 'store_usermeta' ], 10, 1);
add_action('bp_core_activated_user', [ $this, 'subscribe_from_usermeta' ], 10, 3);
} else {
add_action('bp_core_signup_user', [ $this, 'subscribe_from_form' ], 10, 4);
}
/**
* There is one further issue to consider, which is that many BuddyPress
* installs have a user moderation plugin (e.g. BP Registration Options)
* installed. This is because email activation on itself is sometimes not enough to ensure
* that user signups are not spammers. There should therefore be a way for
* plugins to delay the Mailchimp signup process.
*
* Plugins can hook into the 'mc4wp_integration_buddypress_should_subscribe' filter to prevent
* subscriptions from taking place:
*
* add_filter( 'mc4wp_integration_buddypress_should_subscribe', '__return_false' );
*
* The plugin would then then call:
*
* do_action( 'mc4wp_integration_buddypress_subscribe_user', $user_id );
*
* to perform the subscription at a later point.
*/
add_action('mc4wp_integration_buddypress_subscribe_user', [ $this, 'subscribe_buddypress_user' ], 10, 1);
}
/**
* Subscribes from BuddyPress Registration Form.
*
* @param int $user_id
* @param string $user_login
* @param string $user_password
* @param string $user_email
* @return bool
*/
public function subscribe_from_form($user_id, $user_login, $user_password, $user_email)
{
if (! $this->triggered()) {
return false;
}
$subscribe = true;
/**
* Allow other plugins to prevent the Mailchimp sign-up.
*
* @param bool $subscribe False does not subscribe the user.
* @param int $user_id The user ID to subscribe
*/
$subscribe = apply_filters('mc4wp_integration_buddypress_should_subscribe', $subscribe, $user_id);
if (! $subscribe) {
return false;
}
return $this->subscribe_buddypress_user($user_id);
}
/**
* Stores subscription data from BuddyPress Registration Form.
*
* @param array $usermeta The existing usermeta
* @return array $usermeta The modified usermeta
*/
public function store_usermeta($usermeta)
{
// only add meta if triggered (checked)
if ($this->triggered()) {
$usermeta['mc4wp_subscribe'] = '1';
}
return $usermeta;
}
/**
* Subscribes from BuddyPress Activation.
*
* @param int $user_id The activated user ID
* @param string $key the activation key (not used)
* @param array $userdata An array containing the activated user data
* @return bool
*/
public function subscribe_from_usermeta($user_id, $key, $userdata)
{
// sanity check
if (empty($user_id)) {
return false;
}
// bail if our usermeta key is not switched on
$meta = ( isset($userdata['meta']) ) ? $userdata['meta'] : [];
if (empty($meta['mc4wp_subscribe'])) {
return false;
}
$subscribe = true;
/**
* @ignore Documented elsewhere, see MC4WP_BuddyPress_Integration::subscribe_from_form.
*/
$subscribe = apply_filters('mc4wp_integration_buddypress_should_subscribe', $subscribe, $user_id);
if (! $subscribe) {
return false;
}
return $this->subscribe_buddypress_user($user_id);
}
/**
* Subscribes a user to Mailchimp list(s).
*
* @param int $user_id The user ID to subscribe
* @return bool
*/
public function subscribe_buddypress_user($user_id)
{
$user = get_userdata($user_id);
// was a user found with the given ID?
if (! $user instanceof WP_User) {
return false;
}
// gather email address and name from user
$data = $this->user_merge_vars($user);
return $this->subscribe($data, $user_id);
}
/* End BuddyPress functions */
/**
* @return bool
*/
public function is_installed()
{
return class_exists('BuddyPress');
}
}

View File

@@ -0,0 +1,3 @@
<p>
<?php printf(__('To integrate with Contact Form 7, configure the settings below and then add %s to your CF7 form mark-up.', 'mailchimp-for-wp'), '<input type="text" onfocus="this.select()" readonly value="' . esc_attr('[mc4wp_checkbox]') . '">'); ?>
</p>

View File

@@ -0,0 +1,176 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Contact_Form_7_Integration
*
* @ignore
*/
class MC4WP_Contact_Form_7_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'Contact Form 7';
/**
* @var string
*/
public $description = 'Subscribes people from Contact Form 7 forms.';
/**
* Add hooks
*/
public function add_hooks()
{
add_action('wpcf7_init', [ $this, 'init' ]);
add_action('wpcf7_mail_sent', [ $this, 'process' ], 1);
add_action('wpcf7_posted_data', [ $this, 'alter_cf7_data' ]);
}
/**
* Registers the CF7 shortcode
*
* @return boolean
*/
public function init()
{
if (function_exists('wpcf7_add_form_tag')) {
wpcf7_add_form_tag('mc4wp_checkbox', [ $this, 'shortcode' ]);
} else {
wpcf7_add_shortcode('mc4wp_checkbox', [ $this, 'shortcode' ]);
}
return true;
}
/**
* @{inheritdoc}
*
* Contact Form 7 listens to the following triggers.
*
* - _mc4wp_subscribe_contact-form-7
* - mc4wp-subscribe
*
* @return bool
*/
public function checkbox_was_checked()
{
$data = $this->get_data();
return ( isset($data[ $this->checkbox_name ]) && (int) $data[ $this->checkbox_name ] === 1 )
|| ( isset($data['mc4wp-subscribe']) && (int) $data['mc4wp-subscribe'] === 1 );
}
/**
* Alter Contact Form 7 data.
*
* Adds mc4wp_checkbox to post data so users can use `mc4wp_checkbox` in their email templates
*
* @param array $data
* @return array
*/
public function alter_cf7_data($data = [])
{
$data['mc4wp_checkbox'] = $this->checkbox_was_checked() ? __('Yes', 'mailchimp-for-wp') : __('No', 'mailchimp-for-wp');
return $data;
}
/**
* Subscribe from Contact Form 7 Forms
*
* @todo improve smart guessing based on selected Mailchimp lists
*
* @param WPCF7_ContactForm $cf7_form
* @return bool
*/
public function process($cf7_form)
{
// was sign-up checkbox checked?
if (! $this->checkbox_was_checked()) {
return false;
}
$parser = new MC4WP_Field_Guesser($this->get_data());
$data = $parser->combine([ 'guessed', 'namespaced' ]);
// do nothing if no email was found
if (empty($data['EMAIL'])) {
$this->get_log()->warning(sprintf('%s > Unable to find EMAIL field.', $this->name));
return false;
}
return $this->subscribe($data, $cf7_form->id());
}
/**
* Return the shortcode output
*
* @return string
*/
public function shortcode($args = [])
{
if (! empty($args['labels'][0])) {
$this->options['label'] = $args['labels'][0];
}
if (isset($args['options'])) {
// check for default:0 or default:1 to set the checked attribute
if (in_array('default:1', $args['options'], true)) {
$this->options['precheck'] = true;
} elseif (in_array('default:0', $args['options'], true)) {
$this->options['precheck'] = false;
}
}
// disable paragraph wrap because CF7 defaults to `wpautop`
$this->options['wrap_p'] = 0;
return $this->get_checkbox_html();
}
/**
* @return bool
*/
public function is_installed()
{
return function_exists('wpcf7_contact_form');
}
/**
* @since 3.0
* @return array
*/
public function get_ui_elements()
{
return array_diff(parent::get_ui_elements(), [ 'enabled', 'implicit' ]);
}
/**
* @param int $object_id
* @since 3.0
* @return string
*/
public function get_object_link($object_id)
{
// for backwards compatibility, not all CF7 sign-ups have an object id
if (empty($object_id)) {
return '';
}
// Return empty string if CF7 is no longer activated.
if (! function_exists('wpcf7_contact_form')) {
return '';
}
$form = wpcf7_contact_form($object_id);
if (! is_object($form)) {
return '';
}
return sprintf('<a href="%s">%s</a>', admin_url('admin.php?page=wpcf7&post=' . $object_id), $form->title());
}
}

View File

@@ -0,0 +1,15 @@
<p>
<?php _e('To get a custom integration to work, include the following HTML in the form you are trying to integrate with.', 'mailchimp-for-wp'); ?>
</p>
<?php ob_start(); ?>
<p>
<label>
<input type="checkbox" name="mc4wp-subscribe" value="1" />
<?php _e('Subscribe to our newsletter.', 'mailchimp-for-wp'); ?>
</label>
</p>
<?php $html = ob_get_clean(); ?>
<textarea class="widefat code-sample" rows="<?php echo substr_count($html, PHP_EOL); ?>" readonly onfocus="this.select()"><?php echo esc_textarea($html); ?></textarea>

View File

@@ -0,0 +1,133 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Custom_Integration
* @ignore
*/
class MC4WP_Custom_Integration extends MC4WP_Integration
{
/**
* @var string
*/
protected $checkbox_name = 'mc4wp-subscribe';
/**
* @var string
*/
public $name = 'Custom';
/**
* @var string
*/
public $description = 'Integrate with custom third-party forms.';
/**
* Add hooks
*/
public function add_hooks()
{
add_action('init', [ $this, 'listen' ], 50);
}
/**
* Was the integration checkbox checked?
*
* @return bool
*/
public function checkbox_was_checked()
{
$data = $this->get_data();
$value = isset($data[ $this->checkbox_name ]) ? $data[ $this->checkbox_name ] : '';
$truthy_values = [ 1, '1', 'yes', true, 'true', 'y' ];
return in_array($value, $truthy_values, true);
}
/**
* Maybe fire a general subscription request
*
* @return bool|string
*/
public function listen()
{
if (! $this->checkbox_was_checked()) {
return false;
}
// ignore requests from bots, crawlers and link previews
if (empty($_SERVER['HTTP_USER_AGENT']) || preg_match('/bot|crawl|spider|seo|lighthouse|facebookexternalhit|preview/i', $_SERVER['HTTP_USER_AGENT'])) {
return false;
}
// ignore requests without an HTTP referrer
if (empty($_SERVER['HTTP_REFERER'])) {
return false;
}
// ignore requests where HTTP Referer does not contain hostname from home_url
$site_hostname = parse_url(get_home_url(), PHP_URL_HOST);
if (strpos($_SERVER['HTTP_REFERER'], $site_hostname) === false) {
return false;
}
$data = $this->get_data();
// don't run for CF7 or Events Manager requests
// (since they use the same "mc4wp-subscribe" trigger)
$disable_triggers = [
'_wpcf7' => '',
'action' => 'booking_add',
];
foreach ($disable_triggers as $trigger => $trigger_value) {
if (isset($data[ $trigger ])) {
$value = $data[ $trigger ];
// do nothing if trigger value is optional
// or if trigger value matches
if (empty($trigger_value) || $value === $trigger_value) {
return false;
}
}
}
// run!
return $this->process();
}
/**
* Process custom form
*
* @return bool|string
*/
public function process()
{
$parser = new MC4WP_Field_Guesser($this->get_data());
$data = $parser->combine([ 'guessed', 'namespaced' ]);
// do nothing if no email was found
if (empty($data['EMAIL'])) {
$this->get_log()->warning(sprintf('%s > Unable to find EMAIL field.', $this->name));
return false;
}
return $this->subscribe($data);
}
/**
* @return bool
*/
public function is_installed()
{
return true;
}
/**
* @return array
*/
public function get_ui_elements()
{
return [ 'lists', 'double_optin', 'update_existing', 'replace_interests' ];
}
}

View File

@@ -0,0 +1,119 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Easy_Digital_Downloads_Integration
*
* @ignore
*/
class MC4WP_Easy_Digital_Downloads_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'Easy Digital Downloads';
/**
* @var string
*/
public $description = 'Subscribes your Easy Digital Downloads customers.';
/**
*
*/
public function add_hooks()
{
if (! $this->options['implicit']) {
// TODO: Allow more positions
add_action('edd_purchase_form_user_info_fields', [ $this, 'output_checkbox' ], 1);
add_action('edd_payment_meta', [ $this, 'save_checkbox_value' ]);
}
add_action('edd_complete_purchase', [ $this, 'subscribe_from_edd' ], 50);
}
/**
* @param array $meta
*
* @return array
*/
public function save_checkbox_value($meta)
{
// don't save anything if the checkbox was not checked
if (! $this->checkbox_was_checked()) {
return $meta;
}
$meta['_mc4wp_optin'] = 1;
return $meta;
}
/**
* {@inheritdoc}
*
* @param $object_id
*
* @return bool
*/
public function triggered($object_id = null)
{
if ($this->options['implicit']) {
return true;
}
if (! $object_id) {
return false;
}
$meta = edd_get_payment_meta($object_id);
if (is_array($meta) && isset($meta['_mc4wp_optin']) && $meta['_mc4wp_optin']) {
return true;
}
return false;
}
/**
* @param int $payment_id The ID of the payment
*
* @return bool|string
*/
public function subscribe_from_edd($payment_id)
{
if (! $this->triggered($payment_id)) {
return false;
}
$email = (string) edd_get_payment_user_email($payment_id);
$data = [
'EMAIL' => $email,
];
// add first and last name to merge vars, if given
$user_info = (array) edd_get_payment_meta_user_info($payment_id);
if (! empty($user_info['first_name']) && ! empty($user_info['last_name'])) {
$data['NAME'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
}
if (! empty($user_info['first_name'])) {
$data['FNAME'] = $user_info['first_name'];
}
if (! empty($user_info['last_name'])) {
$data['LNAME'] = $user_info['last_name'];
}
return $this->subscribe($data, $payment_id);
}
/**
* @return bool
*/
public function is_installed()
{
return class_exists('Easy_Digital_Downloads');
}
}

View File

@@ -0,0 +1,80 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Events_Manager_Integration
*
* @ignore
*/
class MC4WP_Events_Manager_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'Events Manager';
/**
* @var string
*/
public $description = 'Subscribes people from Events Manager booking forms.';
/**
* Add hooks
*/
public function add_hooks()
{
if (! $this->options['implicit']) {
add_action('em_booking_form_footer', [ $this, 'output_checkbox' ]);
}
add_action('em_bookings_added', [ $this, 'subscribe_from_events_manager' ], 5);
}
/**
* Subscribe from Events Manager booking forms.
*
* @param EM_Booking $args
* @return bool
*/
public function subscribe_from_events_manager($args)
{
// Is this integration triggered? (checkbox checked or implicit)
if (! $this->triggered()) {
return false;
}
$em_data = $this->get_data();
// logged-in users do not have these form fields, so grab from user object instead
if (empty($em_data['user_email']) && is_user_logged_in()) {
$user = wp_get_current_user();
$em_data['user_email'] = $user->user_email;
$em_data['user_name'] = sprintf('%s %s', $user->first_name, $user->last_name);
}
if (empty($em_data['user_email'])) {
return false;
}
$data = [
'EMAIL' => $em_data['user_email'],
'NAME' => $em_data['user_name'],
];
// subscribe using email and name
return $this->subscribe($data, $args->booking_id);
}
/**
* @return bool
*/
public function is_installed()
{
return defined('EM_VERSION');
}
}

View File

@@ -0,0 +1,49 @@
<?php
defined('ABSPATH') or exit;
/**
* @ignore
*/
class MC4WP_Give_Integration extends MC4WP_Integration
{
public $name = 'Give';
public $description = 'Subscribes people from your Give donation forms.';
public $shown = false;
public function add_hooks()
{
if (! $this->options['implicit']) {
add_action('give_purchase_form_register_login_fields', [ $this, 'output_checkbox' ], 50);
}
add_action('give_checkout_before_gateway', [ $this, 'subscribe_from_give' ], 90, 2);
}
public function subscribe_from_give($posted, $user)
{
// was sign-up checkbox checked?
if (true !== $this->triggered()) {
return;
}
$merge_fields = [
'EMAIL' => $user['email'],
];
if (! empty($user['first_name'])) {
$merge_fields['FNAME'] = $user['first_name'];
}
if (! empty($user['last_name'])) {
$merge_fields['LNAME'] = $user['last_name'];
}
return $this->subscribe($merge_fields);
}
public function is_installed()
{
return defined('GIVE_VERSION');
}
}

View File

@@ -0,0 +1,6 @@
<p>
<?php
/* translators: %s links to the Gravity Forms overview page */
echo sprintf(__('To integrate with Gravity Forms, add the "Mailchimp for WordPress" field to <a href="%s">one of your Gravity Forms forms</a>.', 'mailchimp-for-wp'), admin_url('admin.php?page=gf_edit_forms'));
?>
</p>

View File

@@ -0,0 +1,11 @@
<?php
defined('ABSPATH') or exit;
mc4wp_register_integration('gravity-forms', 'MC4WP_Gravity_Forms_Integration', true);
add_action('plugins_loaded', function () {
if (class_exists('GF_Fields')) {
GF_Fields::register(new MC4WP_Gravity_Forms_Field());
}
});

View File

@@ -0,0 +1,134 @@
<?php
class MC4WP_Gravity_Forms_Field extends GF_Field
{
public $type = 'mailchimp';
/**
* Returns the field markup; including field label, description, validation, and the form editor admin buttons.
*
* The {FIELD} placeholder will be replaced in GFFormDisplay::get_field_content with the markup returned by GF_Field::get_field_input().
*
* @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
* @param bool $force_frontend_label Should the frontend label be displayed in the admin even if an admin label is configured.
* @param array $form The Form Object currently being processed.
*
* @return string
*/
public function get_field_content($value, $force_frontend_label, $form)
{
$validation_message = ( $this->failed_validation && ! empty($this->validation_message) ) ? sprintf("<div class='gfield_description validation_message'>%s</div>", $this->validation_message) : '';
$is_form_editor = $this->is_form_editor();
$is_entry_detail = $this->is_entry_detail();
$is_admin = $is_form_editor || $is_entry_detail;
$admin_buttons = $this->get_admin_buttons();
$description = $this->get_description($this->description, 'gfield_description');
if ($this->is_description_above($form)) {
$clear = $is_admin ? "<div class='gf_clear'></div>" : '';
$field_content = sprintf("%s%s{FIELD}%s$clear", $admin_buttons, $description, $validation_message);
} else {
$field_content = sprintf('%s{FIELD}%s%s', $admin_buttons, $description, $validation_message);
}
return $field_content;
}
public function get_form_editor_field_title()
{
return esc_attr__('Mailchimp for WordPress', 'mailchimp-for-wp');
}
public function get_form_editor_field_settings()
{
return [
'label_setting',
'description_setting',
'css_class_setting',
'mailchimp_list_setting',
'mailchimp_double_optin',
'mailchimp_precheck',
'rules_setting',
];
}
public function get_field_input($form, $value = '', $entry = null)
{
$form_id = absint($form['id']);
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$id = $this->id;
$field_id = $is_entry_detail || $is_form_editor || 0 === (int) $form_id ? "input_$id" : 'input_' . $form_id . "_$id";
$disabled_text = $is_form_editor ? 'disabled="disabled"' : '';
return sprintf("<div class='ginput_container ginput_container_checkbox'><ul class='gfield_checkbox' id='%s'>%s</ul></div>", esc_attr($field_id), $this->get_checkbox_choices($value, $disabled_text, $form_id));
}
private function apply_mc4wp_options_filters($options)
{
$options = apply_filters('mc4wp_integration_gravity-forms_options', $options);
return $options;
}
public function get_checkbox_choices($value, $disabled_text, $form_id = 0)
{
$choices = '';
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$options = [
'label' => $this->get_field_label(false, $value),
'precheck' => isset($this->mailchimp_precheck) ? $this->mailchimp_precheck : false,
];
$options = $this->apply_mc4wp_options_filters($options);
// generate html
$choice = [
'text' => $options['label'],
'value' => '1',
'isSelected' => $options['precheck'],
];
$input_id = $this->id;
if ($is_entry_detail || $is_form_editor || 0 === (int) $form_id) {
$id = $this->id;
} else {
$id = $form_id . '_' . $this->id;
}
if (! isset($_GET['gf_token']) && empty($_POST) && rgar($choice, 'isSelected')) {
$checked = "checked='checked'";
} elseif (is_array($value) && RGFormsModel::choice_value_match($this, $choice, rgget($input_id, $value))) {
$checked = "checked='checked'";
} elseif (! is_array($value) && RGFormsModel::choice_value_match($this, $choice, $value)) {
$checked = "checked='checked'";
} else {
$checked = '';
}
$tabindex = $this->get_tabindex();
$choice_value = $choice['value'];
$choice_value = esc_attr($choice_value);
$choice_markup = "<li class='gchoice_{$id}'>
<input name='input_{$input_id}' type='checkbox' value='{$choice_value}' {$checked} id='choice_{$id}' {$tabindex} {$disabled_text} />
<label for='choice_{$id}' id='label_{$id}'>{$choice['text']}</label>
</li>";
$choices .= gf_apply_filters(
[
'gform_field_choice_markup_pre_render',
$this->formId,
$this->id,
],
$choice_markup,
$choice,
$this,
$value
);
return gf_apply_filters([ 'gform_field_choices', $this->formId, $this->id ], $choices, $this);
}
}

View File

@@ -0,0 +1,166 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Ninja_Forms_Integration
*
* @ignore
*/
class MC4WP_Gravity_Forms_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'Gravity Forms';
/**
* @var string
*/
public $description = 'Subscribe visitors from your Gravity Forms forms.';
/**
* Add hooks
*/
public function add_hooks()
{
add_action('gform_field_standard_settings', [ $this, 'settings_fields' ], 10, 2);
add_action('gform_editor_js', [ $this, 'editor_js' ]);
add_action('gform_after_submission', [ $this, 'after_submission' ], 10, 2);
}
public function after_submission($submission, $form)
{
$subscribe = false;
$email_address = '';
$mailchimp_list_id = '';
$double_optin = $this->options['double_optin'];
// find email field & checkbox value
foreach ($form['fields'] as $field) {
if ($field->type === 'email' && empty($email_address) && ! empty($submission[ $field->id ])) {
$email_address = $submission[ $field->id ];
}
if ($field->type === 'mailchimp' && ! empty($submission[ $field->id ])) {
$subscribe = true;
$mailchimp_list_id = $field->mailchimp_list;
if (isset($field->mailchimp_double_optin)) {
$double_optin = $field->mailchimp_double_optin;
}
}
}
if (! $subscribe || empty($email_address)) {
return;
}
// override integration settings with field options
$orig_options = $this->options;
$this->options['lists'] = [ $mailchimp_list_id ];
$this->options['double_optin'] = $double_optin;
// perform the sign-up
$this->subscribe([ 'EMAIL' => $email_address ], $submission['form_id']);
// revert back to original options in case request lives on
$this->options = $orig_options;
}
public function editor_js()
{
?>
<script type="text/javascript">
jQuery(document).on('gform_load_field_settings', function(evt, field) {
jQuery('#field_mailchimp_list').val(field.mailchimp_list || '');
jQuery('#field_mailchimp_double_optin').val(field.mailchimp_double_optin || "1");
jQuery('#field_mailchimp_precheck').val(field.mailchimp_precheck || "0");
});
</script>
<?php
}
public function settings_fields($pos, $form_id)
{
if ($pos !== 0) {
return;
}
$mailchimp = new MC4WP_MailChimp();
$lists = $mailchimp->get_lists();
?>
<li class="mailchimp_list_setting field_setting">
<label for="field_mailchimp_list" class="section_label">
<?php esc_html_e('Mailchimp list', 'mailchimp-for-wp'); ?>
</label>
<select id="field_mailchimp_list" onchange="SetFieldProperty('mailchimp_list', this.value)">
<option value="" disabled><?php _e('Select a Mailchimp list', 'mailchimp-for-wp'); ?></option>
<?php
foreach ($lists as $list) {
echo sprintf('<option value="%s">%s</option>', $list->id, $list->name);
}
?>
</select>
<p class="help">
<?php echo __('Select the list(s) to which people who check the checkbox should be subscribed.', 'mailchimp-for-wp'); ?>
</p>
</li>
<li class="mailchimp_double_optin field_setting">
<label for="field_mailchimp_double_optin" class="section_label">
<?php esc_html_e('Double opt-in?', 'mailchimp-for-wp'); ?>
</label>
<select id="field_mailchimp_double_optin" onchange="SetFieldProperty('mailchimp_double_optin', this.value)">
<option value="1"><?php echo __('Yes', 'mailchimp-for-wp'); ?></option>
<option value="0"><?php echo __('No', 'mailchimp-for-wp'); ?></option>
</select>
<p class="help">
<?php _e('Select "yes" if you want people to confirm their email address before being subscribed (recommended)', 'mailchimp-for-wp'); ?>
</p>
</li>
<li class="mailchimp_precheck field_setting">
<label for="field_mailchimp_precheck" class="section_label">
<?php esc_html_e('Pre-check the checkbox?', 'mailchimp-for-wp'); ?>
</label>
<select id="field_mailchimp_precheck" onchange="SetFieldProperty('mailchimp_precheck', this.value)">
<option value="1"><?php echo __('Yes', 'mailchimp-for-wp'); ?></option>
<option value="0"><?php echo __('No', 'mailchimp-for-wp'); ?></option>
</select>
<p class="help">
<?php
_e('Select "yes" if the checkbox should be pre-checked.', 'mailchimp-for-wp');
echo '<br />';
printf(__('<strong>Warning: </strong> enabling this may affect your <a href="%s">GDPR compliance</a>.', 'mailchimp-for-wp'), 'https://www.mc4wp.com/kb/gdpr-compliance/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page');
?>
</p>
</li>
<?php
}
/**
* @return bool
*/
public function is_installed()
{
return class_exists('GF_Field') && class_exists('GF_Fields');
}
/**
* @since 3.0
* @return array
*/
public function get_ui_elements()
{
return [];
}
/**
* @param int $form_id
* @return string
*/
public function get_object_link($form_id)
{
return '<a href="' . admin_url(sprintf('admin.php?page=gf_edit_forms&id=%d', $form_id)) . '">Gravity Forms</a>';
}
}

View File

@@ -0,0 +1,77 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_MemberPress_Integration
*
* @ignore
*/
class MC4WP_MemberPress_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'MemberPress';
/**
* @var string
*/
public $description = 'Subscribes people from MemberPress register forms.';
/**
* Add hooks
*/
public function add_hooks()
{
if (! $this->options['implicit']) {
if (has_action('mepr_checkout_before_submit')) {
add_action('mepr_checkout_before_submit', [ $this, 'output_checkbox' ]);
} else {
add_action('mepr-checkout-before-submit', [ $this, 'output_checkbox' ]);
}
}
if (has_action('mepr_signup')) {
add_action('mepr_signup', [ $this, 'subscribe_from_memberpress' ], 5);
} else {
add_action('mepr-signup', [ $this, 'subscribe_from_memberpress' ], 5);
}
}
/**
* Subscribe from MemberPress sign-up forms.
*
* @param MeprTransaction $txn
* @return bool
*/
public function subscribe_from_memberpress($txn)
{
// Is this integration triggered? (checkbox checked or implicit)
if (! $this->triggered()) {
return false;
}
$user = get_userdata($txn->user_id);
$data = [
'EMAIL' => $user->user_email,
'FNAME' => $user->first_name,
'LNAME' => $user->last_name,
];
// subscribe using email and name
return $this->subscribe($data, $txn->id);
}
/**
* @return bool
*/
public function is_installed()
{
return defined('MEPR_VERSION');
}
}

View File

@@ -0,0 +1,3 @@
<p>
<?php echo sprintf(__('To integrate with Ninja Forms, add the "Mailchimp" action to <a href="%s">one of your Ninja Forms forms</a>.', 'mailchimp-for-wp'), admin_url('admin.php?page=ninja-forms')); ?>
</p>

View File

@@ -0,0 +1,17 @@
<?php
mc4wp_register_integration('ninja-forms', 'MC4WP_Ninja_Forms_Integration', true);
add_filter('ninja_forms_register_fields', function ($fields) {
if (class_exists(NF_Abstracts_Input::class)) {
$fields['mc4wp_optin'] = new MC4WP_Ninja_Forms_Field();
}
return $fields;
});
add_filter('ninja_forms_register_actions', function ($actions) {
if (class_exists(NF_Abstracts_Action::class)) {
$actions['mc4wp_subscribe'] = new MC4WP_Ninja_Forms_Action();
}
return $actions;
});

View File

@@ -0,0 +1,205 @@
<?php
/**
* Class MC4WP_Ninja_Forms_Action
*/
class MC4WP_Ninja_Forms_Action extends NF_Abstracts_Action
{
protected $_name = 'mc4wp_subscribe';
protected $_nicename = 'Mailchimp';
protected $_tags = [ 'newsletter' ];
protected $_timing = 'normal';
protected $_priority = '10';
protected $_settings = [];
protected $_setting_labels = [
'list' => 'List',
'fields' => 'List Field Mapping',
];
public function __construct()
{
$this->_settings['double_optin'] = [
'name' => 'double_optin',
'type' => 'select',
'label' => 'Use double opt-in?',
'width' => 'full',
'group' => 'primary',
'value' => 1,
'options' => [
[
'value' => 1,
'label' => 'Yes',
],
[
'value' => 0,
'label' => 'No',
],
],
];
$this->_settings['update_existing'] = [
'name' => 'update_existing',
'type' => 'select',
'label' => 'Update existing subscribers?',
'width' => 'full',
'group' => 'primary',
'value' => 0,
'options' => [
[
'value' => 1,
'label' => 'Yes',
],
[
'value' => 0,
'label' => 'No',
],
],
];
add_action('wp_ajax_nf_' . $this->_name . '_get_lists', [$this, '_get_lists']);
add_action('init', [$this, 'translate_props']);
add_action('init', [$this, 'get_list_settings']);
}
public function translate_props()
{
$this->_settings['double_optin']['label'] = __('Use double opt-in?', 'mailchimp-for-wp');
$this->_settings['update_existing']['label'] = __('Update existing subscribers?', 'mailchimp-for-wp');
if (isset($this->_settings[ $this->get_name() . 'newsletter_list_fields' ])) {
$this->_settings[ $this->get_name() . 'newsletter_list_fields' ]['label'] = __('List Field Mapping', 'mailchimp-for-wp');
}
}
/*
* PUBLIC METHODS
*/
public function save($action_settings)
{
}
public function process($action_settings, $form_id, $data)
{
if (empty($action_settings['newsletter_list']) || empty($action_settings['EMAIL'])) {
return;
}
// find "mc4wp_optin" type field, bail if not checked.
foreach ($data['fields'] as $field_data) {
if ($field_data['type'] === 'mc4wp_optin' && empty($field_data['value'])) {
return;
}
}
$list_id = $action_settings['newsletter_list'];
$email_address = $action_settings['EMAIL'];
$mailchimp = new MC4WP_MailChimp();
$merge_fields = $mailchimp->get_list_merge_fields($list_id);
foreach ($merge_fields as $merge_field) {
if (! empty($action_settings[ $merge_field->tag ])) {
$merge_fields[ $merge_field->tag ] = $action_settings[ $merge_field->tag ];
}
}
$double_optin = (int) $action_settings['double_optin'] !== 0;
$update_existing = (int) $action_settings['update_existing'] === 1;
$replace_interests = isset($action_settings['replace_interests']) && (int) $action_settings['replace_interests'] === 1;
do_action('mc4wp_integration_ninja_forms_subscribe', $email_address, $merge_fields, $list_id, $double_optin, $update_existing, $replace_interests, $form_id);
}
public function ajax_get_lists_handler()
{
check_ajax_referer('ninja_forms_builder_nonce', 'security');
$lists = $this->get_lists();
array_unshift($return, [ 'value' => 0, 'label' => '-', 'fields' => [], 'groups' => [] ]);
echo wp_json_encode([ 'lists' => $return ]);
wp_die();
}
private function get_lists()
{
$mailchimp = new MC4WP_MailChimp();
/** @var array $lists */
$lists = $mailchimp->get_lists();
$return = [];
foreach ($lists as $list) {
$list_fields = [];
foreach ($mailchimp->get_list_merge_fields($list->id) as $merge_field) {
$list_fields[] = [
'value' => $merge_field->tag,
'label' => $merge_field->name,
];
}
// TODO: Add support for groups once base class supports this.
$return[] = [
'value' => $list->id,
'label' => $list->name,
'fields' => $list_fields,
];
}
return $return;
}
public function get_list_settings()
{
$label_defaults = [
'list' => 'List',
'fields' => 'List Field Mapping',
];
$labels = array_merge($label_defaults, $this->_setting_labels);
$prefix = $this->get_name();
$lists = $this->get_lists();
$this->_settings[ $prefix . 'newsletter_list' ] = [
'name' => 'newsletter_list',
'type' => 'select',
'label' => $labels[ 'list' ] . ' <a class="js-newsletter-list-update extra"><span class="dashicons dashicons-update"></span></a>',
'width' => 'full',
'group' => 'primary',
'value' => '0',
'options' => [],
];
if (empty($lists)) {
return;
}
$fields = [];
foreach ($lists as $list) {
$this->_settings[ $prefix . 'newsletter_list' ][ 'options' ][] = $list;
//Check to see if list has fields array set.
if (isset($list[ 'fields' ])) {
foreach ($list[ 'fields' ] as $field) {
$name = $list[ 'value' ] . '_' . $field[ 'value' ];
$fields[] = [
'name' => $name,
'type' => 'textbox',
'label' => $field[ 'label' ],
'width' => 'full',
'use_merge_tags' => [
'exclude' => [
'user', 'post', 'system', 'querystrings',
],
],
];
}
}
}
$this->_settings[ $prefix . 'newsletter_list_fields' ] = [
'name' => 'newsletter_list_fields',
'label' => 'List Field Mapping',
'type' => 'fieldset',
'group' => 'primary',
'settings' => [],
];
}
}

View File

@@ -0,0 +1,96 @@
<?php
if (! defined('ABSPATH')) {
exit;
}
/**
* Class MC4WP_Ninja_Forms_Field
*/
class MC4WP_Ninja_Forms_Field extends NF_Abstracts_Input
{
protected $_name = 'mc4wp_optin';
protected $_nicename = 'Mailchimp opt-in';
protected $_section = 'misc';
protected $_type = 'checkbox';
protected $_icon = 'check-square-o';
protected $_templates = 'checkbox';
protected $_test_value = 0;
protected $_settings = [ 'checkbox_default_value', 'checked_calc_value', 'unchecked_calc_value' ];
protected $_settings_exclude = [ 'default', 'placeholder', 'input_limit_set', 'checkbox_values' ];
/**
* NF_Fields_Checkbox constructor.
* @since 3.0
*/
public function __construct()
{
parent::__construct();
$this->_settings['label_pos']['value'] = 'right';
add_filter('ninja_forms_custom_columns', [ $this, 'custom_columns' ], 10, 2);
add_action('init', [$this, 'translate_nicename']);
}
public function translate_nicename()
{
$this->_nicename = __('Mailchimp opt-in', 'mailchimp-for-wp');
}
/**
* Admin Form Element
* Display the checkbox on the edit submissions area.
* @since 3.0
*
* @param $id Field ID.
* @param $value Field value.
* @return string HTML used for display of checkbox.
*/
public function admin_form_element($id, $value)
{
// If the checkboxes value is one...
if (1 === (int) $value) {
// ...this variable to checked.
$checked = 'checked';
} else {
// ...else leave the variable empty.
$checked = '';
}
// Return HTML to be output to the submission edit page.
return "<input type='hidden' name='fields[$id]' value='0' ><input type='checkbox' name='fields[$id]' value='1' id='' $checked>";
}
/**
* Custom Columns
* Creates what is displayed in the columns on the submissions page.
* @since 3.0
*
* @param string $value checkbox value
* @param MC4WP_Ninja_Forms_Field $field field model.
* @return $value string|void
*/
public function custom_columns($value, $field)
{
// If the field type is equal to checkbox...
if ('mc4wp_optin' === $field->get_setting('type')) {
// Backwards compatibility check for the new checked value setting.
if (null === $field->get_setting('checked_value') && 1 === (int) $value) {
return __('Checked', 'ninja-forms');
} elseif (null === $field->get_setting('unchecked_value') && 0 === (int) $value) {
return __('Unchecked', 'ninja-forms');
}
// If the field value is set to 1....
if (1 === (int) $value) {
// Set the value to the checked value setting.
$value = $field->get_setting('checked_value');
} else {
// Else set the value to the unchecked value setting.
$value = $field->get_setting('unchecked_value');
}
}
return $value;
}
}

View File

@@ -0,0 +1,74 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Ninja_Forms_Integration
*
* @ignore
*/
class MC4WP_Ninja_Forms_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'Ninja Forms';
/**
* @var string
*/
public $description = 'Subscribe visitors from your Ninja Forms forms.';
/**
* Add hooks
*/
public function add_hooks()
{
add_action('mc4wp_integration_ninja_forms_subscribe', [ $this, 'subscribe_from_ninja_forms' ], 10, 7);
}
public function subscribe_from_ninja_forms($email_address, $merge_fields, $list_id, $double_optin = true, $update_existing = false, $replace_interests = false, $form_id = 0)
{
// set options from parameters (coming from action)
$orig_options = $this->options;
$this->options['double_optin'] = $double_optin;
$this->options['update_existing'] = $update_existing;
$this->options['replace_interests'] = $replace_interests;
$this->options['lists'] = [ $list_id ];
$data = $merge_fields;
$data['EMAIL'] = $email_address;
$this->subscribe($data, $form_id);
// revert to original options
$this->options = $orig_options;
}
/**
* @return bool
*/
public function is_installed()
{
return class_exists('Ninja_Forms');
}
/**
* @since 3.0
* @return array
*/
public function get_ui_elements()
{
return [];
}
/**
* @param int $form_id
* @return string
*/
public function get_object_link($form_id)
{
return '<a href="' . admin_url(sprintf('admin.php?page=ninja-forms&form_id=%d', $form_id)) . '">Ninja Forms</a>';
}
}

View File

@@ -0,0 +1,148 @@
<?php
$opts = $opts ?? [];
$opts = true === is_array($opts) ?
$opts :
[];
$site_key = $opts['site_key'] ?? '';
$secret_key = $opts['secret_key'] ?? '';
$enabled = $opts['enabled'] ?? '0';
$display_for_authorized = $opts['display_for_authorized'] ?? '0';
$theme = $opts['theme'] ?? '';
$type = $opts['type'] ?? '';
$theme_options = [
'light' => esc_html__('Light', 'mailchimp-for-wp'),
'dark' => esc_html__('Dark', 'mailchimp-for-wp'),
];
$type_options = [
'frictionless' => esc_html__('Frictionless', 'mailchimp-for-wp'),
'pow' => esc_html__('Proof of Work', 'mailchimp-for-wp'),
'image' => esc_html__('Image Captcha', 'mailchimp-for-wp'),
];
?>
<?php
if ('1' === $enabled) {
?>
<p>
<?php echo esc_html__('Preview: if the credentials are valid, you should be able to complete the captcha below:', 'mailchimp-for-wp'); ?>
</p>
<?php
}
$procaptcha_api = MC4WP_Procaptcha::get_instance();
echo $procaptcha_api->print_captcha_element(true, true);
?>
<input class="prosopo-procaptcha__enabled-setting" type="hidden" name="mc4wp_integrations[prosopo-procaptcha][enabled]" value="<?php echo esc_attr($enabled); ?>">
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row"><?php echo esc_html__('Site Key', 'mailchimp-for-wp'); ?></th>
<td class="nowrap integration-toggles-wrap">
<label>
<input class="widefat prosopo-procaptcha__site-key" type="text" name="mc4wp_integrations[prosopo-procaptcha][site_key]"
placeholder="<?php echo esc_attr__('Enter your site key', 'mailchimp-for-wp'); ?>"
value="<?php echo esc_attr($site_key); ?>">
</label>
<p class="description">
<?php
echo
sprintf(
// translators: %1$s: opening anchor tag, %2$s: closing anchor tag
esc_html__('The API key for connecting with your Procaptcha account. %1$s Get your Site key here %2$s', 'mailchimp-for-wp'),
'<a href="https://portal.prosopo.io/" target="_blank">',
'</a>'
);
?>
</p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo esc_html__('Secret Key', 'mailchimp-for-wp'); ?></th>
<td class="nowrap integration-toggles-wrap">
<label>
<input class="widefat prosopo-procaptcha__secret-key" type="password" name="mc4wp_integrations[prosopo-procaptcha][secret_key]"
placeholder="<?php echo esc_attr__('Enter your secret key', 'mailchimp-for-wp'); ?>"
value="<?php echo esc_attr($secret_key); ?>">
</label>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo esc_html__('Theme', 'mailchimp-for-wp'); ?></th>
<td class="nowrap integration-toggles-wrap">
<label>
<select name="mc4wp_integrations[prosopo-procaptcha][theme]" style="width:250px;">
<?php
foreach ($theme_options as $value => $label) {
$selected = $theme === $value ? ' selected' : '';
printf('<option value="%s"%s>%s</option>', esc_attr($value), esc_attr($selected), esc_html($label));
}
?>
</select>
</label>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo esc_html__('Type', 'mailchimp-for-wp'); ?></th>
<td class="nowrap integration-toggles-wrap">
<label>
<select name="mc4wp_integrations[prosopo-procaptcha][type]" style="width:250px;">
<?php
foreach ($type_options as $value => $label) {
$selected = $type === $value ? ' selected' : '';
printf('<option value="%s"%s>%s</option>', esc_attr($value), esc_attr($selected), esc_html($label));
}
?>
</select>
</label>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo esc_html__('Display for authorized users', 'mailchimp-for-wp'); ?></th>
<td class="nowrap integration-toggles-wrap">
<label>
<input type="radio" name="mc4wp_integrations[prosopo-procaptcha][display_for_authorized]" value="1" <?php checked($display_for_authorized, '1'); ?> />&rlm;
<?php echo esc_html__('Yes', 'mailchimp-for-wp'); ?>
</label> &nbsp;
<label>
<input type="radio" name="mc4wp_integrations[prosopo-procaptcha][display_for_authorized]" value="0" <?php checked($display_for_authorized, '0'); ?> />&rlm;
<?php echo esc_html__('No', 'mailchimp-for-wp'); ?>
</label>
<p class="description"><?php echo esc_html__('Select "yes" to require the captcha even from authorized users.', 'mailchimp-for-wp'); ?></p>
</td>
</tr>
</tbody>
</table>
<prosopo-procaptcha-settings></prosopo-procaptcha-settings>
<script type="module">
class ProsopoProcaptchaSettings extends HTMLElement {
connectedCallback(){
"loading" === document.readyState ?
document.addEventListener("DOMContentLoaded", this.setup.bind(this)) :
this.setup()
}
updateEnabledSetting(event){
let form = event.target;
let enabledInput = form.querySelector('.prosopo-procaptcha__enabled-setting');
let siteKey= form.querySelector('.prosopo-procaptcha__site-key').value.trim();
let secretKey = form.querySelector('.prosopo-procaptcha__secret-key').value.trim();
enabledInput.value = '' !== siteKey &&
'' !== secretKey?
1:
0;
}
setup(){
this.closest('form').addEventListener('submit', this.updateEnabledSetting.bind(this))
}
}
customElements.define('prosopo-procaptcha-settings', ProsopoProcaptchaSettings);
</script>

View File

@@ -0,0 +1,11 @@
<?php
// translators: %1$s is opening anchor tag, %2$s is closing anchor tag
echo sprintf(
esc_html__(
'%1$s Procaptcha (by Prosopo) %2$s is offering seamless bot protection without compromising user data. You can customize settings and algorithms, ensuring optimal defense against all types of malicious bots.',
'mailchimp-for-wp'
),
'<a href="https://prosopo.io/" target="_blank">',
'</a>'
);

View File

@@ -0,0 +1,5 @@
<?php
mc4wp_register_integration('prosopo-procaptcha', 'MC4WP_Procaptcha_Integration');
MC4WP_Procaptcha::get_instance()->set_hooks();

View File

@@ -0,0 +1,63 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Ninja_Forms_Integration
*
* @ignore
*/
class MC4WP_Procaptcha_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'Procaptcha (by Prosopo)';
/**
* @var string
*/
public $description = 'Privacy-friendly and GDPR-compliant anti-bot protection.';
/**
* @return void
*/
protected function add_hooks()
{
}
/**
* @return bool
*/
public function is_installed()
{
return true;
}
/**
* @return array
*/
public function get_ui_elements()
{
return [
'procaptcha_site_key',
'procaptcha_secret_key',
];
}
/**
* @return array
*/
protected function get_default_options()
{
return [
'enabled' => '0',
'css' => '0',
'site_key' => '',
'secret_key' => '',
'theme' => 'light',
'type' => 'frictionless',
'display_for_authorized' => '0',
];
}
}

View File

@@ -0,0 +1,382 @@
<?php
/**
* Class MC4WP_Procaptcha
*
* @private
*/
class MC4WP_Procaptcha
{
private const SCRIPT_URL = 'https://js.prosopo.io/js/procaptcha.bundle.js';
private const FORM_FIELD_NAME = 'procaptcha-response';
private const API_URL = 'https://api.prosopo.io/siteverify';
/**
* @var MC4WP_Procaptcha
*/
private static $instance;
/**
* @var bool
*/
private $is_in_use;
/**
* @var bool
*/
private $is_enabled;
/**
* @var bool
*/
private $is_displayed_for_authorized;
/**
* @var string
*/
private $site_key;
/**
* @var string
*/
private $secret_key;
/**
* @var string
*/
private $theme;
/**
* @var string
*/
private $type;
private function __construct()
{
$this->is_in_use = false;
$this->is_enabled = false;
$this->is_displayed_for_authorized = false;
$this->site_key = '';
$this->secret_key = '';
$this->theme = '';
$this->type = '';
$this->read_settings();
}
/**
* @return void
*/
protected function read_settings()
{
$integrations = get_option('mc4wp_integrations', []);
if (
false === is_array($integrations) ||
false === key_exists('prosopo-procaptcha', $integrations) ||
false === is_array($integrations['prosopo-procaptcha'])
) {
return;
}
$settings = $integrations['prosopo-procaptcha'];
$this->is_enabled = true === key_exists('enabled', $settings) &&
'1' === $settings['enabled'];
$this->is_displayed_for_authorized = true === key_exists('display_for_authorized', $settings) &&
'1' === $settings['display_for_authorized'];
$this->site_key = true === key_exists('site_key', $settings) &&
true === is_string($settings['site_key']) ?
$settings['site_key'] :
'';
$this->secret_key = true === key_exists('secret_key', $settings) &&
true === is_string($settings['secret_key']) ?
$settings['secret_key'] :
'';
$this->theme = true === key_exists('theme', $settings) &&
true === is_string($settings['theme']) ?
$settings['theme'] :
'';
$this->type = true === key_exists('type', $settings) &&
true === is_string($settings['type']) ?
$settings['type'] :
'';
}
/**
* @return MC4WP_Procaptcha
*/
public static function get_instance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @return void
*/
protected function print_captcha_js()
{
$attributes = [
'siteKey' => $this->site_key,
'theme' => $this->theme,
'captchaType' => $this->type,
];
?>
<script data-name="prosopo-procaptcha-element" type="module">
let attributes = <?php echo json_encode($attributes); ?>;
class MC4WPProcaptcha extends HTMLElement {
constructor() {
super();
this.isValid = false;
this.validationErrorElement = null;
}
connectedCallback() {
// wait window.load to make sure 'window.procaptcha' is available.
"complete" !== document.readyState ?
window.addEventListener("load", this.setup.bind(this)) :
this.setup();
}
validatedCallback(output) {
this.isValid = true;
// the element is optional.
if (null !== this.validationErrorElement) {
this.validationErrorElement.style.visibility = 'hidden';
}
}
maybePreventSubmission(event) {
if (true === this.isValid ||
// the element is optional.
null === this.validationErrorElement) {
return;
}
event.preventDefault();
event.stopPropagation();
this.validationErrorElement.style.visibility = 'visible';
}
setup() {
this.validationErrorElement = this.querySelector('.mc4wp-procaptcha__validation-error')
attributes.callback = this.validatedCallback.bind(this);
window.procaptcha.render(this.querySelector('.mc4wp-procaptcha__captcha'), attributes);
this.closest('form').addEventListener('submit', this.maybePreventSubmission.bind(this));
}
}
customElements.define("mc4wp-procaptcha", MC4WPProcaptcha);
</script>
<?php
}
/**
* @return bool
*/
protected function is_human_made_request()
{
$token = $_POST[self::FORM_FIELD_NAME] ?? '';
$token = true === is_string($token) ?
$token :
'';
// bail early if the token is empty.
if ('' === $token) {
return false;
}
$response = wp_remote_post(
self::API_URL,
[
'method' => 'POST',
// limit waiting time to 20 seconds.
'timeout' => 20,
'headers' => [
'Content-Type' => 'application/json',
],
'body' => (string) wp_json_encode(
[
'secret' => $this->secret_key,
'token' => $token,
]
),
]
);
// Check if request failed, either locally or remotely
if (true === is_wp_error($response) || wp_remote_retrieve_response_code($response) >= 400) {
/** @var MC4WP_Debug_Log */
$logger = mc4wp('log');
$logger->error(sprintf('ProCaptcha request error: %d %s - %s', wp_remote_retrieve_response_code($response), wp_remote_retrieve_response_message($response), wp_remote_retrieve_body($response)));
return false;
}
$body = wp_remote_retrieve_body($response);
$body = json_decode($body, true);
$is_verified = is_array($body) && isset($body['verified']) && $body['verified'];
return true === $is_verified;
}
public function maybe_add_type_module_attribute(string $tag, string $handle, string $src): string
{
if (
'prosopo-procaptcha' !== $handle ||
// make sure we don't make it twice if other Procaptcha integrations are present.
false !== strpos('type="module"', $tag)
) {
return $tag;
}
// for old WP versions.
$tag = str_replace(' type="text/javascript"', '', $tag);
return str_replace(' src=', ' type="module" src=', $tag);
}
/**
* @return bool
*/
public function is_enabled()
{
return $this->is_enabled;
}
/**
* @param bool $is_without_validation_element
* @param bool $is_forced_render E.g. if it's a preview.
*
* @return string
*/
public function print_captcha_element($is_without_validation_element = false, $is_forced_render = false)
{
if (
false === $this->is_displayed_for_authorized &&
true === is_user_logged_in() &&
false === $is_forced_render
) {
return '';
}
$this->is_in_use = true;
$html = '<mc4wp-procaptcha class="mc4wp-procaptcha" style="display: block;">';
$html .= '<div class="mc4wp-procaptcha__captcha"></div>';
// The element is optional, e.g. should be missing on the settings page.
if (false === $is_without_validation_element) {
$html .= '<p class="mc4wp-procaptcha__validation-error" style="visibility: hidden;color:red;line-height:1;font-size: 12px;padding: 7px 0 10px 10px;margin:0;">';
$html .= esc_html__('Please verify that you are human.', 'mailchimp-for-wp');
$html .= '</p>';
}
$html .= '</mc4wp-procaptcha>';
return $html;
}
/**
* @return void
*/
public function maybe_enqueue_captcha_js()
{
if (false === $this->is_in_use) {
return;
}
// do not use wp_enqueue_module() because it doesn't work on the login screens.
wp_enqueue_script(
'prosopo-procaptcha',
self::SCRIPT_URL,
[],
null,
[
'in_footer' => true,
'strategy' => 'defer',
]
);
$this->print_captcha_js();
}
/**
* @param array<string,string> $messages
* @return array<string,string>
*/
public function register_error_message(array $messages)
{
$messages['procaptcha_required'] = 'Please verify that you are human.';
return $messages;
}
/**
* @param string[] $error_keys
* @param MC4WP_Form $form
*
* @return string[]
*/
public function validate_form($error_keys, $form)
{
if (
false === strpos($form->content, $this->get_field_stub()) ||
(false === $this->is_displayed_for_authorized && true === is_user_logged_in()) ||
true === $this->is_human_made_request()
) {
return $error_keys;
}
$error_keys[] = 'procaptcha_required';
return $error_keys;
}
/**
* @param string $html
* @return string
*/
public function inject_captcha_element($html)
{
$stub = $this->get_field_stub();
if (false === strpos($html, $stub)) {
return $html;
}
$captcha_element = $this->print_captcha_element();
return str_replace($stub, $captcha_element, $html);
}
/**
* @return string
*/
protected function get_field_stub()
{
return '<input type="hidden" name="procaptcha">';
}
public function set_hooks(): void
{
if (false === $this->is_enabled) {
return;
}
add_filter('mc4wp_form_messages', [$this, 'register_error_message']);
add_action('mc4wp_form_content', [$this, 'inject_captcha_element']);
add_filter('mc4wp_form_errors', [$this, 'validate_form'], 10, 2);
add_filter('script_loader_tag', [$this, 'maybe_add_type_module_attribute'], 10, 3);
$hook = true === is_admin() ?
'admin_print_footer_scripts' :
'wp_print_footer_scripts';
// priority must be less than 10, to make sure the wp_enqueue_script still has effect.
add_action($hook, [$this, 'maybe_enqueue_captcha_js'], 9);
}
}

View File

@@ -0,0 +1,52 @@
<?php
$position_options = [
'after_email_field' => __('After email field', 'mailchimp-for-wp'),
'checkout_billing' => __('After billing details', 'mailchimp-for-wp'),
'checkout_shipping' => __('After shipping details', 'mailchimp-for-wp'),
'checkout_after_customer_details' => __('After customer details', 'mailchimp-for-wp'),
'review_order_before_submit' => __('Before submit button', 'mailchimp-for-wp'),
'after_order_notes' => __('After order notes', 'mailchimp-for-wp'),
];
if (defined('CFW_NAME')) {
$position_options['cfw_checkout_before_payment_method_tab_nav'] = __('Checkout for WooCommerce: Before complete order button', 'mailchimp-for-wp');
$position_options['cfw_after_customer_info_account_details'] = __('Checkout for WooCommerce: After account info', 'mailchimp-for-wp');
$position_options['cfw_checkout_after_customer_info_address'] = __('Checkout for WooCommerce: After customer info', 'mailchimp-for-wp');
}
/** @var MC4WP_Integration $integration */
$body_config = [
'element' => 'mc4wp_integrations[' . $integration->slug . '][enabled]',
'value' => '1',
'hide' => false,
];
$config = [
'element' => 'mc4wp_integrations[' . $integration->slug . '][implicit]',
'value' => '0',
];
?>
<table class="form-table">
<tbody class="integration-toggled-settings" data-showif="<?php echo esc_attr(json_encode($body_config)); ?>">
<tr valign="top" data-showif="<?php echo esc_attr(json_encode($config)); ?>">
<th scope="row">
<?php _e('Position', 'mailchimp-for-wp'); ?>
</th>
<td>
<select name="mc4wp_integrations[<?php echo $integration->slug; ?>][position]">
<?php
foreach ($position_options as $value => $label) {
printf('<option value="%s" %s>%s</option>', esc_attr($value), selected($value, $opts['position'], false), esc_html($label));
}
?>
</select>
<p class="description"><?php esc_html_e('Select the location where you would like to show the sign-up checkbox. Note that only works if not using WooCommerce Checkout Block.', 'mailchimp-for-wp'); ?></p>
</td>
</tr>
</tbody>
</table>

View File

@@ -0,0 +1,254 @@
<?php
defined('ABSPATH') or exit;
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;
/**
* Class MC4WP_WooCommerce_Integration
*
* @ignore
*/
class MC4WP_WooCommerce_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'WooCommerce Checkout';
/**
* @var string
*/
public $description = "Subscribes people from WooCommerce's Checkout form or Checkout Block.";
/**
* @var string[]
*/
public $checkbox_classes = [
'input-checkbox',
];
public $wrapper_classes = [
'form-row',
'form-row-wide',
];
/**
* Add hooks
*/
public function add_hooks()
{
if (!$this->options['implicit']) {
if ($this->options['position'] !== 'after_email_field') {
// create hook name based on position setting
$hook = $this->options['position'];
// prefix hook with woocommerce_ if not already properly prefixed
// note: we check for cfw_ prefix here to not override the Checkout for WC hook names
if (strpos($hook, 'cfw_') !== 0 && strpos($hook, 'woocommerce_') !== 0) {
$hook = "woocommerce_{$hook}";
}
add_action($hook, [$this, 'output_checkbox'], 20);
} else {
add_filter('woocommerce_form_field_email', [$this, 'add_checkbox_after_email_field'], 10, 4);
}
add_action('woocommerce_checkout_update_order_meta', [$this, 'save_woocommerce_checkout_checkbox_value']);
// specific hooks for klarna
add_filter('kco_create_order', [$this, 'add_klarna_field']);
add_filter('klarna_after_kco_confirmation', [$this, 'subscribe_from_klarna_checkout'], 10, 2);
// hooks for when using WooCommerce Checkout Block
add_action('woocommerce_init', [$this, 'add_checkout_block_field']);
}
add_action('woocommerce_checkout_order_processed', [$this, 'subscribe_from_woocommerce_checkout']);
add_action('woocommerce_store_api_checkout_order_processed', [$this, 'subscribe_from_woocommerce_checkout']);
if ($this->options['precheck']) {
add_filter('woocommerce_get_default_value_for_mc4wp/optin', function ($value) {
return '1';
});
}
}
/**
* Add default value for "position" setting
*
* @return array
*/
protected function get_default_options()
{
$defaults = parent::get_default_options();
$defaults['position'] = 'billing';
return $defaults;
}
public function add_checkout_block_field()
{
// for compatibility with older WooCommerce versions
// check if function exists before calling
if (!function_exists('woocommerce_register_additional_checkout_field')) {
return;
}
woocommerce_register_additional_checkout_field(
[
'id' => 'mc4wp/optin',
'location' => 'order',
'type' => 'checkbox',
'label' => $this->get_label_text(),
'optionalLabel' => $this->get_label_text(),
]
);
}
public function add_klarna_field($create)
{
$create['options']['additional_checkbox']['text'] = $this->get_label_text();
$create['options']['additional_checkbox']['checked'] = (bool) $this->options['precheck'];
$create['options']['additional_checkbox']['required'] = false;
return $create;
}
public function add_checkbox_after_email_field($field, $key, $args, $value)
{
if ($key !== 'billing_email') {
return $field;
}
return $field . PHP_EOL . $this->get_checkbox_html();
}
/**
* @param int $order_id
*/
public function save_woocommerce_checkout_checkbox_value($order_id)
{
$order = wc_get_order($order_id);
if (!$order) {
return;
}
$order->update_meta_data('_mc4wp_optin', $this->checkbox_was_checked());
$order->save();
}
/**
* {@inheritdoc}
*
* @param int|\WC_Order $order_id
* @return bool|mixed
*/
public function triggered($order_id = null)
{
if ($this->options['implicit']) {
return true;
}
$order = wc_get_order($order_id);
if (!$order) {
return false;
}
// value from default checkout form (shortcode)
$a = $order->get_meta('_mc4wp_optin');
// alternatively, value from Checkout Block field
$b = false;
if (class_exists(Package::class) && class_exists(CheckoutFields::class)) {
$checkout_fields = Package::container()->get(CheckoutFields::class);
if (
$checkout_fields
&& method_exists($checkout_fields, 'get_field_from_object')
// method was private in earlier versions of WooCommerce, so check if callable
&& is_callable([$checkout_fields, 'get_field_from_object'])
) {
$b = $checkout_fields->get_field_from_object('mc4wp/optin', $order, 'contact');
}
}
return $a || $b;
}
public function subscribe_from_klarna_checkout($order_id, $klarna_order)
{
// $klarna_order is the returned object from Klarna
if (false === (bool) $klarna_order['merchant_requested']['additional_checkbox']) {
return;
}
$order = wc_get_order($order_id);
if (!$order) {
return;
}
// store _mc4wp_optin in order meta
$order->update_meta_data('_mc4wp_optin', true);
$order->save();
// continue in regular subscribe flow
$this->subscribe_from_woocommerce_checkout($order_id);
return;
}
/**
* @param int|\WC_Order $order_id
* @return boolean
*/
public function subscribe_from_woocommerce_checkout($order_id)
{
if (!$this->triggered($order_id)) {
return false;
}
$order = wc_get_order($order_id);
if (!$order) {
return false;
}
if (method_exists($order, 'get_billing_email')) {
$data = [
'EMAIL' => $order->get_billing_email(),
'NAME' => "{$order->get_billing_first_name()} {$order->get_billing_last_name()}",
'FNAME' => $order->get_billing_first_name(),
'LNAME' => $order->get_billing_last_name(),
];
} else {
// NOTE: for compatibility with WooCommerce < 3.0
$data = [
'EMAIL' => $order->billing_email,
'NAME' => "{$order->billing_first_name} {$order->billing_last_name}",
'FNAME' => $order->billing_first_name,
'LNAME' => $order->billing_last_name,
];
}
// TODO: add billing address fields, maybe by finding Mailchimp field of type "address"?
return $this->subscribe($data, $order_id);
}
/**
* @return bool
*/
public function is_installed()
{
return class_exists('WooCommerce');
}
/**
* {@inheritdoc}
*
* @return string
*/
public function get_object_link($object_id)
{
return sprintf('<a href="%s">%s</a>', get_edit_post_link($object_id), sprintf(__('Order #%d', 'mailchimp-for-wp'), $object_id));
}
}

View File

@@ -0,0 +1,121 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Comment_Form_Integration
*
* @ignore
*/
class MC4WP_Comment_Form_Integration extends MC4WP_Integration
{
/**
* @var bool
*/
protected $added_through_filter = false;
/**
* @var string
*/
public $name = 'Comment Form';
/**
* @var string
*/
public $description = 'Subscribes people from your WordPress comment form.';
/**
* Add hooks
*/
public function add_hooks()
{
if (! $this->options['implicit']) {
// hooks for outputting the checkbox
add_filter('comment_form_submit_field', [ $this, 'add_checkbox_before_submit_button' ], 90);
add_action('thesis_hook_after_comment_box', [ $this, 'maybe_output_checkbox' ], 90);
add_action('comment_form', [ $this, 'maybe_output_checkbox' ], 90);
}
// hooks for checking if we should subscribe the commenter
add_action('comment_post', [ $this, 'subscribe_from_comment' ], 40, 2);
}
/**
* This adds the checkbox just before the submit button and sets a flag to prevent it from outputting twice
*
* @param $submit_button_html
*
* @return string
*/
public function add_checkbox_before_submit_button($submit_button_html)
{
$this->added_through_filter = true;
return $this->get_checkbox_html() . $submit_button_html;
}
/**
* Output fallback
* Will output the checkbox if comment_form() function does not use `comment_form_submit_field` filter yet.
*/
public function maybe_output_checkbox()
{
if (! $this->added_through_filter) {
$this->output_checkbox();
}
}
/**
* Grabs data from WP Comment Form
*
* @param int $comment_id
* @param string $comment_approved
*
* @return bool|string
*/
public function subscribe_from_comment($comment_id, $comment_approved = '')
{
// was sign-up checkbox checked?
if (! $this->triggered()) {
return false;
}
// is this a spam comment?
if ($comment_approved === 'spam') {
return false;
}
$comment = get_comment($comment_id);
$data = [
'EMAIL' => $comment->comment_author_email,
'NAME' => $comment->comment_author,
'OPTIN_IP' => $comment->comment_author_IP,
];
return $this->subscribe($data, $comment_id);
}
/**
* @return bool
*/
public function is_installed()
{
return true;
}
/**
* {@inheritdoc }
*/
public function get_object_link($object_id)
{
$comment = get_comment($object_id);
if (! $comment) {
return '';
}
return sprintf('<a href="%s">Comment #%d</a>', get_edit_comment_link($object_id), $object_id);
}
}

View File

@@ -0,0 +1,96 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Registration_Form_Integration
*
* @ignore
*/
class MC4WP_Registration_Form_Integration extends MC4WP_User_Integration
{
/**
* @var string
*/
public $name = 'Registration Form';
/**
* @var string
*/
public $description = 'Subscribes people from your WordPress registration form.';
/**
* @var bool
*/
public $shown = false;
/**
* Add hooks
*/
public function add_hooks()
{
if (! $this->options['implicit']) {
add_action('login_head', [ $this, 'print_css_reset' ]);
add_action('um_after_register_fields', [ $this, 'maybe_output_checkbox' ], 20);
add_action('register_form', [ $this, 'maybe_output_checkbox' ], 20);
add_action('woocommerce_register_form', [ $this, 'maybe_output_checkbox' ], 20);
}
add_action('um_user_register', [ $this, 'subscribe_from_registration' ], 90, 1);
add_action('user_register', [ $this, 'subscribe_from_registration' ], 90, 1);
if (defined('um_plugin') && class_exists('UM')) {
$this->name = 'UltimateMember';
$this->description = 'Subscribes people from your UltimateMember registration form.';
}
}
/**
* Output checkbox, once.
*/
public function maybe_output_checkbox()
{
if (! $this->shown) {
$this->output_checkbox();
$this->shown = true;
}
}
/**
* Subscribes from WP Registration Form
*
* @param int $user_id
*
* @return bool|string
*/
public function subscribe_from_registration($user_id)
{
// was sign-up checkbox checked?
if (! $this->triggered()) {
return false;
}
// gather emailadress from user who WordPress registered
$user = get_userdata($user_id);
// was a user found with the given ID?
if (! $user instanceof WP_User) {
return false;
}
$data = $this->user_merge_vars($user);
return $this->subscribe($data, $user_id);
}
/* End registration form functions */
/**
* @return bool
*/
public function is_installed()
{
return true;
}
}

View File

@@ -0,0 +1,3 @@
<p>
<?php printf(__('Use this integration by adding the "Mailchimp" field to <a href="%s">your WPForms forms</a>.', 'mailchimp-for-wp'), admin_url('admin.php?page=wpforms-overview')); ?>
</p>

View File

@@ -0,0 +1,9 @@
<?php
mc4wp_register_integration('wpforms', 'MC4WP_WPForms_Integration', true);
add_action('plugins_loaded', function () {
if (class_exists('WPForms_Field')) {
new MC4WP_WPForms_Field();
}
});

View File

@@ -0,0 +1,261 @@
<?php
class MC4WP_WPForms_Field extends WPForms_Field
{
/**
* Primary class constructor.
*
* @since 1.0.0
*/
public function init()
{
$this->name = 'Mailchimp';
$this->type = 'mailchimp';
$this->icon = 'fa-envelope-o';
$this->order = 21;
$this->defaults = [
[
'label' => __('Sign-up to our newsletter?', 'mailchimp-for-wp'),
'value' => '1',
'default' => '',
],
];
}
/**
* Field options panel inside the builder.
*
* @since 1.0.0
* @param array $field
*/
public function field_options($field)
{
//--------------------------------------------------------------------//
// Basic field options
//--------------------------------------------------------------------//
// Options open markup
$this->field_option('basic-options', $field, [ 'markup' => 'open' ]);
// Mailchimp list
$this->field_option_mailchimp_list($field);
// Choices
$this->field_option_choices($field);
// Description
$this->field_option('description', $field);
// Required toggle
$this->field_option('required', $field);
// Options close markup
$this->field_option('basic-options', $field, [ 'markup' => 'close' ]);
//--------------------------------------------------------------------//
// Advanced field options
//--------------------------------------------------------------------//
// Options open markup
$this->field_option('advanced-options', $field, [ 'markup' => 'open' ]);
// Custom CSS classes
$this->field_option('css', $field);
// Options close markup
$this->field_option('advanced-options', $field, [ 'markup' => 'close' ]);
}
private function field_option_mailchimp_list($field)
{
$mailchimp = new MC4WP_MailChimp();
// Field option label
$tooltip = __('Select the Mailchimp list to subscribe to.', 'mailchimp-for-wp');
$option_label = $this->field_element(
'label',
$field,
[
'slug' => 'mailchimp-list',
'value' => __('Mailchimp list', 'mailchimp-for-wp'),
'tooltip' => $tooltip,
],
false
);
$option_select = sprintf('<select name="fields[%s][mailchimp_list]" data-field-id="%d" data-field-type="%s">', $field['id'], $field['id'], $this->type);
$lists = $mailchimp->get_lists();
foreach ($lists as $list) {
$option_select .= sprintf('<option value="%s" %s>%s</option>', $list->id, selected($list->id, $field['mailchimp_list'], false), $list->name);
}
$option_select .= '</select>';
// Field option row (markup) including label and input.
$output = $this->field_element(
'row',
$field,
[
'slug' => 'mailchimp-list',
'content' => $option_label . $option_select,
]
);
}
private function field_option_choices($field)
{
$tooltip = __('Set your sign-up label text and whether it should be pre-checked.', 'mailchimp-for-wp');
$values = ! empty($field['choices']) ? $field['choices'] : $this->defaults;
$class = ! empty($field['show_values']) && (int) $field['show_values'] === 1 ? 'show-values' : '';
$class .= ! empty($dynamic) ? ' wpforms-hidden' : '';
// Field option label
$option_label = $this->field_element(
'label',
$field,
[
'slug' => 'mailchimp-checkbox',
'value' => __('Sign-up checkbox', 'mailchimp-for-wp'),
'tooltip' => $tooltip,
],
false
);
// Field option choices inputs
$option_choices = sprintf('<ul class="choices-list %s" data-field-id="%d" data-field-type="%s">', $class, $field['id'], $this->type);
foreach ($values as $key => $value) {
$default = ! empty($value['default']) ? $value['default'] : '';
$option_choices .= sprintf('<li data-key="%d">', $key);
$option_choices .= sprintf('<input type="checkbox" name="fields[%s][choices][%s][default]" class="default" value="1" %s>', $field['id'], $key, checked('1', $default, false));
$option_choices .= sprintf('<input type="text" name="fields[%s][choices][%s][label]" value="%s" class="label">', $field['id'], $key, esc_attr($value['label']));
$option_choices .= sprintf('<input type="text" name="fields[%s][choices][%s][value]" value="%s" class="value">', $field['id'], $key, esc_attr($value['value']));
$option_choices .= '</li>';
}
$option_choices .= '</ul>';
// Field option row (markup) including label and input.
$output = $this->field_element(
'row',
$field,
[
'slug' => 'choices',
'content' => $option_label . $option_choices,
]
);
}
/**
* Field preview inside the builder.
*
* @since 1.0.0
* @param array $field
*/
public function field_preview($field)
{
$values = ! empty($field['choices']) ? $field['choices'] : $this->defaults;
// Field checkbox elements
echo '<ul class="primary-input">';
// Notify if currently empty
if (empty($values)) {
$values = [ 'label' => __('(empty)', 'wpforms') ];
}
// Individual checkbox options
foreach ($values as $key => $value) {
$default = isset($value['default']) ? $value['default'] : '';
$selected = checked('1', $default, false);
printf('<li><input type="checkbox" %s disabled>%s</li>', $selected, $value['label']);
}
echo '</ul>';
// Dynamic population is enabled and contains more than 20 items
if (isset($total) && $total > 20) {
echo '<div class="wpforms-alert-dynamic wpforms-alert wpforms-alert-warning">';
printf(__('Showing the first 20 choices.<br> All %d choices will be displayed when viewing the form.', 'wpforms'), absint($total));
echo '</div>';
}
// Description
$this->field_preview_option('description', $field);
}
/**
* Field display on the form front-end.
*
* @since 1.0.0
* @param array $field
* @param array $form_data
*/
public function field_display($field, $field_atts, $form_data)
{
// Setup some defaults because WPForms broke their integration in v1.8.1.1
$field_atts = array_merge([
'input_class' => [],
'input_id' => [],
], $field_atts);
// Setup and sanitize the necessary data
$field_required = ! empty($field['required']) ? ' required' : '';
$field_class = implode(' ', array_map('sanitize_html_class', (array) $field_atts['input_class']));
$field_id = implode(' ', array_map('sanitize_html_class', (array) $field_atts['input_id']));
$form_id = $form_data['id'];
$choices = (array) $field['choices'];
// List
printf('<ul id="%s" class="%s">', $field_id, $field_class);
foreach ($choices as $key => $choice) {
$selected = isset($choice['default']) ? '1' : '0';
$depth = isset($choice['depth']) ? absint($choice['depth']) : 1;
printf('<li class="choice-%d depth-%d">', $key, $depth);
// Checkbox elements
printf(
'<input type="checkbox" id="wpforms-%d-field_%d_%d" name="wpforms[fields][%d]" value="%s" %s %s>',
$form_id,
$field['id'],
$key,
$field['id'],
esc_attr($choice['value']),
checked('1', $selected, false),
$field_required
);
printf('<label class="wpforms-field-label-inline" for="wpforms-%d-field_%d_%d">%s</label>', $form_id, $field['id'], $key, wp_kses_post($choice['label']));
echo '</li>';
}
echo '</ul>';
}
/**
* Formats and sanitizes field.
*
* @since 1.0.2
* @param int $field_id
* @param array $field_submit
* @param array $form_data
*/
public function format($field_id, $field_submit, $form_data)
{
$field = $form_data['fields'][ $field_id ];
$choice = array_pop($field['choices']);
$name = sanitize_text_field($choice['label']);
$data = [
'name' => $name,
'value' => empty($field_submit) ? __('No', 'mailchimp-for-wp') : __('Yes', 'mailchimp-for-wp'),
'value_raw' => $field_submit,
'id' => absint($field_id),
'type' => $this->type,
];
wpforms()->process->fields[ $field_id ] = $data;
}
}

View File

@@ -0,0 +1,81 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_WPForms_Integration
*
* @ignore
*/
class MC4WP_WPForms_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'WPForms';
/**
* @var string
*/
public $description = 'Subscribe visitors from your WPForms forms.';
/**
* Add hooks
*/
public function add_hooks()
{
add_action('wpforms_process', [ $this, 'listen_to_wpforms' ], 20, 3);
}
/**
* @return bool
*/
public function is_installed()
{
return defined('WPFORMS_VERSION');
}
/**
* @since 3.0
* @return array
*/
public function get_ui_elements()
{
return [];
}
public function listen_to_wpforms($fields, $entry, $form_data)
{
foreach ($fields as $field_id => $field) {
if ($field['type'] === 'mailchimp' && (int) $field['value_raw'] === 1) {
return $this->subscribe_from_wpforms($field_id, $fields, $form_data);
}
}
}
public function subscribe_from_wpforms($checkbox_field_id, $fields, $form_data)
{
foreach ($fields as $field) {
if ($field['type'] === 'email') {
$email_address = $field['value'];
}
}
$mailchimp_list_id = $form_data['fields'][ $checkbox_field_id ]['mailchimp_list'];
$this->options['lists'] = [ $mailchimp_list_id ];
if (! empty($email_address)) {
return $this->subscribe([ 'EMAIL' => $email_address ], $form_data['id']);
}
}
/**
* @param int $form_id
* @return string
*/
public function get_object_link($form_id)
{
return '<a href="' . admin_url(sprintf('admin.php?page=wpforms-builder&view=fields&form_id=%d', $form_id)) . '">WPForms</a>';
}
}