1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22
23 if ( ! class_exists( 'WooCommerce' ) ) :
24
25 26 27 28 29 30
31 final class WooCommerce {
32
33 34 35
36 public $version = '2.1.10';
37
38 39 40 41
42 protected static $_instance = null;
43
44 45 46
47 public $session = null;
48
49 50 51
52 public $query = null;
53
54 55 56
57 public $product_factory = null;
58
59 60 61
62 public $countries = null;
63
64 65 66
67 public $integrations = null;
68
69 70 71
72 public $cart = null;
73
74 75 76
77 public $customer = null;
78
79 80 81 82 83 84 85 86 87 88
89 public static function instance() {
90 if ( is_null( self::$_instance ) ) {
91 self::$_instance = new self();
92 }
93 return self::$_instance;
94 }
95
96 97 98 99 100
101 public function __clone() {
102 _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' );
103 }
104
105 106 107 108 109
110 public function __wakeup() {
111 _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' );
112 }
113
114 115 116 117 118
119 public function __construct() {
120
121 if ( function_exists( "__autoload" ) ) {
122 spl_autoload_register( "__autoload" );
123 }
124
125 spl_autoload_register( array( $this, 'autoload' ) );
126
127
128 $this->define_constants();
129
130
131 $this->includes();
132
133
134 $this->api = new WC_API();
135
136
137 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'action_links' ) );
138 add_action( 'widgets_init', array( $this, 'include_widgets' ) );
139 add_action( 'init', array( $this, 'init' ), 0 );
140 add_action( 'init', array( $this, 'include_template_functions' ) );
141 add_action( 'init', array( 'WC_Shortcodes', 'init' ) );
142 add_action( 'after_setup_theme', array( $this, 'setup_environment' ) );
143
144
145 do_action( 'woocommerce_loaded' );
146 }
147
148 149 150 151 152 153
154 public function __get( $key ) {
155 if ( method_exists( $this, $key ) ) {
156 return $this->$key();
157 }
158 else switch( $key ) {
159 case 'template_url':
160 _deprecated_argument( 'Woocommerce->template_url', '2.1', 'WC_TEMPLATE_PATH constant' );
161 return WC_TEMPLATE_PATH;
162 case 'messages':
163 _deprecated_argument( 'Woocommerce->messages', '2.1', 'Use wc_get_notices' );
164 return wc_get_notices( 'success' );
165 case 'errors':
166 _deprecated_argument( 'Woocommerce->errors', '2.1', 'Use wc_get_notices' );
167 return wc_get_notices( 'error' );
168 default:
169 return false;
170 }
171 }
172
173 174 175 176 177 178
179 public function action_links( $links ) {
180 return array_merge( array(
181 '<a href="' . admin_url( 'admin.php?page=wc-settings' ) . '">' . __( 'Settings', 'woocommerce' ) . '</a>',
182 '<a href="' . esc_url( apply_filters( 'woocommerce_docs_url', 'http://docs.woothemes.com/documentation/plugins/woocommerce/', 'woocommerce' ) ) . '">' . __( 'Docs', 'woocommerce' ) . '</a>',
183 '<a href="' . esc_url( apply_filters( 'woocommerce_support_url', 'http://support.woothemes.com/' ) ) . '">' . __( 'Premium Support', 'woocommerce' ) . '</a>',
184 ), $links );
185 }
186
187 188 189 190 191 192
193 public function autoload( $class ) {
194 $path = null;
195 $class = strtolower( $class );
196 $file = 'class-' . str_replace( '_', '-', $class ) . '.php';
197
198 if ( strpos( $class, 'wc_gateway_' ) === 0 ) {
199 $path = $this->plugin_path() . '/includes/gateways/' . trailingslashit( substr( str_replace( '_', '-', $class ), 11 ) );
200 } elseif ( strpos( $class, 'wc_shipping_' ) === 0 ) {
201 $path = $this->plugin_path() . '/includes/shipping/' . trailingslashit( substr( str_replace( '_', '-', $class ), 12 ) );
202 } elseif ( strpos( $class, 'wc_shortcode_' ) === 0 ) {
203 $path = $this->plugin_path() . '/includes/shortcodes/';
204 } elseif ( strpos( $class, 'wc_meta_box' ) === 0 ) {
205 $path = $this->plugin_path() . '/includes/admin/post-types/meta-boxes/';
206 } elseif ( strpos( $class, 'wc_admin' ) === 0 ) {
207 $path = $this->plugin_path() . '/includes/admin/';
208 }
209
210 if ( $path && is_readable( $path . $file ) ) {
211 include_once( $path . $file );
212 return;
213 }
214
215
216 if ( strpos( $class, 'wc_' ) === 0 ) {
217 $path = $this->plugin_path() . '/includes/';
218 }
219
220 if ( $path && is_readable( $path . $file ) ) {
221 include_once( $path . $file );
222 return;
223 }
224 }
225
226 227 228
229 private function define_constants() {
230 define( 'WC_PLUGIN_FILE', __FILE__ );
231 define( 'WC_VERSION', $this->version );
232 define( 'WOOCOMMERCE_VERSION', WC_VERSION );
233
234 if ( ! defined( 'WC_TEMPLATE_PATH' ) ) {
235 define( 'WC_TEMPLATE_PATH', $this->template_path() );
236 }
237
238 if ( ! defined( 'WC_ROUNDING_PRECISION' ) ) {
239 define( 'WC_ROUNDING_PRECISION', 4 );
240 }
241
242
243 if ( ! defined( 'WC_TAX_ROUNDING_MODE' ) ) {
244 define( 'WC_TAX_ROUNDING_MODE', get_option( 'woocommerce_prices_include_tax' ) === 'yes' ? 2 : 1 );
245 }
246
247 if ( ! defined( 'WC_DELIMITER' ) ) {
248 define( 'WC_DELIMITER', '|' );
249 }
250 }
251
252 253 254
255 private function includes() {
256 include_once( 'includes/wc-core-functions.php' );
257 include_once( 'includes/class-wc-install.php' );
258 include_once( 'includes/class-wc-download-handler.php' );
259 include_once( 'includes/class-wc-comments.php' );
260 include_once( 'includes/class-wc-post-data.php' );
261 include_once( 'includes/abstracts/abstract-wc-session.php' );
262 include_once( 'includes/class-wc-session-handler.php' );
263
264 if ( is_admin() ) {
265 include_once( 'includes/admin/class-wc-admin.php' );
266 }
267
268 if ( defined( 'DOING_AJAX' ) ) {
269 $this->ajax_includes();
270 }
271
272 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
273 $this->frontend_includes();
274 }
275
276
277 $this->query = include( 'includes/class-wc-query.php' );
278
279
280 include_once( 'includes/class-wc-post-types.php' );
281
282
283 include_once( 'includes/class-wc-api.php' );
284
285
286 include_once( 'includes/abstracts/abstract-wc-product.php' );
287 include_once( 'includes/abstracts/abstract-wc-settings-api.php' );
288 include_once( 'includes/abstracts/abstract-wc-shipping-method.php' );
289 include_once( 'includes/abstracts/abstract-wc-payment-gateway.php' );
290 include_once( 'includes/abstracts/abstract-wc-integration.php' );
291
292
293 include_once( 'includes/class-wc-product-factory.php' );
294 include_once( 'includes/class-wc-countries.php' );
295 include_once( 'includes/class-wc-integrations.php' );
296 include_once( 'includes/class-wc-cache-helper.php' );
297 include_once( 'includes/class-wc-https.php' );
298
299
300 include_once( 'includes/wc-template-hooks.php' );
301 }
302
303 304 305
306 public function ajax_includes() {
307 include_once( 'includes/class-wc-ajax.php' );
308 }
309
310 311 312
313 public function frontend_includes() {
314 include_once( 'includes/class-wc-template-loader.php' );
315 include_once( 'includes/class-wc-frontend-scripts.php' );
316 include_once( 'includes/class-wc-form-handler.php' );
317 include_once( 'includes/class-wc-cart.php' );
318 include_once( 'includes/class-wc-tax.php' );
319 include_once( 'includes/class-wc-customer.php' );
320 include_once( 'includes/class-wc-shortcodes.php' );
321 }
322
323 324 325
326 public function include_template_functions() {
327 include_once( 'includes/wc-template-functions.php' );
328 }
329
330 331 332
333 public function include_widgets() {
334 include_once( 'includes/abstracts/abstract-wc-widget.php' );
335 include_once( 'includes/widgets/class-wc-widget-cart.php' );
336 include_once( 'includes/widgets/class-wc-widget-products.php' );
337 include_once( 'includes/widgets/class-wc-widget-layered-nav.php' );
338 include_once( 'includes/widgets/class-wc-widget-layered-nav-filters.php' );
339 include_once( 'includes/widgets/class-wc-widget-price-filter.php' );
340 include_once( 'includes/widgets/class-wc-widget-product-categories.php' );
341 include_once( 'includes/widgets/class-wc-widget-product-search.php' );
342 include_once( 'includes/widgets/class-wc-widget-product-tag-cloud.php' );
343 include_once( 'includes/widgets/class-wc-widget-recent-reviews.php' );
344 include_once( 'includes/widgets/class-wc-widget-recently-viewed.php' );
345 include_once( 'includes/widgets/class-wc-widget-top-rated-products.php' );
346 }
347
348 349 350
351 public function init() {
352
353 do_action( 'before_woocommerce_init' );
354
355
356 $this->load_plugin_textdomain();
357
358
359 $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
360
361
362 $this->product_factory = new WC_Product_Factory();
363 $this->countries = new WC_Countries();
364 $this->integrations = new WC_Integrations();
365 $this->session = new $session_class();
366
367
368 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
369
370 $this->cart = new WC_Cart();
371 $this->customer = new WC_Customer();
372 }
373
374
375 $email_actions = array(
376 'woocommerce_low_stock',
377 'woocommerce_no_stock',
378 'woocommerce_product_on_backorder',
379 'woocommerce_order_status_pending_to_processing',
380 'woocommerce_order_status_pending_to_completed',
381 'woocommerce_order_status_pending_to_on-hold',
382 'woocommerce_order_status_failed_to_processing',
383 'woocommerce_order_status_failed_to_completed',
384 'woocommerce_order_status_completed',
385 'woocommerce_new_customer_note',
386 'woocommerce_created_customer'
387 );
388
389 foreach ( $email_actions as $action )
390 add_action( $action, array( $this, 'send_transactional_email' ), 10, 10 );
391
392
393 do_action( 'woocommerce_init' );
394 }
395
396 397 398 399 400
401 public function load_plugin_textdomain() {
402 $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce' );
403
404
405 if ( is_admin() ) {
406 load_textdomain( 'woocommerce', WP_LANG_DIR . "/woocommerce/woocommerce-admin-$locale.mo" );
407 load_textdomain( 'woocommerce', dirname( __FILE__ ) . "/i18n/languages/woocommerce-admin-$locale.mo" );
408 }
409
410
411 load_textdomain( 'woocommerce', WP_LANG_DIR . "/woocommerce/woocommerce-$locale.mo" );
412 load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( __FILE__ ) ) . "/i18n/languages" );
413 }
414
415 416 417
418 public function setup_environment() {
419
420 if ( ! current_theme_supports( 'post-thumbnails', 'product' ) ) {
421 add_theme_support( 'post-thumbnails' );
422 remove_post_type_support( 'post', 'thumbnail' );
423 remove_post_type_support( 'page', 'thumbnail' );
424 } else {
425 add_post_type_support( 'product', 'thumbnail' );
426 }
427
428
429 $shop_thumbnail = wc_get_image_size( 'shop_thumbnail' );
430 $shop_catalog = wc_get_image_size( 'shop_catalog' );
431 $shop_single = wc_get_image_size( 'shop_single' );
432
433 add_image_size( 'shop_thumbnail', $shop_thumbnail['width'], $shop_thumbnail['height'], $shop_thumbnail['crop'] );
434 add_image_size( 'shop_catalog', $shop_catalog['width'], $shop_catalog['height'], $shop_catalog['crop'] );
435 add_image_size( 'shop_single', $shop_single['width'], $shop_single['height'], $shop_single['crop'] );
436
437
438 if ( ! isset($_SERVER['REQUEST_URI'] ) ) {
439 $_SERVER['REQUEST_URI'] = substr( $_SERVER['PHP_SELF'], 1 );
440 if ( isset( $_SERVER['QUERY_STRING'] ) ) {
441 $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING'];
442 }
443 }
444
445
446 if ( ! isset( $_SERVER['REMOTE_ADDR'] ) && isset( $_SERVER['HTTP_REMOTE_ADDR'] ) ) {
447 $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_REMOTE_ADDR'];
448 }
449
450 if ( ! isset( $_SERVER['HTTPS'] ) && ! empty( $_SERVER['HTTP_HTTPS'] ) ) {
451 $_SERVER['HTTPS'] = $_SERVER['HTTP_HTTPS'];
452 }
453
454
455 if ( ! isset( $_SERVER['HTTPS'] ) && ! empty( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ) {
456 $_SERVER['HTTPS'] = '1';
457 }
458 }
459
460
461
462 463 464 465 466
467 public function plugin_url() {
468 return untrailingslashit( plugins_url( '/', __FILE__ ) );
469 }
470
471 472 473 474 475
476 public function plugin_path() {
477 return untrailingslashit( plugin_dir_path( __FILE__ ) );
478 }
479
480 481 482 483 484
485 public function template_path() {
486 return apply_filters( 'WC_TEMPLATE_PATH', 'woocommerce/' );
487 }
488
489 490 491 492 493
494 public function ajax_url() {
495 return admin_url( 'admin-ajax.php', 'relative' );
496 }
497
498 499 500 501 502 503 504
505 public function api_request_url( $request, $ssl = null ) {
506 if ( is_null( $ssl ) ) {
507 $scheme = parse_url( get_option( 'home' ), PHP_URL_SCHEME );
508 } elseif ( $ssl ) {
509 $scheme = 'https';
510 } else {
511 $scheme = 'http';
512 }
513
514 if ( get_option('permalink_structure') ) {
515 return esc_url_raw( trailingslashit( home_url( '/wc-api/' . $request, $scheme ) ) );
516 } else {
517 return esc_url_raw( add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) ) );
518 }
519 }
520
521 522 523 524 525
526 public function send_transactional_email() {
527 $this->mailer();
528 $args = func_get_args();
529 do_action_ref_array( current_filter() . '_notification', $args );
530 }
531
532
533
534 535 536 537 538
539 public function checkout() {
540 return WC_Checkout::instance();
541 }
542
543 544 545 546 547
548 public function payment_gateways() {
549 return WC_Payment_Gateways::instance();
550 }
551
552 553 554 555 556
557 public function shipping() {
558 return WC_Shipping::instance();
559 }
560
561 562 563 564 565
566 public function mailer() {
567 return WC_Emails::instance();
568 }
569
570
571
572 573 574 575 576
577 public function get_image_size( $image_size ) {
578 _deprecated_function( 'Woocommerce->get_image_size', '2.1', 'wc_get_image_size()' );
579 return wc_get_image_size( $image_size );
580 }
581
582 583 584 585
586 public function logger() {
587 _deprecated_function( 'Woocommerce->logger', '2.1', 'new WC_Logger()' );
588 return new WC_Logger();
589 }
590
591 592 593 594
595 public function validation() {
596 _deprecated_function( 'Woocommerce->validation', '2.1', 'new WC_Validation()' );
597 return new WC_Validation();
598 }
599
600 601 602 603 604
605 public function setup_product_data( $post ) {
606 _deprecated_function( 'Woocommerce->setup_product_data', '2.1', 'wc_setup_product_data' );
607 return wc_setup_product_data( $post );
608 }
609
610 611 612 613 614
615 public function force_ssl( $content ) {
616 _deprecated_function( 'Woocommerce->force_ssl', '2.1', 'WC_HTTPS::force_https_url' );
617 return WC_HTTPS::force_https_url( $content );
618 }
619
620 621 622 623
624 public function clear_product_transients( $post_id = 0 ) {
625 _deprecated_function( 'Woocommerce->clear_product_transients', '2.1', 'wc_delete_product_transients' );
626 wc_delete_product_transients( $post_id );
627 }
628
629 630 631 632
633 public function add_inline_js( $code ) {
634 _deprecated_function( 'Woocommerce->add_inline_js', '2.1', 'wc_enqueue_js' );
635 wc_enqueue_js( $code );
636 }
637
638 639 640 641 642 643 644
645 public function nonce_field( $action, $referer = true , $echo = true ) {
646 _deprecated_function( 'Woocommerce->nonce_field', '2.1', 'wp_nonce_field' );
647 return wp_nonce_field('woocommerce-' . $action, '_wpnonce', $referer, $echo );
648 }
649
650 651 652 653 654 655
656 public function nonce_url( $action, $url = '' ) {
657 _deprecated_function( 'Woocommerce->nonce_url', '2.1', 'wp_nonce_url' );
658 return wp_nonce_url( $url , 'woocommerce-' . $action );
659 }
660
661 662 663 664 665 666 667
668 public function verify_nonce( $action, $method = '_POST', $error_message = false ) {
669 _deprecated_function( 'Woocommerce->verify_nonce', '2.1', 'wp_verify_nonce' );
670 if ( ! isset( $method[ '_wpnonce' ] ) ) {
671 return false;
672 }
673 return wp_verify_nonce( $method[ '_wpnonce' ], 'woocommerce-' . $action );
674 }
675
676 677 678 679 680 681 682
683 public function shortcode_wrapper( $function, $atts = array(), $wrapper = array( 'class' => 'woocommerce', 'before' => null, 'after' => null ) ) {
684 _deprecated_function( 'Woocommerce->shortcode_wrapper', '2.1', 'WC_Shortcodes::shortcode_wrapper' );
685 return WC_Shortcodes::shortcode_wrapper( $function, $atts, $wrapper );
686 }
687
688 689 690 691
692 public function get_attribute_taxonomies() {
693 _deprecated_function( 'Woocommerce->get_attribute_taxonomies', '2.1', 'wc_get_attribute_taxonomies' );
694 return wc_get_attribute_taxonomies();
695 }
696
697 698 699 700 701
702 public function attribute_taxonomy_name( $name ) {
703 _deprecated_function( 'Woocommerce->attribute_taxonomy_name', '2.1', 'wc_attribute_taxonomy_name' );
704 return wc_attribute_taxonomy_name( $name );
705 }
706
707 708 709 710 711
712 public function attribute_label( $name ) {
713 _deprecated_function( 'Woocommerce->attribute_label', '2.1', 'wc_attribute_label' );
714 return wc_attribute_label( $name );
715 }
716
717 718 719 720 721
722 public function attribute_orderby( $name ) {
723 _deprecated_function( 'Woocommerce->attribute_orderby', '2.1', 'wc_attribute_orderby' );
724 return wc_attribute_orderby( $name );
725 }
726
727 728 729 730
731 public function get_attribute_taxonomy_names() {
732 _deprecated_function( 'Woocommerce->get_attribute_taxonomy_names', '2.1', 'wc_get_attribute_taxonomy_names' );
733 return wc_get_attribute_taxonomy_names();
734 }
735
736 737 738 739
740 public function get_coupon_discount_types() {
741 _deprecated_function( 'Woocommerce->get_coupon_discount_types', '2.1', 'wc_get_coupon_types' );
742 return wc_get_coupon_types();
743 }
744
745 746 747 748 749
750 public function get_coupon_discount_type( $type = '' ) {
751 _deprecated_function( 'Woocommerce->get_coupon_discount_type', '2.1', 'wc_get_coupon_type' );
752 return wc_get_coupon_type( $type );
753 }
754
755 756 757 758
759 public function add_body_class( $class ) {
760 _deprecated_function( 'Woocommerce->add_body_class', '2.1' );
761 }
762
763 764 765 766
767 public function output_body_class( $classes ) {
768 _deprecated_function( 'Woocommerce->output_body_class', '2.1' );
769 }
770
771 772 773 774
775 public function add_error( $error ) {
776 _deprecated_function( 'Woocommerce->add_error', '2.1', 'wc_add_notice' );
777 wc_add_notice( $error, 'error' );
778 }
779
780 781 782 783
784 public function add_message( $message ) {
785 _deprecated_function( 'Woocommerce->add_message', '2.1', 'wc_add_notice' );
786 wc_add_notice( $message );
787 }
788
789 790 791
792 public function clear_messages() {
793 _deprecated_function( 'Woocommerce->clear_messages', '2.1', 'wc_clear_notices' );
794 wc_clear_notices();
795 }
796
797 798 799 800
801 public function error_count() {
802 _deprecated_function( 'Woocommerce->error_count', '2.1', 'wc_notice_count' );
803 return wc_notice_count( 'error' );
804 }
805
806 807 808 809
810 public function message_count() {
811 _deprecated_function( 'Woocommerce->message_count', '2.1', 'wc_notice_count' );
812 return wc_notice_count( 'message' );
813 }
814
815 816 817 818
819 public function get_errors() {
820 _deprecated_function( 'Woocommerce->get_errors', '2.1', 'wc_get_notices( "error" )' );
821 return wc_get_notices( 'error' );
822 }
823
824 825 826 827
828 public function get_messages() {
829 _deprecated_function( 'Woocommerce->get_messages', '2.1', 'wc_get_notices( "success" )' );
830 return wc_get_notices( 'success' );
831 }
832
833 834 835
836 public function show_messages() {
837 _deprecated_function( 'Woocommerce->show_messages', '2.1', 'wc_print_notices()' );
838 wc_print_notices();
839 }
840
841 842 843
844 public function set_messages() {
845 _deprecated_function( 'Woocommerce->set_messages', '2.1' );
846 }
847 }
848
849 endif;
850
851 852 853 854 855 856
857 function WC() {
858 return WooCommerce::instance();
859 }
860
861
862 $GLOBALS['woocommerce'] = WC();
863