Skip to content

Commit 87c6893

Browse files
committed
feat: delete item and delete all log items
1 parent bfecdfa commit 87c6893

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

plugins/wpgraphql-logging/src/Admin/View/List/List_Table.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function __construct(
4646
* Prepare items for display.
4747
*/
4848
public function prepare_items(): void {
49+
$this->process_bulk_action();
4950
$this->_column_headers =
5051
apply_filters(
5152
'wpgraphql_logging_logs_table_column_headers',
@@ -77,6 +78,34 @@ public function prepare_items(): void {
7778
);
7879
}
7980

81+
/**
82+
* Define bulk actions.
83+
*/
84+
public function get_bulk_actions(): array {
85+
return [
86+
'delete' => __( 'Delete Selected', 'wpgraphql-logging' ),
87+
'delete_all' => __( 'Delete All', 'wpgraphql-logging' ),
88+
];
89+
}
90+
91+
/**
92+
* Handle bulk actions.
93+
*/
94+
public function process_bulk_action(): void {
95+
$repository = $this->repository;
96+
97+
if ('delete' === $this->current_action() && !empty($_POST['log'])) {
98+
$ids = array_map('absint', (array) $_POST['log']);
99+
foreach ($ids as $id) {
100+
$repository->delete($id);
101+
}
102+
}
103+
104+
if ('delete_all' === $this->current_action()) {
105+
$repository->delete_all();
106+
}
107+
}
108+
80109
/**
81110
* Get the columns for the logs table.
82111
*

plugins/wpgraphql-logging/src/Admin/View/List/Templates/wpgraphql-logger-list.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616
exit;
1717
}
1818
?>
19-
19+
2020
<div class="wrap">
2121
<h1 class="wp-heading-inline"><?php esc_html_e( 'WPGraphQL Logs', 'wpgraphql-logging' ); ?></h1>
2222
<hr class="wp-header-end">
2323

2424
<form method="post">
2525
<?php
2626
$list_table->prepare_items();
27+
$list_table->display_bulk_actions_top();
2728
$list_table->display();
29+
$list_table->display_bulk_actions_bottom();
2830
?>
2931
</form>
3032
</div>

plugins/wpgraphql-logging/src/Logger/Database/LogsRepository.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,34 @@ public function get_log_count(): int {
6969
public function get_log( int $id ): ?DatabaseEntity {
7070
return DatabaseEntity::find_by_id( $id );
7171
}
72+
73+
/**
74+
* Delete a single log entry by ID.
75+
*
76+
* @param int $id
77+
* @return bool
78+
*/
79+
public function delete(int $id): bool {
80+
global $wpdb;
81+
$table_name = DatabaseEntity::get_table_name();
82+
83+
if ($id <= 0) {
84+
return false;
85+
}
86+
87+
$result = $wpdb->delete($table_name, ['id' => $id], ['%d']); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
88+
return false !== $result;
89+
}
90+
91+
/**
92+
* Delete all log entries.
93+
*
94+
* @return bool True if all logs were deleted successfully, false otherwise.
95+
*/
96+
public function delete_all(): bool {
97+
global $wpdb;
98+
$table_name = DatabaseEntity::get_table_name();
99+
$result = $wpdb->query("TRUNCATE TABLE {$table_name}"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
100+
return false !== $result;
101+
}
72102
}

0 commit comments

Comments
 (0)