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

301 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 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 esc_html_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 if( !empty($_POST['wp_screen_options'] )){
240 $per_page = (int)$_POST['wp_screen_options']["value"];
241 }else{
242 $per_page = $this->get_items_per_page( 'responses_per_page' );
243
244 }
245 $current_page = $this->get_pagenum();
246 $total_items = self::record_count();
247
248 $this->set_pagination_args( [
249 'total_items' => $total_items, //WE have to calculate the total number of items
250 'per_page' => $per_page //WE have to determine how many items to show on a page
251 ] );
252
253 $this->items = self::get_responses( $per_page, $current_page );
254 }
255
256 public function process_bulk_action() {
257
258
259
260 //Detect when a bulk action is being triggered...
261 if ( 'delete' === $this->current_action() ) {
262
263 // In our file that handles the request, verify the nonce.
264 $nonce = esc_attr( $_REQUEST['_wpnonce'] );
265
266 if ( ! wp_verify_nonce( $nonce, 'wp_delete_query' ) ) {
267 die( 'Go get a life script kiddies' );
268 }
269 else {
270 self::delete_response( absint( $_GET['query'] ) );
271
272 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
273 // add_query_arg() return the current url
274 //wp_redirect( esc_url_raw($this->chatbot_admin_page) );
275 //exit;
276 }
277
278 }
279
280 // If the delete bulk action is triggered
281 if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' )
282 || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )
283 ) {
284
285 $delete_ids = esc_sql( $_POST['bulk-delete'] );
286
287
288 // loop over the array of record IDs and delete them
289 foreach ( $delete_ids as $id ) {
290 self::delete_response( $id );
291
292 }
293
294 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
295 // add_query_arg() return the current url
296 wp_redirect( esc_url_raw($this->chatbot_admin_page) );
297 exit;
298 }
299 }
300
301 }