PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.7.4
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.7.4
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / elementor / modules / optin / actions / mailerlite.php

mailerlite.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.7.4, at includes/elementor/modules/optin/actions/mailerlite.php

193 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || die();
4
5 use Sellkit_Elementor_Optin_Ajaxhandler as AjaxHandler;
6
7 /**
8 * Initializing the MailerLite action by extending Action base and using CRM trait.
9 *
10 * @since 1.5.0
11 */
12 class Sellkit_Elementor_Optin_Action_Mailerlite extends Sellkit_Elementor_Optin_Action_Base {
13 use Sellkit_Elementor_Optin_CRM;
14
15 private $api_key;
16
17 public static $instance;
18
19 public static function get_instance() {
20 if ( empty( static::$instance ) ) {
21 static::$instance = new static();
22 }
23
24 return static::$instance;
25 }
26
27 public function get_name() {
28 return 'mailerlite';
29 }
30
31 public function get_title() {
32 return esc_html__( 'MailerLite', 'sellkit' );
33 }
34
35 protected function get_base_url() {
36 return 'https://api.mailerlite.com/api/v2/';
37 }
38
39 protected function get_headers() {
40 return [
41 'X-MailerLite-ApiKey' => $this->api_key,
42 'Content-Type' => 'application/json',
43 ];
44 }
45
46 protected function get_get_request_args() {
47 return [
48 'timeout' => 100,
49 'sslverify' => false,
50 'headers' => $this->get_headers(),
51 ];
52 }
53
54 public function add_controls( $widget ) {
55 $action = $this->get_name();
56
57 $this->add_api_controls( $widget, esc_html__( 'Group', 'sellkit' ) );
58 $this->add_field_mapping_controls( $widget );
59
60 $widget->add_control( "{$action}_resubscribe",
61 [
62 'label' => esc_html__( 'Double Opt-In', 'sellkit' ),
63 'type' => 'switcher',
64 'description' => esc_html__( 'Activates the existing user, if unsubscribed.', 'sellkit' ),
65 'conditions' => [
66 'terms' => [
67 [
68 'name' => "{$action}_list",
69 'operator' => '!in',
70 'value' => [ 'none', 'fetching', 'noList' ],
71 ],
72 ],
73 ],
74 ]
75 );
76 }
77
78 public function run( AjaxHandler $ajax_handler ) {
79 $form_settings = $ajax_handler->form['settings'];
80
81 // Retrieve and check List.
82 $list_id = $form_settings[ $this->get_name() . '_list' ];
83 if ( empty( $list_id ) || in_array( $list_id, [ 'default', 'fetching', 'noList' ], true ) ) {
84 return $ajax_handler->add_response( 'admin_errors', $this->get_invalid_list_message( 'group' ) );
85 }
86
87 // Retireve and check API credentials.
88 $this->api_key = $this->get_api_param( $form_settings );
89 if ( empty( $this->api_key ) ) {
90 return $ajax_handler->add_response( 'admin_errors', "{$this->get_title()}: " . esc_html__( 'Missing API credentials.', 'sellkit' ) );
91 }
92
93 // Try subscription.
94 $this->ajax_handler = $ajax_handler;
95 $subscriber = $this->create_subscriber_object();
96 $this->subscribe( $subscriber, $list_id );
97 }
98
99 public function get_list( AjaxHandler $ajax_handler, $params ) {
100 $this->api_key = $this->get_api_param( $params );
101 $results = $this->send_get( 'groups' );
102 $groups = [];
103
104 if ( 200 === $results['code'] && ! empty( $results['body'] ) ) {
105 foreach ( $results['body'] as $group ) {
106 if ( is_array( $group ) ) {
107 $groups[ $group['id'] ] = $group['name'];
108 }
109 }
110 }
111
112 $list = [ 'lists' => $groups ];
113 return $ajax_handler->add_response( 'success', $list );
114 }
115
116 public function get_additional_data( AjaxHandler $ajax_handler, $params ) {
117 $this->api_key = $this->get_api_param( $params );
118
119 $data = [
120 'custom_fields' => $this->get_remote_custom_fields(),
121 ];
122
123 return $ajax_handler->add_response( 'success', $data );
124 }
125
126 private function get_remote_custom_fields() {
127 $results = $this->send_get( 'fields' );
128 $default_fields = $this->get_default_remote_fields();
129 $default_fields = array_merge( $default_fields['required'], $default_fields['optional'] );
130 $custom_fields = [];
131
132 if ( empty( $results['body'] ) ) {
133 return $custom_fields;
134 }
135
136 foreach ( $results['body'] as $field ) {
137 if ( is_array( $field ) && ! array_key_exists( $field['key'], $default_fields ) ) {
138 $custom_fields[ $field['key'] ] = $field['title'];
139 }
140 }
141
142 return $custom_fields;
143 }
144
145 protected function get_default_remote_fields() {
146 return [
147 'required' => [
148 'email' => esc_html__( 'Email', 'sellkit' ),
149 ],
150 'optional' => [
151 'name' => esc_html__( 'Name', 'sellkit' ),
152 ],
153 ];
154 }
155
156 protected function create_subscriber_object() {
157 $mapped_fields = $this->get_field_mappings();
158 $default_fields = $this->get_default_remote_fields();
159 $subscriber = [ 'resubscribe' => false ];
160
161 foreach ( $mapped_fields as $key => $value ) {
162 $is_custom =
163 ! array_key_exists( $key, $default_fields['required'] ) &&
164 ! array_key_exists( $key, $default_fields['optional'] );
165
166 if ( $is_custom ) {
167 $subscriber['fields'][ $key ] = $value;
168 continue;
169 }
170
171 $subscriber[ $key ] = $value;
172 }
173
174 if ( 'yes' === $this->ajax_handler->form['settings']['mailerlite_resubscribe'] ) {
175 $subscriber['resubscribe'] = true;
176 }
177
178 return $subscriber;
179 }
180
181 protected function subscribe( $subscriber_data, $group ) {
182 $endpoint = 'groups/' . $group . '/subscribers';
183 $args = [
184 'method' => 'POST',
185 'timeout' => 100,
186 'headers' => $this->get_headers(),
187 'body' => wp_json_encode( $subscriber_data ),
188 ];
189
190 return $this->send_post( $endpoint, $args );
191 }
192 }
193