diff --git a/features/checksum-core.feature b/features/checksum-core.feature index de05fb3f..64de696a 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -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 diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 321b89a4..e7bed59f 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -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. * @@ -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=] + * : Comma-separated list of files or directories to exclude from checksum verification. + * * ## EXAMPLES * * # Verify checksums @@ -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']; @@ -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; @@ -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}" ); } } @@ -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. *