| 1 |
<?php |
| 2 |
/** |
| 3 |
* Autoloader file for plugin. |
| 4 |
* |
| 5 |
* @package responsive-menu |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RMP\Features\Inc\Helpers; |
| 9 |
|
| 10 |
/** |
| 11 |
* Auto loader function. |
| 12 |
* |
| 13 |
* @param string $resource Source namespace. |
| 14 |
* |
| 15 |
* @return void |
| 16 |
*/ |
| 17 |
function autoloader( $resource = '' ) { |
| 18 |
$resource_path = false; |
| 19 |
$namespace_root = 'RMP\Features\\'; |
| 20 |
$resource = trim( $resource, '\\' ); |
| 21 |
|
| 22 |
if ( empty( $resource ) || strpos( $resource, '\\' ) === false || strpos( $resource, $namespace_root ) !== 0 ) { |
| 23 |
// Not our namespace, bail out. |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
// Remove our root namespace. |
| 28 |
$resource = str_replace( $namespace_root, '', $resource ); |
| 29 |
|
| 30 |
$path = explode( |
| 31 |
'\\', |
| 32 |
str_replace( '_', '-', strtolower( $resource ) ) |
| 33 |
); |
| 34 |
|
| 35 |
/** |
| 36 |
* Time to determine which type of resource path it is, |
| 37 |
* so that we can deduce the correct file path for it. |
| 38 |
*/ |
| 39 |
if ( empty( $path[0] ) || empty( $path[1] ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$directory = ''; |
| 44 |
$file_name = ''; |
| 45 |
|
| 46 |
if ( 'inc' === $path[0] ) { |
| 47 |
switch ( $path[1] ) { |
| 48 |
case 'traits': |
| 49 |
$directory = 'traits'; |
| 50 |
$file_name = sprintf( 'trait-%s', trim( strtolower( $path[2] ) ) ); |
| 51 |
break; |
| 52 |
|
| 53 |
case 'post-types': |
| 54 |
case 'taxonomies': |
| 55 |
case 'blocks': |
| 56 |
case 'meta-boxes': |
| 57 |
case 'elementor': |
| 58 |
case 'widgets': |
| 59 |
case 'plugin-configs': |
| 60 |
/** |
| 61 |
* If there is class name provided for specific directory then load that. |
| 62 |
* otherwise find in inc/ directory. |
| 63 |
*/ |
| 64 |
if ( ! empty( $path[2] ) ) { |
| 65 |
$directory = sprintf( 'classes/%s', $path[1] ); |
| 66 |
$file_name = sprintf( 'class-%s', trim( strtolower( $path[2] ) ) ); |
| 67 |
break; |
| 68 |
} |
| 69 |
// no break |
| 70 |
default: |
| 71 |
$directory = 'classes'; |
| 72 |
$file_name = sprintf( 'class-%s', trim( strtolower( $path[1] ) ) ); |
| 73 |
break; |
| 74 |
} |
| 75 |
|
| 76 |
$resource_path = sprintf( '%s/inc/%s/%s.php', untrailingslashit( RMP_PLUGIN_PATH_V4 ), $directory, $file_name ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* If $is_valid_file has 0 means valid path or 2 means the file path contains a Windows drive path. |
| 81 |
*/ |
| 82 |
$is_valid_file = validate_file( $resource_path ); |
| 83 |
|
| 84 |
if ( ! empty( $resource_path ) && file_exists( $resource_path ) && ( 0 === $is_valid_file || 2 === $is_valid_file ) ) { |
| 85 |
// We already making sure that file is exists and valid. |
| 86 |
require_once $resource_path; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
spl_autoload_register( '\RMP\Features\Inc\Helpers\autoloader' ); |
| 91 |
|