← All changes
|
integrations/mail-mint/includes/actions/create-user-contact.php
+195
-0
5.7.9
→
6.0.3
View file →
| @@ -1,0 +1,195 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Create User Contact | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\Mail_Mint\Actions\Create_User_Contact | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | + | |
| 11 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 12 | + | |
| 13 | +class AutomatorWP_Mail_Mint_Create_User_Contact extends AutomatorWP_Integration_Action { | |
| 14 | + | |
| 15 | + public $integration = 'mail_mint'; | |
| 16 | + public $action = 'mail_mint_create_user_contact'; | |
| 17 | + public $result = ''; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Register the trigger | |
| 21 | + * | |
| 22 | + * @since 1.0.0 | |
| 23 | + */ | |
| 24 | + public function register() { | |
| 25 | + | |
| 26 | + automatorwp_register_action( $this->action, array( | |
| 27 | + 'integration' => $this->integration, | |
| 28 | + 'label' => __( 'Add user to Mail Mint', 'automatorwp' ), | |
| 29 | + 'select_option' => __( 'Add <strong>user</strong> to Mail Mint', 'automatorwp' ), | |
| 30 | + /* translators: %1$s: Contact. */ | |
| 31 | + 'edit_label' => sprintf( __( 'Add %1$s to Mail Mint', 'automatorwp' ), '{contact}' ), | |
| 32 | + /* translators: %1$s: Contact. */ | |
| 33 | + 'log_label' => sprintf( __( 'Add %1$s to Mail Mint', 'automatorwp' ), '{contact}' ), | |
| 34 | + 'options' => array( | |
| 35 | + 'contact' => array( | |
| 36 | + 'default' => __( 'user', 'automatorwp' ), | |
| 37 | + 'fields' => array( | |
| 38 | + 'email' => array( | |
| 39 | + 'name' => __( 'User Email:', 'automatorwp' ), | |
| 40 | + 'desc' => __( 'The email of the user to add to Mail Mint. By default, Email of user who completes this automation.', 'automatorwp' ), | |
| 41 | + 'type' => 'text', | |
| 42 | + 'default' => '' | |
| 43 | + ), | |
| 44 | + 'subscribed' => array( | |
| 45 | + 'name' => __( 'Subscribed:', 'automatorwp' ), | |
| 46 | + 'desc' => __( 'Mark Contact as subscribed.', 'automatorwp' ), | |
| 47 | + 'type' => 'checkbox', | |
| 48 | + 'classes' => 'cmb2-switch', | |
| 49 | + ), | |
| 50 | + ) ) | |
| 51 | + ), | |
| 52 | + ) ); | |
| 53 | + | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Action execution function | |
| 58 | + * | |
| 59 | + * @since 1.0.0 | |
| 60 | + * | |
| 61 | + * @param stdClass $action The action object | |
| 62 | + * @param int $user_id The user ID | |
| 63 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 64 | + * @param stdClass $automation The action's automation object | |
| 65 | + */ | |
| 66 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 67 | + | |
| 68 | + // Shorthand | |
| 69 | + $status = (bool) $action_options['subscribed']; | |
| 70 | + $user_email = $action_options['email']; | |
| 71 | + | |
| 72 | + if ( empty( $user_email ) ) { | |
| 73 | + $user = get_user_by( 'id', $user_id ); | |
| 74 | + } else { | |
| 75 | + $user = get_user_by( 'email', $user_email ); | |
| 76 | + | |
| 77 | + if ( empty( $user ) ) { | |
| 78 | + $this->result = __( 'User is not registered', 'automatorwp' ); | |
| 79 | + return; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 83 | + if( ! class_exists ( 'Mint\MRM\DataBase\Models\ContactModel' ) || | |
| 84 | + ! class_exists ( 'Mint\MRM\DataStores\ContactData' ) ) | |
| 85 | + return; | |
| 86 | + | |
| 87 | + // Get contact ID | |
| 88 | + $contact_id = Mint\MRM\DataBase\Models\ContactModel::get_id_by_email( $user->user_email ); | |
| 89 | + | |
| 90 | + // Bail if contact ID | |
| 91 | + if ( $contact_id ){ | |
| 92 | + $this->result = __( 'User is already in Mail Mint.', 'automatorwp' ); | |
| 93 | + return; | |
| 94 | + } | |
| 95 | + | |
| 96 | + // Status | |
| 97 | + $status = $status ? 'subscribed' : 'pending'; | |
| 98 | + | |
| 99 | + $args = array( | |
| 100 | + 'email' => $user->user_email, | |
| 101 | + 'first_name' => $user->first_name, | |
| 102 | + 'last_name' => $user->last_name, | |
| 103 | + 'status' => $status, | |
| 104 | + ); | |
| 105 | + | |
| 106 | + // Create contact object | |
| 107 | + $contact_data = new Mint\MRM\DataStores\ContactData( $user->user_email, $args ); | |
| 108 | + | |
| 109 | + $response = Mint\MRM\DataBase\Models\ContactModel::insert( $contact_data ); | |
| 110 | + | |
| 111 | + if ( $response ){ | |
| 112 | + $this->result = __( 'User added to Mail Mint', 'automatorwp' ); | |
| 113 | + } else { | |
| 114 | + $this->result = __( 'Could not add user to Mail Mint', 'automatorwp' ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * Register required hooks | |
| 121 | + * | |
| 122 | + * @since 1.0.0 | |
| 123 | + */ | |
| 124 | + public function hooks() { | |
| 125 | + | |
| 126 | + // Log meta data | |
| 127 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); | |
| 128 | + | |
| 129 | + // Log fields | |
| 130 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 131 | + | |
| 132 | + parent::hooks(); | |
| 133 | + | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * Action custom log meta | |
| 138 | + * | |
| 139 | + * @since 1.0.0 | |
| 140 | + * | |
| 141 | + * @param array $log_meta Log meta data | |
| 142 | + * @param stdClass $action The action object | |
| 143 | + * @param int $user_id The user ID | |
| 144 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 145 | + * @param stdClass $automation The action's automation object | |
| 146 | + * | |
| 147 | + * @return array | |
| 148 | + */ | |
| 149 | + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 150 | + | |
| 151 | + // Bail if action type don't match this action | |
| 152 | + if( $action->type !== $this->action ) { | |
| 153 | + return $log_meta; | |
| 154 | + } | |
| 155 | + | |
| 156 | + // Store the action's result | |
| 157 | + $log_meta['result'] = $this->result; | |
| 158 | + | |
| 159 | + return $log_meta; | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * Action custom log fields | |
| 164 | + * | |
| 165 | + * @since 1.0.0 | |
| 166 | + * | |
| 167 | + * @param array $log_fields The log fields | |
| 168 | + * @param stdClass $log The log object | |
| 169 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 170 | + * | |
| 171 | + * @return array | |
| 172 | + */ | |
| 173 | + public function log_fields( $log_fields, $log, $object ) { | |
| 174 | + | |
| 175 | + // Bail if log is not assigned to an action | |
| 176 | + if( $log->type !== 'action' ) { | |
| 177 | + return $log_fields; | |
| 178 | + } | |
| 179 | + | |
| 180 | + // Bail if action type don't match this action | |
| 181 | + if( $object->type !== $this->action ) { | |
| 182 | + return $log_fields; | |
| 183 | + } | |
| 184 | + | |
| 185 | + $log_fields['result'] = array( | |
| 186 | + 'name' => __( 'Result:', 'automatorwp' ), | |
| 187 | + 'type' => 'text', | |
| 188 | + ); | |
| 189 | + | |
| 190 | + return $log_fields; | |
| 191 | + } | |
| 192 | + | |
| 193 | +} | |
| 194 | + | |
| 195 | +new AutomatorWP_Mail_Mint_Create_User_Contact(); | |