refactor(Core): refactor and optimize code

This commit is contained in:
2026-05-06 00:54:24 +03:30
parent 32c065e4b6
commit dec4e67b9e
20 changed files with 1787 additions and 361 deletions

View File

@@ -7,11 +7,11 @@ if (!defined('ABSPATH')) {
/**
* Database migrations for Sodino plugin
*/
function sodino_create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$current_version = get_option('sodino_db_version', '0');
// Rules table
$rules_table = $wpdb->prefix . 'sodino_rules';
@@ -22,23 +22,22 @@ function sodino_create_tables() {
actions longtext NOT NULL,
priority int(11) NOT NULL DEFAULT 10,
usage_limit int(11) NOT NULL DEFAULT 0,
usage_count int(11) NOT NULL DEFAULT 0,
user_roles varchar(255) DEFAULT '',
start_date datetime NULL,
end_date datetime NULL,
enabled tinyint(1) DEFAULT 1,
condition_type varchar(100) DEFAULT NULL,
condition_value varchar(255) DEFAULT NULL,
action_type varchar(100) DEFAULT NULL,
action_value varchar(255) DEFAULT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
PRIMARY KEY (id),
KEY enabled_priority (enabled, priority),
KEY start_end_dates (start_date, end_date)
) $charset_collate;";
// Events table
$events_table = $wpdb->prefix . 'sodino_events';
$events_sql = "CREATE TABLE $events_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
id bigint(20) NOT NULL AUTO_INCREMENT,
event_type varchar(100) NOT NULL,
product_id mediumint(9) DEFAULT NULL,
variation_id mediumint(9) DEFAULT NULL,
@@ -49,7 +48,12 @@ function sodino_create_tables() {
discount_value decimal(10,2) DEFAULT 0,
metadata longtext DEFAULT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
PRIMARY KEY (id),
KEY event_type_created (event_type, created_at),
KEY product_id (product_id),
KEY rule_id (rule_id),
KEY session_id (session_id),
KEY created_at (created_at)
) $charset_collate;";
// Upsell table
@@ -64,9 +68,13 @@ function sodino_create_tables() {
discount_value decimal(10,2) DEFAULT 0,
status tinyint(1) DEFAULT 1,
priority int(11) NOT NULL DEFAULT 10,
impressions bigint(20) NOT NULL DEFAULT 0,
conversions bigint(20) NOT NULL DEFAULT 0,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
PRIMARY KEY (id),
KEY status_priority (status, priority),
KEY trigger_type (trigger_type)
) $charset_collate;";
// Banner table
@@ -88,7 +96,25 @@ function sodino_create_tables() {
impressions bigint(20) NOT NULL DEFAULT 0,
clicks bigint(20) NOT NULL DEFAULT 0,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY status_priority (status, priority),
KEY position (position),
KEY start_end_time (start_time, end_time)
) $charset_collate;";
// Analytics cache table
$analytics_table = $wpdb->prefix . 'sodino_analytics_cache';
$analytics_sql = "CREATE TABLE $analytics_table (
id bigint(20) NOT NULL AUTO_INCREMENT,
cache_key varchar(255) NOT NULL,
cache_value longtext NOT NULL,
cache_group varchar(100) NOT NULL DEFAULT 'general',
expires_at datetime NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY cache_key_group (cache_key, cache_group),
KEY expires_at (expires_at)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
@@ -96,7 +122,42 @@ function sodino_create_tables() {
dbDelta($events_sql);
dbDelta($upsell_sql);
dbDelta($banner_sql);
dbDelta($analytics_sql);
// Run migrations
sodino_run_migrations($current_version);
// Add version option
update_option('sodino_db_version', '1.3');
}
update_option('sodino_db_version', '2.0');
}
/**
* Run incremental migrations
*/
function sodino_run_migrations($from_version) {
global $wpdb;
// Migration from 1.x to 2.0
if (version_compare($from_version, '2.0', '<')) {
// Add usage_count column if not exists
$rules_table = $wpdb->prefix . 'sodino_rules';
$column_exists = $wpdb->get_results("SHOW COLUMNS FROM {$rules_table} LIKE 'usage_count'");
if (empty($column_exists)) {
$wpdb->query("ALTER TABLE {$rules_table} ADD COLUMN usage_count int(11) NOT NULL DEFAULT 0 AFTER usage_limit");
}
// Remove deprecated columns
$deprecated_columns = ['condition_type', 'condition_value', 'action_type', 'action_value'];
foreach ($deprecated_columns as $col) {
$col_exists = $wpdb->get_results("SHOW COLUMNS FROM {$rules_table} LIKE '{$col}'");
if (!empty($col_exists)) {
$wpdb->query("ALTER TABLE {$rules_table} DROP COLUMN {$col}");
}
}
// Add indexes for better performance
$wpdb->query("ALTER TABLE {$rules_table} ADD INDEX enabled_priority (enabled, priority)");
$wpdb->query("ALTER TABLE {$rules_table} ADD INDEX start_end_dates (start_date, end_date)");
}
}