Skip to content

Commit

Permalink
bootstrap integrations at plugins_loaded:8 since that is required for…
Browse files Browse the repository at this point in the history
… ninja forms now
  • Loading branch information
dannyvankooten committed Dec 18, 2024
1 parent e6987da commit dc6f7a5
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 170 deletions.
4 changes: 2 additions & 2 deletions integrations/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ function mc4wp_admin_after_integration_settings(MC4WP_Integration $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__ . '/woocommerce/woocommerce.php';
require __DIR__ . '/prosopo-procaptcha/bootstrap.php';
require __DIR__ . '/ninja-forms/bootstrap.php';
require __DIR__ . '/wpforms/bootstrap.php';
require __DIR__ . '/gravity-forms/bootstrap.php';
require __DIR__ . '/ninja-forms/bootstrap.php';
8 changes: 5 additions & 3 deletions integrations/gravity-forms/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

mc4wp_register_integration('gravity-forms', 'MC4WP_Gravity_Forms_Integration', true);

if (class_exists('GF_Fields')) {
GF_Fields::register(new MC4WP_Gravity_Forms_Field());
}
add_action('plugins_loaded', function() {
if (class_exists('GF_Fields')) {
GF_Fields::register(new MC4WP_Gravity_Forms_Field());
}
});
2 changes: 1 addition & 1 deletion integrations/ninja-forms/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
return $fields;
});

add_action('ninja_forms_register_actions', function($actions) {
add_filter('ninja_forms_register_actions', function($actions) {
$actions['mc4wp_subscribe'] = new MC4WP_Ninja_Forms_Action();
return $actions;
});
290 changes: 145 additions & 145 deletions integrations/ninja-forms/class-action.php
Original file line number Diff line number Diff line change
@@ -1,155 +1,155 @@
<?php

if (! defined('ABSPATH')) {
exit;
exit;
}

/**
* Class MC4WP_Ninja_Forms_Action
*/
final class MC4WP_Ninja_Forms_Action extends NF_Abstracts_ActionNewsletter
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 = (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);
}

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;
}
/**
* @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 = (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);
}

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;
}
}
3 changes: 0 additions & 3 deletions integrations/woocommerce/woocommerce.php

This file was deleted.

15 changes: 5 additions & 10 deletions integrations/wpforms/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

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');
add_action('plugins_loaded', function() {
if (class_exists('WPForms_Field')) {
new MC4WP_WPForms_Field();
}
});
11 changes: 5 additions & 6 deletions mailchimp-for-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ function _mc4wp_load_plugin()
$integrations_admin->add_hooks();
}
}

// bootstrap integrations
require __DIR__ . '/integrations/bootstrap.php';
}

function _mc4wp_on_plugin_activation()
Expand All @@ -104,12 +107,8 @@ function _mc4wp_on_plugin_activation()
wp_schedule_event(strtotime($time_string), 'daily', 'mc4wp_refresh_mailchimp_lists');
}

// bootstrap custom integrations
function _mc4wp_bootstrap_integrations()
{
require MC4WP_PLUGIN_DIR . '/integrations/bootstrap.php';
}

// bootstrap main plugin
add_action('plugins_loaded', '_mc4wp_load_plugin', 8);
add_action('plugins_loaded', '_mc4wp_bootstrap_integrations', 90);

register_activation_hook(__FILE__, '_mc4wp_on_plugin_activation');

0 comments on commit dc6f7a5

Please sign in to comment.