| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to handle all database interactions for the Ultimate WP Mail plugin |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( !defined( 'ABSPATH' ) ) |
| 7 |
exit; |
| 8 |
|
| 9 |
if ( !class_exists( 'ewduwpmDatabaseManager' ) ) { |
| 10 |
class ewduwpmDatabaseManager { |
| 11 |
|
| 12 |
|
| 13 |
// Array containing the arguments for the query |
| 14 |
public $args = array(); |
| 15 |
|
| 16 |
// table names |
| 17 |
public $send_events_table_name; |
| 18 |
public $open_events_table_name; |
| 19 |
public $links_clicked_events_table_name; |
| 20 |
public $email_only_users_table_name; |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
global $wpdb; |
| 24 |
|
| 25 |
$this->send_events_table_name = $wpdb->prefix . 'ewd_uwpm_email_send_events'; |
| 26 |
$this->open_events_table_name = $wpdb->prefix . 'ewd_uwpm_email_open_events'; |
| 27 |
$this->links_clicked_events_table_name = $wpdb->prefix . 'ewd_uwpm_email_links_clicked_events'; |
| 28 |
$this->email_only_users_table_name = $wpdb->prefix . 'ewd_uwpm_email_only_users'; |
| 29 |
|
| 30 |
add_action( 'init', array( $this, 'log_user_last_activity' ) ); |
| 31 |
|
| 32 |
add_action( 'init', array( $this, 'maybe_log_email_interaction' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Creates the tables used to store event and user information |
| 37 |
* @since 1.0.0 |
| 38 |
*/ |
| 39 |
public function create_tables() { |
| 40 |
|
| 41 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 42 |
|
| 43 |
$sql = "CREATE TABLE $this->send_events_table_name ( |
| 44 |
Email_Send_ID mediumint(9) NOT NULL AUTO_INCREMENT, |
| 45 |
Email_ID mediumint(9) DEFAULT 0 NOT NULL, |
| 46 |
User_ID mediumint(9) DEFAULT 0 NOT NULL, |
| 47 |
Event_ID mediumint(9) DEFAULT 0 NOT NULL, |
| 48 |
Email_Unique_Identifier text DEFAULT '' NOT NULL, |
| 49 |
Email_Sent_Datetime datetime DEFAULT '0000-00-00 00:00:00' NULL, |
| 50 |
UNIQUE KEY id (Email_Send_ID) |
| 51 |
) |
| 52 |
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
| 53 |
|
| 54 |
dbDelta( $sql ); |
| 55 |
|
| 56 |
$sql = "CREATE TABLE $this->open_events_table_name ( |
| 57 |
Email_Open_ID mediumint(9) NOT NULL AUTO_INCREMENT, |
| 58 |
Email_Send_ID mediumint(9) DEFAULT 0 NOT NULL, |
| 59 |
Email_Opened text DEFAULT '' NOT NULL, |
| 60 |
Email_Opened_Datetime datetime DEFAULT '0000-00-00 00:00:00' NULL, |
| 61 |
UNIQUE KEY id (Email_Open_ID) |
| 62 |
) |
| 63 |
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
| 64 |
|
| 65 |
dbDelta( $sql ); |
| 66 |
|
| 67 |
$sql = "CREATE TABLE $this->links_clicked_events_table_name ( |
| 68 |
Email_Link_Clicked_ID mediumint(9) NOT NULL AUTO_INCREMENT, |
| 69 |
Email_Send_ID mediumint(9) DEFAULT 0 NOT NULL, |
| 70 |
Link_URL text DEFAULT '' NOT NULL, |
| 71 |
Link_Click_Count text DEFAULT '' NOT NULL, |
| 72 |
Link_Clicked_Datetime datetime DEFAULT '0000-00-00 00:00:00' NULL, |
| 73 |
UNIQUE KEY id (Email_Link_Clicked_ID) |
| 74 |
) |
| 75 |
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
| 76 |
|
| 77 |
dbDelta( $sql ); |
| 78 |
|
| 79 |
$sql = "CREATE TABLE $this->email_only_users_table_name ( |
| 80 |
EOU_ID mediumint(9) NOT NULL AUTO_INCREMENT, |
| 81 |
Email_Address text DEFAULT '' NOT NULL, |
| 82 |
EOU_Date_Added datetime DEFAULT '0000-00-00 00:00:00' NULL, |
| 83 |
UNIQUE KEY id (EOU_ID) |
| 84 |
) |
| 85 |
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
| 86 |
|
| 87 |
dbDelta($sql); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Return all occurrences of an email being sent that match the arguments received |
| 92 |
* @since 1.0.0 |
| 93 |
*/ |
| 94 |
public function get_emails_sent( $args ) { |
| 95 |
global $wpdb; |
| 96 |
|
| 97 |
$query_sql = "SELECT * FROM $this->send_events_table_name sends WHERE 1=%d"; |
| 98 |
|
| 99 |
$query_args = array( 1 ); |
| 100 |
|
| 101 |
if ( ! empty( $args['email_send_id'] ) ) { |
| 102 |
|
| 103 |
$query_sql .= " AND Email_Send_ID=%d"; |
| 104 |
$query_args[] = intval( $args['email_send_id'] ); |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! empty( $args['email_id'] ) ) { |
| 108 |
|
| 109 |
$query_sql .= " AND Email_ID=%d"; |
| 110 |
$query_args[] = intval( $args['email_id'] ); |
| 111 |
} |
| 112 |
|
| 113 |
if ( ! empty( $args['user_id'] ) ) { |
| 114 |
|
| 115 |
$query_sql .= " AND User_ID=%d"; |
| 116 |
$query_args[] = intval( $args['user_id'] ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( ! empty( $args['after'] ) ) { |
| 120 |
|
| 121 |
$query_sql .= " AND Email_Sent_Datetime>=%s"; |
| 122 |
$query_args[] = $args['after']; |
| 123 |
} |
| 124 |
|
| 125 |
if ( ! empty( $args['before'] ) ) { |
| 126 |
|
| 127 |
$query_sql .= " AND Email_Sent_Datetime<=%s"; |
| 128 |
$query_args[] = $args['before']; |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! empty( $args['date'] ) ) { |
| 132 |
|
| 133 |
$query_sql .= " AND DATE(Email_Sent_Datetime)=%s"; |
| 134 |
$query_args[] = $args['date']; |
| 135 |
} |
| 136 |
|
| 137 |
if ( ! empty( $args['orderby'] ) ) { |
| 138 |
|
| 139 |
$query_sql .= ' ORDER BY ' . esc_sql( $args['orderby'] ) . ' ' . ( strtolower( $args['order'] ) == 'desc' ? 'DESC' : 'ASC' ); |
| 140 |
} |
| 141 |
|
| 142 |
if ( ! empty( $args['sends_per_page'] ) and $args['sends_per_page'] > 0 ) { |
| 143 |
|
| 144 |
$query_sql .= ' LIMIT ' . intval( ( $args['paged'] - 1 ) * $args['sends_per_page'] ) . ', ' . intval( $args['sends_per_page'] ); |
| 145 |
} |
| 146 |
|
| 147 |
$db_email_sends = $wpdb->get_results( $wpdb->prepare( $query_sql, $query_args ) ); |
| 148 |
|
| 149 |
return $db_email_sends; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Return all occurrences of an email being opened that match the arguments received |
| 154 |
* @since 1.0.0 |
| 155 |
*/ |
| 156 |
public function get_email_opens( $args ) { |
| 157 |
global $wpdb; |
| 158 |
|
| 159 |
$query_sql = "SELECT * FROM $this->open_events_table_name opens INNER JOIN $this->send_events_table_name sends ON sends.Email_Send_ID = opens.Email_Send_ID WHERE 1=%d"; |
| 160 |
|
| 161 |
$query_args = array( 1 ); |
| 162 |
|
| 163 |
if ( ! empty( $args['email_send_id'] ) ) { |
| 164 |
|
| 165 |
$query_sql .= " AND opens.Email_Send_ID=%d"; |
| 166 |
$query_args[] = intval( $args['email_send_id'] ); |
| 167 |
} |
| 168 |
|
| 169 |
if ( ! empty( $args['email_id'] ) ) { |
| 170 |
|
| 171 |
$query_sql .= " AND sends.Email_ID=%d"; |
| 172 |
$query_args[] = intval( $args['email_id'] ); |
| 173 |
} |
| 174 |
|
| 175 |
if ( ! empty( $args['user_id'] ) ) { |
| 176 |
|
| 177 |
$query_sql .= " AND sends.User_ID=%d"; |
| 178 |
$query_args[] = intval( $args['user_id'] ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( ! empty( $args['after'] ) ) { |
| 182 |
|
| 183 |
$query_sql .= " AND opens.Email_Opened_Datetime>=%s"; |
| 184 |
$query_args[] = $args['after']; |
| 185 |
} |
| 186 |
|
| 187 |
if ( ! empty( $args['before'] ) ) { |
| 188 |
|
| 189 |
$query_sql .= " AND opens.Email_Opened_Datetime<=%s"; |
| 190 |
$query_args[] = $args['before']; |
| 191 |
} |
| 192 |
|
| 193 |
if ( ! empty( $args['date'] ) ) { |
| 194 |
|
| 195 |
$query_sql .= " AND DATE(opens.Email_Opened_Datetime)=%s"; |
| 196 |
$query_args[] = $args['date']; |
| 197 |
} |
| 198 |
|
| 199 |
if ( ! empty( $args['orderby'] ) ) { |
| 200 |
|
| 201 |
$query_sql .= ' ORDER BY ' . esc_sql( $args['orderby'] ) . ' ' . ( strtolower( $args['order'] ) == 'desc' ? 'DESC' : 'ASC' ); |
| 202 |
} |
| 203 |
|
| 204 |
if ( ! empty( $args['opens_per_page'] ) and $args['opens_per_page'] > 0 ) { |
| 205 |
|
| 206 |
$query_sql .= ' LIMIT ' . intval( ( $args['paged'] - 1 ) * $args['opens_per_page'] ) . ', ' . intval( $args['opens_per_page'] ); |
| 207 |
} |
| 208 |
|
| 209 |
$db_emails_opened = $wpdb->get_results( $wpdb->prepare( $query_sql, $query_args ) ); |
| 210 |
|
| 211 |
return $db_emails_opened; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Return the time that an email was last opened by a particular user |
| 216 |
* @since 1.0.0 |
| 217 |
*/ |
| 218 |
public function get_user_last_email_open( $user_id ) { |
| 219 |
global $wpdb; |
| 220 |
|
| 221 |
return $wpdb->get_var( $wpdb->prepare( "SELECT Email_Opened_Datetime FROM $this->open_events_table_name opens INNER JOIN $this->send_events_table_name sends ON sends.Email_Send_ID = opens.Email_Send_ID WHERE sends.User_ID=%d", $user_id ) ); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Return all occurrences of a link being clicked that match the arguments received |
| 226 |
* @since 1.0.0 |
| 227 |
*/ |
| 228 |
public function get_email_links_clicked( $args ) { |
| 229 |
global $wpdb; |
| 230 |
|
| 231 |
$query_sql = "SELECT * FROM $this->links_clicked_events_table_name clicks INNER JOIN $this->send_events_table_name sends ON sends.Email_Send_ID = clicks.Email_Send_ID WHERE 1=%d"; |
| 232 |
|
| 233 |
$query_args = array( 1 ); |
| 234 |
|
| 235 |
if ( ! empty( $args['email_send_id'] ) ) { |
| 236 |
|
| 237 |
$query_sql .= " AND clicks.Email_Send_ID=%d"; |
| 238 |
$query_args[] = intval( $args['email_send_id'] ); |
| 239 |
} |
| 240 |
|
| 241 |
if ( ! empty( $args['email_id'] ) ) { |
| 242 |
|
| 243 |
$query_sql .= " AND sends.Email_ID=%d"; |
| 244 |
$query_args[] = intval( $args['email_id'] ); |
| 245 |
} |
| 246 |
|
| 247 |
if ( ! empty( $args['user_id'] ) ) { |
| 248 |
|
| 249 |
$query_sql .= " AND sends.User_ID=%d"; |
| 250 |
$query_args[] = intval( $args['user_id'] ); |
| 251 |
} |
| 252 |
|
| 253 |
if ( ! empty( $args['after'] ) ) { |
| 254 |
|
| 255 |
$query_sql .= " AND clicks.Link_Clicked_Datetime>=%s"; |
| 256 |
$query_args[] = $args['after']; |
| 257 |
} |
| 258 |
|
| 259 |
if ( ! empty( $args['before'] ) ) { |
| 260 |
|
| 261 |
$query_sql .= " AND clicks.Link_Clicked_Datetime<=%s"; |
| 262 |
$query_args[] = $args['before']; |
| 263 |
} |
| 264 |
|
| 265 |
if ( ! empty( $args['date'] ) ) { |
| 266 |
|
| 267 |
$query_sql .= " AND DATE(clicks.Link_Clicked_Datetime)=%s"; |
| 268 |
$query_args[] = $args['date']; |
| 269 |
} |
| 270 |
|
| 271 |
if ( ! empty( $args['orderby'] ) ) { |
| 272 |
|
| 273 |
$query_sql .= ' ORDER BY ' . esc_sql( $args['orderby'] ) . ' ' . ( strtolower( $args['order'] ) == 'desc' ? 'DESC' : 'ASC' ); |
| 274 |
} |
| 275 |
|
| 276 |
if ( ! empty( $args['links_per_page'] ) and $args['links_per_page'] > 0 ) { |
| 277 |
|
| 278 |
$query_sql .= ' LIMIT ' . intval( ( $args['paged'] - 1 ) * $args['links_per_page'] ) . ', ' . intval( $args['links_per_page'] ); |
| 279 |
} |
| 280 |
|
| 281 |
$db_links_clicked = $wpdb->get_results( $wpdb->prepare( $query_sql, $query_args ) ); |
| 282 |
|
| 283 |
return $db_links_clicked; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Accepts a send event array, inserts it into the database, and returns the ID of the newly inserted send event |
| 288 |
* @since 1.0.0 |
| 289 |
*/ |
| 290 |
public function insert_send_event( $send_event ) { |
| 291 |
global $wpdb; |
| 292 |
global $ewd_uwpm_controller; |
| 293 |
|
| 294 |
$query_args = array( |
| 295 |
'Email_ID' => ! empty( $send_event['email_id'] ) ? $send_event['email_id'] : 0, |
| 296 |
'User_ID' => ! empty( $send_event['user_id'] ) ? $send_event['user_id'] : 0, |
| 297 |
'Event_ID' => ! empty( $send_event['event_id'] ) ? $send_event['event_id'] : 0, |
| 298 |
'Email_Unique_Identifier' => ! empty( $send_event['unique_identifier'] ) ? $send_event['unique_identifier'] : '', |
| 299 |
'Email_Sent_Datetime' => date( 'Y-m-d H:i:s' ), |
| 300 |
); |
| 301 |
|
| 302 |
$wpdb->insert( |
| 303 |
$this->send_events_table_name, |
| 304 |
$query_args |
| 305 |
); |
| 306 |
|
| 307 |
return $wpdb->insert_id; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Accepts an open event array, inserts it into the database, and returns the ID of the newly inserted open event |
| 312 |
* @since 1.0.0 |
| 313 |
*/ |
| 314 |
public function insert_open_event( $open_event ) { |
| 315 |
global $wpdb; |
| 316 |
global $ewd_uwpm_controller; |
| 317 |
|
| 318 |
$query_args = array( |
| 319 |
'Email_Send_ID' => ! empty( $open_event['email_send_id'] ) ? $open_event['email_send_id'] : 0, |
| 320 |
'Email_Opened' => ! empty( $open_event['email_opened'] ) ? $open_event['email_opened'] : '', |
| 321 |
'Email_Opened_Datetime' => date( 'Y-m-d H:i:s' ), |
| 322 |
); |
| 323 |
|
| 324 |
$wpdb->insert( |
| 325 |
$this->open_events_table_name, |
| 326 |
$query_args |
| 327 |
); |
| 328 |
|
| 329 |
return $wpdb->insert_id; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Accepts a click event array, inserts it into the database, and returns the ID of the newly inserted click event |
| 334 |
* @since 1.0.0 |
| 335 |
*/ |
| 336 |
public function insert_click_event( $click_event ) { |
| 337 |
global $wpdb; |
| 338 |
global $ewd_uwpm_controller; |
| 339 |
|
| 340 |
$query_args = array( |
| 341 |
'Email_Send_ID' => ! empty( $click_event['email_send_id'] ) ? $click_event['email_send_id'] : 0, |
| 342 |
'Link_URL' => ! empty( $click_event['link_url'] ) ? $click_event['link_url'] : '', |
| 343 |
'Link_Click_Count' => ! empty( $click_event['link_click_count'] ) ? $click_event['link_click_count'] : 0, |
| 344 |
'Link_Clicked_Datetime' => date( 'Y-m-d H:i:s' ), |
| 345 |
); |
| 346 |
|
| 347 |
$wpdb->insert( |
| 348 |
$this->links_clicked_events_table_name, |
| 349 |
$query_args |
| 350 |
); |
| 351 |
|
| 352 |
return $wpdb->insert_id; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Accepts an email user array, inserts it into the database, and returns the ID of the newly inserted email user |
| 357 |
* @since 1.0.0 |
| 358 |
*/ |
| 359 |
public function insert_email_user( $email_user ) { |
| 360 |
global $wpdb; |
| 361 |
global $ewd_uwpm_controller; |
| 362 |
|
| 363 |
$query_args = array( |
| 364 |
'Email_Address' => ! empty( $email_user['email_address'] ) ? $email_user['email_address'] : '', |
| 365 |
'EOU_Date_Added' => date( 'Y-m-d H:i:s' ), |
| 366 |
); |
| 367 |
|
| 368 |
$wpdb->insert( |
| 369 |
$this->email_only_users_table_name, |
| 370 |
$query_args |
| 371 |
); |
| 372 |
|
| 373 |
return $wpdb->insert_id; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Accepts a send event array, updates it into the database, and returns the ID if successful or false otherwise |
| 378 |
* @since 1.0.0 |
| 379 |
*/ |
| 380 |
public function update_send_event( $send_event ) { |
| 381 |
global $wpdb; |
| 382 |
global $ewd_uwpm_controller; |
| 383 |
|
| 384 |
if ( empty( $send_event['email_send_id'] ) ) { return false; } |
| 385 |
|
| 386 |
$query_args = array( |
| 387 |
'Email_ID' => ! empty( $send_event['email_id'] ) ? $send_event['email_id'] : 0, |
| 388 |
'User_ID' => ! empty( $send_event['user_id'] ) ? $send_event['user_id'] : 0, |
| 389 |
'Event_ID' => ! empty( $send_event['event_id'] ) ? $send_event['event_id'] : 0, |
| 390 |
'Email_Unique_Identifier' => ! empty( $send_event['unique_identifier'] ) ? $send_event['unique_identifier'] : '', |
| 391 |
); |
| 392 |
|
| 393 |
$wpdb->update( |
| 394 |
$this->send_events_table_name, |
| 395 |
$query_args, |
| 396 |
array( 'Email_Send_ID' => $send_event['email_send_id'] ) |
| 397 |
); |
| 398 |
|
| 399 |
return $send_event['email_send_id']; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Accepts an open event array, updates it in the database, and returns the ID if successful or false otherwise |
| 404 |
* @since 1.0.0 |
| 405 |
*/ |
| 406 |
public function update_open_event( $open_event ) { |
| 407 |
global $wpdb; |
| 408 |
global $ewd_uwpm_controller; |
| 409 |
|
| 410 |
if ( empty( $open_event['email_open_id'] ) ) { return false; } |
| 411 |
|
| 412 |
$query_args = array( |
| 413 |
'Email_Send_ID' => ! empty( $open_event['email_send_id'] ) ? $open_event['email_send_id'] : 0, |
| 414 |
'Email_Opened' => ! empty( $open_event['email_opened'] ) ? $open_event['email_opened'] : '', |
| 415 |
); |
| 416 |
|
| 417 |
$wpdb->update( |
| 418 |
$this->send_events_table_name, |
| 419 |
$query_args, |
| 420 |
array( 'Email_Open_ID' => $open_event['email_open_id'] ) |
| 421 |
); |
| 422 |
|
| 423 |
return $open_event['email_open_id']; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Accepts a click event array, updates it in the database, and returns the ID if successful or false otherwise |
| 428 |
* @since 1.0.0 |
| 429 |
*/ |
| 430 |
public function update_click_event( $click_event ) { |
| 431 |
global $wpdb; |
| 432 |
global $ewd_uwpm_controller; |
| 433 |
|
| 434 |
if ( empty( $click_event['email_link_clicked_id'] ) ) { return false; } |
| 435 |
|
| 436 |
$query_args = array( |
| 437 |
'Email_Send_ID' => ! empty( $click_event['email_send_id'] ) ? $click_event['email_send_id'] : 0, |
| 438 |
'Link_URL' => ! empty( $click_event['link_url'] ) ? $click_event['link_url'] : '', |
| 439 |
'Link_Click_Count' => ! empty( $click_event['link_click_count'] ) ? $click_event['link_click_count'] : 0, |
| 440 |
); |
| 441 |
|
| 442 |
$wpdb->update( |
| 443 |
$this->send_events_table_name, |
| 444 |
$query_args, |
| 445 |
array( 'Email_Link_Clicked_ID' => $click_event['email_link_clicked_id'] ) |
| 446 |
); |
| 447 |
|
| 448 |
return $click_event['email_link_clicked_id']; |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Accepts an email user array, updates it in the database, and returns the ID if successful or false otherwise |
| 453 |
* @since 1.0.0 |
| 454 |
*/ |
| 455 |
public function update_email_user( $email_user ) { |
| 456 |
global $wpdb; |
| 457 |
global $ewd_uwpm_controller; |
| 458 |
|
| 459 |
if ( empty( $email_user['eou_id'] ) ) { return false; } |
| 460 |
|
| 461 |
$query_args = array( |
| 462 |
'Email_Address' => ! empty( $email_user['email_address'] ) ? $email_user['email_address'] : '', |
| 463 |
); |
| 464 |
|
| 465 |
$wpdb->update( |
| 466 |
$this->send_events_table_name, |
| 467 |
$query_args, |
| 468 |
array( 'EOU_ID' => $email_user['eou_id'] ) |
| 469 |
); |
| 470 |
|
| 471 |
return $email_user['eou_id']; |
| 472 |
} |
| 473 |
|
| 474 |
public function delete_user_statistics( $user_id ) { |
| 475 |
global $wpdb; |
| 476 |
|
| 477 |
$wpdb->get_results( $wpdb->prepare( "DELETE $this->open_events_table_name FROM $this->open_events_table_name INNER JOIN $this->send_events_table_name sends ON sends.Email_Send_ID = $this->open_events_table_name.Email_Send_ID WHERE sends.User_ID=%d", $user_id ) ); |
| 478 |
|
| 479 |
$wpdb->get_results( $wpdb->prepare( "DELETE $this->links_clicked_events_table_name FROM $this->links_clicked_events_table_name INNER JOIN $this->send_events_table_name sends ON sends.Email_Send_ID = $this->links_clicked_events_table_name.Email_Send_ID WHERE sends.User_ID=%d", $user_id ) ); |
| 480 |
|
| 481 |
$wpdb->get_results( $wpdb->prepare( "DELETE FROM $this->send_events_table_name WHERE User_ID=%d", $user_id ) ); |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* Return the max Event ID + 1 from the send events table |
| 486 |
* @since 1.0.0 |
| 487 |
*/ |
| 488 |
public function get_next_send_event_id() { |
| 489 |
global $wpdb; |
| 490 |
|
| 491 |
return $wpdb->get_var( "SELECT MAX( Event_ID ) FROM $this->send_events_table_name" ) + 1; |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Return all users who haven't logged in since a certain time |
| 496 |
* @since 1.0.0 |
| 497 |
*/ |
| 498 |
public function get_users_without_login( $cutoff_time ) { |
| 499 |
global $wpdb; |
| 500 |
|
| 501 |
return $wpdb->get_results( |
| 502 |
"SELECT user_id FROM $wpdb->usermeta |
| 503 |
WHERE $wpdb->usermeta.meta_key = 'EWD_UWPM_User_Last_Activity' |
| 504 |
AND $wpdb->usermeta.meta_value < $cutoff_time |
| 505 |
AND user_id IN ( |
| 506 |
SELECT user_id FROM $wpdb->usermeta |
| 507 |
WHERE ($wpdb->usermeta.meta_key = 'EWD_UWPM_Login_Reminder_Sent' |
| 508 |
AND $wpdb->usermeta.meta_value != 'Yes') |
| 509 |
)" |
| 510 |
); |
| 511 |
} |
| 512 |
|
| 513 |
/** |
| 514 |
* Return all users who have abandoned there WooCommerce carts since a certain time |
| 515 |
* @since 1.0.0 |
| 516 |
*/ |
| 517 |
public function get_cart_abandoned_users( $cutoff_time ) { |
| 518 |
global $wpdb; |
| 519 |
|
| 520 |
return $wpdb->get_results( |
| 521 |
"SELECT user_id FROM $wpdb->usermeta |
| 522 |
WHERE $wpdb->usermeta.meta_key = 'EWD_UWPM_User_Cart_Update_Time' |
| 523 |
AND $wpdb->usermeta.meta_value < $cutoff_time |
| 524 |
AND user_id IN ( |
| 525 |
SELECT user_id FROM $wpdb->usermeta |
| 526 |
WHERE $wpdb->usermeta.meta_key = 'EWD_UWPM_Abandoned_Cart_Reminder_Sent' |
| 527 |
AND $wpdb->usermeta.meta_value != 'Yes' |
| 528 |
)" |
| 529 |
); |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* Record the most recent activity time for logged in users |
| 534 |
* @since 1.0.0 |
| 535 |
*/ |
| 536 |
public function log_user_last_activity() { |
| 537 |
|
| 538 |
if ( ! get_current_user_id() ) { return; } |
| 539 |
|
| 540 |
update_user_meta( get_current_user_id(), 'EWD_UWPM_User_Last_Activity', time() ); |
| 541 |
update_user_meta( get_current_user_id(), 'EWD_UWPM_Login_Reminder_Sent', 'No' ); |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* Record if a site visit represents an email open or an email link being clicked |
| 546 |
* @since 1.0.0 |
| 547 |
*/ |
| 548 |
public function maybe_log_email_interaction() { |
| 549 |
global $wpdb, $ewd_uwpm_controller; |
| 550 |
|
| 551 |
if ( is_admin() ) { return; } |
| 552 |
|
| 553 |
if ( empty( $_GET['ewd_uwpm_id'] ) ) { return; } |
| 554 |
|
| 555 |
$email_send_id = $wpdb->get_var( $wpdb->prepare( "SELECT Email_Send_ID FROM $this->send_events_table_name WHERE Email_Unique_Identifier=%s", $email_unique_identifier ) ); |
| 556 |
|
| 557 |
if ( $email_send_id == 0 ) { return; } |
| 558 |
|
| 559 |
if ( empty( $_GET['ewd_upwm_link_url'] ) ) { |
| 560 |
|
| 561 |
$this->track_opens(); |
| 562 |
} |
| 563 |
else { |
| 564 |
|
| 565 |
$this->track_clicks(); |
| 566 |
} |
| 567 |
|
| 568 |
add_action( 'shutdown', 'ewd_uwpm_end_clean', 1 ); |
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
* Record an email open, if it's the first time an email is opened |
| 573 |
* @since 1.0.0 |
| 574 |
*/ |
| 575 |
|
| 576 |
public function track_opens() { |
| 577 |
global $wpdb; |
| 578 |
|
| 579 |
header( 'Content-Type: image/png' ); |
| 580 |
|
| 581 |
readfile( EWD_UWPM_PLUGIN_DIR . '/assets/img/transparent.png' ); |
| 582 |
|
| 583 |
$email_unique_identifier = sanitize_text_field( $_GET['ewd_uwpm_id'] ); |
| 584 |
|
| 585 |
$email_send_id = $wpdb->get_var( $wpdb->prepare( "SELECT Email_Send_ID FROM $this->send_events_table_name WHERE Email_Unique_Identifier=%s", $email_unique_identifier ) ); |
| 586 |
|
| 587 |
if ( $email_send_id == 0 ) { return; } |
| 588 |
|
| 589 |
$wpdb->get_row( $wpdb->prepare( "SELECT Email_Send_ID FROM $this->open_events_table_name WHERE Email_Send_ID=%d", $email_send_id ) ); |
| 590 |
|
| 591 |
if ($wpdb->num_rows == 0) { |
| 592 |
|
| 593 |
$email_opened_datetime = date( 'Y-m-d H:i:s' ); |
| 594 |
|
| 595 |
$wpdb->insert( |
| 596 |
$this->open_events_table_name , |
| 597 |
array( |
| 598 |
'Email_Send_ID' => $email_send_id, |
| 599 |
'Email_Opened' => 'Yes', |
| 600 |
'Email_Opened_Datetime' => $email_opened_datetime |
| 601 |
) |
| 602 |
); |
| 603 |
} |
| 604 |
|
| 605 |
die(); |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Record an email link click |
| 610 |
* @since 1.0.0 |
| 611 |
*/ |
| 612 |
|
| 613 |
public function track_clicks() { |
| 614 |
global $wpdb, $ewd_uwpm_controller; |
| 615 |
|
| 616 |
if ( empty( $ewd_uwpm_controller->settings->get_setting( 'track-clicks' ) ) ) { |
| 617 |
|
| 618 |
return false; |
| 619 |
} |
| 620 |
|
| 621 |
$email_unique_identifier = sanitize_text_field( $_GET['ewd_uwpm_id'] ); |
| 622 |
|
| 623 |
$link_url = sanitize_url( $_GET['ewd_upwm_link_url'] ); |
| 624 |
|
| 625 |
$email_send_id = $wpdb->get_var( $wpdb->prepare( "SELECT Email_Send_ID FROM $this->send_events_table_name WHERE Email_Unique_Identifier=%s", $email_unique_identifier ) ); |
| 626 |
|
| 627 |
if ( $email_send_id == 0 ) { |
| 628 |
|
| 629 |
header( 'location:' . $link_url ); |
| 630 |
} |
| 631 |
|
| 632 |
$links_clicked = $wpdb->get_row( $wpdb->prepare( "SELECT Email_Send_ID, Link_Click_Count FROM $this->links_clicked_events_table_name WHERE Email_Send_ID=%d", $email_send_id ) ); |
| 633 |
|
| 634 |
if ( $wpdb->num_rows == 0 ) { |
| 635 |
|
| 636 |
$link_clicked_datetime = date( 'Y-m-d H:i:s' ); |
| 637 |
|
| 638 |
$wpdb->insert( |
| 639 |
$this->links_clicked_events_table_name, |
| 640 |
array( |
| 641 |
'Email_Send_ID' => $email_send_id, |
| 642 |
'Link_URL' => $link_url, |
| 643 |
'Link_Click_Count' => 1, |
| 644 |
'Link_Clicked_Datetime' => $link_clicked_datetime |
| 645 |
) |
| 646 |
); |
| 647 |
} |
| 648 |
else { |
| 649 |
$wpdb->update( |
| 650 |
$this->links_clicked_events_table_name, |
| 651 |
array( |
| 652 |
'Link_Click_Count' => $links_clicked->Link_Click_Count + 1 |
| 653 |
), |
| 654 |
array( |
| 655 |
'Email_Link_Clicked_ID' => $links_clicked->Email_Link_Clicked_ID |
| 656 |
) |
| 657 |
); |
| 658 |
} |
| 659 |
|
| 660 |
header( 'location:' . $link_url ); |
| 661 |
} |
| 662 |
} |
| 663 |
} |