Last active
October 10, 2024 06:08
-
-
Save shaunpalmer/665c933067c0f73e5244d4f8680ab9ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is required if remove fatal error will occur | |
if ( ! defined( 'AYS_PLUGIN_PATH' ) ) { | |
define( 'AYS_PLUGIN_PATH', wp_normalize_path( plugin_dir_path( __FILE__ ) ) ); | |
} | |
spl_autoload_register( function ( $class_name ) { | |
// Log the class name being attempted to load | |
error_log( "Autoloader: Attempting to load class $class_name" ); | |
// Only autoload classes that start with 'Ays_' | |
if ( strpos( $class_name, 'Ays_' ) !== 0 ) { | |
error_log( "Autoloader: Class $class_name does not start with 'Ays_'. Skipping." ); | |
return; | |
} | |
// Define the mappings of class prefixes to directories | |
$prefix_to_dir = array( | |
'Ays_CPT_' => 'includes/post-types/', | |
'Ays_Taxonomy_' => 'includes/taxonomies/', | |
'Ays_Helper_' => 'includes/helpers/', | |
// Add more mappings here as needed | |
); | |
// Remove the default directory assignment | |
// $directory = 'includes/classes/'; | |
// Initialize a flag to check if a prefix matched | |
$prefix_matched = false; | |
// Determine the directory based on class prefix | |
foreach ( $prefix_to_dir as $prefix => $dir ) { | |
if ( strpos( $class_name, $prefix ) === 0 ) { | |
$directory = $dir; | |
// Remove the matched prefix from the class name for file naming | |
$class_without_prefix = substr( $class_name, strlen( $prefix ) ); | |
error_log( "Autoloader: Matched prefix $prefix. Directory set to $directory. Class without prefix: $class_without_prefix" ); | |
$prefix_matched = true; | |
break; | |
} | |
} | |
// If no specific prefix matched, log an error and exit | |
if ( ! $prefix_matched ) { | |
error_log( "Autoloader: No matching prefix found for class $class_name. Autoloader stopped." ); | |
return; | |
} | |
// Convert the class name to lowercase and replace underscores with dashes | |
$file_name = 'ays-' . strtolower( str_replace( '_', '-', $class_without_prefix ) ) . '.php'; | |
error_log( "Autoloader: File name constructed as $file_name" ); | |
// Full file path | |
$file = AYS_PLUGIN_PATH . $directory . $file_name; | |
error_log( "Autoloader: Full file path is $file" ); | |
// Include the file if it exists | |
if ( file_exists( $file ) ) { | |
error_log( "Autoloader: File $file found. Including file." ); | |
require_once $file; | |
} else { | |
error_log( "Autoloader: File $file not found for class $class_name" ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not production ready still debugging it are the guts that's there