Skip to content

Commit

Permalink
wp-cligh-64 added exclude argument in checksums
Browse files Browse the repository at this point in the history
  • Loading branch information
ethicaladitya committed Jun 13, 2024
1 parent f691199 commit 403b964
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
17 changes: 17 additions & 0 deletions features/checksum-core.feature
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,20 @@ Feature: Validate checksums for WordPress install
Success: WordPress installation verifies against checksums.
"""
And STDERR should be empty

Scenario: Verify core checksums with excluded file
Given a WP install
And I run `rm wp-config-sample.php`

When I try `wp core verify-checksums`
Then STDERR should contain:
"""
Warning: File doesn't exist: wp-config-sample.php
"""

When I run `wp core verify-checksums --exclude=wp-config-sample.php`
Then STDOUT should be:
"""
Success: WordPress installation verifies against checksums.
"""
And STDERR should be empty
39 changes: 39 additions & 0 deletions src/Checksum_Core_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class Checksum_Core_Command extends Checksum_Base_Command {
*/
private $include_root = false;

/**
* Files or directories to exclude from verification.
*
* @var array
*/
private $exclude_paths = [];

/**
* Verifies WordPress files against WordPress.org's checksums.
*
Expand Down Expand Up @@ -44,6 +51,9 @@ class Checksum_Core_Command extends Checksum_Base_Command {
* [--insecure]
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
*
* [--exclude=<paths>]
* : Comma-separated list of files or directories to exclude from checksum verification.
*
* ## EXAMPLES
*
* # Verify checksums
Expand Down Expand Up @@ -83,6 +93,10 @@ public function __invoke( $args, $assoc_args ) {
$this->include_root = true;
}

if ( ! empty( $assoc_args['exclude'] ) ) {
$this->exclude_paths = explode( ',', $assoc_args['exclude'] );
}

if ( empty( $wp_version ) ) {
$details = self::get_wp_details();
$wp_version = $details['wp_version'];
Expand Down Expand Up @@ -112,6 +126,11 @@ public function __invoke( $args, $assoc_args ) {
continue;
}

// Skip excluded paths
if ( $this->is_excluded( $file ) ) {
continue;
}

if ( ! file_exists( ABSPATH . $file ) ) {
WP_CLI::warning( "File doesn't exist: {$file}" );
$has_errors = true;
Expand All @@ -131,6 +150,10 @@ public function __invoke( $args, $assoc_args ) {

if ( ! empty( $additional_files ) ) {
foreach ( $additional_files as $additional_file ) {
// Skip excluded paths
if ( $this->is_excluded( $additional_file ) ) {
continue;
}
WP_CLI::warning( "File should not exist: {$additional_file}" );
}
}
Expand All @@ -142,6 +165,22 @@ public function __invoke( $args, $assoc_args ) {
}
}

/**
* Checks if a file path is excluded.
*
* @param string $file Path to a file.
*
* @return bool
*/
private function is_excluded( $file ) {
foreach ( $this->exclude_paths as $exclude_path ) {
if ( strpos( $file, $exclude_path ) !== false ) {
return true;
}
}
return false;
}

/**
* Whether to include the file in the verification or not.
*
Expand Down

0 comments on commit 403b964

Please sign in to comment.