Skip to content

Commit

Permalink
Accept plugin URL in check command
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Jul 24, 2024
1 parent 436bcec commit f67cdb1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions includes/Checker/Abstract_Check_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,12 @@ final public function get_plugin_basename() {

if ( is_dir( $plugin ) ) {
$this->plugin_basename = $plugin;
} elseif ( filter_var( $plugin, FILTER_VALIDATE_URL ) ) {
$plugin_full_path = Plugin_Request_Utility::download_plugin( $plugin );

if ( false !== $plugin_full_path ) {
$this->plugin_basename = $plugin_full_path;
}
} else {
$this->plugin_basename = Plugin_Request_Utility::get_plugin_basename_from_input( $plugin );
}
Expand Down
59 changes: 59 additions & 0 deletions includes/Utilities/Plugin_Request_Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,63 @@ public static function get_files_to_ignore() {

return $files_to_ignore;
}

/**
* Downloads the plugin from the URL.
*
* @since 1.1.0
*
* @param string $plugin_url The URL of the plugin to download.
* @return string|bool The plugin basename if the plugin is downloaded successfully, false otherwise.
*/
public static function download_plugin( $plugin_url ) {
$response = wp_remote_get( $plugin_url );

if ( is_wp_error( $response ) ) {
return false;

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

View check run for this annotation

Codecov / codecov/patch

includes/Utilities/Plugin_Request_Utility.php#L197

Added line #L197 was not covered by tests
}

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

WP_Filesystem();

$basename = basename( $plugin_url );

$plugin_check_dir = self::get_temporary_upload_dir();

$file_path = $plugin_check_dir . $basename;
$file_process = fopen( $file_path, 'w' );
fwrite( $file_process, $response['body'] );
fclose( $file_process );

$folder_name = pathinfo( $plugin_url, PATHINFO_FILENAME );

$target_folder_name = explode( '.', $folder_name )[0];

// Unzip file.
if ( unzip_file( $file_path, $plugin_check_dir ) ) {
unlink( $file_path );

return $plugin_check_dir . $target_folder_name;
}

return false;

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

View check run for this annotation

Codecov / codecov/patch

includes/Utilities/Plugin_Request_Utility.php#L224

Added line #L224 was not covered by tests
}

/**
* Get the upload directory for the plugin check.
*
* @since 1.1.0
*
* @return string The upload directory for the plugin check.
*/
public static function get_temporary_upload_dir() {
$upload_dir = trailingslashit( get_temp_dir() ) . 'plugin-check/';

if ( ! is_dir( $upload_dir ) ) {
mkdir( $upload_dir, 0755, true );
}

return $upload_dir;
}
}
13 changes: 13 additions & 0 deletions tests/behat/features/plugin-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,16 @@ Feature: Test that the WP-CLI command works.
"""
no_plugin_readme
"""

Scenario: Check a plugin from external zip
Given a WP install with the Plugin Check plugin

When I run the WP-CLI command `plugin check https://downloads.wordpress.org/plugin/hello-dolly.1.7.3.zip --fields=code,type --format=csv`
And STDOUT should contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR
"""
And STDOUT should contain:
"""
stable_tag_mismatch,ERROR
"""

0 comments on commit f67cdb1

Please sign in to comment.