Skip to content

Commit 34df024

Browse files
committed
Fix admin_init/init executed in reverse order and wrong user logged in
Fix wp-cli#6010
1 parent e214df2 commit 34df024

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

php/WP_CLI/Context/Admin.php

+24-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use WP_CLI;
66
use WP_CLI\Context;
7+
use WP_CLI\Fetchers\User;
78
use WP_Session_Tokens;
89

910
/**
@@ -39,12 +40,29 @@ public function process( $config ) {
3940

4041
// Bootstrap the WordPress administration area.
4142
WP_CLI::add_wp_hook(
42-
'init',
43+
'plugins_loaded',
44+
function () use ( $config ) {
45+
if ( isset( $config['user'] ) ) {
46+
$fetcher = new User();
47+
$user = $fetcher->get_check( $config['user'] );
48+
$admin_user_id = $user->ID;
49+
} else {
50+
// TODO: Add logic to find an administrator user.
51+
$admin_user_id = 1;
52+
}
53+
54+
$this->log_in_as_admin_user( $admin_user_id );
55+
},
56+
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : -2147483648, // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
57+
0
58+
);
59+
60+
WP_CLI::add_wp_hook(
61+
'wp_loaded',
4362
function () {
44-
$this->log_in_as_admin_user();
4563
$this->load_admin_environment();
4664
},
47-
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : -2147483648, // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
65+
defined( 'PHP_INT_MAX' ) ? PHP_INT_MAX : 2147483648, // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_maxFound
4866
0
4967
);
5068
}
@@ -56,12 +74,11 @@ function () {
5674
* A lot of premium plugins/themes have their custom update routines locked
5775
* behind an is_admin() call.
5876
*
77+
* @param int<1, max> $admin_user_id to log in as
78+
*
5979
* @return void
6080
*/
61-
private function log_in_as_admin_user() {
62-
// TODO: Add logic to find an administrator user.
63-
$admin_user_id = 1;
64-
81+
private function log_in_as_admin_user( $admin_user_id ) {
6582
wp_set_current_user( $admin_user_id );
6683

6784
$expiration = time() + DAY_IN_SECONDS;

0 commit comments

Comments
 (0)