| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add user sequence |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\ConvertKit\Actions\Add_User_Sequence |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_ConvertKit_Add_User_Sequence extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'convertkit'; |
| 15 |
public $action = 'convertkit_add_user_sequence'; |
| 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 sequence', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Add <strong>user</strong> to <strong>sequence</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Form name. */ |
| 29 |
'edit_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{sequence}' ), |
| 30 |
/* translators: %1$s: Form name. */ |
| 31 |
'log_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{sequence}' ), |
| 32 |
'options' => array( |
| 33 |
'sequence' => automatorwp_utilities_ajax_selector_option( array( |
| 34 |
'field' => 'sequence', |
| 35 |
'option_default' => __( 'Select a sequence', 'automatorwp-hubspot' ), |
| 36 |
'name' => __( 'Sequence:', 'automatorwp-hubspot' ), |
| 37 |
'action_cb' => 'automatorwp_convertkit_get_sequences', |
| 38 |
'options_cb' => 'automatorwp_convertkit_options_cb_sequence', |
| 39 |
'default' => '' |
| 40 |
) ), |
| 41 |
), |
| 42 |
) ); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Action execution function |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
* |
| 52 |
* @param stdClass $action The action object |
| 53 |
* @param int $user_id The user ID |
| 54 |
* @param array $action_options The action's stored options (with tags already passed) |
| 55 |
* @param stdClass $automation The action's automation object |
| 56 |
*/ |
| 57 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 58 |
|
| 59 |
// Shorthand |
| 60 |
$sequence_id = $action_options['sequence']; |
| 61 |
$user = get_user_by ( 'ID', $user_id ); |
| 62 |
|
| 63 |
$this->result = ''; |
| 64 |
|
| 65 |
// Bail if sequence is empty |
| 66 |
if ( empty ( $sequence_id ) ){ |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
// Bail if ConvertKit not configured |
| 71 |
if( ! automatorwp_convertkit_get_api() ) { |
| 72 |
$this->result = __( 'ConvertKit integration not configured in AutomatorWP settings.', 'automatorwp' ); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$status_code = automatorwp_convertkit_add_subscriber_sequence( $sequence_id, $user->user_email, $user->first_name ); |
| 77 |
|
| 78 |
if ( !isset( $status_code ) || $status_code !== 200 ) { |
| 79 |
$this->result = __( 'Unexpected error. The subscriber has not been added.', 'automatorwp' ); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$sequence_name = automatorwp_convertkit_get_sequence_name( $sequence_id ); |
| 84 |
|
| 85 |
$this->result = sprintf( __( '%s added to %s', 'automatorwp' ), $user->user_email, $sequence_name ); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Register required hooks |
| 91 |
* |
| 92 |
* @since 1.0.0 |
| 93 |
*/ |
| 94 |
public function hooks() { |
| 95 |
|
| 96 |
// Configuration notice |
| 97 |
add_filter( 'automatorwp_automation_ui_after_item_label', array( $this, 'configuration_notice' ), 10, 2 ); |
| 98 |
|
| 99 |
// Log meta data |
| 100 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 101 |
|
| 102 |
// Log fields |
| 103 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 104 |
|
| 105 |
parent::hooks(); |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Configuration notice |
| 111 |
* |
| 112 |
* @since 1.0.0 |
| 113 |
* |
| 114 |
* @param stdClass $object The trigger/action object |
| 115 |
* @param string $item_type The object type (trigger|action) |
| 116 |
*/ |
| 117 |
public function configuration_notice( $object, $item_type ) { |
| 118 |
|
| 119 |
// Bail if action type don't match this action |
| 120 |
if( $item_type !== 'action' ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
if( $object->type !== $this->action ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
// Warn user if the authorization has not been setup from settings |
| 129 |
if( ! automatorwp_convertkit_get_api() ) : ?> |
| 130 |
<div class="automatorwp-notice-warning" style="margin-top: 10px; margin-bottom: 0;"> |
| 131 |
<?php echo sprintf( |
| 132 |
__( 'You need to configure the <a href="%s" target="_blank">ConvertKit settings</a> to get this action to work.', 'automatorwp' ), |
| 133 |
get_admin_url() . 'admin.php?page=automatorwp_settings&tab=opt-tab-convertkit' |
| 134 |
); ?> |
| 135 |
<?php echo sprintf( |
| 136 |
__( '<a href="%s" target="_blank">Documentation</a>', 'automatorwp' ), |
| 137 |
'https://automatorwp.com/docs/convertkit/' |
| 138 |
); ?> |
| 139 |
</div> |
| 140 |
<?php endif; |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Action custom log meta |
| 146 |
* |
| 147 |
* @since 1.0.0 |
| 148 |
* |
| 149 |
* @param array $log_meta Log meta data |
| 150 |
* @param stdClass $action The action object |
| 151 |
* @param int $user_id The user ID |
| 152 |
* @param array $action_options The action's stored options (with tags already passed) |
| 153 |
* @param stdClass $automation The action's automation object |
| 154 |
* |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 158 |
|
| 159 |
// Bail if action type don't match this action |
| 160 |
if( $action->type !== $this->action ) { |
| 161 |
return $log_meta; |
| 162 |
} |
| 163 |
|
| 164 |
// Store the action's result |
| 165 |
$log_meta['result'] = $this->result; |
| 166 |
|
| 167 |
return $log_meta; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Action custom log fields |
| 172 |
* |
| 173 |
* @since 1.0.0 |
| 174 |
* |
| 175 |
* @param array $log_fields The log fields |
| 176 |
* @param stdClass $log The log object |
| 177 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 178 |
* |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
public function log_fields( $log_fields, $log, $object ) { |
| 182 |
|
| 183 |
// Bail if log is not assigned to an action |
| 184 |
if( $log->type !== 'action' ) { |
| 185 |
return $log_fields; |
| 186 |
} |
| 187 |
|
| 188 |
// Bail if action type don't match this action |
| 189 |
if( $object->type !== $this->action ) { |
| 190 |
return $log_fields; |
| 191 |
} |
| 192 |
|
| 193 |
$log_fields['result'] = array( |
| 194 |
'name' => __( 'Result:', 'automatorwp' ), |
| 195 |
'type' => 'text', |
| 196 |
); |
| 197 |
|
| 198 |
return $log_fields; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
new AutomatorWP_ConvertKit_Add_User_Sequence(); |