templates
1 year ago
SendinblueAccount.php
1 year ago
SendinblueApiClient.php
1 year ago
function.wp_mail.php
8 years ago
http-build-url.php
1 year ago
index.php
8 years ago
mailin.php
3 years ago
push-admin.php
1 year ago
push-amp.php
1 year ago
push-api.php
1 year ago
push-httpclient.php
1 year ago
push-public.php
1 year ago
push-settings.php
1 year ago
push-utils.php
1 year ago
push-woocommerce.php
1 year ago
sendinblue.php
3 years ago
sib-api-manager.php
1 year ago
sib-form-preview.php
2 years ago
sib-sms-code.php
3 years ago
table-forms.php
1 year ago
sendinblue.php
176 lines
| 1 | <?php |
| 2 | class Sendinblue |
| 3 | { |
| 4 | public $api_key; |
| 5 | public $base_url; |
| 6 | public $curl_opts = array(); |
| 7 | |
| 8 | public function __construct($api_key) |
| 9 | { |
| 10 | if (!function_exists('curl_init')) { |
| 11 | throw new Exception('Sendinblue requires CURL module'); |
| 12 | } |
| 13 | $this->base_url = 'https://in-automate.brevo.com/p'; |
| 14 | $this->api_key = $api_key; |
| 15 | //create a session cookie |
| 16 | if (!array_key_exists('session_id', $_COOKIE)) { |
| 17 | $domain = self::get_app_domain(); |
| 18 | //store session_id cookie |
| 19 | $session_id = md5(uniqid(time())); |
| 20 | $expiry_time = self::get_default_cookie_expiry(); |
| 21 | setcookie("session_id", $session_id, $expiry_time, "/", $domain, is_ssl()); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @param $input |
| 27 | * @return mixed |
| 28 | */ |
| 29 | private function do_request($input) |
| 30 | { |
| 31 | $input['key'] = $this->api_key; |
| 32 | $url = $this->base_url . "?" . http_build_query($input); |
| 33 | $data = wp_remote_retrieve_body(wp_remote_request($url, ['method' => 'GET'])); |
| 34 | |
| 35 | return json_decode($data, true); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return string |
| 40 | */ |
| 41 | private static function get_app_domain() |
| 42 | { |
| 43 | $url = get_site_url(); |
| 44 | $parsed_url = parse_url($url); |
| 45 | return !empty($parsed_url['host']) ? $parsed_url['host'] : 'localhost'; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @return int |
| 50 | */ |
| 51 | private static function get_default_cookie_expiry() |
| 52 | { |
| 53 | return time() + 8640; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * @param string $email |
| 59 | * @param int $expiry_time |
| 60 | * @return void |
| 61 | */ |
| 62 | private function set_email_cookie($email) |
| 63 | { |
| 64 | if (is_string($email)) { |
| 65 | $expiry_time = self::get_default_cookie_expiry(); |
| 66 | $domain = self::get_app_domain(); |
| 67 | setcookie("email_id", sanitize_email($email), $expiry_time, "/", $domain, is_ssl()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public function identify($data) |
| 72 | { |
| 73 | $data['sib_type'] = 'identify'; |
| 74 | |
| 75 | if (!array_key_exists('name', $data)) { |
| 76 | $data['name'] = "Contact Created"; |
| 77 | } |
| 78 | $url = esc_url_raw((isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"); |
| 79 | if (!array_key_exists('url', $data)) { |
| 80 | $data['url'] = $url; |
| 81 | } |
| 82 | if (isset($_COOKIE['session_id']) && $_COOKIE['session_id'] != '') { |
| 83 | $data['session_id'] = sanitize_text_field($_COOKIE['session_id']); |
| 84 | } |
| 85 | //store email_id cookie |
| 86 | $this->set_email_cookie($data['email_id']); |
| 87 | return $this->do_request($data); |
| 88 | } |
| 89 | |
| 90 | public function track($data) |
| 91 | { |
| 92 | $data['sib_type'] = 'track'; |
| 93 | $url = esc_url_raw((isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"); |
| 94 | if (!array_key_exists('url', $data)) { |
| 95 | $data['url'] = $url; |
| 96 | } |
| 97 | |
| 98 | if (!array_key_exists('sib_name', $data)) { |
| 99 | if (array_key_exists('name', $data)) { |
| 100 | $data['sib_name'] = $data['name']; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | //get email cookie |
| 105 | |
| 106 | if (isset($_COOKIE['email_id']) && $_COOKIE['email_id'] != '') { |
| 107 | $data['email_id'] = sanitize_email($_COOKIE['email_id']); |
| 108 | } |
| 109 | if (isset($_COOKIE['session_id']) && $_COOKIE['session_id'] != '') { |
| 110 | $data['session_id'] = sanitize_text_field($_COOKIE['session_id']); |
| 111 | } |
| 112 | |
| 113 | //store email cookie |
| 114 | $obj = $this->do_request($data); |
| 115 | if (isset($obj['email_id']) && $obj['email_id'] != '') { |
| 116 | $this->set_email_cookie($obj['email_id']); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public function page($data) |
| 121 | { |
| 122 | $data['sib_type'] = 'page'; |
| 123 | $url = esc_url_raw((isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"); |
| 124 | if (!array_key_exists('url', $data)) { |
| 125 | $data['url'] = $url; |
| 126 | } |
| 127 | //get email cookie |
| 128 | if (isset($_COOKIE['email_id']) && $_COOKIE['email_id'] != '') { |
| 129 | $data['email_id'] = sanitize_email($_COOKIE['email_id']); |
| 130 | } |
| 131 | if (isset($_COOKIE['session_id']) && $_COOKIE['session_id'] != '') { |
| 132 | $data['session_id'] = sanitize_text_field($_COOKIE['session_id']); |
| 133 | } |
| 134 | //referrer |
| 135 | if (!array_key_exists('referrer', $data) && array_key_exists('HTTP_REFERER', $_SERVER)) { |
| 136 | $data['referrer'] = sanitize_text_field( wp_unslash( $_SERVER['HTTP_REFERER'] ) ); |
| 137 | } |
| 138 | //pathname |
| 139 | if (!array_key_exists('pathname', $data)) { |
| 140 | $data['pathname'] = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 141 | } |
| 142 | |
| 143 | //name |
| 144 | if (!array_key_exists('name', $data)) { |
| 145 | $data['name'] = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 146 | } |
| 147 | |
| 148 | //store email cookie |
| 149 | $obj = $this->do_request($data); |
| 150 | if (isset($obj['email_id']) && $obj['email_id'] != '') { |
| 151 | $this->set_email_cookie($obj['email_id']); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | public function trackLink($data) |
| 156 | { |
| 157 | $data['sib_type'] = 'trackLink'; |
| 158 | //get email cookie |
| 159 | if (isset($_COOKIE['email_id']) && $_COOKIE['email_id'] != '') { |
| 160 | $data['email_id'] = sanitize_email($_COOKIE['email_id']); |
| 161 | } |
| 162 | if (isset($_COOKIE['session_id']) && $_COOKIE['session_id'] != '') { |
| 163 | $data['session_id'] = sanitize_text_field($_COOKIE['session_id']); |
| 164 | } |
| 165 | $url = esc_url_raw((isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"); |
| 166 | if (!array_key_exists('url', $data)) { |
| 167 | $data['url'] = $url; |
| 168 | } |
| 169 | //store email cookie |
| 170 | $obj = $this->do_request($data); |
| 171 | if (isset($obj['email_id']) && $obj['email_id'] != '') { |
| 172 | $this->set_email_cookie($obj['email_id']); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 |