Skip to content

Commit

Permalink
Admin Context: Fix PHP fatals when admin.php has CRLF line endings (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
xknown committed Nov 16, 2023
1 parent a92e34a commit e5b8f05
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions features/context.feature
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,20 @@ Feature: Context handling via --context global flag
"""
custom context was added
"""

Scenario: Core wp-admin/admin.php with CRLF lines does not fail.
Given a WP install
And a modify-wp-admin.php file:
"""
<?php
$admin_php_file = file( __DIR__ . '/wp-admin/admin.php' );
$admin_php_file = implode( "\r\n", array_map( 'trim', $admin_php_file ) );
file_put_contents( __DIR__ . '/wp-admin/admin.php', $admin_php_file );
unset( $admin_php_file );
"""

When I run `wp --require=modify-wp-admin.php --context=admin eval 'var_export( is_admin() );'`
And STDOUT should be:
"""
true
"""
2 changes: 1 addition & 1 deletion php/WP_CLI/Context/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private function load_admin_environment() {
$admin_php_file = preg_replace( '/\s+\?>$/', '', $admin_php_file );

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

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

0 comments on commit e5b8f05

Please sign in to comment.