Skip to content

Commit 6058af0

Browse files
Copilotswissspidy
andcommitted
Add --actual flag to wp core version command for multisite db version reporting
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 877b8ce commit 6058af0

3 files changed

Lines changed: 150 additions & 2 deletions

File tree

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"core multisite-install",
4848
"core update",
4949
"core update-db",
50-
"core version"
50+
"core version",
51+
"core version-actual-db"
5152
]
5253
},
5354
"autoload": {

‎features/core-version.feature‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,73 @@ Feature: Find version for WordPress install
3737
TinyMCE version: 4.208 (4208-20151113)
3838
Package language: de_DE
3939
"""
40+
41+
# This test downgrades to an older WordPress version, but the SQLite plugin requires 6.0+
42+
@require-mysql
43+
Scenario: Verify actual database version shows correct values
44+
Given a WP install
45+
And I run `wp core download --version=5.4 --force`
46+
And I run `wp option update db_version 45805`
47+
48+
# Without --actual, should show expected version from version.php
49+
When I run `wp core version --extra`
50+
Then STDOUT should contain:
51+
"""
52+
Database revision: 47018
53+
"""
54+
55+
# With --actual, should show actual database version
56+
When I run `wp core version --extra --actual`
57+
Then STDOUT should contain:
58+
"""
59+
Database revision: 45805
60+
"""
61+
62+
# This test downgrades to an older WordPress version, but the SQLite plugin requires 6.0+
63+
@require-mysql
64+
Scenario: Verify actual database version in multisite subsite
65+
Given a WP multisite install
66+
And I run `wp core download --version=5.4 --force`
67+
And I run `wp option update db_version 47018`
68+
And I run `wp site create --slug=subsite --porcelain`
69+
And save STDOUT as {SUBSITE_ID}
70+
And I run `wp option update db_version 45805 --url=example.com/subsite`
71+
72+
# Main site shows expected version from version.php without --actual
73+
When I run `wp core version --extra`
74+
Then STDOUT should contain:
75+
"""
76+
Database revision: 47018
77+
"""
78+
79+
# Main site shows actual database version with --actual
80+
When I run `wp core version --extra --actual`
81+
Then STDOUT should contain:
82+
"""
83+
Database revision: 47018
84+
"""
85+
86+
# Subsite shows expected version from version.php without --actual
87+
When I run `wp core version --extra --url=example.com/subsite`
88+
Then STDOUT should contain:
89+
"""
90+
Database revision: 47018
91+
"""
92+
93+
# Subsite shows its own actual database version with --actual
94+
When I run `wp core version --extra --actual --url=example.com/subsite`
95+
Then STDOUT should contain:
96+
"""
97+
Database revision: 45805
98+
"""
99+
100+
Scenario: Error when using --actual without --extra
101+
Given a WP install
102+
103+
When I try `wp core version --actual`
104+
Then STDERR should contain:
105+
"""
106+
Error: The --actual flag can only be used with --extra.
107+
"""
108+
And the return code should be 1
109+

‎src/Core_Command.php‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,9 @@ private static function get_clean_basedomain() {
903903
* [--extra]
904904
* : Show extended version information.
905905
*
906+
* [--actual]
907+
* : Show the actual database version from the database. This requires WordPress to be loaded and only works with --extra flag. Useful for checking the database version of a specific site in a multisite installation using --url.
908+
*
906909
* ## EXAMPLES
907910
*
908911
* # Display the WordPress version
@@ -916,12 +919,50 @@ private static function get_clean_basedomain() {
916919
* TinyMCE version: 4.310 (4310-20160418)
917920
* Package language: en_US
918921
*
922+
* # Display actual database version for a multisite subsite
923+
* $ wp core version --extra --actual --url=example.com/subsite
924+
* WordPress version: 4.5.2
925+
* Database revision: 35700
926+
* TinyMCE version: 4.310 (4310-20160418)
927+
* Package language: en_US
928+
*
919929
* @when before_wp_load
920930
*
921931
* @param string[] $args Positional arguments. Unused.
922-
* @param array{extra?: bool} $assoc_args Associative arguments.
932+
* @param array{extra?: bool, actual?: bool} $assoc_args Associative arguments.
923933
*/
924934
public function version( $args = [], $assoc_args = [] ) {
935+
$use_actual_db_version = Utils\get_flag_value( $assoc_args, 'actual' );
936+
937+
// If --actual flag is used, delegate to a command that runs after WP load
938+
if ( $use_actual_db_version ) {
939+
if ( ! Utils\get_flag_value( $assoc_args, 'extra' ) ) {
940+
WP_CLI::error( 'The --actual flag can only be used with --extra.' );
941+
}
942+
943+
// Build the command to run after WP load
944+
$cmd_args = [];
945+
foreach ( $assoc_args as $key => $value ) {
946+
if ( 'actual' !== $key ) {
947+
if ( true === $value ) {
948+
$cmd_args[] = "--{$key}";
949+
} elseif ( is_string( $value ) ) {
950+
$cmd_args[] = "--{$key}=" . escapeshellarg( $value );
951+
}
952+
}
953+
}
954+
$cmd = 'core version-actual-db ' . implode( ' ', $cmd_args );
955+
956+
WP_CLI::runcommand(
957+
$cmd,
958+
[
959+
'launch' => true,
960+
'exit_error' => true,
961+
]
962+
);
963+
return;
964+
}
965+
925966
$details = self::get_wp_details();
926967

927968
if ( ! Utils\get_flag_value( $assoc_args, 'extra' ) ) {
@@ -948,6 +989,42 @@ public function version( $args = [], $assoc_args = [] ) {
948989
);
949990
}
950991

992+
/**
993+
* Helper command to display actual database version (runs after WP load).
994+
*
995+
* This is an internal command called by 'core version --actual'.
996+
*
997+
* @subcommand version-actual-db
998+
* @when after_wp_load
999+
*
1000+
* @param string[] $args Positional arguments. Unused.
1001+
* @param array{extra?: bool} $assoc_args Associative arguments.
1002+
*/
1003+
public function version_actual_db( $args = [], $assoc_args = [] ) {
1004+
$details = self::get_wp_details();
1005+
1006+
// Get the actual database version from the options table
1007+
$actual_db_version = get_option( 'db_version' );
1008+
1009+
$match = [];
1010+
$found_version = preg_match( '/(\d)(\d+)-/', $details['tinymce_version'], $match );
1011+
$human_readable_tiny_mce = $found_version ? "{$match[1]}.{$match[2]}" : '';
1012+
1013+
echo Utils\mustache_render(
1014+
self::get_template_path( 'versions.mustache' ),
1015+
[
1016+
'wp-version' => $details['wp_version'],
1017+
'db-version' => $actual_db_version,
1018+
'local-package' => empty( $details['wp_local_package'] )
1019+
? 'en_US'
1020+
: $details['wp_local_package'],
1021+
'mce-version' => $human_readable_tiny_mce
1022+
? "{$human_readable_tiny_mce} ({$details['tinymce_version']})"
1023+
: $details['tinymce_version'],
1024+
]
1025+
);
1026+
}
1027+
9511028
/**
9521029
* Gets version information from `wp-includes/version.php`.
9531030
*

0 commit comments

Comments
 (0)