| 1 |
<?php |
| 2 |
/** |
| 3 |
* Autoloader class. |
| 4 |
* |
| 5 |
* @package WebberZone\Top_Ten |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WebberZone\Top_Ten; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Autoloader for WebberZone\Snippetz classes. |
| 14 |
* |
| 15 |
* @param string $class_name The name of the class to load. |
| 16 |
*/ |
| 17 |
function autoload( $class_name ) { |
| 18 |
$namespace = __NAMESPACE__; |
| 19 |
$classes_subfolder = 'includes'; |
| 20 |
|
| 21 |
if ( false !== strpos( $class_name, $namespace ) ) { |
| 22 |
$classes_dir = realpath( TOP_TEN_PLUGIN_DIR ) . DIRECTORY_SEPARATOR . $classes_subfolder . DIRECTORY_SEPARATOR; |
| 23 |
|
| 24 |
// Project namespace. |
| 25 |
$project_namespace = $namespace . '\\'; |
| 26 |
$length = strlen( $project_namespace ); |
| 27 |
|
| 28 |
$class_file = substr( $class_name, $length ); // Remove top level namespace (that is the current dir). |
| 29 |
$class_file = str_replace( '_', '-', strtolower( $class_file ) ); // Swap underscores for dashes and lowercase. |
| 30 |
|
| 31 |
// Prepend `class-` to the filename (last class part). |
| 32 |
$class_parts = explode( '\\', $class_file ); // Split the class name into parts. |
| 33 |
$last_index = count( $class_parts ) - 1; // Get the last index. |
| 34 |
$class_parts[ $last_index ] = 'class-' . $class_parts[ $last_index ]; // Replace the last part with `class-`. |
| 35 |
|
| 36 |
// Join everything back together and add the file extension. |
| 37 |
$class_file = implode( DIRECTORY_SEPARATOR, $class_parts ) . '.php'; |
| 38 |
$location = $classes_dir . $class_file; |
| 39 |
|
| 40 |
if ( ! is_file( $location ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
require_once $location; |
| 45 |
} |
| 46 |
} |
| 47 |
spl_autoload_register( __NAMESPACE__ . '\autoload' ); |
| 48 |
|