PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.4.9
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.4.9
8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 All 534 releases
chatbot / includes / chat-sessions / inc / chatsession-db-structure.php

chatsession-db-structure.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.4.9, at includes/chat-sessions/inc/chatsession-db-structure.php

204 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /******************************************
4 * DB Install
5 ******************************************/
6
7 global $wpdb;
8
9 $collate = '';
10
11 if ( $wpdb->has_cap( 'collation' ) ) {
12
13 if ( ! empty( $wpdb->charset ) ) {
14
15 $collate .= "DEFAULT CHARACTER SET $wpdb->charset";
16 }
17 if ( ! empty( $wpdb->collate ) ) {
18
19 $collate .= " COLLATE $wpdb->collate";
20
21 }
22 }
23
24 $table1 = $wpdb->prefix . 'wpbot_user';
25
26 if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table1 ) ) ) != $table1 ) {
27 $sql_sliders_Table1 = "
28 CREATE TABLE IF NOT EXISTS `$table1` (
29 `id` int(11) NOT NULL AUTO_INCREMENT,
30 `session_id` varchar(256) NOT NULL,
31 `name` varchar(256) NOT NULL,
32 `email` varchar(256) NOT NULL,
33 `phone` varchar(256) NOT NULL,
34 `date` datetime NOT NULL,
35 `user_id` int(11) NOT NULL,
36 `interaction` int(11) NOT NULL,
37 PRIMARY KEY (`id`)
38 ) $collate AUTO_INCREMENT=1 ";
39
40 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
41 dbDelta( $sql_sliders_Table1 );
42 }
43
44 $table1 = $wpdb->prefix . 'wpbot_conversation';
45
46 if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table1 ) ) ) != strtolower( $table1 ) ) {
47 $sql_sliders_Table1 = "
48 CREATE TABLE IF NOT EXISTS `$table1` (
49 `id` int(11) NOT NULL AUTO_INCREMENT,
50 `user_id` int(11) NOT NULL,
51 `conversation` LONGTEXT NOT NULL,
52 `interaction` int(11) NOT NULL,
53 `environment_info` LONGTEXT NOT NULL,
54 PRIMARY KEY (`id`)
55 ) $collate AUTO_INCREMENT=1 ";
56
57 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
58 dbDelta( $sql_sliders_Table1 );
59 }
60
61 $table_failed_response = $wpdb->prefix . 'wpbot_failed_response';
62
63 if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_failed_response ) ) ) != $table_failed_response ) {
64 $sql_failed_response = "
65 CREATE TABLE IF NOT EXISTS `$table_failed_response` (
66 `id` int(11) NOT NULL AUTO_INCREMENT,
67 `query` varchar(256) NOT NULL,
68 `count` int(11) NOT NULL,
69 `status` int(11) NOT NULL,
70 PRIMARY KEY (`id`)
71 ) $collate AUTO_INCREMENT=1 ";
72
73 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
74 dbDelta( $sql_failed_response );
75 }
76
77 /******************************************
78 * Helper Function: Check if a Database Column Exists
79 */
80 if ( ! function_exists( 'qcwp_isset_table_column' ) ) {
81
82 function qcwp_isset_table_column( $table_name, $column_name ) {
83 global $wpdb;
84
85 $columns = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM %i', $table_name ), ARRAY_A );
86
87 foreach ( $columns as $column ) {
88 if ( $column['Field'] == $column_name ) {
89 return true;
90 }
91 }
92 }
93 }
94
95 /******************************************
96 * When the main plugin is activated,
97 * Build the necessary DB Structure.
98 */
99 register_activation_hook( __FILE__, 'qcld_wb_chatboot_sessions_defualt_options' );
100
101 function qcld_wb_chatboot_sessions_defualt_options() {
102 global $wpdb;
103
104 $collate = '';
105
106 if ( $wpdb->has_cap( 'collation' ) ) {
107
108 if ( ! empty( $wpdb->charset ) ) {
109
110 $collate .= "DEFAULT CHARACTER SET $wpdb->charset";
111 }
112 if ( ! empty( $wpdb->collate ) ) {
113
114 $collate .= " COLLATE $wpdb->collate";
115
116 }
117 }
118
119 // Create Table: wpbot_user
120
121 $table1 = $wpdb->prefix . 'wpbot_user';
122
123 $sql_sliders_Table1 = "
124 CREATE TABLE IF NOT EXISTS `$table1` (
125 `id` int(11) NOT NULL AUTO_INCREMENT,
126 `session_id` varchar(256) NOT NULL,
127 `name` varchar(256) NOT NULL,
128 `email` varchar(256) NOT NULL,
129 `phone` varchar(256) NOT NULL,
130 `date` datetime NOT NULL,
131 `user_id` int(11) NOT NULL,
132 `interaction` int(11) NOT NULL,
133 PRIMARY KEY (`id`)
134 ) $collate AUTO_INCREMENT=1 ";
135
136 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
137
138 dbDelta( $sql_sliders_Table1 );
139
140 if ( ! qcwp_isset_table_column( $table1, 'phone' ) ) {
141 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD `phone` varchar(256) NOT NULL', $table1 ) );
142 }
143
144 // Create Table: wpbot_conversation
145
146 $table2 = $wpdb->prefix . 'wpbot_conversation';
147
148 $sql_sliders_Table2 = "
149 CREATE TABLE IF NOT EXISTS `$table2` (
150 `id` int(11) NOT NULL AUTO_INCREMENT,
151 `user_id` int(11) NOT NULL,
152 `conversation` LONGTEXT NOT NULL,
153 `interaction` int(11) NOT NULL,
154 `environment_info` LONGTEXT NOT NULL,
155 PRIMARY KEY (`id`)
156 ) $collate AUTO_INCREMENT=1 ";
157
158 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
159
160 dbDelta( $sql_sliders_Table2 );
161
162 if ( ! qcwp_isset_table_column( $table2, 'interaction' ) ) {
163 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD `interaction` int(11) NOT NULL', $table2 ) );
164 }
165
166 if ( ! qcwp_isset_table_column( $table1, 'interaction' ) ) {
167 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD `interaction` int(11) NOT NULL', $table1 ) );
168 }
169 }
170
171 /******************************************
172 * Update DB Structure, If Necessary
173 ******************************************/
174 function qcldwpbot_chatsession_db_update_check() {
175
176 global $wpdb;
177
178 $version = get_option( 'wpbot_chatsession_db_version', '1.0' );
179
180 $table1 = $wpdb->prefix . 'wpbot_user';
181
182 $table2 = $wpdb->prefix . 'wpbot_conversation';
183
184 if ( version_compare( $version, '2.0' ) < 0 ) {
185
186 if ( ! qcwp_isset_table_column( $table1, 'interaction' ) ) {
187 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD `interaction` int(11) NOT NULL', $table1 ) );
188 }
189
190 if ( ! qcwp_isset_table_column( $table2, 'interaction' ) ) {
191 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD `interaction` int(11) NOT NULL', $table2 ) );
192 }
193
194 if ( ! qcwp_isset_table_column( $table2, 'environment_info' ) ) {
195 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD `environment_info` LONGTEXT NOT NULL', $table2 ) );
196 }
197
198 update_option( 'wpbot_chatsession_db_version', '2.0' );
199
200 }
201 }
202
203 add_action( 'plugins_loaded', 'qcldwpbot_chatsession_db_update_check' );
204