Last active
January 12, 2022 12:00
-
-
Save kagg-design/88c9e2a769588124b7b4ebe3dc70a3c2 to your computer and use it in GitHub Desktop.
Fix performance problem with woodmart autoload.
This file contains 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
<?php | |
if( ! function_exists( 'woodmart_autoload' ) ) { | |
function woodmart_autoload($className) { | |
global $woodmart_files; | |
$className = ltrim($className, '\\'); | |
$fileName = ''; | |
$namespace = ''; | |
if ($lastNsPos = strripos($className, '\\')) { | |
$namespace = substr($className, 0, $lastNsPos); | |
$className = substr($className, $lastNsPos + 1); | |
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; | |
} | |
$className = str_replace('WOODMART_', '', $className); | |
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; | |
$fileName = get_template_directory() . '/inc/classes' . DIRECTORY_SEPARATOR . $fileName; | |
$fileName = str_replace( '\\', '/', $fileName ); | |
if ( ! in_array( $fileName, $woodmart_files, true ) ) { | |
return; | |
} | |
if( file_exists( $fileName )) { | |
require $fileName; | |
} | |
} | |
global $woodmart_files; | |
$woodmart_files = glob( get_template_directory() . '/inc/classes/*.php' ); | |
$woodmart_files = array_map( | |
function( $file ) { | |
return str_replace( '\\', '/', $file ); | |
}, | |
$woodmart_files | |
); | |
spl_autoload_register('woodmart_autoload'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment