Skip to content

Commit

Permalink
Merge pull request #775 from WordPress/separate-license-error-codes
Browse files Browse the repository at this point in the history
Add separate license error codes for Plugin Header
  • Loading branch information
ernilambar authored Nov 11, 2024
2 parents 077ed8e + 79c10db commit 2b29910
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
40 changes: 40 additions & 0 deletions includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Static_Check;
use WordPress\Plugin_Check\Traits\Amend_Check_Result;
use WordPress\Plugin_Check\Traits\License_Utils;
use WordPress\Plugin_Check\Traits\Stable_Check;

/**
Expand All @@ -22,6 +23,7 @@
class Plugin_Header_Fields_Check implements Static_Check {

use Amend_Check_Result;
use License_Utils;
use Stable_Check;

/**
Expand Down Expand Up @@ -67,6 +69,8 @@ public function run( Check_Result $result ) {
'RequiresPHP' => 'Requires PHP',
'UpdateURI' => 'Update URI',
'RequiresPlugins' => 'Requires Plugins',
'License' => 'License',
'LicenseURI' => 'License URI',
);

$restricted_labels = array(
Expand Down Expand Up @@ -249,6 +253,42 @@ public function run( Check_Result $result ) {
}
}

if ( empty( $plugin_header['License'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( '<strong>Missing "%s" in Plugin Header.</strong><br>Please update your Plugin Header with a valid GPLv2 (or later) compatible license.', 'plugin-check' ),
esc_html( $labels['License'] )
),
'plugin_header_no_license',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared',
9
);
} else {
$plugin_license = $this->get_normalized_license( $plugin_header['License'] );
if ( ! $this->is_license_gpl_compatible( $plugin_license ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: license */
__( '<strong>Invalid %1$s: %2$s.</strong><br>Please update your Plugin Header with a valid GPLv2 (or later) compatible license.', 'plugin-check' ),
esc_html( $labels['License'] ),
esc_html( $plugin_header['License'] )
),
'plugin_header_invalid_license',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared',
9
);
}
}

$found_headers = array();

foreach ( $restricted_labels as $restricted_key => $restricted_label ) {
Expand Down
30 changes: 4 additions & 26 deletions includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,35 +338,13 @@ private function check_license( Check_Result $result, string $readme_file, Parse
);
}

$plugin_license = '';

$pattern = preg_quote( 'License', '/' );
$has_license = self::file_preg_match( "/(*ANYCRLF)^.*$pattern\s*:\s*(.*)$/im", array( $plugin_main_file ), $matches_license );
if ( ! $has_license ) {
$this->add_result_error_for_file(
$result,
__( '<strong>Your plugin has no license declared in Plugin Header.</strong><br>Please update your plugin header with a GPLv2 (or later) compatible license. It is necessary to declare the license of this plugin. You can do this by using the fields available both in the plugin readme and in the plugin headers.', 'plugin-check' ),
'no_license',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared',
9
);
} else {
$plugin_license = $this->get_normalized_license( $matches_license[1] );
}

// Checks for a valid license in Plugin Header.
if ( ! empty( $plugin_license ) && ! $this->is_license_gpl_compatible( $plugin_license ) ) {
$this->add_result_error_for_file(
$result,
__( '<strong>Your plugin has an invalid license declared in Plugin Header.</strong><br>Please update your readme with a valid GPL license identifier. It is necessary to declare the license of this plugin. You can do this by using the fields available both in the plugin readme and in the plugin headers.', 'plugin-check' ),
'invalid_license',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared',
9
);
if ( $has_license ) {
$plugin_license = $this->get_normalized_license( $matches_license[1] );
}

// Check different license types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* Version: 1.0.0
* Author: WordPress Performance Team
* Author URI: This is not a valid URL
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-mismathed-textdomain-here
* Domain Path: /nonexistent-folder
* Network: random-value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function test_run_with_errors() {
$this->assertCount( 0, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_restricted_fields' ) ) );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_requires_wp' ) ) );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_requires_php' ) ) );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_no_license' ) ) );
$this->assertCount( 1, wp_list_filter( $warnings['load.php'][0][0], array( 'code' => 'plugin_header_invalid_plugin_uri_domain' ) ) );
$this->assertCount( 1, wp_list_filter( $warnings['load.php'][0][0], array( 'code' => 'plugin_header_invalid_plugin_description' ) ) );
$this->assertCount( 1, wp_list_filter( $warnings['load.php'][0][0], array( 'code' => 'plugin_header_invalid_author_uri' ) ) );
Expand Down Expand Up @@ -57,4 +58,19 @@ public function test_run_with_valid_requires_plugins_header() {
$this->assertCount( 0, wp_list_filter( $warnings['load.php'][0][0], array( 'code' => 'plugin_header_invalid_requires_plugins' ) ) );
}
}

public function test_run_with_invalid_mpl1_license() {
$check = new Plugin_Header_Fields_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-mpl1-license-with-errors/load.php' );
$check_result = new Check_Result( $check_context );

$check->run( $check_result );

$errors = $check_result->get_errors();

$this->assertNotEmpty( $errors );

// Check for invalid license.
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_license' ) ) );
}
}
18 changes: 0 additions & 18 deletions tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,6 @@ public function test_run_without_error_mpl2_license() {
$this->assertEmpty( $errors );
}

public function test_run_with_errors_mpl1_license() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-mpl1-license-with-errors/load.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

$errors = $check_result->get_errors();

$this->assertNotEmpty( $errors );
$this->assertArrayHasKey( 'load.php', $errors );

// Check for invalid license.
$this->assertArrayHasKey( 0, $errors['load.php'] );
$this->assertArrayHasKey( 0, $errors['load.php'][0] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'invalid_license' ) ) );
}

public function test_run_with_errors_tested_upto() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-errors-tested-upto/load.php' );
Expand Down

0 comments on commit 2b29910

Please sign in to comment.