Skip to content

Commit 1ccb8af

Browse files
committed
Added Database entity and plugin hook class to add/drop table on activation/de-activation.
1 parent b604a64 commit 1ccb8af

File tree

10 files changed

+195
-15
lines changed

10 files changed

+195
-15
lines changed

plugins/wpgraphql-logging/src/Admin/.gitkeep

Whitespace-only changes.

plugins/wpgraphql-logging/src/Events/.gitkeep

Whitespace-only changes.

plugins/wpgraphql-logging/src/Hooks/.gitkeep

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Hooks;
6+
7+
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
8+
9+
class PluginHooks {
10+
/**
11+
* The single instance of the class.
12+
*
13+
* @var \WPGraphQL\Logging\Hooks\PluginHooks|null
14+
*/
15+
private static ?PluginHooks $instance = null;
16+
17+
/**
18+
* Get or create the single instance of the class.
19+
*/
20+
public static function init(): PluginHooks {
21+
if ( null === self::$instance ) {
22+
self::$instance = new self();
23+
self::$instance->register();
24+
}
25+
return self::$instance;
26+
}
27+
28+
/**
29+
* Activation callback for the plugin.
30+
*/
31+
public static function activate_plugin(): void {
32+
DatabaseEntity::create_table();
33+
}
34+
35+
/**
36+
* Deactivation callback for the plugin.
37+
*/
38+
public static function deactivate_plugin(): void {
39+
// @TODO: Add configuration to determine if the table should be dropped on deactivation.
40+
DatabaseEntity::drop_table();
41+
}
42+
43+
/**
44+
* Register actions and filters.
45+
*/
46+
protected function register(): void {
47+
add_action( 'wpgraphql_logging_activate', [ self::class, 'activate_plugin' ] );
48+
add_action( 'wpgraphql_logging_deactivate', [ self::class, 'deactivate_plugin' ] );
49+
}
50+
}

plugins/wpgraphql-logging/src/Logger/.gitkeep

Whitespace-only changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Logger\Database;
6+
7+
class DatabaseEntity {
8+
/**
9+
* Gets the name of the logging table.
10+
*
11+
* @return string The name of the logging table.
12+
*/
13+
public static function get_table_name(): string {
14+
global $wpdb;
15+
16+
return (string) apply_filters( 'wpgraphql_logging_database_name', $wpdb->prefix . 'wpgraphql_logging' );
17+
}
18+
19+
/**
20+
* Gets the database schema for the logging table.
21+
*
22+
* @return string The SQL CREATE an TABLE statement.
23+
*/
24+
public static function get_schema(): string {
25+
global $wpdb;
26+
$table_name = self::get_table_name();
27+
$charset_collate = $wpdb->get_charset_collate();
28+
29+
return "
30+
CREATE TABLE {$table_name} (
31+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
32+
channel VARCHAR(191) NOT NULL,
33+
level SMALLINT UNSIGNED NOT NULL,
34+
level_name VARCHAR(50) NOT NULL,
35+
message LONGTEXT NOT NULL,
36+
context JSON NULL,
37+
extra JSON NULL,
38+
datetime DATETIME NOT NULL,
39+
INDEX channel_index (channel),
40+
INDEX level_index (level),
41+
INDEX datetime_index (datetime)
42+
) {$charset_collate};
43+
";
44+
}
45+
46+
/**
47+
* Creates the logging table in the database.
48+
*/
49+
public static function create_table(): void {
50+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
51+
$schema = self::get_schema();
52+
dbDelta( $schema );
53+
}
54+
55+
/**
56+
* Drops the logging table from the database.
57+
*/
58+
public static function drop_table(): void {
59+
global $wpdb;
60+
$table_name = self::get_table_name();
61+
62+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
63+
$wpdb->query( "DROP TABLE IF EXISTS {$table_name}" );
64+
}
65+
}

plugins/wpgraphql-logging/src/Plugin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace WPGraphQL\Logging;
66

7+
use WPGraphQL\Logging\Hooks\PluginHooks;
8+
79
/**
810
* Plugin class for WPGraphQL Logging.
911
*
@@ -48,7 +50,7 @@ public static function init(): self {
4850
* Initialize the plugin admin, frontend & api functionality.
4951
*/
5052
public function setup(): void {
51-
// @TODO
53+
PluginHooks::init();
5254
}
5355

5456
/**

plugins/wpgraphql-logging/tests/wpunit/Core/ActivationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@
66

77

88
use lucatume\WPBrowser\TestCase\WPTestCase;
9+
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
910

1011
/**
1112
* Test class for the activation callback.
1213
*/
1314
class ActivationTest extends WPTestCase {
15+
1416
protected function setUp(): void {
1517
parent::setUp();
1618
if ( ! function_exists( 'wpgraphql_logging_activation_callback' ) ) {
1719
require_once dirname( __DIR__ ) . '/activation.php';
1820
}
21+
$this->drop_table();
22+
}
23+
24+
public function drop_table(): void {
25+
DatabaseEntity::drop_table();
1926
}
2027

2128
public function test_activation_callback_function_exists(): void {

plugins/wpgraphql-logging/tests/wpunit/Core/PluginTest.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use WPGraphQL\Logging\Plugin;
88
use lucatume\WPBrowser\TestCase\WPTestCase;
99
use ReflectionClass;
10+
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
1011

1112
/**
1213
* Class PluginTest
@@ -52,18 +53,4 @@ public function test_clone_method_throws_error() {
5253
// Verify the clone exists to ensure the operation completed
5354
$this->assertInstanceOf( Plugin::class, $clone );
5455
}
55-
56-
public function test_wakeup_method_throws_error() {
57-
$this->setExpectedIncorrectUsage( 'WPGraphQL\Logging\Plugin::__wakeup' );
58-
59-
// Create a fresh instance
60-
$reflection = new ReflectionClass( Plugin::class );
61-
$plugin = $reflection->newInstanceWithoutConstructor();
62-
63-
$serialized = serialize( $plugin );
64-
$unserialized = unserialize( $serialized );
65-
66-
// Verify the unserialized object exists
67-
$this->assertInstanceOf( Plugin::class, $unserialized );
68-
}
6956
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
namespace WPGraphQL\Logging\Tests\Hooks;
6+
7+
use WPGraphQL\Logging\Hooks\PluginHooks;
8+
use lucatume\WPBrowser\TestCase\WPTestCase;
9+
use ReflectionClass;
10+
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
11+
12+
/**
13+
* Class PluginTest
14+
*
15+
* Tests for the Plugin class
16+
*
17+
*/
18+
class PluginHooksTest extends WPTestCase {
19+
20+
public function setUp(): void {
21+
parent::setUp();
22+
$this->drop_table();
23+
}
24+
25+
public function drop_table(): void {
26+
DatabaseEntity::drop_table();
27+
}
28+
29+
public function test_instance_from_function_in_hwp_previews() {
30+
$instance = PluginHooks::init();
31+
$this->assertTrue( $instance instanceof PluginHooks );
32+
}
33+
34+
public function test_singleton_returns_same_instance() {
35+
$first = PluginHooks::init();
36+
$second = PluginHooks::init();
37+
$this->assertSame( $first, $second, 'PluginHooks::instance() should always return the same instance' );
38+
}
39+
40+
public function test_instance_creates_and_sets_up_plugin_when_not_set() {
41+
$reflection = new ReflectionClass( PluginHooks::class );
42+
$instanceProperty = $reflection->getProperty( 'instance' );
43+
$instanceProperty->setAccessible( true );
44+
$instanceProperty->setValue( null );
45+
46+
$this->assertNull( $instanceProperty->getValue() );
47+
$instance = PluginHooks::init();
48+
49+
$this->assertInstanceOf( PluginHooks::class, $instanceProperty->getValue() );
50+
$this->assertSame( $instance, $instanceProperty->getValue(), 'Plugin::instance() should set the static instance property' );
51+
}
52+
53+
54+
public function test_database_table_creation() {
55+
global $wpdb;
56+
57+
// Setup plugin and run activation hook
58+
PluginHooks::init();
59+
wpgraphql_logging_activation_callback();
60+
61+
// Check if the table now exists
62+
$table_exists = $wpdb->get_var( $wpdb->prepare(
63+
"SHOW TABLES LIKE %s",
64+
DatabaseEntity::get_table_name()
65+
) );
66+
67+
$this->assertEquals( DatabaseEntity::get_table_name(), $table_exists, 'Database table should be created by setup method.' );
68+
}
69+
}

0 commit comments

Comments
 (0)