Skip to content

Commit

Permalink
Merge pull request #819 from WordPress/add-check-restricted-contributors
Browse files Browse the repository at this point in the history
Add check for restricted contributors
  • Loading branch information
ernilambar authored Dec 9, 2024
2 parents f23a0b4 + a6c3d3c commit 4505d67
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
101 changes: 100 additions & 1 deletion includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ private function check_for_donate_link( Check_Result $result, string $readme_fil
*
* @param Check_Result $result The Check Result to amend.
* @param string $readme_file Readme file.
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function check_for_contributors( Check_Result $result, string $readme_file ) {
$regex = '/Contributors\s?:(?:\*\*|\s)?(.*?)\R/';
Expand All @@ -666,7 +668,7 @@ private function check_for_contributors( Check_Result $result, string $readme_fi

$usernames = explode( ',', $matches[1] );

$usernames = array_map( 'trim', $usernames );
$usernames = array_unique( array_map( 'trim', $usernames ) );

$valid = true;

Expand All @@ -692,6 +694,72 @@ private function check_for_contributors( Check_Result $result, string $readme_fi
'',
6
);

return;
}

$restricted_contributors = $this->get_restricted_contributors();

$disallowed_contributors = array_keys(
array_filter(
$restricted_contributors,
function ( $value ) {
return 'error' === strtolower( $value );
}
)
);

if ( ! empty( $disallowed_contributors ) ) {
$disallowed_usernames = array_intersect( $usernames, $disallowed_contributors );

if ( ! empty( $disallowed_usernames ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: usernames */
__( 'The "%1$s" header in the readme file contains restricted username(s). Found: %2$s', 'plugin-check' ),
'Contributors',
'"' . implode( '", "', $disallowed_usernames ) . '"'
),
'readme_restricted_contributors',
$readme_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/#readme-header-information',
7
);
}
}

$reserved_contributors = array_keys(
array_filter(
$restricted_contributors,
function ( $value ) {
return 'warning' === strtolower( $value );
}
)
);

if ( ! empty( $reserved_contributors ) ) {
$reserved_usernames = array_intersect( $usernames, $reserved_contributors );

if ( ! empty( $reserved_usernames ) ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: usernames */
__( 'The "%1$s" header in the readme file contains reserved username(s). Found: %2$s', 'plugin-check' ),
'Contributors',
'"' . implode( '", "', $reserved_usernames ) . '"'
),
'readme_reserved_contributors',
$readme_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/#readme-header-information',
6
);
}
}
}

Expand Down Expand Up @@ -762,6 +830,37 @@ private function get_ignored_warnings( Parser $parser ) {
return $ignored_warnings;
}

/**
* Returns restricted contributors.
*
* @since 1.4.0
*
* @return array Restricted contributors.
*/
private function get_restricted_contributors() {
$restricted_contributors = array(
'username' => 'error',
'your-name' => 'error',
'your-username' => 'error',
'your-wordpress-username' => 'error',
'your_wordpress_username' => 'error',
'yourusername' => 'error',
'yourwordpressusername' => 'error',
'wordpressdotorg' => 'warning',
);

/**
* Filter the list of restricted contributors.
*
* @since 1.4.0
*
* @param array $restricted_contributors Array of restricted contributors with error type.
*/
$restricted_contributors = (array) apply_filters( 'wp_plugin_check_restricted_contributors', $restricted_contributors );

return $restricted_contributors;
}

/**
* Gets the description for the check.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== Plugin Name ===

Contributors: plugin-check
Contributors: plugin-check, username, wordpressdotorg
Requires at least: 6.0
Tested up to: 6.1
Requires PHP: 5.6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

=== Test Plugin Readme Errors Parser Warnings ===

Contributors: plugin check, wordpressdotorg
Contributors: plugin check, johndoe
Requires at least: 6.0
Tested up to: Latest
Requires PHP: 5.6
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ public function test_run_with_errors_invalid_name() {
$this->assertCount( 1, wp_list_filter( $errors['readme.txt'][0][0], array( 'code' => 'invalid_plugin_name' ) ) );
}

public function test_run_with_errors_restricted_contributors() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-errors-invalid-name/load.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

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

$this->assertNotEmpty( $errors );
$this->assertNotEmpty( $warnings );
$this->assertArrayHasKey( 'readme.txt', $errors );
$this->assertArrayHasKey( 'readme.txt', $warnings );

// Check for restricted contributors error.
$this->assertCount( 1, wp_list_filter( $errors['readme.txt'][0][0], array( 'code' => 'readme_restricted_contributors' ) ) );

// Check for reserved contributors warning.
$this->assertCount( 1, wp_list_filter( $warnings['readme.txt'][0][0], array( 'code' => 'readme_reserved_contributors' ) ) );
}

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

0 comments on commit 4505d67

Please sign in to comment.