class-wc-autoloader.php 2.5 KB

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