Skip to content

Commit ad55712

Browse files
committed
Add admin how-to guides and update documentation
Added three new how-to guides for extending WPGraphQL Logging admin: adding fields to existing tabs, creating new settings tabs, and adding columns to the logs grid. Updated index and reference docs to link and document these extension points. Added relevant screenshots for each guide.
1 parent 5c9db69 commit ad55712

File tree

8 files changed

+598
-241
lines changed

8 files changed

+598
-241
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## How to add a new field to an existing tab and query it
2+
3+
This guide shows how to add custom fields to the WPGraphQL Logging settings and how to read or query those values.
4+
5+
![WPGraphQL Logging Settings Page](../screenshots/admin_how_to_add_field.png)
6+
*The WPGraphQL Logging settings page with Basic Configuration and Data Management tabs where custom fields can be added*
7+
8+
9+
### Step 1 — Add a field via filter
10+
11+
Add a field to an existing tab using the provided filters. Common tabs are `basic_configuration` and `data_management`.
12+
13+
Example: add a checkbox to Basic Configuration
14+
15+
```php
16+
add_filter( 'wpgraphql_logging_basic_configuration_fields', function( $fields ) {
17+
$fields['my_feature_enabled'] = new \WPGraphQL\Logging\Admin\Settings\Fields\Field\CheckboxField(
18+
'my_feature_enabled',
19+
'basic_configuration',
20+
__( 'Enable My Feature', 'my-plugin' )
21+
);
22+
return $fields;
23+
});
24+
```
25+
26+
Example: add a text input to Data Management
27+
28+
```php
29+
add_filter( 'wpgraphql_logging_data_management_fields', function( $fields ) {
30+
$fields['my_data_region'] = new \WPGraphQL\Logging\Admin\Settings\Fields\Field\TextInputField(
31+
'my_data_region',
32+
'data_management',
33+
__( 'Data Region', 'my-plugin' ),
34+
'',
35+
__( 'e.g., us-east-1', 'my-plugin' ),
36+
__( 'us-east-1', 'my-plugin' )
37+
);
38+
return $fields;
39+
});
40+
```
41+
42+
Notes:
43+
44+
- Field classes available: `CheckboxField`, `TextInputField`, `SelectField`, `TextIntegerField`.
45+
- The second argument is the tab key (use the tab’s `get_name()`), not the option key.
46+
47+
### Step 2 — Where the value is stored
48+
49+
Values are saved to the option key `wpgraphql_logging_settings` under the tab key and field id, for example:
50+
51+
```php
52+
$options = get_option( 'wpgraphql_logging_settings', [] );
53+
// Example structure
54+
// [
55+
// 'basic_configuration' => [ 'my_feature_enabled' => true ],
56+
// 'data_management' => [ 'my_data_region' => 'us-east-1' ],
57+
// ]
58+
```
59+
60+
### Step 3 — Read the value in PHP
61+
62+
```php
63+
$options = get_option( 'wpgraphql_logging_settings', [] );
64+
$is_enabled = ! empty( $options['basic_configuration']['my_feature_enabled'] );
65+
$my_data_region = $options['data_management']['my_data_region'] ?? '';
66+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
## How to add a new Settings tab to WPGraphQL Logging
2+
3+
This guide shows how to add your own settings tab and fields to the WPGraphQL Logging Admin using the `wpgraphql_logging_settings_field_collection_init` action.
4+
5+
6+
![Add New Settings Tab](../screenshots/admin_how_to_add_tab.png)
7+
*Example of a custom settings tab added to WPGraphQL Logging admin interface*
8+
9+
10+
11+
### Step 1 — Create a Tab class implementing `SettingsTabInterface`
12+
13+
Create a new class that implements the required static methods and returns the fields you want to render/save.
14+
15+
```php
16+
<?php
17+
18+
namespace MyPlugin\Admin\Settings\Fields\Tab;
19+
20+
use WPGraphQL\Logging\Admin\Settings\Fields\Field\CheckboxField;
21+
use WPGraphQL\Logging\Admin\Settings\Fields\Field\TextInputField;
22+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\SettingsTabInterface;
23+
24+
class MyCustomTab implements SettingsTabInterface {
25+
public const ENABLE_FEATURE = 'my_custom_enable_feature';
26+
public const API_ENDPOINT = 'my_custom_api_endpoint';
27+
28+
public static function get_name(): string {
29+
return 'my_custom_tab';
30+
}
31+
32+
public static function get_label(): string {
33+
return __( 'My Custom Tab', 'my-plugin' );
34+
}
35+
36+
public function get_fields(): array {
37+
$fields = [];
38+
39+
$fields[ self::ENABLE_FEATURE ] = new CheckboxField(
40+
self::ENABLE_FEATURE,
41+
self::get_name(),
42+
__( 'Enable Feature', 'my-plugin' ),
43+
'',
44+
__( 'Turn on a custom behavior for WPGraphQL Logging.', 'my-plugin' ),
45+
);
46+
47+
$fields[ self::API_ENDPOINT ] = new TextInputField(
48+
self::API_ENDPOINT,
49+
self::get_name(),
50+
__( 'API Endpoint', 'my-plugin' ),
51+
'',
52+
__( 'Your service endpoint for processing log data.', 'my-plugin' ),
53+
__( 'https://api.example.com/logs', 'my-plugin' )
54+
);
55+
56+
return $fields;
57+
}
58+
}
59+
```
60+
61+
Notes:
62+
63+
- `get_name()` is the tab identifier (slug).
64+
- `get_label()` is the tab title shown in the UI.
65+
- `get_fields()` returns an array of field objects keyed by a unique field ID. Available field types include `CheckboxField`, `TextInputField`, `SelectField`, and `TextIntegerField`.
66+
67+
### Step 2 — Register the tab via the action
68+
69+
Hook into the field collection and add your tab instance. The collection automatically registers your tab’s fields.
70+
71+
```php
72+
add_action( 'wpgraphql_logging_settings_field_collection_init', function ( $collection ) {
73+
// Ensure your class is autoloaded or required before this runs
74+
$collection->add_tab( new \MyPlugin\Admin\Settings\Fields\Tab\MyCustomTab() );
75+
}, 10, 1 );
76+
```
77+
78+
### Step 3 — Reading saved values
79+
80+
Values are stored with the plugin’s option key (filterable via `wpgraphql_logging_settings_group_option_key`). A simple way to access them:
81+
82+
```php
83+
namespace MyPlugin\Admin\Settings\Fields\Tab\MyCustomTab;
84+
namespace WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;
85+
// You could also do this
86+
// $options = get_option( 'wpgraphql_logging_settings', [] );
87+
// $enabled = ! empty( $options['my_custom_tab']['enable_feature'] );
88+
// $api_url = $options['my_custom_tab']['api_endpoint'] ?? '';
89+
90+
// Example: Show an admin notice based on the setting
91+
add_action( 'admin_notices', function() use ( $enabled, $apiUrl ) {
92+
$helper = ConfigurationHelper::get_instance();
93+
$tab = 'my_custom_tab';
94+
$enabled = $helper->get_setting($tab, MyCustomTab::ENABLE_FEATURE, false);
95+
$api_url = $helper->get_setting($tab), MyCustomTab::API_ENDPOINT, '');
96+
97+
98+
if ( $enabled && empty( $api_url ) ) {
99+
echo '<div class="notice notice-warning"><p>';
100+
echo __( 'Custom feature is enabled but no API endpoint is configured.', 'my-plugin' );
101+
echo '</p></div>';
102+
} else {
103+
echo '<div class="notice notice-info"><p>';
104+
if ( $enabled ) {
105+
echo sprintf( __( 'Custom feature is enabled. API URL: %s', 'my-plugin' ), esc_html( $api_url ) );
106+
} else {
107+
echo __( 'Custom feature is disabled.', 'my-plugin' );
108+
}
109+
echo '</p></div>';
110+
}
111+
});
112+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## How to add a new column to the Logs admin grid
2+
3+
This guide shows how to add a custom column to the Logs list table using the provided filters. We’ll add a Memory Peak Usage column sourced from the log entry’s `extra.memory_peak_usage`.
4+
5+
6+
![Add New Column to Logs Table](../screenshots/admin_how_to_add_column_to_grid.png)
7+
*Example of a custom Memory Peak Usage column added to the WPGraphQL Logging admin table*
8+
9+
10+
### Hooks overview
11+
12+
- `wpgraphql_logging_logs_table_column_headers`: modify the visible columns and sortable metadata
13+
- `wpgraphql_logging_logs_table_column_value`: control how each column’s value is rendered
14+
15+
Source: `src/Admin/View/List/ListTable.php`
16+
17+
### Step 1 — Add the column header
18+
19+
```php
20+
add_filter( 'wpgraphql_logging_logs_table_column_headers', function( $headers ) {
21+
if ( isset( $headers[0] ) && is_array( $headers[0] ) ) {
22+
$headers[0]['peak_memory_usage'] = __( 'Memory Peak (MB)', 'my-plugin' );
23+
// Optionally make it sortable by a DB column
24+
// $headers[2]['peak_memory_usage'] = [ 'memory_peak_usage', false ];
25+
return $headers;
26+
}
27+
28+
// Fallback when filter is called for get_columns()
29+
$headers['peak_memory_usage'] = __( 'Memory Peak (MB)', 'my-plugin' );
30+
return $headers;
31+
}, 10, 1 );
32+
```
33+
34+
### Step 2 — Provide the cell value
35+
36+
Each row item is a `\WPGraphQL\Logging\Logger\Database\DatabaseEntity`. Memory data is stored in `$item->get_extra()['memory_peak_usage']`.
37+
38+
```php
39+
add_filter( 'wpgraphql_logging_logs_table_column_value', function( $value, $item, $column_name ) {
40+
if ( 'peak_memory_usage' !== $column_name ) {
41+
return $value;
42+
}
43+
44+
if ( ! $item instanceof \WPGraphQL\Logging\Logger\Database\DatabaseEntity ) {
45+
return $value;
46+
}
47+
48+
$extra = $item->get_extra();
49+
$raw = $extra['memory_peak_usage'] ?? '';
50+
if ( '' === $raw ) {
51+
return '';
52+
}
53+
54+
// Normalize to MB if the stored value is in bytes
55+
if ( is_numeric( $raw ) ) {
56+
$mb = round( (float) $raw / 1048576, 2 );
57+
return sprintf( '%s MB', number_format_i18n( $mb, 2 ) );
58+
}
59+
60+
// If already formatted (e.g., "24 MB"), just escape and return
61+
return esc_html( (string) $raw );
62+
}, 10, 3 );
63+
```

plugins/wpgraphql-logging/docs/index.md

Lines changed: 5 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ The plugin is developer focussed and can be extended in multiple ways.
179179
## Admin - (admin configuration, view and functionality)
180180

181181
- [Actions/Filters](reference/admin.md)
182+
- [How to add a new Settings tab to WPGraphQL Logging](how-to/admin_add_new_tab.md)
183+
- [How to add a new field to an existing tab and query it](how-to/admin_add_fields.md)
184+
- [How to add a new column to the Logs admin grid](how-to/admin_add_view_column.md)
185+
182186

183187
@TODO - How to guides
184188

@@ -193,110 +197,4 @@ The plugin is developer focussed and can be extended in multiple ways.
193197
@TODO - How to guides
194198

195199

196-
----
197-
198-
# Old Notes
199-
200-
- Need to be refactored.
201-
202-
203-
204-
205-
206-
207-
### Developer Hooks
208-
209-
Customize sanitization behavior using WordPress filters:
210-
211-
```php
212-
// Enable/disable sanitization programmatically
213-
add_filter( 'wpgraphql_logging_data_sanitization_enabled', function( $enabled ) {
214-
return current_user_can( 'manage_options' ) ? false : $enabled;
215-
});
216-
217-
// Modify recommended rules
218-
add_filter( 'wpgraphql_logging_data_sanitization_recommended_rules', function( $rules ) {
219-
$rules['custom.sensitive.field'] = 'remove';
220-
return $rules;
221-
});
222-
223-
// Modify all sanitization rules
224-
add_filter( 'wpgraphql_logging_data_sanitization_rules', function( $rules ) {
225-
// Add additional rules or modify existing ones
226-
$rules['request.custom_header'] = 'anonymize';
227-
return $rules;
228-
});
229-
230-
// Modify the final log record after sanitization
231-
add_filter( 'wpgraphql_logging_data_sanitization_record', function( $record ) {
232-
// Additional processing after sanitization
233-
return $record;
234-
});
235-
```
236-
237-
### Performance Considerations
238-
239-
- Sanitization runs on every log record when enabled
240-
- Complex nested field paths may impact performance on high-traffic sites
241-
- Consider using recommended rules for optimal performance
242-
- Test custom rules thoroughly to ensure they target the intended fields
243-
244-
### Security Best Practices
245-
246-
1. **Review logs regularly** to ensure sanitization is working as expected
247-
2. **Test field paths** in a development environment before applying to production
248-
3. **Use remove over anonymize** for highly sensitive data
249-
4. **Monitor performance impact** when implementing extensive custom rules
250-
5. **Keep rules updated** as your GraphQL schema evolves
251-
252-
---
253-
254-
## Usage
255-
256-
WPGraphQL Logging Plugin is highly configurable and extendable and built with developers in mind to allow them to modify, change or add data, loggers etc to this plugin. Please read the docs below:
257-
258-
259-
The following documentation is available in the `docs/` directory:
260-
261-
- [Events](docs/Events.md):
262-
Learn about the event system, available events, and how to subscribe, transform, or listen to WPGraphQL Logging events.
263-
264-
- [Logging](docs/Logging.md):
265-
Learn about the logging system, Monolog integration, handlers, processors, and how to use or extend the logger.
266-
267-
- [Admin](docs/admin.md):
268-
Learn how the admin settings page works, all available hooks, and how to add tabs/fields via actions and filters.
269-
270-
---
271-
272-
273-
274-
---
275-
276-
## Admin & Settings
277-
278-
See `docs/admin.md` for a full overview of the admin/settings architecture, hooks, and examples for adding tabs and fields.
279-
280-
## Testing
281-
282-
See [Testing.md](TESTING.md) for details on how to test the plugin.
283-
284-
## Screenshots
285-
286-
@TODO - When before BETA release.
287-
288-
289-
290-
### Manual Data Cleanup
291-
292-
If you prefer to manually clean up data without defining the constant, you can:
293-
294-
1. Use the plugin's admin interface to clear logs (when available)
295-
2. Manually drop the database table: `{$wpdb->prefix}wpgraphql_logging`
296-
3. Remove plugin options from the WordPress options table
297-
298-
---
299-
300-
@TODO add more info once we have configuration setup.
301-
302-
@TODO add more info once we have configuration setup.
200+
----

0 commit comments

Comments
 (0)