$url, 'key' => $key, ); } /** * Get ActiveCampaign webhook URL. * * @since 1.0.0 * * @return string */ function automatorwp_activecampaign_get_webhook_url() { $prefix = 'automatorwp_activecampaign_'; $settings = get_option( 'automatorwp_settings', array() ); $webhook_url = ( isset( $settings[$prefix . 'webhook'] ) ) ? $settings[$prefix . 'webhook'] : ''; // Check if webhook URL exists if ( empty( $webhook_url ) ){ $slug = strtolower( wp_generate_password( 8, false ) ); $webhook_url = get_rest_url() . 'activecampaign/webhooks/' . $slug; $settings[$prefix . 'webhook'] = $webhook_url; $settings[$prefix . 'slug'] = $slug; // Update settings update_option( 'automatorwp_settings', $settings ); } return $webhook_url; } /** * Get ActiveCampaign webhook slug. * * @since 1.0.0 * * @return string */ function automatorwp_activecampaign_get_webhook_slug() { $prefix = 'automatorwp_activecampaign_'; $settings = get_option( 'automatorwp_settings' ); $slug = $settings[$prefix . 'slug']; // Check if webhook slug exists if ( empty( $slug ) ){ $slug = strtolower( wp_generate_password( 8, false ) ); $webhook_url = get_rest_url() . 'activecampaign/webhooks/' . $slug; $settings[$prefix . 'webhook'] = $webhook_url; $settings[$prefix . 'slug'] = $slug; // Update settings update_option( 'automatorwp_settings', $settings ); } return $slug; } /** * Get ActiveCampaign webhook URL. * * @since 1.0.0 * * @return string */ function automatorwp_activecampaign_trigger_notice() { $webhook_url = automatorwp_activecampaign_get_webhook_url(); // Warn user about copy the webhook url at ActiveCampaign ?>
Documentation', 'automatorwp' ), 'https://automatorwp.com/docs/activecampaign/' ); ?> | View Webhook', 'automatorwp' ); ?>
array( 'Accept' => 'application/json', 'Api-Token' => $api['key'], 'Content-Type' => 'application/json' ), 'body' => json_encode( array( 'contact' => array( 'email' => $contact['email'], 'firstName' => ( isset( $contact['firstName'] ) ? $contact['firstName'] : '' ), 'lastName' => ( isset( $contact['lastName'] ) ? $contact['lastName'] : '' ), 'phone' => ( isset( $contact['phone'] ) ? $contact['phone'] : '' ) ) ) ) ) ); } /** * Get ActiveCampaign contact data. Filter email * * @since 1.0.0 * * @param string $email The contact email * * @return array */ function automatorwp_activecampaign_get_contact( $email ) { $api = automatorwp_activecampaign_get_api(); if( ! $api ) { return array( 'contacts' => array() ); } // To manage special characters in email; $email = urlencode( $email ); $response = wp_remote_get( $api['url'] . '/api/3/contacts?email=' . $email, array( 'headers' => array( 'Accept' => 'application/json', 'Api-Token' => $api['key'], 'Content-Type' => 'application/json' ) ) ); $response = json_decode( wp_remote_retrieve_body( $response ), true ); return $response; } /** * Get tags from ActiveCampaign * * @since 1.0.0 * * @param stdClass $field * * @return array */ function automatorwp_activecampaign_options_cb_tag( $field ) { // Setup vars $value = $field->escaped_value; $none_value = 'any'; $none_label = __( 'any tag', 'automatorwp' ); $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); if( ! empty( $value ) ) { if( ! is_array( $value ) ) { $value = array( $value ); } foreach( $value as $tag_id ) { // Skip option none if( $tag_id === $none_value ) { continue; } $options[$tag_id] = automatorwp_activecampaign_get_tag_name( $tag_id ); } } return $options; } /** * Get tags from ActiveCampaign * * @since 1.0.0 * * @param string $search * @param int $page * * @return array */ function automatorwp_activecampaign_get_tags( $search = '', $page = 1 ) { $tags = array(); $api = automatorwp_activecampaign_get_api(); if( ! $api ) { return $tags; } $limit = 20; $offset = $limit * ($page - 1); $response = wp_remote_get( $api['url'] . '/api/3/tags', array( 'headers' => array( 'Accept' => 'application/json', 'Api-Token' => $api['key'], 'Content-Type' => 'application/json' ), 'body' => array( 'search' => $search, 'limit' => $limit, 'offset' => $offset, ) ) ); $response = json_decode( wp_remote_retrieve_body( $response ), true ); foreach ( $response['tags'] as $tag_id ){ $tags[] = array( 'id' => $tag_id['id'], 'name' => $tag_id['tag'], ); } return $tags; } /** * Get tag name * * @since 1.0.0 * * @param int $tag_id ID tag * */ function automatorwp_activecampaign_get_tag_name( $tag_id ) { $api = automatorwp_activecampaign_get_api(); if( ! $api ) { return ''; } $response = wp_remote_get( $api['url'] . '/api/3/tags/' . $tag_id, array( 'headers' => array( 'Accept' => 'application/json', 'Api-Token' => $api['key'], 'Content-Type' => 'application/json' ) ) ); $response = json_decode( wp_remote_retrieve_body( $response ), true ); return $response['tag']['tag']; } /** * Add tag to contact * * @since 1.0.0 * * @param string $contact_id Contact ID * @param string $tag_id Tag ID */ function automatorwp_activecampaign_add_contact_tag( $contact_id, $tag_id ){ $api = automatorwp_activecampaign_get_api(); if( ! $api ) { return; } $response = wp_remote_post( $api['url'] . '/api/3/contactTags', array( 'headers' => array( 'Accept' => 'application/json', 'Api-Token' => $api['key'], 'Content-Type' => 'application/json' ), 'body' => json_encode( array( 'contactTag' => array( 'contact' => $contact_id, 'tag' => $tag_id, ) ) ) )); } /** * Get ActiveCampaign contact tags. * * @since 1.0.0 * * @param string $contact_id ID contact */ function automatorwp_activecampaign_get_contact_tags( $contact_id ){ $api = automatorwp_activecampaign_get_api(); if( ! $api ) { return array( 'contactTags' => array() ); } $response = wp_remote_get( $api['url'] . '/api/3/contacts/' . $contact_id . '/contactTags', array( 'headers' => array( 'Accept' => 'application/json', 'Api-Token' => $api['key'], 'Content-Type' => 'application/json' ) )); $response = json_decode( wp_remote_retrieve_body( $response ), true ); return $response; }