class-action.php
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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;
}
}