PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
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 / convertkit.php

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

193 lines 4.8 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 ConvertKit action by extending Action base and using CRM trait.
9 *
10 * @since 1.5.0
11 */
12 class Sellkit_Elementor_Optin_Action_Convertkit 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 'convertkit';
29 }
30
31 public function get_title() {
32 return esc_html__( 'ConvertKit', 'sellkit' );
33 }
34
35 protected function get_base_url() {
36 return 'https://api.convertkit.com/v3/';
37 }
38
39 protected function get_headers() {
40 return [
41 'Content-Type' => 'application/json',
42 ];
43 }
44
45 protected function get_get_request_args() {
46 return [
47 'timeout' => 100,
48 'sslverify' => false,
49 ];
50 }
51
52 public function add_controls( $widget ) {
53 $this->add_api_controls( $widget, esc_html__( 'Form', 'sellkit' ) );
54 $this->add_field_mapping_controls( $widget );
55 $this->add_tag_control( $widget );
56 }
57
58 public function run( AjaxHandler $ajax_handler ) {
59 $form_settings = $ajax_handler->form['settings'];
60
61 // Retrieve and check List.
62 $list_id = $form_settings[ $this->get_name() . '_list' ];
63 if ( empty( $list_id ) || in_array( $list_id, [ 'default', 'fetching', 'noList' ], true ) ) {
64 return $ajax_handler->add_response( 'admin_errors', $this->get_invalid_list_message( 'form' ) );
65 }
66
67 // Retireve and check API credentials.
68 $this->api_key = $this->get_api_param( $form_settings );
69 if ( empty( $this->api_key ) ) {
70 return $ajax_handler->add_response( 'admin_errors', "{$this->get_title()}: " . esc_html__( 'Missing API credentials.', 'sellkit' ) );
71 }
72
73 // Try subscription.
74 $this->ajax_handler = $ajax_handler;
75 $subscriber = $this->create_subscriber_object();
76 $this->subscribe( $subscriber, $list_id );
77 }
78
79 public function get_list( AjaxHandler $ajax_handler, $params ) {
80 $this->api_key = $this->get_api_param( $params );
81 $results = $this->send_get( 'forms/?api_key=' . $this->api_key );
82 $forms = [];
83
84 if ( ! empty( $results['body']['forms'] ) ) {
85 foreach ( $results['body']['forms'] as $index => $form ) {
86 if ( is_array( $form ) ) {
87 $forms[ $form['id'] ] = $form['name'];
88 }
89 }
90 }
91
92 $list = [ 'lists' => $forms ];
93
94 return $ajax_handler->add_response( 'success', $list );
95 }
96
97 public function get_additional_data( AjaxHandler $ajax_handler, $params ) {
98 $this->api_key = $this->get_api_param( $params );
99
100 $data = [
101 'custom_fields' => $this->get_remote_custom_fields(),
102 'tags' => $this->get_remote_tags(),
103 ];
104
105 return $ajax_handler->add_response( 'success', $data );
106 }
107
108 private function get_remote_custom_fields() {
109 $results = $this->send_get( 'custom_fields?api_key=' . $this->api_key );
110 $custom_fields = [];
111
112 if ( empty( $results['body']['custom_fields'] ) ) {
113 return $custom_fields;
114 }
115
116 foreach ( $results['body']['custom_fields'] as $field ) {
117 if ( is_array( $field ) ) {
118 $custom_fields[ $field['key'] ] = $field['label'];
119 }
120 }
121
122 return $custom_fields;
123 }
124
125 private function get_remote_tags() {
126 $results = $this->send_get( 'tags/?api_key=' . $this->api_key );
127 $tags = [];
128
129 if ( empty( $results['body']['tags'] ) ) {
130 return $tags;
131 }
132
133 foreach ( $results['body']['tags'] as $index => $tag ) {
134 if ( is_array( $tag ) ) {
135 $tags[ $tag['id'] ] = $tag['name'];
136 }
137 }
138
139 return $tags;
140 }
141
142 protected function get_default_remote_fields() {
143 return [
144 'required' => [
145 'email' => esc_html__( 'Email', 'sellkit' ),
146 ],
147 'optional' => [
148 'first_name' => esc_html__( 'First Name', 'sellkit' ),
149 ],
150 ];
151 }
152
153 protected function create_subscriber_object() {
154 $mapped_fields = $this->get_field_mappings();
155 $subscriber = [ 'api_key' => $this->api_key ];
156 $default_fields = $this->get_default_remote_fields();
157
158 foreach ( $mapped_fields as $key => $value ) {
159 $is_custom =
160 ! array_key_exists( $key, $default_fields['required'] ) &&
161 ! array_key_exists( $key, $default_fields['optional'] );
162
163 if ( $is_custom ) {
164 $subscriber['fields'][ $key ] = $value;
165 continue;
166 }
167
168 $subscriber[ $key ] = $value;
169 }
170
171 //add tags if present
172 $form_settings = $this->ajax_handler->form['settings'];
173
174 if ( ! empty( $form_settings['convertkit_tags'] ) ) {
175 $subscriber['tags'] = $form_settings['convertkit_tags'];
176 }
177
178 return $subscriber;
179 }
180
181 protected function subscribe( $subscriber_data, $form_id ) {
182 $endpoint = 'forms/' . $form_id . '/subscribe?api_key=' . $this->api_key;
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