first commit
This commit is contained in:
@@ -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,155 @@
|
||||
<?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 = (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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?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 === (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;
|
||||
}
|
||||
}
|
||||
@@ -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', 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>';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user