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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
<?php declare(strict_types = 1); /** * Function call backtrace container. * * @package query-monitor */
if ( ! defined( 'ABSPATH' ) ) { exit; }
if ( ! class_exists( 'QM_Backtrace' ) ) { class QM_Backtrace {
/** * @var array<string, bool> */ protected static $ignore_class = array( 'wpdb' => true, 'hyperdb' => true, 'LudicrousDB' => true, 'QueryMonitor' => true, 'W3_Db' => true, 'Debug_Bar_PHP' => true, 'WP_Hook' => true, 'Altis\Cloud\DB' => true, 'Yoast\WP\Lib\ORM' => true, 'Perflab_SQLite_DB' => true, 'WP_SQLite_DB' => true, );
/** * @var array<string, bool> */ protected static $ignore_namespace = array();
/** * @var array<string, array<string, bool>> */ protected static $ignore_method = array();
/** * @var array<string, bool> */ protected static $ignore_func = array( 'include_once' => true, 'require_once' => true, 'include' => true, 'require' => true, 'call_user_func_array' => true, 'call_user_func' => true, 'trigger_error' => true, '_doing_it_wrong' => true, '_deprecated_argument' => true, '_deprecated_constructor' => true, '_deprecated_file' => true, '_deprecated_function' => true, '_deprecated_hook' => true, 'dbDelta' => true, 'maybe_create_table' => true, );
/** * @var array<string, int|string> */ protected static $show_args = array( 'do_action' => 1, 'apply_filters' => 1, 'do_action_ref_array' => 1, 'apply_filters_ref_array' => 1, 'do_action_deprecated' => 1, 'apply_filters_deprecated' => 1, 'get_query_template' => 1, 'resolve_block_template' => 1, 'get_template_part' => 2, 'get_extended_template_part' => 2, 'ai_get_template_part' => 2, 'load_template' => 'dir', 'dynamic_sidebar' => 1, 'get_header' => 1, 'get_sidebar' => 1, 'get_footer' => 1, 'get_transient' => 1, 'set_transient' => 1, 'class_exists' => 2, 'current_user_can' => 3, 'user_can' => 4, 'current_user_can_for_blog' => 4, 'user_can_for_site' => 5, 'current_user_can_for_site' => 4, 'author_can' => 4, );
/** * @var array<string, bool> */ protected static $ignore_hook = array();
/** * @var bool */ protected static $filtered = false;
/** * @var array<string, mixed[]> */ protected $args = array();
/** * @var mixed[] */ protected $trace;
/** * @var mixed[]|null */ protected $filtered_trace = null;
/** * @var int */ protected $calling_line = 0;
/** * @var string */ protected $calling_file = '';
/** * @var QM_Component|null */ protected $component = null;
/** * @var mixed[]|null */ protected $top_frame = null;
/** * @param array<string, mixed[]> $args * @param mixed[] $trace */ public function __construct( array $args = array(), ?array $trace = null ) { $this->trace = $trace ?? debug_backtrace( 0 );
$this->args = array_merge( array( 'ignore_class' => array(), 'ignore_namespace' => array(), 'ignore_method' => array(), 'ignore_func' => array(), 'ignore_hook' => array(), 'show_args' => array(), ), $args );
foreach ( $this->trace as & $frame ) { if ( ! isset( $frame['args'] ) ) { continue; }
if ( isset( $frame['function'], self::$show_args[ $frame['function'] ] ) ) { $show = self::$show_args[ $frame['function'] ];
if ( ! is_int( $show ) ) { $show = 1; }
$frame['args'] = array_slice( $frame['args'], 0, $show );
} else { unset( $frame['args'] ); } } }
/** * @param mixed[] $frame * @return void */ public function push_frame( array $frame ) { $this->top_frame = $frame; }
/** * @return array<int, string> */ public function get_stack() {
$trace = $this->get_filtered_trace(); $stack = array_column( $trace, 'display' );
return $stack;
}
/** * @return mixed[]|false */ public function get_caller() {
$trace = $this->get_filtered_trace();
return reset( $trace );
}
/** * @return QM_Component */ public function get_component() { if ( isset( $this->component ) ) { return $this->component; }
$components = array(); $frames = $this->get_filtered_trace();
if ( $this->top_frame ) { array_unshift( $frames, $this->top_frame ); }
foreach ( $frames as $frame ) { $component = self::get_frame_component( $frame );
if ( $component ) { if ( 'plugin' === $component->type ) { // If the component is a plugin then it can't be anything else, // so short-circuit and return early. $this->component = $component; return $this->component; }
$components[ $component->type ] = $component; } }
$file_dirs = QM_Util::get_file_dirs(); $file_dirs['dropin'] = WP_CONTENT_DIR;
foreach ( $file_dirs as $type => $dir ) { if ( isset( $components[ $type ] ) ) { $this->component = $components[ $type ]; return $this->component; } }
$component = new QM_Component(); $component->type = 'unknown'; $component->name = __( 'Unknown', 'query-monitor' ); $component->context = 'unknown';
return $component; }
/** * Attempts to determine the component responsible for a given frame. * * @param mixed[] $frame A single frame from a trace. * @phpstan-param array{ * class?: class-string, * function?: string, * file?: string, * } $frame * @return QM_Component|null An object representing the component, or null if * the component cannot be determined. */ public static function get_frame_component( array $frame ) { try {
if ( isset( $frame['class'], $frame['function'] ) ) { if ( ! class_exists( $frame['class'], false ) ) { return null; } if ( ! method_exists( $frame['class'], $frame['function'] ) ) { return null; } $ref = new ReflectionMethod( $frame['class'], $frame['function'] ); $file = $ref->getFileName(); } elseif ( isset( $frame['function'] ) && function_exists( $frame['function'] ) ) { $ref = new ReflectionFunction( $frame['function'] ); $file = $ref->getFileName(); } elseif ( isset( $frame['file'] ) ) { $file = $frame['file']; } else { return null; }
if ( ! $file ) { return null; }
return QM_Util::get_file_component( $file );
} catch ( ReflectionException $e ) { return null; } }
/** * @return mixed[] */ public function get_trace() { return $this->trace; }
/** * @deprecated Use the `::get_filtered_trace()` method instead. * * @return mixed[] */ public function get_display_trace() { return $this->get_filtered_trace(); }
/** * @return array<int, array<string, mixed>> * @phpstan-return list<array{ * file: string, * line: int, * display: string, * }> */ public function get_filtered_trace() {
if ( ! isset( $this->filtered_trace ) ) {
$trace = array_map( array( $this, 'filter_trace' ), $this->trace ); $trace = array_values( array_filter( $trace ) );
if ( empty( $trace ) && ! empty( $this->trace ) ) { $lowest = $this->trace[0]; $file = QM_Util::standard_dir( $lowest['file'], '' ); $lowest['calling_file'] = $lowest['file']; $lowest['calling_line'] = $lowest['line']; $lowest['function'] = $file; $lowest['display'] = $file; $lowest['id'] = $file; unset( $lowest['class'], $lowest['args'], $lowest['type'] );
// When a PHP error is triggered which doesn't have a stack trace, for example a // deprecated error, QM will blame itself due to its error handler. This prevents that. if ( false === strpos( $file, 'query-monitor/collectors/php_errors.php' ) ) { $trace[0] = $lowest; } }
$this->filtered_trace = $trace;
}
return $this->filtered_trace;
}
/** * @param array<int, string> $stack * @return array<int, string> */ public static function get_filtered_stack( array $stack ) { $trace = new self( array(), array() ); $return = array();
foreach ( $stack as $i => $item ) { $frame = array( 'function' => $item, );
if ( false !== strpos( $item, '->' ) ) { list( $class, $function ) = explode( '->', $item ); $frame = array( 'class' => $class, 'type' => '->', 'function' => $function, ); }
if ( false !== strpos( $item, '::' ) ) { list( $class, $function ) = explode( '::', $item ); $frame = array( 'class' => $class, 'type' => '::', 'function' => $function, ); }
$frame['args'] = array();
if ( $trace->filter_trace( $frame ) ) { $return[] = $item; } }
return $return; }
/** * @deprecated Use the `ignore_class`, `ignore_method`, `ignore_func`, and `ignore_hook` arguments instead. * * @param int $num * @return self */ public function ignore( $num ) { for ( $i = 0; $i < $num; $i++ ) { unset( $this->trace[ $i ] ); } $this->trace = array_values( $this->trace ); return $this; }
/** * @param mixed[] $frame * @return mixed[]|null */ public function filter_trace( array $frame ) {
if ( ! self::$filtered && function_exists( 'did_action' ) && did_action( 'plugins_loaded' ) ) {
/** * Filters which classes to ignore when constructing user-facing call stacks. * * @since 2.7.0 * * @param array<string, bool> $ignore_class Array of class names to ignore. The array keys are class names to ignore, * the array values are whether to ignore the class (usually true). */ self::$ignore_class = apply_filters( 'qm/trace/ignore_class', self::$ignore_class );
/** * Filters which namespaces to ignore when constructing user-facing call stacks. * * @since 3.18.1 * * @param array<string, bool> $ignore_namespace Array of namespace names to ignore. The array keys are namespace names to ignore, * the array values are whether to ignore the namespace (usually true). */ self::$ignore_namespace = apply_filters( 'qm/trace/ignore_namespace', self::$ignore_namespace );
/** * Filters which class methods to ignore when constructing user-facing call stacks. * * @since 2.7.0 * * @param array<string, array<string, bool>> $ignore_method Array of method names to ignore. The top level array keys are * class names, the second level array keys are method names, and * the array values are whether to ignore the method (usually true). */ self::$ignore_method = apply_filters( 'qm/trace/ignore_method', self::$ignore_method );
/** * Filters which functions to ignore when constructing user-facing call stacks. * * @since 2.7.0 * * @param array<string, bool> $ignore_func Array of function names to ignore. The array keys are function names to ignore, * the array values are whether to ignore the function (usually true). */ self::$ignore_func = apply_filters( 'qm/trace/ignore_func', self::$ignore_func );
/** * Filters which action and filter names to ignore when constructing user-facing call stacks. * * @since 3.8.0 * * @param array<string, bool> $ignore_hook Array of hook names to ignore. The array keys are hook names to ignore, * the array values are whether to ignore the hook (usually true). */ self::$ignore_hook = apply_filters( 'qm/trace/ignore_hook', self::$ignore_hook );
/** * Filters the number of argument values to show for the given function name when constructing user-facing * call stacks. * * @since 2.7.0 * * @param array<string,int|string> $show_args The number of argument values to show for the given function name. The * array keys are function names, the array values are either integers or * "dir" to specifically treat the function argument as a directory path. */ self::$show_args = apply_filters( 'qm/trace/show_args', self::$show_args );
self::$filtered = true;
}
$return = $frame; $ignore_class = array_filter( array_merge( self::$ignore_class, $this->args['ignore_class'] ) ); $ignore_namespace = array_filter( array_merge( self::$ignore_namespace, $this->args['ignore_namespace'] ) ); $ignore_method = array_filter( array_merge( self::$ignore_method, $this->args['ignore_method'] ) ); $ignore_func = array_filter( array_merge( self::$ignore_func, $this->args['ignore_func'] ) ); $ignore_hook = array_filter( array_merge( self::$ignore_hook, $this->args['ignore_hook'] ) ); $show_args = array_merge( self::$show_args, $this->args['show_args'] );
$hook_functions = array( 'apply_filters' => true, 'do_action' => true, 'apply_filters_ref_array' => true, 'do_action_ref_array' => true, 'apply_filters_deprecated' => true, 'do_action_deprecated' => true, );
if ( ! isset( $frame['function'] ) ) { $frame['function'] = '(unknown)'; }
if ( isset( $frame['class'] ) ) { if ( isset( $ignore_class[ $frame['class'] ] ) ) { $return = null; } elseif ( isset( $ignore_method[ $frame['class'] ][ $frame['function'] ] ) ) { $return = null; } elseif ( 0 === strpos( $frame['class'], 'QM' ) ) { $return = null; } else { // Check if the class belongs to an ignored namespace $namespace_ignored = false; foreach ( array_keys( $ignore_namespace ) as $namespace ) { if ( 0 === strpos( $frame['class'], $namespace . '\\' ) ) { $namespace_ignored = true; break; } }
if ( $namespace_ignored ) { $return = null; } else { $return['id'] = $frame['class'] . $frame['type'] . $frame['function'] . '()'; $return['display'] = QM_Util::shorten_fqn( $frame['class'] . $frame['type'] . $frame['function'] ) . '()'; } } } else { if ( isset( $ignore_func[ $frame['function'] ] ) ) { $return = null; } else { // Check if the function belongs to an ignored namespace $namespace_ignored = false; foreach ( array_keys( $ignore_namespace ) as $namespace ) { if ( 0 === strpos( $frame['function'], $namespace . '\\' ) ) { $namespace_ignored = true; break; } }
if ( $namespace_ignored ) { $return = null; } }
if ( $return && isset( $show_args[ $frame['function'] ] ) ) { $show = $show_args[ $frame['function'] ];
if ( 'dir' === $show ) { if ( isset( $frame['args'][0] ) ) { $arg = QM_Util::standard_dir( $frame['args'][0], '' ); $return['id'] = $frame['function'] . '()'; $return['display'] = QM_Util::shorten_fqn( $frame['function'] ) . "('{$arg}')"; } } else { if ( isset( $hook_functions[ $frame['function'] ], $frame['args'][0] ) && is_string( $frame['args'][0] ) && isset( $ignore_hook[ $frame['args'][0] ] ) ) { $return = null; } else { $args = array(); for ( $i = 0; $i < $show; $i++ ) { if ( isset( $frame['args'] ) && array_key_exists( $i, $frame['args'] ) ) { if ( is_string( $frame['args'][ $i ] ) ) { $args[] = '\'' . $frame['args'][ $i ] . '\''; } else { $args[] = QM_Util::display_variable( $frame['args'][ $i ] ); } } } $return['id'] = $frame['function'] . '()'; $return['display'] = QM_Util::shorten_fqn( $frame['function'] ) . '(' . implode( ',', $args ) . ')'; } } } elseif ( $return ) { $return['id'] = $frame['function'] . '()'; $return['display'] = QM_Util::shorten_fqn( $frame['function'] ) . '()'; } }
if ( $return ) {
$return['calling_file'] = $this->calling_file; $return['calling_line'] = $this->calling_line;
if ( ! isset( $return['file'] ) ) { $return['file'] = $this->calling_file; }
if ( ! isset( $return['line'] ) ) { $return['line'] = $this->calling_line; } }
if ( isset( $frame['line'] ) ) { $this->calling_line = $frame['line']; } if ( isset( $frame['file'] ) ) { $this->calling_file = $frame['file']; }
return $return;
}
} } else {
add_action( 'init', 'QueryMonitor::symlink_warning' );
}
|