Files
yeuchaybo/lib/autoload.php
T
Thuan Bui fd25f13a4f Initial
2018-06-26 11:09:22 +07:00

195 lines
3.3 KiB
PHP

<?php
/**
* Child Theme Library
*
* WARNING: This file is a part of the core Child Theme Library.
* DO NOT edit this file under any circumstances. Please use
* the functions.php file to make any theme modifications.
*
* @package SEOThemes\ChildThemeLibrary\Functions
* @link https://github.com/seothemes/child-theme-library
* @author Thuan Bui
* @copyright Copyright © 2018 Thuan Bui
* @license GPL-2.0+
*/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
die;
}
add_action( 'genesis_setup', 'child_theme_composer_autoload' );
/**
* Includes the composer autoloader.
*
* @since 1.0.0
*
* @return void
*/
function child_theme_composer_autoload() {
if ( file_exists( CHILD_THEME_DIR . '/vendor/autoload.php' ) ) {
require CHILD_THEME_DIR . '/vendor/autoload.php';
}
}
spl_autoload_register( 'child_theme_spl_autoload_register' );
/**
* Register class autoloader.
*
* @since 1.0.0
*
* @param string $class Class to load.
*
* @return void
*/
function child_theme_spl_autoload_register( $class ) {
$file_name = str_replace( '_', '-', strtolower( $class ) );
$file = CHILD_THEME_LIB . "/classes/class-{$file_name}.php";
if ( stream_resolve_include_path( $file ) ) {
include $file;
}
}
/**
* Recursively loads all php files in all subdirectories of the given path.
*
* @since 1.0.0
*
* @link https://github.com/AaronHolbrook/autoload
*
* @param string $directory Directory of files to autoload.
*
* @throws Exception If too many files are loaded.
*
* @return void
*/
function child_theme_autoload_dir( $directory ) {
// Get a listing of the current directory.
$scanned_dir = scandir( $directory );
if ( empty( $scanned_dir ) ) {
return;
}
if ( count( $scanned_dir ) > 100 ) {
throw new Exception( 'Too many files attempted to load via autoload' );
}
// Ignore these items from scandir.
$ignore = [
'.',
'..',
];
// Remove the ignored items.
$scanned_dir = array_diff( $scanned_dir, $ignore );
foreach ( $scanned_dir as $item ) {
$filename = $directory . '/' . $item;
$real_path = realpath( $filename );
if ( false === $real_path ) {
continue;
}
$filetype = filetype( $real_path );
if ( empty( $filetype ) ) {
continue;
}
// If it's a directory then recursively load it.
if ( 'dir' === $filetype ) {
child_theme_autoload( $real_path );
} elseif ( 'file' === $filetype ) {
// Don't allow files that have been uploaded.
if ( is_uploaded_file( $real_path ) ) {
continue;
}
$filesize = filesize( $real_path );
// Don't include empty or negative sized files.
if ( $filesize <= 0 ) {
continue;
}
// Don't include files that are greater than 300kb.
if ( $filesize > 300000 ) {
continue;
}
$pathinfo = pathinfo( $real_path );
// An empty filename wouldn't be a good idea.
if ( empty( $pathinfo['filename'] ) ) {
continue;
}
// Sorry, need an extension.
if ( empty( $pathinfo['extension'] ) ) {
continue;
}
// Actually, we want just a PHP extension!
if ( 'php' !== $pathinfo['extension'] ) {
continue;
}
// Only for files that really exist.
if ( true !== file_exists( $real_path ) ) {
continue;
}
if ( true !== is_readable( $real_path ) ) {
continue;
}
require_once $real_path;
}
}
}