PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 7.8.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v7.8.0
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 / class-response-list.php

class-response-list.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 7.8.0, at includes/class-response-list.php

317 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! class_exists( 'WP_List_Table' ) ) {
3 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
4 }
5
6 class Response_list extends WP_List_Table {
7
8
9 public $chatbot_admin_page;
10 /** Class constructor */
11 public function __construct() {
12
13 parent::__construct( [
14 'singular' => __( 'Response', 'chatbot' ), //singular name of the listed records
15 'plural' => __( 'Responses', 'chatbot' ), //plural name of the listed records
16 'ajax' => false //does this table support ajax?
17 ] );
18
19 $this->chatbot_admin_page = admin_url('admin.php?page=simple-text-response');
20
21 }
22
23
24 /**
25 * Retrieve customers data from the database
26 *
27 * @param int $per_page
28 * @param int $page_number
29 *
30 * @return mixed
31 */
32 public static function get_responses( $per_page = 5, $page_number = 1 ) {
33 global $wpdb;
34
35 // Default order by and order
36 $orderby = 'id';
37 $order = 'DESC';
38
39 // Allow sorting by column and order if provided and valid
40 if ( ! empty( $_REQUEST['orderby'] ) ) {
41 // Whitelist allowed columns to prevent SQL injection
42 $allowed = array( 'id', 'intent', 'response', 'type' );
43 $orderby_request = esc_sql( $_REQUEST['orderby'] );
44 if ( in_array( $orderby_request, $allowed ) ) {
45 $orderby = $orderby_request;
46 }
47 }
48 if ( ! empty( $_REQUEST['order'] ) ) {
49 $order_request = strtoupper( esc_sql( $_REQUEST['order'] ) );
50 if ( in_array( $order_request, array( 'ASC', 'DESC' ) ) ) {
51 $order = $order_request;
52 }
53 }
54
55 $offset = ( $page_number - 1 ) * $per_page;
56
57 // Build the SQL query with validated orderby and order
58 $sql = $wpdb->prepare(
59 "SELECT * FROM {$wpdb->prefix}wpbot_response ORDER BY $orderby $order LIMIT %d OFFSET %d",
60 $per_page,
61 $offset
62 );
63
64 $result = $wpdb->get_results( $sql, 'ARRAY_A' ); //DB Call OK, No Caching OK
65
66 return $result;
67 }
68
69
70 /**
71 * Delete a customer record.
72 *
73 * @param int $id customer ID
74 */
75 public static function delete_response( $id ) {
76 global $wpdb;
77
78 $wpdb->delete(
79 "{$wpdb->prefix}wpbot_response",
80 [ 'id' => $id ],
81 [ '%d' ]
82 ); //DB Call OK, No Caching OK
83
84 }
85
86
87 /**
88 * Returns the count of records in the database.
89 *
90 * @return null|string
91 */
92 public static function record_count() {
93 global $wpdb;
94
95 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}wpbot_response";
96
97 return $wpdb->get_var( $sql ); //DB Call OK, No Caching OK
98 }
99
100
101 /** Text displayed when no customer data is available */
102 public function no_items() {
103 esc_html_e( 'No responses avaliable.', 'chatbot' );
104 }
105
106
107 /**
108 * Render a column when no column specific method exist.
109 *
110 * @param array $item
111 * @param string $column_name
112 *
113 * @return mixed
114 */
115 public function column_default( $item, $column_name ) {
116
117
118
119
120 if(class_exists('Qcld_str_pro')){
121 switch ( $column_name ) {
122 case 'query':
123 case 'keyword':
124 case 'intent':
125 case 'category':
126 return $item[ $column_name ];
127
128 case 'responses':
129 return $item[ 'response' ];
130 default:
131 return print_r( $item, true ); //Show the whole array for troubleshooting purposes
132 }
133 }else{
134 switch ( $column_name ) {
135 case 'query':
136 case 'keyword':
137 case 'intent':
138 return $item[ $column_name ];
139
140 case 'responses':
141 return $item[ 'response' ];
142 default:
143 return print_r( $item, true ); //Show the whole array for troubleshooting purposes
144 }
145 }
146
147 }
148
149 /**
150 * Render the bulk edit checkbox
151 *
152 * @param array $item
153 *
154 * @return string
155 */
156 function column_cb( $item ) {
157 return sprintf(
158 '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['id']
159 );
160 }
161
162
163 /**
164 * Method for name column
165 *
166 * @param array $item an array of DB data
167 *
168 * @return string
169 */
170 function column_query( $item ) {
171
172 $delete_nonce = wp_create_nonce( 'wp_delete_query' );
173 $edit_nonce = wp_create_nonce( 'wp_edit_query' );
174
175 $title = '<strong>' . $item['query'] . '</strong>';
176 // var_dump($title);
177 $actions = [
178 'edit' => sprintf( '<a href="?page=%s&action=%s&query=%s&_wpnonce=%s">Edit</a>', esc_attr( $_REQUEST['page'] ), 'edit', absint( $item['id'] ), $edit_nonce ),
179 'delete' => sprintf( '<a href="?page=%s&action=%s&query=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['id'] ), $delete_nonce )
180 ];
181
182 return $title . $this->row_actions( $actions );
183 }
184
185
186 /**
187 * Associative array of columns
188 *
189 * @return array
190 */
191 function get_columns() {
192
193
194 if(class_exists('Qcld_str_pro')){
195 $columns = [
196 'cb' => '<input type="checkbox" />',
197 'query' => __( 'Query', 'chatbot' ),
198 'keyword' => __( 'Keyword', 'chatbot' ),
199 'responses' => __( 'Response', 'chatbot' ),
200 'intent'=> __( 'Intent', 'chatbot' ),
201 'category'=> __( 'Category', 'chatbot' ),
202
203 ];
204 }else{
205 $columns = [
206 'cb' => '<input type="checkbox" />',
207 'query' => __( 'Query', 'chatbot' ),
208 'keyword' => __( 'Keyword', 'chatbot' ),
209 'responses' => __( 'Response', 'chatbot' ),
210 'intent'=> __( 'Intent', 'chatbot' ),
211
212 ];
213 }
214
215 return $columns;
216 }
217
218
219 /**
220 * Columns to make sortable.
221 *
222 * @return array
223 */
224 public function get_sortable_columns() {
225 $sortable_columns = array(
226 'query' => array( 'query', true ),
227 );
228
229 return $sortable_columns;
230 }
231
232 /**
233 * Returns an associative array containing the bulk action
234 *
235 * @return array
236 */
237 public function get_bulk_actions() {
238 $actions = [
239 'bulk-delete' => 'Delete'
240 ];
241
242 return $actions;
243 }
244
245
246 /**
247 * Handles data query and filter, sorting, and pagination.
248 */
249 public function prepare_items() {
250
251 $this->_column_headers = $this->get_column_info();
252
253 /** Process bulk action */
254 $this->process_bulk_action();
255 if( !empty($_POST['wp_screen_options'] )){
256 $per_page = (int)$_POST['wp_screen_options']["value"];
257 }else{
258 $per_page = $this->get_items_per_page( 'responses_per_page' );
259
260 }
261 $current_page = $this->get_pagenum();
262 $total_items = self::record_count();
263
264 $this->set_pagination_args( [
265 'total_items' => $total_items, //WE have to calculate the total number of items
266 'per_page' => $per_page //WE have to determine how many items to show on a page
267 ] );
268
269 $this->items = self::get_responses( $per_page, $current_page );
270 }
271
272 public function process_bulk_action() {
273
274
275
276 //Detect when a bulk action is being triggered...
277 if ( 'delete' === $this->current_action() ) {
278
279 // In our file that handles the request, verify the nonce.
280 $nonce = esc_attr( $_REQUEST['_wpnonce'] );
281
282 if ( ! wp_verify_nonce( $nonce, 'wp_delete_query' ) ) {
283 die( 'Go get a life script kiddies' );
284 }
285 else {
286 self::delete_response( absint( $_GET['query'] ) );
287
288 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
289 // add_query_arg() return the current url
290 //wp_redirect( esc_url_raw($this->chatbot_admin_page) );
291 //exit;
292 }
293
294 }
295
296 // If the delete bulk action is triggered
297 if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' )
298 || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )
299 ) {
300
301 $delete_ids = esc_sql( $_POST['bulk-delete'] );
302
303
304 // loop over the array of record IDs and delete them
305 foreach ( $delete_ids as $id ) {
306 self::delete_response( $id );
307
308 }
309
310 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
311 // add_query_arg() return the current url
312 wp_redirect( esc_url_raw($this->chatbot_admin_page) );
313 exit;
314 }
315 }
316
317 }