/var/www/html_de/wp-content/plugins/query-monitor/classes/Dispatcher.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php declare(strict_types 1);
/**
 * Abstract dispatcher.
 *
 * @package query-monitor
 */

if ( ! class_exists'QM_Dispatcher' ) ) {
abstract class 
QM_Dispatcher {

    
/**
     * Outputter instances.
     *
     * @var array<string, QM_Output> Array of outputters.
     */
    
protected $outputters = array();

    
/**
     * Query Monitor plugin instance.
     *
     * @var QM_Plugin Plugin instance.
     */
    
protected $qm;

    
/**
     * @var string
     */
    
public $id '';

    
/**
     * @var bool
     */
    
protected $ceased false;

    public function 
__constructQM_Plugin $qm ) {
        
$this->qm $qm;

        if ( ! 
defined'QM_COOKIE' ) ) {
            
define'QM_COOKIE''wp-query_monitor_' COOKIEHASH );
        }
        if ( ! 
defined'QM_EDITOR_COOKIE' ) ) {
            
define'QM_EDITOR_COOKIE''wp-query_monitor_editor_' COOKIEHASH );
        }

        
add_action'init', array( $this'init' ) );

    }

    
/**
     * @return bool
     */
    
abstract public function is_active();

    
/**
     * @param string $message
     * @param mixed[] $e
     * @phpstan-param array{
     *   message: string,
     *   file: string,
     *   line: int,
     *   type?: int,
     *   trace?: mixed|null,
     * } $e
     */
    
public function output_fatal$message, array $e ): void {
        
print_r$e );
    }

    
/**
     * @return bool
     */
    
final public function should_dispatch() {

        
$e error_get_last();

        
# Don't dispatch if a fatal has occurred:
        
if ( ! empty( $e ) && ( $e['type'] & QM_ERROR_FATALS ) ) {
            return 
false;
        }

        
/**
         * Allows users to disable this dispatcher.
         *
         * The dynamic portion of the hook name, `$this->id`, refers to the dispatcher ID.
         *
         * Possible filter names include:
         *
         *  - `qm/dispatch/html`
         *  - `qm/dispatch/ajax`
         *  - `qm/dispatch/redirect`
         *  - `qm/dispatch/rest`
         *  - `qm/dispatch/wp_die`
         *
         * @since 2.8.0
         *
         * @param bool $true Whether or not the dispatcher is enabled.
         */
        
if ( ! apply_filters"qm/dispatch/{$this->id}"true ) ) {
            return 
false;
        }

        return 
$this->is_active();

    }

    
/**
     * @return void
     */
    
public function cease() {
        
$this->ceased true;

        
add_filter"qm/dispatch/{$this->id}"'__return_false' );
    }

    
/**
     * Processes and fetches the outputters for this dispatcher.
     *
     * @param string $outputter_id The outputter ID.
     * @return array<string, QM_Output> Array of outputters.
     */
    
public function get_outputters$outputter_id ) {
        
$collectors QM_Collectors::init();
        
$collectors->process();

        
/**
         * Allows users to filter what outputs.
         *
         * The dynamic portion of the hook name, `$outputter_id`, refers to the outputter ID.
         *
         * @since 2.8.0
         *
         * @param array<string, QM_Output> $outputters Array of outputters.
         * @param QM_Collectors            $collectors List of collectors.
         */
        
$this->outputters apply_filters"qm/outputter/{$outputter_id}", array(), $collectors );

        return 
$this->outputters;
    }

    
/**
     * @return void
     */
    
public function init() {
        if ( ! 
self::user_can_view() ) {
            
do_action'qm/cease' );
            return;
        }

        if ( ! 
defined'DONOTCACHEPAGE' ) ) {
            
define'DONOTCACHEPAGE');
        }

        
add_action'send_headers''nocache_headers' );
    }

    
/**
     * @return void
     */
    
protected function before_output() {
    }

    
/**
     * @return void
     */
    
protected function after_output() {
    }

    
/**
     * @return bool
     */
    
public static function user_can_view() {

        if ( ! 
did_action'plugins_loaded' ) ) {
            return 
false;
        }

        if ( 
current_user_can'view_query_monitor' ) ) {
            return 
true;
        }

        return 
self::user_verified();

    }

    
/**
     * @return bool
     */
    
public static function user_verified() {
        if ( isset( 
$_COOKIE[QM_COOKIE] ) ) { // phpcs:ignore
            
return self::verify_cookiewp_unslash$_COOKIE[QM_COOKIE] ) ); // phpcs:ignore
        
}
        return 
false;
    }

    
/**
     * @return string
     */
    
public static function editor_cookie() {
        if ( 
defined'QM_EDITOR_COOKIE' ) && isset( $_COOKIE[QM_EDITOR_COOKIE] ) ) { // phpcs:ignore
            
return $_COOKIE[QM_EDITOR_COOKIE]; // phpcs:ignore
        
}
        return 
'';
    }

    
/**
     * @param string $value
     * @return bool
     */
    
public static function verify_cookie$value ) {
        
$old_user_id wp_validate_auth_cookie$value'logged_in' );
        if ( 
$old_user_id ) {
            return 
user_can$old_user_id'view_query_monitor' );
        }
        return 
false;
    }

    
/**
     * Attempts to switch to the given locale.
     *
     * This is a wrapper around `switch_to_locale()` which is safe to call at any point, even
     * before the `$wp_locale_switcher` global is initialised.
     *
     * @param string $locale The locale.
     * @return bool True on success, false on failure.
     */
    
public static function switch_to_locale$locale ) {
        global 
$wp_locale_switcher;

        if ( 
$wp_locale_switcher instanceof WP_Locale_Switcher ) {
            return 
switch_to_locale$locale );
        }

        return 
false;
    }

    
/**
     * Attempts to restore the previous locale.
     *
     * This is a wrapper around `restore_previous_locale()` which is safe to call at any point, even
     * before the `$wp_locale_switcher` global is initialised.
     *
     * @return string|false Locale on success, false on error.
     */
    
public static function restore_previous_locale() {
        global 
$wp_locale_switcher;

        if ( 
$wp_locale_switcher instanceof WP_Locale_Switcher ) {
            return 
restore_previous_locale();
        }

        return 
false;
    }
}
}