PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services / 3.2.9
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services v3.2.9
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 8.5.0 8.4.9 All 531 releases
chatbot / includes / class-response-list.php

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

264 lines 6.2 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 switch ( $column_name ) {
103 case 'query':
104 case 'keyword':
105 return $item[ $column_name ];
106
107 case 'responses':
108 return $item[ 'response' ];
109 default:
110 return print_r( $item, true ); //Show the whole array for troubleshooting purposes
111 }
112 }
113
114 /**
115 * Render the bulk edit checkbox
116 *
117 * @param array $item
118 *
119 * @return string
120 */
121 function column_cb( $item ) {
122 return sprintf(
123 '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['id']
124 );
125 }
126
127
128 /**
129 * Method for name column
130 *
131 * @param array $item an array of DB data
132 *
133 * @return string
134 */
135 function column_query( $item ) {
136
137 $delete_nonce = wp_create_nonce( 'wp_delete_query' );
138 $edit_nonce = wp_create_nonce( 'wp_edit_query' );
139
140 $title = '<strong>' . $item['query'] . '</strong>';
141
142 $actions = [
143 'edit' => sprintf( '<a href="?page=%s&action=%s&query=%s&_wpnonce=%s">Edit</a>', esc_attr( $_REQUEST['page'] ), 'edit', absint( $item['id'] ), $edit_nonce ),
144 'delete' => sprintf( '<a href="?page=%s&action=%s&query=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['id'] ), $delete_nonce )
145 ];
146
147 return $title . $this->row_actions( $actions );
148 }
149
150
151 /**
152 * Associative array of columns
153 *
154 * @return array
155 */
156 function get_columns() {
157 $columns = [
158 'cb' => '<input type="checkbox" />',
159 'query' => __( 'Query', 'wpchatbot' ),
160 'keyword' => __( 'Keyword', 'wpchatbot' ),
161 'responses' => __( 'Response', 'wpchatbot' ),
162
163
164 ];
165
166 return $columns;
167 }
168
169
170 /**
171 * Columns to make sortable.
172 *
173 * @return array
174 */
175 public function get_sortable_columns() {
176 $sortable_columns = array(
177 'query' => array( 'query', true ),
178 );
179
180 return $sortable_columns;
181 }
182
183 /**
184 * Returns an associative array containing the bulk action
185 *
186 * @return array
187 */
188 public function get_bulk_actions() {
189 $actions = [
190 'bulk-delete' => 'Delete'
191 ];
192
193 return $actions;
194 }
195
196
197 /**
198 * Handles data query and filter, sorting, and pagination.
199 */
200 public function prepare_items() {
201
202 $this->_column_headers = $this->get_column_info();
203
204 /** Process bulk action */
205 $this->process_bulk_action();
206
207 $per_page = $this->get_items_per_page( 'responses_per_page', 5 );
208 $current_page = $this->get_pagenum();
209 $total_items = self::record_count();
210
211 $this->set_pagination_args( [
212 'total_items' => $total_items, //WE have to calculate the total number of items
213 'per_page' => $per_page //WE have to determine how many items to show on a page
214 ] );
215
216 $this->items = self::get_responses( $per_page, $current_page );
217 }
218
219 public function process_bulk_action() {
220
221
222
223 //Detect when a bulk action is being triggered...
224 if ( 'delete' === $this->current_action() ) {
225
226 // In our file that handles the request, verify the nonce.
227 $nonce = esc_attr( $_REQUEST['_wpnonce'] );
228
229 if ( ! wp_verify_nonce( $nonce, 'wp_delete_query' ) ) {
230 die( 'Go get a life script kiddies' );
231 }
232 else {
233 self::delete_response( absint( $_GET['query'] ) );
234
235 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
236 // add_query_arg() return the current url
237 wp_redirect( esc_url_raw($this->chatbot_admin_page) );
238 exit;
239 }
240
241 }
242
243 // If the delete bulk action is triggered
244 if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' )
245 || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )
246 ) {
247
248 $delete_ids = esc_sql( $_POST['bulk-delete'] );
249
250
251 // loop over the array of record IDs and delete them
252 foreach ( $delete_ids as $id ) {
253 self::delete_response( $id );
254
255 }
256
257 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
258 // add_query_arg() return the current url
259 wp_redirect( esc_url_raw($this->chatbot_admin_page) );
260 exit;
261 }
262 }
263
264 }