-
Notifications
You must be signed in to change notification settings - Fork 97
WP_UNINSTALL_PLUGIN check must be there in uninstall.php file
#958
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
Merged
davidperezgar
merged 9 commits into
WordPress:trunk
from
hrkhal:929-wp-uninstall-plugin-constant-check-uninstall-php
Jun 23, 2025
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eb72088
feat: Check for usage of Uninstall constant in uninstall.php
hrkhal b1612f4
Testing and test plugins
hrkhal 5febadd
Checking for defined check as opposed to just existence
hrkhal 1face82
Fixing tests and returning error instead of warning
hrkhal 025b897
Remove debug code
hrkhal 3844805
linting
hrkhal 9dc3fde
Merge branch 'trunk' into 929-wp-uninstall-plugin-constant-check-unin…
ernilambar e67433a
Optimize the uninstall check and update tests
ernilambar b107b10
Fix name and textdomains
ernilambar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
| /** | ||
| * Class Plugin_Uninstall_Constant_Check. | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| namespace WordPress\Plugin_Check\Checker\Checks\Plugin_Repo; | ||
|
|
||
| use Exception; | ||
| use WordPress\Plugin_Check\Checker\Check_Categories; | ||
| use WordPress\Plugin_Check\Checker\Check_Result; | ||
| use WordPress\Plugin_Check\Checker\Checks\Abstract_File_Check; | ||
| use WordPress\Plugin_Check\Traits\Amend_Check_Result; | ||
| use WordPress\Plugin_Check\Traits\Stable_Check; | ||
|
|
||
| /** | ||
| * Check to detect if a definition check for WP_UNINSTALL_PLUGIN takes place in uninstall.php, if it exists | ||
| * | ||
| * @since 1.0.0 | ||
| */ | ||
| class Plugin_Uninstall_Constant_Check extends Abstract_File_Check { | ||
|
|
||
| use Amend_Check_Result; | ||
| use Stable_Check; | ||
|
|
||
| /** | ||
| * Gets the categories for the check. | ||
| * | ||
| * Every check must have at least one category. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return array The categories for the check. | ||
| */ | ||
| public function get_categories() { | ||
| return array( Check_Categories::CATEGORY_PLUGIN_REPO ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks the uninstall.php file for a definition check of the WP_UNINSTALL_PLUGIN constant | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @param Check_Result $result The check result to amend, including the plugin context to check. | ||
| * @param array $files List of absolute file paths. | ||
| * | ||
| * @throws Exception Thrown when the check fails with a critical error (unrelated to any errors detected as part of | ||
| * the check). | ||
| */ | ||
| protected function check_files( Check_Result $result, array $files ) { | ||
| $constant_regex = '#defined\s*\(.*WP_UNINSTALL_PLUGIN.*\)#'; | ||
| $matches = array(); | ||
| $plugin_uninstall_file = self::filter_files_by_regex( $files, '/uninstall\.php$/' ); | ||
| if ( $plugin_uninstall_file ) { | ||
| foreach ( $plugin_uninstall_file as $file ) { | ||
| $uninstall_constant = self::file_preg_match( $constant_regex, [ $file ], $matches ); | ||
| if ( ! $uninstall_constant ) { | ||
| $this->add_result_warning_for_file( | ||
| $result, | ||
| sprintf( | ||
| /* translators: %s: The match file name. */ | ||
| __( 'WP_UNINSTALL_PLUGIN constant not being checked. Detected: %s', 'plugin-check' ), | ||
| esc_html( $matches[0] ) | ||
| ), | ||
| 'uninstall_no_constant_check', | ||
| $file, | ||
| 0, | ||
| 0, | ||
| 'https://developer.wordpress.org/plugins/plugin-basics/uninstall-methods/#method-2-uninstall-php', | ||
| 9 | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Gets the description for the check. | ||
| * | ||
| * Every check must have a short description explaining what the check does. | ||
| * | ||
| * @since 1.1.0 | ||
| * | ||
| * @return string Description. | ||
| */ | ||
| public function get_description(): string { | ||
| return __( 'Confirms usage of WP_Uninstall_Plugin constant checker in uninstall.php', 'plugin-check' ); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the documentation URL for the check. | ||
| * | ||
| * Every check must have a URL with further information about the check. | ||
| * | ||
| * @since 1.1.0 | ||
| * | ||
| * @return string The documentation URL. | ||
| */ | ||
| public function get_documentation_url(): string { | ||
| return __( 'https://developer.wordpress.org/plugins/plugin-basics/uninstall-methods/#method-2-uninstall-php', 'plugin-check' ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/phpunit/testdata/plugins/test-plugin-uninstall-constant-errors/load.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
| /** | ||
| * Plugin Name: Test Plugin Uninstall Constant without Errors for Plugin Check | ||
| * Plugin URI: https://github.com/WordPress/plugin-check | ||
| * Description: Some plugin description. | ||
| * Requires at least: 6.0 | ||
| * Requires PHP: 5.6 | ||
| * Version: 1.0.0 | ||
| * Author: WordPress Performance Team | ||
| * Author URI: https://make.wordpress.org/performance/ | ||
| * License: GPLv2 or later | ||
| * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
| * Text Domain: test-plugin-uninstall-constant-without-errors | ||
| * Update URI: https://wordpress.org/plugins/example-plugin/ | ||
| * | ||
| * @package test-plugin-uninstall-constant-without-errors | ||
| */ |
2 changes: 2 additions & 0 deletions
2
tests/phpunit/testdata/plugins/test-plugin-uninstall-constant-errors/uninstall.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <?php | ||
| // Silence |
17 changes: 17 additions & 0 deletions
17
tests/phpunit/testdata/plugins/test-plugin-uninstall-constant-without-errors/load.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
| /** | ||
| * Plugin Name: Test Plugin Uninstall Constant without Errors for Plugin Check | ||
| * Plugin URI: https://github.com/WordPress/plugin-check | ||
| * Description: Some plugin description. | ||
| * Requires at least: 6.0 | ||
| * Requires PHP: 5.6 | ||
| * Version: 1.0.0 | ||
| * Author: WordPress Performance Team | ||
| * Author URI: https://make.wordpress.org/performance/ | ||
| * License: GPLv2 or later | ||
| * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
| * Text Domain: test-plugin-uninstall-constant-without-errors | ||
| * Update URI: https://wordpress.org/plugins/example-plugin/ | ||
| * | ||
| * @package test-plugin-uninstall-constant-without-errors | ||
| */ |
4 changes: 4 additions & 0 deletions
4
tests/phpunit/testdata/plugins/test-plugin-uninstall-constant-without-errors/uninstall.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <?php | ||
| if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { | ||
| die; | ||
| } |
46 changes: 46 additions & 0 deletions
46
tests/phpunit/tests/Checker/Checks/Plugin_Uninstall_Constant_Check.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the Plugin_Uninstall_Constant_Check class. | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| use WordPress\Plugin_Check\Checker\Check_Context; | ||
| use WordPress\Plugin_Check\Checker\Check_Result; | ||
| use WordPress\Plugin_Check\Checker\Checks\Plugin_Repo\Plugin_Uninstall_Constant_Check; | ||
|
|
||
| class Plugin_Uninstall_Constant_Check_Tests extends WP_UnitTestCase { | ||
|
|
||
| public function test_run_with_plugin_uninstall_errors( ) { | ||
| $test_file = 'test-plugin-uninstall-constant-errors/load.php'; | ||
| $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . $test_file ); | ||
| $check_result = new Check_Result( $check_context ); | ||
|
|
||
| $check = new Plugin_Uninstall_Constant_Check(); | ||
| $check->run( $check_result ); | ||
|
|
||
| $errors = $check_result->get_errors(); | ||
|
|
||
| $this->assertNotEmpty( $errors ); | ||
| $this->assertArrayHasKey( $test_file, $errors ); | ||
| $this->assertSame( 1, $check_result->get_error_count() ); | ||
|
|
||
| $this->assertTrue( isset( $errors[ $test_file ][0][0][0] ) ); | ||
| $this->assertSame( 'uninstall_no_constant_check', $errors[ $test_file ][0][0][0]['code'] ); | ||
|
|
||
| } | ||
|
|
||
| public function test_run_without_any_errors() { | ||
| $test_file = 'test-plugin-uninstall-constant-without-errors/load.php'; | ||
| $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . $test_file); | ||
| $check_result = new Check_Result( $check_context ); | ||
|
|
||
| $check = new Plugin_Uninstall_Constant_Check(); | ||
| $check->run( $check_result ); | ||
|
|
||
| $errors = $check_result->get_errors(); | ||
|
|
||
| $this->assertEmpty( $errors ); | ||
| $this->assertEquals( 0, $check_result->get_error_count() ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.