*/ // Loading WP_List_Table class file // We need to load it as it's not automatically loaded by WordPress if (!class_exists('WP_List_Table')) { require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); } class Aitoce_Agent extends WP_List_Table { private $account_membership; // define $table_data property private $table_data; // Define table columns public function get_columns() { $columns = [ 'cb' => '', 'agent_name' => __('Name', 'ai-tool-center'), 'agent_description' => __('Description', 'ai-tool-center'), 'agent_provider' => __('Provider', 'ai-tool-center'), 'agent_model' => __('Model', 'ai-tool-center'), 'agent_custom_interface' => __('Interface', 'ai-tool-center'), 'agent_styling' => __('Styling', 'ai-tool-center'), 'agent_shortcode' => __('Shortcode', 'ai-tool-center') ]; return $columns; } // Bind table with columns, data and all public function prepare_items() { // phpcs:disable WordPress.Security.NonceVerification // Delete agents ((isset($_POST['action']) && $_POST['action'] === 'delete_all') || (isset($_GET['action']) && $_GET['action'] === 'delete_agent'))? $this->aitoce_delete_agent():''; // Search agents if required $search = isset($_POST['s'])? sanitize_text_field(wp_unslash($_POST['s'])):''; !empty($search)? $this->table_data = $this->aitoce_get_table_data($search) : $this->table_data = $this->aitoce_get_table_data(); $columns = $this->get_columns(); $hidden = ( is_array(get_user_meta( get_current_user_id(), 'manageai-tool-center_page_'.AITOCE_MENU_SLUG_AGENTS.'columnshidden', true)) ) ? get_user_meta( get_current_user_id(), 'manageai-tool-center_page_'.AITOCE_MENU_SLUG_AGENTS.'columnshidden', true) : ['agent_custom_interface']; $sortable = $this->get_sortable_columns(); $primary = 'agent_name'; $this->_column_headers = [$columns, $hidden, $sortable, $primary]; usort($this->table_data, [&$this, 'usort_reorder']); /* pagination */ $per_page_option = get_current_screen()->get_option('per_page'); $per_page = get_user_meta( get_current_user_id(), $per_page_option['option'], true ) ?: $per_page_option['default']; $current_page = $this->get_pagenum(); $total_items = count($this->table_data); $this->table_data = array_slice($this->table_data, ($current_page - 1) * $per_page, $per_page); $this->set_pagination_args(array( 'total_items' => $total_items, // total number of items 'per_page' => $per_page, // items to show on a page 'total_pages' => ceil( $total_items / $per_page ) // use ceil to round up )); $this->items = $this->table_data; } private function aitoce_get_table_data($search = '') { // phpcs:disable WordPress.DB global $wpdb; // Need to load default settings manually $settings = new Aitoce_Agent_Settings(); $table_name = esc_sql($wpdb->prefix . AITOCE_AGENT_TABLE); if ( !empty($search) ) { $search = esc_sql($search); $sql = "SELECT * FROM {$table_name} WHERE agent_name Like '%{$search}%' OR agent_description Like '%{$search}%'"; $result_data = (array) $wpdb->get_results($sql, ARRAY_A ); }else{ $sql = "SELECT * FROM {$table_name}"; $result_data = (array) $wpdb->get_results($sql, ARRAY_A ); } // Translate the data to display in the list for ($i=0; $i < count($result_data); $i++) { $result_data[$i]['agent_description'] = (!empty($result_data[$i]['agent_description']))?substr($result_data[$i]['agent_description'], 0, 80).'...':''; $result_data[$i]['agent_shortcode'] = '[nimbot-assistant id='.$result_data[$i]['agent_id'].'] '; $result_data[$i]['agent_provider'] = $settings->providers[$result_data[$i]['agent_provider']]; $result_data[$i]['agent_custom_interface'] = $settings->interfaces[$result_data[$i]['agent_custom_interface']]; $style = json_decode($result_data[$i]['agent_styling'], true); $result_data[$i]['agent_styling'] = '