forked from johnbillion/query-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirects.php
72 lines (57 loc) · 1.33 KB
/
redirects.php
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
<?php declare(strict_types = 1);
/**
* HTTP redirect collector.
*
* @package query-monitor
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @extends QM_DataCollector<QM_Data_Redirect>
*/
class QM_Collector_Redirects extends QM_DataCollector {
public $id = 'redirects';
public function get_storage(): QM_Data {
return new QM_Data_Redirect();
}
/**
* @return void
*/
public function set_up() {
parent::set_up();
add_filter( 'wp_redirect', array( $this, 'filter_wp_redirect' ), 9999, 2 );
}
/**
* @return void
*/
public function tear_down() {
remove_filter( 'wp_redirect', array( $this, 'filter_wp_redirect' ), 9999 );
parent::tear_down();
}
/**
* @param string $location
* @param int $status
* @return string
*/
public function filter_wp_redirect( $location, $status ) {
if ( ! $location ) {
return $location;
}
$trace = new QM_Backtrace( array(
'ignore_hook' => array(
current_filter() => true,
),
'ignore_func' => array(
'wp_redirect' => true,
'wp_safe_redirect' => true,
),
) );
$this->data->trace = $trace;
$this->data->location = $location;
$this->data->status = $status;
return $location;
}
}
# Load early in case a plugin is doing a redirect when it initialises instead of after the `plugins_loaded` hook
QM_Collectors::add( new QM_Collector_Redirects() );