PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 3.4.2
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v3.4.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 3.4.2, at includes/class-response-list.php

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