| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add User |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\MailerLite\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_MailerLite_Add_User extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'mailerlite'; |
| 15 |
public $action = 'mailerlite_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/Update user to MailerLite', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Add/Update <strong>user</strong> to MailerLite', 'automatorwp' ), |
| 28 |
'edit_label' => __( 'Add/Update user to MailerLite', 'automatorwp' ), |
| 29 |
'log_label' => __( 'Add/Update user to MailerLite', '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 MailerLite not configured |
| 52 |
if( ! automatorwp_mailerlite_get_api() ) { |
| 53 |
$this->result = __( 'MailerLite integration not configured in AutomatorWP settings.', 'automatorwp' ); |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$subscriber_data = array( |
| 58 |
'email' => $user->user_email, |
| 59 |
'first_name' => $user->first_name, |
| 60 |
'last_name' => $user->last_name, |
| 61 |
); |
| 62 |
|
| 63 |
$response = automatorwp_mailerlite_add_subscriber( $subscriber_data ); |
| 64 |
|
| 65 |
if ( $response === 201 ) { |
| 66 |
|
| 67 |
$this->result = sprintf( __( '%s added to MailerLite', 'automatorwp' ), $user->user_email ); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
if ( $response === 200 ) { |
| 72 |
|
| 73 |
$this->result = sprintf( __( '%s updated in MailerLite', 'automatorwp' ), $user->user_email ); |
| 74 |
|
| 75 |
} |
| 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_mailerlite_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">MailerLite settings</a> to get this action to work.', 'automatorwp' ), |
| 123 |
get_admin_url() . 'admin.php?page=automatorwp_settings&tab=opt-tab-mailerlite' |
| 124 |
); ?> |
| 125 |
<?php echo sprintf( |
| 126 |
__( '<a href="%s" target="_blank">Documentation</a>', 'automatorwp' ), |
| 127 |
'https://automatorwp.com/docs/mailerlite/' |
| 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_MailerLite_Add_User(); |