Skip to content

Commit

Permalink
Add check for mismatched requires headers
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Jan 31, 2025
1 parent f43fc9e commit d528d19
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
66 changes: 66 additions & 0 deletions includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ protected function check_files( Check_Result $result, array $files ) {

// Check the readme file for contributors.
$this->check_for_contributors( $result, $readme_file );

// Check the readme file for requires headers.
$this->check_requires_headers( $result, $readme_file, $parser );
}

/**
Expand Down Expand Up @@ -788,6 +791,69 @@ function ( $value ) {
}
}

/**
* Checks the readme file for requires headers.
*
* @since 1.5.0
*
* @param Check_Result $result The Check Result to amend.
* @param string $readme_file Readme file.
* @param Parser $parser The Parser object.
*/
private function check_requires_headers( Check_Result $result, string $readme_file, Parser $parser ) {
$ignored_warnings = $this->get_ignored_warnings( $parser );

$found_warnings = $parser->warnings ? $parser->warnings : array();

$current_warnings = array_diff( array_keys( $found_warnings ), $ignored_warnings );

$requires = array(
'requires_header_ignored' => array(
'label' => 'Requires at least',
'key' => 'requires',
'header_field' => 'RequiresWP',
),
'requires_php_header_ignored' => array(
'label' => 'Requires PHP',
'key' => 'requires_php',
'header_field' => 'RequiresPHP',
),
);

// Find potential requires keys to check.
$potential_requires = array_diff( array_keys( $requires ), $current_warnings );

// Bail if not found.
if ( empty( $potential_requires ) ) {
return;
}

$plugin_data = get_plugin_data( $result->plugin()->main_file(), false, false );

foreach ( $potential_requires as $require ) {
$readme_value = $parser->{$requires[ $require ]['key']};
$plugin_value = $plugin_data[ $requires[ $require ]['header_field'] ];

if ( $readme_value !== $plugin_value ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: 1: readme header tag, 2: versions comparison */
__( '<strong>Mismatched %1$s: %2$s.</strong><br>"%1$s" needs to be exactly the same with that in your main plugin file\'s header.', 'plugin-check' ),
esc_html( $requires[ $require ]['label'] ),
esc_html( $readme_value ) . ' != ' . esc_html( $plugin_value )
),
'readme_mismatched_header_' . $requires[ $require ]['key'],
$readme_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/#readme-header-information',
6
);
}
}
}

/**
* Returns ignored warnings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Plugin Name: Test Trademarks Plugin Readme Errors
* Plugin URI: https://github.com/WordPress/plugin-check
* Description: Test plugin for the Trademarks check.
* Requires at least: 6.0
* Requires PHP: 5.6
* Requires at least: 5.0
* Requires PHP: 7.2
* Version: 1.0.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
Expand Down
16 changes: 16 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 @@ -541,4 +541,20 @@ public function test_run_without_errors_readme_contributors_warning() {
// Should not contain contributors warning.
$this->assertCount( 0, wp_list_filter( $warnings['readme.txt'][0][0], array( 'code' => 'readme_invalid_contributors' ) ) );
}

public function test_run_with_mismatched_requires_headers() {
$check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-trademarks-plugin-readme-errors/load.php' );
$check_result = new Check_Result( $check_context );

$check->run( $check_result );

$warnings = $check_result->get_warnings();

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

$this->assertCount( 1, wp_list_filter( $warnings['readme.txt'][0][0], array( 'code' => 'readme_mismatched_header_requires' ) ) );
$this->assertCount( 1, wp_list_filter( $warnings['readme.txt'][0][0], array( 'code' => 'readme_mismatched_header_requires_php' ) ) );
}
}

0 comments on commit d528d19

Please sign in to comment.