| 1 |
<?php |
| 2 |
namespace Elementor\Core\Common\Modules\EventTracker; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Base_Object; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
class DB extends Base_Object { |
| 11 |
|
| 12 |
/** |
| 13 |
* @var \wpdb |
| 14 |
*/ |
| 15 |
private $wpdb; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
private $table_name; |
| 21 |
|
| 22 |
const TABLE_NAME = 'e_events'; |
| 23 |
const DB_VERSION_OPTION_KEY = 'elementor_events_db_version'; |
| 24 |
const CURRENT_DB_VERSION = 1; |
| 25 |
|
| 26 |
// Create DB entry |
| 27 |
// Delete DB entry |
| 28 |
|
| 29 |
/** |
| 30 |
* Prepare Database for Entry |
| 31 |
* |
| 32 |
* The events database should have a limit of up to 1000 event entries stored daily. |
| 33 |
* That limit is managed in the FIFO method - If the limit is reached, the last |
| 34 |
* |
| 35 |
* @since 3.6.0 |
| 36 |
*/ |
| 37 |
public function prepare_db_for_entry() { |
| 38 |
$events = $this->get_events_from_db(); |
| 39 |
|
| 40 |
if ( 1000 <= count( $events ) ) { |
| 41 |
// Sort the array by entry ID |
| 42 |
$id_col = array_column( $events, 'id' ); |
| 43 |
array_multisort( $id_col, SORT_ASC, $events ); |
| 44 |
|
| 45 |
$this->delete_entry( $events[0]['id'] ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function delete_entry( $id ) { |
| 50 |
$this->wpdb->insert( $this->get_table_name(), [ 'ID' => $id ] ); |
| 51 |
} |
| 52 |
|
| 53 |
public function create_entry( $event_data ) { |
| 54 |
$this->prepare_db_for_entry(); |
| 55 |
|
| 56 |
$entry = [ |
| 57 |
'event_data' => wp_json_encode( $event_data ), |
| 58 |
]; |
| 59 |
|
| 60 |
$this->wpdb->insert( $this->get_table_name(), $entry ); |
| 61 |
} |
| 62 |
|
| 63 |
public function get_events_from_db() { |
| 64 |
$query = $this->wpdb->prepare( 'SELECT `id` FROM %s', $this->get_table_name() ); |
| 65 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 66 |
return $this->wpdb->get_results( $query ); |
| 67 |
} |
| 68 |
|
| 69 |
public function get_events_count() { |
| 70 |
$query = $this->wpdb->prepare( 'SELECT COUNT(*) FROM %s', $this->get_table_name() ); |
| 71 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 72 |
return $this->wpdb->get_results( $query ); |
| 73 |
} |
| 74 |
|
| 75 |
private function create_table() { |
| 76 |
$charset_collate = $this->wpdb->get_charset_collate(); |
| 77 |
|
| 78 |
$e_events_table = "CREATE TABLE `{$this->table_name}` ( |
| 79 |
id bigint(20) unsigned auto_increment primary key, |
| 80 |
event_data text null, |
| 81 |
created_at datetime not null |
| 82 |
) {$charset_collate};"; |
| 83 |
|
| 84 |
dbDelta( $e_events_table ); |
| 85 |
|
| 86 |
update_option( self::DB_VERSION_OPTION_KEY, self::CURRENT_DB_VERSION, false ); |
| 87 |
} |
| 88 |
|
| 89 |
private function add_indexes() { |
| 90 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 91 |
$this->wpdb->query( 'ALTER TABLE ' . $this->get_table_name() . ' |
| 92 |
ADD INDEX `created_at_index` (`created_at`) |
| 93 |
' ); |
| 94 |
} |
| 95 |
|
| 96 |
public function get_table_name() { |
| 97 |
return $this->wpdb->prefix . $this->table_name; |
| 98 |
} |
| 99 |
|
| 100 |
private function set_table_prefix() { |
| 101 |
$this->wpdb->{$this->table_name} = $this->get_table_name(); |
| 102 |
} |
| 103 |
|
| 104 |
public function __construct() { |
| 105 |
$this->table_name = $this->wpdb->prefix . self::TABLE_NAME; |
| 106 |
|
| 107 |
global $wpdb; |
| 108 |
$this->wpdb = $wpdb; |
| 109 |
|
| 110 |
$installed_version = get_option( self::DB_VERSION_OPTION_KEY, -1 ); |
| 111 |
|
| 112 |
if ( self::CURRENT_DB_VERSION !== $installed_version ) { |
| 113 |
$this->create_table(); |
| 114 |
$this->add_indexes(); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|