Skip to content

Commit 7231933

Browse files
committed
Refactor logging rules interface and update imports
Moved LoggingRuleInterface to a new Api namespace and updated all references accordingly. Cleaned up admin asset enqueuing logic, improved documentation in EventManager, and made minor code style improvements. No functional changes to logging logic.
1 parent d29d4dd commit 7231933

File tree

15 files changed

+35
-33
lines changed

15 files changed

+35
-33
lines changed

plugins/wpgraphql-logging/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "wpengine/wpgraphql-logging",
33
"type": "wordpress-plugin",
44
"description": "A plugin for logging WPGraphQL request lifecycle tp help with debugging and performance analysis for WPGraphQL queries.",
5-
"license": "GPL-2.0",
5+
"license": "GPLv2 or later",
66
"version": "0.1.0",
77
"authors": [
88
{
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
// There are many ways to WordPress.

plugins/wpgraphql-logging/src/Admin/Settings/ConfigurationHelper.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,26 +185,21 @@ protected function load_config(): void {
185185

186186
$cache_duration = (int) apply_filters( 'wpgraphql_logging_config_cache_duration', self::CACHE_DURATION );
187187

188-
// Try to get from wp_cache first (in-memory cache).
189188
$cached_config = wp_cache_get( $option_key, self::CACHE_GROUP );
190189
if ( is_array( $cached_config ) ) {
191190
$this->config = $cached_config;
192191
return;
193192
}
194193

195-
// Try to get from the WordPress object cache (could be Redis, Memcached, etc.).
196194
$cached_config = wp_cache_get( $option_key, $this->get_settings_group() );
197195
if ( is_array( $cached_config ) ) {
198196
$this->config = $cached_config;
199-
// Store in our custom cache group for faster access next time.
200197
wp_cache_set( $option_key, $cached_config, self::CACHE_GROUP, $cache_duration );
201198
return;
202199
}
203200

204-
// Load from database.
205201
$this->config = $this->get_option_value( $option_key, [] );
206202

207-
// Cache the result in both cache groups.
208203
wp_cache_set( $option_key, $this->config, self::CACHE_GROUP, $cache_duration );
209204
wp_cache_set( $option_key, $this->config, $this->get_settings_group(), $cache_duration );
210205
}

plugins/wpgraphql-logging/src/Admin/SettingsPage.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,19 @@ public function load_scripts_styles( string $hook_suffix ): void {
196196
return;
197197
}
198198

199-
// Enqueue admin styles if they exist.
200-
$style_path = trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_URL ) . 'assets/css/settings/wp-graphql-logging-settings.css';
201199
if ( file_exists( trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_DIR ) . 'assets/css/settings/wp-graphql-logging-settings.css' ) ) {
202200
wp_enqueue_style(
203201
'wpgraphql-logging-settings-css',
204-
$style_path,
202+
trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_URL ) . 'assets/css/settings/wp-graphql-logging-settings.css',
205203
[],
206204
WPGRAPHQL_LOGGING_VERSION
207205
);
208206
}
209207

210-
// Enqueue admin scripts if they exist.
211-
$script_path = trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_URL ) . 'assets/js/settings/wp-graphql-logging-settings.js';
212208
if ( file_exists( trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_DIR ) . 'assets/js/settings/wp-graphql-logging-settings.js' ) ) {
213209
wp_enqueue_script(
214210
'wpgraphql-logging-settings-js',
215-
$script_path,
211+
trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_URL ) . 'assets/js/settings/wp-graphql-logging-settings.js',
216212
[],
217213
WPGRAPHQL_LOGGING_VERSION,
218214
true

plugins/wpgraphql-logging/src/Admin/ViewLogsPage.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,10 @@ public function enqueue_admin_scripts_styles( string $hook_suffix ): void {
130130

131131
wp_enqueue_style( 'jquery-ui-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css', [], '1.12.1' );
132132

133-
// Enqueue admin scripts if they exist.
134-
$script_path = trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_URL ) . 'assets/js/settings/wp-graphql-logging-view.js';
135133
if ( file_exists( trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_DIR ) . 'assets/js/settings/wp-graphql-logging-view.js' ) ) {
136134
wp_enqueue_script(
137135
'wpgraphql-logging-view-js',
138-
$script_path,
136+
trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_URL ) . 'assets/js/settings/wp-graphql-logging-view.js',
139137
[ 'jquery' ],
140138
WPGRAPHQL_LOGGING_VERSION,
141139
true
@@ -233,8 +231,8 @@ public function get_redirect_url(): string {
233231
$redirect_url = add_query_arg( array_filter( $filters, static function ( $value ) {
234232
return '' !== $value;
235233
} ), $redirect_url );
236-
$redirect_url = apply_filters( 'wpgraphql_logging_filter_redirect_url', $redirect_url, $filters );
237-
return (string) $redirect_url;
234+
235+
return (string) apply_filters( 'wpgraphql_logging_filter_redirect_url', $redirect_url, $filters );
238236
}
239237

240238
/**

plugins/wpgraphql-logging/src/Events/EventManager.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@
55
namespace WPGraphQL\Logging\Events;
66

77
/**
8-
* Simple pub/sub Event Manager for WPGraphQL Logging
8+
* Pub/sub Event Manager for WPGraphQL Logging.
99
*
10-
* Provides a lightweight event bus with optional WordPress bridge.
10+
* This class provides a lightweight event bus with a WordPress bridge.
1111
*
12-
* Users can:
13-
* - subscribe to events using subscribe()
14-
* - publish events using publish()
15-
* - also listen via WordPress hooks: `wpgraphql_logging_event_{event_name}`
12+
* Users can subscribe to events using subscribe() and publish events using publish().
13+
* They can also listen via WordPress hooks: `wpgraphql_logging_event_{event_name}`.
14+
*
15+
* @package WPGraphQL\Logging
16+
*
17+
* @since 0.0.1
1618
*/
1719
final class EventManager {
1820
/**
19-
* In-memory map of event name to priority to listeners.
21+
* Events that can be subscribed to.
2022
*
2123
* @var array<string, array<int, array<int, callable>>>
2224
*/
23-
private static array $events = [];
25+
protected static array $events = [];
2426

2527
/**
26-
* Transform listeners that can modify a payload.
28+
* Transformers that can modify an event's payload.
2729
*
2830
* @var array<string, array<int, array<int, callable>>>
2931
*/
@@ -52,13 +54,14 @@ public static function subscribe(string $event_name, callable $listener, int $pr
5254
*
5355
* @param string $event_name Event name (see Events constants).
5456
* @param array<string, mixed> $payload Arbitrary payload for listeners.
57+
*
58+
* @psalm-suppress HookNotFound
5559
*/
5660
public static function publish(string $event_name, array $payload = []): void {
5761

5862
$ordered_listeners = self::get_ordered_listeners( $event_name );
5963

6064
if ( [] === $ordered_listeners ) {
61-
/** @psalm-suppress HookNotFound */
6265
do_action( 'wpgraphql_logging_event_' . $event_name, $payload );
6366
return;
6467
}
@@ -67,7 +70,6 @@ public static function publish(string $event_name, array $payload = []): void {
6770
self::invoke_listener( $listener, $payload );
6871
}
6972

70-
/** @psalm-suppress HookNotFound */
7173
do_action( 'wpgraphql_logging_event_' . $event_name, $payload );
7274
}
7375

@@ -95,21 +97,21 @@ public static function subscribe_to_transform(string $event_name, callable $tran
9597
* @param string $event_name Event name.
9698
* @param array<string, mixed> $payload Initial payload.
9799
*
100+
* @psalm-suppress HookNotFound
101+
*
98102
* @return array<string, mixed> Modified payload.
99103
*/
100104
public static function transform(string $event_name, array $payload): array {
101105

102106
$ordered_transforms = self::get_ordered_transforms( $event_name );
103107
if ( [] === $ordered_transforms ) {
104-
/** @psalm-suppress HookNotFound */
105108
return apply_filters( 'wpgraphql_logging_filter_' . $event_name, $payload );
106109
}
107110

108111
foreach ( $ordered_transforms as $transform ) {
109112
$payload = self::invoke_transform( $transform, $payload );
110113
}
111114

112-
/** @psalm-suppress HookNotFound */
113115
return apply_filters( 'wpgraphql_logging_filter_' . $event_name, $payload );
114116
}
115117

plugins/wpgraphql-logging/src/Logger/Rules/LoggingRuleInterface.php renamed to plugins/wpgraphql-logging/src/Logger/Api/LoggingRuleInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace WPGraphQL\Logging\Logger\Rules;
5+
namespace WPGraphQL\Logging\Logger\Api;
66

77
/**
88
* Interface for logging rules.

plugins/wpgraphql-logging/src/Logger/Rules/EnabledRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace WPGraphQL\Logging\Logger\Rules;
66

77
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
8+
use WPGraphQL\Logging\Logger\Api\LoggingRuleInterface;
89

910
/**
1011
* Rule to check if logging is enabled.

plugins/wpgraphql-logging/src/Logger/Rules/ExcludeQueryRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace WPGraphQL\Logging\Logger\Rules;
66

77
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
8+
use WPGraphQL\Logging\Logger\Api\LoggingRuleInterface;
89

910
/**
1011
* Rule to check if the query is excluded from logging.

plugins/wpgraphql-logging/src/Logger/Rules/IpRestrictionsRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace WPGraphQL\Logging\Logger\Rules;
66

77
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
8+
use WPGraphQL\Logging\Logger\Api\LoggingRuleInterface;
89

910
/**
1011
* Rule to check if logging should occur based on IP restrictions.

0 commit comments

Comments
 (0)