Skip to content

Commit ab68db6

Browse files
committed
Add custom data sanitization options to settings
Introduces a new data sanitization method selection in the settings, allowing users to choose between recommended and custom methods. Adds fields for specifying custom fields to anonymize, remove, or truncate. Includes supporting JavaScript and CSS for dynamic UI behavior, and minor code cleanup in LogsRepository.
1 parent c8b008e commit ab68db6

File tree

4 files changed

+94
-11
lines changed

4 files changed

+94
-11
lines changed

plugins/wpgraphql-logging/assets/css/settings/wp-graphql-logging-settings.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,8 @@ settings_page_wpgraphql-logging #poststuff .postbox .inside h2 {
8585
margin-left: 30px;
8686
padding-bottom: 16px;
8787
}
88+
89+
90+
.wpgraphql-logging-custom:not(.block) {
91+
display: none;
92+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Add this to your admin JavaScript file
2+
document.addEventListener('DOMContentLoaded', function() {
3+
4+
const sanitizationMethodSelect = document.querySelector("#data_sanitization_method");
5+
if (! sanitizationMethodSelect || sanitizationMethodSelect.length === 0) {
6+
return;
7+
}
8+
9+
function toggleCustomFields() {
10+
const isCustom = sanitizationMethodSelect.value === 'custom';
11+
12+
if (isCustom) {
13+
document.querySelectorAll('.wpgraphql-logging-custom').forEach((el) => {
14+
el.classList.add('block');
15+
});
16+
} else {
17+
document.querySelectorAll('.wpgraphql-logging-custom').forEach((el) => {
18+
el.classList.remove('block');
19+
});
20+
}
21+
}
22+
23+
// Initial check on page load
24+
toggleCustomFields();
25+
26+
// Listen for changes
27+
sanitizationMethodSelect.addEventListener('change', toggleCustomFields);
28+
});

plugins/wpgraphql-logging/src/Admin/Settings/Fields/Tab/Data_Management_Tab.php

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace WPGraphQL\Logging\Admin\Settings\Fields\Tab;
66

77
use WPGraphQL\Logging\Admin\Settings\Fields\Field\Checkbox_Field;
8+
use WPGraphQL\Logging\Admin\Settings\Fields\Field\Select_Field;
89
use WPGraphQL\Logging\Admin\Settings\Fields\Field\Text_Input_Field;
910
use WPGraphQL\Logging\Admin\Settings\Fields\Field\Text_Integer_Field;
1011

@@ -38,11 +39,32 @@ class Data_Management_Tab implements Settings_Tab_Interface {
3839
public const DATA_SANITIZATION_ENABLED = 'data_sanitization_enabled';
3940

4041
/**
41-
* The field ID for the data sanitization fields.
42+
* The field ID for the data sanitization method.
4243
*
4344
* @var string
4445
*/
45-
public const DATA_SANITIZATION_FIELDS = 'data_sanitization_fields';
46+
public const DATA_SANITIZATION_METHOD = 'data_sanitization_method';
47+
48+
/**
49+
* The field ID for the custom fields to sanitize.
50+
*
51+
* @var string
52+
*/
53+
public const DATA_SANITIZATION_CUSTOM_FIELD_ANONYMIZE = 'data_sanitization_custom_field_anonymize';
54+
55+
/**
56+
* The field ID for the custom fields to remove.
57+
*
58+
* @var string
59+
*/
60+
public const DATA_SANITIZATION_CUSTOM_FIELD_REMOVE = 'data_sanitization_custom_field_remove';
61+
62+
/**
63+
* The field ID for the custom fields to truncate.
64+
*
65+
* @var string
66+
*/
67+
public const DATA_SANITIZATION_CUSTOM_FIELD_TRUNCATE = 'data_sanitization_custom_field_truncate';
4668

4769
/**
4870
* Get the name/identifier of the tab.
@@ -95,16 +117,44 @@ public function get_fields(): array {
95117
);
96118

97119

98-
$fields[ self::DATA_SANITIZATION_FIELDS ] = new Text_Input_Field(
99-
self::DATA_SANITIZATION_FIELDS,
120+
$fields[ self::DATA_SANITIZATION_METHOD ] = new Select_Field(
121+
self::DATA_SANITIZATION_METHOD,
100122
$this->get_name(),
101-
__( 'Data Sanitization Fields', 'wpgraphql-logging' ),
123+
__( 'Data Sanitization Method', 'wpgraphql-logging' ),
124+
[
125+
'recommended' => __( 'Recommended', 'wpgraphql-logging' ),
126+
'custom' => __( 'Custom', 'wpgraphql-logging' ),
127+
],
102128
'',
103-
__( 'A comma-separated list of fields to sanitize for WPGraphQL logging.', 'wpgraphql-logging' ),
104-
__( 'e.g., user.email, user.name, user.firstName, user.lastName', 'wpgraphql-logging' ),
105-
'user_email, user_pass, user_login, user_status, display_name, nickname, first_name, last_name'
129+
__( 'Select the method to use for data sanitization.', 'wpgraphql-logging' ),
130+
false
131+
);
132+
133+
134+
$fields[ self::DATA_SANITIZATION_CUSTOM_FIELD_ANONYMIZE ] = new Text_Input_Field(
135+
self::DATA_SANITIZATION_CUSTOM_FIELD_ANONYMIZE,
136+
$this->get_name(),
137+
__( 'Custom Fields to Anonymize', 'wpgraphql-logging' ),
138+
'wpgraphql-logging-custom',
139+
__( 'Comma-separated list of custom fields to anonymize.', 'wpgraphql-logging' ),
140+
'e.g., user_email, user_ip'
141+
);
142+
143+
$fields[ self::DATA_SANITIZATION_CUSTOM_FIELD_REMOVE ] = new Text_Input_Field(
144+
self::DATA_SANITIZATION_CUSTOM_FIELD_REMOVE,
145+
$this->get_name(),
146+
__( 'Custom Fields to Remove', 'wpgraphql-logging' ),
147+
'wpgraphql-logging-custom',
148+
__( 'Comma-separated list of custom fields to remove.', 'wpgraphql-logging' ),
106149
);
107150

151+
$fields[ self::DATA_SANITIZATION_CUSTOM_FIELD_TRUNCATE ] = new Text_Input_Field(
152+
self::DATA_SANITIZATION_CUSTOM_FIELD_TRUNCATE,
153+
$this->get_name(),
154+
__( 'Custom Fields to Truncate', 'wpgraphql-logging' ),
155+
'wpgraphql-logging-custom',
156+
__( 'Comma-separated list of custom fields to truncate.', 'wpgraphql-logging' ),
157+
);
108158

109159
return apply_filters( 'wpgraphql_logging_data_management_fields', $fields );
110160
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ public function delete_log_older_than(DateTime $date): bool {
120120
global $wpdb;
121121
$table_name = DatabaseEntity::get_table_name();
122122

123-
$result = $wpdb->query( $wpdb->prepare(
124-
"DELETE FROM %i WHERE datetime < %s",
123+
$result = $wpdb->query( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
124+
'DELETE FROM %i WHERE datetime < %s',
125125
$table_name,
126126
$date->format( 'Y-m-d H:i:s' )
127-
) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
127+
) );
128128
return false !== $result;
129129
}
130130

0 commit comments

Comments
 (0)