| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Mailpoet Integration |
| 5 |
*/ |
| 6 |
class WeForms_Integration_MailPoet_Free extends WeForms_Abstract_Integration { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
$this->id = 'mailpoet'; |
| 10 |
$this->title = __( 'MailPoet', 'weforms' ); |
| 11 |
$this->icon = WEFORMS_ASSET_URI . '/images/icon-mailpoet.svg'; |
| 12 |
$this->template = __DIR__ . '/component/template.php'; |
| 13 |
|
| 14 |
$this->settings_fields = [ |
| 15 |
'enabled' => false, |
| 16 |
'list' => '', |
| 17 |
'double' => false, |
| 18 |
'fields' => [ |
| 19 |
'email' => '', |
| 20 |
'first_name' => '', |
| 21 |
'last_name' => '', |
| 22 |
], |
| 23 |
]; |
| 24 |
|
| 25 |
add_filter( 'admin_footer', [ $this, 'load_template' ] ); |
| 26 |
add_action( 'wp_ajax_wpuf_mailpoet_fetch_lists', [ $this, 'fetch_lists' ] ); |
| 27 |
add_filter( 'weforms_builder_scripts', [ $this, 'enqueue_mixin' ] ); |
| 28 |
add_action( 'weforms_entry_submission', [ $this, 'subscribe_user' ], 10, 4 ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Enqueue the mixin |
| 33 |
* |
| 34 |
* @param $scritps |
| 35 |
* |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
public function enqueue_mixin( $scripts ) { |
| 39 |
$scripts['weforms-int-mailpoet'] = [ |
| 40 |
'src' => plugins_url( 'component/index.js', __FILE__ ), |
| 41 |
'deps' => [ 'weforms-form-builder-components' ], |
| 42 |
]; |
| 43 |
|
| 44 |
return $scripts; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Fetch Mailpoets saved list from server |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
public function fetch_lists() { |
| 53 |
if ( class_exists( 'WYSIJA' ) ) { |
| 54 |
$mail_poet_lists = WYSIJA::get( 'list', 'model' ); |
| 55 |
$lists = $mail_poet_lists->get( [ 'name', 'list_id' ], [ 'is_enabled' => 1 ] ); |
| 56 |
wp_send_json_success( $lists ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Subscribe a user when a form is submitted |
| 62 |
* |
| 63 |
* @param int $entry_id |
| 64 |
* @param int $form_id |
| 65 |
* @param int $page_id |
| 66 |
* @param array $form_settings |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function subscribe_user( $entry_id, $form_id, $page_id, $form_settings ) { |
| 71 |
if ( !class_exists( 'WYSIJA' ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$integration = weforms_is_integration_active( $form_id, $this->id ); |
| 76 |
|
| 77 |
if ( false === $integration ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
if ( empty( $integration->list ) || empty( $integration->fields->email ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$email = WeForms_Notification::replace_field_tags( $integration->fields->email, $entry_id ); |
| 86 |
|
| 87 |
if ( empty( $email ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$first_name = WeForms_Notification::replace_name_tag( $integration->fields->first_name, $entry_id ); |
| 92 |
$last_name = WeForms_Notification::replace_name_tag( $integration->fields->last_name, $entry_id ); |
| 93 |
|
| 94 |
// Populate data submitted. |
| 95 |
if ( $first_name && 'false' !== $first_name ) { |
| 96 |
$userData = [ 'email' => $email, 'firstname' => $first_name, 'lastname' => $last_name ]; |
| 97 |
} else { |
| 98 |
$userData = [ 'email' => $user->user_email ]; |
| 99 |
} |
| 100 |
|
| 101 |
$data = [ |
| 102 |
'user' => $userData, |
| 103 |
'user_list' => [ 'list_ids' => [ $integration->list ] ], |
| 104 |
]; |
| 105 |
|
| 106 |
// Add subscriber to MailPoet. |
| 107 |
$weHelper = WYSIJA::get( 'user', 'helper' ); |
| 108 |
$weHelper->addSubscriber( $data ); |
| 109 |
} |
| 110 |
} |
| 111 |
|