Skip to content

Instantly share code, notes, and snippets.

@kagg-design
Last active January 12, 2022 12:00
Show Gist options
  • Save kagg-design/88c9e2a769588124b7b4ebe3dc70a3c2 to your computer and use it in GitHub Desktop.
Save kagg-design/88c9e2a769588124b7b4ebe3dc70a3c2 to your computer and use it in GitHub Desktop.
Fix performance problem with woodmart autoload.
<?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