| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add User |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\AWeber\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 |
|
| 13 |
class AutomatorWP_AWeber_Add_User_List extends AutomatorWP_Integration_Action { |
| 14 |
|
| 15 |
public $integration = 'aweber'; |
| 16 |
public $action = 'aweber_add_user_list'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Store the action result |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
* |
| 23 |
* @var string $result |
| 24 |
*/ |
| 25 |
public $result = ''; |
| 26 |
|
| 27 |
/** |
| 28 |
* Register the trigger |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
*/ |
| 32 |
public function register() { |
| 33 |
|
| 34 |
automatorwp_register_action( $this->action, array( |
| 35 |
'integration' => $this->integration, |
| 36 |
'label' => __( 'Add user to list', 'automatorwp' ), |
| 37 |
'select_option' => __( 'Add <strong>user</strong> to list', 'automatorwp' ), |
| 38 |
/* translators: %1$s: List. */ |
| 39 |
'edit_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{list}' ), |
| 40 |
/* translators: %1$s: List. */ |
| 41 |
'log_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{list}' ), |
| 42 |
'options' => array( |
| 43 |
'list' => array( |
| 44 |
'from' => 'lists', |
| 45 |
'default' => __( 'list', 'automatorwp' ), |
| 46 |
'fields' => array( |
| 47 |
'account' => automatorwp_utilities_ajax_selector_field( array( |
| 48 |
'name' => __( 'Account:', 'automatorwp' ), |
| 49 |
'option_none' => false, |
| 50 |
'action_cb' => 'automatorwp_aweber_get_accounts', |
| 51 |
'options_cb' => 'automatorwp_aweber_options_cb_account', |
| 52 |
'placeholder' => 'Select an account', |
| 53 |
'default' => '' |
| 54 |
) ), |
| 55 |
'lists' => automatorwp_utilities_ajax_selector_field( array( |
| 56 |
'name' => __( 'List:', 'automatorwp' ), |
| 57 |
'option_none' => false, |
| 58 |
'action_cb' => 'automatorwp_aweber_get_lists', |
| 59 |
'options_cb' => 'automatorwp_aweber_options_cb_list', |
| 60 |
'placeholder' => 'Select a list', |
| 61 |
'default' => '' |
| 62 |
) ), |
| 63 |
), |
| 64 |
) |
| 65 |
), |
| 66 |
) ); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Action execution function |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* |
| 75 |
* @param stdClass $action The action object |
| 76 |
* @param int $user_id The user ID |
| 77 |
* @param array $action_options The action's stored options (with tags already passed) |
| 78 |
* @param stdClass $automation The action's automation object |
| 79 |
*/ |
| 80 |
public function execute( $action, $user_id, $action_options, $automation){ |
| 81 |
|
| 82 |
$params = automatorwp_aweber_get_request_parameters(); |
| 83 |
|
| 84 |
// Bail if the authorization has not been setup from settings |
| 85 |
if( $params === false ) { |
| 86 |
$this->result = __( 'AWeber integration not configured in AutomatorWP settings.', 'automatorwp' ); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Shorthand |
| 91 |
$user = get_user_by ( 'ID', $user_id ); |
| 92 |
$account_id = $action_options['account']; |
| 93 |
$list_id = $action_options['lists']; |
| 94 |
|
| 95 |
$subscriber_data = array( |
| 96 |
'email' => $user->user_email, |
| 97 |
'name' => $user->first_name . ' ' .$user->last_name, |
| 98 |
'update_existing' => true, |
| 99 |
); |
| 100 |
|
| 101 |
$response = automatorwp_aweber_add_subscriber( $subscriber_data, $account_id, $list_id ); |
| 102 |
|
| 103 |
if ( isset( $response['error'] ) ){ |
| 104 |
$this->result = sprintf( __( '%s could not be added to the list in AWeber. Error: %s, %s', 'automatorwp' ), $user->user_email, $response['error']['status'], $response['error']['message'] ); |
| 105 |
} else { |
| 106 |
$this->result = sprintf( __( '%s added to list in AWeber', 'automatorwp' ), $user->user_email ); |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Register required hooks |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
*/ |
| 116 |
public function hooks() { |
| 117 |
|
| 118 |
// Log meta data |
| 119 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 120 |
|
| 121 |
// Log fields |
| 122 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 123 |
|
| 124 |
parent::hooks(); |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Action custom log meta |
| 130 |
* |
| 131 |
* @since 1.0.0 |
| 132 |
* |
| 133 |
* @param array $log_meta Log meta data |
| 134 |
* @param stdClass $action The action object |
| 135 |
* @param int $user_id The user ID |
| 136 |
* @param array $action_options The action's stored options (with tags already passed) |
| 137 |
* @param stdClass $automation The action's automation object |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 142 |
|
| 143 |
// Bail if action type don't match this action |
| 144 |
if( $action->type !== $this->action ) { |
| 145 |
return $log_meta; |
| 146 |
} |
| 147 |
|
| 148 |
$log_meta['result'] = $this->result; |
| 149 |
|
| 150 |
return $log_meta; |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Action custom log fields |
| 156 |
* |
| 157 |
* @since 1.0.0 |
| 158 |
* |
| 159 |
* @param array $log_fields The log fields |
| 160 |
* @param stdClass $log The log object |
| 161 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 162 |
* |
| 163 |
* @return array |
| 164 |
*/ |
| 165 |
public function log_fields( $log_fields, $log, $object ) { |
| 166 |
|
| 167 |
// Bail if log is not assigned to an action |
| 168 |
if( $log->type !== 'action' ) { |
| 169 |
return $log_fields; |
| 170 |
} |
| 171 |
|
| 172 |
// Bail if action type don't match this action |
| 173 |
if( $object->type !== $this->action ) { |
| 174 |
return $log_fields; |
| 175 |
} |
| 176 |
|
| 177 |
$log_fields['result'] = array( |
| 178 |
'name' => __( 'Result:', 'automatorwp' ), |
| 179 |
'type' => 'text', |
| 180 |
); |
| 181 |
|
| 182 |
return $log_fields; |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
new AutomatorWP_AWeber_Add_User_List(); |