-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwp-redirect-log.php
More file actions
174 lines (147 loc) · 4.58 KB
/
wp-redirect-log.php
File metadata and controls
174 lines (147 loc) · 4.58 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
<?php
/**
* Plugin Name: WP Redirect Log
* Description: Logs all wp_redirect and wp_safe_redirect calls to a log file in the uploads directory.
* Version: 1.0.1
* Author: ManagingWP
* Author URI: https://github.com/managingwp/wordpress-code-snippets
* Type: mu-plugin
* Status: Complete
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Hook into WP redirects
add_filter( 'wp_redirect', 'wprl_log_redirect', 10, 2 );
add_filter( 'wp_safe_redirect', 'wprl_log_redirect', 10, 2 );
// Also catch raw Location headers
add_filter( 'wp_headers', 'wprl_log_raw_header' );
/**
* Get full path to our log file in uploads/
*
* @return string
*/
function wprl_get_log_file() {
$uploads = wp_upload_dir();
$basedir = trailingslashit( $uploads['basedir'] );
$file = $basedir . 'wp-redirects.log';
if ( ! file_exists( $file ) ) {
@touch( $file );
@chmod( $file, 0664 );
}
return $file;
}
/**
* Append a line to our log file
*
* @param string $line
* @return void
*/
function wprl_write_log( $line ) {
$file = wprl_get_log_file();
file_put_contents( $file, $line . PHP_EOL, FILE_APPEND | LOCK_EX );
}
/**
* Build readable backtrace frames for logging.
*
* @param int $limit
* @return array
*/
function wprl_get_backtrace_frames( $limit = 20 ) {
$frames = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, $limit + 8 );
$summary = array();
foreach ( $frames as $frame ) {
$function = $frame['function'] ?? '';
// Skip this plugin's trace helper/log wrappers to surface the real redirect path.
if ( strpos( $function, 'wprl_' ) === 0 ) {
continue;
}
$file = isset( $frame['file'] ) ? wp_basename( $frame['file'] ) : '[internal]';
$line = isset( $frame['line'] ) ? (int) $frame['line'] : 0;
if ( ! empty( $frame['class'] ) ) {
$function = $frame['class'] . ( $frame['type'] ?? '::' ) . $function;
}
if ( $function === '' ) {
$function = '[main]';
}
$summary[] = sprintf( '%s:%d %s()', $file, $line, $function );
if ( count( $summary ) >= $limit ) {
break;
}
}
return $summary;
}
/**
* Log wp_redirect / wp_safe_redirect usage
*
* @param string $location
* @param int $status
* @return string
*/
function wprl_log_redirect( $location, $status ) {
// Keep one caller for quick scanning and add full stack for deep debugging.
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 10 );
$caller = isset( $trace[2] ) ? $trace[2] : ( $trace[1] ?? array() );
$caller_info = '';
if ( ! empty( $caller['file'] ) ) {
$caller_info = wp_basename( $caller['file'] ) . ':' . ( $caller['line'] ?? 0 );
}
$backtrace = wprl_get_backtrace_frames( 20 );
// Grab siteurl and home from options
$siteurl = get_option( 'siteurl' );
$home = get_option( 'home' );
$msg_lines = array(
'[WP-REDIRECT]',
'request: ' . ( $_SERVER['REQUEST_URI'] ?? 'unknown' ),
'location: ' . $location,
'status: ' . (int) $status,
'caller: ' . ( $caller_info ?: 'unknown' ),
'siteurl: ' . $siteurl,
'home: ' . $home,
'trace:',
);
if ( empty( $backtrace ) ) {
$msg_lines[] = ' 01. n/a';
} else {
foreach ( $backtrace as $index => $frame ) {
$msg_lines[] = sprintf( ' %02d. %s', $index + 1, $frame );
}
}
$msg_lines[] = str_repeat( '-', 80 );
$msg = implode( PHP_EOL, $msg_lines );
wprl_write_log( $msg );
return $location;
}
/**
* Log any raw Location headers before send
*
* @param array $headers
* @return array
*/
function wprl_log_raw_header( $headers ) {
if ( ! empty( $headers['Location'] ) ) {
$backtrace = wprl_get_backtrace_frames( 20 );
// Grab siteurl and home from options
$siteurl = get_option( 'siteurl' );
$home = get_option( 'home' );
$msg_lines = array(
'[WP-HEADER]',
'request: ' . ( $_SERVER['REQUEST_URI'] ?? 'unknown' ),
'location: ' . $headers['Location'],
'siteurl: ' . $siteurl,
'home: ' . $home,
'trace:',
);
if ( empty( $backtrace ) ) {
$msg_lines[] = ' 01. n/a';
} else {
foreach ( $backtrace as $index => $frame ) {
$msg_lines[] = sprintf( ' %02d. %s', $index + 1, $frame );
}
}
$msg_lines[] = str_repeat( '-', 80 );
$msg = implode( PHP_EOL, $msg_lines );
wprl_write_log( $msg );
}
return $headers;
}