WC_Autoloader

WooCommerce Autoloader.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-autoloader.php  
  1. class WC_Autoloader { 
  2.  
  3. /** 
  4. * Path to the includes directory. 
  5. * @var string 
  6. */ 
  7. private $include_path = ''; 
  8.  
  9. /** 
  10. * The Constructor. 
  11. */ 
  12. public function __construct() { 
  13. if ( function_exists( "__autoload" ) ) { 
  14. spl_autoload_register( "__autoload" ); 
  15.  
  16. spl_autoload_register( array( $this, 'autoload' ) ); 
  17.  
  18. $this->include_path = untrailingslashit( plugin_dir_path( WC_PLUGIN_FILE ) ) . '/includes/'; 
  19.  
  20. /** 
  21. * Take a class name and turn it into a file name. 
  22. * @param string $class 
  23. * @return string 
  24. */ 
  25. private function get_file_name_from_class( $class ) { 
  26. return 'class-' . str_replace( '_', '-', $class ) . '.php'; 
  27.  
  28. /** 
  29. * Include a class file. 
  30. * @param string $path 
  31. * @return bool successful or not 
  32. */ 
  33. private function load_file( $path ) { 
  34. if ( $path && is_readable( $path ) ) { 
  35. include_once( $path ); 
  36. return true; 
  37. return false; 
  38.  
  39. /** 
  40. * Auto-load WC classes on demand to reduce memory consumption. 
  41. * @param string $class 
  42. */ 
  43. public function autoload( $class ) { 
  44. $class = strtolower( $class ); 
  45.  
  46. if ( 0 !== strpos( $class, 'wc_' ) ) { 
  47. return; 
  48.  
  49. $file = $this->get_file_name_from_class( $class ); 
  50. $path = ''; 
  51.  
  52. if ( strpos( $class, 'wc_addons_gateway_' ) === 0 ) { 
  53. $path = $this->include_path . 'gateways/' . substr( str_replace( '_', '-', $class ), 18 ) . '/'; 
  54. } elseif ( strpos( $class, 'wc_gateway_' ) === 0 ) { 
  55. $path = $this->include_path . 'gateways/' . substr( str_replace( '_', '-', $class ), 11 ) . '/'; 
  56. } elseif ( strpos( $class, 'wc_shipping_' ) === 0 ) { 
  57. $path = $this->include_path . 'shipping/' . substr( str_replace( '_', '-', $class ), 12 ) . '/'; 
  58. } elseif ( strpos( $class, 'wc_shortcode_' ) === 0 ) { 
  59. $path = $this->include_path . 'shortcodes/'; 
  60. } elseif ( strpos( $class, 'wc_meta_box' ) === 0 ) { 
  61. $path = $this->include_path . 'admin/meta-boxes/'; 
  62. } elseif ( strpos( $class, 'wc_admin' ) === 0 ) { 
  63. $path = $this->include_path . 'admin/'; 
  64. } elseif ( strpos( $class, 'wc_payment_token_' ) === 0 ) { 
  65. $path = $this->include_path . 'payment-tokens/'; 
  66. } elseif ( strpos( $class, 'wc_log_handler_' ) === 0 ) { 
  67. $path = $this->include_path . 'log-handlers/'; 
  68.  
  69. if ( empty( $path ) || ! $this->load_file( $path . $file ) ) { 
  70. $this->load_file( $this->include_path . $file );