Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin Context: Fix PHP fatals when admin.php has CRLF line endings #5875

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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