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

Check restricted plugin header fields #670

Merged
merged 3 commits into from
Sep 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function run( Check_Result $result ) {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$plugin_main_file = $result->plugin()->main_file();
$plugin_header = get_plugin_data( $plugin_main_file );

$labels = array(
'Name' => 'Plugin Name',
Expand All @@ -74,6 +69,16 @@
'RequiresPlugins' => 'Requires Plugins',
);

$restricted_labels = array(
'BitbucketPluginURI' => 'Bitbucket Plugin URI',
'GistPluginURI' => 'Gist Plugin URI',
'GiteaPluginURI' => 'Gitea Plugin URI',
'GitHubPluginURI' => 'GitHub Plugin URI',
'GitLabPluginURI' => 'GitLab Plugin URI',
);
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

$plugin_header = $this->get_plugin_data( $plugin_main_file, array_merge( $labels, $restricted_labels ) );

if ( ! empty( $plugin_header['Name'] ) ) {
if ( in_array( $plugin_header['Name'], array( 'Plugin Name', 'My Basics Plugin' ), true ) ) {
$this->add_result_warning_for_file(
Expand Down Expand Up @@ -229,6 +234,31 @@
}
}

$found_headers = array();

foreach ( $restricted_labels as $restricted_key => $restricted_label ) {
if ( array_key_exists( $restricted_key, $plugin_header ) && ! empty( $plugin_header[ $restricted_key ] ) ) {
$found_headers[ $restricted_key ] = $restricted_label;

Check warning on line 241 in includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php#L241

Added line #L241 was not covered by tests
}
}

if ( ! empty( $found_headers ) ) {
$this->add_result_error_for_file(
$result,
sprintf(

Check warning on line 248 in includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php#L246-L248

Added lines #L246 - L248 were not covered by tests
/* translators: %s: header fields */
__( 'Restricted plugin header field(s) found: %s', 'plugin-check' ),
"'" . implode( "', '", array_values( $found_headers ) ) . "'"
),
'plugin_header_restricted_fields',
$plugin_main_file,
0,
0,
'',
7

Check warning on line 258 in includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php#L250-L258

Added lines #L250 - L258 were not covered by tests
);
}

if ( ! $result->plugin()->is_single_file_plugin() ) {
if ( ! empty( $plugin_header['TextDomain'] ) ) {
$plugin_slug = $result->plugin()->slug();
Expand Down Expand Up @@ -308,6 +338,30 @@
return filter_var( $url, FILTER_VALIDATE_URL ) === $url && str_starts_with( $url, 'http' );
}

/**
* Parses the plugin contents to retrieve plugin's metadata.
*
* @since 1.2.0
*
* @param string $plugin_file Absolute path to the main plugin file.
* @param array $default_headers List of headers, in the format `array( 'HeaderKey' => 'Header Name' )`.
* @return string[] Array of file header values keyed by header name.
*/
private function get_plugin_data( $plugin_file, $default_headers ) {
$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );

// If no text domain is defined fall back to the plugin slug.
if ( ! $plugin_data['TextDomain'] ) {
$plugin_slug = dirname( plugin_basename( $plugin_file ) );

if ( '.' !== $plugin_slug && ! str_contains( $plugin_slug, '/' ) ) {
$plugin_data['TextDomain'] = $plugin_slug;
}
}

return $plugin_data;
}

/**
* Gets the description for the check.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-mismathed-textdomain-here
* Domain Path: /nonexistent-folder
* GitHub Plugin URI: johndoe/package
* Requires Plugins: Example Plugin, OtherPlugin
*
* @package test-plugin-header-fields-with-errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ public function test_run_with_errors() {

$check->run( $check_result );

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

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

$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_restricted_fields' ) ) );
$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