-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathAdmin.php
More file actions
225 lines (187 loc) · 6.6 KB
/
Admin.php
File metadata and controls
225 lines (187 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace WP_CLI\Context;
use WP_CLI;
use WP_CLI\Context;
use WP_CLI\Fetchers\User;
use WP_User;
/**
* Context which simulates the administrator backend.
*/
final class Admin implements Context {
/**
* Process the context to set up the environment correctly.
*
* @param array $config Associative array of configuration data.
* @return void
*/
public function process( $config ) {
if ( defined( 'WP_ADMIN' ) ) {
// @phpstan-ignore phpstanWP.wpConstant.fetch
if ( ! WP_ADMIN ) {
WP_CLI::warning( 'Could not fake admin request.' );
}
return;
}
WP_CLI::debug( 'Faking an admin request', Context::DEBUG_GROUP );
// Define `WP_ADMIN` as being true. This causes the helper method
// `is_admin()` to return true as well.
define( 'WP_ADMIN', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
// Set a fake entry point to ensure wp-includes/vars.php does not throw
// notices/errors. This will be reflected in the global `$pagenow`
// variable. We try to use a realistic admin page based on the current
// command so that plugins which check `$pagenow` behave correctly.
$_SERVER['PHP_SELF'] = '/wp-admin/' . $this->get_fake_admin_page();
// Bootstrap the WordPress administration area.
WP_CLI::add_wp_hook(
'plugins_loaded',
function () use ( $config ) {
if ( isset( $config['user'] ) ) {
$fetcher = new User();
$user = $fetcher->get_check( $config['user'] );
$admin_user_id = $user->ID;
} else {
$admin_user_id = $this->find_admin_user_id();
}
/**
* @var int<1, max> $admin_user_id
*/
WP_CLI::debug( sprintf( 'Continuing as admin user %d', $admin_user_id ), Context::DEBUG_GROUP );
$this->log_in_as_admin_user( $admin_user_id );
},
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : -2147483648, // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
0
);
WP_CLI::add_wp_hook(
'wp_loaded',
function () {
$this->load_admin_environment();
},
defined( 'PHP_INT_MAX' ) ? PHP_INT_MAX : 2147483648, // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_maxFound
0
);
}
/**
* Find a suitable admin user ID for the current environment.
*
* On multisite, resolves a super admin via get_super_admins().
* On single site, finds a user with the administrator role.
*
* @return int<1, max> Admin user ID.
*/
private function find_admin_user_id() {
if ( is_multisite() ) {
$super_admins = get_super_admins();
foreach ( $super_admins as $super_admin_login ) {
$user = get_user_by( 'login', $super_admin_login );
if ( $user instanceof WP_User ) {
/** @var int<1, max> */
return $user->ID;
}
}
WP_CLI::error( 'No super admin user found. Specify one with --user=<login>.' );
}
$admins = get_users(
[
'role' => 'administrator',
'number' => 1,
'orderby' => 'ID',
'order' => 'ASC',
]
);
if ( ! empty( $admins ) ) {
return $admins[0]->ID;
}
WP_CLI::error( 'No administrator user found. Specify one with --user=<login>.' );
}
/**
* Get a fake admin page filename that reflects the current command.
*
* Returns 'plugins.php' for `wp plugin` commands, 'themes.php' for
* `wp theme` commands, and 'wp-cli-fake-admin-file.php' otherwise.
*
* @return string Admin page filename.
*/
private function get_fake_admin_page(): string {
$command = WP_CLI::get_runner()->arguments;
$command_map = [
'plugin' => 'plugins.php',
'theme' => 'themes.php',
];
$command_name = $command[0] ?? '';
return $command_map[ $command_name ] ?? 'wp-cli-fake-admin-file.php';
}
/**
* Ensure the current request is done under a logged-in administrator
* account.
*
* A lot of premium plugins/themes have their custom update routines locked
* behind an is_admin() call.
*
* @param int<1, max> $admin_user_id to log in as
*
* @return void
*/
private function log_in_as_admin_user( $admin_user_id ): void {
wp_set_current_user( $admin_user_id );
$expiration = time() + DAY_IN_SECONDS;
$_COOKIE[ AUTH_COOKIE ] = wp_generate_auth_cookie(
$admin_user_id,
$expiration,
'auth'
);
$_COOKIE[ SECURE_AUTH_COOKIE ] = wp_generate_auth_cookie(
$admin_user_id,
$expiration,
'secure_auth'
);
}
/**
* Load the admin environment.
*
* This tries to load `wp-admin/admin.php` while trying to avoid issues
* like re-loading the wp-config.php file (which redeclares constants).
*
* To make this work across WordPress versions, we use the actual file and
* modify it on-the-fly.
*
* @global string $hook_suffix
* @global string $pagenow
* @global int $wp_db_version
* @global array $_wp_submenu_nopriv
* @global array $menu_order
* @global array $default_menu_order
* @global array $menu
* @global array $submenu
* @global array $compat
*/
private function load_admin_environment(): void {
global $compat, $default_menu_order, $hook_suffix, $menu, $menu_order, $pagenow, $submenu, $wp_db_version, $_wp_submenu_nopriv;
if ( ! isset( $hook_suffix ) ) {
$hook_suffix = 'index'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
// Make sure we don't trigger a DB upgrade as that tries to redirect
// the page.
/**
* @var string $wp_db_version
*/
$wp_db_version = get_option( 'db_version' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$wp_db_version = (int) $wp_db_version; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
// Ensure WP does not iterate over an undefined variable in
// `user_can_access_admin_page()`.
if ( ! isset( $_wp_submenu_nopriv ) ) {
$_wp_submenu_nopriv = []; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
$admin_php_file = (string) file_get_contents( ABSPATH . 'wp-admin/admin.php' );
// First we remove the opening and closing PHP tags.
$admin_php_file = (string) preg_replace( '/^<\?php\s+/', '', $admin_php_file );
$admin_php_file = (string) preg_replace( '/\s+\?>$/', '', $admin_php_file );
// Then we remove the loading of either wp-config.php or wp-load.php.
$admin_php_file = (string) preg_replace( '/^\s*(?:include|require).*[\'"]\/?wp-(?:load|config)\.php[\'"]\s*\)?;\s*$/m', '', $admin_php_file );
// We also remove the authentication redirect.
$admin_php_file = (string) preg_replace( '/^\s*auth_redirect\(\);$/m', '', $admin_php_file );
// Finally, we avoid sending headers.
$admin_php_file = (string) preg_replace( '/^\s*nocache_headers\(\);$/m', '', $admin_php_file );
$_GET['noheader'] = true;
eval( $admin_php_file ); // phpcs:ignore Squiz.PHP.Eval.Discouraged
}
}