-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoloader.php
72 lines (54 loc) · 1.67 KB
/
autoloader.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
/**
* To Register autoloader
*
* @package wp-web-vitals
*/
spl_autoload_register( function ( $resource = '' ) {
$resource_path = false;
$namespace_root = 'WP_Web_Vitals\\';
$resource = trim( $resource, '\\' );
if ( empty( $resource ) || strpos( $resource, '\\' ) === false || strpos( $resource, $namespace_root ) !== 0 ) {
// Not our namespace, bail out.
return;
}
$theme_root = __DIR__;
$path = explode(
'\\',
str_replace( '_', '-', strtolower( $resource ) )
);
/**
* Time to determine which type of resource path it is,
* so that we can deduce the correct file path for it.
*/
if ( empty( $path[1] ) || empty( $path[2] ) ) {
return;
}
$directory = '';
$file_name = '';
if ( 'inc' === $path[1] ) {
switch ( $path[2] ) {
case 'traits':
$directory = 'traits';
$file_name = sprintf( 'trait-%s', trim( strtolower( $path[3] ) ) );
break;
case 'widgets':
if ( ! empty( $path[3] ) ) {
$directory = 'classes/widgets';
$file_name = sprintf( 'class-%s', trim( strtolower( $path[3] ) ) );
break;
}
default:
$directory = 'classes';
$file_name = sprintf( 'class-%s', trim( strtolower( $path[2] ) ) );
break;
}
$resource_path = sprintf( '%s/inc/%s/%s.php', untrailingslashit( $theme_root ), $directory, $file_name );
}
$validate_file = validate_file( $resource_path );
// Function validate_file returns 2 for Windows drive path, so we check that as well.
if ( ! empty( $resource_path ) && file_exists( $resource_path ) && ( 0 === $validate_file || 2 === $validate_file ) ) {
// We are already making sure that file exists and it's valid.
require_once( $resource_path ); // phpcs:ignore
}
} );