| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -if (!defined('ABSPATH')) exit; // Exit if accessed directly | |
| 3 | 2 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 4 | 3 | require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 5 | 4 | } |
| 6 | 5 | |
| @@ -11,10 +10,10 @@ | ||
| 11 | 10 | /** Class constructor */ |
| 12 | 11 | public function __construct() { |
| 13 | 12 | |
| 14 | 13 | parent::__construct( [ |
| 15 | - 'singular' => __( 'Response', 'chatbot' ), //singular name of the listed records | |
| 16 | - 'plural' => __( 'Responses', 'chatbot' ), //plural name of the listed records | |
| 14 | + 'singular' => __( 'Response', 'wpchatbot' ), //singular name of the listed records | |
| 15 | + 'plural' => __( 'Responses', 'wpchatbot' ), //plural name of the listed records | |
| 17 | 16 | 'ajax' => false //does this table support ajax? |
| 18 | 17 | ] ); |
| 19 | 18 | |
| 20 | 19 | $this->chatbot_admin_page = admin_url('admin.php?page=simple-text-response'); |
| @@ -30,41 +29,26 @@ | ||
| 30 | 29 | * |
| 31 | 30 | * @return mixed |
| 32 | 31 | */ |
| 33 | 32 | public static function get_responses( $per_page = 5, $page_number = 1 ) { |
| 33 | + | |
| 34 | 34 | global $wpdb; |
| 35 | 35 | |
| 36 | - // Default order by and order | |
| 37 | - $orderby = 'id'; | |
| 38 | - $order = 'DESC'; | |
| 36 | + $sql = "SELECT * FROM {$wpdb->prefix}wpbot_response"; | |
| 39 | 37 | |
| 40 | - // Allow sorting by column and order if provided and valid | |
| 41 | 38 | if ( ! empty( $_REQUEST['orderby'] ) ) { |
| 42 | - // Whitelist allowed columns to prevent SQL injection | |
| 43 | - $allowed = array( 'id', 'intent', 'response', 'type' ); | |
| 44 | - $orderby_request = esc_sql( wp_unslash($_REQUEST['orderby']) ); | |
| 45 | - if ( in_array( $orderby_request, $allowed ) ) { | |
| 46 | - $orderby = $orderby_request; | |
| 47 | - } | |
| 39 | + $sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] ); | |
| 40 | + $sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC'; | |
| 48 | 41 | } |
| 49 | - if ( ! empty( $_REQUEST['order'] ) ) { | |
| 50 | - $order_request = strtoupper( esc_sql( wp_unslash($_REQUEST['order']) ) ); | |
| 51 | - if ( in_array( $order_request, array( 'ASC', 'DESC' ) ) ) { | |
| 52 | - $order = $order_request; | |
| 53 | - } | |
| 54 | - } | |
| 55 | 42 | |
| 56 | - $offset = ( $page_number - 1 ) * $per_page; | |
| 43 | + $sql .= " LIMIT $per_page"; | |
| 44 | + $sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page; | |
| 57 | 45 | |
| 58 | - // Build the SQL query with validated orderby and order | |
| 59 | - $sql = $wpdb->prepare( | |
| 60 | - "SELECT * FROM {$wpdb->prefix}wpbot_response ORDER BY $orderby $order LIMIT %d OFFSET %d", | |
| 61 | - $per_page, | |
| 62 | - $offset | |
| 63 | - ); | |
| 64 | 46 | |
| 65 | - $result = $wpdb->get_results( $sql, 'ARRAY_A' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 47 | + $result = $wpdb->get_results( $sql, 'ARRAY_A' ); | |
| 66 | 48 | |
| 49 | + | |
| 50 | + | |
| 67 | 51 | return $result; |
| 68 | 52 | } |
| 69 | 53 | |
| 70 | 54 | |
| @@ -75,14 +59,13 @@ | ||
| 75 | 59 | */ |
| 76 | 60 | public static function delete_response( $id ) { |
| 77 | 61 | global $wpdb; |
| 78 | 62 | |
| 79 | - $wpdb->delete( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 63 | + $wpdb->delete( | |
| 80 | 64 | "{$wpdb->prefix}wpbot_response", |
| 81 | 65 | [ 'id' => $id ], |
| 82 | 66 | [ '%d' ] |
| 83 | 67 | ); |
| 84 | - | |
| 85 | 68 | } |
| 86 | 69 | |
| 87 | 70 | |
| 88 | 71 | /** |
| @@ -94,15 +77,15 @@ | ||
| 94 | 77 | global $wpdb; |
| 95 | 78 | |
| 96 | 79 | $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}wpbot_response"; |
| 97 | 80 | |
| 98 | - return $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared | |
| 81 | + return $wpdb->get_var( $sql ); | |
| 99 | 82 | } |
| 100 | 83 | |
| 101 | 84 | |
| 102 | 85 | /** Text displayed when no customer data is available */ |
| 103 | 86 | public function no_items() { |
| 104 | - esc_html_e( 'No responses avaliable.', 'chatbot' ); | |
| 87 | + _e( 'No responses avaliable.', 'wpchatbot' ); | |
| 105 | 88 | } |
| 106 | 89 | |
| 107 | 90 | |
| 108 | 91 | /** |
| @@ -173,12 +156,12 @@ | ||
| 173 | 156 | $delete_nonce = wp_create_nonce( 'wp_delete_query' ); |
| 174 | 157 | $edit_nonce = wp_create_nonce( 'wp_edit_query' ); |
| 175 | 158 | |
| 176 | 159 | $title = '<strong>' . $item['query'] . '</strong>'; |
| 177 | -// var_dump($title); | |
| 160 | + | |
| 178 | 161 | $actions = [ |
| 179 | - 'edit' => sprintf( '<a href="?page=%s&action=%s&query=%s&_wpnonce=%s">Edit</a>', esc_attr(wp_unslash($_REQUEST['page'])), 'edit', absint( $item['id'] ), $edit_nonce ), | |
| 180 | - 'delete' => sprintf( '<a href="?page=%s&action=%s&query=%s&_wpnonce=%s">Delete</a>', esc_attr(wp_unslash($_REQUEST['page'])), 'delete', absint( $item['id'] ), $delete_nonce ) | |
| 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 ) | |
| 181 | 164 | ]; |
| 182 | 165 | |
| 183 | 166 | return $title . $this->row_actions( $actions ); |
| 184 | 167 | } |
| @@ -194,22 +177,22 @@ | ||
| 194 | 177 | |
| 195 | 178 | if(class_exists('Qcld_str_pro')){ |
| 196 | 179 | $columns = [ |
| 197 | 180 | 'cb' => '<input type="checkbox" />', |
| 198 | - 'query' => __( 'Query', 'chatbot' ), | |
| 199 | - 'keyword' => __( 'Keyword', 'chatbot' ), | |
| 200 | - 'responses' => __( 'Response', 'chatbot' ), | |
| 201 | - 'intent'=> __( 'Intent', 'chatbot' ), | |
| 202 | - 'category'=> __( 'Category', 'chatbot' ), | |
| 181 | + 'query' => __( 'Query', 'wpchatbot' ), | |
| 182 | + 'keyword' => __( 'Keyword', 'wpchatbot' ), | |
| 183 | + 'responses' => __( 'Response', 'wpchatbot' ), | |
| 184 | + 'intent'=> __( 'Intent', 'wpchatbot' ), | |
| 185 | + 'category'=> __( 'Category', 'wpchatbot' ), | |
| 203 | 186 | |
| 204 | 187 | ]; |
| 205 | 188 | }else{ |
| 206 | 189 | $columns = [ |
| 207 | 190 | 'cb' => '<input type="checkbox" />', |
| 208 | - 'query' => __( 'Query', 'chatbot' ), | |
| 209 | - 'keyword' => __( 'Keyword', 'chatbot' ), | |
| 210 | - 'responses' => __( 'Response', 'chatbot' ), | |
| 211 | - 'intent'=> __( 'Intent', 'chatbot' ), | |
| 191 | + 'query' => __( 'Query', 'wpchatbot' ), | |
| 192 | + 'keyword' => __( 'Keyword', 'wpchatbot' ), | |
| 193 | + 'responses' => __( 'Response', 'wpchatbot' ), | |
| 194 | + 'intent'=> __( 'Intent', 'wpchatbot' ), | |
| 212 | 195 | |
| 213 | 196 | ]; |
| 214 | 197 | } |
| 215 | 198 | |
| @@ -252,14 +235,10 @@ | ||
| 252 | 235 | $this->_column_headers = $this->get_column_info(); |
| 253 | 236 | |
| 254 | 237 | /** Process bulk action */ |
| 255 | 238 | $this->process_bulk_action(); |
| 256 | - if( !empty($_POST['wp_screen_options'] )){ | |
| 257 | - $per_page = (int)$_POST['wp_screen_options']["value"]; | |
| 258 | - }else{ | |
| 259 | - $per_page = $this->get_items_per_page( 'responses_per_page' ); | |
| 260 | - | |
| 261 | - } | |
| 239 | + | |
| 240 | + $per_page = $this->get_items_per_page( 'responses_per_page', 5 ); | |
| 262 | 241 | $current_page = $this->get_pagenum(); |
| 263 | 242 | $total_items = self::record_count(); |
| 264 | 243 | |
| 265 | 244 | $this->set_pagination_args( [ |
| @@ -277,15 +256,15 @@ | ||
| 277 | 256 | //Detect when a bulk action is being triggered... |
| 278 | 257 | if ( 'delete' === $this->current_action() ) { |
| 279 | 258 | |
| 280 | 259 | // In our file that handles the request, verify the nonce. |
| 281 | - $nonce = esc_attr(wp_unslash($_REQUEST['_wpnonce'])); | |
| 260 | + $nonce = esc_attr( $_REQUEST['_wpnonce'] ); | |
| 282 | 261 | |
| 283 | 262 | if ( ! wp_verify_nonce( $nonce, 'wp_delete_query' ) ) { |
| 284 | 263 | die( 'Go get a life script kiddies' ); |
| 285 | 264 | } |
| 286 | 265 | else { |
| 287 | - self::delete_response( absint( wp_unslash($_GET['query']) ) ); | |
| 266 | + self::delete_response( absint( $_GET['query'] ) ); | |
| 288 | 267 | |
| 289 | 268 | // esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 290 | 269 | // add_query_arg() return the current url |
| 291 | 270 | //wp_redirect( esc_url_raw($this->chatbot_admin_page) ); |
| @@ -294,13 +273,13 @@ | ||
| 294 | 273 | |
| 295 | 274 | } |
| 296 | 275 | |
| 297 | 276 | // If the delete bulk action is triggered |
| 298 | - if ( ( isset($_POST['action']) && wp_unslash($_POST['action']) == 'bulk-delete' ) | |
| 299 | - || ( isset($_POST['action2']) && wp_unslash($_POST['action2']) == 'bulk-delete' ) | |
| 277 | + if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) | |
| 278 | + || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) | |
| 300 | 279 | ) { |
| 301 | 280 | |
| 302 | - $delete_ids = esc_sql( wp_unslash($_POST['bulk-delete']) ); | |
| 281 | + $delete_ids = esc_sql( $_POST['bulk-delete'] ); | |
| 303 | 282 | |
| 304 | 283 | |
| 305 | 284 | // loop over the array of record IDs and delete them |
| 306 | 285 | foreach ( $delete_ids as $id ) { |
| @@ -309,10 +288,10 @@ | ||
| 309 | 288 | } |
| 310 | 289 | |
| 311 | 290 | // esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 312 | 291 | // add_query_arg() return the current url |
| 313 | - wp_safe_redirect( esc_url_raw($this->chatbot_admin_page) ); | |
| 292 | + wp_redirect( esc_url_raw($this->chatbot_admin_page) ); | |
| 314 | 293 | exit; |
| 315 | 294 | } |
| 316 | 295 | } |
| 317 | 296 | |
| 318 | 297 | } |