class-thwcfd-autoloader.php
83 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Auto-loads the required dependencies for this plugin. |
| 4 | * |
| 5 | * @link https://themehigh.com |
| 6 | * @since 1.5.0 |
| 7 | * |
| 8 | * @package woo-checkout-field-editor-pro |
| 9 | * @subpackage woo-checkout-field-editor-pro/includes |
| 10 | */ |
| 11 | if(!defined('WPINC')){ die; } |
| 12 | |
| 13 | if(!class_exists('THWCFD_Autoloader')): |
| 14 | |
| 15 | class THWCFD_Autoloader { |
| 16 | private $include_path = ''; |
| 17 | |
| 18 | private $class_path = array(); |
| 19 | |
| 20 | public function __construct() { |
| 21 | $this->include_path = untrailingslashit(THWCFD_PATH); |
| 22 | |
| 23 | if(function_exists("__autoload")){ |
| 24 | spl_autoload_register("__autoload"); |
| 25 | } |
| 26 | spl_autoload_register(array($this, 'autoload')); |
| 27 | } |
| 28 | |
| 29 | /** Include a class file. */ |
| 30 | private function load_file( $path ) { |
| 31 | if ( $path && is_readable( $path ) ) { |
| 32 | require_once( $path ); |
| 33 | return true; |
| 34 | } |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | /** Class name to file name. */ |
| 39 | private function get_file_name_from_class( $class ) { |
| 40 | return 'class-' . str_replace( '_', '-', $class ) . '.php'; |
| 41 | } |
| 42 | |
| 43 | public function autoload( $class ) { |
| 44 | $class = strtolower( $class ); |
| 45 | $file = $this->get_file_name_from_class( $class ); |
| 46 | $path = ''; |
| 47 | $file_path = ''; |
| 48 | |
| 49 | if(isset($this->class_path[$class])){ |
| 50 | $file_path = $this->include_path . '/' . $this->class_path[$class]; |
| 51 | |
| 52 | } else { |
| 53 | if (strpos($class, 'thwcfd_admin') === 0){ |
| 54 | $path = $this->include_path . '/admin/'; |
| 55 | |
| 56 | } elseif (strpos($class, 'thwcfd_public') === 0){ |
| 57 | $path = $this->include_path . '/public/'; |
| 58 | |
| 59 | } elseif (strpos($class, 'thwcfd_utils') === 0){ |
| 60 | $path = $this->include_path . '/includes/utils/'; |
| 61 | |
| 62 | } elseif (strpos($class, 'wcfe_checkout_field') === 0){ |
| 63 | $path = $this->include_path . '/includes/model/fields/'; |
| 64 | |
| 65 | } elseif (strpos($class, 'wcfe_checkout_section') === 0){ |
| 66 | $path = $this->include_path . '/includes/model/'; |
| 67 | |
| 68 | } else{ |
| 69 | $path = $this->include_path . '/includes/'; |
| 70 | } |
| 71 | $file_path = $path . $file; |
| 72 | } |
| 73 | |
| 74 | if( empty($file_path) || (!$this->load_file($file_path) && strpos($class, 'thwcfd_') === 0) ) { |
| 75 | $this->load_file( $this->include_path . $file ); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | endif; |
| 81 | |
| 82 | new THWCFD_Autoloader(); |
| 83 |