Skip to content

Commit

Permalink
Add private method for custom get_plugin_data() output
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Sep 26, 2024
1 parent 07f788f commit 4ccecc6
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public function get_categories() {
* @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 @@ public function run( Check_Result $result ) {
'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',
);

$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,20 +234,10 @@ public function run( Check_Result $result ) {
}
}

$restricted_headers = array(
'BitbucketPluginURI' => 'Bitbucket Plugin URI',
'GistPluginURI' => 'Gist Plugin URI',
'GiteaPluginURI' => 'Gitea Plugin URI',
'GitHubPluginURI' => 'GitHub Plugin URI',
'GitLabPluginURI' => 'GitLab Plugin URI',
);

$plugin_data = get_file_data( $plugin_main_file, $restricted_headers, 'plugin' );

$found_headers = array();

foreach ( $restricted_headers as $restricted_key => $restricted_label ) {
if ( array_key_exists( $restricted_key, $plugin_data ) && ! empty( $plugin_data[ $restricted_key ] ) ) {
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
}
}
Expand Down Expand Up @@ -343,6 +338,30 @@ private function is_valid_url( $url ) {
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

0 comments on commit 4ccecc6

Please sign in to comment.