$key(); } } /** * WooCommerce Constructor. * @access public * @return WooCommerce */ public function __construct() { $this->define_constants(); $this->includes(); // Init WC API $this->api = new WC_API(); // Hooks add_action( 'after_setup_theme', array( $this, 'setup_environment' ) ); add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 ); add_action( 'init', array( $this, 'init' ), 0 ); add_action( 'init', array( 'WC_Shortcodes', 'init' ) ); add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) ); // Loaded action do_action( 'woocommerce_loaded' ); } /** * Define constant if not already set * @param string $name * @param string|bool $value */ private function define( $name, $value ) { if ( ! defined( $name ) ) { define( $name, $value ); } } /** * What type of request is this? * string $type ajax, frontend or admin * @return string */ private function is_request( $type ) { switch ( $type ) { case 'admin' : return is_admin(); case 'ajax' : return defined( 'DOING_AJAX' ); case 'frontend' : return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ); } } /** * Define WC Constants */ private function define_constants() { $upload_dir = wp_upload_dir(); $this->define( 'WC_PLUGIN_FILE', __FILE__ ); $this->define( 'WC_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); $this->define( 'WC_VERSION', $this->version ); $this->define( 'WOOCOMMERCE_VERSION', $this->version ); $this->define( 'WC_ROUNDING_PRECISION', 4 ); $this->define( 'WC_TAX_ROUNDING_MODE', 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' ) ? 2 : 1 ); $this->define( 'WC_DELIMITER', '|' ); $this->define( 'WC_LOG_DIR', $upload_dir['basedir'] . '/wc-logs/' ); } /** * Include required core files used in admin and on the frontend. */ private function includes() { include_once( 'includes/class-wc-autoloader.php' ); include_once( 'includes/wc-core-functions.php' ); include_once( 'includes/wc-widget-functions.php' ); include_once( 'includes/wc-webhook-functions.php' ); include_once( 'includes/class-wc-install.php' ); include_once( 'includes/class-wc-geolocation.php' ); include_once( 'includes/class-wc-download-handler.php' ); include_once( 'includes/class-wc-comments.php' ); include_once( 'includes/class-wc-post-data.php' ); if ( $this->is_request( 'admin' ) ) { include_once( 'includes/admin/class-wc-admin.php' ); } if ( $this->is_request( 'ajax' ) ) { $this->ajax_includes(); } if ( $this->is_request( 'frontend' ) ) { $this->frontend_includes(); } // Query class $this->query = include( 'includes/class-wc-query.php' ); // The main query class include_once( 'includes/class-wc-post-types.php' ); // Registers post types include_once( 'includes/class-wc-api.php' ); // API Class include_once( 'includes/abstracts/abstract-wc-product.php' ); // Products include_once( 'includes/abstracts/abstract-wc-order.php' ); // Orders include_once( 'includes/abstracts/abstract-wc-settings-api.php' ); // Settings API (for gateways, shipping, and integrations) include_once( 'includes/abstracts/abstract-wc-shipping-method.php' ); // A Shipping method include_once( 'includes/abstracts/abstract-wc-payment-gateway.php' ); // A Payment gateway include_once( 'includes/abstracts/abstract-wc-integration.php' ); // An integration with a service include_once( 'includes/class-wc-product-factory.php' ); // Product factory include_once( 'includes/class-wc-countries.php' ); // Defines countries and states include_once( 'includes/class-wc-integrations.php' ); // Loads integrations include_once( 'includes/class-wc-cache-helper.php' ); // Cache Helper include_once( 'includes/class-wc-language-pack-upgrader.php' ); // Download/update languages } /** * Include required ajax files. */ public function ajax_includes() { include_once( 'includes/class-wc-ajax.php' ); // Ajax functions for admin and the front-end } /** * Include required frontend files. */ public function frontend_includes() { include_once( 'includes/wc-cart-functions.php' ); include_once( 'includes/wc-notice-functions.php' ); include_once( 'includes/abstracts/abstract-wc-session.php' ); include_once( 'includes/class-wc-session-handler.php' ); include_once( 'includes/wc-template-hooks.php' ); include_once( 'includes/class-wc-template-loader.php' ); // Template Loader include_once( 'includes/class-wc-frontend-scripts.php' ); // Frontend Scripts include_once( 'includes/class-wc-form-handler.php' ); // Form Handlers include_once( 'includes/class-wc-cart.php' ); // The main cart class include_once( 'includes/class-wc-tax.php' ); // Tax class include_once( 'includes/class-wc-customer.php' ); // Customer class include_once( 'includes/class-wc-shortcodes.php' ); // Shortcodes class include_once( 'includes/class-wc-https.php' ); // https Helper } /** * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes. */ public function include_template_functions() { if ( $this->is_request( 'frontend' ) ) { include_once( 'includes/wc-template-functions.php' ); } } /** * Init WooCommerce when WordPress Initialises. */ public function init() { // Before init action do_action( 'before_woocommerce_init' ); // Set up localisation $this->load_plugin_textdomain(); // Load class instances $this->product_factory = new WC_Product_Factory(); // Product Factory to create new product instances $this->order_factory = new WC_Order_Factory(); // Order Factory to create new order instances $this->countries = new WC_Countries(); // Countries class $this->integrations = new WC_Integrations(); // Integrations class // Classes/actions loaded for the frontend and for ajax requests if ( $this->is_request( 'frontend' ) ) { // Session class, handles session data for users - can be overwritten if custom handler is needed $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' ); // Class instances $this->session = new $session_class(); $this->cart = new WC_Cart(); // Cart class, stores the cart contents $this->customer = new WC_Customer(); // Customer class, handles data such as customer location } $this->load_webhooks(); // Init action do_action( 'woocommerce_init' ); } /** * Load Localisation files. * * Note: the first-loaded translation file overrides any following ones if the same translation is present. * * Admin Locales are found in: * - WP_LANG_DIR/woocommerce/woocommerce-admin-LOCALE.mo * - WP_LANG_DIR/plugins/woocommerce-admin-LOCALE.mo * * Frontend/global Locales found in: * - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo * - woocommerce/i18n/languages/woocommerce-LOCALE.mo (which if not found falls back to:) * - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo */ public function load_plugin_textdomain() { $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce' ); if ( $this->is_request( 'admin' ) ) { load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-admin-' . $locale . '.mo' ); load_textdomain( 'woocommerce', WP_LANG_DIR . '/plugins/woocommerce-admin-' . $locale . '.mo' ); } load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' ); load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( __FILE__ ) ) . "/i18n/languages" ); } /** * Ensure theme and server variable compatibility and setup image sizes. */ public function setup_environment() { /** * @deprecated 2.2 Use WC()->template_path() */ $this->define( 'WC_TEMPLATE_PATH', $this->template_path() ); $this->add_thumbnail_support(); $this->add_image_sizes(); $this->fix_server_vars(); } /** * Ensure post thumbnail support is turned on */ private function add_thumbnail_support() { if ( ! current_theme_supports( 'post-thumbnails' ) ) { add_theme_support( 'post-thumbnails' ); } add_post_type_support( 'product', 'thumbnail' ); } /** * Add WC Image sizes to WP * * @since 2.3 */ private function add_image_sizes() { $shop_thumbnail = wc_get_image_size( 'shop_thumbnail' ); $shop_catalog = wc_get_image_size( 'shop_catalog' ); $shop_single = wc_get_image_size( 'shop_single' ); add_image_size( 'shop_thumbnail', $shop_thumbnail['width'], $shop_thumbnail['height'], $shop_thumbnail['crop'] ); add_image_size( 'shop_catalog', $shop_catalog['width'], $shop_catalog['height'], $shop_catalog['crop'] ); add_image_size( 'shop_single', $shop_single['width'], $shop_single['height'], $shop_single['crop'] ); } /** * Fix `$_SERVER` variables for various setups. * * Note: Removed IIS handling due to wp_fix_server_vars() * * @since 2.3 */ private function fix_server_vars() { // NGINX Proxy if ( ! isset( $_SERVER['REMOTE_ADDR'] ) && isset( $_SERVER['HTTP_REMOTE_ADDR'] ) ) { $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_REMOTE_ADDR']; } if ( ! isset( $_SERVER['HTTPS'] ) ) { if ( ! empty( $_SERVER['HTTP_HTTPS'] ) ) { $_SERVER['HTTPS'] = $_SERVER['HTTP_HTTPS']; } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ) { $_SERVER['HTTPS'] = '1'; } } } /** * Get the plugin url. * @return string */ public function plugin_url() { return untrailingslashit( plugins_url( '/', __FILE__ ) ); } /** * Get the plugin path. * @return string */ public function plugin_path() { return untrailingslashit( plugin_dir_path( __FILE__ ) ); } /** * Get the template path. * @return string */ public function template_path() { return apply_filters( 'woocommerce_template_path', 'woocommerce/' ); } /** * Get Ajax URL. * @return string */ public function ajax_url() { return admin_url( 'admin-ajax.php', 'relative' ); } /** * Return the WC API URL for a given request * * @param string $request * @param mixed $ssl (default: null) * @return string */ public function api_request_url( $request, $ssl = null ) { if ( is_null( $ssl ) ) { $scheme = parse_url( home_url(), PHP_URL_SCHEME ); } elseif ( $ssl ) { $scheme = 'https'; } else { $scheme = 'http'; } if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) { $api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) ); } elseif ( get_option( 'permalink_structure' ) ) { $api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) ); } else { $api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) ); } return esc_url_raw( $api_request_url ); } /** * Load & enqueue active webhooks * * @since 2.2 */ private function load_webhooks() { $args = array( 'fields' => 'ids', 'post_type' => 'shop_webhook', 'post_status' => 'publish', ); $query = new WP_Query( $args ); if ( ! empty( $query->posts ) ) { foreach ( $query->posts as $id ) { $webhook = new WC_Webhook( $id ); $webhook->enqueue(); } } } /** * Get Checkout Class. * @return WC_Checkout */ public function checkout() { return WC_Checkout::instance(); } /** * Get gateways class * @return WC_Payment_Gateways */ public function payment_gateways() { return WC_Payment_Gateways::instance(); } /** * Get shipping class * @return WC_Shipping */ public function shipping() { return WC_Shipping::instance(); } /** * Email Class. * @return WC_Emails */ public function mailer() { return WC_Emails::instance(); } } endif; /** * Returns the main instance of WC to prevent the need to use globals. * * @since 2.1 * @return WooCommerce */ function WC() { return WooCommerce::instance(); } // Global for backwards compatibility. $GLOBALS['woocommerce'] = WC();