-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoload.php
46 lines (45 loc) · 1.55 KB
/
autoload.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
<?php
if ( !defined( 'ABSPATH' ) ) {
header( 'HTTP/1.0 403 Forbidden' );
exit;
}
if (
is_array( spl_autoload_functions() )
&& in_array( '__autoload', spl_autoload_functions() )
) {
spl_autoload_register( '__autoload' );
}
if ( !function_exists( 'wpss_autoload' ) ) {
function wpss_autoload( $dir ): void {
if ( !file_exists( "$dir/composer.json" ) ) {
return;
}
if ( file_exists( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
}
if ( file_exists( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
}
$content = new WP_Filesystem_Direct( false );
$composer = $content->get_contents( WPSS_URCM_PLUGIN_PATH . "composer.json" );
$composer = json_decode( $composer, true );
$namespaces = $composer['autoload']['psr-4'] ?? [];
foreach ( $namespaces as $namespace => $classpaths ) {
if ( !is_array( $classpaths ) ) {
$classpaths = [ $classpaths ];
}
spl_autoload_register( function( $classname ) use ( $namespace, $classpaths, $dir ) {
if ( preg_match( "#^" . preg_quote( $namespace ) . "#", $classname ) ) {
$classname = str_replace( $namespace, "", $classname );
$filename = preg_replace( "#\\\\#", "/", $classname ) . ".php";
foreach ( $classpaths as $classpath ) {
$full_path = $dir . "/" . $classpath . "/$filename";
if ( file_exists( $full_path ) ) {
include_once $full_path;
}
}
}
} );
}
}
}