container = $container; } public function init() { add_action('admin_init', [$this, 'check_and_install']); } /** * Checks if the DB needs to be installed or upgraded. */ public function check_and_install() { $installed_version = get_option('awas_search_db_version'); if ($installed_version !== self::DB_VERSION) { $this->install(); } } /** * Creates or updates the custom database table. */ public function install() { global $wpdb; $table_name = $wpdb->prefix . 'awas_search_index'; $log_table = $wpdb->prefix . 'awas_search_logs'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, object_id bigint(20) unsigned NOT NULL, variation_id bigint(20) unsigned NOT NULL DEFAULT 0, title text NOT NULL, content longtext NOT NULL, sku varchar(100) NOT NULL DEFAULT '', search_terms longtext NOT NULL, weight_data longtext NULL, lang varchar(20) NOT NULL DEFAULT '', status varchar(20) NOT NULL DEFAULT 'publish', PRIMARY KEY (id), KEY object_id (object_id), KEY variation_id (variation_id), KEY sku (sku), KEY status (status), FULLTEXT KEY search_title (title), FULLTEXT KEY search_terms (search_terms) ) $charset_collate;"; $sql2 = "CREATE TABLE $log_table ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, keyword varchar(255) NOT NULL, hits bigint(20) unsigned NOT NULL DEFAULT 1, no_results bigint(20) unsigned NOT NULL DEFAULT 0, clicks bigint(20) unsigned NOT NULL DEFAULT 0, last_searched datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY keyword (keyword) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); dbDelta($sql2); update_option('awas_search_db_version', self::DB_VERSION); } }