Skip to content

Commit 060dc8b

Browse files
committed
Updated API Interfaces and updated the logic for activate/de-activate the plugin to use interface methods instead so developers can replace the service via the log store service.
1 parent 77fb41e commit 060dc8b

File tree

5 files changed

+81
-42
lines changed

5 files changed

+81
-42
lines changed

plugins/wpgraphql-logging/src/Logger/Api/LogEntityInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ public function get_datetime(): string;
9797
public function get_query(): ?string;
9898

9999
/**
100-
* Gets the schema for the log entry.
100+
* Saves the log entry to the database.
101101
*
102-
* @return string The schema for the log entry.
102+
* @return int The ID of the saved log entry, or 0 on failure.
103103
*/
104-
public function get_schema(): string;
104+
public function save(): int;
105105

106106
/**
107-
* Saves the log entry to the database.
107+
* Gets the schema for the log entry.
108108
*
109-
* @return int The ID of the saved log entry, or 0 on failure.
109+
* @return string The schema for the log entry.
110110
*/
111-
public function save(): int;
111+
public static function get_schema(): string;
112112
}

plugins/wpgraphql-logging/src/Logger/Api/LogServiceInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
* @since 0.0.1
1515
*/
1616
interface LogServiceInterface {
17+
/**
18+
* Activates the log service.
19+
*/
20+
public function activate(): void;
21+
22+
/**
23+
* Deactivates the log service.
24+
*/
25+
public function deactivate(): void;
26+
1727
/**
1828
* Creates a new log entity.
1929
*

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -177,35 +177,6 @@ public function get_datetime(): string {
177177
return $this->datetime;
178178
}
179179

180-
/**
181-
* Gets the schema for the log entry.
182-
*
183-
* @return string The schema for the log entry.
184-
*/
185-
public function get_schema(): string {
186-
global $wpdb;
187-
$table_name = self::get_table_name();
188-
$charset_collate = $wpdb->get_charset_collate();
189-
190-
return "
191-
CREATE TABLE {$table_name} (
192-
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
193-
channel VARCHAR(191) NOT NULL,
194-
level SMALLINT UNSIGNED NOT NULL,
195-
level_name VARCHAR(50) NOT NULL,
196-
message LONGTEXT NOT NULL,
197-
context JSON NULL,
198-
extra JSON NULL,
199-
datetime DATETIME NOT NULL,
200-
PRIMARY KEY (id),
201-
INDEX channel_index (channel),
202-
INDEX level_name_index (level_name),
203-
INDEX level_index (level),
204-
INDEX datetime_index (datetime)
205-
) {$charset_collate};
206-
";
207-
}
208-
209180
/**
210181
* Extracts and returns the GraphQL query from the context, if available.
211182
*
@@ -275,6 +246,35 @@ public function save(): int {
275246
return 0;
276247
}
277248

249+
/**
250+
* Gets the schema for the log entry.
251+
*
252+
* @return string The schema for the log entry.
253+
*/
254+
public static function get_schema(): string {
255+
global $wpdb;
256+
$table_name = self::get_table_name();
257+
$charset_collate = $wpdb->get_charset_collate();
258+
259+
return "
260+
CREATE TABLE {$table_name} (
261+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
262+
channel VARCHAR(191) NOT NULL,
263+
level SMALLINT UNSIGNED NOT NULL,
264+
level_name VARCHAR(50) NOT NULL,
265+
message LONGTEXT NOT NULL,
266+
context JSON NULL,
267+
extra JSON NULL,
268+
datetime DATETIME NOT NULL,
269+
PRIMARY KEY (id),
270+
INDEX channel_index (channel),
271+
INDEX level_name_index (level_name),
272+
INDEX level_index (level),
273+
INDEX datetime_index (datetime)
274+
) {$charset_collate};
275+
";
276+
}
277+
278278
/**
279279
* Gets the name of the table for the log entry.
280280
*

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,27 @@ public function count_entities_by_where(array $args = []): int {
171171
return (int) $wpdb->get_var( $wpdb->prepare( $sql, $this->where_values ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
172172
}
173173

174+
/**
175+
* Activates the log service.
176+
*/
177+
public function activate(): void {
178+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
179+
dbDelta( WordPressDatabaseEntity::get_schema() );
180+
}
181+
182+
/**
183+
* Deactivates the log service.
184+
*/
185+
public function deactivate(): void {
186+
if ( ! defined( 'WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN' ) ) {
187+
return;
188+
}
189+
190+
global $wpdb;
191+
$table_name = $this->get_table_name();
192+
$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table_name ) );
193+
}
194+
174195
/**
175196
* Gets the table name.
176197
*/

plugins/wpgraphql-logging/src/Plugin.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
use WPGraphQL\Logging\Admin\ViewLogsPage;
1010
use WPGraphQL\Logging\Events\EventManager;
1111
use WPGraphQL\Logging\Events\QueryEventLifecycle;
12-
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
12+
use WPGraphQL\Logging\Logger\Api\LogServiceInterface;
1313
use WPGraphQL\Logging\Logger\Scheduler\DataDeletionScheduler;
14+
use WPGraphQL\Logging\Logger\Store\LogStoreService;
1415

1516
/**
1617
* Plugin class for WPGraphQL Logging.
@@ -98,11 +99,21 @@ public static function transform( string $event_name, callable $transform, int $
9899
EventManager::subscribe_to_transform( $event_name, $transform, $priority );
99100
}
100101

102+
/**
103+
* Gets the log service instance.
104+
*
105+
* @return \WPGraphQL\Logging\Logger\Api\LogServiceInterface The log service instance.
106+
*/
107+
public static function get_log_service(): LogServiceInterface {
108+
return LogStoreService::get_log_service();
109+
}
110+
101111
/**
102112
* Activation callback for the plugin.
103113
*/
104114
public static function activate(): void {
105-
DatabaseEntity::create_table();
115+
$log_service = self::get_log_service();
116+
$log_service->activate();
106117
}
107118

108119
/**
@@ -113,11 +124,8 @@ public static function activate(): void {
113124
public static function deactivate(): void {
114125

115126
DataDeletionScheduler::clear_scheduled_deletion();
116-
117-
if ( ! defined( 'WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN' ) ) {
118-
return;
119-
}
120-
DatabaseEntity::drop_table();
127+
$log_service = self::get_log_service();
128+
$log_service->deactivate();
121129
}
122130

123131
/**

0 commit comments

Comments
 (0)