← All changes
|
integrations/activecampaign/includes/actions/add-user.php
+193
-0
5.7.2
→
6.0.3
View file →
| @@ -1,0 +1,193 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Add User | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\ActiveCampaign\Actions\Add_User | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_ActiveCampaign_Add_User extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + public $integration = 'activecampaign'; | |
| 15 | + public $action = 'activecampaign_add_user'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Register the trigger | |
| 19 | + * | |
| 20 | + * @since 1.0.0 | |
| 21 | + */ | |
| 22 | + public function register() { | |
| 23 | + | |
| 24 | + automatorwp_register_action( $this->action, array( | |
| 25 | + 'integration' => $this->integration, | |
| 26 | + 'label' => __( 'Add user to ActiveCampaign', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'Add <strong>user</strong> to ActiveCampaign', 'automatorwp' ), | |
| 28 | + 'edit_label' => __( 'Add user to ActiveCampaign', 'automatorwp' ), | |
| 29 | + 'log_label' => __( 'Add user to ActiveCampaign', 'automatorwp' ), | |
| 30 | + 'options' => array( | |
| 31 | + // No options | |
| 32 | + ), | |
| 33 | + ) ); | |
| 34 | + | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Action execution function | |
| 39 | + * | |
| 40 | + * @since 1.0.0 | |
| 41 | + * | |
| 42 | + * @param stdClass $action The action object | |
| 43 | + * @param int $user_id The user ID | |
| 44 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 45 | + * @param stdClass $automation The action's automation object | |
| 46 | + */ | |
| 47 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 48 | + | |
| 49 | + $user = get_user_by ( 'ID', $user_id ); | |
| 50 | + | |
| 51 | + // Bail if ActiveCampaign not configured | |
| 52 | + if( ! automatorwp_activecampaign_get_api() ) { | |
| 53 | + $this->result = __( 'ActiveCampaign integration not configured in AutomatorWP settings.', 'automatorwp' ); | |
| 54 | + return; | |
| 55 | + } | |
| 56 | + | |
| 57 | + $response = automatorwp_activecampaign_get_contact( $user->user_email ); | |
| 58 | + | |
| 59 | + // Bail if user exists in ActiveCampaign | |
| 60 | + if ( ! empty( $response['contacts'] ) ) { | |
| 61 | + | |
| 62 | + $this->result = sprintf( __( '%s is already a contact in ActiveCampaign', 'automatorwp' ), $user->user_email ); | |
| 63 | + return; | |
| 64 | + | |
| 65 | + } | |
| 66 | + | |
| 67 | + $contact_data = array( | |
| 68 | + 'email' => $user->user_email, | |
| 69 | + 'firstName' => $user->first_name, | |
| 70 | + 'lastName' => $user->last_name, | |
| 71 | + ); | |
| 72 | + | |
| 73 | + automatorwp_activecampaign_add_contact( $contact_data ); | |
| 74 | + | |
| 75 | + $this->result = sprintf( __( '%s added to ActiveCampaign', 'automatorwp' ), $user->user_email ); | |
| 76 | + | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Register required hooks | |
| 81 | + * | |
| 82 | + * @since 1.0.0 | |
| 83 | + */ | |
| 84 | + public function hooks() { | |
| 85 | + | |
| 86 | + // Configuration notice | |
| 87 | + add_filter( 'automatorwp_automation_ui_after_item_label', array( $this, 'configuration_notice' ), 10, 2 ); | |
| 88 | + | |
| 89 | + // Log meta data | |
| 90 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); | |
| 91 | + | |
| 92 | + // Log fields | |
| 93 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 94 | + | |
| 95 | + parent::hooks(); | |
| 96 | + | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * Configuration notice | |
| 101 | + * | |
| 102 | + * @since 1.0.0 | |
| 103 | + * | |
| 104 | + * @param stdClass $object The trigger/action object | |
| 105 | + * @param string $item_type The object type (trigger|action) | |
| 106 | + */ | |
| 107 | + public function configuration_notice( $object, $item_type ) { | |
| 108 | + | |
| 109 | + // Bail if action type don't match this action | |
| 110 | + if( $item_type !== 'action' ) { | |
| 111 | + return; | |
| 112 | + } | |
| 113 | + | |
| 114 | + if( $object->type !== $this->action ) { | |
| 115 | + return; | |
| 116 | + } | |
| 117 | + | |
| 118 | + // Warn user if the authorization has not been setup from settings | |
| 119 | + if( ! automatorwp_activecampaign_get_api() ) : ?> | |
| 120 | + <div class="automatorwp-notice-warning" style="margin-top: 10px; margin-bottom: 0;"> | |
| 121 | + <?php echo sprintf( | |
| 122 | + __( 'You need to configure the <a href="%s" target="_blank">ActiveCampaign settings</a> to get this action to work.', 'automatorwp' ), | |
| 123 | + get_admin_url() . 'admin.php?page=automatorwp_settings&tab=opt-tab-activecampaign' | |
| 124 | + ); ?> | |
| 125 | + <?php echo sprintf( | |
| 126 | + __( '<a href="%s" target="_blank">Documentation</a>', 'automatorwp' ), | |
| 127 | + 'https://automatorwp.com/docs/activecampaign/' | |
| 128 | + ); ?> | |
| 129 | + </div> | |
| 130 | + <?php endif; | |
| 131 | + | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Action custom log meta | |
| 136 | + * | |
| 137 | + * @since 1.0.0 | |
| 138 | + * | |
| 139 | + * @param array $log_meta Log meta data | |
| 140 | + * @param stdClass $action The action object | |
| 141 | + * @param int $user_id The user ID | |
| 142 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 143 | + * @param stdClass $automation The action's automation object | |
| 144 | + * | |
| 145 | + * @return array | |
| 146 | + */ | |
| 147 | + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 148 | + | |
| 149 | + // Bail if action type don't match this action | |
| 150 | + if( $action->type !== $this->action ) { | |
| 151 | + return $log_meta; | |
| 152 | + } | |
| 153 | + | |
| 154 | + // Store the action's result | |
| 155 | + $log_meta['result'] = $this->result; | |
| 156 | + | |
| 157 | + return $log_meta; | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Action custom log fields | |
| 162 | + * | |
| 163 | + * @since 1.0.0 | |
| 164 | + * | |
| 165 | + * @param array $log_fields The log fields | |
| 166 | + * @param stdClass $log The log object | |
| 167 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 168 | + * | |
| 169 | + * @return array | |
| 170 | + */ | |
| 171 | + public function log_fields( $log_fields, $log, $object ) { | |
| 172 | + | |
| 173 | + // Bail if log is not assigned to an action | |
| 174 | + if( $log->type !== 'action' ) { | |
| 175 | + return $log_fields; | |
| 176 | + } | |
| 177 | + | |
| 178 | + // Bail if action type don't match this action | |
| 179 | + if( $object->type !== $this->action ) { | |
| 180 | + return $log_fields; | |
| 181 | + } | |
| 182 | + | |
| 183 | + $log_fields['result'] = array( | |
| 184 | + 'name' => __( 'Result:', 'automatorwp' ), | |
| 185 | + 'type' => 'text', | |
| 186 | + ); | |
| 187 | + | |
| 188 | + return $log_fields; | |
| 189 | + } | |
| 190 | + | |
| 191 | +} | |
| 192 | + | |
| 193 | +new AutomatorWP_ActiveCampaign_Add_User(); | |