| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add User To List |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\MailPoet\Actions\Add_User_To_List |
| 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_MailPoet_Add_User_To_List extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'mailpoet'; |
| 15 |
public $action = 'mailpoet_add_user_to_list'; |
| 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 a list', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Add user to a <strong>list</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: List. */ |
| 29 |
'edit_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{list}' ), |
| 30 |
/* translators: %1$s: List. */ |
| 31 |
'log_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{list}' ), |
| 32 |
'options' => array( |
| 33 |
'list' => array( |
| 34 |
'from' => 'list', |
| 35 |
'fields' => array( |
| 36 |
'list' => array( |
| 37 |
'name' => __( 'List:', 'automatorwp' ), |
| 38 |
'type' => 'select', |
| 39 |
'options_cb' => 'automatorwp_mailpoet_lists_options_cb', |
| 40 |
'option_none' => true, |
| 41 |
'default' => '' |
| 42 |
), |
| 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 |
if ( ! class_exists( '\MailPoet\API\API' ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// Shorthand |
| 67 |
$list_id = $action_options['list']; |
| 68 |
|
| 69 |
// Bail if not list selected |
| 70 |
if( $list_id === '' ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Get the MailPoet API |
| 75 |
$mailpoet = \MailPoet\API\API::MP( 'v1' ); |
| 76 |
|
| 77 |
// Get the user to find its subscriber |
| 78 |
$user = get_userdata( $user_id ); |
| 79 |
|
| 80 |
try { |
| 81 |
// Get the subscriber |
| 82 |
$subscriber = $mailpoet->getSubscriber( $user->user_email ); |
| 83 |
|
| 84 |
// Subscribe him to the list |
| 85 |
$mailpoet->subscribeToList( $subscriber['id'], $list_id, array( 'send_confirmation_email' => true ) ); |
| 86 |
} catch ( \MailPoet\API\MP\v1\APIException $e ) { |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
new AutomatorWP_MailPoet_Add_User_To_List(); |