PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 5.0.2
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v5.0.2
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 8.5.2 All 533 releases
chatbot / includes / class-response-list.php

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

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