| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit_Elementor_Optin_Ajaxhandler as AjaxHandler; |
| 6 |
|
| 7 |
/** |
| 8 |
* Initializing the Drip action by extending Action base and using CRM trait. |
| 9 |
* |
| 10 |
* @since 1.5.0 |
| 11 |
*/ |
| 12 |
class Sellkit_Elementor_Optin_Action_Drip 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 'drip'; |
| 29 |
} |
| 30 |
|
| 31 |
public function get_title() { |
| 32 |
return esc_html__( 'Drip', 'sellkit' ); |
| 33 |
} |
| 34 |
|
| 35 |
protected function get_base_url() { |
| 36 |
return 'https://api.getdrip.com/v2/'; |
| 37 |
} |
| 38 |
|
| 39 |
protected function get_headers() { |
| 40 |
return [ |
| 41 |
'Authorization' => 'Basic ' . base64_encode( $this->api_key ), |
| 42 |
'Content-Type' => 'application/vnd.api+json', |
| 43 |
'User-Agent' => $this->user_agent, |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
protected function get_get_request_args() { |
| 48 |
return [ |
| 49 |
'api_key' => $this->api_key, |
| 50 |
'api_output' => 'json', |
| 51 |
'timeout' => 100, |
| 52 |
'sslverify' => false, |
| 53 |
'headers' => $this->get_headers(), |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
public function add_controls( $widget ) { |
| 58 |
$this->add_api_controls( $widget, esc_html__( 'Account', 'sellkit' ) ); |
| 59 |
$this->add_field_mapping_controls( $widget ); |
| 60 |
$this->add_tag_control( $widget ); |
| 61 |
} |
| 62 |
|
| 63 |
public function run( AjaxHandler $ajax_handler ) { |
| 64 |
$form_settings = $ajax_handler->form['settings']; |
| 65 |
|
| 66 |
// Retrieve and check List. |
| 67 |
$list_id = $form_settings[ $this->get_name() . '_list' ]; |
| 68 |
if ( empty( $list_id ) || in_array( $list_id, [ 'default', 'fetching', 'noList' ], true ) ) { |
| 69 |
return $ajax_handler->add_response( 'admin_errors', $this->get_invalid_list_message( 'account' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
// Retireve and check API credentials. |
| 73 |
$this->api_key = $this->get_api_param( $form_settings ); |
| 74 |
if ( empty( $this->api_key ) ) { |
| 75 |
return $ajax_handler->add_response( 'admin_errors', "{$this->get_title()}: " . esc_html__( 'Missing API credentials.', 'sellkit' ) ); |
| 76 |
} |
| 77 |
|
| 78 |
// Try subscription. |
| 79 |
$this->ajax_handler = $ajax_handler; |
| 80 |
$subscriber = $this->create_subscriber_object(); |
| 81 |
$this->subscribe( $subscriber, $list_id ); |
| 82 |
} |
| 83 |
|
| 84 |
public function get_list( AjaxHandler $ajax_handler, $params ) { |
| 85 |
$this->api_key = $this->get_api_param( $params ); |
| 86 |
$results = $this->send_get( 'accounts' ); |
| 87 |
$accounts = []; |
| 88 |
|
| 89 |
if ( ! empty( $results['body']['accounts'] ) ) { |
| 90 |
foreach ( $results['body']['accounts'] as $account ) { |
| 91 |
$accounts[ $account['id'] ] = $account['name']; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
$list = [ 'lists' => $accounts ]; |
| 96 |
|
| 97 |
return $ajax_handler->add_response( 'success', $list ); |
| 98 |
} |
| 99 |
|
| 100 |
public function get_additional_data( AjaxHandler $ajax_handler, $params ) { |
| 101 |
$this->api_key = $this->get_api_param( $params ); |
| 102 |
|
| 103 |
$data = [ |
| 104 |
'custom_fields' => $this->get_remote_custom_fields( $params['list_id'] ), |
| 105 |
'tags' => $this->get_remote_tags( $params['list_id'] ), |
| 106 |
]; |
| 107 |
|
| 108 |
return $ajax_handler->add_response( 'success', $data ); |
| 109 |
} |
| 110 |
|
| 111 |
private function get_remote_custom_fields( $account_id ) { |
| 112 |
$results = $this->send_get( "{$account_id}/custom_field_identifiers" ); |
| 113 |
|
| 114 |
$default_fields = $this->get_default_remote_fields()['optional']; |
| 115 |
$custom_fields = []; |
| 116 |
|
| 117 |
if ( empty( $results['body']['custom_field_identifiers'] ) ) { |
| 118 |
return $custom_fields; |
| 119 |
} |
| 120 |
|
| 121 |
foreach ( $results['body']['custom_field_identifiers'] as $field ) { |
| 122 |
if ( ! array_key_exists( $field, $default_fields ) ) { |
| 123 |
$custom_fields[ $field ] = $field; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $custom_fields; |
| 128 |
} |
| 129 |
|
| 130 |
private function get_remote_tags( $account_id ) { |
| 131 |
$results = $this->send_get( "{$account_id}/tags" ); |
| 132 |
$tags = []; |
| 133 |
|
| 134 |
if ( empty( $results['body']['tags'] ) ) { |
| 135 |
return $tags; |
| 136 |
} |
| 137 |
|
| 138 |
foreach ( $results['body']['tags'] as $tag ) { |
| 139 |
$tags[ $tag ] = $tag; |
| 140 |
} |
| 141 |
|
| 142 |
return $tags; |
| 143 |
} |
| 144 |
|
| 145 |
protected function get_default_remote_fields() { |
| 146 |
return [ |
| 147 |
'required' => [ |
| 148 |
'email' => esc_html__( 'Email', 'sellkit' ), |
| 149 |
], |
| 150 |
'optional' => [ |
| 151 |
'first_name' => esc_html__( 'First Name', 'sellkit' ), |
| 152 |
'last_name' => esc_html__( 'Last Name', 'sellkit' ), |
| 153 |
'address1' => esc_html__( 'Address 1', 'sellkit' ), |
| 154 |
'address2' => esc_html__( 'Address 2', 'sellkit' ), |
| 155 |
'city' => esc_html__( 'City', 'sellkit' ), |
| 156 |
'state' => esc_html__( 'State', 'sellkit' ), |
| 157 |
'country' => esc_html__( 'Country', 'sellkit' ), |
| 158 |
'zip' => esc_html__( 'Zip', 'sellkit' ), |
| 159 |
'phone' => esc_html__( 'Phone', 'sellkit' ), |
| 160 |
'sms_number' => esc_html__( 'SMS Number', 'sellkit' ), |
| 161 |
'sms_consent' => esc_html__( 'SMS Consent', 'sellkit' ), |
| 162 |
'time_zone' => esc_html__( 'Timezone', 'sellkit' ), |
| 163 |
], |
| 164 |
]; |
| 165 |
} |
| 166 |
|
| 167 |
protected function create_subscriber_object() { |
| 168 |
$mapped_fields = $this->get_field_mappings(); |
| 169 |
$subscriber = [ 'ip_address' => static::get_client_ip() ]; |
| 170 |
$default_fields = $this->get_default_remote_fields(); |
| 171 |
|
| 172 |
foreach ( $mapped_fields as $key => $value ) { |
| 173 |
$is_custom = |
| 174 |
! array_key_exists( $key, $default_fields['required'] ) && |
| 175 |
! array_key_exists( $key, $default_fields['optional'] ); |
| 176 |
|
| 177 |
if ( $is_custom ) { |
| 178 |
$subscriber['custom_fields'][ $key ] = $value; |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
$subscriber[ $key ] = $value; |
| 183 |
} |
| 184 |
|
| 185 |
//add tags if present |
| 186 |
$form_settings = $this->ajax_handler->form['settings']; |
| 187 |
|
| 188 |
if ( ! empty( $form_settings['drip_tags'] ) ) { |
| 189 |
$subscriber['tags'] = $form_settings['drip_tags']; |
| 190 |
} |
| 191 |
|
| 192 |
return $subscriber; |
| 193 |
} |
| 194 |
|
| 195 |
protected function subscribe( $subscriber_data, $account_id ) { |
| 196 |
$endpoint = $account_id . '/subscribers/'; |
| 197 |
$args = [ |
| 198 |
'method' => 'POST', |
| 199 |
'timeout' => 100, |
| 200 |
'headers' => $this->get_headers(), |
| 201 |
'body' => wp_json_encode( [ |
| 202 |
'subscribers' => [ $subscriber_data ], |
| 203 |
] ), |
| 204 |
]; |
| 205 |
|
| 206 |
return $this->send_post( $endpoint, $args ); |
| 207 |
} |
| 208 |
} |
| 209 |
|