images
7 years ago
activecampaign.php
5 months ago
hustle-activecampaign-api.php
5 months ago
hustle-activecampaign-form-hooks.php
5 months ago
hustle-activecampaign-form-settings.php
5 months ago
hustle-activecampaign.php
3 years ago
hustle-activecampaign-api.php
390 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_Activecampaign_Api class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * ActiveCampaign API implementation |
| 10 | * |
| 11 | * Class Hustle_Activecampaign_Api |
| 12 | */ |
| 13 | class Hustle_Activecampaign_Api { |
| 14 | |
| 15 | /** |
| 16 | * URL |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | private $url; |
| 21 | /** |
| 22 | * Key |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private $key; |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | * |
| 31 | * @param string $url URL. |
| 32 | * @param string $api_key Api key. |
| 33 | */ |
| 34 | public function __construct( $url, $api_key ) { |
| 35 | $this->url = trailingslashit( $url ) . 'admin/api.php'; |
| 36 | $this->key = $api_key; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Sends request to the endpoint url with the provided $action |
| 41 | * |
| 42 | * @param string $action rest action. |
| 43 | * @param string $verb Verb. |
| 44 | * @param array $args Args. |
| 45 | * @return object|WP_Error |
| 46 | * @throws Exception Failed to process request. |
| 47 | */ |
| 48 | private function request( $action, $verb = 'GET', $args = array() ) { |
| 49 | |
| 50 | $utils = Hustle_Provider_Utils::get_instance(); |
| 51 | |
| 52 | $url = $this->url; |
| 53 | |
| 54 | $args = array_merge( |
| 55 | array( |
| 56 | 'api_action' => $action, |
| 57 | 'api_output' => 'json', |
| 58 | ), |
| 59 | $args |
| 60 | ); |
| 61 | |
| 62 | $headers = array( |
| 63 | 'Content-Type' => 'application/x-www-form-urlencoded', |
| 64 | 'API-TOKEN' => $this->key, |
| 65 | ); |
| 66 | |
| 67 | $_args = array( |
| 68 | 'method' => $verb, |
| 69 | 'headers' => $headers, |
| 70 | ); |
| 71 | |
| 72 | if ( 'GET' === $verb ) { |
| 73 | $url .= ( '?' . http_build_query( $args ) ); |
| 74 | } else { |
| 75 | $_args['body'] = $args; |
| 76 | } |
| 77 | |
| 78 | $utils->last_url_request = $url; |
| 79 | $utils->last_data_sent = $args; |
| 80 | |
| 81 | $res = wp_remote_request( $url, $_args ); |
| 82 | |
| 83 | if ( is_wp_error( $res ) || ! $res ) { |
| 84 | Opt_In_Utils::maybe_log( __METHOD__, $res ); |
| 85 | throw new Exception( |
| 86 | esc_html__( 'Failed to process request, make sure your API URL and API KEY are correct and your server has internet connection.', 'hustle' ) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | if ( isset( $res['response']['code'] ) ) { |
| 91 | $status_code = $res['response']['code']; |
| 92 | $msg = ''; |
| 93 | if ( $status_code > 400 ) { |
| 94 | if ( isset( $res['response']['message'] ) ) { |
| 95 | $msg = $res['response']['message']; |
| 96 | } |
| 97 | |
| 98 | /* translators: error message */ |
| 99 | throw new Exception( sprintf( esc_html__( 'Failed to processing request : %s', 'hustle' ), esc_html( $msg ) ) ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | $body = wp_remote_retrieve_body( $res ); |
| 104 | |
| 105 | // probably silent mode. |
| 106 | if ( ! empty( $body ) ) { |
| 107 | $res = json_decode( $body, true ); |
| 108 | |
| 109 | // auto validate. |
| 110 | if ( ! empty( $res ) ) { |
| 111 | // list_field_view may return empty when there are no custom fields, so we shouldn't throw an exception. |
| 112 | if ( ( ! isset( $res['result_code'] ) || 1 !== $res['result_code'] ) && 'list_field_view' !== $action ) { |
| 113 | $message = ''; |
| 114 | if ( isset( $res['result_message'] ) && ! empty( $res['result_message'] ) ) { |
| 115 | $message = ' ' . $res['result_message']; |
| 116 | } |
| 117 | /* translators: error message */ |
| 118 | throw new Exception( sprintf( esc_html__( 'Failed to get ActiveCampaign data. %s', 'hustle' ), esc_html( $message ) ) ); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | $utils->last_data_received = $res; |
| 124 | |
| 125 | return $res; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Sends rest GET request |
| 130 | * |
| 131 | * @param string $action Action. |
| 132 | * @param array $args Args. |
| 133 | * @return array|mixed|object|WP_Error |
| 134 | */ |
| 135 | private function get( $action, $args = array() ) { |
| 136 | return $this->request( $action, 'GET', $args ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sends rest POST request |
| 141 | * |
| 142 | * @param string $action Action. |
| 143 | * @param array $args Args. |
| 144 | * @return array|mixed|object|WP_Error |
| 145 | */ |
| 146 | private function post( $action, $args = array() ) { |
| 147 | return $this->request( $action, 'POST', $args ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Retrieves lists as array of objects |
| 152 | * |
| 153 | * @return array|WP_Error |
| 154 | */ |
| 155 | public function get_lists() { |
| 156 | |
| 157 | try { |
| 158 | $res = $this->get( |
| 159 | 'list_list', |
| 160 | array( |
| 161 | 'ids' => 'all', |
| 162 | 'global_fields' => 0, |
| 163 | ) |
| 164 | ); |
| 165 | |
| 166 | $res2 = array(); |
| 167 | foreach ( $res as $key => $value ) { |
| 168 | if ( is_numeric( $key ) ) { |
| 169 | array_push( $res2, $value ); |
| 170 | } |
| 171 | } |
| 172 | } catch ( Exception $e ) { |
| 173 | return array(); |
| 174 | } |
| 175 | |
| 176 | return $res2; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get Account Detail |
| 181 | * |
| 182 | * @since 4.1 |
| 183 | * |
| 184 | * @return array|mixed|object |
| 185 | */ |
| 186 | public function get_account() { |
| 187 | |
| 188 | return $this->get( 'account_view' ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Retrieves Custom fields |
| 193 | * |
| 194 | * @return array|WP_Error |
| 195 | */ |
| 196 | public function get_custom_fields() { |
| 197 | $res = $this->get( |
| 198 | 'list_field_view', |
| 199 | array( |
| 200 | 'ids' => 'all', |
| 201 | ) |
| 202 | ); |
| 203 | |
| 204 | if ( is_wp_error( $res ) || ! is_array( $res ) ) { |
| 205 | return $res; |
| 206 | } |
| 207 | |
| 208 | $custom_fields = array(); |
| 209 | |
| 210 | if ( isset( $res['result_code'] ) && 0 !== $res['result_code'] ) { |
| 211 | foreach ( $res as $key => $value ) { |
| 212 | if ( is_numeric( $key ) ) { |
| 213 | array_push( $custom_fields, $value ); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return $custom_fields; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get the existing forms |
| 223 | * |
| 224 | * @return array |
| 225 | */ |
| 226 | public function get_forms() { |
| 227 | |
| 228 | $res2 = array(); |
| 229 | try { |
| 230 | $res = $this->get( 'form_getforms' ); |
| 231 | |
| 232 | foreach ( $res as $key => $value ) { |
| 233 | if ( is_numeric( $key ) ) { |
| 234 | array_push( $res2, $value ); |
| 235 | } |
| 236 | } |
| 237 | } catch ( Exception $e ) { |
| 238 | return array(); |
| 239 | } |
| 240 | |
| 241 | return $res2; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Add new contact |
| 246 | * |
| 247 | * @param string $id ID of the List or Form to which the user will be subscribed to. |
| 248 | * @param array $data with the subscription data. |
| 249 | * @param Hustle_Module_Model $module Module. |
| 250 | * @param array $orig_data Original data. |
| 251 | * @param string $sign_up_to Indicates if the subscription is done to a Form or to a List. |
| 252 | * |
| 253 | * @return array|mixed|object|WP_Error |
| 254 | */ |
| 255 | public function subscribe( $id, array $data, Hustle_Module_Model $module, $orig_data, $sign_up_to = 'list' ) { |
| 256 | if ( false === $this->email_exist( $data['email'], $id, $sign_up_to ) ) { |
| 257 | if ( 'list' === $sign_up_to ) { |
| 258 | if ( (int) $id > 0 ) { |
| 259 | $data['instantresponders'] = array( $id => 1 ); |
| 260 | $data['noresponders'] = array( $id => 0 ); |
| 261 | |
| 262 | $data['p'] = array( $id => $id ); |
| 263 | $data['status'] = array( $id => 1 ); |
| 264 | $res = $this->post( 'contact_sync', $data ); |
| 265 | } else { |
| 266 | $res = $this->post( 'contact_add', $data ); |
| 267 | } |
| 268 | } else { |
| 269 | $data['form'] = $id; |
| 270 | $res = $this->post( 'contact_sync', $data ); |
| 271 | } |
| 272 | |
| 273 | if ( is_array( $res ) && isset( $res['result_code'] ) && 'SUCCESS' === $res['result_code'] ) { |
| 274 | return __( 'Successful subscription', 'hustle' ); |
| 275 | } elseif ( empty( $res ) ) { |
| 276 | return __( 'Successful subscription', 'hustle' ); |
| 277 | } |
| 278 | } else { |
| 279 | $res = $this->post( 'contact_sync', $data ); |
| 280 | } |
| 281 | |
| 282 | if ( is_array( $res ) && isset( $res['result_code'] ) ) { |
| 283 | if ( 'FAILED' === $res['result_code'] ) { |
| 284 | $orig_data['error'] = ! empty( $res['result_message'] ) ? $res['result_message'] : __( 'Unexpected error occurred.', 'hustle' ); |
| 285 | $module->log_error( $orig_data ); |
| 286 | return $orig_data['error']; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return $res; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Delete subscriber from the list |
| 295 | * |
| 296 | * @param string $list_id List ID. |
| 297 | * @param string $email Email. |
| 298 | * @param string $sign_up_to Indicates if the subscription is done to a Form or to a List. |
| 299 | * |
| 300 | * @return bool |
| 301 | */ |
| 302 | public function delete_email( $list_id, $email, $sign_up_to ) { |
| 303 | if ( empty( $sign_up_to ) ) { |
| 304 | $sign_up_to = 'list'; |
| 305 | } |
| 306 | |
| 307 | $data = array( |
| 308 | 'email' => $email, |
| 309 | 'status' => array( $list_id => 2 ), |
| 310 | 'p' => array( $list_id => $list_id ), |
| 311 | ); |
| 312 | |
| 313 | if ( 'form' === $sign_up_to ) { |
| 314 | $forms = $this->get_forms(); |
| 315 | $lists = wp_list_pluck( $forms, 'lists', 'id' ); |
| 316 | if ( empty( $lists[ $list_id ] ) || ! is_array( $lists[ $list_id ] ) ) { |
| 317 | return false; |
| 318 | } |
| 319 | $data['status'] = array_fill_keys( $lists[ $list_id ], 2 ); |
| 320 | $data['p'] = array_combine( $lists[ $list_id ], $lists[ $list_id ] ); |
| 321 | } |
| 322 | |
| 323 | $res = $this->post( 'contact_sync', $data ); |
| 324 | |
| 325 | return ! isset( $res['result_code'] ) || 'FAILED' !== $res['result_code']; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Checks email in a list |
| 330 | * |
| 331 | * @param string $email Email. |
| 332 | * @param string $id ID. |
| 333 | * @param string $type Type. |
| 334 | * @return boolean |
| 335 | */ |
| 336 | public function email_exist( $email, $id, $type = 'list' ) { |
| 337 | |
| 338 | try { |
| 339 | |
| 340 | $res = $this->get( 'contact_view_email', array( 'email' => $email ) ); |
| 341 | |
| 342 | // See if duplicate exists. |
| 343 | if ( |
| 344 | ! empty( $res ) |
| 345 | && ! empty( $res['id'] ) |
| 346 | && ! empty( $res['lists'] ) |
| 347 | ) { |
| 348 | if ( 'list' === $type ) { |
| 349 | // Also make sure duplicate is in active list. |
| 350 | foreach ( $res['lists'] as $response_list ) { |
| 351 | if ( $response_list['listid'] === $id ) { |
| 352 | // Duplicate exists. |
| 353 | return true; |
| 354 | } |
| 355 | } |
| 356 | } elseif ( $id === $res['formid'] ) { // Or active form if checking on a form. |
| 357 | return true; |
| 358 | } |
| 359 | } |
| 360 | } catch ( Exception $e ) { |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | // Otherwise assume no duplicate. |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Add custom filed |
| 370 | * |
| 371 | * @param array $custom_fields Custom fields. |
| 372 | */ |
| 373 | public function add_custom_fields( $custom_fields ) { |
| 374 | if ( ! empty( $custom_fields ) ) { |
| 375 | foreach ( $custom_fields as $key => $value ) { |
| 376 | |
| 377 | $field_data = array( |
| 378 | 'title' => $value['label'], |
| 379 | 'type' => $value['type'], // support for text and date field. |
| 380 | 'perstag' => $key, |
| 381 | 'p[0]' => 0, |
| 382 | 'req' => 0, |
| 383 | ); |
| 384 | |
| 385 | $this->post( 'list_field_add', $field_data ); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 |