Skip to content

Commit a2321cd

Browse files
authored
Merge pull request #525 from wpengine/hwp-previews-uninstall-cleanup
chore(previews): Add uninstallation hook
2 parents 9807af7 + a120c89 commit a2321cd

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

.changeset/tall-wasps-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@wpengine/hwp-previews-wordpress-plugin": patch
3+
---
4+
5+
Add optional data cleanup on uninstall via HWP_PREVIEWS_UNINSTALL_PLUGIN constant

docs/plugins/hwp-previews/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This plugin bridges the preview gap in headless WordPress architectures, allowin
1717
* [Configuration](#configuration)
1818
* [Front-End Integration](#front-end-integration)
1919
* [Using With Faust.js](#using-with-faustjs)
20+
* [Uninstallation](#uninstallation)
2021
* [Documentation](#documentation)
2122
* [Contributing](#contributing)
2223

@@ -132,6 +133,18 @@ To implement previews from scratch, refer to your framework's documentation:
132133

133134
HWP Previews automatically integrates with [Faust.js](https://faustjs.org/) when both plugins are active. See the [Integrate with Faust.js](how-to/integrate-with-faust/index.md) guide for details.
134135

136+
## Uninstallation
137+
138+
By default, HWP Previews preserves all settings when the plugin is deactivated to prevent accidental data loss.
139+
140+
If you would like to remove all plugin settings and data, you must set the PHP constant before you uninstall the plugin:
141+
142+
```php
143+
define( 'HWP_PREVIEWS_UNINSTALL_PLUGIN', true );
144+
```
145+
146+
You can add this constant to your `wp-config.php` file if you want to enable automatic cleanup during uninstallation.
147+
135148
## Documentation
136149

137150
### How-to guides

plugins/hwp-previews/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- [Using With Faust.js](#using-with-faustjs)
3636
- [Extending the Functionality](#extending-the-functionality)
3737
- [Testing](#testing)
38+
- [Uninstallation](#uninstallation)
3839

3940
## Overview
4041

@@ -171,6 +172,18 @@ See the [Actions & Filters documentation](ACTIONS_AND_FILTERS.md) for a comprehe
171172

172173
See [Testing.md](TESTING.md) for details on how to test the plugin.
173174

175+
## Uninstallation
176+
177+
By default, HWP Previews preserves all settings when the plugin is deactivated to prevent accidental data loss.
178+
179+
If you would like to remove all plugin settings and data, you must set the PHP constant before you uninstall the plugin:
180+
181+
```php
182+
define( 'HWP_PREVIEWS_UNINSTALL_PLUGIN', true );
183+
```
184+
185+
You can add this constant to your `wp-config.php` file if you want to enable automatic cleanup during uninstallation.
186+
174187
## Screenshots
175188

176189
<details>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
namespace HWP\Previews\Tests\Core;
6+
7+
use lucatume\WPBrowser\TestCase\WPTestCase;
8+
9+
/**
10+
* Test class for the uninstall callback.
11+
*/
12+
class UninstallTest extends WPTestCase {
13+
14+
public function test_uninstall_file_exists(): void {
15+
$uninstall_file = dirname( __DIR__, 3 ) . '/uninstall.php';
16+
$this->assertFileExists( $uninstall_file, 'uninstall.php file should exist' );
17+
}
18+
19+
public function test_option_can_be_deleted(): void {
20+
// Test that delete_option works (simulating what uninstall.php does).
21+
$option_key = HWP_PREVIEWS_SETTINGS_KEY;
22+
$test_data = [ 'test' => 'data' ];
23+
24+
update_option( $option_key, $test_data );
25+
$this->assertEquals( $test_data, get_option( $option_key ) );
26+
27+
delete_option( $option_key );
28+
$this->assertFalse( get_option( $option_key ) );
29+
}
30+
31+
public function test_uninstall_action_hook_exists(): void {
32+
// Verify the action hook can be registered.
33+
$called = false;
34+
35+
add_action( 'hwp_previews_after_uninstall', function () use ( &$called ) {
36+
$called = true;
37+
} );
38+
39+
do_action( 'hwp_previews_after_uninstall' );
40+
41+
$this->assertTrue( $called, 'hwp_previews_after_uninstall action should fire' );
42+
}
43+
}
44+

plugins/hwp-previews/uninstall.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Uninstall HWP Previews
4+
*
5+
* Deletes all plugin data when the plugin is uninstalled,
6+
* only if HWP_PREVIEWS_UNINSTALL_PLUGIN constant is defined.
7+
*
8+
* @package HWP\Previews
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
// If uninstall not called from WordPress, then exit.
14+
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
15+
exit;
16+
}
17+
18+
// Only proceed if the uninstall constant is defined.
19+
if ( ! defined( 'HWP_PREVIEWS_UNINSTALL_PLUGIN' ) || ! HWP_PREVIEWS_UNINSTALL_PLUGIN ) {
20+
return;
21+
}
22+
23+
// Define constants if not already defined.
24+
if ( ! defined( 'HWP_PREVIEWS_SETTINGS_KEY' ) ) {
25+
define( 'HWP_PREVIEWS_SETTINGS_KEY', 'hwp_previews_settings' );
26+
}
27+
28+
// Delete plugin settings.
29+
delete_option( HWP_PREVIEWS_SETTINGS_KEY );
30+
31+
// Fire action for extensibility.
32+
do_action( 'hwp_previews_after_uninstall' );
33+

0 commit comments

Comments
 (0)