File tree Expand file tree Collapse file tree 3 files changed +62
-1
lines changed
plugins/wpgraphql-logging/src Expand file tree Collapse file tree 3 files changed +62
-1
lines changed Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff line change 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>
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments