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,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>';
}
}