AC_ANNON_REMOVETAG
Class AC_ANNON_REMOVETAG
Source Source
File: src/integrations/active-campaign/actions/ac-annon-removetag.php
class AC_ANNON_REMOVETAG { use \Uncanny_Automator\Recipe\Actions; public $prefix = ''; public function __construct() { $this->prefix = 'AC_ANNON_REMOVETAG'; $this->ac_endpoint_uri = AUTOMATOR_API_URL . 'v2/active-campaign'; // Allow overwrite in wp-config.php. if ( DEFINED( 'UO_AUTOMATOR_DEV_AC_ENDPOINT_URL' ) ) { $this->ac_endpoint_uri = UO_AUTOMATOR_DEV_AC_ENDPOINT_URL; } $this->setup_action(); } /** * Setup Action. * * @return void. */ protected function setup_action() { $this->set_integration( 'ACTIVE_CAMPAIGN' ); $this->set_action_code( $this->prefix . '_CODE' ); $this->set_action_meta( $this->prefix . '_META' ); $this->set_is_pro( false ); $this->set_requires_user( false ); /* translators: Action - WordPress */ $this->set_sentence( sprintf( esc_attr__( 'Remove {{a tag:%1$s}} from {{a contact:%2$s}}', 'uncanny-automator' ), $this->get_action_meta(), $this->prefix . '_CONTACT_ID' . ':' . $this->get_action_meta() ) ); /* translators: Action - WordPress */ $this->set_readable_sentence( esc_attr__( 'Remove {{a tag}} from {{a contact}}', 'uncanny-automator' ) ); $options_group = array( $this->get_action_meta() => array( array( 'option_code' => $this->get_action_meta(), /* translators: Email field */ 'label' => esc_attr__( 'Tag', 'uncanny-automator' ), 'input_type' => 'select', 'supports_custom_value' => false, 'required' => true, 'is_ajax' => true, 'endpoint' => 'active-campaign-list-tags', 'fill_values_in' => $this->get_action_meta(), ), array( 'option_code' => $this->prefix . '_CONTACT_ID', /* translators: Contact field */ 'label' => esc_attr__( 'Email', 'uncanny-automator' ), 'placeholder' => esc_attr__( 'me@domain.com', 'uncanny-automator' ), 'input_type' => 'email', 'required' => true, ), ), ); $this->set_options_group( $options_group ); $this->register_action(); } /** * @param int $user_id * @param array $action_data * @param int $recipe_id * @param array $args * @param $parsed * * @return void. */ protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) { $ac_helper = Automator()->helpers->recipe->active_campaign->options; $contact_email = isset( $parsed[ $this->prefix . '_CONTACT_ID' ] ) ? sanitize_text_field( $parsed[ $this->prefix . '_CONTACT_ID' ] ) : 0; $tag_id = isset( $parsed[ $this->get_action_meta() ] ) ? sanitize_text_field( $parsed[ $this->get_action_meta() ] ) : 0; $contact = $ac_helper->get_user_by_email( $contact_email ); // Get the contact id from email. if ( true === $contact['error'] ) { $action_data['complete_with_errors'] = true; Automator()->complete->action( $user_id, $action_data, $recipe_id, $contact['message'] ); return; } $contact_id = isset( $contact['message']->id ) ? $contact['message']->id : 0; $form_data = array( 'action' => 'get_contact_tags', 'url' => get_option( 'uap_active_campaign_api_url', '' ), 'token' => get_option( 'uap_active_campaign_api_key', '' ), 'contactId' => $contact_id, ); $response = wp_remote_post( $this->ac_endpoint_uri, array( 'body' => $form_data, ) ); if ( is_wp_error( $response ) ) { $action_data['complete_with_errors'] = true; Automator()->complete->action( $user_id, $action_data, $recipe_id, $response->get_error_message() ); return; } $body = json_decode( wp_remote_retrieve_body( $response ) ); $contact_tags = isset( $body->data->contactTags ) ? $body->data->contactTags : ''; $contact_tag_id = 0; if ( ! empty( $contact_tags ) ) { foreach ( $contact_tags as $contact_tag ) { if ( $tag_id === $contact_tag->tag ) { $contact_tag_id = $contact_tag->id; } } } // Delete the tag. $form_data = array( 'action' => 'delete_contact_tag', 'url' => get_option( 'uap_active_campaign_api_url', '' ), 'token' => get_option( 'uap_active_campaign_api_key', '' ), 'contactTagId' => $contact_tag_id, ); $response = wp_remote_post( $this->ac_endpoint_uri, array( 'body' => $form_data, ) ); if ( is_wp_error( $response ) ) { $action_data['complete_with_errors'] = true; Automator()->complete->action( $user_id, $action_data, $recipe_id, $response->get_error_message() ); return; } $body = json_decode( wp_remote_retrieve_body( $response ) ); $message = isset( $body->data->message ) ? $body->data->message : ''; if ( 0 === $contact_tag_id ) { $action_data['complete_with_errors'] = true; $message = __( 'Cannot find the tag. Please check to see if the tag exists.', 'uncanny-automator' ); Automator()->complete->action( $user_id, $action_data, $recipe_id, $message ); return; } if ( ! empty( $message ) ) { $action_data['complete_with_errors'] = true; Automator()->complete->action( $user_id, $action_data, $recipe_id, $message ); return; } Automator()->complete->action( $user_id, $action_data, $recipe_id ); } }
Expand full source code Collapse full source code View on Github
Methods Methods
- __construct
- process_action
- setup_action — Setup Action.