| 1 |
<?php |
| 2 |
namespace WPFunnels\Integrations; |
| 3 |
|
| 4 |
use Mint\MRM\DataStores\ContactData; |
| 5 |
|
| 6 |
use Mint\MRM\DataBase\Models\ContactModel; |
| 7 |
use Mint\MRM\Admin\API\Controllers\MessageController; |
| 8 |
use Mint\MRM\DataBase\Models\ContactGroupModel; |
| 9 |
|
| 10 |
/** |
| 11 |
* MailMint class for managing contact creation, updates, and associations with lists and tags. |
| 12 |
* |
| 13 |
* @since 3.2.0 |
| 14 |
*/ |
| 15 |
class MailMint { |
| 16 |
|
| 17 |
/** |
| 18 |
* Contact data array. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
* @since 3.2.0 |
| 22 |
*/ |
| 23 |
protected $contact_data = []; |
| 24 |
|
| 25 |
/** |
| 26 |
* Lists array. |
| 27 |
* |
| 28 |
* @var array |
| 29 |
* @since 3.2.0 |
| 30 |
*/ |
| 31 |
protected $lists = []; |
| 32 |
|
| 33 |
/** |
| 34 |
* Tags array. |
| 35 |
* |
| 36 |
* @var array |
| 37 |
* @since 3.2.0 |
| 38 |
*/ |
| 39 |
protected $tags = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* Contact ID. |
| 43 |
* |
| 44 |
* @var int |
| 45 |
* @since 3.2.0 |
| 46 |
*/ |
| 47 |
protected $contact_id; |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructor to initialize contact data, lists, and tags. |
| 51 |
* |
| 52 |
* @param array $contact_data Contact data. |
| 53 |
* @param array $lists Lists to associate with the contact. |
| 54 |
* @param array $tags Tags to associate with the contact. |
| 55 |
* |
| 56 |
* @since 3.2.0 |
| 57 |
*/ |
| 58 |
public function __construct( $contact_data = [], $lists = [], $tags = [] ) { |
| 59 |
$this->contact_data = $contact_data; |
| 60 |
$this->lists = $lists; |
| 61 |
$this->tags = $tags; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Create or update a contact based on provided contact data. |
| 67 |
* |
| 68 |
* This method checks if the contact exists and updates it or creates a new contact. |
| 69 |
* It also handles double opt-in if configured. |
| 70 |
* |
| 71 |
* @since 3.2.0 |
| 72 |
*/ |
| 73 |
public function create_or_update_contact(){ |
| 74 |
if( Helper::maybe_enabled() && !empty( $this->contact_data['email'] ) ){ |
| 75 |
|
| 76 |
$settings = get_option( '_mint_integration_settings', array( |
| 77 |
'zero_bounce' => array( |
| 78 |
'api_key' => '', |
| 79 |
'email_address' => '', |
| 80 |
'is_integrated' => false, |
| 81 |
), |
| 82 |
) ); |
| 83 |
|
| 84 |
$zero_bounce = isset( $settings['zero_bounce'] ) ? $settings['zero_bounce'] : array(); |
| 85 |
$api_key = isset( $zero_bounce['api_key'] ) ? $zero_bounce['api_key'] : ''; |
| 86 |
$is_integrated = isset( $zero_bounce['is_integrated'] ) ? $zero_bounce['is_integrated'] : false; |
| 87 |
|
| 88 |
if( $is_integrated ) { |
| 89 |
$ch = curl_init(); |
| 90 |
curl_setopt($ch, CURLOPT_URL, 'https://api.zerobounce.net/v2/validate?api_key='.$api_key.'&email='.$this->contact_data['email'].'&ip_address=null'); |
| 91 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 92 |
|
| 93 |
$output = curl_exec($ch); |
| 94 |
|
| 95 |
curl_close($ch); |
| 96 |
|
| 97 |
$response = json_decode($output, true); |
| 98 |
|
| 99 |
if( isset( $response['status'] ) && 'valid' !== $response['status'] ){ |
| 100 |
return; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$exist = ContactModel::is_contact_exist( $this->contact_data['email'] ); |
| 105 |
if( ! $exist ){ |
| 106 |
$contact = new ContactData( $this->contact_data['email'], $this->contact_data ); |
| 107 |
$this->contact_id = ContactModel::insert( $contact ); |
| 108 |
} else { |
| 109 |
// Get existing contact ID |
| 110 |
$existing_contact = ContactModel::get_contact_by_email( $this->contact_data['email'] ); |
| 111 |
if ( $existing_contact ) { |
| 112 |
$this->contact_id = $existing_contact['id']; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if ( isset( $this->contact_data['status'] ) && 'pending' === $this->contact_data['status'] ) { |
| 117 |
MessageController::get_instance()->send_double_opt_in( $this->contact_id ); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Add contact to specified lists. |
| 124 |
* |
| 125 |
* This method associates the contact with the provided lists. |
| 126 |
* |
| 127 |
* @since 3.2.0 |
| 128 |
*/ |
| 129 |
public function add_contact_to_lists(){ |
| 130 |
if( !empty( $this->lists ) && $this->contact_id ){ |
| 131 |
ContactGroupModel::set_lists_to_contact( $this->lists, $this->contact_id ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Add contact to specified tags. |
| 137 |
* |
| 138 |
* This method associates the contact with the provided tags. |
| 139 |
* |
| 140 |
* @since 3.2.0 |
| 141 |
*/ |
| 142 |
public function add_contact_to_tags(){ |
| 143 |
if( !empty( $this->tags ) && $this->contact_id ){ |
| 144 |
ContactGroupModel::set_tags_to_contact( $this->tags, $this->contact_id ); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|