-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-gravitywp-connector-mailpoet-gravityforms.php
306 lines (268 loc) · 7.83 KB
/
class-gravitywp-connector-mailpoet-gravityforms.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* GravityWP_Connector_MailPoet_GravityForms
*
* @package GravityWP\Connector_MailPoet_GravityForms
*/
namespace GravityWP\Connector_MailPoet_GravityForms;
defined( 'ABSPATH' ) || die();
use GFFeedAddOn;
use GFForms;
GFForms::include_feed_addon_framework();
/**
* Class GravityWP_Connector_MailPoet_GravityForms
*
* @author GravityWP
* @since v1.0.0
* @see GFAddOn
*
* @global
*/
class GravityWP_Connector_MailPoet_GravityForms extends GFFeedAddOn {
/**
* Version of the plugin.
*
* @var string $_version
*/
protected $_version = GRAVITYWP_MP_GF_VERSION;
/**
* Minimum Gravity Forms version required.
*
* @var string $_min_gravityforms_version
*/
protected $_min_gravityforms_version = '2.4';
/**
* Plugin slug.
*
* @var string $_slug
*/
protected $_slug = 'connector-for-gravityforms-mailpoet';
/**
* Plugin path.
*
* @var string $_path
*/
protected $_path = 'connector-for-gravityforms-mailpoet/connector-for-gravityforms-mailpoet.php';
/**
* Full plugin file path.
*
* @var string $_full_path
*/
protected $_full_path = __FILE__;
/**
* Plugin title.
*
* @var string $_title
*/
protected $_title = 'Connector for MailPoet and Gravity Forms';
/**
* Short plugin title.
*
* @var string $_short_title
*/
protected $_short_title = 'MailPoet Connector';
/**
* Instance of this class.
*
* @var GravityWP_Connector_MailPoet_GravityForms $_instance
*/
private static $_instance = null;
/**
* Get an instance of this class.
*
* @return GravityWP_Connector_MailPoet_GravityForms
*/
public static function get_instance() {
if ( self::$_instance === null ) {
self::$_instance = new GravityWP_Connector_MailPoet_GravityForms();
}
return self::$_instance;
}
/**
* Configures which columns should be displayed on the feed list page.
*
* @return array<mixed>
*/
public function feed_list_columns() {
return array(
'feedname' => esc_html__( 'Name', 'connector-for-gravityforms-mailpoet' ),
'mailpoet_list' => esc_html__( 'MailPoet Lists', 'connector-for-gravityforms-mailpoet' ),
);
}
/**
* Feed settings fields.
*
* @return array<mixed>
*/
public function feed_settings_fields() {
return array(
array(
'title' => esc_html__( 'MailPoet Feed Settings', 'connector-for-gravityforms-mailpoet' ),
'fields' => array(
array(
'label' => esc_html__( 'Feed name', 'connector-for-gravityforms-mailpoet' ),
'type' => 'text',
'name' => 'feedname',
'class' => '',
'required' => true,
),
array(
'name' => 'mappedfields',
'label' => esc_html__( 'Map Fields', 'connector-for-gravityforms-mailpoet' ),
'type' => 'field_map',
'tooltip' => esc_html__( 'Map the Gravity Form fields to the MailPoet subscriber fields', 'connector-for-gravityforms-mailpoet' ),
'field_map' => array(
array(
'name' => 'first_name',
'label' => esc_html__( 'First Name', 'connector-for-gravityforms-mailpoet' ),
),
array(
'name' => 'last_name',
'label' => esc_html__( 'Last Name', 'connector-for-gravityforms-mailpoet' ),
),
array(
'name' => 'email',
'label' => esc_html__( 'Email', 'connector-for-gravityforms-mailpoet' ),
'field_type' => array( 'email', 'hidden' ),
),
),
),
array(
'label' => esc_html__( 'Existing subscribers', 'connector-for-gravityforms-mailpoet' ),
'type' => 'checkbox',
'name' => 'existing_subscriber',
'choices' => array(
array(
'label' => esc_html__( 'Update the name of the existing subscriber', 'connector-for-gravityforms-mailpoet' ),
'name' => 'update_existing_subscriber',
),
),
),
$this->get_mplists_setting_array(),
array(
'name' => 'condition',
'label' => esc_html__( 'Condition', 'connector-for-gravityforms-mailpoet' ),
'type' => 'feed_condition',
'checkbox_label' => esc_html__( 'Enable Condition', 'connector-for-gravityforms-mailpoet' ),
'instructions' => esc_html__( 'Process this feed if', 'connector-for-gravityforms-mailpoet' ),
),
),
),
);
}
/**
* Returns the MailPoet lists as settings array.
*
* @since 1.0
*
* @return array<mixed>
*/
protected function get_mplists_setting_array() {
$mp_lists = $this->get_mailpoet_lists();
$choices = array();
foreach ( $mp_lists as $list ) {
$choices[] = array(
'label' => $list['label'],
'name' => 'mp_lists_' . $list['value'],
);
}
if ( empty( $choices ) ) {
return array(
'name' => 'no_lists',
'label' => esc_html__( 'MailPoet Lists', 'connector-for-gravityforms-mailpoet' ),
'type' => 'html',
'html' => esc_html__( "You don't have any lists set up.", 'connector-for-gravityforms-mailpoet' ),
);
}
return array(
'name' => 'mailpoet_lists',
'required' => true,
'label' => esc_html__( 'MailPoet Lists', 'connector-for-gravityforms-mailpoet' ),
'type' => 'checkbox',
'choices' => $choices,
);
}
/**
* Retrieve available MailPoet lists.
*
* @return array<mixed>
*/
private function get_mailpoet_lists() {
if ( ! class_exists( \MailPoet\API\API::class ) ) {
$this->log_error( __METHOD__ . '(): MailPoet API class does not exist.' );
return array();
}
$mailpoet_api = \MailPoet\API\API::MP( 'v1' );
$lists = array();
try {
$mailpoet_lists = $mailpoet_api->getLists();
foreach ( $mailpoet_lists as $list ) {
$lists[] = array(
'label' => esc_html( $list['name'] ),
'value' => esc_attr( $list['id'] ),
);
}
} catch ( \Exception $e ) {
$this->log_error( __METHOD__ . '(): MailPoet API Error: ' . $e->getMessage() );
}
return $lists;
}
/**
* Process feed on form submission.
*
* @param array<mixed> $feed The current feed.
* @param array<mixed> $entry The Entry object.
* @param array<mixed> $form The Form object.
*
* @return null|array<mixed> Return null or modified entry.
*/
public function process_feed( $feed, $entry, $form ) {
if ( ! class_exists( \MailPoet\API\API::class ) ) {
$this->log_error( __METHOD__ . '(): MailPoet API class does not exist.' );
return $entry;
}
$mp_lists = $this->get_mailpoet_lists();
// Get the selected MailPoet lists from the feed meta.
$selected_lists = array();
foreach ( $mp_lists as $list ) {
$selected_list = rgar( $feed['meta'], 'mp_lists_' . $list['value'] );
if ( $selected_list === '1' ) {
$selected_lists[] = $list['value'];
}
}
// Collect email and other fields from the entry..
$email = rgar( $entry, $feed['meta']['mappedfields_email'] );
$first_name = rgar( $entry, $feed['meta']['mappedfields_first_name'] );
$last_name = rgar( $entry, $feed['meta']['mappedfields_last_name'] );
$subscriber = array(
'email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
);
// Default options.
$options = array();
// Get MailPoet API instance.
$mailpoet_api = \MailPoet\API\API::MP( 'v1' );
try {
// Check if subscriber exists.
$existing_subscriber = $mailpoet_api->getSubscriber( $subscriber['email'] );
} catch ( \Exception $e ) {
$existing_subscriber = false;
}
try {
if ( ! $existing_subscriber ) {
$mailpoet_api->addSubscriber( $subscriber, $selected_lists, $options );
} else {
// Optionally update the subscriber..
if ( rgar( $feed['meta'], 'update_existing_subscriber' ) ) {
$mailpoet_api->updateSubscriber( $subscriber['email'], $subscriber );
}
// Subscriber exists, add to new lists..
$mailpoet_api->subscribeToLists( $subscriber['email'], $selected_lists, $options );
}
} catch ( \Exception $e ) {
$this->log_error( __METHOD__ . '(): MailPoet API Error: ' . $e->getMessage() );
}
return $entry;
}
}