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 @@ -55,6 +55,7 @@ public function __construct(
5555 * Prepare items for display.
5656 */
5757 public function prepare_items (): void {
58+ $ this ->process_bulk_action ();
5859 $ this ->_column_headers =
5960 apply_filters (
6061 'wpgraphql_logging_logs_table_column_headers ' ,
@@ -86,6 +87,34 @@ public function prepare_items(): void {
8687 );
8788 }
8889
90+ /**
91+ * Define bulk actions.
92+ */
93+ public function get_bulk_actions (): array {
94+ return [
95+ 'delete ' => __ ( 'Delete Selected ' , 'wpgraphql-logging ' ),
96+ 'delete_all ' => __ ( 'Delete All ' , 'wpgraphql-logging ' ),
97+ ];
98+ }
99+
100+ /**
101+ * Handle bulk actions.
102+ */
103+ public function process_bulk_action (): void {
104+ $ repository = $ this ->repository ;
105+
106+ if ('delete ' === $ this ->current_action () && !empty ($ _POST ['log ' ])) {
107+ $ ids = array_map ('absint ' , (array ) $ _POST ['log ' ]);
108+ foreach ($ ids as $ id ) {
109+ $ repository ->delete ($ id );
110+ }
111+ }
112+
113+ if ('delete_all ' === $ this ->current_action ()) {
114+ $ repository ->delete_all ();
115+ }
116+ }
117+
89118 /**
90119 * Get the columns for the logs table.
91120 *
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 @@ -77,4 +77,34 @@ public function get_log_count(): int {
7777 public function get_log ( int $ id ): ?DatabaseEntity {
7878 return DatabaseEntity::find_by_id ( $ id );
7979 }
80+
81+ /**
82+ * Delete a single log entry by ID.
83+ *
84+ * @param int $id
85+ * @return bool
86+ */
87+ public function delete (int $ id ): bool {
88+ global $ wpdb ;
89+ $ table_name = DatabaseEntity::get_table_name ();
90+
91+ if ($ id <= 0 ) {
92+ return false ;
93+ }
94+
95+ $ result = $ wpdb ->delete ($ table_name , ['id ' => $ id ], ['%d ' ]); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
96+ return false !== $ result ;
97+ }
98+
99+ /**
100+ * Delete all log entries.
101+ *
102+ * @return bool True if all logs were deleted successfully, false otherwise.
103+ */
104+ public function delete_all (): bool {
105+ global $ wpdb ;
106+ $ table_name = DatabaseEntity::get_table_name ();
107+ $ result = $ wpdb ->query ("TRUNCATE TABLE {$ table_name }" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
108+ return false !== $ result ;
109+ }
80110}
You can’t perform that action at this time.
0 commit comments