PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.11.0
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.11.0
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 1.11.0, at inc/classes/class-merchant-db-tables.php

239 lines 7.9 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 )
44 );
45 }
46
47 /**
48 * Get the sales notifications table definition.
49 *
50 * @return array
51 */
52 private static function get_sales_notifications_table() {
53 global $wpdb;
54
55 $table_name = $wpdb->prefix . 'merchant_sales_notifications';
56 $collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : '';
57
58 return array(
59 'name' => $table_name,
60 'query' => "
61 CREATE TABLE $table_name (
62 ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
63 event_type VARCHAR(255) NOT NULL, -- cart, order, product_view.
64 product_id BIGINT UNSIGNED NOT NULL,
65 quantity BIGINT UNSIGNED NOT NULL,
66 order_id BIGINT UNSIGNED NULL, -- To be used for excluding notifications for the some order.
67 customer_id VARCHAR(255) NULL, -- Adjusted to accommodate string customer IDs
68 timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
69 PRIMARY KEY (ID),
70 INDEX product_event_type_idx (product_id, event_type),
71 INDEX product_event_type_customer_idx (product_id, event_type, customer_id),
72 INDEX timestamp_idx (timestamp),
73 CONSTRAINT fk_product_id FOREIGN KEY (product_id) REFERENCES `{$wpdb->prefix}posts`(ID) ON DELETE CASCADE
74 ) $collate;
75 ",
76 'version' => 1,
77 'schema_updater' => '', // Attach a callable here to update the schema when needed, you must increment the version number
78 );
79 }
80
81 /**
82 * Get the sales notifications shown table definition.
83 *
84 * This table records the user events that have been shown to each customer.
85 * The event_id is a foreign key to the merchant_sales_notifications table.
86 * The customer_id is null when the notification is shown to an anonymous visitor.
87 *
88 * @return array
89 */
90 private static function get_sales_notifications_shown_table() {
91 global $wpdb;
92
93 $table_name = $wpdb->prefix . 'merchant_sales_notifications_shown';
94 $collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : '';
95
96 return array(
97 'name' => $table_name,
98 'query' => "
99 CREATE TABLE $table_name (
100 ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
101 event_id BIGINT UNSIGNED NOT NULL,
102 customer_id VARCHAR(255) NULL, -- Adjusted to accommodate string customer IDs
103 timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
104 PRIMARY KEY (ID),
105 INDEX event_id_customer_idx (event_id, customer_id),
106 CONSTRAINT fk_event_id FOREIGN KEY (event_id) REFERENCES `{$wpdb->prefix}merchant_sales_notifications`(ID) ON DELETE CASCADE
107 ) $collate;
108 ",
109 'version' => 1,
110 'schema_updater' => '', // Attach a callable here to update the schema when needed, you must increment the version number
111 );
112 }
113
114 /**
115 * Creates or updates all registered database tables.
116 * This method is called on plugin activation and plugin update.
117 * It will create the tables if they don't exist, and update them if needed.
118 */
119 public static function create_tables() {
120 global $wpdb;
121
122 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
123
124 foreach ( self::get_db_tables() as $table_key => $table ) {
125 $show_errors = $wpdb->hide_errors();
126 $table_name = $table['name'];
127 $current_table_version = get_option( 'merchant_' . $table_name . '_version' );
128 $exists = self::maybe_create_table( $table_name, $table['query'] );
129 $schema_updater = $table['schema_updater'];
130 if ( $exists && is_callable( $schema_updater ) && $current_table_version < $table['version'] ) {
131 call_user_func( $schema_updater, $table_name, $wpdb );
132 }
133 /**
134 * Fires after a database table is created or updated.
135 *
136 * @param string $table_name The name of the table that was created or updated.
137 * @param bool $exists Whether the table already existed.
138 * @param bool $show_errors Whether to show database errors.
139 *
140 * @since 1.10.3
141 */
142 do_action( 'merchant_db_table_created', $table['name'], $exists, $show_errors );
143 update_option( 'merchant_' . $table_name . '_version', $table['version'] );
144 }
145 }
146
147 /**
148 * Create database table, if it doesn't already exist.
149 *
150 * Based on admin/install-helper.php maybe_create_table function.
151 *
152 * @param string $table_name Database table name.
153 * @param string $create_sql Create database table SQL.
154 *
155 * @return bool False on error, true if already exists or success.
156 */
157 private static function maybe_create_table( $table_name, $create_sql ) {
158 global $wpdb;
159
160 if ( in_array( $table_name, $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ), 0 ), true ) ) {
161 return true;
162 }
163
164 $wpdb->query( $create_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
165
166 return in_array( $table_name, $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ), 0 ), true );
167 }
168
169 /**
170 * Runs after plugin updates to check if the database schema needs updating.
171 *
172 * @param WP_Upgrader $upgrader The upgrader instance.
173 * @param array $options Array of data about the upgrade.
174 */
175 public static function on_upgrade( $upgrader, $options ) {
176 if ( self::is_plugin_update( $options ) ) {
177 self::create_tables();
178 }
179 }
180
181 /**
182 * Checks if the current update is for the Merchant plugin.
183 *
184 * @param array $options Array of data about the upgrade.
185 *
186 * @return bool
187 */
188 private static function is_plugin_update( $options ) {
189 if (
190 isset( $options['action'], $options['type'], $options['plugins'] )
191 && $options['action'] === 'update'
192 && $options['type'] === 'plugin'
193 && in_array( plugin_basename( MERCHANT_FILE ), $options['plugins'], true )
194 ) {
195 return true;
196 }
197
198 return false;
199 }
200
201 /**
202 * Check if the tables need to be created.
203 */
204 public static function maybe_create_tables() {
205 if ( ! is_blog_installed() ) {
206 return;
207 }
208
209 if ( is_admin() ) {
210 $installed_version = self::get_db_version();
211
212 if ( MERCHANT_DB_VERSION !== $installed_version ) {
213 self::create_tables();
214 self::update_db_version();
215 }
216 }
217 }
218
219 /**
220 * Get the current database version.
221 *
222 * @return string
223 */
224 private static function get_db_version() {
225 return get_option( 'merchant_db_version' );
226 }
227
228 /**
229 * Update the database version option.
230 *
231 * @return void
232 */
233 private static function update_db_version() {
234 update_option( 'merchant_db_version', MERCHANT_DB_VERSION, false );
235 }
236 }
237 }
238
239