PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 7.8.14
Hustle – Email Marketing, Lead Generation, Optins, Popups v7.8.14
7.8.14 7.8.14.1 7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / inc / providers / mailerlite / hustle-mailerlite.php
wordpress-popup / inc / providers / mailerlite Last commit date
images 7 years ago hustle-mailerlite-api.php 3 years ago hustle-mailerlite-form-hooks.php 5 months ago hustle-mailerlite-form-settings.php 5 months ago hustle-mailerlite.php 5 months ago mailerlite.php 5 months ago
hustle-mailerlite.php
404 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Hustle_MailerLite class
4 *
5 * @package Hustle
6 */
7
8 if ( ! class_exists( 'Hustle_MailerLite' ) ) :
9
10 include_once 'hustle-mailerlite-api.php';
11
12 /**
13 * Class Hustle_MailerLite
14 */
15 class Hustle_MailerLite extends Hustle_Provider_Abstract {
16
17 const SLUG = 'mailerlite';
18
19 /**
20 * Provider Instance
21 *
22 * @since 3.0.5
23 *
24 * @var self|null
25 */
26 protected static $instance = null;
27
28 /**
29 * Slug
30 *
31 * @since 3.0.5
32 * @var string
33 */
34 protected $slug = 'mailerlite';
35
36 /**
37 * Version
38 *
39 * @since 3.0.5
40 * @var string
41 */
42 protected $version = '1.0';
43
44 /**
45 * Class
46 *
47 * @since 3.0.5
48 * @var string
49 */
50 protected $class = __CLASS__;
51
52 /**
53 * Title
54 *
55 * @since 3.0.5
56 * @var string
57 */
58 protected $title = 'MailerLite';
59
60 /**
61 * Class name of form settings
62 *
63 * @var string
64 */
65 protected $form_settings = 'Hustle_MailerLite_Form_Settings';
66
67 /**
68 * Class name of form hooks
69 *
70 * @since 4.0
71 * @var string
72 */
73 protected $form_hooks = 'Hustle_MailerLite_Form_Hooks';
74
75 /**
76 * Provider constructor.
77 */
78 public function __construct() {
79 $this->icon_2x = plugin_dir_url( __FILE__ ) . 'images/icon.png';
80 $this->logo_2x = plugin_dir_url( __FILE__ ) . 'images/logo.png';
81 }
82
83 /**
84 * Get Instance
85 *
86 * @return self|null
87 */
88 public static function get_instance() {
89 if ( is_null( self::$instance ) ) {
90 self::$instance = new self();
91 }
92
93 return self::$instance;
94 }
95
96 /**
97 * Get api
98 *
99 * @param string $api_key Api key.
100 * @return \Hustle_MailerLite_Api
101 */
102 public static function api( $api_key ) {
103 $api = new Hustle_MailerLite_Api( $api_key );
104
105 return $api;
106 }
107
108 /**
109 * Get the wizard callbacks for the global settings.
110 *
111 * @since 4.0
112 *
113 * @return array
114 */
115 public function settings_wizards() {
116 return array(
117 array(
118 'callback' => array( $this, 'configure_api_key' ),
119 'is_completed' => array( $this, 'is_connected' ),
120 ),
121 );
122 }
123
124
125 /**
126 * Configure the API key settings. Global settings.
127 *
128 * @since 4.0
129 *
130 * @param array $submitted_data Submitted data.
131 * @return array
132 */
133 public function configure_api_key( $submitted_data ) {
134 $has_errors = false;
135 $default_data = array(
136 'api_key' => '',
137 'name' => '',
138 );
139 $current_data = $this->get_current_data( $default_data, $submitted_data );
140 $is_submit = isset( $submitted_data['api_key'] );
141 $global_multi_id = $this->get_global_multi_id( $submitted_data );
142
143 $api_key_validated = true;
144 if ( $is_submit ) {
145
146 $api_key_validated = $this->validate_api_key( $submitted_data['api_key'] );
147 if ( ! $api_key_validated ) {
148 $error_message = $this->provider_connection_falied();
149 $has_errors = true;
150 }
151
152 if ( ! $has_errors ) {
153 $settings_to_save = array(
154 'api_key' => $current_data['api_key'],
155 'name' => $current_data['name'],
156 );
157 // If not active, activate it.
158 // TODO: Wrap this in a friendlier method.
159 if ( Hustle_Provider_Utils::is_provider_active( $this->slug )
160 || Hustle_Providers::get_instance()->activate_addon( $this->slug ) ) {
161 $this->save_multi_settings_values( $global_multi_id, $settings_to_save );
162 } else {
163 $error_message = $this->provider_connection_falied();
164 $has_errors = true;
165 }
166 }
167
168 if ( ! $has_errors ) {
169
170 return array(
171 'html' => Hustle_Provider_Utils::get_integration_modal_title_markup( __( 'MailerLite Added', 'hustle' ), __( 'You can now go to your pop-ups, slide-ins and embeds and assign them to this integration', 'hustle' ) ),
172 'buttons' => array(
173 'close' => array(
174 'markup' => Hustle_Provider_Utils::get_provider_button_markup( __( 'Close', 'hustle' ), 'sui-button-ghost', 'close' ),
175 ),
176 ),
177 'redirect' => false,
178 'has_errors' => false,
179 'notification' => array(
180 'type' => 'success',
181 'text' => '<strong>' . $this->get_title() . '</strong> ' . esc_html__( 'Successfully connected', 'hustle' ),
182 ),
183 );
184
185 }
186 }
187
188 $options = array(
189 array(
190 'type' => 'wrapper',
191 'class' => $api_key_validated ? '' : 'sui-form-field-error',
192 'elements' => array(
193 'label' => array(
194 'type' => 'label',
195 'for' => 'api_key',
196 'value' => __( 'API Key', 'hustle' ),
197 ),
198 'api_key' => array(
199 'type' => 'text',
200 'name' => 'api_key',
201 'value' => $current_data['api_key'],
202 'placeholder' => __( 'Enter Key', 'hustle' ),
203 'id' => 'api_key',
204 'icon' => 'key',
205 ),
206 'error' => array(
207 'type' => 'error',
208 'class' => $api_key_validated ? 'sui-hidden' : '',
209 'value' => __( 'Please enter a valid MailerLite API key', 'hustle' ),
210 ),
211 ),
212 ),
213 array(
214 'type' => 'wrapper',
215 'style' => 'margin-bottom: 0;',
216 'elements' => array(
217 'label' => array(
218 'type' => 'label',
219 'for' => 'instance-name-input',
220 'value' => __( 'Identifier', 'hustle' ),
221 ),
222 'name' => array(
223 'type' => 'text',
224 'name' => 'name',
225 'value' => $current_data['name'],
226 'placeholder' => __( 'E.g. Business Account', 'hustle' ),
227 'id' => 'instance-name-input',
228 ),
229 'message' => array(
230 'type' => 'description',
231 'value' => __( 'Helps to distinguish your integrations if you have connected to the multiple accounts of this integration.', 'hustle' ),
232 ),
233 ),
234 ),
235 );
236
237 if ( $has_errors ) {
238 $error_notice = array(
239 'type' => 'notice',
240 'icon' => 'info',
241 'class' => 'sui-notice-error',
242 'value' => esc_html( $error_message ),
243 );
244 array_unshift( $options, $error_notice );
245 }
246
247 $step_html = Hustle_Provider_Utils::get_integration_modal_title_markup(
248 __( 'Configure MailerLite', 'hustle' ),
249 sprintf(
250 /* translators: 1. opening 'a' tag to MailerLite API page, 2. closing 'a' tag */
251 __( 'Log in to %1$sMailerLite Integrations%2$s page to get your API Key.', 'hustle' ),
252 '<a href="https://app.mailerlite.com/integrations/api/" target="_blank">',
253 '</a>'
254 )
255 );
256
257 $step_html .= Hustle_Provider_Utils::get_html_for_options( $options );
258
259 $is_edit = $this->settings_are_completed( $global_multi_id );
260 if ( $is_edit ) {
261 $buttons = array(
262 'disconnect' => array(
263 'markup' => Hustle_Provider_Utils::get_provider_button_markup(
264 __( 'Disconnect', 'hustle' ),
265 'sui-button-ghost',
266 'disconnect',
267 true
268 ),
269 ),
270 'save' => array(
271 'markup' => Hustle_Provider_Utils::get_provider_button_markup(
272 __( 'Save', 'hustle' ),
273 '',
274 'connect',
275 true
276 ),
277 ),
278 );
279 } else {
280 $buttons = array(
281 'connect' => array(
282 'markup' => Hustle_Provider_Utils::get_provider_button_markup(
283 __( 'Connect', 'hustle' ),
284 'sui-button-right',
285 'connect',
286 true
287 ),
288 ),
289 );
290
291 }
292
293 $response = array(
294 'html' => $step_html,
295 'buttons' => $buttons,
296 'has_errors' => $has_errors,
297 );
298
299 return $response;
300 }
301
302 /**
303 * Validate the provided API key.
304 *
305 * @since 4.0
306 *
307 * @param string $api_key Api key.
308 * @return bool
309 */
310 private function validate_api_key( $api_key ) {
311 if ( empty( trim( $api_key ) ) ) {
312 return false;
313 }
314
315 // Check API Key by validating it on get_info request.
316 try {
317 // Check if API key is valid.
318 $api = self::api( $api_key );
319
320 // Check if API key is valid.
321 $_lists = null;
322 if ( $api ) {
323 $_lists = $api->list_groups();
324 }
325
326 if ( ! is_array( $_lists ) || is_wp_error( $_lists ) || isset( $_lists['error'] ) ) {
327 Hustle_Provider_Utils::maybe_log( __METHOD__, __( 'Invalid MailerLite API key.', 'hustle' ) );
328 return false;
329 }
330 } catch ( Exception $e ) {
331 Hustle_Provider_Utils::maybe_log( __METHOD__, $e->getMessage() );
332 return false;
333 }
334
335 return true;
336 }
337
338 /**
339 * Check if an email exists
340 *
341 * @param int $group_id - the group id.
342 * @param string $email - the email.
343 * @param object $api - the API class.
344 *
345 * @return bool
346 */
347 public function email_exists( $group_id, $email, $api ) {
348 $member_groups = $api->get_subscriber( $email );
349 if ( is_wp_error( $member_groups ) ) {
350 return false;
351 } elseif ( ! isset( $member_groups['error'] ) ) {
352 foreach ( $member_groups as $member_group => $group ) {
353 if ( $group['id'] === (int) $group_id ) {
354 return true;
355 }
356 }
357 } else {
358 return false;
359 }
360 return false;
361 }
362
363 /**
364 * Get 3.0 provider mappings
365 *
366 * @return type
367 */
368 public function get_30_provider_mappings() {
369 return array(
370 'api_key' => 'api_key',
371 );
372 }
373
374 /**
375 * Add custom fields
376 *
377 * @param array $fields Fields.
378 * @param object $api Api.
379 * @return type
380 */
381 public static function add_custom_fields( $fields, $api ) {
382 $existing_fields = $api->get_custom_field();
383 $existing_field_keys = wp_list_pluck( $existing_fields, 'key' );
384
385 foreach ( $fields as $field ) {
386 if ( $existing_field_keys && ! in_array( $field['name'], $existing_field_keys, true ) ) {
387 $api->add_custom_field(
388 array(
389 'title' => $field['name'],
390 'type' => strtoupper( $field['type'] ),
391 )
392 );
393 }
394 }
395
396 return array(
397 'success' => true,
398 'field' => $fields,
399 );
400 }
401 }
402
403 endif;
404