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

Add check for mismatched requires headers #868

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
65 changes: 65 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,68 @@ 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_error_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'
);
}
}
}

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

$errors = $check_result->get_errors();

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

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