Skip to content

chore: Created admin functionality for displaying and saving data #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugins/wpgraphql-logging/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ c3.php

# Cache
phpcs-cache.json
.psalm-cache/
tests/_support/
tests/_output/
tests/_generated/
tests/_data/

# Playwright outputs
artifacts
artifacts
9 changes: 9 additions & 0 deletions plugins/wpgraphql-logging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ A WPGraphQL logging plugin that provides visibility into request lifecycle to he
- [Features](#features)
- [Usage](#usage)
- [Configuration](#configuration)
- [Admin & Settings](#admin--settings)
- [Extending the Functionality](#extending-the-functionality)
- [Testing](#testing)

Expand Down Expand Up @@ -59,6 +60,7 @@ wpgraphql-logging/
├── docs/ # Docs for extending the plugin.
├── src/ # Main plugin source code
│ ├── Admin/ # Admin settings, menu, and settings page logic
│ ├── Settings/ # Admin settings functionality for displaying and saving data.
│ ├── Events/ # Event logging, pub/sub event manager for extending the logging.
│ ├── Logging/ # Logging logic, logger service, Monolog handlers & processors
│ ├── Plugin.php # Main plugin class (entry point)
Expand Down Expand Up @@ -110,6 +112,9 @@ The following documentation is available in the `docs/` directory:
- [Logging](docs/Logging.md):
Learn about the logging system, Monolog integration, handlers, processors, and how to use or extend the logger.

- [Admin](docs/admin.md):
Learn how the admin settings page works, all available hooks, and how to add tabs/fields via actions and filters.

---


Expand All @@ -126,6 +131,10 @@ The following documentation is available in the `docs/` directory:

---

## Admin & Settings

See `docs/admin.md` for a full overview of the admin/settings architecture, hooks, and examples for adding tabs and fields.

## Testing

See [Testing.md](TESTING.md) for details on how to test the plugin.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
settings_page_wpgraphql-logging #poststuff .postbox .inside h2 {
font-size: 1.3em;
font-weight: 600;
padding-left: 0;
}


.form-table td input[type="text"] {
width: calc(99% - 24px);
display: inline-block;
}
.form-table td select {
width: calc(99% - 24px);
display: inline-block;
}

.wpgraphql-logging-tooltip {
position: relative;
vertical-align: middle;
display: inline-block;
margin-right: 0.25rem;
}

.wpgraphql-logging-tooltip .dashicons {
color: #787c82;
vertical-align: middle;
}

.wpgraphql-logging-tooltip .tooltip-text.description {
opacity: 0;
visibility: hidden;
text-align: center;
color: #fff;
background-color: #1d2327;
border-radius: 4px;
position: absolute;
z-index: 1;
width: 180px;
padding: 0.5rem;
top: 50%;
transform: translateY(-50%);
vertical-align: middle;
margin-left: 0.25rem;
transition: opacity 0.12s ease;
}

.wpgraphql-logging-tooltip .tooltip-text::after {
content: "";
position: absolute;
top: 0;
left: -10px;
border-width: 6px;
border-style: solid;
border-color: transparent #1d2327 transparent transparent;
top: 50%;
transform: translateY(-50%);
}

.wpgraphql-logging-tooltip:hover .tooltip-text,
.wpgraphql-logging-tooltip:focus-within .tooltip-text {
visibility: visible;
opacity: 1;
}

.wpgraphql-logging-docs ul li {
list-style-type: none;
margin-left: 30px;
padding-bottom: 16px;
}

.wpgraphql-logging-docs ul li:before {
content: url(../../icons/doc.svg);
height: 1em;
margin-left: -29px;
margin-top: -2px;
position: absolute;
width: 0.5em;
}


.wpgraphql-logging-feature-list {
list-style-type: disc;
font-size: 1.1em;
margin-left: 30px;
padding-bottom: 16px;
}
3 changes: 3 additions & 0 deletions plugins/wpgraphql-logging/assets/icons/doc.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions plugins/wpgraphql-logging/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@
"phpstan": [
"vendor/bin/phpstan analyze --ansi --memory-limit=1G"
],
"php:psalm": "psalm",
"php:psalm:info": "psalm --show-info=true",
"php:psalm:fix": "psalm --alter",
"php:psalm": "psalm --output-format=text --no-progress",
"php:psalm:fix": "psalm --alter --output-format=text --no-progress",
"qa": "sh bin/local/run-qa.sh",
"test": [
"sh bin/local/run-unit-tests.sh coverage",
Expand Down
193 changes: 193 additions & 0 deletions plugins/wpgraphql-logging/docs/admin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
### Admin and Settings

This document explains how the WPGraphQL Logging admin settings UI is built and how to extend it with your own tabs and fields.

---

## Architecture Overview

- **Settings page entry**: `WPGraphQL\Logging\Admin\Settings_Page`
- Registers the submenu page and orchestrates fields and tabs
- Hooks added: `init` (init fields), `admin_menu` (page), `admin_init` (fields), `admin_enqueue_scripts` (assets)
- **Menu page**: `WPGraphQL\Logging\Admin\Settings\Menu\Menu_Page`
- Adds a submenu under Settings → WPGraphQL Logging (`wpgraphql-logging`)
- Renders template `src/Admin/Settings/Templates/admin.php`
- **Form manager**: `WPGraphQL\Logging\Admin\Settings\Settings_Form_Manager`
- Registers the settings (`register_setting`) and sections/fields per tab
- Sanitizes and saves values per tab; unknown fields are pruned
- **Field collection**: `WPGraphQL\Logging\Admin\Settings\Fields\Settings_Field_Collection`
- Holds all tabs and fields. A default `Basic_Configuration_Tab` is registered
- **Tabs**: Implement `Settings_Tab_Interface` with `get_name()`, `get_label()`, `get_fields()`
- **Fields**: Implement `Settings_Field_Interface` or use built-ins:
- `Field\Checkbox_Field`
- `Field\Text_Input_Field`
- `Field\Select_Field`

Settings are stored in an array option. Keys are filterable:

- Option key: `wpgraphql_logging_settings` (filter `wpgraphql_logging_settings_group_option_key`)
- Settings group: `wpgraphql_logging_settings_group` (filter `wpgraphql_logging_settings_group_settings_group`)

To read values at runtime, use `WPGraphQL\Logging\Admin\Settings\Logging_Settings_Service`:

```php
use WPGraphQL\Logging\Admin\Settings\Logging_Settings_Service;

$settings = new Logging_Settings_Service();
$enabled = $settings->get_setting('basic_configuration', 'enabled', false);
```

---

## Hooks Reference (Admin)

- Action: `wpgraphql_logging_settings_init( Settings_Page $instance )`
- Fired after the settings page is initialized
- Action: `wpgraphql_logging_settings_field_collection_init( Settings_Field_Collection $collection )`
- Fired after default tabs/fields are registered; primary extension point to add tabs/fields
- Action: `wpgraphql_logging_settings_form_manager_init( Settings_Form_Manager $manager )`
- Fired when the form manager is constructed
- Filter: `wpgraphql_logging_settings_group_option_key( string $option_key )`
- Change the option key used to store settings
- Filter: `wpgraphql_logging_settings_group_settings_group( string $group )`
- Change the settings group name used in `register_setting`

- Filter: `wpgraphql_logging_basic_configuration_fields( array $fields )`
- Modify the default fields rendered in the `basic_configuration` tab. You can add, remove, or replace fields by returning a modified associative array of `field_id => Settings_Field_Interface`.
- Example:
```php
use WPGraphQL\Logging\Admin\Settings\Fields\Field\Checkbox_Field;

add_filter('wpgraphql_logging_basic_configuration_fields', function(array $fields): array {
// Add a custom toggle into the Basic Configuration tab
$fields['enable_feature_x'] = new Checkbox_Field(
'enable_feature_x',
'basic_configuration',
'Enable Feature X',
'',
'Turn on extra logging for Feature X.'
);

// Optionally remove an existing field
// unset($fields[ WPGraphQL\Logging\Admin\Settings\Fields\Tab\Basic_Configuration_Tab::DATA_SAMPLING ]);

return $fields;
});
```

Related (non-admin) hooks for context:

- Action: `wpgraphql_logging_init( Plugin $instance )` (plugin initialized)
- Action: `wpgraphql_logging_activate` / `wpgraphql_logging_deactivate`

---

## Add a New Tab

Create a tab class implementing `Settings_Tab_Interface` and register it during `wpgraphql_logging_settings_field_collection_init`.

```php
<?php
namespace MyPlugin\WPGraphQLLogging;

use WPGraphQL\Logging\Admin\Settings\Fields\Settings_Field_Collection;
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\Settings_Tab_Interface;
use WPGraphQL\Logging\Admin\Settings\Fields\Field\Text_Input_Field;

class My_Custom_Tab implements Settings_Tab_Interface {
public function get_name(): string {
return 'my_custom_tab';
}

public function get_label(): string {
return 'My Custom Tab';
}

public function get_fields(): array {
return [
'my_setting' => new Text_Input_Field(
'my_setting',
$this->get_name(),
'My Setting',
'',
'Describe what this setting does.',
'e.g., value'
),
];
}
}

add_action('wpgraphql_logging_settings_field_collection_init', function (Settings_Field_Collection $collection): void {
$collection->add_tab(new My_Custom_Tab());
});
```

Notes:

- `get_name()` must be a unique slug; it is used in the admin page URL (`tab` query arg) and section IDs
- Fields returned by `get_fields()` must set their `tab` to this slug so they render on the tab

---

## Add a Field to an Existing Tab

You can add fields directly to the shared field collection. Ensure the field’s `tab` matches the target tab name.

```php
<?php
namespace MyPlugin\WPGraphQLLogging;

use WPGraphQL\Logging\Admin\Settings\Fields\Settings_Field_Collection;
use WPGraphQL\Logging\Admin\Settings\Fields\Field\Checkbox_Field;

add_action('wpgraphql_logging_settings_field_collection_init', function (Settings_Field_Collection $collection): void {
$collection->add_field(
'enable_feature_x',
new Checkbox_Field(
'enable_feature_x',
'basic_configuration', // target the built-in Basic Configuration tab
'Enable Feature X',
'',
'Turn on extra logging for Feature X.'
)
);
});
```

Tips:

- Only fields present in the collection are saved; unknown keys are pruned during sanitize
- Field input names follow: `{$option_key}[{$tab}][{$field_id}]`

---

## Reading/Saving Behavior

- Each submit saves only the current tab’s fields
- Sanitization is delegated to each field via `sanitize_field($value)`
- Unknown fields or tabs are ignored/pruned

Example of reading a value elsewhere:

```php
use WPGraphQL\Logging\Admin\Settings\Logging_Settings_Service;

$settings = new Logging_Settings_Service();
$thresholdSeconds = (float) $settings->get_setting('basic_configuration', 'performance_metrics', '0');
```

---

## Common Use Cases

- Add organization-specific logging toggles (privacy, PII redaction)
- Integrate with other plugins by exposing their settings under a new tab
- Provide presets for log points (e.g., only log slow queries) via a custom select field

---

## Admin Page Details

- Menu: Settings → WPGraphQL Logging (`admin.php?page=wpgraphql-logging`)
- Tabs: `admin.php?page=wpgraphql-logging&tab={tab_slug}`
- Sections and fields are rendered with `do_settings_sections('wpgraphql-logging-{tab_slug}')`
2 changes: 1 addition & 1 deletion plugins/wpgraphql-logging/phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@

<rule ref="SlevomatCodingStandard.Complexity.Cognitive">
<properties>
<property name="warningThreshold" value="7"/>
<property name="warningThreshold" value="8"/>
<property name="errorThreshold" value="10"/>
</properties>
</rule>
Expand Down
4 changes: 4 additions & 0 deletions plugins/wpgraphql-logging/phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ parameters:
paths:
- wpgraphql-logging.php
- src/
ignoreErrors:
- identifier: empty.notAllowed
-
message: '#Constant WPGRAPHQL_LOGGING.* not found\.#'
1 change: 1 addition & 0 deletions plugins/wpgraphql-logging/psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
findUnusedBaselineEntry="true"
findUnusedCode="false"
phpVersion="8.1"
cacheDirectory=".psalm-cache"
>
<projectFiles>
<file name="wpgraphql-logging.php"/>
Expand Down
Empty file.
Loading
Loading