| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create User Contact |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\MailerPress\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_MailerPress_Create_User_Contact extends AutomatorWP_Integration_Action { |
| 14 |
|
| 15 |
public $integration = 'mailerpress'; |
| 16 |
public $action = 'mailerpress_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 MailerPress', 'automatorwp' ), |
| 29 |
'select_option' => __( 'Add <strong>user</strong> to MailerPress', 'automatorwp' ), |
| 30 |
/* translators: %1$s: Contact. */ |
| 31 |
'edit_label' => sprintf( __( 'Add %1$s to MailerPress', 'automatorwp' ), '{contact}' ), |
| 32 |
/* translators: %1$s: Contact. */ |
| 33 |
'log_label' => sprintf( __( 'Add %1$s to MailerPress', 'automatorwp' ), '{contact}' ), |
| 34 |
'options' => array( |
| 35 |
'contact' => array( |
| 36 |
'default' => __( 'user', 'automatorwp' ), |
| 37 |
'fields' => array( |
| 38 |
'subscribed' => array( |
| 39 |
'name' => __( 'Subscribed:', 'automatorwp' ), |
| 40 |
'desc' => __( 'Mark Contact as subscribed.', 'automatorwp' ), |
| 41 |
'type' => 'checkbox', |
| 42 |
'classes' => 'cmb2-switch', |
| 43 |
), |
| 44 |
) ) |
| 45 |
), |
| 46 |
) ); |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Action execution function |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* |
| 55 |
* @param stdClass $action The action object |
| 56 |
* @param int $user_id The user ID |
| 57 |
* @param array $action_options The action's stored options (with tags already passed) |
| 58 |
* @param stdClass $automation The action's automation object |
| 59 |
*/ |
| 60 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 61 |
|
| 62 |
// Shorthand |
| 63 |
$status = (bool) $action_options['subscribed']; |
| 64 |
$user = get_user_by( 'ID', $user_id ); |
| 65 |
$user_email = $user->user_email; |
| 66 |
|
| 67 |
if( ! class_exists ( 'MailerPress\Models\Contacts' ) ) |
| 68 |
return; |
| 69 |
|
| 70 |
// Get contact data |
| 71 |
$contact_data = new MailerPress\Models\Contacts; |
| 72 |
$contact = $contact_data->getContactByEmail( $user_email ); |
| 73 |
|
| 74 |
// Bail if not contact ID |
| 75 |
if ( $contact ){ |
| 76 |
$this->result = __( 'User is a contact in MailerPress.', 'automatorwp' ); |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
// Status |
| 81 |
$status = $status ? 'subscribed' : 'pending'; |
| 82 |
|
| 83 |
$args = array( |
| 84 |
'email' => $user->user_email, |
| 85 |
'first_name' => $user->first_name, |
| 86 |
'last_name' => $user->last_name, |
| 87 |
'subscription_status' => $status, |
| 88 |
'opt_in_source' => 'manual', |
| 89 |
'created_at' => current_time('mysql'), |
| 90 |
'updated_at' => current_time('mysql'), |
| 91 |
); |
| 92 |
|
| 93 |
$response = automatorwp_mailerpress_add_contact( $args ); |
| 94 |
|
| 95 |
if ( $response ){ |
| 96 |
$this->result = __( 'User added to MailerPress', 'automatorwp' ); |
| 97 |
} else { |
| 98 |
$this->result = __( 'Could not add user to MailerPress', 'automatorwp' ); |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Register required hooks |
| 105 |
* |
| 106 |
* @since 1.0.0 |
| 107 |
*/ |
| 108 |
public function hooks() { |
| 109 |
|
| 110 |
// Log meta data |
| 111 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 112 |
|
| 113 |
// Log fields |
| 114 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 115 |
|
| 116 |
parent::hooks(); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Action custom log meta |
| 122 |
* |
| 123 |
* @since 1.0.0 |
| 124 |
* |
| 125 |
* @param array $log_meta Log meta data |
| 126 |
* @param stdClass $action The action object |
| 127 |
* @param int $user_id The user ID |
| 128 |
* @param array $action_options The action's stored options (with tags already passed) |
| 129 |
* @param stdClass $automation The action's automation object |
| 130 |
* |
| 131 |
* @return array |
| 132 |
*/ |
| 133 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 134 |
|
| 135 |
// Bail if action type don't match this action |
| 136 |
if( $action->type !== $this->action ) { |
| 137 |
return $log_meta; |
| 138 |
} |
| 139 |
|
| 140 |
// Store the action's result |
| 141 |
$log_meta['result'] = $this->result; |
| 142 |
|
| 143 |
return $log_meta; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Action custom log fields |
| 148 |
* |
| 149 |
* @since 1.0.0 |
| 150 |
* |
| 151 |
* @param array $log_fields The log fields |
| 152 |
* @param stdClass $log The log object |
| 153 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 154 |
* |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
public function log_fields( $log_fields, $log, $object ) { |
| 158 |
|
| 159 |
// Bail if log is not assigned to an action |
| 160 |
if( $log->type !== 'action' ) { |
| 161 |
return $log_fields; |
| 162 |
} |
| 163 |
|
| 164 |
// Bail if action type don't match this action |
| 165 |
if( $object->type !== $this->action ) { |
| 166 |
return $log_fields; |
| 167 |
} |
| 168 |
|
| 169 |
$log_fields['result'] = array( |
| 170 |
'name' => __( 'Result:', 'automatorwp' ), |
| 171 |
'type' => 'text', |
| 172 |
); |
| 173 |
|
| 174 |
return $log_fields; |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
new AutomatorWP_MailerPress_Create_User_Contact(); |