| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Class LP_Sessions_DB |
| 9 |
* |
| 10 |
* @since 4.1.1 |
| 11 |
*/ |
| 12 |
class LP_Sessions_DB extends LP_Database { |
| 13 |
/** |
| 14 |
* @var LP_Sessions_DB |
| 15 |
*/ |
| 16 |
private static $instance; |
| 17 |
|
| 18 |
protected function __construct() { |
| 19 |
parent::__construct(); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Instance |
| 24 |
* |
| 25 |
* @return LP_Sessions_DB |
| 26 |
*/ |
| 27 |
public static function getInstance(): self { |
| 28 |
if ( is_null( self::$instance ) ) { |
| 29 |
self::$instance = new self(); |
| 30 |
} |
| 31 |
|
| 32 |
return self::$instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get sessions. |
| 37 |
* |
| 38 |
* @param LP_Session_Filter $filter |
| 39 |
* |
| 40 |
* @return array|int|string|null |
| 41 |
* @throws Exception |
| 42 |
*/ |
| 43 |
public function get_sessions( LP_Session_Filter $filter ) { |
| 44 |
$default_fields = $filter->all_fields; |
| 45 |
$filter->fields = array_merge( $default_fields, $filter->fields ); |
| 46 |
|
| 47 |
if ( empty( $filter->collection ) ) { |
| 48 |
$filter->collection = $this->tb_lp_sessions; |
| 49 |
} |
| 50 |
|
| 51 |
if ( empty( $filter->collection_alias ) ) { |
| 52 |
$filter->collection_alias = 'ss'; |
| 53 |
} |
| 54 |
|
| 55 |
if ( empty( $filter->field_count ) ) { |
| 56 |
$filter->field_count = 'session_id'; |
| 57 |
} |
| 58 |
|
| 59 |
// Filter by session_key. |
| 60 |
if ( ! empty( $filter->session_key ) ) { |
| 61 |
$filter->where[] = $this->wpdb->prepare( 'AND session_key = %s', $filter->session_key ); |
| 62 |
} |
| 63 |
|
| 64 |
return $this->execute( $filter ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get delete rows in session table |
| 69 |
* |
| 70 |
* @throws |
| 71 |
*/ |
| 72 |
public function delete_rows() { |
| 73 |
$now = current_time( 'timestamp' ); |
| 74 |
$adayago = $now - ( 24 * 60 * 60 ); |
| 75 |
$where = 'WHERE session_expiry < ' . $adayago . ''; |
| 76 |
$table = $this->tb_lp_sessions; |
| 77 |
$limit = 100; |
| 78 |
$result = $this->wpdb->query( |
| 79 |
" |
| 80 |
DELETE FROM {$table} |
| 81 |
{$where} |
| 82 |
LIMIT {$limit} |
| 83 |
" |
| 84 |
); |
| 85 |
|
| 86 |
$this->check_execute_has_error(); |
| 87 |
|
| 88 |
return $result; |
| 89 |
} |
| 90 |
public function count_row_db_sessions() { |
| 91 |
global $wpdb; |
| 92 |
$now = current_time( 'timestamp' ); |
| 93 |
$adayago = $now - ( 24 * 60 * 60 ); |
| 94 |
$where = 'WHERE session_expiry < ' . $adayago . ' AND 0=%d'; |
| 95 |
$query = $wpdb->prepare( |
| 96 |
" |
| 97 |
SELECT count(*) |
| 98 |
FROM $this->tb_lp_sessions |
| 99 |
{$where} |
| 100 |
", |
| 101 |
0 |
| 102 |
); |
| 103 |
|
| 104 |
$result = $wpdb->get_var( $query ); |
| 105 |
return $result; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|