Skip to content

Commit

Permalink
clean-up ninja forms action so it does not depend on NF_Abstracts_Act…
Browse files Browse the repository at this point in the history
…ionNewsletter
  • Loading branch information
dannyvankooten committed Dec 18, 2024
1 parent dc6f7a5 commit 6bd7e4c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
100 changes: 88 additions & 12 deletions integrations/ninja-forms/class-action.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@
/**
* Class MC4WP_Ninja_Forms_Action
*/
class MC4WP_Ninja_Forms_Action extends NF_Abstracts_ActionNewsletter
class MC4WP_Ninja_Forms_Action extends NF_Abstracts_Action
{
/**
* @var string
*/
protected $_name = 'mc4wp_subscribe';
protected $_nicename = 'Mailchimp';
protected $_tags = array( 'newsletter' );
protected $_timing = 'normal';
protected $_priority = '10';
protected $_settings = array();
protected $_setting_labels = array(
'list' => 'List',
'fields' => 'List Field Mapping',
);

/**
* 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'),
'label' => 'Use double opt-in?',
'width' => 'full',
'group' => 'primary',
'value' => 1,
Expand All @@ -48,7 +50,7 @@ public function __construct()
$this->_settings['update_existing'] = array(
'name' => 'update_existing',
'type' => 'select',
'label' => __('Update existing subscribers?', 'mailchimp-for-wp'),
'label' => 'Update existing subscribers?',
'width' => 'full',
'group' => 'primary',
'value' => 0,
Expand Down Expand Up @@ -82,6 +84,17 @@ public function __construct()
// ),
// ),
// );

add_action( 'wp_ajax_nf_' . $this->_name . '_get_lists', array( $this, '_get_lists' ) );
add_action('init', array($this, 'translate_props'));

$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');
}

/*
Expand Down Expand Up @@ -123,7 +136,16 @@ public function process($action_settings, $form_id, $data)
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()
public function ajax_get_lists_handler()
{
check_ajax_referer( 'ninja_forms_builder_nonce', 'security' );
$lists = $this->get_lists();
array_unshift( $return, array( 'value' => 0, 'label' => '-', 'fields' => array(), 'groups' => array() ) );
echo wp_json_encode( array( 'lists' => $return ) );
wp_die();
}

private function get_lists()
{
$mailchimp = new MC4WP_MailChimp();

Expand All @@ -142,7 +164,6 @@ protected function get_lists()
}

// TODO: Add support for groups once base class supports this.

$return[] = array(
'value' => $list->id,
'label' => $list->name,
Expand All @@ -152,4 +173,59 @@ protected function get_lists()

return $return;
}

private function get_list_settings()
{
$label_defaults = array(
'list' => 'List',
'fields' => 'List Field Mapping',
);
$labels = array_merge( $label_defaults, $this->_setting_labels );
$prefix = $this->get_name();
$lists = $this->get_lists();

if( empty( $lists ) ) return;

$this->_settings[ $prefix . 'newsletter_list' ] = array(
'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' => array(),
);

$fields = array();
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[] = array(
'name' => $name,
'type' => 'textbox',
'label' => $field[ 'label' ],
'width' => 'full',
'use_merge_tags' => array(
'exclude' => array(
'user', 'post', 'system', 'querystrings'
)
)
);
}
}
}

$this->_settings[ $prefix . 'newsletter_list_fields' ] = array(
'name' => 'newsletter_list_fields',
'label' => esc_html__( 'List Field Mapping', 'ninja-forms' ),
'type' => 'fieldset',
'group' => 'primary',
'settings' => array()
);
}
}
10 changes: 7 additions & 3 deletions integrations/ninja-forms/class-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class MC4WP_Ninja_Forms_Field extends NF_Abstracts_Input
{
protected $_name = 'mc4wp_optin';
protected $_nicename = 'Mailchimp';
protected $_nicename = 'Mailchimp opt-in';
protected $_section = 'misc';
protected $_type = 'checkbox';
protected $_icon = 'check-square-o';
Expand All @@ -27,11 +27,15 @@ 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);
add_action('init', array($this, 'translate_nicename'));
}

public function translate_nicename()
{
$this->_nicename = __('Mailchimp opt-in', 'mailchimp-for-wp');
}

/**
Expand Down

0 comments on commit 6bd7e4c

Please sign in to comment.