| 1 |
<?php |
| 2 |
|
| 3 |
if (!class_exists('WP_List_Table')) { |
| 4 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); |
| 5 |
} |
| 6 |
|
| 7 |
class Mbe_Shipping_Csv_Editor extends WP_List_Table |
| 8 |
{ |
| 9 |
protected $tableName = ''; |
| 10 |
protected $title; |
| 11 |
protected $csvType; |
| 12 |
protected $default; |
| 13 |
protected $columns = []; |
| 14 |
protected $sortable_columns = []; |
| 15 |
protected $backLink = 'mbe_welcome'; |
| 16 |
protected $isRemote = false; |
| 17 |
protected $idColumn = 'id'; |
| 18 |
protected $sql = ''; |
| 19 |
|
| 20 |
|
| 21 |
public function __construct( $args = array() ) { |
| 22 |
global $wpdb; |
| 23 |
$this->tableName = $wpdb->prefix . $this->tableName; |
| 24 |
parent::__construct( $args ); |
| 25 |
} |
| 26 |
|
| 27 |
/** Function to be overridden by the child class |
| 28 |
* |
| 29 |
*/ |
| 30 |
public function get_remoteRows($orderBy = null, $order = 'asc'): array { |
| 31 |
return []; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Prepares the list of items for displaying. |
| 36 |
*/ |
| 37 |
public function prepare_items() { |
| 38 |
global $wpdb; |
| 39 |
|
| 40 |
$this->_column_headers = [ |
| 41 |
$this->get_columns(), |
| 42 |
array(), // hidden |
| 43 |
$this->get_sortable_columns() |
| 44 |
]; |
| 45 |
$this->process_bulk_action(); // Process bulk actioin or single row actions |
| 46 |
$order = $this->get_items_query_order(); |
| 47 |
|
| 48 |
if($this->isRemote) { |
| 49 |
$this->items = $this->get_remoteRows(sanitize_text_field($_REQUEST['orderby'])??null, sanitize_text_field($_REQUEST['order'])??null); |
| 50 |
} else { |
| 51 |
$this->items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i {$order}", $this->tableName), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 52 |
} |
| 53 |
|
| 54 |
$this->set_pagination_args([]); |
| 55 |
} |
| 56 |
|
| 57 |
public function get_backlink() { |
| 58 |
return $this->backLink; |
| 59 |
} |
| 60 |
|
| 61 |
public function get_isRemote() { |
| 62 |
return $this->isRemote; |
| 63 |
} |
| 64 |
|
| 65 |
public function get_ID() { |
| 66 |
return $this->idColumn; |
| 67 |
} |
| 68 |
|
| 69 |
public function get_tablename(){ |
| 70 |
return $this->tableName; |
| 71 |
} |
| 72 |
public function get_sql() { |
| 73 |
return $this->sql; |
| 74 |
} |
| 75 |
|
| 76 |
public function get_title($singular = true){ |
| 77 |
if($singular) { |
| 78 |
return ucwords(__($this->_args['singular'], 'mail-boxes-etc')); |
| 79 |
} |
| 80 |
return ucwords(__($this->_args['plural'], 'mail-boxes-etc')); |
| 81 |
} |
| 82 |
|
| 83 |
public function get_defaults() |
| 84 |
{ |
| 85 |
return $this->default; |
| 86 |
} |
| 87 |
|
| 88 |
function get_columns() |
| 89 |
{ |
| 90 |
return $this->columns; |
| 91 |
} |
| 92 |
|
| 93 |
function get_sortable_columns() |
| 94 |
{ |
| 95 |
return $this->sortable_columns; |
| 96 |
} |
| 97 |
|
| 98 |
function get_bulk_actions() |
| 99 |
{ |
| 100 |
return array( |
| 101 |
'delete' => 'Delete' |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
function process_bulk_action() |
| 106 |
{ |
| 107 |
global $wpdb; |
| 108 |
$table_name = $this->tableName; |
| 109 |
|
| 110 |
if ('delete' === $this->current_action()) { |
| 111 |
$ids = isset($_REQUEST['id']) ? (array)wc_clean($_REQUEST['id']) : []; |
| 112 |
|
| 113 |
if (!empty($ids)) { |
| 114 |
do_action(MBE_ESHIP_ID . '_csv_editor_deleting', $ids); |
| 115 |
if (!$this->isRemote) { |
| 116 |
$wpdb->query( $wpdb->prepare("DELETE FROM %i WHERE %i IN ( " . implode( ',', $ids ) . " )" , $table_name, $this->get_ID()) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 117 |
} |
| 118 |
do_action(MBE_ESHIP_ID . '_csv_editor_deleted', $ids, $this->csvType); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
function column_default( $item, $column_name ) { |
| 124 |
esc_html_e($item[$column_name]); |
| 125 |
} |
| 126 |
|
| 127 |
function column_id($item) |
| 128 |
{ |
| 129 |
$actions = array( |
| 130 |
'edit' => sprintf( '<a href="?page=' . MBE_ESHIP_ID . '_csv_edit_form&csv=%s&id=%s&nonce=%s">%s</a>', sanitize_text_field($_REQUEST['csv']), $item[$this->get_ID()], wp_create_nonce('csv_form_page_handler'), __('Edit', 'mail-boxes-etc')), |
| 131 |
'delete' => sprintf('<a href="?page=%s&action=delete&csv=%s&id=%s&nonce=%s">%s</a>', sanitize_text_field($_REQUEST['page']), sanitize_text_field($_REQUEST['csv']), $item[$this->get_ID()], wp_create_nonce($_REQUEST['page']), __('Delete', 'mail-boxes-etc')), |
| 132 |
); |
| 133 |
|
| 134 |
return sprintf('%s %s', |
| 135 |
$item[$this->get_ID()], |
| 136 |
$this->row_actions($actions) |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
function column_cb($item) |
| 141 |
{ |
| 142 |
return sprintf( |
| 143 |
'<input type="checkbox" name="id[]" value="%s" />', |
| 144 |
$item[$this->get_ID()] |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
protected function get_items_query_order() { |
| 149 |
if ( ! empty( $_REQUEST['orderby'] ) && in_array( sanitize_text_field($_REQUEST['orderby']), array_keys($this->sortable_columns) ) ) { |
| 150 |
$by =esc_sql( wc_clean( $_REQUEST['orderby'] )); |
| 151 |
|
| 152 |
if ( ! empty( $_REQUEST['order'] ) && 'asc' === strtolower( sanitize_text_field($_REQUEST['order']) ) ) { |
| 153 |
$order = 'ASC'; |
| 154 |
} else { |
| 155 |
$order = 'DESC'; |
| 156 |
} |
| 157 |
|
| 158 |
return "ORDER BY {$by} {$order}"; |
| 159 |
} |
| 160 |
return 'ORDER BY ID ASC'; |
| 161 |
} |
| 162 |
|
| 163 |
protected function extra_tablenav( $which ) { |
| 164 |
static $has_items; |
| 165 |
|
| 166 |
if ( ! isset( $has_items ) ) { |
| 167 |
$has_items = $this->has_items(); |
| 168 |
} |
| 169 |
|
| 170 |
echo '<div class="alignright actions">'; |
| 171 |
|
| 172 |
if ( 'top' === $which ) { |
| 173 |
echo sprintf( '<a class="add-new-h2" href="?page=' . esc_attr(MBE_ESHIP_ID) . '_csv_edit_form&action=new&csv=' . esc_attr($this->csvType) . '&id=%s&nonce=%s">%s</a>', null, wp_create_nonce('csv_form_page_handler'), esc_html__('Add', 'mail-boxes-etc') . ' ' . esc_html($this->get_title())); |
| 174 |
} |
| 175 |
|
| 176 |
echo '</div>'; |
| 177 |
} |
| 178 |
|
| 179 |
public function validate_row($item){} |
| 180 |
|
| 181 |
public function has_duplicates($field, $value, $type, $id = null) |
| 182 |
{ |
| 183 |
if(!$this->get_isRemote()) { |
| 184 |
global $wpdb; |
| 185 |
$idParam = ! empty( $id ) ? " AND id <> $id" : ''; |
| 186 |
$wpdb->get_results( $wpdb->prepare( "SELECT id FROM %i WHERE $field = $type $idParam", $this->tableName, $value ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 187 |
|
| 188 |
return $wpdb->num_rows > 0; |
| 189 |
} |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
public function isReservedCode($value) |
| 194 |
{ |
| 195 |
if($value === Mbe_Shipping_Helper_Data::MBE_CSV_PACKAGES_RESERVED_CODE) { |
| 196 |
return true; |
| 197 |
} |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
protected function sortRemoteRows($remoteRows, $orderBy, $order) { |
| 202 |
usort( $remoteRows, function ( $a, $b ) use ( $orderBy, $order ) { |
| 203 |
if ( $order === 'desc' ) { |
| 204 |
return $a[ $orderBy ] < $b[ $orderBy ] ? 1 : - 1; |
| 205 |
} else { |
| 206 |
return $a[ $orderBy ] < $b[ $orderBy ] ? - 1 : 1; |
| 207 |
} |
| 208 |
} ); |
| 209 |
|
| 210 |
return $remoteRows; |
| 211 |
} |
| 212 |
|
| 213 |
} |