-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
94 lines (78 loc) · 1.79 KB
/
functions.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
<?php
namespace Helsinki\WordPress\Site\Core;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Autoloading
*/
function class_loader( $class ) {
if ( false === stripos( $class, __NAMESPACE__ ) ) {
return;
}
$parts = array_filter(
explode(
DIRECTORY_SEPARATOR,
str_replace(
[__NAMESPACE__, '\\'],
['', DIRECTORY_SEPARATOR],
$class
)
)
);
$class = array_pop( $parts );
$class = 'class-' . str_replace( '_', '-', strtolower( $class ) );
$file = path_to_php_file(array_merge(
array_map('strtolower', $parts),
[$class]
));
if ( file_exists( $file ) ) {
require_once $file;
}
}
/**
* Execution
*/
function load_includes() : void {
load_php_files( 'includes', load_config('includes') );
}
function load_features() : void {
load_php_files( 'features', load_config('features') );
}
function load_integrations() : void {
load_php_files( 'integrations', load_config('integrations') );
}
/**
* Files
*/
function path_to_file( string $name ) : string {
return plugin_path() . trim( $name );
}
function path_to_php_file( $name ) : string {
return is_array( $name )
? path_to_file( implode( DIRECTORY_SEPARATOR, $name ) . '.php' )
: path_to_file( $name . '.php' );
}
function load_php_files( string $path, array $files ) : void {
foreach ( $files as $dir => $file ) {
$parts = array( $path );
if ( is_string( $dir ) && $dir ) {
$parts[] = $dir;
}
if ( is_array( $file ) ) {
load_php_files( implode( DIRECTORY_SEPARATOR , $parts ), $file );
} else {
$parts[] = $file;
require_once path_to_php_file( implode( DIRECTORY_SEPARATOR , $parts ) );
}
}
}
function load_config( string $name ) : array {
$file = path_to_php_file(['config', $name]);
if ( file_exists( $file ) ) {
$config = include $file;
return $config;
} else {
return [];
}
}