PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 2.7.0
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v2.7.0
2.7.0 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 All 43 releases
sellkit / includes / block-editor / blocks / optin / crm / drip.php

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

143 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 * Drip class.
8 *
9 * @since 2.3.0
10 */
11 class Drip {
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 Drip 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_drip();
33 }
34
35 /**
36 * Handle Drip.
37 *
38 * @since 2.3.0
39 */
40 private static function handle_drip() {
41 if ( empty( self::$helper->attributes['drip'] ) ) {
42 return self::$helper->add_response( 'admin_errors', esc_html__( 'Drip error: Missing configuration.', 'sellkit' ) );
43 }
44
45 self::$helper->check_api_params( 'drip', 'Drip' );
46
47 $mapping_fields = self::$helper->attributes['drip'];
48
49 self::$helper->check_required_fields( $mapping_fields, [ 'account', 'email' ] );
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_drip( $subscriber_data, (int) $mapping_fields['account'] );
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 if ( $code < 200 || $code >= 300 ) {
62 return self::$helper->add_response(
63 'admin_errors',
64 sprintf(
65 /* Translators: 1: CRM name 2: Error code 3: Error message */
66 esc_html__( '%1$s: Request error-%2$s -- %3$s', 'sellkit' ),
67 'Drip',
68 esc_html( $code ),
69 wp_remote_retrieve_response_message( $response )
70 ) . esc_html__( ' (issued by endpoint)', 'sellkit' )
71 );
72 }
73 }
74
75 /**
76 * Map fields to Drip subscriber data.
77 *
78 * @param array $mapping_fields The mapping fields.
79 * @param array $fields The form data fields.
80 * @return array The subscriber data.
81 */
82 private static function map_fields_to_subscriber( $mapping_fields, $fields ) {
83 $subscriber_data = [
84 'email' => isset( $fields[ $mapping_fields['email'] ] ) ? $fields[ $mapping_fields['email'] ] : '',
85 ];
86
87 foreach ( $mapping_fields as $index => $value ) {
88 if ( ! empty( $fields[ $value ] ) ) {
89 $fields[ $value ] = self::$helper->get_address_field( $fields[ $value ], 'address' );
90 }
91
92 if ( ! in_array( $index, [ 'account', 'email', 'tagsValue' ], true ) && ! empty( $fields[ $value ] ) ) {
93 $subscriber_data[ $index ] = $fields[ $value ];
94 }
95 }
96
97 if ( ! empty( $mapping_fields['tagsValue'] ) ) {
98 $subscriber_data['tags'] = [ $mapping_fields['tagsValue'] ];
99 }
100
101 $subscriber_data['ip_address'] = self::$helper::get_client_ip();
102
103 return $subscriber_data;
104 }
105
106 /**
107 * Send subscriber data to Drip API.
108 *
109 * @param array $subscriber_data The subscriber data.
110 * @param string $account_id The Drip account ID.
111 * @return array|WP_Error The response or WP_Error on failure.
112 */
113 private static function send_subscriber_to_drip( $subscriber_data, $account_id ) {
114 $endpoint = $account_id . '/subscribers/';
115 $args = [
116 'method' => 'POST',
117 'timeout' => 100,
118 'headers' => [
119 'Content-Type' => 'application/vnd.api+json',
120 'User-Agent' => 'Sellkit',
121 'Authorization' => 'Basic ' . base64_encode( self::$helper->api_key ),
122 ],
123 'body' => wp_json_encode( [
124 'subscribers' => [ $subscriber_data ],
125 ] ),
126 ];
127
128 return self::send_post( $endpoint, $args );
129 }
130
131 /**
132 * Send a POST request to the specified endpoint.
133 *
134 * @param string $endpoint The API endpoint.
135 * @param array $args The request arguments.
136 * @return array|WP_Error The response or WP_Error on failure.
137 */
138 private static function send_post( $endpoint, $args ) {
139 $api_url = 'https://api.getdrip.com/v2/' . $endpoint;
140 return wp_remote_post( $api_url, $args );
141 }
142 }
143