| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add Subscriber To List |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\MailPoet\Actions\Add_Subscriber_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_Subscriber_To_List extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'mailpoet'; |
| 15 |
public $action = 'mailpoet_add_subscriber_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 subscriber to a list', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Add <strong>subscriber</strong> to a <strong>list</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Subscriber. %2$s: List. */ |
| 29 |
'edit_label' => sprintf( __( 'Add %1$s to %2$s', 'automatorwp' ), '{subscriber}', '{list}' ), |
| 30 |
/* translators: %1$s: Subscriber. %2$s: List. */ |
| 31 |
'log_label' => sprintf( __( 'Add %1$s to %2$s', 'automatorwp' ), '{subscriber}', '{list}' ), |
| 32 |
'options' => array( |
| 33 |
'subscriber' => array( |
| 34 |
'from' => 'email', |
| 35 |
'default' => __( 'subscriber', 'automatorwp' ), |
| 36 |
'fields' => array( |
| 37 |
'email' => array( |
| 38 |
'name' => __( 'Email:', 'automatorwp' ), |
| 39 |
'type' => 'text', |
| 40 |
), |
| 41 |
'first_name' => array( |
| 42 |
'name' => __( 'First name:', 'automatorwp' ), |
| 43 |
'type' => 'text', |
| 44 |
), |
| 45 |
'last_name' => array( |
| 46 |
'name' => __( 'Last name:', 'automatorwp' ), |
| 47 |
'type' => 'text', |
| 48 |
), |
| 49 |
'status' => array( |
| 50 |
'name' => __( 'Status:', 'automatorwp' ), |
| 51 |
'type' => 'select', |
| 52 |
'options' => array( |
| 53 |
'subscribed' => __( 'Subscribed', 'automatorwp' ), |
| 54 |
'unsubscribed' => __( 'Unsubscribed', 'automatorwp' ), |
| 55 |
'unconfirmed' => __( 'Unconfirmed', 'automatorwp' ), |
| 56 |
'inactive' => __( 'Inactive', 'automatorwp' ), |
| 57 |
'bounced' => __( 'Bounced', 'automatorwp' ), |
| 58 |
), |
| 59 |
'default' => '' |
| 60 |
), |
| 61 |
) |
| 62 |
), |
| 63 |
'list' => array( |
| 64 |
'from' => 'list', |
| 65 |
'fields' => array( |
| 66 |
'list' => array( |
| 67 |
'name' => __( 'List:', 'automatorwp' ), |
| 68 |
'type' => 'select', |
| 69 |
'options_cb' => 'automatorwp_mailpoet_lists_options_cb', |
| 70 |
'option_none' => true, |
| 71 |
'default' => '' |
| 72 |
), |
| 73 |
) |
| 74 |
) |
| 75 |
), |
| 76 |
) ); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Action execution function |
| 82 |
* |
| 83 |
* @since 1.0.0 |
| 84 |
* |
| 85 |
* @param stdClass $action The action object |
| 86 |
* @param int $user_id The user ID |
| 87 |
* @param array $action_options The action's stored options (with tags already passed) |
| 88 |
* @param stdClass $automation The action's automation object |
| 89 |
*/ |
| 90 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 91 |
|
| 92 |
if ( ! class_exists( '\MailPoet\API\API' ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
// Shorthand |
| 97 |
$email = $action_options['email']; |
| 98 |
$first_name = $action_options['first_name']; |
| 99 |
$last_name = $action_options['last_name']; |
| 100 |
$status = $action_options['status']; |
| 101 |
$list_id = $action_options['list']; |
| 102 |
|
| 103 |
// Bail if not list selected |
| 104 |
if( $list_id === '' ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// Get the MailPoet API |
| 109 |
$mailpoet = \MailPoet\API\API::MP( 'v1' ); |
| 110 |
|
| 111 |
// Setup the subscriber data |
| 112 |
$subscriber = array( |
| 113 |
'email' => $email, |
| 114 |
'first_name' => $first_name, |
| 115 |
'last_name' => $last_name, |
| 116 |
'status' => $status, |
| 117 |
); |
| 118 |
|
| 119 |
try { |
| 120 |
// Check if email is already a subscriber |
| 121 |
$existing_subscriber = \MailPoet\Models\Subscriber::findOne( $subscriber['email'] ); |
| 122 |
|
| 123 |
if( $existing_subscriber ) { |
| 124 |
// Add existing subscriber to the list |
| 125 |
$mailpoet->subscribeToList( $existing_subscriber->id, $list_id, array( 'send_confirmation_email' => true ) ); |
| 126 |
} else { |
| 127 |
// Register the new subscriber |
| 128 |
$mailpoet->addSubscriber( $subscriber, array( $list_id ), array( 'send_confirmation_email' => true ) ); |
| 129 |
} |
| 130 |
} catch ( \MailPoet\API\MP\v1\APIException $e ) { |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
new AutomatorWP_MailPoet_Add_Subscriber_To_List(); |