← All changes
|
integrations/activecampaign/includes/functions.php
+385
-0
5.7.9
→
6.0.3
View file →
| @@ -1,0 +1,385 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\ActiveCampaign\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Helper function to get the ActiveCampaign API parameters | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @return array|false | |
| 17 | + */ | |
| 18 | +function automatorwp_activecampaign_get_api() { | |
| 19 | + | |
| 20 | + $url = automatorwp_activecampaign_get_option( 'url', '' ); | |
| 21 | + $key = automatorwp_activecampaign_get_option( 'key', '' ); | |
| 22 | + | |
| 23 | + if( empty( $url ) || empty( $key ) ) { | |
| 24 | + return false; | |
| 25 | + } | |
| 26 | + | |
| 27 | + return array( | |
| 28 | + 'url' => $url, | |
| 29 | + 'key' => $key, | |
| 30 | + ); | |
| 31 | + | |
| 32 | +} | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * Get ActiveCampaign webhook URL. | |
| 36 | + * | |
| 37 | + * @since 1.0.0 | |
| 38 | + * | |
| 39 | + * @return string | |
| 40 | + */ | |
| 41 | +function automatorwp_activecampaign_get_webhook_url() { | |
| 42 | + | |
| 43 | + $prefix = 'automatorwp_activecampaign_'; | |
| 44 | + $settings = get_option( 'automatorwp_settings', array() ); | |
| 45 | + | |
| 46 | + $webhook_url = ( isset( $settings[$prefix . 'webhook'] ) ) ? $settings[$prefix . 'webhook'] : ''; | |
| 47 | + | |
| 48 | + // Check if webhook URL exists | |
| 49 | + if ( empty( $webhook_url ) ){ | |
| 50 | + | |
| 51 | + $slug = strtolower( wp_generate_password( 8, false ) ); | |
| 52 | + $webhook_url = get_rest_url() . 'activecampaign/webhooks/' . $slug; | |
| 53 | + | |
| 54 | + $settings[$prefix . 'webhook'] = $webhook_url; | |
| 55 | + $settings[$prefix . 'slug'] = $slug; | |
| 56 | + | |
| 57 | + // Update settings | |
| 58 | + update_option( 'automatorwp_settings', $settings ); | |
| 59 | + | |
| 60 | + } | |
| 61 | + | |
| 62 | + return $webhook_url; | |
| 63 | + | |
| 64 | +} | |
| 65 | + | |
| 66 | +/** | |
| 67 | + * Get ActiveCampaign webhook slug. | |
| 68 | + * | |
| 69 | + * @since 1.0.0 | |
| 70 | + * | |
| 71 | + * @return string | |
| 72 | + */ | |
| 73 | + | |
| 74 | +function automatorwp_activecampaign_get_webhook_slug() { | |
| 75 | + | |
| 76 | + $prefix = 'automatorwp_activecampaign_'; | |
| 77 | + $settings = get_option( 'automatorwp_settings' ); | |
| 78 | + | |
| 79 | + $slug = $settings[$prefix . 'slug']; | |
| 80 | + | |
| 81 | + // Check if webhook slug exists | |
| 82 | + if ( empty( $slug ) ){ | |
| 83 | + | |
| 84 | + $slug = strtolower( wp_generate_password( 8, false ) ); | |
| 85 | + $webhook_url = get_rest_url() . 'activecampaign/webhooks/' . $slug; | |
| 86 | + | |
| 87 | + $settings[$prefix . 'webhook'] = $webhook_url; | |
| 88 | + $settings[$prefix . 'slug'] = $slug; | |
| 89 | + | |
| 90 | + // Update settings | |
| 91 | + update_option( 'automatorwp_settings', $settings ); | |
| 92 | + | |
| 93 | + } | |
| 94 | + | |
| 95 | + return $slug; | |
| 96 | + | |
| 97 | +} | |
| 98 | + | |
| 99 | +/** | |
| 100 | + * Get ActiveCampaign webhook URL. | |
| 101 | + * | |
| 102 | + * @since 1.0.0 | |
| 103 | + * | |
| 104 | + * @return string | |
| 105 | + */ | |
| 106 | +function automatorwp_activecampaign_trigger_notice() { | |
| 107 | + $webhook_url = automatorwp_activecampaign_get_webhook_url(); | |
| 108 | + | |
| 109 | + // Warn user about copy the webhook url at ActiveCampaign | |
| 110 | + ?> | |
| 111 | + <div class="automatorwp-notice-warning" style="margin-top: 10px; margin-bottom: 0;"> | |
| 112 | + <?php echo sprintf( | |
| 113 | + __( 'It is required to configure a webhook in your ActiveCampaign account to make this trigger work.', 'automatorwp' ), | |
| 114 | + get_admin_url() . 'admin.php?page=automatorwp_settings&tab=opt-tab-activecampaign' | |
| 115 | + ); ?> | |
| 116 | + <?php echo sprintf( | |
| 117 | + __( '<a href="%s" target="_blank">Documentation</a>', 'automatorwp' ), | |
| 118 | + 'https://automatorwp.com/docs/activecampaign/' | |
| 119 | + ); ?> | | |
| 120 | + <?php echo __( '<a href="#" class="automatorwp-view-webhook">View Webhook</a>', 'automatorwp' ); ?> | |
| 121 | + </div> | |
| 122 | + | |
| 123 | + <div class="automatorwp-option-form-container" style="display:none"> | |
| 124 | + <div class="cmb2-wrap form-table automatorwp-form automatorwp-option-form"> | |
| 125 | + <div id="cmb2-metabox-webhook_form" class="cmb2-metabox cmb-field-list"> | |
| 126 | + <div class="cmb-row cmb-type-text cmb2-id-url table-layout automatorwp-webhooks-url" data-fieldtype="text"> | |
| 127 | + <div class="cmb-th"> | |
| 128 | + <label for="url"><?php echo __('URL:', 'automatorwp'); ?></label> | |
| 129 | + </div> | |
| 130 | + <div class="cmb-td"> | |
| 131 | + <input type="text" class="regular-text" name="" id="url" value=<?php echo $webhook_url; ?> readonly data-option="url"> | |
| 132 | + <p class="cmb2-metabox-description"><?php echo __('Copy this URL in your ActiveCampaign account.', 'automatorwp'); ?></p> | |
| 133 | + </div> | |
| 134 | + </div> | |
| 135 | + </div> | |
| 136 | + </div> | |
| 137 | + <button type="button" class="button button-primary automatorwp-cancel-option-form"><?php echo __('Hide Webhook', 'automatorwp'); ?></button> | |
| 138 | + </div> | |
| 139 | + | |
| 140 | + <?php | |
| 141 | +} | |
| 142 | + | |
| 143 | +/** | |
| 144 | + * Add contact to ActiveCampaign | |
| 145 | + * | |
| 146 | + * @since 1.0.0 | |
| 147 | + * | |
| 148 | + * @param array $contact The contact data | |
| 149 | + */ | |
| 150 | +function automatorwp_activecampaign_add_contact( $contact ) { | |
| 151 | + | |
| 152 | + $api = automatorwp_activecampaign_get_api(); | |
| 153 | + | |
| 154 | + if( ! $api ) { | |
| 155 | + return; | |
| 156 | + } | |
| 157 | + | |
| 158 | + $response = wp_remote_post( $api['url'] . '/api/3/contacts', array( | |
| 159 | + 'headers' => array( | |
| 160 | + 'Accept' => 'application/json', | |
| 161 | + 'Api-Token' => $api['key'], | |
| 162 | + 'Content-Type' => 'application/json' | |
| 163 | + ), | |
| 164 | + 'body' => json_encode( array( | |
| 165 | + 'contact' => array( | |
| 166 | + 'email' => $contact['email'], | |
| 167 | + 'firstName' => ( isset( $contact['firstName'] ) ? $contact['firstName'] : '' ), | |
| 168 | + 'lastName' => ( isset( $contact['lastName'] ) ? $contact['lastName'] : '' ), | |
| 169 | + 'phone' => ( isset( $contact['phone'] ) ? $contact['phone'] : '' ) | |
| 170 | + ) | |
| 171 | + ) ) | |
| 172 | + ) ); | |
| 173 | + | |
| 174 | +} | |
| 175 | + | |
| 176 | +/** | |
| 177 | + * Get ActiveCampaign contact data. Filter email | |
| 178 | + * | |
| 179 | + * @since 1.0.0 | |
| 180 | + * | |
| 181 | + * @param string $email The contact email | |
| 182 | + * | |
| 183 | + * @return array | |
| 184 | + */ | |
| 185 | +function automatorwp_activecampaign_get_contact( $email ) { | |
| 186 | + | |
| 187 | + $api = automatorwp_activecampaign_get_api(); | |
| 188 | + | |
| 189 | + if( ! $api ) { | |
| 190 | + return array( 'contacts' => array() ); | |
| 191 | + } | |
| 192 | + | |
| 193 | + // To manage special characters in email; | |
| 194 | + $email = urlencode( $email ); | |
| 195 | + | |
| 196 | + $response = wp_remote_get( $api['url'] . '/api/3/contacts?email=' . $email, array( | |
| 197 | + 'headers' => array( | |
| 198 | + 'Accept' => 'application/json', | |
| 199 | + 'Api-Token' => $api['key'], | |
| 200 | + 'Content-Type' => 'application/json' | |
| 201 | + ) | |
| 202 | + ) ); | |
| 203 | + | |
| 204 | + $response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 205 | + | |
| 206 | + return $response; | |
| 207 | + | |
| 208 | +} | |
| 209 | + | |
| 210 | +/** | |
| 211 | + * Get tags from ActiveCampaign | |
| 212 | + * | |
| 213 | + * @since 1.0.0 | |
| 214 | + * | |
| 215 | + * @param stdClass $field | |
| 216 | + * | |
| 217 | + * @return array | |
| 218 | + */ | |
| 219 | +function automatorwp_activecampaign_options_cb_tag( $field ) { | |
| 220 | + | |
| 221 | + // Setup vars | |
| 222 | + $value = $field->escaped_value; | |
| 223 | + $none_value = 'any'; | |
| 224 | + $none_label = __( 'any tag', 'automatorwp' ); | |
| 225 | + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); | |
| 226 | + | |
| 227 | + if( ! empty( $value ) ) { | |
| 228 | + if( ! is_array( $value ) ) { | |
| 229 | + $value = array( $value ); | |
| 230 | + } | |
| 231 | + | |
| 232 | + foreach( $value as $tag_id ) { | |
| 233 | + | |
| 234 | + // Skip option none | |
| 235 | + if( $tag_id === $none_value ) { | |
| 236 | + continue; | |
| 237 | + } | |
| 238 | + | |
| 239 | + $options[$tag_id] = automatorwp_activecampaign_get_tag_name( $tag_id ); | |
| 240 | + } | |
| 241 | + } | |
| 242 | + | |
| 243 | + return $options; | |
| 244 | + | |
| 245 | +} | |
| 246 | + | |
| 247 | +/** | |
| 248 | + * Get tags from ActiveCampaign | |
| 249 | + * | |
| 250 | + * @since 1.0.0 | |
| 251 | + * | |
| 252 | + * @param string $search | |
| 253 | + * @param int $page | |
| 254 | + * | |
| 255 | + * @return array | |
| 256 | + */ | |
| 257 | +function automatorwp_activecampaign_get_tags( $search = '', $page = 1 ) { | |
| 258 | + | |
| 259 | + $tags = array(); | |
| 260 | + | |
| 261 | + $api = automatorwp_activecampaign_get_api(); | |
| 262 | + | |
| 263 | + if( ! $api ) { | |
| 264 | + return $tags; | |
| 265 | + } | |
| 266 | + | |
| 267 | + $limit = 20; | |
| 268 | + $offset = $limit * ($page - 1); | |
| 269 | + | |
| 270 | + $response = wp_remote_get( $api['url'] . '/api/3/tags', array( | |
| 271 | + 'headers' => array( | |
| 272 | + 'Accept' => 'application/json', | |
| 273 | + 'Api-Token' => $api['key'], | |
| 274 | + 'Content-Type' => 'application/json' | |
| 275 | + ), | |
| 276 | + 'body' => array( | |
| 277 | + 'search' => $search, | |
| 278 | + 'limit' => $limit, | |
| 279 | + 'offset' => $offset, | |
| 280 | + ) | |
| 281 | + ) ); | |
| 282 | + | |
| 283 | + $response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 284 | + | |
| 285 | + foreach ( $response['tags'] as $tag_id ){ | |
| 286 | + $tags[] = array( | |
| 287 | + 'id' => $tag_id['id'], | |
| 288 | + 'name' => $tag_id['tag'], | |
| 289 | + ); | |
| 290 | + | |
| 291 | + } | |
| 292 | + | |
| 293 | + return $tags; | |
| 294 | + | |
| 295 | +} | |
| 296 | + | |
| 297 | +/** | |
| 298 | + * Get tag name | |
| 299 | + * | |
| 300 | + * @since 1.0.0 | |
| 301 | + * | |
| 302 | + * @param int $tag_id ID tag | |
| 303 | + * | |
| 304 | + */ | |
| 305 | +function automatorwp_activecampaign_get_tag_name( $tag_id ) { | |
| 306 | + | |
| 307 | + $api = automatorwp_activecampaign_get_api(); | |
| 308 | + | |
| 309 | + if( ! $api ) { | |
| 310 | + return ''; | |
| 311 | + } | |
| 312 | + | |
| 313 | + $response = wp_remote_get( $api['url'] . '/api/3/tags/' . $tag_id, array( | |
| 314 | + 'headers' => array( | |
| 315 | + 'Accept' => 'application/json', | |
| 316 | + 'Api-Token' => $api['key'], | |
| 317 | + 'Content-Type' => 'application/json' | |
| 318 | + ) | |
| 319 | + ) ); | |
| 320 | + | |
| 321 | + $response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 322 | + | |
| 323 | + return $response['tag']['tag']; | |
| 324 | + | |
| 325 | +} | |
| 326 | + | |
| 327 | +/** | |
| 328 | + * Add tag to contact | |
| 329 | + * | |
| 330 | + * @since 1.0.0 | |
| 331 | + * | |
| 332 | + * @param string $contact_id Contact ID | |
| 333 | + * @param string $tag_id Tag ID | |
| 334 | + */ | |
| 335 | +function automatorwp_activecampaign_add_contact_tag( $contact_id, $tag_id ){ | |
| 336 | + | |
| 337 | + $api = automatorwp_activecampaign_get_api(); | |
| 338 | + | |
| 339 | + if( ! $api ) { | |
| 340 | + return; | |
| 341 | + } | |
| 342 | + | |
| 343 | + $response = wp_remote_post( $api['url'] . '/api/3/contactTags', array( | |
| 344 | + 'headers' => array( | |
| 345 | + 'Accept' => 'application/json', | |
| 346 | + 'Api-Token' => $api['key'], | |
| 347 | + 'Content-Type' => 'application/json' | |
| 348 | + ), | |
| 349 | + 'body' => json_encode( array( | |
| 350 | + 'contactTag' => array( | |
| 351 | + 'contact' => $contact_id, | |
| 352 | + 'tag' => $tag_id, | |
| 353 | + ) | |
| 354 | + ) ) | |
| 355 | + )); | |
| 356 | + | |
| 357 | +} | |
| 358 | + | |
| 359 | +/** | |
| 360 | + * Get ActiveCampaign contact tags. | |
| 361 | + * | |
| 362 | + * @since 1.0.0 | |
| 363 | + * | |
| 364 | + * @param string $contact_id ID contact | |
| 365 | + */ | |
| 366 | +function automatorwp_activecampaign_get_contact_tags( $contact_id ){ | |
| 367 | + | |
| 368 | + $api = automatorwp_activecampaign_get_api(); | |
| 369 | + | |
| 370 | + if( ! $api ) { | |
| 371 | + return array( 'contactTags' => array() ); | |
| 372 | + } | |
| 373 | + | |
| 374 | + $response = wp_remote_get( $api['url'] . '/api/3/contacts/' . $contact_id . '/contactTags', array( | |
| 375 | + 'headers' => array( | |
| 376 | + 'Accept' => 'application/json', | |
| 377 | + 'Api-Token' => $api['key'], | |
| 378 | + 'Content-Type' => 'application/json' | |
| 379 | + ) | |
| 380 | + )); | |
| 381 | + | |
| 382 | + $response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 383 | + | |
| 384 | + return $response; | |
| 385 | +} | |