Check_Email_Table_Manager.php
670 lines
| 1 | <?php namespace CheckEmail\Core\DB; |
| 2 | |
| 3 | /** |
| 4 | * Handle installation and db table creation. |
| 5 | */ |
| 6 | use CheckEmail\Core\Loadie; |
| 7 | use CheckEmail\Util; |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 10 | |
| 11 | /** |
| 12 | * Helper class to create table. |
| 13 | */ |
| 14 | class Check_Email_Table_Manager implements Loadie { |
| 15 | |
| 16 | /* Database table name */ |
| 17 | const LOG_TABLE_NAME = 'check_email_log'; |
| 18 | |
| 19 | /* Database option name */ |
| 20 | const DB_OPTION_NAME = 'check_email-log-db'; |
| 21 | |
| 22 | /* Database version */ |
| 23 | const DB_VERSION = '0.3'; |
| 24 | |
| 25 | /** |
| 26 | * Setup hooks. |
| 27 | */ |
| 28 | public function load() { |
| 29 | add_action( 'wpmu_new_blog', array( $this, 'create_table_for_new_blog' ) ); |
| 30 | |
| 31 | add_filter( 'wpmu_drop_tables', array( $this, 'delete_table_from_deleted_blog' ) ); |
| 32 | |
| 33 | add_filter( 'admin_init', array( $this, 'add_backtrace_segment_field' ) ); |
| 34 | |
| 35 | $option = get_option( 'check-email-log-core' ); |
| 36 | if ((isset($option['is_retention_amount_enable']) && $option['is_retention_amount_enable']) || (isset($option['is_retention_period_enable']) && $option['is_retention_period_enable'])) { |
| 37 | add_action('admin_init', array( $this, 'check_mail_cron_schedule' )); |
| 38 | add_action('check_mail_cron_hook', array( $this, 'check_mail_cron_execute' )); |
| 39 | } |
| 40 | |
| 41 | // Do any DB upgrades. |
| 42 | $this->update_table_if_needed(); |
| 43 | } |
| 44 | |
| 45 | public function on_activate( $network_wide ) { |
| 46 | if ( is_multisite() && $network_wide ) { |
| 47 | // Note: if there are more than 10,000 blogs or |
| 48 | // if `wp_is_large_network` filter is set, then this may fail. |
| 49 | $sites = get_sites(); |
| 50 | |
| 51 | foreach ( $sites as $site ) { |
| 52 | switch_to_blog( $site->blog_id ); |
| 53 | $this->create_table_if_needed(); |
| 54 | restore_current_blog(); |
| 55 | } |
| 56 | } else { |
| 57 | $this->create_table_if_needed(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Create email log table when a new blog is created. |
| 63 | */ |
| 64 | public function create_table_for_new_blog( $blog_id ) { |
| 65 | if ( is_plugin_active_for_network( 'check-email-log/check-email.php' ) ) { |
| 66 | switch_to_blog( $blog_id ); |
| 67 | $this->create_table_if_needed(); |
| 68 | restore_current_blog(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add email log table to the list of tables deleted when a blog is deleted. |
| 74 | */ |
| 75 | public function delete_table_from_deleted_blog( $tables ) { |
| 76 | $tables[] = $this->get_log_table_name(); |
| 77 | |
| 78 | return $tables; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get email log table name. |
| 83 | */ |
| 84 | public function get_log_table_name() { |
| 85 | global $wpdb; |
| 86 | |
| 87 | return $wpdb->prefix . self::LOG_TABLE_NAME; |
| 88 | } |
| 89 | |
| 90 | public function insert_log( $data ) { |
| 91 | global $wpdb; |
| 92 | |
| 93 | $table_name = $this->get_log_table_name(); |
| 94 | $wpdb->insert( $table_name, $data ); |
| 95 | } |
| 96 | |
| 97 | public function delete_logs( $ids ) { |
| 98 | global $wpdb; |
| 99 | |
| 100 | $table_name = $this->get_log_table_name(); |
| 101 | |
| 102 | $ids = esc_sql( $ids ); |
| 103 | |
| 104 | return $wpdb->query( "DELETE FROM {$table_name} where id IN ( {$ids} )" ); |
| 105 | } |
| 106 | |
| 107 | public function delete_all_logs() { |
| 108 | global $wpdb; |
| 109 | |
| 110 | $table_name = $this->get_log_table_name(); |
| 111 | |
| 112 | return $wpdb->query( "DELETE FROM {$table_name}" ); |
| 113 | } |
| 114 | |
| 115 | public function delete_logs_older_than( $interval_in_days ) { |
| 116 | global $wpdb; |
| 117 | $table_name = $this->get_log_table_name(); |
| 118 | |
| 119 | $query = $wpdb->prepare( "DELETE FROM {$table_name} WHERE sent_date < DATE_SUB( CURDATE(), INTERVAL %d DAY )", $interval_in_days ); |
| 120 | $deleted_rows_count = $wpdb->query( $query ); |
| 121 | |
| 122 | return $deleted_rows_count; |
| 123 | } |
| 124 | |
| 125 | public function fetch_log_items_by_id( $ids = array(), $additional_args = array() ) { |
| 126 | global $wpdb; |
| 127 | $table_name = $this->get_log_table_name(); |
| 128 | |
| 129 | $query = "SELECT * FROM {$table_name}"; |
| 130 | |
| 131 | $date_column_format_key = 'date_column_format'; |
| 132 | if ( array_key_exists( $date_column_format_key, $additional_args ) && ! empty( $additional_args[ $date_column_format_key ] ) ) { |
| 133 | $query = "SELECT DATE_FORMAT(sent_date, \"{$additional_args[ $date_column_format_key ]}\") as sent_date_custom, el.* FROM {$table_name} as el"; |
| 134 | } |
| 135 | |
| 136 | if ( ! empty( $ids ) ) { |
| 137 | $ids = array_map( 'absint', $ids ); |
| 138 | |
| 139 | // Can't use wpdb->prepare for the below query. |
| 140 | $ids_list = esc_sql( implode( ',', $ids ) ); |
| 141 | |
| 142 | $query .= " where id IN ( {$ids_list} )"; |
| 143 | } |
| 144 | |
| 145 | return $wpdb->get_results( $query, 'ARRAY_A' ); //@codingStandardsIgnoreLine |
| 146 | } |
| 147 | |
| 148 | public function fetch_log_items( $request, $per_page, $current_page_no ) { |
| 149 | global $wpdb; |
| 150 | $table_name = $this->get_log_table_name(); |
| 151 | |
| 152 | $query = 'SELECT * FROM ' . $table_name; |
| 153 | $count_query = 'SELECT count(*) FROM ' . $table_name; |
| 154 | $query_cond = ''; |
| 155 | |
| 156 | if ( isset( $request['s'] ) && is_string( $request['s'] ) && $request['s'] !== '' ) { |
| 157 | $search_term = trim( esc_sql( $request['s'] ) ); |
| 158 | |
| 159 | if ( Util\wp_chill_check_email_advanced_search_term( $search_term ) ) { |
| 160 | $predicates = Util\wp_chill_check_email_get_advanced_search_term_predicates( $search_term ); |
| 161 | |
| 162 | foreach ( $predicates as $column => $email ) { |
| 163 | switch ( $column ) { |
| 164 | case 'id': |
| 165 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 166 | $query_cond .= "id = '$email'"; |
| 167 | break; |
| 168 | case 'to': |
| 169 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 170 | $query_cond .= "to_email LIKE '%$email%'"; |
| 171 | break; |
| 172 | case 'email': |
| 173 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 174 | $query_cond .= ' ( '; /* Begin 1st */ |
| 175 | $query_cond .= " ( to_email LIKE '%$email%' OR subject LIKE '%$email%' ) "; /* Begin 2nd & End 2nd */ |
| 176 | $query_cond .= ' OR '; |
| 177 | $query_cond .= ' ( '; /* Begin 3rd */ |
| 178 | $query_cond .= "headers <> ''"; |
| 179 | $query_cond .= ' AND '; |
| 180 | $query_cond .= ' ( '; /* Begin 4th */ |
| 181 | $query_cond .= "headers REGEXP '[F|f]rom:.*$email' OR "; |
| 182 | $query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' OR "; |
| 183 | $query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' OR "; |
| 184 | $query_cond .= "headers REGEXP '[R|r]eply-[T|t]o:.*$email'"; |
| 185 | $query_cond .= ' ) '; /* End 4th */ |
| 186 | $query_cond .= ' ) '; /* End 3rd */ |
| 187 | $query_cond .= ' ) '; /* End 1st */ |
| 188 | break; |
| 189 | case 'cc': |
| 190 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 191 | $query_cond .= ' ( '; /* Begin 1st */ |
| 192 | $query_cond .= "headers <> ''"; |
| 193 | $query_cond .= ' AND '; |
| 194 | $query_cond .= ' ( '; /* Begin 2nd */ |
| 195 | $query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' "; |
| 196 | $query_cond .= ' ) '; /* End 2nd */ |
| 197 | $query_cond .= ' ) '; /* End 1st */ |
| 198 | break; |
| 199 | case 'bcc': |
| 200 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 201 | $query_cond .= ' ( '; /* Begin 1st */ |
| 202 | $query_cond .= "headers <> ''"; |
| 203 | $query_cond .= ' AND '; |
| 204 | $query_cond .= ' ( '; /* Begin 2nd */ |
| 205 | $query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' "; |
| 206 | $query_cond .= ' ) '; /* End 2nd */ |
| 207 | $query_cond .= ' ) '; /* End 1st */ |
| 208 | break; |
| 209 | case 'reply-to': |
| 210 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 211 | $query_cond .= ' ( '; /* Begin 1st */ |
| 212 | $query_cond .= "headers <> ''"; |
| 213 | $query_cond .= ' AND '; |
| 214 | $query_cond .= ' ( '; /* Begin 2nd */ |
| 215 | $query_cond .= "headers REGEXP '[R|r]eply-to:.*$email' "; |
| 216 | $query_cond .= ' ) '; /* End 2nd */ |
| 217 | $query_cond .= ' ) '; /* End 1st */ |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | } else { |
| 222 | $query_cond .= " WHERE ( to_email LIKE '%$search_term%' OR subject LIKE '%$search_term%' OR message LIKE '%$search_term%' ) "; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if ( isset( $request['d'] ) && $request['d'] !== '' ) { |
| 227 | $search_date = trim( esc_sql( $request['d'] ) ); |
| 228 | if ( '' === $query_cond ) { |
| 229 | $query_cond .= " WHERE sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' "; |
| 230 | } else { |
| 231 | $query_cond .= " AND sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' "; |
| 232 | } |
| 233 | } |
| 234 | if ( isset( $request['status'] ) && $request['status'] !== '' ) { |
| 235 | $status = trim( esc_sql( $request['status'] ) ); |
| 236 | switch( $status ) { |
| 237 | case 'failed': |
| 238 | $query_cond .= " WHERE `result` IS NULL OR `result` = ''"; |
| 239 | break; |
| 240 | case 'complete': |
| 241 | $query_cond .= " WHERE `result` IS NOT NULL AND `result` != ''"; |
| 242 | break; |
| 243 | default: |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Ordering parameters. |
| 249 | $orderby = ! empty( $request['orderby'] ) ? sanitize_sql_orderby( $request['orderby'] ) : 'sent_date'; |
| 250 | if ( isset( $request['order'] ) ) { |
| 251 | $order = in_array( strtoupper($request['order']), array( 'DESC', 'ASC' ) ) ? esc_sql( $request['order'] ) : 'DESC'; |
| 252 | }else{ |
| 253 | $order = 'DESC'; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | if ( ! empty( $orderby ) & ! empty( $order ) ) { |
| 258 | $query_cond .= ' ORDER BY ' . $orderby . ' ' . $order; |
| 259 | } |
| 260 | |
| 261 | // Find total number of items. |
| 262 | $count_query = $count_query . $query_cond; |
| 263 | $total_items = $wpdb->get_var( $count_query ); |
| 264 | |
| 265 | // Adjust the query to take pagination into account. |
| 266 | if ( ! empty( $current_page_no ) && ! empty( $per_page ) ) { |
| 267 | $offset = ( $current_page_no - 1 ) * $per_page; |
| 268 | $query_cond .= ' LIMIT ' . (int) $offset . ',' . (int) $per_page; |
| 269 | } |
| 270 | |
| 271 | // Fetch the items. |
| 272 | $query = $query . $query_cond; |
| 273 | $items = $wpdb->get_results( $query ); |
| 274 | |
| 275 | return array( $items, $total_items ); |
| 276 | } |
| 277 | |
| 278 | public function create_table_if_needed() { |
| 279 | global $wpdb; |
| 280 | |
| 281 | $table_name = $this->get_log_table_name(); |
| 282 | |
| 283 | if ( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) { |
| 284 | |
| 285 | $sql = $this->get_create_table_query(); |
| 286 | |
| 287 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 288 | dbDelta( $sql ); |
| 289 | |
| 290 | add_option( self::DB_OPTION_NAME, self::DB_VERSION ); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | public function get_logs_count() { |
| 295 | global $wpdb; |
| 296 | |
| 297 | $query = 'SELECT count(*) FROM ' . $this->get_log_table_name(); |
| 298 | |
| 299 | return $wpdb->get_var( $query ); |
| 300 | } |
| 301 | |
| 302 | public function fetch_log_id_by_data( $data ) { |
| 303 | if ( empty( $data ) || ! is_array( $data ) ) { |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | global $wpdb; |
| 308 | $table_name = $this->get_log_table_name(); |
| 309 | |
| 310 | $query = "SELECT ID FROM {$table_name}"; |
| 311 | $query_cond = ''; |
| 312 | $where = array(); |
| 313 | |
| 314 | // Execute the following `if` conditions only when $data is array. |
| 315 | if ( array_key_exists( 'to', $data ) ) { |
| 316 | // Since the value is stored as CSV in DB, convert the values from error data to CSV to compare. |
| 317 | $to_email = Util\wp_chill_check_email_stringify( $data['to'] ); |
| 318 | |
| 319 | $to_email = trim( esc_sql( $to_email ) ); |
| 320 | $where[] = "to_email = '$to_email'"; |
| 321 | } |
| 322 | |
| 323 | if ( array_key_exists( 'subject', $data ) ) { |
| 324 | $subject = trim( esc_sql( $data['subject'] ) ); |
| 325 | $where[] = "subject = '$subject'"; |
| 326 | } |
| 327 | |
| 328 | if ( array_key_exists( 'attachments', $data ) ) { |
| 329 | if ( is_array( $data['attachments'] ) ) { |
| 330 | $attachments = count( $data['attachments'] ) > 0 ? 'true' : 'false'; |
| 331 | } else { |
| 332 | $attachments = empty( $data['attachments'] ) ? 'false' : 'true'; |
| 333 | } |
| 334 | $attachments = trim( esc_sql( $attachments ) ); |
| 335 | $where[] = "attachments = '$attachments'"; |
| 336 | } |
| 337 | |
| 338 | foreach ( $where as $index => $value ) { |
| 339 | $query_cond .= 0 === $index ? ' WHERE ' : ' AND '; |
| 340 | $query_cond .= $value; |
| 341 | } |
| 342 | |
| 343 | // Get only the latest logged item when multiple rows match. |
| 344 | $query_cond .= ' ORDER BY id DESC LIMIT 1'; |
| 345 | |
| 346 | $query = $query . $query_cond; |
| 347 | |
| 348 | return absint( $wpdb->get_var( $query ) ); |
| 349 | } |
| 350 | |
| 351 | public function mark_log_as_failed( $log_item_id, $message ) { |
| 352 | global $wpdb; |
| 353 | $table_name = $this->get_log_table_name(); |
| 354 | |
| 355 | $wpdb->update( |
| 356 | $table_name, |
| 357 | array( |
| 358 | 'result' => '0', |
| 359 | 'error_message' => $message, |
| 360 | ), |
| 361 | array( 'ID' => $log_item_id ), |
| 362 | array( |
| 363 | '%d', // `result` format. |
| 364 | '%s', // `error_message` format. |
| 365 | ), |
| 366 | array( |
| 367 | '%d', // `ID` format. |
| 368 | ) |
| 369 | ); |
| 370 | } |
| 371 | |
| 372 | private function update_table_if_needed() { |
| 373 | if ( get_option( self::DB_OPTION_NAME, false ) === self::DB_VERSION ) { |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | $sql = $this->get_create_table_query(); |
| 378 | |
| 379 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 380 | dbDelta( $sql ); |
| 381 | |
| 382 | update_option( self::DB_OPTION_NAME, self::DB_VERSION ); |
| 383 | } |
| 384 | |
| 385 | private function get_create_table_query() { |
| 386 | global $wpdb; |
| 387 | $table_name = $this->get_log_table_name(); |
| 388 | $charset_collate = $wpdb->get_charset_collate(); |
| 389 | |
| 390 | $sql = 'CREATE TABLE ' . $table_name . ' ( |
| 391 | id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 392 | to_email VARCHAR(500) NOT NULL, |
| 393 | subject VARCHAR(500) NOT NULL, |
| 394 | message TEXT NOT NULL, |
| 395 | backtrace_segment TEXT NOT NULL, |
| 396 | headers TEXT NOT NULL, |
| 397 | attachments TEXT NOT NULL, |
| 398 | sent_date timestamp NOT NULL, |
| 399 | attachment_name VARCHAR(1000), |
| 400 | ip_address VARCHAR(15), |
| 401 | result TINYINT(1), |
| 402 | error_message VARCHAR(1000), |
| 403 | PRIMARY KEY (id) |
| 404 | ) ' . $charset_collate . ';'; |
| 405 | |
| 406 | return $sql; |
| 407 | } |
| 408 | |
| 409 | private function validate_columns( $column ) { |
| 410 | return in_array( $column, array( 'to' ), true ); |
| 411 | } |
| 412 | |
| 413 | public function query_log_items_by_column( $columns ) { |
| 414 | if ( ! is_array( $columns ) ) { |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | $columns_keys = array_keys( $columns ); |
| 419 | if ( ! array_filter( $columns_keys, array( $this, 'validate_columns' ) ) ) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | global $wpdb; |
| 424 | |
| 425 | $table_name = $this->get_log_table_name(); |
| 426 | $query = "SELECT id, sent_date, to_email, subject FROM {$table_name}"; |
| 427 | $query_cond = ''; |
| 428 | $where = array(); |
| 429 | |
| 430 | // Execute the following `if` conditions only when $data is array. |
| 431 | if ( array_key_exists( 'to', $columns ) ) { |
| 432 | // Since the value is stored as CSV in DB, convert the values from error data to CSV to compare. |
| 433 | $to_email = Util\wp_chill_check_email_stringify( $columns['to'] ); |
| 434 | |
| 435 | $to_email = trim( esc_sql( $to_email ) ); |
| 436 | $where[] = "to_email = '$to_email'"; |
| 437 | |
| 438 | foreach ( $where as $index => $value ) { |
| 439 | $query_cond .= 0 === $index ? ' WHERE ' : ' AND '; |
| 440 | $query_cond .= $value; |
| 441 | } |
| 442 | |
| 443 | // Get only the latest logged item when multiple rows match. |
| 444 | $query_cond .= ' ORDER BY id DESC'; |
| 445 | |
| 446 | $query = $query . $query_cond; |
| 447 | |
| 448 | return $wpdb->get_results( $query ); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Add new backtrace_segment field to check_email_log table |
| 454 | * @since 1.0.12 |
| 455 | * */ |
| 456 | public function add_backtrace_segment_field(){ |
| 457 | global $wpdb; |
| 458 | $table_name = $this->get_log_table_name(); |
| 459 | |
| 460 | // Field to check |
| 461 | $field_name = 'backtrace_segment'; |
| 462 | |
| 463 | // Query to check if the field exists in the table |
| 464 | $field_exists = $wpdb->get_results( |
| 465 | $wpdb->prepare( |
| 466 | "SHOW COLUMNS FROM $table_name LIKE %s", |
| 467 | $field_name |
| 468 | ) |
| 469 | ); |
| 470 | |
| 471 | if(empty($field_exists)){ |
| 472 | $query = "ALTER TABLE $table_name ADD backtrace_segment TEXT NULL DEFAULT NULL AFTER message"; |
| 473 | $wpdb->query($query); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | public function fetch_log_count_by_status( $request, $per_page, $current_page_no,$status='all' ) { |
| 478 | global $wpdb; |
| 479 | $table_name = $this->get_log_table_name(); |
| 480 | |
| 481 | |
| 482 | $count_query = 'SELECT count(*) FROM ' . $table_name; |
| 483 | $query_cond = ''; |
| 484 | |
| 485 | if ( isset( $request['s'] ) && is_string( $request['s'] ) && $request['s'] !== '' ) { |
| 486 | $search_term = trim( esc_sql( $request['s'] ) ); |
| 487 | |
| 488 | if ( Util\wp_chill_check_email_advanced_search_term( $search_term ) ) { |
| 489 | $predicates = Util\wp_chill_check_email_get_advanced_search_term_predicates( $search_term ); |
| 490 | |
| 491 | foreach ( $predicates as $column => $email ) { |
| 492 | switch ( $column ) { |
| 493 | case 'id': |
| 494 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 495 | $query_cond .= "id = '$email'"; |
| 496 | break; |
| 497 | case 'to': |
| 498 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 499 | $query_cond .= "to_email LIKE '%$email%'"; |
| 500 | break; |
| 501 | case 'email': |
| 502 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 503 | $query_cond .= ' ( '; /* Begin 1st */ |
| 504 | $query_cond .= " ( to_email LIKE '%$email%' OR subject LIKE '%$email%' ) "; /* Begin 2nd & End 2nd */ |
| 505 | $query_cond .= ' OR '; |
| 506 | $query_cond .= ' ( '; /* Begin 3rd */ |
| 507 | $query_cond .= "headers <> ''"; |
| 508 | $query_cond .= ' AND '; |
| 509 | $query_cond .= ' ( '; /* Begin 4th */ |
| 510 | $query_cond .= "headers REGEXP '[F|f]rom:.*$email' OR "; |
| 511 | $query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' OR "; |
| 512 | $query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' OR "; |
| 513 | $query_cond .= "headers REGEXP '[R|r]eply-[T|t]o:.*$email'"; |
| 514 | $query_cond .= ' ) '; /* End 4th */ |
| 515 | $query_cond .= ' ) '; /* End 3rd */ |
| 516 | $query_cond .= ' ) '; /* End 1st */ |
| 517 | break; |
| 518 | case 'cc': |
| 519 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 520 | $query_cond .= ' ( '; /* Begin 1st */ |
| 521 | $query_cond .= "headers <> ''"; |
| 522 | $query_cond .= ' AND '; |
| 523 | $query_cond .= ' ( '; /* Begin 2nd */ |
| 524 | $query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' "; |
| 525 | $query_cond .= ' ) '; /* End 2nd */ |
| 526 | $query_cond .= ' ) '; /* End 1st */ |
| 527 | break; |
| 528 | case 'bcc': |
| 529 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 530 | $query_cond .= ' ( '; /* Begin 1st */ |
| 531 | $query_cond .= "headers <> ''"; |
| 532 | $query_cond .= ' AND '; |
| 533 | $query_cond .= ' ( '; /* Begin 2nd */ |
| 534 | $query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' "; |
| 535 | $query_cond .= ' ) '; /* End 2nd */ |
| 536 | $query_cond .= ' ) '; /* End 1st */ |
| 537 | break; |
| 538 | case 'reply-to': |
| 539 | $query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; |
| 540 | $query_cond .= ' ( '; /* Begin 1st */ |
| 541 | $query_cond .= "headers <> ''"; |
| 542 | $query_cond .= ' AND '; |
| 543 | $query_cond .= ' ( '; /* Begin 2nd */ |
| 544 | $query_cond .= "headers REGEXP '[R|r]eply-to:.*$email' "; |
| 545 | $query_cond .= ' ) '; /* End 2nd */ |
| 546 | $query_cond .= ' ) '; /* End 1st */ |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | } else { |
| 551 | $query_cond .= " WHERE ( to_email LIKE '%$search_term%' OR subject LIKE '%$search_term%' ) "; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if ( isset( $request['d'] ) && $request['d'] !== '' ) { |
| 556 | $search_date = trim( esc_sql( $request['d'] ) ); |
| 557 | if ( '' === $query_cond ) { |
| 558 | $query_cond .= " WHERE sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' "; |
| 559 | } else { |
| 560 | $query_cond .= " AND sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' "; |
| 561 | } |
| 562 | } |
| 563 | if ( !empty($status) ) { |
| 564 | $status = trim( esc_sql( $status ) ); |
| 565 | if ($status != 'all') { |
| 566 | if ( empty($request['d']) && empty($request['s']) ) { |
| 567 | $query_cond .= " WHERE "; |
| 568 | }else{ |
| 569 | $query_cond .= " AND "; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // print_r($query_cond);die; |
| 574 | |
| 575 | switch( $status ) { |
| 576 | case 'failed': |
| 577 | $query_cond .= " `result` = 0"; |
| 578 | break; |
| 579 | case 'complete': |
| 580 | $query_cond .= " `result` != 0"; |
| 581 | break; |
| 582 | default: |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | // Ordering parameters. |
| 588 | $orderby = ! empty( $request['orderby'] ) ? sanitize_sql_orderby( $request['orderby'] ) : 'sent_date'; |
| 589 | if ( isset( $request['order'] ) ) { |
| 590 | $order = in_array( strtoupper($request['order']), array( 'DESC', 'ASC' ) ) ? esc_sql( $request['order'] ) : 'DESC'; |
| 591 | }else{ |
| 592 | $order = 'DESC'; |
| 593 | } |
| 594 | |
| 595 | if ( ! empty( $orderby ) & ! empty( $order ) ) { |
| 596 | $query_cond .= ' ORDER BY ' . $orderby . ' ' . $order; |
| 597 | } |
| 598 | |
| 599 | // Find total number of items. |
| 600 | $count_query = $count_query . $query_cond; |
| 601 | $total_items = $wpdb->get_var( $count_query ); |
| 602 | return $total_items; |
| 603 | } |
| 604 | |
| 605 | public function deleteLogOlderThan($timeInterval = null) |
| 606 | { |
| 607 | if ( ! current_user_can( 'manage_check_email' ) ) { |
| 608 | return; |
| 609 | } |
| 610 | global $wpdb; |
| 611 | $table_name = $this->get_log_table_name(); |
| 612 | $option = get_option( 'check-email-log-core' ); |
| 613 | if (isset($option['is_retention_amount_enable']) && isset($option['retention_amount']) && $option['is_retention_amount_enable']) { |
| 614 | $limit= intval($option['retention_amount']); |
| 615 | if(!empty($limit)){ |
| 616 | $count_query = 'SELECT count(*) FROM ' . $table_name; |
| 617 | $total_items = $wpdb->get_var( $count_query ); |
| 618 | if ($total_items > $limit) { |
| 619 | $data_to_delete = $total_items - $limit; |
| 620 | $old_posts = $wpdb->get_col( $wpdb->prepare(" |
| 621 | SELECT ID FROM $table_name |
| 622 | ORDER BY ID ASC |
| 623 | LIMIT %d",$data_to_delete) ); |
| 624 | |
| 625 | // Delete the logs |
| 626 | foreach ($old_posts as $column_value) { |
| 627 | $sql = $wpdb->prepare( |
| 628 | "DELETE FROM $table_name WHERE ID = %d", |
| 629 | $column_value |
| 630 | ); |
| 631 | $wpdb->query($sql); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | } |
| 636 | } |
| 637 | if (isset($option['is_retention_period_enable']) && $option['is_retention_period_enable']) { |
| 638 | |
| 639 | if ($option['log_retention_period'] == 'custom_in_days') { |
| 640 | $custom_in_days = empty($option['log_retention_period_in_days']) ? 1 : intval($option['log_retention_period_in_days']); |
| 641 | $time_interval = strtotime('+' . $custom_in_days. ' days'); |
| 642 | }else{ |
| 643 | $periods = array( '1_day' =>86400, |
| 644 | '1_week' =>604800, |
| 645 | '1_month' =>2419200, |
| 646 | '6_month' =>15780000, |
| 647 | '1_year' =>31560000 |
| 648 | ); |
| 649 | $time_interval = $periods[$option['log_retention_period']]; |
| 650 | } |
| 651 | $timestamp = time() - $time_interval; |
| 652 | |
| 653 | $sql = "DELETE FROM " . $table_name . " WHERE Unix_timestamp(sent_date) <= %d"; |
| 654 | $sql = $wpdb->prepare($sql, $timestamp); |
| 655 | $wpdb->query($sql); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | function check_mail_cron_schedule() { |
| 660 | if (!wp_next_scheduled('check_mail_cron_hook')) { |
| 661 | wp_schedule_event(time(), 'daily', 'check_mail_cron_hook'); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | function check_mail_cron_execute() { |
| 666 | $this->deleteLogOlderThan(); |
| 667 | error_log('Cron job executed at' . date('Y-m-d H:i:s')); |
| 668 | } |
| 669 | } |
| 670 |