Skip to content

Commit e5b8f05

Browse files
authored
Admin Context: Fix PHP fatals when admin.php has CRLF line endings (#5875)
If `wp-admin/admin.php` has CRLF line endings, running `wp --context=admin` with any command results in a PHP fatal error because the regular expression to strip the wp-load.php require statement
1 parent a92e34a commit e5b8f05

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

features/context.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,20 @@ Feature: Context handling via --context global flag
176176
"""
177177
custom context was added
178178
"""
179+
180+
Scenario: Core wp-admin/admin.php with CRLF lines does not fail.
181+
Given a WP install
182+
And a modify-wp-admin.php file:
183+
"""
184+
<?php
185+
$admin_php_file = file( __DIR__ . '/wp-admin/admin.php' );
186+
$admin_php_file = implode( "\r\n", array_map( 'trim', $admin_php_file ) );
187+
file_put_contents( __DIR__ . '/wp-admin/admin.php', $admin_php_file );
188+
unset( $admin_php_file );
189+
"""
190+
191+
When I run `wp --require=modify-wp-admin.php --context=admin eval 'var_export( is_admin() );'`
192+
And STDOUT should be:
193+
"""
194+
true
195+
"""

php/WP_CLI/Context/Admin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private function load_admin_environment() {
114114
$admin_php_file = preg_replace( '/\s+\?>$/', '', $admin_php_file );
115115

116116
// Then we remove the loading of either wp-config.php or wp-load.php.
117-
$admin_php_file = preg_replace( '/^\s*(?:include|require).*[\'"]\/?wp-(?:load|config)\.php[\'"]\s*\)?;$/m', '', $admin_php_file );
117+
$admin_php_file = preg_replace( '/^\s*(?:include|require).*[\'"]\/?wp-(?:load|config)\.php[\'"]\s*\)?;\s*$/m', '', $admin_php_file );
118118

119119
// We also remove the authentication redirect.
120120
$admin_php_file = preg_replace( '/^\s*auth_redirect\(\);$/m', '', $admin_php_file );

0 commit comments

Comments
 (0)