Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
23856fb
feat: add --format param for core checksums verification
alaminfirdows Aug 7, 2025
0f0586c
fix: improve error handling output format in checksum verification
alaminfirdows Aug 7, 2025
0b19b82
feat: add scenarios for verifying core checksums with format parameter
alaminfirdows Aug 7, 2025
805292f
refactor: remove redundant scenarios for verifying core checksums wit…
alaminfirdows Aug 7, 2025
37e7679
fix: optimize format handling in checksum verification errors
alaminfirdows Aug 7, 2025
a4665e3
feat: add scenarios for verifying core checksums with various format …
alaminfirdows Aug 7, 2025
f9ffb78
refactor: remove redundant scenario for verifying core checksums with…
alaminfirdows Aug 7, 2025
01629b0
fix: update error message for clarity in checksum verification
alaminfirdows Aug 7, 2025
263de93
fix: remove add_error method and use inline array item push
alaminfirdows Aug 15, 2025
5f7b163
fix: improve error handling and output formatting in checksum verific…
alaminfirdows Aug 15, 2025
b51a64c
fix: update error handling to use warnings for checksum verification …
alaminfirdows Aug 15, 2025
5851d4b
fix: update scenarios for core checksums verification with format par…
alaminfirdows Aug 15, 2025
e4d4f6f
fix: yoda condition check
alaminfirdows Aug 15, 2025
1b3112e
fix: update checksum verification scenarios to include new file check…
alaminfirdows Aug 16, 2025
098bb25
wip
alaminfirdows Aug 16, 2025
f0601d7
wip
alaminfirdows Aug 16, 2025
e2845ba
fix: standardize error flag assignment spacing in checksum verification
alaminfirdows Aug 16, 2025
967e0dd
fix: standardize spacing for error flag assignment in checksum verifi…
alaminfirdows Aug 16, 2025
9518886
wip
alaminfirdows Aug 17, 2025
92ecea1
fix: add missing commas in error messages for clarity
alaminfirdows Aug 17, 2025
1b4e576
fix: clarify output format description in checksum verification options
alaminfirdows Aug 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions features/checksum-core.feature
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,55 @@ Feature: Validate checksums for WordPress install
Error: WordPress installation doesn't verify against checksums.
"""
And the return code should be 1

Scenario: Core checksums verify with format parameter
Given a WP install
And "WordPress" replaced with "Modified WordPress" in the wp-includes/functions.php file
And a wp-includes/test.log file:
"""
log content
"""

When I try `wp core verify-checksums --format=table`
Then STDOUT should be a table containing rows:
| file | message |
| wp-includes/functions.php | File doesn't verify against checksum |
| wp-includes/test.log | File should not exist |
And the return code should be 1

When I try `wp core verify-checksums --format=csv`
Then STDOUT should contain:
"""
file,message
wp-includes/functions.php,"File doesn't verify against checksum"
wp-includes/test.log,"File should not exist"
"""
And the return code should be 1

When I try `wp core verify-checksums --format=json`
Then STDOUT should contain:
"""
"file":"wp-includes\/functions.php","message":"File doesn't verify against checksum"
"""
And the return code should be 1

When I try `wp core verify-checksums --format=count`
Then STDOUT should be:
"""
2
"""
And the return code should be 1

When I try `wp core verify-checksums --format=json --exclude=wp-includes/test.log`
Then STDOUT should contain:
"""
"file":"wp-includes\/functions.php","message":"File doesn't verify against checksum"
"""
And the return code should be 1

When I try `wp core verify-checksums --format=json --exclude=wp-includes/functions.php,wp-includes/test.log`
Then STDOUT should be:
"""
Success: WordPress installation verifies against checksums.
"""
And the return code should be 0
63 changes: 58 additions & 5 deletions src/Checksum_Core_Command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use WP_CLI\Formatter;
use WP_CLI\Utils;
use WP_CLI\WpOrgApi;

Expand All @@ -24,6 +25,13 @@ class Checksum_Core_Command extends Checksum_Base_Command {
*/
private $exclude_files = [];

/**
* Array of detected errors.
*
* @var array
*/
private $errors = [];

/**
* Verifies WordPress files against WordPress.org's checksums.
*
Expand Down Expand Up @@ -54,6 +62,19 @@ class Checksum_Core_Command extends Checksum_Base_Command {
* [--exclude=<files>]
* : Exclude specific files from the checksum verification. Provide a comma-separated list of file paths.
*
* [--format=<format>]
* : Render output in a specific format. When provided, messages are displayed in the chosen format.
* ---
* default: plain
* options:
* - plain
* - table
* - json
* - csv
* - yaml
* - count
* ---
*
* ## EXAMPLES
*
* # Verify checksums
Expand All @@ -79,6 +100,11 @@ class Checksum_Core_Command extends Checksum_Base_Command {
* $ wp core verify-checksums --exclude="readme.html"
* Success: WordPress installation verifies against checksums.
*
* # Verify checksums with formatted output
* $ wp core verify-checksums --format=json
* [{"file":"readme.html","message":"File doesn't verify against checksum"}]
* Error: WordPress installation doesn't verify against checksums.
*
* @when before_wp_load
*/
public function __invoke( $args, $assoc_args ) {
Expand Down Expand Up @@ -137,14 +163,23 @@ public function __invoke( $args, $assoc_args ) {
}

if ( ! file_exists( ABSPATH . $file ) ) {
WP_CLI::warning( "File doesn't exist: {$file}" );
$this->errors[] = [
'file' => $file,
'message' => "File doesn't exist",
];

$has_errors = true;

continue;
}

$md5_file = md5_file( ABSPATH . $file );
if ( $md5_file !== $checksum ) {
WP_CLI::warning( "File doesn't verify against checksum: {$file}" );
if ( $checksum !== $md5_file ) {
$this->errors[] = [
'file' => $file,
'message' => "File doesn't verify against checksum",
];

$has_errors = true;
}
}
Expand All @@ -158,7 +193,25 @@ public function __invoke( $args, $assoc_args ) {
if ( in_array( $additional_file, $this->exclude_files, true ) ) {
continue;
}
WP_CLI::warning( "File should not exist: {$additional_file}" );

$this->errors[] = [
'file' => $additional_file,
'message' => 'File should not exist',
];
}
}

if ( ! empty( $this->errors ) ) {
if ( ! isset( $assoc_args['format'] ) || 'plain' === $assoc_args['format'] ) {
foreach ( $this->errors as $error ) {
WP_CLI::warning( sprintf( '%s: %s', $error['message'], $error['file'] ) );
}
} else {
$formatter = new Formatter(
$assoc_args,
array( 'file', 'message' )
);
$formatter->display_items( $this->errors );
}
}

Expand Down Expand Up @@ -234,7 +287,7 @@ private static function get_wp_details() {
private static function find_var( $var_name, $code ) {
$start = strpos( $code, '$' . $var_name . ' = ' );

if ( ! $start ) {
if ( false === $start ) {
return null;
}

Expand Down