Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Comment thread
davidperezgar marked this conversation as resolved.
Outdated

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(

Check warning on line 61 in includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php#L56-L61

Added lines #L56 - L61 were not covered by tests
/* 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
);

Check warning on line 72 in includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php#L63-L72

Added lines #L63 - L72 were not covered by tests
}
}
}
}


/**
* 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' );

Check warning on line 89 in includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php#L88-L89

Added lines #L88 - L89 were not covered by tests
}

/**
* 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' );

Check warning on line 102 in includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php#L101-L102

Added lines #L101 - L102 were not covered by tests
}
}
1 change: 1 addition & 0 deletions includes/Checker/Default_Check_Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private function register_default_checks() {
'plugin_header_fields' => new Checks\Plugin_Repo\Plugin_Header_Fields_Check(),
'late_escaping' => new Checks\Security\Late_Escaping_Check(),
'plugin_updater' => new Checks\Plugin_Repo\Plugin_Updater_Check(),
'plugin_uninstall' => new Checks\Plugin_Repo\Plugin_Uninstall_Constant_Check(),
'plugin_review_phpcs' => new Checks\Plugin_Repo\Plugin_Review_PHPCS_Check(),
'direct_db_queries' => new Checks\Security\Direct_DB_Queries_Check(),
'performant_wp_query_params' => new Checks\Performance\Performant_WP_Query_Params_Check(),
Expand Down
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
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
// Silence
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
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
die;
}
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() );
}
}
Loading