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

Allow static checks to be run against an arbitrary path #490

Merged
4 changes: 3 additions & 1 deletion includes/Checker/Abstract_Check_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,9 @@ final protected function get_categories() {
* @return Check_Context The check context for the plugin file.
*/
private function get_check_context() {
return new Check_Context( WP_PLUGIN_DIR . '/' . $this->get_plugin_basename() );
$plugin_basename = $this->get_plugin_basename();
$plugin_path = is_dir( $plugin_basename ) ? $plugin_basename : WP_PLUGIN_DIR . '/' . $plugin_basename;
return new Check_Context( $plugin_path );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/Checker/Checks/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private function check_default_text( Check_Result $result, string $readme_file,
private function check_license( Check_Result $result, string $readme_file, Parser $parser ) {
$license = $parser->license;
$matches_license = array();
$plugin_main_file = WP_PLUGIN_DIR . '/' . $result->plugin()->basename();
$plugin_main_file = $result->plugin()->main_file();
ernilambar marked this conversation as resolved.
Show resolved Hide resolved

// Filter the readme files.
if ( empty( $license ) ) {
Expand Down
26 changes: 25 additions & 1 deletion includes/Plugin_Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@
return plugin_basename( $this->main_file );
}

/**
* Returns the plugin main file.
*
* @since 1.0.0
ernilambar marked this conversation as resolved.
Show resolved Hide resolved
*
* @return string Plugin main file.
*/
public function main_file() {
if ( false === strpos( $this->main_file, '.php' ) ) {
davidperezgar marked this conversation as resolved.
Show resolved Hide resolved
$files = glob( $this->main_file . '/*.php' );
foreach ( $files as $file ) {
$plugin_data = get_plugin_data( $file );
if ( ! empty( $plugin_data['Name'] ) ) {
return $file;

Check warning on line 85 in includes/Plugin_Context.php

View check run for this annotation

Codecov / codecov/patch

includes/Plugin_Context.php#L81-L85

Added lines #L81 - L85 were not covered by tests
}
}
}
return $this->main_file;
}

/**
* Returns the absolute path for a relative path to the plugin directory.
*
Expand All @@ -78,7 +98,11 @@
* @return string Absolute path.
*/
public function path( $relative_path = '/' ) {
return plugin_dir_path( $this->main_file ) . ltrim( $relative_path, '/' );
if ( is_dir( $this->main_file ) ) {
return trailingslashit( $this->main_file ) . ltrim( $relative_path, '/' );

Check warning on line 102 in includes/Plugin_Context.php

View check run for this annotation

Codecov / codecov/patch

includes/Plugin_Context.php#L102

Added line #L102 was not covered by tests
} else {
return plugin_dir_path( $this->main_file ) . ltrim( $relative_path, '/' );
}
}

/**
Expand Down
6 changes: 5 additions & 1 deletion includes/Utilities/Plugin_Request_Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@
*
* @since 1.0.0
*
* @param string $plugin_slug The plugin slug or basename.
* @param string $plugin_slug The plugin slug, basename or arbitrary path.
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
* @return string The plugin basename.
*
* @throws Exception Thrown if an invalid basename or plugin slug is provided.
*/
public static function get_plugin_basename_from_input( $plugin_slug ) {
if ( is_dir( $plugin_slug ) ) {
return $plugin_slug;

Check warning on line 50 in includes/Utilities/Plugin_Request_Utility.php

View check run for this annotation

Codecov / codecov/patch

includes/Utilities/Plugin_Request_Utility.php#L50

Added line #L50 was not covered by tests
}
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

require_once ABSPATH . 'wp-admin/includes/plugin.php';

if ( empty( $plugin_slug ) ) {
Expand Down
Loading