Skip to content
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

Improve behavior for single-file plugins #319

Merged
merged 11 commits into from
Nov 22, 2023
6 changes: 3 additions & 3 deletions includes/Checker/Checks/Abstract_File_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ private static function get_files( Check_Context $plugin ) {

// If the location is a plugin folder, get all its files.
// Otherwise, it is a single-file plugin.
if ( $plugin->path() === $location ) {
if ( $plugin->is_single_file_plugin() ) {
self::$file_list_cache[ $location ][] = $location;
} else {
$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $location ) );
foreach ( $iterator as $file ) {
if ( ! $file->isFile() ) {
Expand All @@ -229,8 +231,6 @@ private static function get_files( Check_Context $plugin ) {
self::$file_list_cache[ $location ][] = $file_path;
}
}
} else {
self::$file_list_cache[ $location ][] = $location;
}

return self::$file_list_cache[ $location ];
Expand Down
5 changes: 5 additions & 0 deletions includes/Checker/Checks/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function get_categories() {
*/
protected function check_files( Check_Result $result, array $files ) {

// Check if single file plugin, then bail early.
if ( $result->plugin()->is_single_file_plugin() ) {
return;
}

$plugin_relative_path = $result->plugin()->path();

// Filter the readme files.
Expand Down
5 changes: 5 additions & 0 deletions includes/Checker/Checks/Trademarks_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ private function check_for_name_in_main_file( Check_Result $result ) {
* @param Check_Result $result The Check Result to amend.
*/
private function check_for_slug( Check_Result $result ) {
// Check if single file plugin, then bail early.
if ( $result->plugin()->is_single_file_plugin() ) {
return;
}

$plugin_slug = basename( $result->plugin()->path() );

try {
Expand Down
13 changes: 13 additions & 0 deletions includes/Plugin_Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,17 @@ public function location() {

return $path;
}

/**
* Checks if the plugin is a single file plugin without a dedicated directory.
*
* This is the case when the single file is directly placed within `wp-content/plugins`.
*
* @since n.e.x.t
*
* @return bool true if the single file plugin, otherwise false.
*/
public function is_single_file_plugin() {
return $this->path() !== $this->location();
}
}
16 changes: 16 additions & 0 deletions tests/phpunit/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,20 @@ public function test_run_root_readme_file_without_errors() {
$this->assertEquals( 0, $check_result->get_error_count() );
$this->assertEquals( 0, $check_result->get_warning_count() );
}

public function test_single_file_plugin_without_error_for_trademarks() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( WP_PLUGIN_DIR . '/single-file-plugin.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

$errors = $check_result->get_errors();
$warnings = $check_result->get_warnings();

$this->assertEmpty( $errors );
$this->assertEmpty( $warnings );
$this->assertSame( 0, $check_result->get_error_count() );
$this->assertSame( 0, $check_result->get_warning_count() );
}
}
17 changes: 17 additions & 0 deletions tests/phpunit/Checker/Checks/Trademarks_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,21 @@ public function test_trademarks_with_for_woocommerce_exceptions() {
$this->assertEmpty( $errors );
$this->assertSame( 0, $check_result->get_error_count() );
}

public function test_single_file_plugin_without_error_for_trademarks() {
$trademarks_check = new Trademarks_Check();
$check_context = new Check_Context( WP_PLUGIN_DIR . '/single-file-plugin.php' );
$check_result = new Check_Result( $check_context );

$trademarks_check->run( $check_result );

$errors = $check_result->get_errors();
$warnings = $check_result->get_warnings();

$this->assertEmpty( $errors );
$this->assertEmpty( $warnings );

$this->assertSame( 0, $check_result->get_error_count() );
$this->assertSame( 0, $check_result->get_warning_count() );
}
}
7 changes: 7 additions & 0 deletions tests/phpunit/Plugin_Context_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ public function test_location_with_single_file_plugin() {

$this->assertSame( $single_file, $context->location() );
}

public function test_is_single_file_plugin() {
$single_file = WP_PLUGIN_DIR . '/single-file-plugin.php';
$context = new Plugin_Context( $single_file );

$this->assertTrue( $context->is_single_file_plugin() );
}
}