| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit_Elementor_Optin_Ajaxhandler as AjaxHandler; |
| 6 |
|
| 7 |
/** |
| 8 |
* Initializing the ActiveCampaign action by extending Action base and using CRM trait. |
| 9 |
* |
| 10 |
* @since 1.5.0 |
| 11 |
*/ |
| 12 |
class Sellkit_Elementor_Optin_Action_ActiveCampaign extends Sellkit_Elementor_Optin_Action_Base { |
| 13 |
use Sellkit_Elementor_Optin_CRM; |
| 14 |
|
| 15 |
private $api_key; |
| 16 |
|
| 17 |
private $api_url; |
| 18 |
|
| 19 |
public static $instance; |
| 20 |
|
| 21 |
public static function get_instance() { |
| 22 |
if ( empty( static::$instance ) ) { |
| 23 |
static::$instance = new static(); |
| 24 |
} |
| 25 |
|
| 26 |
return static::$instance; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_name() { |
| 30 |
return 'activecampaign'; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_title() { |
| 34 |
return esc_html__( 'ActiveCampaign', 'sellkit' ); |
| 35 |
} |
| 36 |
|
| 37 |
protected function get_base_url() { |
| 38 |
return trailingslashit( $this->api_url ) . 'api/3/'; |
| 39 |
} |
| 40 |
|
| 41 |
protected function get_headers() { |
| 42 |
return [ |
| 43 |
'Api-Token' => $this->api_key, |
| 44 |
'Accept' => 'application/json', |
| 45 |
'Content-Type' => 'application/json', |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
protected function get_get_request_args() { |
| 50 |
return [ |
| 51 |
'timeout' => 100, |
| 52 |
'sslverify' => false, |
| 53 |
'headers' => $this->get_headers(), |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
public function add_controls( $widget ) { |
| 58 |
$action = $this->get_name(); |
| 59 |
|
| 60 |
$this->add_api_controls( $widget, esc_html__( 'List', 'sellkit' ) ); |
| 61 |
$this->add_field_mapping_controls( $widget ); |
| 62 |
$this->add_tag_control( $widget ); |
| 63 |
|
| 64 |
// We need to inject a new text control for API URL. |
| 65 |
$widget->start_injection( [ 'of' => "{$action}_custom_api_key" ] ); |
| 66 |
$widget->add_control( "{$action}_custom_api_url", |
| 67 |
[ |
| 68 |
'label' => esc_html__( 'Custom API URL', 'sellkit' ), |
| 69 |
'type' => 'text', |
| 70 |
'placeholder' => 'https://ACCOUNT-NAME.api-us1.com', |
| 71 |
'title' => 'https://ACCOUNT-NAME.api-us1.com', |
| 72 |
/* translators: Action name */ |
| 73 |
'description' => sprintf( esc_html__( 'Enter your %s API URL for only this form.', 'sellkit' ), $this->get_title() ), |
| 74 |
'condition' => [ "{$action}_api_key_source" => 'custom' ], |
| 75 |
] |
| 76 |
); |
| 77 |
$widget->end_injection(); |
| 78 |
} |
| 79 |
|
| 80 |
public function run( AjaxHandler $ajax_handler ) { |
| 81 |
$form_settings = $ajax_handler->form['settings']; |
| 82 |
|
| 83 |
// Retrieve and check List. |
| 84 |
$list_id = $form_settings[ $this->get_name() . '_list' ]; |
| 85 |
if ( empty( $list_id ) || in_array( $list_id, [ 'default', 'fetching', 'noList' ], true ) ) { |
| 86 |
return $ajax_handler->add_response( 'admin_errors', $this->get_invalid_list_message( 'list' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
// Retireve and check API credentials. |
| 90 |
$this->api_key = $this->get_api_param( $form_settings ); |
| 91 |
$this->api_url = $this->get_api_param( $form_settings, 'url' ); |
| 92 |
if ( empty( $this->api_key ) || empty( $this->api_url ) ) { |
| 93 |
return $ajax_handler->add_response( 'admin_errors', "{$this->get_title()}: " . esc_html__( 'Missing API credentials.', 'sellkit' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
// Try subscription. |
| 97 |
$this->ajax_handler = $ajax_handler; |
| 98 |
$subscriber = $this->create_subscriber_object(); |
| 99 |
$this->subscribe( $subscriber, $list_id, $form_settings['activecampaign_tags'] ); |
| 100 |
} |
| 101 |
|
| 102 |
public function get_list( AjaxHandler $ajax_handler, $params ) { |
| 103 |
$this->api_key = $this->get_api_param( $params ); |
| 104 |
$this->api_url = $this->get_api_param( $params, 'url' ); |
| 105 |
|
| 106 |
$results = $this->send_get( 'lists' ); |
| 107 |
$lists = []; |
| 108 |
|
| 109 |
if ( ! empty( $results['body']['lists'] ) ) { |
| 110 |
foreach ( $results['body']['lists'] as $index => $list ) { |
| 111 |
$lists[ $list['id'] ] = $list['name']; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
$list = [ 'lists' => $lists ]; |
| 116 |
|
| 117 |
return $ajax_handler->add_response( 'success', $list ); |
| 118 |
} |
| 119 |
|
| 120 |
public function get_additional_data( AjaxHandler $ajax_handler, $params ) { |
| 121 |
$this->api_key = $this->get_api_param( $params ); |
| 122 |
$this->api_url = $this->get_api_param( $params, 'url' ); |
| 123 |
|
| 124 |
$data = [ |
| 125 |
'custom_fields' => $this->get_remote_custom_fields(), |
| 126 |
'tags' => $this->get_remote_tags(), |
| 127 |
]; |
| 128 |
|
| 129 |
return $ajax_handler->add_response( 'success', $data ); |
| 130 |
} |
| 131 |
|
| 132 |
private function get_remote_custom_fields() { |
| 133 |
$results = $this->send_get( 'fields' ); |
| 134 |
|
| 135 |
$default_fields = $this->get_default_remote_fields()['optional']; |
| 136 |
$custom_fields = []; |
| 137 |
|
| 138 |
if ( empty( $results['body']['fields'] ) ) { |
| 139 |
return $custom_fields; |
| 140 |
} |
| 141 |
|
| 142 |
foreach ( $results['body']['fields'] as $field ) { |
| 143 |
if ( ! array_key_exists( $field['id'], $default_fields ) ) { |
| 144 |
$custom_fields[ $field['id'] ] = $field['title']; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return $custom_fields; |
| 149 |
} |
| 150 |
|
| 151 |
private function get_remote_tags() { |
| 152 |
$results = $this->send_get( 'tags' ); |
| 153 |
$tags = []; |
| 154 |
|
| 155 |
if ( empty( $results['body']['tags'] ) ) { |
| 156 |
return $tags; |
| 157 |
} |
| 158 |
|
| 159 |
foreach ( $results['body']['tags'] as $tag ) { |
| 160 |
$tags[ $tag['id'] ] = $tag['tag']; |
| 161 |
} |
| 162 |
|
| 163 |
return $tags; |
| 164 |
} |
| 165 |
|
| 166 |
protected function get_default_remote_fields() { |
| 167 |
return [ |
| 168 |
'required' => [ |
| 169 |
'email' => esc_html__( 'Email', 'sellkit' ), |
| 170 |
], |
| 171 |
'optional' => [ |
| 172 |
'firstName' => esc_html__( 'First Name', 'sellkit' ), |
| 173 |
'lastName' => esc_html__( 'Last Name', 'sellkit' ), |
| 174 |
'phone' => esc_html__( 'Phone', 'sellkit' ), |
| 175 |
], |
| 176 |
]; |
| 177 |
} |
| 178 |
|
| 179 |
protected function create_subscriber_object() { |
| 180 |
$mapped_fields = $this->get_field_mappings(); |
| 181 |
$default_fields = $this->get_default_remote_fields(); |
| 182 |
|
| 183 |
foreach ( $mapped_fields as $key => $value ) { |
| 184 |
$is_custom = |
| 185 |
! array_key_exists( $key, $default_fields['required'] ) && |
| 186 |
! array_key_exists( $key, $default_fields['optional'] ); |
| 187 |
|
| 188 |
if ( $is_custom ) { |
| 189 |
$subscriber['fieldValues'][] = [ |
| 190 |
'field' => $key, |
| 191 |
'value' => $value, |
| 192 |
]; |
| 193 |
continue; |
| 194 |
} |
| 195 |
|
| 196 |
$subscriber[ $key ] = $value; |
| 197 |
} |
| 198 |
|
| 199 |
return [ 'contact' => $subscriber ]; |
| 200 |
} |
| 201 |
|
| 202 |
protected function subscribe( $subscriber_data, $list_id, $tags ) { |
| 203 |
$args = [ |
| 204 |
'method' => 'POST', |
| 205 |
'timeout' => 100, |
| 206 |
'headers' => $this->get_headers(), |
| 207 |
'body' => wp_json_encode( $subscriber_data ), |
| 208 |
]; |
| 209 |
|
| 210 |
$result = $this->send_post( 'contacts', $args, 'temp_activecampaign' ); |
| 211 |
|
| 212 |
// If subscriber already exists, sync it with new data. |
| 213 |
if ( 422 === $result['code'] ) { |
| 214 |
unset( $this->ajax_handler->response['admin_errors']['temp_activecampaign'] ); |
| 215 |
|
| 216 |
$result = $this->send_post( 'contact/sync', $args ); |
| 217 |
} |
| 218 |
|
| 219 |
if ( isset( $result['body'] ) && isset( $result['body']['contact'] ) ) { |
| 220 |
$subscriber_id = $result['body']['contact']['id']; |
| 221 |
|
| 222 |
$this->add_subscriber_to_list( $subscriber_id, $list_id ); |
| 223 |
$this->add_tags_to_subscriber( $subscriber_id, $tags ); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
private function add_subscriber_to_list( $subscriber_id, $list_id ) { |
| 228 |
$args = [ |
| 229 |
'method' => 'POST', |
| 230 |
'headers' => $this->get_headers(), |
| 231 |
'body' => wp_json_encode( [ |
| 232 |
'contactList' => [ |
| 233 |
'list' => strval( $list_id ), |
| 234 |
'contact' => strval( $subscriber_id ), |
| 235 |
'status' => '1', |
| 236 |
], |
| 237 |
] ), |
| 238 |
]; |
| 239 |
|
| 240 |
return $this->send_post( 'contactLists', $args ); |
| 241 |
} |
| 242 |
|
| 243 |
private function add_tags_to_subscriber( $subscriber_id, $tags ) { |
| 244 |
if ( empty( $tags ) ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
foreach ( $tags as $tag ) { |
| 249 |
$args = [ |
| 250 |
'method' => 'POST', |
| 251 |
'headers' => $this->get_headers(), |
| 252 |
'body' => wp_json_encode( [ |
| 253 |
'contactTag' => [ |
| 254 |
'contact' => $subscriber_id, |
| 255 |
'tag' => $tag, |
| 256 |
], |
| 257 |
] ), |
| 258 |
]; |
| 259 |
|
| 260 |
return $this->send_post( 'contactTags', $args ); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|