Watcher.php
1.51 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
<?php
namespace SearchWP\Debug;
use SearchWP\Query;
use SearchWP\Settings;
/**
* Watcher collects SearchWP Debug data.
*
* @since 4.2.9
*/
class Watcher {
/**
* Storage for queries that happened during this request.
*
* @since 4.2.9
*
* @var Query[]
*/
private $queries = [];
/**
* Storage for the log entries recorded during this request.
*
* @since 4.2.9
*
* @var array
*/
private $logs = [];
/**
* Init.
*
* @since 4.2.9
*/
public function init() {
if ( self::is_enabled() ) {
$this->hooks();
}
}
/**
* Hooks.
*
* @since 4.2.9
*/
public function hooks() {
add_action( 'searchwp\query\ran', function( $query ) {
$this->queries[ $query->get_id() ] = $query;
} );
add_action( 'searchwp\debug\log', function( $log ) {
$this->logs[] = $log;
}, 1, 2 );
}
/**
* Check if current user can run the Watcher.
*
* @since 4.2.9
*/
public static function is_enabled() {
if ( ! apply_filters( 'searchwp\debug', Settings::get( 'debug', 'boolean' ) ) ) {
return false;
}
if ( ! apply_filters( 'searchwp\debug\watcher', true ) ) {
return false;
}
if ( ! current_user_can( Settings::get_capability() ) ) {
return false;
}
return true;
}
/**
* Getter for queries.
*
* @since 4.2.9
*
* @return Query[]
*/
public function get_queries() {
return $this->queries;
}
/**
* Getter for logs.
*
* @since 4.2.9
*
* @return array
*/
public function get_logs() {
return $this->logs;
}
}