abstracts
1 month ago
admin
6 days ago
blocks
11 months ago
cli
1 month ago
customizer
4 months ago
data-stores
1 month ago
emails
5 days ago
export
1 year ago
gateways
1 month ago
import
3 months ago
integrations
2 months ago
interfaces
4 months ago
legacy
4 months ago
libraries
1 month ago
log-handlers
1 year ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
4 months ago
react-admin
1 month ago
rest-api
1 month ago
shipping
3 months ago
shortcodes
1 month ago
theme-support
2 years ago
tracks
1 month ago
traits
5 years ago
walkers
5 years ago
wccom-site
1 month ago
widgets
2 months ago
class-wc-ajax.php
1 month ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
9 months ago
class-wc-background-emailer.php
2 months ago
class-wc-background-updater.php
5 years ago
class-wc-brands-brand-settings-manager.php
1 year ago
class-wc-brands-coupons.php
1 year ago
class-wc-brands.php
1 month ago
class-wc-breadcrumb.php
4 months ago
class-wc-cache-helper.php
1 month ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
3 months ago
class-wc-cart-totals.php
11 months ago
class-wc-cart.php
1 month ago
class-wc-checkout.php
1 month ago
class-wc-cli.php
10 months ago
class-wc-comments.php
4 months ago
class-wc-countries.php
2 months ago
class-wc-coupon.php
2 months ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
1 day ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
2 years ago
class-wc-deprecated-filter-hooks.php
3 months ago
class-wc-discounts.php
11 months ago
class-wc-download-handler.php
1 month ago
class-wc-emails.php
1 month ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
3 months ago
class-wc-frontend-scripts.php
2 months ago
class-wc-geo-ip.php
9 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
2 months ago
class-wc-https.php
3 years ago
class-wc-install.php
6 days ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
4 months ago
class-wc-meta-data.php
1 month ago
class-wc-order-factory.php
2 months ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
5 months ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
1 month ago
class-wc-order-item-shipping.php
5 months ago
class-wc-order-item-tax.php
1 month ago
class-wc-order-item.php
1 month ago
class-wc-order-query.php
1 month ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
1 month ago
class-wc-payment-gateways.php
1 month ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
2 months ago
class-wc-post-types.php
1 month ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
10 months ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
1 year ago
class-wc-product-attribute.php
4 months ago
class-wc-product-download.php
4 months ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
3 months ago
class-wc-product-grouped.php
3 months ago
class-wc-product-query.php
4 months ago
class-wc-product-simple.php
11 months ago
class-wc-product-variable.php
1 month ago
class-wc-product-variation.php
1 month ago
class-wc-query.php
1 month ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
2 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
2 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
3 months ago
class-wc-shipping-rate.php
1 year ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
7 months ago
class-wc-shipping.php
2 months ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
1 month ago
class-wc-tax.php
2 months ago
class-wc-template-loader.php
7 months ago
class-wc-tracker.php
1 month ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
1 month ago
class-woocommerce.php
1 day ago
wc-account-functions.php
1 month ago
wc-attribute-functions.php
1 month ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
1 month ago
wc-conditional-functions.php
11 months ago
wc-core-functions.php
2 months ago
wc-coupon-functions.php
5 months ago
wc-deprecated-functions.php
1 month ago
wc-formatting-functions.php
1 month ago
wc-interactivity-api-functions.php
2 months ago
wc-notice-functions.php
5 months ago
wc-order-functions.php
1 month ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
4 months ago
wc-page-functions.php
1 month ago
wc-product-functions.php
2 months ago
wc-rest-functions.php
7 months ago
wc-stock-functions.php
7 months ago
wc-template-functions.php
1 month ago
wc-template-hooks.php
10 months ago
wc-term-functions.php
2 months ago
wc-update-functions.php
6 days ago
wc-user-functions.php
1 month ago
wc-webhook-functions.php
1 month ago
wc-widget-functions.php
5 years ago
class-wc-autoloader.php
182 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Autoloader. |
| 4 | * |
| 5 | * @package WooCommerce\Classes |
| 6 | * @version 2.3.0 |
| 7 | */ |
| 8 | |
| 9 | declare(strict_types=1); |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | use Automattic\WooCommerce\Admin\Features\Features; |
| 14 | |
| 15 | /** |
| 16 | * Autoloader class. |
| 17 | */ |
| 18 | class WC_Autoloader { |
| 19 | |
| 20 | /** |
| 21 | * Path to the includes directory. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | private $include_path = ''; |
| 26 | |
| 27 | /** |
| 28 | * The Constructor. |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | if ( function_exists( '__autoload' ) ) { |
| 32 | spl_autoload_register( '__autoload' ); |
| 33 | } |
| 34 | |
| 35 | spl_autoload_register( array( $this, 'autoload' ) ); |
| 36 | |
| 37 | $this->include_path = untrailingslashit( plugin_dir_path( WC_PLUGIN_FILE ) ) . '/includes/'; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Take a class name and turn it into a file name. |
| 42 | * |
| 43 | * @param string $class Class name. |
| 44 | * @return string |
| 45 | */ |
| 46 | private function get_file_name_from_class( $class ) { |
| 47 | return 'class-' . str_replace( '_', '-', $class ) . '.php'; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Include a class file. |
| 52 | * |
| 53 | * @param string $path File path. |
| 54 | * @return bool Successful or not. |
| 55 | */ |
| 56 | private function load_file( $path ) { |
| 57 | if ( $path && is_readable( $path ) ) { |
| 58 | include_once $path; |
| 59 | return true; |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Auto-load WC classes on demand to reduce memory consumption. |
| 66 | * |
| 67 | * @param string $class Class name. |
| 68 | */ |
| 69 | public function autoload( $class ) { |
| 70 | $class = strtolower( $class ); |
| 71 | |
| 72 | if ( 0 !== strpos( $class, 'wc_' ) ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // The Legacy REST API was removed in WooCommerce 9.0, but some servers still have |
| 77 | // the includes/class-wc-api.php file after they upgrade, which causes a fatal error when executing |
| 78 | // "class_exists('WC_API')". This will prevent this error, while still making the class visible |
| 79 | // when it's provided by the WooCommerce Legacy REST API plugin. |
| 80 | if ( 'wc_api' === $class ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // If the class is already loaded from a merged package, prevent autoloader from loading it as well. |
| 85 | if ( \Automattic\WooCommerce\Packages::should_load_class( $class ) ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | $file = $this->get_file_name_from_class( $class ); |
| 90 | $path = ''; |
| 91 | |
| 92 | if ( 0 === strpos( $class, 'wc_addons_gateway_' ) ) { |
| 93 | $path = $this->include_path . 'gateways/' . substr( str_replace( '_', '-', $class ), 18 ) . '/'; |
| 94 | } elseif ( 0 === strpos( $class, 'wc_gateway_' ) ) { |
| 95 | $path = $this->include_path . 'gateways/' . substr( str_replace( '_', '-', $class ), 11 ) . '/'; |
| 96 | } elseif ( 0 === strpos( $class, 'wc_shipping_' ) ) { |
| 97 | $path = $this->include_path . 'shipping/' . substr( str_replace( '_', '-', $class ), 12 ) . '/'; |
| 98 | } elseif ( 0 === strpos( $class, 'wc_shortcode_' ) ) { |
| 99 | $path = $this->include_path . 'shortcodes/'; |
| 100 | } elseif ( 0 === strpos( $class, 'wc_meta_box' ) ) { |
| 101 | $path = $this->include_path . 'admin/meta-boxes/'; |
| 102 | } elseif ( 0 === strpos( $class, 'wc_admin' ) ) { |
| 103 | $path = $this->include_path . 'admin/'; |
| 104 | } elseif ( 0 === strpos( $class, 'wc_payment_token_' ) ) { |
| 105 | $path = $this->include_path . 'payment-tokens/'; |
| 106 | } elseif ( 0 === strpos( $class, 'wc_log_handler_' ) ) { |
| 107 | $path = $this->include_path . 'log-handlers/'; |
| 108 | } elseif ( 0 === strpos( $class, 'wc_integration' ) ) { |
| 109 | $path = $this->include_path . 'integrations/' . substr( str_replace( '_', '-', $class ), 15 ) . '/'; |
| 110 | } elseif ( 0 === strpos( $class, 'wc_notes_' ) ) { |
| 111 | $path = $this->include_path . 'admin/notes/'; |
| 112 | } elseif ( 0 === strpos( $class, 'wc_rest_' ) ) { |
| 113 | // Handle REST API controllers in subdirectories. |
| 114 | // For V4 controllers, check if the feature is enabled first. |
| 115 | if ( false !== strpos( $class, '_v4_' ) ) { |
| 116 | // Only load V4 controllers if the feature is enabled. |
| 117 | if ( Features::is_enabled( 'rest-api-v4' ) ) { |
| 118 | $rest_controller_paths = array( |
| 119 | 'rest-api/Controllers/Version4/', |
| 120 | ); |
| 121 | |
| 122 | foreach ( $rest_controller_paths as $rest_path ) { |
| 123 | if ( $this->load_file( $this->include_path . $rest_path . $file ) ) { |
| 124 | return; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Also check subdirectories recursively for V4. |
| 129 | $this->load_rest_v4_controller_recursively( $file ); |
| 130 | } |
| 131 | } else { |
| 132 | // For non-V4 controllers, load normally. |
| 133 | $rest_controller_paths = array( |
| 134 | 'rest-api/Controllers/Version1/', |
| 135 | 'rest-api/Controllers/Version2/', |
| 136 | 'rest-api/Controllers/Version3/', |
| 137 | 'rest-api/Controllers/Telemetry/', |
| 138 | ); |
| 139 | |
| 140 | foreach ( $rest_controller_paths as $rest_path ) { |
| 141 | if ( $this->load_file( $this->include_path . $rest_path . $file ) ) { |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if ( empty( $path ) || ! $this->load_file( $path . $file ) ) { |
| 149 | $this->load_file( $this->include_path . $file ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Recursively load REST API V4 controllers from subdirectories. |
| 155 | * |
| 156 | * @param string $file File name to search for. |
| 157 | */ |
| 158 | private function load_rest_v4_controller_recursively( $file ): bool { |
| 159 | $v4_base_path = $this->include_path . 'rest-api/Controllers/Version4/'; |
| 160 | |
| 161 | // Use RecursiveDirectoryIterator to search subdirectories. |
| 162 | if ( is_dir( $v4_base_path ) ) { |
| 163 | $iterator = new RecursiveIteratorIterator( |
| 164 | new RecursiveDirectoryIterator( $v4_base_path, RecursiveDirectoryIterator::SKIP_DOTS ), |
| 165 | RecursiveIteratorIterator::SELF_FIRST |
| 166 | ); |
| 167 | |
| 168 | foreach ( $iterator as $dir_info ) { |
| 169 | if ( $dir_info->isDir() ) { |
| 170 | $subdir_path = $dir_info->getPathname() . '/'; |
| 171 | if ( $this->load_file( $subdir_path . $file ) ) { |
| 172 | return true; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | new WC_Autoloader(); |
| 182 |