PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.2
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.2
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / classes / class-merchant-db-tables.php

class-merchant-db-tables.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.2, at inc/classes/class-merchant-db-tables.php

285 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file contains the methods that introduce the customized DB tables required to run merchant & merchant-pro functionalities.
5 */
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly
8 }
9
10 if ( ! class_exists( 'Merchant_DB_Tables' ) ) {
11 /**
12 * Class Merchant_DB_Tables
13 */
14 class Merchant_DB_Tables {
15
16 /**
17 * Initializes database by creating or updating the table structure.
18 */
19 public static function init() {
20 add_action( 'init', array( __CLASS__, 'maybe_create_tables' ) );
21 add_action( 'upgrader_process_complete', array( __CLASS__, 'on_upgrade' ), 10, 2 );
22 register_activation_hook( MERCHANT_FILE, array( __CLASS__, 'create_tables' ) );
23 }
24
25 /**
26 * Get the list of database tables to be created or updated.
27 *
28 * @return array
29 */
30 private static function get_db_tables() {
31 /**
32 * Filter the tables to be created in the database.
33 *
34 * @param array $tables The tables to be created in the database.
35 *
36 * @since 1.10.3
37 */
38 return apply_filters(
39 'merchant_db_tables',
40 array(
41 'sales_notifications_table' => self::get_sales_notifications_table(),
42 'sales_notifications_shown_table' => self::get_sales_notifications_shown_table(),
43 'modules_analytics_table' => self::get_modules_analytics_table(),
44 )
45 );
46 }
47
48 /**
49 * Get the modules analytics table definition.
50 * This table is used to store analytics data for merchant modules.
51 *
52 * @return array
53 */
54 private static function get_modules_analytics_table() {
55 global $wpdb;
56
57 $table_name = $wpdb->prefix . 'merchant_modules_analytics';
58 $collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : '';
59
60 return array(
61 'name' => $table_name,
62 'query' => "
63 CREATE TABLE $table_name (
64 id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
65 source_product_id BIGINT UNSIGNED DEFAULT NULL,
66 event_type VARCHAR(200) DEFAULT NULL,
67 customer_id VARCHAR(400) DEFAULT NULL,
68 related_event_id BIGINT UNSIGNED DEFAULT NULL,
69 module_id VARCHAR(200) DEFAULT NULL,
70 campaign_id VARCHAR(200) DEFAULT NULL,
71 campaign_cost DECIMAL(12, 4) DEFAULT NULL,
72 order_id BIGINT UNSIGNED DEFAULT NULL,
73 order_subtotal DECIMAL(12, 4) DEFAULT NULL,
74 order_total DECIMAL(12, 4) DEFAULT NULL,
75 meta_data LONGTEXT DEFAULT NULL,
76 meta_data_2 LONGTEXT DEFAULT NULL,
77 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
78 PRIMARY KEY (id),
79 INDEX (event_type),
80 INDEX (event_type, timestamp),
81 INDEX (event_type, timestamp, module_id, campaign_id),
82 INDEX (module_id),
83 INDEX (campaign_id),
84 INDEX (order_id),
85 INDEX (timestamp)
86 ) $collate;
87 ",
88 'version' => 1,
89 'schema_updater' => '', // Attach a callable here to update the schema when needed, you must increment the version number
90 );
91 }
92
93 /**
94 * Get the sales notifications table definition.
95 *
96 * @return array
97 */
98 private static function get_sales_notifications_table() {
99 global $wpdb;
100
101 $table_name = $wpdb->prefix . 'merchant_sales_notifications';
102 $collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : '';
103
104 return array(
105 'name' => $table_name,
106 'query' => "
107 CREATE TABLE $table_name (
108 ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
109 event_type VARCHAR(255) NOT NULL, -- cart, order, product_view.
110 product_id BIGINT UNSIGNED NOT NULL,
111 quantity BIGINT UNSIGNED NOT NULL,
112 order_id BIGINT UNSIGNED NULL, -- To be used for excluding notifications for the some order.
113 customer_id VARCHAR(255) NULL, -- Adjusted to accommodate string customer IDs
114 timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
115 PRIMARY KEY (ID),
116 INDEX product_event_type_idx (product_id, event_type),
117 INDEX product_event_type_customer_idx (product_id, event_type, customer_id),
118 INDEX timestamp_idx (timestamp),
119 CONSTRAINT fk_product_id FOREIGN KEY (product_id) REFERENCES `{$wpdb->prefix}posts`(ID) ON DELETE CASCADE
120 ) $collate;
121 ",
122 'version' => 1,
123 'schema_updater' => '', // Attach a callable here to update the schema when needed, you must increment the version number
124 );
125 }
126
127 /**
128 * Get the sales notifications shown table definition.
129 *
130 * This table records the user events that have been shown to each customer.
131 * The event_id is a foreign key to the merchant_sales_notifications table.
132 * The customer_id is null when the notification is shown to an anonymous visitor.
133 *
134 * @return array
135 */
136 private static function get_sales_notifications_shown_table() {
137 global $wpdb;
138
139 $table_name = $wpdb->prefix . 'merchant_sales_notifications_shown';
140 $collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : '';
141
142 return array(
143 'name' => $table_name,
144 'query' => "
145 CREATE TABLE $table_name (
146 ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
147 event_id BIGINT UNSIGNED NOT NULL,
148 customer_id VARCHAR(255) NULL, -- Adjusted to accommodate string customer IDs
149 timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
150 PRIMARY KEY (ID),
151 INDEX event_id_customer_idx (event_id, customer_id),
152 CONSTRAINT fk_event_id FOREIGN KEY (event_id) REFERENCES `{$wpdb->prefix}merchant_sales_notifications`(ID) ON DELETE CASCADE
153 ) $collate;
154 ",
155 'version' => 1,
156 'schema_updater' => '', // Attach a callable here to update the schema when needed, you must increment the version number
157 );
158 }
159
160 /**
161 * Creates or updates all registered database tables.
162 * This method is called on plugin activation and plugin update.
163 * It will create the tables if they don't exist, and update them if needed.
164 */
165 public static function create_tables() {
166 global $wpdb;
167
168 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
169
170 foreach ( self::get_db_tables() as $table_key => $table ) {
171 $show_errors = $wpdb->hide_errors();
172 $table_name = $table['name'];
173 $current_table_version = get_option( 'merchant_' . $table_name . '_version' );
174 $exists = self::maybe_create_table( $table_name, $table['query'] );
175 $schema_updater = $table['schema_updater'];
176 if ( $exists && is_callable( $schema_updater ) && $current_table_version < $table['version'] ) {
177 call_user_func( $schema_updater, $table_name, $wpdb );
178 }
179 /**
180 * Fires after a database table is created or updated.
181 *
182 * @param string $table_name The name of the table that was created or updated.
183 * @param bool $exists Whether the table already existed.
184 * @param bool $show_errors Whether to show database errors.
185 *
186 * @since 1.10.3
187 */
188 do_action( 'merchant_db_table_created', $table['name'], $exists, $show_errors );
189 update_option( 'merchant_' . $table_name . '_version', $table['version'] );
190 }
191 }
192
193 /**
194 * Create database table, if it doesn't already exist.
195 *
196 * Based on admin/install-helper.php maybe_create_table function.
197 *
198 * @param string $table_name Database table name.
199 * @param string $create_sql Create database table SQL.
200 *
201 * @return bool False on error, true if already exists or success.
202 */
203 private static function maybe_create_table( $table_name, $create_sql ) {
204 global $wpdb;
205
206 if ( in_array( $table_name, $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ), 0 ), true ) ) {
207 return true;
208 }
209
210 $wpdb->query( $create_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
211
212 return in_array( $table_name, $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ), 0 ), true );
213 }
214
215 /**
216 * Runs after plugin updates to check if the database schema needs updating.
217 *
218 * @param WP_Upgrader $upgrader The upgrader instance.
219 * @param array $options Array of data about the upgrade.
220 */
221 public static function on_upgrade( $upgrader, $options ) {
222 if ( self::is_plugin_update( $options ) ) {
223 self::create_tables();
224 }
225 }
226
227 /**
228 * Checks if the current update is for the Merchant plugin.
229 *
230 * @param array $options Array of data about the upgrade.
231 *
232 * @return bool
233 */
234 private static function is_plugin_update( $options ) {
235 if (
236 isset( $options['action'], $options['type'], $options['plugins'] )
237 && $options['action'] === 'update'
238 && $options['type'] === 'plugin'
239 && in_array( plugin_basename( MERCHANT_FILE ), $options['plugins'], true )
240 ) {
241 return true;
242 }
243
244 return false;
245 }
246
247 /**
248 * Check if the tables need to be created.
249 */
250 public static function maybe_create_tables() {
251 if ( ! is_blog_installed() ) {
252 return;
253 }
254
255 if ( is_admin() ) {
256 $installed_version = self::get_db_version();
257
258 if ( MERCHANT_DB_VERSION !== $installed_version ) {
259 self::create_tables();
260 self::update_db_version();
261 }
262 }
263 }
264
265 /**
266 * Get the current database version.
267 *
268 * @return string
269 */
270 private static function get_db_version() {
271 return get_option( 'merchant_db_version' );
272 }
273
274 /**
275 * Update the database version option.
276 *
277 * @return void
278 */
279 private static function update_db_version() {
280 update_option( 'merchant_db_version', MERCHANT_DB_VERSION, false );
281 }
282 }
283 }
284
285