|
<?php |
|
/** |
|
* Plugin Name: MU Cookie Debugger |
|
* Description: Logs WordPress login cookie diagnostics for troubleshooting cookie issues. |
|
*/ |
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
exit; |
|
} |
|
|
|
if ( ! function_exists( 'mcd_log' ) ) { |
|
function mcd_log( string $label, array $context = [] ): void { |
|
$log_file = WP_CONTENT_DIR . '/mu-cookie-debug.log'; |
|
|
|
$line = '[' . gmdate( 'Y-m-d H:i:s' ) . ' UTC] ' . $label; |
|
|
|
if ( ! empty( $context ) ) { |
|
$line .= ' ' . wp_json_encode( |
|
$context, |
|
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
|
); |
|
} |
|
|
|
$line .= PHP_EOL; |
|
|
|
error_log( $line, 3, $log_file ); |
|
} |
|
} |
|
|
|
if ( ! function_exists( 'mcd_set_cookie_names' ) ) { |
|
function mcd_set_cookie_names(): array { |
|
$names = []; |
|
|
|
foreach ( headers_list() as $header ) { |
|
if ( stripos( $header, 'Set-Cookie:' ) === 0 ) { |
|
$cookie = trim( substr( $header, strlen( 'Set-Cookie:' ) ) ); |
|
$pair = explode( ';', $cookie, 2 )[0]; |
|
[$name] = array_pad( explode( '=', $pair, 2 ), 2, '' ); |
|
|
|
$names[] = trim( $name ); |
|
} |
|
} |
|
|
|
return array_values( array_unique( $names ) ); |
|
} |
|
} |
|
|
|
if ( ! function_exists( 'mcd_snapshot' ) ) { |
|
function mcd_snapshot( string $stage ): void { |
|
$test_cookie = defined( 'TEST_COOKIE' ) ? TEST_COOKIE : 'wordpress_test_cookie'; |
|
|
|
$headers_sent = headers_sent( $file, $line ); |
|
|
|
$context = [ |
|
'stage' => $stage, |
|
'request' => [ |
|
'method' => $_SERVER['REQUEST_METHOD'] ?? null, |
|
'host' => $_SERVER['HTTP_HOST'] ?? null, |
|
'uri' => $_SERVER['REQUEST_URI'] ?? null, |
|
'referer' => $_SERVER['HTTP_REFERER'] ?? null, |
|
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null, |
|
'https' => $_SERVER['HTTPS'] ?? null, |
|
], |
|
'wordpress' => [ |
|
'is_ssl' => function_exists( 'is_ssl' ) ? is_ssl() : null, |
|
'home_url' => function_exists( 'home_url' ) ? home_url( '/' ) : null, |
|
'site_url' => function_exists( 'site_url' ) ? site_url( '/' ) : null, |
|
], |
|
'cookies' => [ |
|
'expected_test_cookie' => $test_cookie, |
|
'has_expected_test_cookie'=> isset( $_COOKIE[ $test_cookie ] ), |
|
'cookie_names' => array_keys( $_COOKIE ), |
|
'http_cookie_header' => $_SERVER['HTTP_COOKIE'] ?? null, |
|
], |
|
'headers' => [ |
|
'headers_sent' => $headers_sent ? ( $file . ':' . $line ) : false, |
|
'set_cookie_names' => mcd_set_cookie_names(), |
|
], |
|
'constants' => [ |
|
'COOKIE_DOMAIN' => defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : null, |
|
'COOKIEPATH' => defined( 'COOKIEPATH' ) ? COOKIEPATH : null, |
|
'SITECOOKIEPATH' => defined( 'SITECOOKIEPATH' ) ? SITECOOKIEPATH : null, |
|
'ADMIN_COOKIE_PATH' => defined( 'ADMIN_COOKIE_PATH' ) ? ADMIN_COOKIE_PATH : null, |
|
'TEST_COOKIE' => defined( 'TEST_COOKIE' ) ? TEST_COOKIE : null, |
|
'LOGGED_IN_COOKIE' => defined( 'LOGGED_IN_COOKIE' ) ? LOGGED_IN_COOKIE : null, |
|
'AUTH_COOKIE' => defined( 'AUTH_COOKIE' ) ? AUTH_COOKIE : null, |
|
'SECURE_AUTH_COOKIE' => defined( 'SECURE_AUTH_COOKIE' ) ? SECURE_AUTH_COOKIE : null, |
|
], |
|
]; |
|
|
|
mcd_log( 'cookie snapshot', $context ); |
|
} |
|
} |
|
|
|
$GLOBALS['mcd_login_request'] = false; |
|
|
|
add_action( 'login_init', function () { |
|
$GLOBALS['mcd_login_request'] = true; |
|
mcd_snapshot( 'login_init' ); |
|
}, 1 ); |
|
|
|
add_action( 'wp_login_failed', function ( $username ) { |
|
$GLOBALS['mcd_login_request'] = true; |
|
mcd_snapshot( 'wp_login_failed: ' . $username ); |
|
}, 10, 1 ); |
|
|
|
add_action( 'login_footer', function () { |
|
if ( ! empty( $GLOBALS['mcd_login_request'] ) ) { |
|
mcd_snapshot( 'login_footer' ); |
|
} |
|
}, 999 ); |
|
|
|
add_action( 'shutdown', function () { |
|
if ( ! empty( $GLOBALS['mcd_login_request'] ) ) { |
|
mcd_snapshot( 'shutdown' ); |
|
} |
|
}, 999 ); |