Skip to content

Commit

Permalink
Properly handle reset row action
Browse files Browse the repository at this point in the history
  • Loading branch information
xxsimoxx committed Apr 9, 2020
1 parent f271cd9 commit 431f2f0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
47 changes: 0 additions & 47 deletions classes/ListTable.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,52 +73,6 @@ function get_sortable_columns() {
return $sortable_columns;
}

// Deal with "reset" action.
function process_bulk_action() {

// Security checks.
if ($this->current_action() !== 'delete') {
return;
}
if (!check_admin_referer('delete', '_sfum')) {
return;
}
if (!current_user_can('manage_options')) {
return;
}

// Find the slug and title.
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
$id = (int) $id;
$slug = get_post_meta($id, 'id', true);
$name = get_the_title($id);
if ($slug === '') {
return;
}

// Delete from DB.
$where = ['slug' => $slug];
global $wpdb;
$wpdb->delete($wpdb->prefix.DB_TABLE_NAME, $where);

// Delete from the already build array.
foreach ($this->data as $index => $val) {
if ($val['identifier'] === $slug) {
unset($this->data[$index]);
}
}

// Give feedback to the user.
// Translators: %1$s is plugin or theme name.
echo '<div class="notice notice-success is-dismissible"><p>'.sprintf(__('Statistics for %1$s has been successfully reset.', 'stats-for-update-manager'), $name).'</p></div>';

// Remove delete action from query args.
echo '<script>';
echo 'window.history.pushState({}, document.title, "'.remove_query_arg(['action', 'id', '_sfum']).'");';
echo '</script>';

}

// Filter plugin or themes for display.
function filter_data($data) {
$list = [];
Expand Down Expand Up @@ -196,7 +150,6 @@ function prepare_items() {
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$this->_column_headers = [$columns, $hidden, $sortable];
$this->process_bulk_action();
$data = $this->filter_data($this->data);
usort($data, [&$this, 'reorder']);
$this->items = $data;
Expand Down
51 changes: 51 additions & 0 deletions stats-for-update-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,51 @@ public function create_menu() {
[$this, 'render_page']
);

// Remove action from url.
add_action('load-'.$this->screen, [$this, 'delete_action']);

}

// Deal with Reset row action.
public function delete_action() {

// Sanity check.
if (!isset($_GET['action'])) {
return;
}
if ($_GET['action'] !== 'delete') {
return;
}
if (!check_admin_referer('delete', '_sfum')) {
return;
}
if (!current_user_can('manage_options')) {
return;
}
if (!isset($_REQUEST['id'])) {
return;
}
$id = (int) $_REQUEST['id'];

// Find the slug and title.
$slug = get_post_meta($id, 'id', true);
$name = get_the_title($id);
if ($slug === '') {
return;
}

// Delete from DB.
global $wpdb;
$where = ['slug' => $slug];
$deleted = $wpdb->delete($wpdb->prefix.DB_TABLE_NAME, $where);

// Redirect to right url.
$sendback = remove_query_arg(['action', 'id', '_sfum'], wp_get_referer());
if ($deleted > 0) {
$sendback = add_query_arg('deleted', urlencode($name), $sendback);
}
wp_redirect($sendback);

}

// Enqueue CSS for debug section only in the page and only if WP_DEBUG is true.
Expand All @@ -396,6 +441,12 @@ public function render_page() {
echo '<div class="wrap">';
echo '<h1 class="wp-heading-inline" style="margin-bottom:10px;">'.esc_html_x('Update Manager &#8211; Statistics', 'Page Title', 'stats-for-update-manager').'</h1>';

// Give feedback to the user about deleted item from row actions.
if (isset($_GET['deleted'])) {
// Translators: %1$s is plugin or theme name.
echo '<div class="notice notice-success is-dismissible"><p>'.sprintf(__('Statistics for %1$s has been successfully reset.', 'stats-for-update-manager'), $_GET['deleted']).'</p></div>';
}

// Render list table.
$statistics = $this->get_statistics();
$ListTable = new SFUM_List_Table();
Expand Down

0 comments on commit 431f2f0

Please sign in to comment.