| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Autoloader |
| 5 |
* |
| 6 |
* This autoloader will load classes based on their class names. |
| 7 |
* Class names should follow the convention: Namespace\Sub_Namespace\My_Class |
| 8 |
* File names should follow the convention: sub-namespace/my-class.php |
| 9 |
* |
| 10 |
* @package Repeaterly |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Repeaterly; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Autoload classes based on their class names. |
| 21 |
*/ |
| 22 |
function autoload_classes($class_name) { |
| 23 |
$class_name = ltrim($class_name, '\\'); |
| 24 |
if (strpos($class_name, __NAMESPACE__ . '\\') !== 0) { |
| 25 |
return; |
| 26 |
} |
| 27 |
$class_name = str_replace(__NAMESPACE__ . '\\', '', $class_name); |
| 28 |
|
| 29 |
$file_parts = explode('\\', $class_name); |
| 30 |
$file_name = strtolower(str_replace('_', '-', array_pop($file_parts))); |
| 31 |
$namespace_path = !empty($file_parts) ? strtolower(implode(DIRECTORY_SEPARATOR, $file_parts)) . DIRECTORY_SEPARATOR : ''; |
| 32 |
|
| 33 |
$file_path = plugin_dir_path(__FILE__) . $namespace_path . $file_name . '.php'; |
| 34 |
|
| 35 |
if (file_exists($file_path)) { |
| 36 |
require_once $file_path; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Register the autoloader |
| 41 |
spl_autoload_register(__NAMESPACE__ . '\\autoload_classes'); |