first commit

This commit is contained in:
DESKTOP-GBA0BK8\Admin
2023-04-08 12:19:53 -04:00
commit 7c8c8b1c76
4586 changed files with 2050693 additions and 0 deletions
@@ -0,0 +1,89 @@
<?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', array( $this, 'maybe_output_checkbox' ), 20);
}
add_action('affwp_register_user', array( $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');
}
}
@@ -0,0 +1,58 @@
<?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 = dirname(__FILE__) . sprintf('/%s/admin-before.php', $integration->slug);
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 = dirname(__FILE__) . sprintf('/%s/admin-after.php', $integration->slug);
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('ninja-forms-2', 'MC4WP_Ninja_Forms_v2_Integration', true);
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('woocommerce', 'MC4WP_WooCommerce_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);
$dir = dirname(__FILE__);
require $dir . '/ninja-forms/bootstrap.php';
require $dir . '/wpforms/bootstrap.php';
require $dir . '/gravity-forms/bootstrap.php';
@@ -0,0 +1,184 @@
<?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', array( $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', array( $this, 'store_usermeta' ), 10, 1);
add_action('bp_core_activated_user', array( $this, 'subscribe_from_usermeta' ), 10, 3);
} else {
add_action('bp_core_signup_user', array( $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', array( $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'] : array();
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');
}
}
@@ -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>
@@ -0,0 +1,179 @@
<?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', array( $this, 'init'));
add_action('wpcf7_mail_sent', array( $this, 'process' ), 1);
add_action('wpcf7_posted_data', array( $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', array( $this, 'shortcode' ));
} else {
wpcf7_add_shortcode('mc4wp_checkbox', array( $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 ]) && $data[ $this->checkbox_name ] == 1)
|| (isset($data[ 'mc4wp-subscribe' ]) && $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 = array())
{
$data['mc4wp_checkbox'] = $this->checkbox_was_checked() ? __('Yes') : __('No');
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(array( '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 = array())
{
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'])) {
$this->options['precheck'] = true;
} elseif (in_array('default:0', $args['options'])) {
$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(), array( '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());
}
}
@@ -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>
@@ -0,0 +1,116 @@
<?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', array( $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 = array(1, "1", "yes", true, "true", "y");
return in_array($value, $truthy_values, true);
}
/**
* Maybe fire a general subscription request
*/
public function listen()
{
if (! $this->checkbox_was_checked()) {
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 = array(
'_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(array( '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 array( 'lists', 'double_optin', 'update_existing', 'replace_interests' );
}
}
@@ -0,0 +1,121 @@
<?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', array( $this, 'output_checkbox' ), 1);
add_action('edd_payment_meta', array( $this, 'save_checkbox_value' ));
}
add_action('edd_complete_purchase', array( $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 = array(
'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');
}
}
@@ -0,0 +1,81 @@
<?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', array( $this, 'output_checkbox' ));
}
add_action('em_bookings_added', array( $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 = array(
'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');
}
}
@@ -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_donation_form_top', array( $this, 'output_checkbox' ), 50);
}
add_action('give_checkout_before_gateway', array( $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 = array(
'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');
}
}
@@ -0,0 +1,3 @@
<p>
<?php 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>
@@ -0,0 +1,9 @@
<?php
defined('ABSPATH') or exit;
mc4wp_register_integration('gravity-forms', 'MC4WP_Gravity_Forms_Integration', true);
if (class_exists('GF_Fields')) {
GF_Fields::register(new MC4WP_Gravity_Forms_Field());
}
@@ -0,0 +1,128 @@
<?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 array(
'label_setting',
'description_setting',
'css_class_setting',
'mailchimp_list_setting',
'mailchimp_double_optin',
'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 || $form_id == 0 ? "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_gravity-forms_integration_options', $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 = array(
'label' => $this->get_field_label(false, $value),
'precheck' => false,
);
$options = $this->apply_mc4wp_options_filters($options);
// generate html
$choice = array(
'text' => $options['label'],
'value' => "1",
'isSelected' => $options['precheck'],
);
$input_id = $this->id;
if ($is_entry_detail || $is_form_editor || $form_id == 0) {
$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(array(
'gform_field_choice_markup_pre_render',
$this->formId,
$this->id
), $choice_markup, $choice, $this, $value);
return gf_apply_filters(array( 'gform_field_choices', $this->formId, $this->id ), $choices, $this);
}
}
@@ -0,0 +1,145 @@
<?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', array( $this, 'settings_fields' ), 10, 2);
add_action('gform_editor_js', array( $this, 'editor_js' ));
add_action('gform_after_submission', array( $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'] = array( $mailchimp_list_id );
$this->options['double_optin'] = $double_optin;
// perform the sign-up
$this->subscribe(array( '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">
/*
* When the field settings are initialized, populate
* the custom field setting.
*/
jQuery(document).on('gform_load_field_settings', function(ev, field) {
jQuery('#field_mailchimp_list').val(field.mailchimp_list || '');
jQuery('#field_mailchimp_double_optin').val(field.mailchimp_double_optin || "1");
});
</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>
</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'); ?></option>
<option value="0"><?php echo __('No'); ?></option>
</select>
</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 array();
}
/**
* @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>';
}
}
@@ -0,0 +1,71 @@
<?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']) {
add_action('mepr_checkout_before_submit', array( $this, 'output_checkbox' ));
}
add_action('mepr_signup', array( $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 = array(
'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');
}
}
@@ -0,0 +1,3 @@
<p>
<?php echo __('To integrate with Ninja Forms, add the "Mailchimp" field to your Ninja Forms forms.', 'mailchimp-for-wp'); ?>
</p>
@@ -0,0 +1,145 @@
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Ninja_Forms_v2_Integration
*
* @ignore
*/
class MC4WP_Ninja_Forms_v2_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = "Ninja Forms (v2)";
/**
* @var string
*/
public $description = "Subscribe visitors from your Ninja Forms forms.";
/**
* Add hooks
*/
public function add_hooks()
{
add_action('init', array( $this, 'register_field' ));
}
public function register_field()
{
$args = array(
'name' => __('Mailchimp', 'ninja-forms'),
'edit_function' => '',
'display_function' => 'ninja_forms_field_checkbox_display',
'group' => 'standard_fields',
'sidebar' => 'template_fields',
'edit_label' => true,
'edit_label_pos' => true,
'label_pos_options' => array(
array('name' => __('Left of Element', 'ninja-forms'), 'value' => 'left'),
array('name' => __('Above Element', 'ninja-forms'), 'value' => 'above'),
array('name' => __('Below Element', 'ninja-forms'), 'value' => 'below'),
array('name' => __('Right of Element', 'ninja-forms'), 'value' => 'right'),
),
'edit_placeholder' => false,
'edit_req' => true,
'edit_custom_class' => true,
'edit_help' => true,
'edit_desc' => true,
'edit_meta' => false,
'process' => array( $this, 'process' ),
'default_label' => $this->options['label'],
'edit_options' => array(
array(
'type' => 'select', //What type of input should this be?
'options' => array(
array(
'name' => __('Unchecked', 'ninja-forms'),
'value' => 'unchecked',
),
array(
'name' => __('Checked', 'ninja-forms'),
'value' => 'checked',
),
),
'name' => 'default_value', //What should it be named. This should always be a programmatic name, not a label.
'label' => __('Default Value', 'ninja-forms'),
'class' => 'widefat', //Additional classes to be added to the input element.
),
),
);
ninja_forms_register_field('mc4wp-subscribe', $args);
}
/**
* Process form submissions
*
* @param int $id
* @param string $value
*
* @return bool|string
*/
public function process($id, $value)
{
// field was not checked
if ($value !== 'checked') {
return false;
}
/**
* @var Ninja_Forms_Processing $ninja_forms_processing
*/
global $ninja_forms_processing;
// generate an array of field label => field value
$fields = $ninja_forms_processing->get_all_submitted_fields();
$pretty = array();
foreach ($fields as $field_id => $field_value) {
// try admin label for "mc4wp-" prefixed fields, otherwise use general label
$label = $ninja_forms_processing->get_field_setting($field_id, 'admin_label');
if (empty($label) || stripos($label, 'mc4wp-') !== 0) {
$label = $ninja_forms_processing->get_field_setting($field_id, 'label');
}
$pretty[ $label ] = $field_value;
}
// guess mailchimp variables
$parser = new MC4WP_Field_Guesser($pretty);
$data = $parser->combine(array( '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, $ninja_forms_processing->get_form_ID());
}
/**
* @return bool
*/
public function is_installed()
{
return function_exists('ninja_forms_register_field');
}
/**
* @since 3.0
* @return array
*/
public function get_ui_elements()
{
return array_diff(parent::get_ui_elements(), array( 'enabled', 'implicit', 'precheck', 'css', 'label' ));
}
}
@@ -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>
@@ -0,0 +1,15 @@
<?php
mc4wp_register_integration('ninja-forms', 'MC4WP_Ninja_Forms_Integration', true);
if (class_exists('Ninja_Forms') && method_exists('Ninja_Forms', 'instance')) {
$ninja_forms = Ninja_Forms::instance();
if (isset($ninja_forms->fields)) {
$ninja_forms->fields['mc4wp_optin'] = new MC4WP_Ninja_Forms_Field();
}
if (isset($ninja_forms->actions)) {
$ninja_forms->actions['mc4wp_subscribe'] = new MC4WP_Ninja_Forms_Action();
}
}
@@ -0,0 +1,153 @@
<?php if (! defined('ABSPATH')) {
exit;
}
/**
* Class MC4WP_Ninja_Forms_Action
*/
final class MC4WP_Ninja_Forms_Action extends NF_Abstracts_ActionNewsletter
{
/**
* @var string
*/
protected $_name = 'mc4wp_subscribe';
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->_nicename = __('Mailchimp', 'mailchimp-for-wp');
$prefix = $this->get_name();
unset($this->_settings[ $prefix . 'newsletter_list_groups' ]);
$this->_settings[ 'double_optin' ] = array(
'name' => 'double_optin',
'type' => 'select',
'label' => __('Use double opt-in?', 'mailchimp-for-wp'),
'width' => 'full',
'group' => 'primary',
'value' => 1,
'options' => array(
array(
'value' => 1,
'label' => 'Yes',
),
array(
'value' => 0,
'label' => 'No',
),
),
);
$this->_settings[ 'update_existing' ] = array(
'name' => 'update_existing',
'type' => 'select',
'label' => __('Update existing subscribers?', 'mailchimp-for-wp'),
'width' => 'full',
'group' => 'primary',
'value' => 0,
'options' => array(
array(
'value' => 1,
'label' => 'Yes',
),
array(
'value' => 0,
'label' => 'No',
),
),
);
// $this->_settings[ 'replace_interests' ] = array(
// 'name' => 'replace_interests',
// 'type' => 'select',
// 'label' => __( 'Replace existing interest groups?', 'mailchimp-for-wp'),
// 'width' => 'full',
// 'group' => 'primary',
// 'value' => 0,
// 'options' => array(
// array(
// 'value' => 1,
// 'label' => 'Yes',
// ),
// array(
// 'value' => 0,
// 'label' => 'No',
// ),
// ),
// );
}
/*
* 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 = $action_settings['double_optin'] != '0';
$update_existing = $action_settings['update_existing'] == '1';
$replace_interests = isset($action_settings['replace_interests']) && $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);
}
protected function get_lists()
{
$mailchimp = new MC4WP_MailChimp();
/** @var array $lists */
$lists = $mailchimp->get_lists();
$return = array();
foreach ($lists as $list) {
$list_fields = array();
foreach ($mailchimp->get_list_merge_fields($list->id) as $merge_field) {
$list_fields[] = array(
'value' => $merge_field->tag,
'label' => $merge_field->name,
);
}
// TODO: Add support for groups once base class supports this.
$return[] = array(
'value' => $list->id,
'label' => $list->name,
'fields' => $list_fields,
);
}
return $return;
}
}
@@ -0,0 +1,98 @@
<?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';
protected $_section = 'misc';
protected $_type = 'checkbox';
protected $_icon = 'check-square-o';
protected $_templates = 'checkbox';
protected $_test_value = 0;
protected $_settings = array( 'checkbox_default_value', 'checked_calc_value', 'unchecked_calc_value' );
protected $_settings_exclude = array( 'default', 'placeholder', 'input_limit_set', 'checkbox_values' );
/**
* NF_Fields_Checkbox constructor.
* @since 3.0
*/
public function __construct()
{
parent::__construct();
$this->_nicename = __('Mailchimp opt-in', 'mailchimp-for-wp');
$this->_settings[ 'label_pos' ][ 'value' ] = 'right';
add_filter('ninja_forms_custom_columns', array( $this, 'custom_columns' ), 10, 2);
}
/**
* 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 == $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 $value checkbox value
* @param $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 == $value) {
return __('Checked', 'ninja-forms');
} elseif (null == $field->get_setting('unchecked_value') && 0 == $value) {
return __('Unchecked', 'ninja-forms');
}
// If the field value is set to 1....
if (1 == $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;
}
}
@@ -0,0 +1,75 @@
<?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', array( $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'] = array( $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 array();
}
/**
* @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>';
}
}
@@ -0,0 +1,33 @@
<?php
$position_options = array(
'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' ),
);
/** @var MC4WP_Integration $integration */
?>
<table class="form-table">
<?php $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 _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>
</td>
</tr>
</table>
@@ -0,0 +1,155 @@
<?php
defined('ABSPATH') or exit;
/**
* 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.";
/**
* Add hooks
*/
public function add_hooks()
{
if (! $this->options['implicit']) {
// create hook name based on position setting
$hook = sprintf('woocommerce_%s', $this->options['position']);
add_action($hook, array( $this, 'output_checkbox' ), 20);
add_action('woocommerce_checkout_update_order_meta', array( $this, 'save_woocommerce_checkout_checkbox_value' ));
// specific hooks for klarna
add_filter('kco_create_order', array($this, 'add_klarna_field'));
add_filter('klarna_after_kco_confirmation', array($this, 'subscribe_from_klarna_checkout'), 10, 2);
}
add_action('woocommerce_checkout_order_processed', array( $this, 'subscribe_from_woocommerce_checkout' ));
}
/**
* 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_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;
}
/**
* @param int $order_id
*/
public function save_woocommerce_checkout_checkbox_value($order_id)
{
update_post_meta($order_id, '_mc4wp_optin', $this->checkbox_was_checked());
}
/**
* {@inheritdoc}
*
* @param $order_id
*
* @return bool|mixed
*/
public function triggered($order_id = null)
{
if ($this->options['implicit']) {
return true;
}
if (! $order_id) {
return false;
}
$do_optin = get_post_meta($order_id, '_mc4wp_optin', true);
return $do_optin;
}
public function subscribe_from_klarna_checkout($order_id, $klarna_order)
{
// $klarna_order is the returned object from Klarna
if (false == $klarna_order['merchant_requested']['additional_checkbox']) {
return;
}
// get back into regular subscribe flow
update_post_meta($order_id, '_mc4wp_optin', true);
$this->subscribe_from_woocommerce_checkout($order_id);
return;
}
/**
* @param int $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 (method_exists($order, 'get_billing_email')) {
$data = array(
'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 = array(
'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));
}
}
@@ -0,0 +1,122 @@
<?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', array( $this, 'add_checkbox_before_submit_button' ), 90);
add_action('thesis_hook_after_comment_box', array( $this, 'maybe_output_checkbox' ), 90);
add_action('comment_form', array( $this, 'maybe_output_checkbox' ), 90);
}
// hooks for checking if we should subscribe the commenter
add_action('comment_post', array( $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 = array(
'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);
}
}
@@ -0,0 +1,97 @@
<?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', array( $this, 'print_css_reset' ));
add_action('um_after_register_fields', array( $this, 'maybe_output_checkbox' ), 20);
add_action('register_form', array( $this, 'maybe_output_checkbox' ), 20);
add_action('woocommerce_register_form', array( $this, 'maybe_output_checkbox' ), 20);
}
add_action('um_user_register', array( $this, 'subscribe_from_registration' ), 90, 1);
add_action('user_register', array( $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;
}
}
@@ -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>
@@ -0,0 +1,14 @@
<?php
mc4wp_register_integration('wpforms', 'MC4WP_WPForms_Integration', true);
function _mc4wp_wpforms_register_field()
{
if (! class_exists('WPForms_Field')) {
return;
}
new MC4WP_WPForms_Field();
}
add_action('init', '_mc4wp_wpforms_register_field');
@@ -0,0 +1,275 @@
<?php
/**
* Checkbox field.
*
* @package WPForms
* @author WPForms
* @since 1.0.0
* @license GPL-2.0+
* @copyright Copyright (c) 2016, WPForms LLC
*/
class MC4WP_WPForms_Field extends WPForms_Field
{
/**
* @var MC4WP_MailChimp
*/
private $mailchimp;
/**
* Primary class constructor.
*
* @since 1.0.0
*/
public function init()
{
// Define field type information
$this->name = 'Mailchimp';
$this->type = 'mailchimp';
$this->icon = 'fa-envelope-o';
$this->order = 21;
$this->defaults = array(
0 => array(
'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, array('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, array('markup' => 'close'));
//--------------------------------------------------------------------//
// Advanced field options
//--------------------------------------------------------------------//
// Options open markup
$this->field_option('advanced-options', $field, array('markup' => 'open'));
// Custom CSS classes
$this->field_option('css', $field);
// Options close markup
$this->field_option('advanced-options', $field, array('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,
array(
'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,
array(
'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']) && $field['show_values'] == '1' ? 'show-values' : '';
$class .= !empty($dynamic) ? ' wpforms-hidden' : '';
// Field option label
$option_label = $this->field_element(
'label',
$field,
array(
'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,
array(
'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 = array('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 and sanitize the necessary data
$field_required = !empty($field['required']) ? ' required' : '';
$field_class = implode(' ', array_map('sanitize_html_class', $field_atts['input_class']));
$field_id = implode(' ', array_map('sanitize_html_class', $field_atts['input_id']));
$form_id = $form_data['id'];
$choices = $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 = array(
'name' => $name,
'value' => empty($field_submit) ? __('No') : __('Yes'),
'value_raw' => $field_submit,
'id' => absint($field_id),
'type' => $this->type,
);
wpforms()->process->fields[$field_id] = $data;
}
}
@@ -0,0 +1,82 @@
<?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', array( $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 array();
}
public function listen_to_wpforms($fields, $entry, $form_data)
{
foreach ($fields as $field_id => $field) {
if ($field['type'] === 'mailchimp' && $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'] = array( $mailchimp_list_id );
if (! empty($email_address)) {
return $this->subscribe(array( '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>';
}
}