| 1 |
<?php |
| 2 |
namespace Sellkit\Blocks\Optin; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die(); |
| 5 |
|
| 6 |
/** |
| 7 |
* MailerLite class. |
| 8 |
* |
| 9 |
* @since 2.3.0 |
| 10 |
*/ |
| 11 |
class Mailerlite { |
| 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_mailerlite(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Handle MailerLite subscription. |
| 37 |
*/ |
| 38 |
private static function handle_mailerlite() { |
| 39 |
if ( empty( self::$helper->attributes['mailerLite'] ) ) { |
| 40 |
return self::$helper->add_response( 'admin_errors', esc_html__( 'MailerLite error: Missing configuration.', 'sellkit' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
self::$helper->check_api_params( 'mailerlite', 'MailerLite' ); |
| 44 |
|
| 45 |
$mapping_fields = self::$helper->attributes['mailerLite']; |
| 46 |
self::$helper->check_required_fields( $mapping_fields, [ 'group', 'email' ] ); |
| 47 |
|
| 48 |
$fields = self::$helper->form_data['fields']; |
| 49 |
|
| 50 |
$subscriber_data = self::map_fields_to_subscriber( $mapping_fields, $fields ); |
| 51 |
|
| 52 |
$response = self::send_subscriber_to_mailerlite( $subscriber_data, $mapping_fields['group'] ); |
| 53 |
|
| 54 |
if ( is_wp_error( $response ) ) { |
| 55 |
return self::$helper->add_response( 'admin_errors', $response->get_error_message() ); |
| 56 |
} |
| 57 |
|
| 58 |
$code = $response['code']; |
| 59 |
|
| 60 |
if ( $code < 200 || $code >= 300 ) { |
| 61 |
return self::$helper->add_response( |
| 62 |
'admin_errors', |
| 63 |
sprintf( |
| 64 |
/* Translators: 1: CRM name 2: Error code 3: Error message */ |
| 65 |
esc_html__( '%1$s: Request error-%2$s -- %3$s', 'sellkit' ), |
| 66 |
'MailerLite', |
| 67 |
esc_html( $code ), |
| 68 |
wp_remote_retrieve_response_message( $response ) |
| 69 |
) . esc_html__( ' (issued by endpoint)', 'sellkit' ) |
| 70 |
); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Map fields to MailerLite subscriber data. |
| 76 |
* |
| 77 |
* @param array $mapping_fields The mapping fields. |
| 78 |
* @param array $fields The form data fields. |
| 79 |
* @return array The subscriber data. |
| 80 |
*/ |
| 81 |
private static function map_fields_to_subscriber( $mapping_fields, $fields ) { |
| 82 |
$subscriber_data = [ |
| 83 |
'email' => ! empty( $fields[ $mapping_fields['email'] ] ) ? $fields[ $mapping_fields['email'] ] : '', |
| 84 |
'name' => ( isset( $mapping_fields['name'] ) && ! empty( $fields[ $mapping_fields['name'] ] ) ) ? $fields[ $mapping_fields['name'] ] : '', |
| 85 |
'resubscribe' => false, |
| 86 |
]; |
| 87 |
|
| 88 |
foreach ( $mapping_fields as $index => $value ) { |
| 89 |
if ( ! empty( $fields[ $value ] ) ) { |
| 90 |
$fields[ $value ] = self::$helper->get_address_field( $fields[ $value ], 'address' ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! in_array( $index, [ 'email', 'name', 'group' ], true ) && ! empty( $fields[ $value ] ) ) { |
| 94 |
$subscriber_data['fields'][ $index ] = $fields[ $value ]; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! empty( $mapping_fields['doubleOptIn'] ) ) { |
| 99 |
$subscriber_data['resubscribe'] = true; |
| 100 |
} |
| 101 |
|
| 102 |
return $subscriber_data; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Send subscriber data to MailerLite API. |
| 107 |
* |
| 108 |
* @param array $subscriber_data The subscriber data. |
| 109 |
* @param string $group_id The MailerLite group ID. |
| 110 |
* @return array|WP_Error The response or WP_Error on failure. |
| 111 |
*/ |
| 112 |
private static function send_subscriber_to_mailerlite( $subscriber_data, $group_id ) { |
| 113 |
$endpoint = "groups/{$group_id}/subscribers"; |
| 114 |
$args = [ |
| 115 |
'method' => 'POST', |
| 116 |
'timeout' => 100, |
| 117 |
'headers' => self::get_headers(), |
| 118 |
'body' => wp_json_encode( $subscriber_data ), |
| 119 |
]; |
| 120 |
|
| 121 |
return self::send_post( $endpoint, $args ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Send a POST request to the specified endpoint. |
| 126 |
* |
| 127 |
* @param string $endpoint The API endpoint. |
| 128 |
* @param array $args The request arguments. |
| 129 |
* @return array|WP_Error The response or WP_Error on failure. |
| 130 |
*/ |
| 131 |
private static function send_post( $endpoint, $args ) { |
| 132 |
$api_url = 'https://api.mailerlite.com/api/v2/' . $endpoint; |
| 133 |
$response = wp_remote_post( $api_url, $args ); |
| 134 |
|
| 135 |
if ( is_wp_error( $response ) ) { |
| 136 |
return $response; |
| 137 |
} |
| 138 |
|
| 139 |
$code = (int) wp_remote_retrieve_response_code( $response ); |
| 140 |
|
| 141 |
if ( $code < 200 || $code >= 300 ) { |
| 142 |
self::$helper->add_response( |
| 143 |
'admin_errors', |
| 144 |
/* translators: %s Error code */ |
| 145 |
sprintf( esc_html__( 'MailerLite: Error in request, code: %s', 'sellkit' ), esc_html( $code ) ) |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
return [ |
| 150 |
'code' => $code, |
| 151 |
'body' => json_decode( wp_remote_retrieve_body( $response ), true ), |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get headers for the API request. |
| 157 |
* |
| 158 |
* @return array The headers for the API request. |
| 159 |
*/ |
| 160 |
private static function get_headers() { |
| 161 |
return [ |
| 162 |
'Content-Type' => 'application/json', |
| 163 |
'X-MailerLite-ApiKey' => self::$helper->api_key, |
| 164 |
]; |
| 165 |
} |
| 166 |
} |
| 167 |
|