| 1 |
<?php |
| 2 |
namespace Elementor\Core\Common\Modules\EventTracker; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Base_Object; |
| 5 |
use Elementor\Core\Common\Modules\Connect\Apps\Common_App; |
| 6 |
use Elementor\Core\Common\Modules\Connect\Apps\Library; |
| 7 |
use Elementor\Plugin; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class DB extends Base_Object { |
| 14 |
|
| 15 |
/** |
| 16 |
* @var \wpdb |
| 17 |
*/ |
| 18 |
private $wpdb; |
| 19 |
|
| 20 |
const TABLE_NAME = 'e_events'; |
| 21 |
const DB_VERSION_OPTION_KEY = 'elementor_events_db_version'; |
| 22 |
const CURRENT_DB_VERSION = '1.0.0'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Get Table Name |
| 26 |
* |
| 27 |
* Returns the Events database table's name with the `wpdb` prefix. |
| 28 |
* |
| 29 |
* @since 3.6.0 |
| 30 |
* |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
public function get_table_name() { |
| 34 |
return $this->wpdb->prefix . self::TABLE_NAME; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Prepare Database for Entry |
| 39 |
* |
| 40 |
* The events database should have a limit of up to 1000 event entries stored daily. |
| 41 |
* Before adding a new entry to the database, we make sure that the limit of 1000 events is not reached. |
| 42 |
* If there are 1000 or more entries in the DB, we delete the earliest-inserted entry before inserting a new one. |
| 43 |
* |
| 44 |
* @since 3.6.0 |
| 45 |
*/ |
| 46 |
public function prepare_db_for_entry() { |
| 47 |
$events = $this->get_event_ids_from_db(); |
| 48 |
|
| 49 |
if ( 1000 <= count( $events ) ) { |
| 50 |
$event_ids = []; |
| 51 |
|
| 52 |
foreach ( $events as $event ) { |
| 53 |
$event_ids[] = $event->id; |
| 54 |
} |
| 55 |
|
| 56 |
// Sort the array by entry ID |
| 57 |
array_multisort( $event_ids, SORT_ASC, $events ); |
| 58 |
|
| 59 |
// Delete the smallest ID (which is the earliest DB entry) |
| 60 |
$this->wpdb->delete( $this->get_table_name(), [ 'ID' => $events[0]->id ] ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Create Entry |
| 66 |
* |
| 67 |
* Adds an event entry to the database. |
| 68 |
* |
| 69 |
* @since 3.6.0 |
| 70 |
*/ |
| 71 |
public function create_entry( $event_data ) { |
| 72 |
$this->prepare_db_for_entry(); |
| 73 |
|
| 74 |
$connect = Plugin::$instance->common->get_component( 'connect' ); |
| 75 |
/** @var Library $library */ |
| 76 |
$library = $connect->get_apps()['library']; |
| 77 |
|
| 78 |
if ( ! isset( $event_data['details'] ) ) { |
| 79 |
$event_data['details'] = []; |
| 80 |
} |
| 81 |
|
| 82 |
if ( $library->is_connected() ) { |
| 83 |
$user_connect_data = get_user_option( Common_App::OPTION_CONNECT_COMMON_DATA_KEY ); |
| 84 |
|
| 85 |
// Add the user's client ID to the event. |
| 86 |
$event_data['details']['client_id'] = $user_connect_data['client_id']; |
| 87 |
} |
| 88 |
|
| 89 |
$event_data['details'] = wp_json_encode( $event_data['details'] ); |
| 90 |
|
| 91 |
$entry = [ |
| 92 |
'event_data' => wp_json_encode( $event_data ), |
| 93 |
'created_at' => $event_data['ts'], |
| 94 |
]; |
| 95 |
|
| 96 |
$this->wpdb->insert( $this->get_table_name(), $entry ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get Event IDs From DB |
| 101 |
* |
| 102 |
* Fetches the IDs of all events saved in the database. |
| 103 |
* |
| 104 |
* @since 3.6.0 |
| 105 |
* |
| 106 |
* @return array|object|null |
| 107 |
*/ |
| 108 |
public function get_event_ids_from_db() { |
| 109 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 110 |
return $this->wpdb->get_results( "SELECT id FROM {$this->get_table_name()}" ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Reset Table |
| 115 |
* |
| 116 |
* Empties the contents of the Events DB table. |
| 117 |
* |
| 118 |
* @since 3.6.0 |
| 119 |
*/ |
| 120 |
public static function reset_table() { |
| 121 |
global $wpdb; |
| 122 |
|
| 123 |
$table_name = $wpdb->prefix . self::TABLE_NAME; |
| 124 |
|
| 125 |
// Delete all content of the table. |
| 126 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 127 |
$wpdb->query( "TRUNCATE TABLE {$table_name}" ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Create Table |
| 132 |
* |
| 133 |
* Creates the `wp_e_events` database table. |
| 134 |
* |
| 135 |
* @since 3.6.0 |
| 136 |
* |
| 137 |
* @param string $query to that looks for the Events table in the DB. Used for checking if table was created. |
| 138 |
*/ |
| 139 |
private function create_table( $query ) { |
| 140 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 141 |
|
| 142 |
$table_name = $this->get_table_name(); |
| 143 |
$charset_collate = $this->wpdb->get_charset_collate(); |
| 144 |
|
| 145 |
$e_events_table = "CREATE TABLE `{$table_name}` ( |
| 146 |
id bigint(20) unsigned auto_increment primary key, |
| 147 |
event_data text null, |
| 148 |
created_at datetime not null |
| 149 |
) {$charset_collate};"; |
| 150 |
|
| 151 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
| 152 |
$this->wpdb->query( $e_events_table ); |
| 153 |
|
| 154 |
// Check if table was created successfully. |
| 155 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
| 156 |
if ( $this->wpdb->get_var( $query ) === $table_name ) { |
| 157 |
update_option( self::DB_VERSION_OPTION_KEY, self::CURRENT_DB_VERSION, false ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Add Indexes |
| 163 |
* |
| 164 |
* Adds an index to the events table for the creation date column. |
| 165 |
* |
| 166 |
* @since 3.6.0 |
| 167 |
*/ |
| 168 |
private function add_indexes() { |
| 169 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 170 |
$this->wpdb->query( 'ALTER TABLE ' . $this->get_table_name() . ' |
| 171 |
ADD INDEX `created_at_index` (`created_at`) |
| 172 |
' ); |
| 173 |
} |
| 174 |
|
| 175 |
public function __construct() { |
| 176 |
global $wpdb; |
| 177 |
$this->wpdb = $wpdb; |
| 178 |
|
| 179 |
// Check if table exists. If not, create it. |
| 180 |
$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $this->get_table_name() ) ); |
| 181 |
|
| 182 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
| 183 |
if ( $wpdb->get_var( $query ) !== $this->get_table_name() ) { |
| 184 |
$this->create_table( $query ); |
| 185 |
$this->add_indexes(); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|