/var/www/html_nl/wp-content/plugins/query-monitor/classes/Collector_Assets.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
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
<?php declare(strict_types 1);
/**
 * Enqueued scripts and styles collector.
 *
 * @package query-monitor
 */

if ( ! defined'ABSPATH' ) ) {
    exit;
}

/**
 * @extends QM_DataCollector<QM_Data_Assets>
 */
abstract class QM_Collector_Assets extends QM_DataCollector {

    public function 
get_storage(): QM_Data {
        return new 
QM_Data_Assets();
    }

    
/**
     * @return void
     */
    
public function set_up() {
        
parent::set_up();
        
add_action'admin_print_footer_scripts', array( $this'action_print_footer_scripts' ), 9999 );
        
add_action'wp_print_footer_scripts', array( $this'action_print_footer_scripts' ), 9999 );
        
add_action'admin_head', array( $this'action_head' ), 9999 );
        
add_action'wp_head', array( $this'action_head' ), 9999 );
        
add_action'login_head', array( $this'action_head' ), 9999 );
        
add_action'embed_head', array( $this'action_head' ), 9999 );
    }

    
/**
     * @return void
     */
    
public function tear_down() {
        
remove_action'admin_print_footer_scripts', array( $this'action_print_footer_scripts' ), 9999 );
        
remove_action'wp_print_footer_scripts', array( $this'action_print_footer_scripts' ), 9999 );
        
remove_action'admin_head', array( $this'action_head' ), 9999 );
        
remove_action'wp_head', array( $this'action_head' ), 9999 );
        
remove_action'login_head', array( $this'action_head' ), 9999 );
        
remove_action'embed_head', array( $this'action_head' ), 9999 );

        
parent::tear_down();
    }

    
/**
     * @return string
     */
    
abstract public function get_dependency_type();

    
/**
     * @return void
     */
    
public function action_head() {
        
$type $this->get_dependency_type();

        
/** @var WP_Dependencies $dependencies */
        
$dependencies $GLOBALS"wp_{$type}];

        
$this->data->header $dependencies->done;
    }

    
/**
     * @return void
     */
    
public function action_print_footer_scripts() {
        if ( empty( 
$this->data->header ) ) {
            return;
        }

        
$type $this->get_dependency_type();

        
/** @var WP_Dependencies $dependencies */
        
$dependencies $GLOBALS"wp_{$type}];

        
$this->data->footer array_diff$dependencies->done$this->data->header );
    }

    
/**
     * @return void
     */
    
public function process() {
        
$type $this->get_dependency_type();
        
$modules null;

        if ( 
$type === 'scripts' ) {
            
$modules self::get_script_modules();
        }

        if ( empty( 
$this->data->header ) && empty( $this->data->footer ) && empty( $modules ) ) {
            return;
        }

        
$this->data->is_ssl is_ssl();
        
$this->data->full_host self::get_host();
        
$this->data->host = (string) parse_url$this->data->full_hostPHP_URL_HOST );
        
$this->data->default_version get_bloginfo'version' );
        
$this->data->port = (string) parse_url$this->data->full_hostPHP_URL_PORT );

        
$positions = array(
            
'missing',
            
'broken',
            
'header',
            
'footer',
        );

        
$this->data->counts = array(
            
'missing' => 0,
            
'broken' => 0,
            
'header' => 0,
            
'footer' => 0,
            
'total' => 0,
        );

        foreach ( array( 
'header''footer' ) as $position ) {
            if ( empty( 
$this->data->{$position} ) ) {
                
$this->data->{$position} = array();
            }
        }

        
/** @var WP_Dependencies $raw */
        
$raw $GLOBALS"wp_{$type}];
        
$broken array_valuesarray_diff$raw->queue$raw->done ) );
        
$missing array_valuesarray_diff$raw->queuearray_keys$raw->registered ) ) );

        
// A broken asset is one which has been deregistered without also being dequeued
        
if ( ! empty( $broken ) ) {
            foreach ( 
$broken as $key => $handle ) {
                
/** @var _WP_Dependency|false $item */
                
$item $raw->query$handle );
                if ( 
$item ) {
                    
$broken array_merge$brokenself::get_broken_dependencies$item$raw ) );
                } else {
                    unset( 
$broken$key ] );
                    
$missing[] = $handle;
                }
            }

            if ( ! empty( 
$broken ) ) {
                
$this->data->broken array_unique$broken );
            }
        }

        
// A missing asset is one which has been enqueued with dependencies that don't exist
        
if ( ! empty( $missing ) ) {
            
$this->data->missing array_unique$missing );
            foreach ( 
$this->data->missing as $handle ) {
                
$raw->add$handlefalse );
                
$key array_search$handle$raw->donetrue );
                if ( 
false !== $key ) {
                    unset( 
$raw->done$key ] );
                }
            }
        }

        
$all_dependencies = array();
        
$all_dependents = array();

        
$missing_dependencies = array();

        foreach ( 
$positions as $position ) {
            if ( empty( 
$this->data->{$position} ) ) {
                continue;
            }

            
/** @var string $handle */
            
foreach ( $this->data->{$position} as $handle ) {
                
/** @var _WP_Dependency|false $dependency */
                
$dependency $raw->query$handle );

                if ( ! 
$dependency ) {
                    continue;
                }

                
$all_dependencies array_merge$all_dependencies$dependency->deps );
                
$dependents $this->get_dependents$dependency$raw );
                
$all_dependents array_merge$all_dependents$dependents );

                list( 
$host$source$local$port ) = $this->get_dependency_data$dependency );

                if ( empty( 
$dependency->ver ) ) {
                    
$ver '';
                } else {
                    
$ver $dependency->ver;
                }

                
$warning = ! in_array$handle$raw->donetrue );

                if ( 
$source instanceof WP_Error ) {
                    
$display $source->get_error_message();
                } else {
                    
$display ltrimpreg_replace'#https?://' preg_quote$this->data->full_host'#' ) . '#'''remove_query_arg'ver'$source ) ), '/' );
                }

                
$dependencies $dependency->deps;

                foreach ( 
$dependencies as $dep ) {
                    if ( ! 
$raw->query$dep ) ) {
                        
// A missing dependency is a dependency on an asset that doesn't exist
                        
$missing_dependencies$dep ] = true;
                    }
                }

                
$this->data->assets$position ][ $handle ] = array(
                    
'host' => $host,
                    
'port' => $port,
                    
'source' => $source,
                    
'local' => $local,
                    
'ver' => $ver,
                    
'warning' => $warning,
                    
'display' => $display,
                    
'dependents' => $dependents,
                    
'dependencies' => $dependencies,
                );

                
$this->data->counts$position ]++;
                
$this->data->counts['total']++;
            }
        }

        unset( 
$this->data->{$position} );

        if ( 
is_array$modules ) ) {
            foreach ( 
$modules as $id => $module ) {
                list( 
$host$source$local$port ) = $this->get_module_data$module['src'] );

                
$display ltrimpreg_replace'#https?://' preg_quote$this->data->full_host'#' ) . '#'''remove_query_arg'ver'$source ) ), '/' );

                
$this->data->assets['modules'][ $id ] = array(
                    
'host' => $host,
                    
'port' => $port,
                    
'source' => $source,
                    
'local' => $local,
                    
'ver' => $module['version'] ?: '',
                    
'warning' => false,
                    
'display' => $display,
                    
'dependents' => $module['dependents'],
                    
'dependencies' => $module['dependencies'],
                );

                
$all_dependencies array_merge$all_dependencies$module['dependencies'] );
                
$all_dependents array_merge$all_dependents$module['dependents'] );
            }
        }

        
$all_dependencies array_unique$all_dependencies );
        
sort$all_dependencies );
        
$this->data->dependencies $all_dependencies;

        
$all_dependents array_unique$all_dependents );
        
sort$all_dependents );
        
$this->data->dependents $all_dependents;

        
$this->data->missing_dependencies $missing_dependencies;
    }

    
/**
     * Undocumented function
     *
     * @return array<string, array>|null
     * @phpstan-return array<string, array{
     *   id: string,
     *   src: string,
     *   version: string|false|null,
     *   dependencies: list<string>,
     *   dependents: list<string>,
     * }>|null
     */
    
protected static function get_script_modules(): ?array {
        
// WP 6.5
        
if ( ! function_exists'wp_script_modules' ) ) {
            return 
null;
        }

        
$modules wp_script_modules();

        if ( ! ( 
$modules instanceof \WP_Script_Modules ) ) {
            return 
null;
        }

        
$reflector = new ReflectionClass$modules );

        
$get_marked_for_enqueue $reflector->getMethod'get_marked_for_enqueue' );
        
$get_marked_for_enqueue->setAccessibletrue );

        
$get_dependencies $reflector->getMethod'get_dependencies' );
        
$get_dependencies->setAccessibletrue );

        
$get_src $reflector->getMethod'get_src' );
        
$get_src->setAccessibletrue );

        
/**
         * @var array<string, array<string, mixed>> $enqueued
         * @phpstan-var array<string, array{
         *   src: string,
         *   version: string|false|null,
         *   enqueue: bool,
         *   dependencies: list<array{
         *     id: string,
         *     import: 'static'|'dynamic',
         *   }>,
         * }> $enqueued
         */
        
$enqueued $get_marked_for_enqueue->invoke$modules );

        
/**
         * @var array<string, array<string, mixed>> $deps
         * @phpstan-var array<string, array{
         *   src: string,
         *   version: string|false|null,
         *   enqueue: bool,
         *   dependencies: list<array{
         *     id: string,
         *     import: 'static'|'dynamic',
         *   }>,
         * }> $deps
         */
        
$deps $get_dependencies->invoke$modulesarray_keys$enqueued ) );

        
$all_modules array_merge(
            
$enqueued,
            
$deps
        
);

        
/**
         * @var array<string, array<string, mixed>> $sources
         * @phpstan-var array<string, array{
         *   id: string,
         *   src: string,
         *   version: string|false|null,
         *   dependencies: list<string>,
         *   dependents: list<string>,
         * }> $sources
         */
        
$sources = array();

        foreach ( 
$all_modules as $id => $module ) {
            
/** @var string $src */
            
$src $get_src->invoke$modules$id );

            
/**
             * @var array<string, array<string, mixed>> $script_dependencies
             */
            
$script_dependencies $get_dependencies->invoke$modules, array( $id ) );
            
$dependencies array_keys$script_dependencies );
            
$dependents = array();

            foreach ( 
$all_modules as $dep_id => $dep ) {
                foreach ( 
$dep['dependencies'] as $dependency ) {
                    if ( 
$dependency['id'] === $id ) {
                        
$dependents[] = $dep_id;
                    }
                }
            }

            
$sources$id ] = array(
                
'id' => $id,
                
'src' => $src,
                
'version' => $module['version'],
                
'dependencies' => $dependencies,
                
'dependents' => $dependents,
            );
        }

        
// @todo check isPrivate before changing visibility back
        
$get_marked_for_enqueue->setAccessiblefalse );
        
$get_dependencies->setAccessiblefalse );
        
$get_src->setAccessiblefalse );

        return 
$sources;
    }

    
/**
     * @param _WP_Dependency $item
     * @param WP_Dependencies $dependencies
     * @return array<int, string>
     */
    
protected static function get_broken_dependencies_WP_Dependency $itemWP_Dependencies $dependencies ) {
        
$broken = array();

        foreach ( 
$item->deps as $handle ) {
            
$dep $dependencies->query$handle );
            if ( 
$dep instanceof _WP_Dependency ) {
                
$broken array_merge$brokenself::get_broken_dependencies$dep$dependencies ) );
            } else {
                
$broken[] = $item->handle;
            }
        }

        return 
$broken;
    }

    
/**
     * @param _WP_Dependency $dependency
     * @param WP_Dependencies $dependencies
     * @return array<int, string>
     */
    
public function get_dependents_WP_Dependency $dependencyWP_Dependencies $dependencies ) {
        
$dependents = array();
        
$handles array_uniquearray_merge$dependencies->queue$dependencies->done ) );

        foreach ( 
$handles as $handle ) {
            
$item $dependencies->query$handle );
            if ( 
$item instanceof _WP_Dependency ) {
                if ( 
in_array$dependency->handle$item->depstrue ) ) {
                    
$dependents[] = $handle;
                }
            }
        }

        
sort$dependents );

        return 
$dependents;
    }

    
/**
     * @param _WP_Dependency $dependency
     * @return mixed[]
     * @phpstan-return array{
     *   0: string,
     *   1: string|WP_Error,
     *   2: bool,
     *   3: string,
     * }
     */
    
public function get_dependency_data_WP_Dependency $dependency ) {
        
/** @var QM_Data_Assets */
        
$data $this->get_data();
        
$loader rtrim$this->get_dependency_type(), 's' );
        
$src $dependency->src;
        
$host '';
        
$full_host '';
        
$scheme '';
        
$port '';

        if ( 
null === $dependency->ver ) {
            
$ver '';
        } else {
            
$ver $dependency->ver ?: $this->data->default_version;
        }

        if ( ! empty( 
$src ) && ! empty( $ver ) ) {
            
$src add_query_arg'ver'$ver$src );
        }

        
/** This filter is documented in wp-includes/class.wp-scripts.php */
        
$source apply_filters"{$loader}_loader_src"$src$dependency->handle );

        if ( 
is_string$source ) ) {
            
$host = (string) parse_url$sourcePHP_URL_HOST );
            
$scheme = (string) parse_url$sourcePHP_URL_SCHEME );
            
$port = (string) parse_url$sourcePHP_URL_PORT );
            
$full_host $host;

            if ( ! empty( 
$port ) ) {
                
$full_host .= ':' $port;
            }
        }

        if ( empty( 
$host ) ) {
            
$full_host $data->full_host;
            
$host $data->host;
            
$port $data->port;
        }

        if ( 
$scheme && $data->is_ssl && ( 'https' !== $scheme ) && ( 'localhost' !== $host ) ) {
            
$source = new WP_Error'qm_insecure_content'__'Insecure content''query-monitor' ), array(
                
'src' => $source,
            ) );
        }

        if ( 
$source instanceof WP_Error ) {
            
$error_data $source->get_error_data();
            if ( 
$error_data && isset( $error_data['src'] ) ) {
                
$host = (string) parse_url$error_data['src'], PHP_URL_HOST );
            }
        } elseif ( empty( 
$source ) ) {
            
$source '';
            
$host '';
        }

        
$local = ( $data->full_host === $full_host );

        return array( 
$host$source$local$port );
    }

    
/**
     * @param string $src
     * @return mixed[]
     * @phpstan-return array{
     *   0: string,
     *   1: string,
     *   2: bool,
     *   3: string,
     * }
     */
    
protected function get_module_datastring $src ): array {
        
/** @var QM_Data_Assets */
        
$data $this->get_data();

        
$host = (string) parse_url$srcPHP_URL_HOST );
        
$port = (string) parse_url$srcPHP_URL_PORT );
        
$full_host $host;

        if ( ! empty( 
$port ) ) {
            
$full_host .= ':' $port;
        }

        if ( empty( 
$host ) ) {
            
$full_host $data->full_host;
            
$host $data->host;
            
$port $data->port;
        }

        
$source $src;
        
$local = ( $data->full_host === $full_host );

        return array( 
$host$source$local$port );
    }
}