diff --git a/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php b/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php index 33705de67..e2fe1add3 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php @@ -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', @@ -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( @@ -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; } } @@ -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. *