暫無描述

class-woocommerce.php 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. <?php
  2. /**
  3. * WooCommerce setup
  4. *
  5. * @package WooCommerce
  6. * @since 3.2.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. use Automattic\WooCommerce\Internal\AssignDefaultCategory;
  10. use Automattic\WooCommerce\Internal\DownloadPermissionsAdjuster;
  11. use Automattic\WooCommerce\Internal\ProductAttributesLookup\DataRegenerator;
  12. use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore;
  13. use Automattic\WooCommerce\Internal\RestockRefundedItemsAdjuster;
  14. use Automattic\WooCommerce\Proxies\LegacyProxy;
  15. /**
  16. * Main WooCommerce Class.
  17. *
  18. * @class WooCommerce
  19. */
  20. final class WooCommerce {
  21. /**
  22. * WooCommerce version.
  23. *
  24. * @var string
  25. */
  26. public $version = '5.7.2';
  27. /**
  28. * WooCommerce Schema version.
  29. *
  30. * @since 4.3 started with version string 430.
  31. *
  32. * @var string
  33. */
  34. public $db_version = '430';
  35. /**
  36. * The single instance of the class.
  37. *
  38. * @var WooCommerce
  39. * @since 2.1
  40. */
  41. protected static $_instance = null;
  42. /**
  43. * Session instance.
  44. *
  45. * @var WC_Session|WC_Session_Handler
  46. */
  47. public $session = null;
  48. /**
  49. * Query instance.
  50. *
  51. * @var WC_Query
  52. */
  53. public $query = null;
  54. /**
  55. * Product factory instance.
  56. *
  57. * @var WC_Product_Factory
  58. */
  59. public $product_factory = null;
  60. /**
  61. * Countries instance.
  62. *
  63. * @var WC_Countries
  64. */
  65. public $countries = null;
  66. /**
  67. * Integrations instance.
  68. *
  69. * @var WC_Integrations
  70. */
  71. public $integrations = null;
  72. /**
  73. * Cart instance.
  74. *
  75. * @var WC_Cart
  76. */
  77. public $cart = null;
  78. /**
  79. * Customer instance.
  80. *
  81. * @var WC_Customer
  82. */
  83. public $customer = null;
  84. /**
  85. * Order factory instance.
  86. *
  87. * @var WC_Order_Factory
  88. */
  89. public $order_factory = null;
  90. /**
  91. * Structured data instance.
  92. *
  93. * @var WC_Structured_Data
  94. */
  95. public $structured_data = null;
  96. /**
  97. * Array of deprecated hook handlers.
  98. *
  99. * @var array of WC_Deprecated_Hooks
  100. */
  101. public $deprecated_hook_handlers = array();
  102. /**
  103. * Main WooCommerce Instance.
  104. *
  105. * Ensures only one instance of WooCommerce is loaded or can be loaded.
  106. *
  107. * @since 2.1
  108. * @static
  109. * @see WC()
  110. * @return WooCommerce - Main instance.
  111. */
  112. public static function instance() {
  113. if ( is_null( self::$_instance ) ) {
  114. self::$_instance = new self();
  115. }
  116. return self::$_instance;
  117. }
  118. /**
  119. * Cloning is forbidden.
  120. *
  121. * @since 2.1
  122. */
  123. public function __clone() {
  124. wc_doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', 'woocommerce' ), '2.1' );
  125. }
  126. /**
  127. * Unserializing instances of this class is forbidden.
  128. *
  129. * @since 2.1
  130. */
  131. public function __wakeup() {
  132. wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'woocommerce' ), '2.1' );
  133. }
  134. /**
  135. * Auto-load in-accessible properties on demand.
  136. *
  137. * @param mixed $key Key name.
  138. * @return mixed
  139. */
  140. public function __get( $key ) {
  141. if ( in_array( $key, array( 'payment_gateways', 'shipping', 'mailer', 'checkout' ), true ) ) {
  142. return $this->$key();
  143. }
  144. }
  145. /**
  146. * WooCommerce Constructor.
  147. */
  148. public function __construct() {
  149. $this->define_constants();
  150. $this->define_tables();
  151. $this->includes();
  152. $this->init_hooks();
  153. }
  154. /**
  155. * When WP has loaded all plugins, trigger the `woocommerce_loaded` hook.
  156. *
  157. * This ensures `woocommerce_loaded` is called only after all other plugins
  158. * are loaded, to avoid issues caused by plugin directory naming changing
  159. * the load order. See #21524 for details.
  160. *
  161. * @since 3.6.0
  162. */
  163. public function on_plugins_loaded() {
  164. do_action( 'woocommerce_loaded' );
  165. }
  166. /**
  167. * Hook into actions and filters.
  168. *
  169. * @since 2.3
  170. */
  171. private function init_hooks() {
  172. register_activation_hook( WC_PLUGIN_FILE, array( 'WC_Install', 'install' ) );
  173. register_shutdown_function( array( $this, 'log_errors' ) );
  174. add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), -1 );
  175. add_action( 'admin_notices', array( $this, 'build_dependencies_notice' ) );
  176. add_action( 'after_setup_theme', array( $this, 'setup_environment' ) );
  177. add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 );
  178. add_action( 'init', array( $this, 'init' ), 0 );
  179. add_action( 'init', array( 'WC_Shortcodes', 'init' ) );
  180. add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
  181. add_action( 'init', array( $this, 'add_image_sizes' ) );
  182. add_action( 'init', array( $this, 'load_rest_api' ) );
  183. add_action( 'switch_blog', array( $this, 'wpdb_table_fix' ), 0 );
  184. add_action( 'activated_plugin', array( $this, 'activated_plugin' ) );
  185. add_action( 'deactivated_plugin', array( $this, 'deactivated_plugin' ) );
  186. add_action( 'woocommerce_installed', array( $this, 'add_woocommerce_inbox_variant' ) );
  187. add_action( 'woocommerce_updated', array( $this, 'add_woocommerce_inbox_variant' ) );
  188. // These classes set up hooks on instantiation.
  189. wc_get_container()->get( DownloadPermissionsAdjuster::class );
  190. wc_get_container()->get( AssignDefaultCategory::class );
  191. wc_get_container()->get( DataRegenerator::class );
  192. wc_get_container()->get( LookupDataStore::class );
  193. wc_get_container()->get( RestockRefundedItemsAdjuster::class );
  194. }
  195. /**
  196. * Add woocommerce_inbox_variant for the Remote Inbox Notification.
  197. *
  198. * P2 post can be found at https://wp.me/paJDYF-1uJ.
  199. */
  200. public function add_woocommerce_inbox_variant() {
  201. $config_name = 'woocommerce_inbox_variant_assignment';
  202. if ( false === get_option( $config_name, false ) ) {
  203. update_option( $config_name, wp_rand( 1, 12 ) );
  204. }
  205. }
  206. /**
  207. * Ensures fatal errors are logged so they can be picked up in the status report.
  208. *
  209. * @since 3.2.0
  210. */
  211. public function log_errors() {
  212. $error = error_get_last();
  213. if ( $error && in_array( $error['type'], array( E_ERROR, E_PARSE, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) {
  214. $logger = wc_get_logger();
  215. $logger->critical(
  216. /* translators: 1: error message 2: file name and path 3: line number */
  217. sprintf( __( '%1$s in %2$s on line %3$s', 'woocommerce' ), $error['message'], $error['file'], $error['line'] ) . PHP_EOL,
  218. array(
  219. 'source' => 'fatal-errors',
  220. )
  221. );
  222. do_action( 'woocommerce_shutdown_error', $error );
  223. }
  224. }
  225. /**
  226. * Define WC Constants.
  227. */
  228. private function define_constants() {
  229. $upload_dir = wp_upload_dir( null, false );
  230. $this->define( 'WC_ABSPATH', dirname( WC_PLUGIN_FILE ) . '/' );
  231. $this->define( 'WC_PLUGIN_BASENAME', plugin_basename( WC_PLUGIN_FILE ) );
  232. $this->define( 'WC_VERSION', $this->version );
  233. $this->define( 'WOOCOMMERCE_VERSION', $this->version );
  234. $this->define( 'WC_ROUNDING_PRECISION', 6 );
  235. $this->define( 'WC_DISCOUNT_ROUNDING_MODE', 2 );
  236. $this->define( 'WC_TAX_ROUNDING_MODE', 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' ) ? 2 : 1 );
  237. $this->define( 'WC_DELIMITER', '|' );
  238. $this->define( 'WC_LOG_DIR', $upload_dir['basedir'] . '/wc-logs/' );
  239. $this->define( 'WC_SESSION_CACHE_GROUP', 'wc_session_id' );
  240. $this->define( 'WC_TEMPLATE_DEBUG_MODE', false );
  241. $this->define( 'WC_NOTICE_MIN_PHP_VERSION', '7.2' );
  242. $this->define( 'WC_NOTICE_MIN_WP_VERSION', '5.2' );
  243. $this->define( 'WC_PHP_MIN_REQUIREMENTS_NOTICE', 'wp_php_min_requirements_' . WC_NOTICE_MIN_PHP_VERSION . '_' . WC_NOTICE_MIN_WP_VERSION );
  244. /** Define if we're checking against major, minor or no versions in the following places:
  245. * - plugin screen in WP Admin (displaying extra warning when updating to new major versions)
  246. * - System Status Report ('Installed version not tested with active version of WooCommerce' warning)
  247. * - core update screen in WP Admin (displaying extra warning when updating to new major versions)
  248. * - enable/disable automated updates in the plugin screen in WP Admin (if there are any plugins
  249. * that don't declare compatibility, the auto-update is disabled)
  250. *
  251. * We dropped SemVer before WC 5.0, so all versions are backwards compatible now, thus no more check needed.
  252. * The SSR in the name is preserved for bw compatibility, as this was initially used in System Status Report.
  253. */
  254. $this->define( 'WC_SSR_PLUGIN_UPDATE_RELEASE_VERSION_TYPE', 'none' );
  255. }
  256. /**
  257. * Register custom tables within $wpdb object.
  258. */
  259. private function define_tables() {
  260. global $wpdb;
  261. // List of tables without prefixes.
  262. $tables = array(
  263. 'payment_tokenmeta' => 'woocommerce_payment_tokenmeta',
  264. 'order_itemmeta' => 'woocommerce_order_itemmeta',
  265. 'wc_product_meta_lookup' => 'wc_product_meta_lookup',
  266. 'wc_tax_rate_classes' => 'wc_tax_rate_classes',
  267. 'wc_reserved_stock' => 'wc_reserved_stock',
  268. );
  269. foreach ( $tables as $name => $table ) {
  270. $wpdb->$name = $wpdb->prefix . $table;
  271. $wpdb->tables[] = $table;
  272. }
  273. }
  274. /**
  275. * Define constant if not already set.
  276. *
  277. * @param string $name Constant name.
  278. * @param string|bool $value Constant value.
  279. */
  280. private function define( $name, $value ) {
  281. if ( ! defined( $name ) ) {
  282. define( $name, $value );
  283. }
  284. }
  285. /**
  286. * Returns true if the request is a non-legacy REST API request.
  287. *
  288. * Legacy REST requests should still run some extra code for backwards compatibility.
  289. *
  290. * @todo: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061.
  291. *
  292. * @return bool
  293. */
  294. public function is_rest_api_request() {
  295. if ( empty( $_SERVER['REQUEST_URI'] ) ) {
  296. return false;
  297. }
  298. $rest_prefix = trailingslashit( rest_get_url_prefix() );
  299. $is_rest_api_request = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix ) ); // phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  300. return apply_filters( 'woocommerce_is_rest_api_request', $is_rest_api_request );
  301. }
  302. /**
  303. * Load REST API.
  304. */
  305. public function load_rest_api() {
  306. \Automattic\WooCommerce\RestApi\Server::instance()->init();
  307. }
  308. /**
  309. * What type of request is this?
  310. *
  311. * @param string $type admin, ajax, cron or frontend.
  312. * @return bool
  313. */
  314. private function is_request( $type ) {
  315. switch ( $type ) {
  316. case 'admin':
  317. return is_admin();
  318. case 'ajax':
  319. return defined( 'DOING_AJAX' );
  320. case 'cron':
  321. return defined( 'DOING_CRON' );
  322. case 'frontend':
  323. return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ) && ! $this->is_rest_api_request();
  324. }
  325. }
  326. /**
  327. * Include required core files used in admin and on the frontend.
  328. */
  329. public function includes() {
  330. /**
  331. * Class autoloader.
  332. */
  333. include_once WC_ABSPATH . 'includes/class-wc-autoloader.php';
  334. /**
  335. * Interfaces.
  336. */
  337. include_once WC_ABSPATH . 'includes/interfaces/class-wc-abstract-order-data-store-interface.php';
  338. include_once WC_ABSPATH . 'includes/interfaces/class-wc-coupon-data-store-interface.php';
  339. include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-data-store-interface.php';
  340. include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-data-store-interface.php';
  341. include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-log-data-store-interface.php';
  342. include_once WC_ABSPATH . 'includes/interfaces/class-wc-object-data-store-interface.php';
  343. include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-data-store-interface.php';
  344. include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-data-store-interface.php';
  345. include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-product-data-store-interface.php';
  346. include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-type-data-store-interface.php';
  347. include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-refund-data-store-interface.php';
  348. include_once WC_ABSPATH . 'includes/interfaces/class-wc-payment-token-data-store-interface.php';
  349. include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-data-store-interface.php';
  350. include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-variable-data-store-interface.php';
  351. include_once WC_ABSPATH . 'includes/interfaces/class-wc-shipping-zone-data-store-interface.php';
  352. include_once WC_ABSPATH . 'includes/interfaces/class-wc-logger-interface.php';
  353. include_once WC_ABSPATH . 'includes/interfaces/class-wc-log-handler-interface.php';
  354. include_once WC_ABSPATH . 'includes/interfaces/class-wc-webhooks-data-store-interface.php';
  355. include_once WC_ABSPATH . 'includes/interfaces/class-wc-queue-interface.php';
  356. /**
  357. * Core traits.
  358. */
  359. include_once WC_ABSPATH . 'includes/traits/trait-wc-item-totals.php';
  360. /**
  361. * Abstract classes.
  362. */
  363. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-data.php';
  364. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-object-query.php';
  365. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-token.php';
  366. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-product.php';
  367. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-order.php';
  368. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-settings-api.php';
  369. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-shipping-method.php';
  370. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-gateway.php';
  371. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-integration.php';
  372. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-log-handler.php';
  373. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-deprecated-hooks.php';
  374. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-session.php';
  375. include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-privacy.php';
  376. /**
  377. * Core classes.
  378. */
  379. include_once WC_ABSPATH . 'includes/wc-core-functions.php';
  380. include_once WC_ABSPATH . 'includes/class-wc-datetime.php';
  381. include_once WC_ABSPATH . 'includes/class-wc-post-types.php';
  382. include_once WC_ABSPATH . 'includes/class-wc-install.php';
  383. include_once WC_ABSPATH . 'includes/class-wc-geolocation.php';
  384. include_once WC_ABSPATH . 'includes/class-wc-download-handler.php';
  385. include_once WC_ABSPATH . 'includes/class-wc-comments.php';
  386. include_once WC_ABSPATH . 'includes/class-wc-post-data.php';
  387. include_once WC_ABSPATH . 'includes/class-wc-ajax.php';
  388. include_once WC_ABSPATH . 'includes/class-wc-emails.php';
  389. include_once WC_ABSPATH . 'includes/class-wc-data-exception.php';
  390. include_once WC_ABSPATH . 'includes/class-wc-query.php';
  391. include_once WC_ABSPATH . 'includes/class-wc-meta-data.php';
  392. include_once WC_ABSPATH . 'includes/class-wc-order-factory.php';
  393. include_once WC_ABSPATH . 'includes/class-wc-order-query.php';
  394. include_once WC_ABSPATH . 'includes/class-wc-product-factory.php';
  395. include_once WC_ABSPATH . 'includes/class-wc-product-query.php';
  396. include_once WC_ABSPATH . 'includes/class-wc-payment-tokens.php';
  397. include_once WC_ABSPATH . 'includes/class-wc-shipping-zone.php';
  398. include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-cc.php';
  399. include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-echeck.php';
  400. include_once WC_ABSPATH . 'includes/class-wc-countries.php';
  401. include_once WC_ABSPATH . 'includes/class-wc-integrations.php';
  402. include_once WC_ABSPATH . 'includes/class-wc-cache-helper.php';
  403. include_once WC_ABSPATH . 'includes/class-wc-https.php';
  404. include_once WC_ABSPATH . 'includes/class-wc-deprecated-action-hooks.php';
  405. include_once WC_ABSPATH . 'includes/class-wc-deprecated-filter-hooks.php';
  406. include_once WC_ABSPATH . 'includes/class-wc-background-emailer.php';
  407. include_once WC_ABSPATH . 'includes/class-wc-discounts.php';
  408. include_once WC_ABSPATH . 'includes/class-wc-cart-totals.php';
  409. include_once WC_ABSPATH . 'includes/customizer/class-wc-shop-customizer.php';
  410. include_once WC_ABSPATH . 'includes/class-wc-regenerate-images.php';
  411. include_once WC_ABSPATH . 'includes/class-wc-privacy.php';
  412. include_once WC_ABSPATH . 'includes/class-wc-structured-data.php';
  413. include_once WC_ABSPATH . 'includes/class-wc-shortcodes.php';
  414. include_once WC_ABSPATH . 'includes/class-wc-logger.php';
  415. include_once WC_ABSPATH . 'includes/queue/class-wc-action-queue.php';
  416. include_once WC_ABSPATH . 'includes/queue/class-wc-queue.php';
  417. include_once WC_ABSPATH . 'includes/admin/marketplace-suggestions/class-wc-marketplace-updater.php';
  418. include_once WC_ABSPATH . 'includes/blocks/class-wc-blocks-utils.php';
  419. /**
  420. * Data stores - used to store and retrieve CRUD object data from the database.
  421. */
  422. include_once WC_ABSPATH . 'includes/class-wc-data-store.php';
  423. include_once WC_ABSPATH . 'includes/data-stores/class-wc-data-store-wp.php';
  424. include_once WC_ABSPATH . 'includes/data-stores/class-wc-coupon-data-store-cpt.php';
  425. include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-data-store-cpt.php';
  426. include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-grouped-data-store-cpt.php';
  427. include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variable-data-store-cpt.php';
  428. include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variation-data-store-cpt.php';
  429. include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-item-type-data-store.php';
  430. include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-data-store.php';
  431. include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-coupon-data-store.php';
  432. include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-fee-data-store.php';
  433. include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-product-data-store.php';
  434. include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-shipping-data-store.php';
  435. include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-tax-data-store.php';
  436. include_once WC_ABSPATH . 'includes/data-stores/class-wc-payment-token-data-store.php';
  437. include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store.php';
  438. include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store-session.php';
  439. include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-data-store.php';
  440. include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-log-data-store.php';
  441. include_once WC_ABSPATH . 'includes/data-stores/class-wc-shipping-zone-data-store.php';
  442. include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-data-store-cpt.php';
  443. include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-data-store-cpt.php';
  444. include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-refund-data-store-cpt.php';
  445. include_once WC_ABSPATH . 'includes/data-stores/class-wc-webhook-data-store.php';
  446. /**
  447. * REST API.
  448. */
  449. include_once WC_ABSPATH . 'includes/legacy/class-wc-legacy-api.php';
  450. include_once WC_ABSPATH . 'includes/class-wc-api.php';
  451. include_once WC_ABSPATH . 'includes/class-wc-rest-authentication.php';
  452. include_once WC_ABSPATH . 'includes/class-wc-rest-exception.php';
  453. include_once WC_ABSPATH . 'includes/class-wc-auth.php';
  454. include_once WC_ABSPATH . 'includes/class-wc-register-wp-admin-settings.php';
  455. /**
  456. * WCCOM Site.
  457. */
  458. include_once WC_ABSPATH . 'includes/wccom-site/class-wc-wccom-site.php';
  459. /**
  460. * Libraries and packages.
  461. */
  462. include_once WC_ABSPATH . 'packages/action-scheduler/action-scheduler.php';
  463. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  464. include_once WC_ABSPATH . 'includes/class-wc-cli.php';
  465. }
  466. if ( $this->is_request( 'admin' ) ) {
  467. include_once WC_ABSPATH . 'includes/admin/class-wc-admin.php';
  468. }
  469. if ( $this->is_request( 'frontend' ) ) {
  470. $this->frontend_includes();
  471. }
  472. if ( $this->is_request( 'cron' ) && 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
  473. include_once WC_ABSPATH . 'includes/class-wc-tracker.php';
  474. }
  475. $this->theme_support_includes();
  476. $this->query = new WC_Query();
  477. $this->api = new WC_API();
  478. $this->api->init();
  479. }
  480. /**
  481. * Include classes for theme support.
  482. *
  483. * @since 3.3.0
  484. */
  485. private function theme_support_includes() {
  486. if ( wc_is_wp_default_theme_active() ) {
  487. switch ( get_template() ) {
  488. case 'twentyten':
  489. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-ten.php';
  490. break;
  491. case 'twentyeleven':
  492. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-eleven.php';
  493. break;
  494. case 'twentytwelve':
  495. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-twelve.php';
  496. break;
  497. case 'twentythirteen':
  498. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-thirteen.php';
  499. break;
  500. case 'twentyfourteen':
  501. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-fourteen.php';
  502. break;
  503. case 'twentyfifteen':
  504. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-fifteen.php';
  505. break;
  506. case 'twentysixteen':
  507. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-sixteen.php';
  508. break;
  509. case 'twentyseventeen':
  510. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-seventeen.php';
  511. break;
  512. case 'twentynineteen':
  513. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-nineteen.php';
  514. break;
  515. case 'twentytwenty':
  516. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-twenty.php';
  517. break;
  518. case 'twentytwentyone':
  519. include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-twenty-one.php';
  520. break;
  521. }
  522. }
  523. }
  524. /**
  525. * Include required frontend files.
  526. */
  527. public function frontend_includes() {
  528. include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
  529. include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
  530. include_once WC_ABSPATH . 'includes/wc-template-hooks.php';
  531. include_once WC_ABSPATH . 'includes/class-wc-template-loader.php';
  532. include_once WC_ABSPATH . 'includes/class-wc-frontend-scripts.php';
  533. include_once WC_ABSPATH . 'includes/class-wc-form-handler.php';
  534. include_once WC_ABSPATH . 'includes/class-wc-cart.php';
  535. include_once WC_ABSPATH . 'includes/class-wc-tax.php';
  536. include_once WC_ABSPATH . 'includes/class-wc-shipping-zones.php';
  537. include_once WC_ABSPATH . 'includes/class-wc-customer.php';
  538. include_once WC_ABSPATH . 'includes/class-wc-embed.php';
  539. include_once WC_ABSPATH . 'includes/class-wc-session-handler.php';
  540. }
  541. /**
  542. * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes.
  543. */
  544. public function include_template_functions() {
  545. include_once WC_ABSPATH . 'includes/wc-template-functions.php';
  546. }
  547. /**
  548. * Init WooCommerce when WordPress Initialises.
  549. */
  550. public function init() {
  551. // Before init action.
  552. do_action( 'before_woocommerce_init' );
  553. // Set up localisation.
  554. $this->load_plugin_textdomain();
  555. // Load class instances.
  556. $this->product_factory = new WC_Product_Factory();
  557. $this->order_factory = new WC_Order_Factory();
  558. $this->countries = new WC_Countries();
  559. $this->integrations = new WC_Integrations();
  560. $this->structured_data = new WC_Structured_Data();
  561. $this->deprecated_hook_handlers['actions'] = new WC_Deprecated_Action_Hooks();
  562. $this->deprecated_hook_handlers['filters'] = new WC_Deprecated_Filter_Hooks();
  563. // Classes/actions loaded for the frontend and for ajax requests.
  564. if ( $this->is_request( 'frontend' ) ) {
  565. wc_load_cart();
  566. }
  567. $this->load_webhooks();
  568. // Init action.
  569. do_action( 'woocommerce_init' );
  570. }
  571. /**
  572. * Load Localisation files.
  573. *
  574. * Note: the first-loaded translation file overrides any following ones if the same translation is present.
  575. *
  576. * Locales found in:
  577. * - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo
  578. * - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo
  579. */
  580. public function load_plugin_textdomain() {
  581. $locale = determine_locale();
  582. $locale = apply_filters( 'plugin_locale', $locale, 'woocommerce' );
  583. unload_textdomain( 'woocommerce' );
  584. load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
  585. load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( WC_PLUGIN_FILE ) ) . '/i18n/languages' );
  586. }
  587. /**
  588. * Ensure theme and server variable compatibility and setup image sizes.
  589. */
  590. public function setup_environment() {
  591. /**
  592. * WC_TEMPLATE_PATH constant.
  593. *
  594. * @deprecated 2.2 Use WC()->template_path() instead.
  595. */
  596. $this->define( 'WC_TEMPLATE_PATH', $this->template_path() );
  597. $this->add_thumbnail_support();
  598. }
  599. /**
  600. * Ensure post thumbnail support is turned on.
  601. */
  602. private function add_thumbnail_support() {
  603. if ( ! current_theme_supports( 'post-thumbnails' ) ) {
  604. add_theme_support( 'post-thumbnails' );
  605. }
  606. add_post_type_support( 'product', 'thumbnail' );
  607. }
  608. /**
  609. * Add WC Image sizes to WP.
  610. *
  611. * As of 3.3, image sizes can be registered via themes using add_theme_support for woocommerce
  612. * and defining an array of args. If these are not defined, we will use defaults. This is
  613. * handled in wc_get_image_size function.
  614. *
  615. * 3.3 sizes:
  616. *
  617. * woocommerce_thumbnail - Used in product listings. We assume these work for a 3 column grid layout.
  618. * woocommerce_single - Used on single product pages for the main image.
  619. *
  620. * @since 2.3
  621. */
  622. public function add_image_sizes() {
  623. $thumbnail = wc_get_image_size( 'thumbnail' );
  624. $single = wc_get_image_size( 'single' );
  625. $gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
  626. add_image_size( 'woocommerce_thumbnail', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
  627. add_image_size( 'woocommerce_single', $single['width'], $single['height'], $single['crop'] );
  628. add_image_size( 'woocommerce_gallery_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
  629. /**
  630. * Legacy image sizes.
  631. *
  632. * @deprecated 3.3.0 These sizes will be removed in 4.6.0.
  633. */
  634. add_image_size( 'shop_catalog', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
  635. add_image_size( 'shop_single', $single['width'], $single['height'], $single['crop'] );
  636. add_image_size( 'shop_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
  637. }
  638. /**
  639. * Get the plugin url.
  640. *
  641. * @return string
  642. */
  643. public function plugin_url() {
  644. return untrailingslashit( plugins_url( '/', WC_PLUGIN_FILE ) );
  645. }
  646. /**
  647. * Get the plugin path.
  648. *
  649. * @return string
  650. */
  651. public function plugin_path() {
  652. return untrailingslashit( plugin_dir_path( WC_PLUGIN_FILE ) );
  653. }
  654. /**
  655. * Get the template path.
  656. *
  657. * @return string
  658. */
  659. public function template_path() {
  660. return apply_filters( 'woocommerce_template_path', 'woocommerce/' );
  661. }
  662. /**
  663. * Get Ajax URL.
  664. *
  665. * @return string
  666. */
  667. public function ajax_url() {
  668. return admin_url( 'admin-ajax.php', 'relative' );
  669. }
  670. /**
  671. * Return the WC API URL for a given request.
  672. *
  673. * @param string $request Requested endpoint.
  674. * @param bool|null $ssl If should use SSL, null if should auto detect. Default: null.
  675. * @return string
  676. */
  677. public function api_request_url( $request, $ssl = null ) {
  678. if ( is_null( $ssl ) ) {
  679. $scheme = wp_parse_url( home_url(), PHP_URL_SCHEME );
  680. } elseif ( $ssl ) {
  681. $scheme = 'https';
  682. } else {
  683. $scheme = 'http';
  684. }
  685. if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
  686. $api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) );
  687. } elseif ( get_option( 'permalink_structure' ) ) {
  688. $api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) );
  689. } else {
  690. $api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
  691. }
  692. return esc_url_raw( apply_filters( 'woocommerce_api_request_url', $api_request_url, $request, $ssl ) );
  693. }
  694. /**
  695. * Load & enqueue active webhooks.
  696. *
  697. * @since 2.2
  698. */
  699. private function load_webhooks() {
  700. if ( ! is_blog_installed() ) {
  701. return;
  702. }
  703. /**
  704. * Hook: woocommerce_load_webhooks_limit.
  705. *
  706. * @since 3.6.0
  707. * @param int $limit Used to limit how many webhooks are loaded. Default: no limit.
  708. */
  709. $limit = apply_filters( 'woocommerce_load_webhooks_limit', null );
  710. wc_load_webhooks( 'active', $limit );
  711. }
  712. /**
  713. * Initialize the customer and cart objects and setup customer saving on shutdown.
  714. *
  715. * @since 3.6.4
  716. * @return void
  717. */
  718. public function initialize_cart() {
  719. // Cart needs customer info.
  720. if ( is_null( $this->customer ) || ! $this->customer instanceof WC_Customer ) {
  721. $this->customer = new WC_Customer( get_current_user_id(), true );
  722. // Customer should be saved during shutdown.
  723. add_action( 'shutdown', array( $this->customer, 'save' ), 10 );
  724. }
  725. if ( is_null( $this->cart ) || ! $this->cart instanceof WC_Cart ) {
  726. $this->cart = new WC_Cart();
  727. }
  728. }
  729. /**
  730. * Initialize the session class.
  731. *
  732. * @since 3.6.4
  733. * @return void
  734. */
  735. public function initialize_session() {
  736. // Session class, handles session data for users - can be overwritten if custom handler is needed.
  737. $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
  738. if ( is_null( $this->session ) || ! $this->session instanceof $session_class ) {
  739. $this->session = new $session_class();
  740. $this->session->init();
  741. }
  742. }
  743. /**
  744. * Set tablenames inside WPDB object.
  745. */
  746. public function wpdb_table_fix() {
  747. $this->define_tables();
  748. }
  749. /**
  750. * Ran when any plugin is activated.
  751. *
  752. * @since 3.6.0
  753. * @param string $filename The filename of the activated plugin.
  754. */
  755. public function activated_plugin( $filename ) {
  756. include_once dirname( __FILE__ ) . '/admin/helper/class-wc-helper.php';
  757. if ( '/woocommerce.php' === substr( $filename, -16 ) ) {
  758. set_transient( 'woocommerce_activated_plugin', $filename );
  759. }
  760. WC_Helper::activated_plugin( $filename );
  761. }
  762. /**
  763. * Ran when any plugin is deactivated.
  764. *
  765. * @since 3.6.0
  766. * @param string $filename The filename of the deactivated plugin.
  767. */
  768. public function deactivated_plugin( $filename ) {
  769. include_once dirname( __FILE__ ) . '/admin/helper/class-wc-helper.php';
  770. WC_Helper::deactivated_plugin( $filename );
  771. }
  772. /**
  773. * Get queue instance.
  774. *
  775. * @return WC_Queue_Interface
  776. */
  777. public function queue() {
  778. return WC_Queue::instance();
  779. }
  780. /**
  781. * Get Checkout Class.
  782. *
  783. * @return WC_Checkout
  784. */
  785. public function checkout() {
  786. return WC_Checkout::instance();
  787. }
  788. /**
  789. * Get gateways class.
  790. *
  791. * @return WC_Payment_Gateways
  792. */
  793. public function payment_gateways() {
  794. return WC_Payment_Gateways::instance();
  795. }
  796. /**
  797. * Get shipping class.
  798. *
  799. * @return WC_Shipping
  800. */
  801. public function shipping() {
  802. return WC_Shipping::instance();
  803. }
  804. /**
  805. * Email Class.
  806. *
  807. * @return WC_Emails
  808. */
  809. public function mailer() {
  810. return WC_Emails::instance();
  811. }
  812. /**
  813. * Check if plugin assets are built and minified
  814. *
  815. * @return bool
  816. */
  817. public function build_dependencies_satisfied() {
  818. // Check if we have compiled CSS.
  819. if ( ! file_exists( WC()->plugin_path() . '/assets/css/admin.css' ) ) {
  820. return false;
  821. }
  822. // Check if we have minified JS.
  823. if ( ! file_exists( WC()->plugin_path() . '/assets/js/admin/woocommerce_admin.min.js' ) ) {
  824. return false;
  825. }
  826. return true;
  827. }
  828. /**
  829. * Output a admin notice when build dependencies not met.
  830. *
  831. * @return void
  832. */
  833. public function build_dependencies_notice() {
  834. if ( $this->build_dependencies_satisfied() ) {
  835. return;
  836. }
  837. $message_one = __( 'You have installed a development version of WooCommerce which requires files to be built and minified. From the plugin directory, run <code>grunt assets</code> to build and minify assets.', 'woocommerce' );
  838. $message_two = sprintf(
  839. /* translators: 1: URL of WordPress.org Repository 2: URL of the GitHub Repository release page */
  840. __( 'Or you can download a pre-built version of the plugin from the <a href="%1$s">WordPress.org repository</a> or by visiting <a href="%2$s">the releases page in the GitHub repository</a>.', 'woocommerce' ),
  841. 'https://wordpress.org/plugins/woocommerce/',
  842. 'https://github.com/woocommerce/woocommerce/releases'
  843. );
  844. printf( '<div class="error"><p>%s %s</p></div>', $message_one, $message_two ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  845. }
  846. /**
  847. * Is the WooCommerce Admin actively included in the WooCommerce core?
  848. * Based on presence of a basic WC Admin function.
  849. *
  850. * @return boolean
  851. */
  852. public function is_wc_admin_active() {
  853. return function_exists( 'wc_admin_url' );
  854. }
  855. /**
  856. * Call a user function. This should be used to execute any non-idempotent function, especially
  857. * those in the `includes` directory or provided by WordPress.
  858. *
  859. * This method can be useful for unit tests, since functions called using this method
  860. * can be easily mocked by using WC_Unit_Test_Case::register_legacy_proxy_function_mocks.
  861. *
  862. * @param string $function_name The function to execute.
  863. * @param mixed ...$parameters The parameters to pass to the function.
  864. *
  865. * @return mixed The result from the function.
  866. *
  867. * @since 4.4
  868. */
  869. public function call_function( $function_name, ...$parameters ) {
  870. return wc_get_container()->get( LegacyProxy::class )->call_function( $function_name, ...$parameters );
  871. }
  872. /**
  873. * Call a static method in a class. This should be used to execute any non-idempotent method in classes
  874. * from the `includes` directory.
  875. *
  876. * This method can be useful for unit tests, since methods called using this method
  877. * can be easily mocked by using WC_Unit_Test_Case::register_legacy_proxy_static_mocks.
  878. *
  879. * @param string $class_name The name of the class containing the method.
  880. * @param string $method_name The name of the method.
  881. * @param mixed ...$parameters The parameters to pass to the method.
  882. *
  883. * @return mixed The result from the method.
  884. *
  885. * @since 4.4
  886. */
  887. public function call_static( $class_name, $method_name, ...$parameters ) {
  888. return wc_get_container()->get( LegacyProxy::class )->call_static( $class_name, $method_name, ...$parameters );
  889. }
  890. /**
  891. * Gets an instance of a given legacy class.
  892. * This must not be used to get instances of classes in the `src` directory.
  893. *
  894. * This method can be useful for unit tests, since objects obtained using this method
  895. * can be easily mocked by using WC_Unit_Test_Case::register_legacy_proxy_class_mocks.
  896. *
  897. * @param string $class_name The name of the class to get an instance for.
  898. * @param mixed ...$args Parameters to be passed to the class constructor or to the appropriate internal 'get_instance_of_' method.
  899. *
  900. * @return object The instance of the class.
  901. * @throws \Exception The requested class belongs to the `src` directory, or there was an error creating an instance of the class.
  902. *
  903. * @since 4.4
  904. */
  905. public function get_instance_of( string $class_name, ...$args ) {
  906. return wc_get_container()->get( LegacyProxy::class )->get_instance_of( $class_name, ...$args );
  907. }
  908. }