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 / block-editor / blocks / optin / crm / convertkit.php

convertkit.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/block-editor/blocks/optin/crm/convertkit.php

142 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Sellkit\Blocks\Optin;
3
4 defined( 'ABSPATH' ) || die();
5
6 /**
7 * ConvertKit class.
8 *
9 * @since 2.3.0
10 */
11 class ConvertKit {
12 /**
13 * Object of helper class.
14 *
15 * @since 2.3.0
16 * @var object
17 */
18 private static $helper;
19
20 /**
21 * Run the ConvertKit process.
22 *
23 * @param object $helper The helper class instance.
24 */
25 public static function run( $helper ) {
26 self::$helper = $helper;
27
28 if ( empty( self::$helper ) ) {
29 return;
30 }
31
32 self::handle_convertkit();
33 }
34
35 /**
36 * Handle convertKit.
37 *
38 * @since 2.3.0
39 */
40 private static function handle_convertkit() {
41 if ( empty( self::$helper->attributes['convertKit'] ) ) {
42 return self::$helper->add_response( 'admin_errors', esc_html__( 'ConvertKit error: Missing configuration.', 'sellkit' ) );
43 }
44
45 self::$helper->check_api_params( 'convertkit', 'ConvertKit' );
46
47 $mapping_fields = self::$helper->attributes['convertKit'];
48 self::$helper->check_required_fields( $mapping_fields, [ 'form', 'email' ] );
49
50 $fields = self::$helper->form_data['fields'];
51
52 $subscriber_data = self::map_fields_to_subscriber( $mapping_fields, $fields );
53
54 $response = self::send_subscriber_to_convertkit( $subscriber_data, $mapping_fields['form'] );
55
56 if ( is_wp_error( $response ) ) {
57 return self::$helper->add_response( 'admin_errors', $response->get_error_message() );
58 }
59
60 $code = (int) wp_remote_retrieve_response_code( $response );
61
62 if ( $code < 200 || $code >= 300 ) {
63 return self::$helper->add_response(
64 'admin_errors',
65 sprintf(
66 /* Translators: 1: CRM name 2: Error code 3: Error message */
67 esc_html__( '%1$s: Request error-%2$s -- %3$s', 'sellkit' ),
68 'ConvertKit',
69 esc_html( $code ),
70 wp_remote_retrieve_response_message( $response )
71 ) . esc_html__( ' (issued by endpoint)', 'sellkit' )
72 );
73 }
74 }
75
76 /**
77 * Send subscriber data to ConvertKit API.
78 *
79 * @param array $subscriber_data The subscriber data.
80 * @param string $form_id The ConvertKit form ID.
81 * @return array|WP_Error The response or WP_Error on failure.
82 */
83 private static function send_subscriber_to_convertkit( $subscriber_data, $form_id ) {
84 $endpoint = 'forms/' . $form_id . '/subscribe?api_key=' . self::$helper->api_key;
85 $args = [
86 'method' => 'POST',
87 'timeout' => 100,
88 'headers' => [
89 'Content-Type' => 'application/json',
90 ],
91 'body' => wp_json_encode( $subscriber_data ),
92 ];
93
94 return self::send_post( $endpoint, $args );
95 }
96
97 /**
98 * Map fields to ConvertKit subscriber data.
99 *
100 * @param array $mapping_fields The mapping fields.
101 * @param array $fields The form data fields.
102 * @return array The subscriber data.
103 */
104 private static function map_fields_to_subscriber( $mapping_fields, $fields ) {
105 $subscriber_data = [
106 'form_id' => $mapping_fields['form'],
107 'email' => isset( $fields[ $mapping_fields['email'] ] ) ? $fields[ $mapping_fields['email'] ] : '',
108 'tags' => [],
109 ];
110
111 foreach ( $mapping_fields as $index => $value ) {
112 if ( ! empty( $fields[ $value ] ) ) {
113 $fields[ $value ] = self::$helper->get_address_field( $fields[ $value ], 'address' );
114 }
115
116 if ( ! in_array( $index, [ 'form', 'email', 'tags' ], true ) && ! empty( $fields[ $value ] ) ) {
117 $subscriber_data[ $index ] = $fields[ $value ];
118 }
119 }
120
121 if ( ! empty( $mapping_fields['tagsValue'] ) ) {
122 $subscriber_data['tags'] = [ $mapping_fields['tagsValue'] ];
123 }
124
125 $subscriber_data['api_key'] = self::$helper->api_key;
126
127 return $subscriber_data;
128 }
129
130 /**
131 * Send a POST request to the specified endpoint.
132 *
133 * @param string $endpoint The API endpoint.
134 * @param array $args The request arguments.
135 * @return array|WP_Error The response or WP_Error on failure.
136 */
137 private static function send_post( $endpoint, $args ) {
138 $api_url = 'https://api.convertkit.com/v3/' . $endpoint;
139 return wp_remote_post( $api_url, $args );
140 }
141 }
142