PluginProbe
Elementor Website Builder – more than just a page builder / 3.6.0-beta1
Elementor Website Builder – more than just a page builder v3.6.0-beta1
4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 All 457 releases
← All changes | core/common/modules/event-tracker/db.php +42 -113 4.2.0-dev2 → 3.6.0-beta1 View file →
@@ -1,11 +1,8 @@
1 1 <?php
2 2 namespace Elementor\Core\Common\Modules\EventTracker;
3 3
4 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 5
9 6 if ( ! defined( 'ABSPATH' ) ) {
10 7 exit; // Exit if accessed directly.
11 8 }
@@ -16,156 +13,80 @@
16 13 * @var \wpdb
17 14 */
18 15 private $wpdb;
19 16
17 + /**
18 + * @var string
19 + */
20 + private $table_name;
21 +
20 22 const TABLE_NAME = 'e_events';
21 23 const DB_VERSION_OPTION_KEY = 'elementor_events_db_version';
22 - const CURRENT_DB_VERSION = '1.0.0';
24 + const CURRENT_DB_VERSION = 1;
23 25
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 - }
26 + // Create DB entry
27 + // Delete DB entry
36 28
37 29 /**
38 30 * Prepare Database for Entry
39 31 *
40 32 * 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.
33 + * That limit is managed in the FIFO method - If the limit is reached, the last
43 34 *
44 35 * @since 3.6.0
45 36 */
46 37 public function prepare_db_for_entry() {
47 - $events = $this->get_event_ids_from_db();
38 + $events = $this->get_events_from_db();
48 39
49 40 if ( 1000 <= count( $events ) ) {
50 - $event_ids = [];
51 -
52 - foreach ( $events as $event ) {
53 - $event_ids[] = $event->id;
54 - }
55 -
56 41 // Sort the array by entry ID
57 - array_multisort( $event_ids, SORT_ASC, $events );
42 + $id_col = array_column( $events, 'id' );
43 + array_multisort( $id_col, SORT_ASC, $events );
58 44
59 - // Delete the smallest ID (which is the earliest DB entry)
60 - $this->wpdb->delete( $this->get_table_name(), [ 'ID' => $events[0]->id ] );
45 + $this->delete_entry( $events[0]['id'] );
61 46 }
62 47 }
63 48
64 - /**
65 - * Create Entry
66 - *
67 - * Adds an event entry to the database.
68 - *
69 - * @since 3.6.0
70 - */
49 + public function delete_entry( $id ) {
50 + $this->wpdb->insert( $this->get_table_name(), [ 'ID' => $id ] );
51 + }
52 +
71 53 public function create_entry( $event_data ) {
72 54 $this->prepare_db_for_entry();
73 55
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 56 $entry = [
92 57 'event_data' => wp_json_encode( $event_data ),
93 - 'created_at' => $event_data['ts'],
94 58 ];
95 59
96 60 $this->wpdb->insert( $this->get_table_name(), $entry );
97 61 }
98 62
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()}" );
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 );
111 67 }
112 68
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}" );
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 );
128 73 }
129 74
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();
75 + private function create_table() {
143 76 $charset_collate = $this->wpdb->get_charset_collate();
144 77
145 - $e_events_table = "CREATE TABLE `{$table_name}` (
78 + $e_events_table = "CREATE TABLE `{$this->table_name}` (
146 79 id bigint(20) unsigned auto_increment primary key,
147 80 event_data text null,
148 81 created_at datetime not null
149 82 ) {$charset_collate};";
150 83
151 - // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
152 - $this->wpdb->query( $e_events_table );
84 + dbDelta( $e_events_table );
153 85
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 - }
86 + update_option( self::DB_VERSION_OPTION_KEY, self::CURRENT_DB_VERSION, false );
159 87 }
160 88
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 89 private function add_indexes() {
169 90 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
170 91 $this->wpdb->query( 'ALTER TABLE ' . $this->get_table_name() . '
171 92 ADD INDEX `created_at_index` (`created_at`)
@@ -171,18 +92,26 @@
171 92 ADD INDEX `created_at_index` (`created_at`)
172 93 ' );
173 94 }
174 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 +
175 104 public function __construct() {
105 + $this->table_name = $this->wpdb->prefix . self::TABLE_NAME;
106 +
176 107 global $wpdb;
177 108 $this->wpdb = $wpdb;
178 109
179 - // Check if table exists. If not, create it.
180 - $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $this->get_table_name() ) );
110 + $installed_version = get_option( self::DB_VERSION_OPTION_KEY, -1 );
181 111
182 - // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
183 - if ( $wpdb->get_var( $query ) !== $this->get_table_name() ) {
184 - $this->create_table( $query );
112 + if ( self::CURRENT_DB_VERSION !== $installed_version ) {
113 + $this->create_table();
185 114 $this->add_indexes();
186 115 }
187 116 }
188 117 }