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
<?php
function papi_get_all_files_in_directory( $directory = '' ) {
$result = [];
if ( empty( $directory ) || ! is_string( $directory ) ) {
return $result;
}
if ( file_exists( $directory ) && $handle = opendir( $directory ) ) {
while ( false !== ( $file = readdir( $handle ) ) ) {
if ( ! in_array( $file, ['..', '.'] ) && $file[0] !== '.' ) {
if ( is_dir( $directory . '/' . $file ) ) {
$result = array_merge( $result, papi_get_all_files_in_directory( $directory . '/' . $file ) );
} else {
$file = $directory . '/' . $file;
$result[] = preg_replace( '/\/\//si', '/', $file );
}
}
}
closedir( $handle );
}
return $result;
}
function papi_get_core_type_file_path( $file_path ) {
if ( empty( $file_path ) ) {
return [];
}
$directories = papi_filter_settings_directories();
$result = [];
foreach ( $directories as $directory ) {
$directory = rtrim( $directory, '/' ) . '/';
$file_path = str_replace( $directory, '', $file_path );
$path = $directory . $file_path;
if ( file_exists( $path ) ) {
$result[] = $path;
}
}
return array_pop( $result );
}
function papi_get_all_core_type_files() {
return papi()->once( __FUNCTION__, function() {
$directories = papi_filter_settings_directories();
$result = [];
foreach ( $directories as $directory ) {
$result = array_merge( $result, papi_get_all_files_in_directory( $directory ) );
}
$result = array_map( 'papi_get_core_type_file_path', $result );
return array_unique( $result );
} );
}
function papi_get_file_path( $file ) {
if ( empty( $file ) || ! is_string( $file ) ) {
return;
}
$directories = papi_filter_settings_directories();
$file = '/' . str_replace( ' ', '-', str_replace( '_', '-', $file ) );
foreach ( $directories as $directory ) {
if ( file_exists( $directory . $file ) ) {
return $directory . $file;
}
if ( file_exists( $directory . $file . '.php' ) ) {
return $directory . $file . '.php';
}
}
}
function papi_get_core_type_base_path( $file ) {
if ( empty( $file ) || ! is_string( $file ) ) {
return;
}
$directories = papi_filter_settings_directories();
foreach ( $directories as $directory ) {
if ( strpos( $file, $directory ) !== false ) {
$file = str_replace( $directory . '/', '', $file );
}
}
$file = explode( '.', $file );
return $file[0];
}