woocommerce/woocommerce.php

1451 lines
54 KiB
PHP
Raw Normal View History

2011-08-10 17:11:11 +00:00
<?php
2012-03-20 13:22:35 +00:00
/**
* Plugin Name: WooCommerce
* Plugin URI: http://www.woothemes.com/woocommerce/
* Description: An e-commerce toolkit that helps you sell anything. Beautifully.
2012-06-10 19:12:06 +00:00
* Version: 1.5.7
2012-03-20 13:22:35 +00:00
* Author: WooThemes
* Author URI: http://woothemes.com
2012-03-30 15:10:14 +00:00
* Requires at least: 3.3
2012-05-10 11:45:14 +00:00
* Tested up to: 3.4
2012-03-20 13:22:35 +00:00
*
* Text Domain: woocommerce
* Domain Path: /languages/
*
* @package WooCommerce
* @category Core
* @author WooThemes
*/
2012-05-24 11:20:11 +00:00
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2011-08-10 17:11:11 +00:00
2012-05-24 11:20:11 +00:00
if ( ! class_exists( 'Woocommerce' ) ) {
2011-12-08 12:50:50 +00:00
2011-09-04 00:02:44 +00:00
/**
* Main WooCommerce Class
*
* Contains the main functions for WooCommerce, stores variables, and handles error messages
*
* @since WooCommerce 1.4
*/
class Woocommerce {
2011-08-10 17:11:11 +00:00
/** Version ***************************************************************/
2011-08-10 17:11:11 +00:00
2012-06-10 19:12:06 +00:00
var $version = '1.5.7';
2011-09-04 11:03:57 +00:00
/** URLS ******************************************************************/
var $plugin_url;
var $plugin_path;
var $template_url;
/** Errors / Messages *****************************************************/
var $errors = array(); // Stores store errors
var $messages = array(); // Stores store messages
/** Class Instances *******************************************************/
var $query;
var $customer;
var $shipping;
var $cart;
var $payment_gateways;
var $countries;
var $woocommerce_email;
2012-01-12 00:54:45 +00:00
var $checkout;
2012-04-30 19:50:35 +00:00
var $integrations;
/** Taxonomies ************************************************************/
var $attribute_taxonomies; // Stores the attribute taxonomies used in the store
2011-12-02 11:11:33 +00:00
/** Body Classes **********************************************************/
private $_body_classes = array();
/** Inline JavaScript *****************************************************/
private $_inline_js = '';
2011-12-02 11:11:33 +00:00
/**
* WooCommerce Constructor
*
* Gets things started
*/
function __construct() {
2011-12-11 15:20:34 +00:00
// Start a PHP session
2012-03-20 13:22:35 +00:00
if ( ! session_id() ) session_start();
2011-12-11 15:20:34 +00:00
2012-02-01 21:49:08 +00:00
// Define version constant
define( 'WOOCOMMERCE_VERSION', $this->version );
// Include required files
$this->includes();
// Installation
2012-05-24 11:20:11 +00:00
if ( is_admin() && ! defined('DOING_AJAX') ) $this->install();
2012-05-08 15:23:09 +00:00
// Actions
2012-03-20 19:22:10 +00:00
add_action( 'init', array( &$this, 'init' ), 0 );
add_action( 'init', array( &$this, 'include_template_functions' ), 25 );
add_action( 'after_setup_theme', array( &$this, 'compatibility' ) );
2012-03-20 18:59:12 +00:00
// Loaded action
do_action( 'woocommerce_loaded' );
2011-10-03 08:14:07 +00:00
}
/**
* Include required core files
**/
function includes() {
2012-03-20 13:22:35 +00:00
if ( is_admin() ) $this->admin_includes();
if ( defined('DOING_AJAX') ) $this->ajax_includes();
if ( ! is_admin() || defined('DOING_AJAX') ) $this->frontend_includes();
2012-03-20 13:22:35 +00:00
include( 'woocommerce-core-functions.php' ); // Contains core functions for the front/back end
include( 'widgets/widget-init.php' ); // Widget classes
2012-01-27 16:38:39 +00:00
include( 'classes/class-wc-countries.php' ); // Defines countries and states
include( 'classes/class-wc-order.php' ); // Single order class
include( 'classes/class-wc-product.php' ); // Product class
include( 'classes/class-wc-product-variation.php' ); // Product variation class
include( 'classes/class-wc-tax.php' ); // Tax class
2012-04-30 19:50:35 +00:00
include( 'classes/class-wc-settings-api.php' ); // Settings API
2012-04-30 19:50:35 +00:00
// Include Core Payment Gateways
2012-01-27 16:38:39 +00:00
include( 'classes/gateways/class-wc-payment-gateways.php' );
include( 'classes/gateways/class-wc-payment-gateway.php' );
2012-06-08 10:46:10 +00:00
include( 'classes/gateways/bacs/class-wc-bacs.php' );
include( 'classes/gateways/cheque/class-wc-cheque.php' );
include( 'classes/gateways/paypal/class-wc-paypal.php' );
include( 'classes/gateways/cod/class-wc-cod.php' );
include( 'classes/gateways/mijireh/class-wc-mijireh-checkout.php' );
2012-04-30 19:50:35 +00:00
// Include Core Shipping Methods
2012-01-27 16:38:39 +00:00
include( 'classes/shipping/class-wc-shipping.php' );
include( 'classes/shipping/class-wc-shipping-method.php' );
include( 'classes/shipping/class-wc-flat-rate.php' );
2012-01-31 18:13:32 +00:00
include( 'classes/shipping/class-wc-international-delivery.php' );
2012-01-27 16:38:39 +00:00
include( 'classes/shipping/class-wc-free-shipping.php' );
include( 'classes/shipping/class-wc-local-delivery.php' );
include( 'classes/shipping/class-wc-local-pickup.php' );
2012-04-30 19:50:35 +00:00
// Include Core Integrations
include( 'classes/integrations/class-wc-integration.php' );
include( 'classes/integrations/class-wc-integrations.php' );
include( 'classes/integrations/google-analytics/class-wc-google-analytics.php' );
include( 'classes/integrations/sharethis/class-wc-sharethis.php' );
include( 'classes/integrations/sharedaddy/class-wc-sharedaddy.php' );
include( 'classes/integrations/shareyourcart/class-wc-shareyourcart.php' );
2012-05-08 15:23:09 +00:00
}
/**
* Include required admin files
**/
function admin_includes() {
2011-12-12 16:34:56 +00:00
include( 'admin/woocommerce-admin-init.php' ); // Admin section
2011-10-03 08:14:07 +00:00
}
/**
* Include required ajax files
**/
function ajax_includes() {
2011-12-11 00:33:28 +00:00
include( 'woocommerce-ajax.php' ); // Ajax functions for admin and the front-end
2011-10-03 08:14:07 +00:00
}
/**
* Include required frontend files
**/
function frontend_includes() {
2011-12-11 00:33:28 +00:00
include( 'woocommerce-hooks.php' ); // Template hooks used on the front-end
include( 'woocommerce-functions.php' ); // Contains functions for various front-end events
2012-03-20 13:22:35 +00:00
include( 'shortcodes/shortcode-init.php' ); // Init the shortcodes
include( 'classes/class-wc-query.php' ); // The main store queries
include( 'classes/class-wc-cart.php' ); // The main cart class
include( 'classes/class-wc-coupon.php' ); // Coupon class
include( 'classes/class-wc-customer.php' ); // Customer class
2011-10-03 08:14:07 +00:00
}
/**
* Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes
**/
function include_template_functions() {
2011-12-11 00:33:28 +00:00
include( 'woocommerce-template.php' );
}
/**
* Install upon activation
**/
function install() {
register_activation_hook( __FILE__, 'activate_woocommerce' );
register_activation_hook( __FILE__, 'flush_rewrite_rules' );
2012-03-20 13:22:35 +00:00
if ( get_option('woocommerce_db_version') != $this->version )
2012-03-20 19:22:10 +00:00
add_action( 'init', 'install_woocommerce', 1 );
2011-08-10 17:11:11 +00:00
}
/**
* Init WooCommerce when WordPress Initialises
**/
function init() {
// Set up localisation
$this->load_plugin_textdomain();
// Variables
$this->template_url = apply_filters( 'woocommerce_template_url', 'woocommerce/' );
// Load class instances
2012-03-20 19:22:10 +00:00
$this->payment_gateways = new WC_Payment_gateways(); // Payment gateways. Loads and stores payment methods
$this->shipping = new WC_Shipping(); // Shipping class. loads and stores shipping methods
$this->countries = new WC_Countries(); // Countries class
2012-04-30 19:50:35 +00:00
$this->integrations = new WC_Integrations(); // Integrations class
2012-03-20 19:22:10 +00:00
2012-04-30 19:50:35 +00:00
// Init shipping, payment gateways, and integrations
$this->shipping->init();
$this->payment_gateways->init();
2012-04-30 19:50:35 +00:00
$this->integrations->init();
// Classes/actions loaded for the frontend and for ajax requests
if ( ! is_admin() || defined('DOING_AJAX') ) {
// Class instances
$this->cart = new WC_Cart(); // Cart class, stores the cart contents
$this->customer = new WC_Customer(); // Customer class, sorts out session data such as location
$this->query = new WC_Query(); // Query class, handles front-end queries and loops
// Load messages
$this->load_messages();
// Hooks
add_filter( 'template_include', array(&$this, 'template_loader') );
add_filter( 'comments_template', array(&$this, 'comments_template_loader') );
add_filter( 'wp_redirect', array(&$this, 'redirect'), 1, 2 );
add_action( 'get_header', array(&$this, 'buffer_checkout') );
add_action( 'wp_enqueue_scripts', array(&$this, 'frontend_scripts') );
add_action( 'wp_head', array(&$this, 'generator') );
add_action( 'wp_head', array(&$this, 'wp_head') );
add_filter( 'body_class', array(&$this, 'output_body_class') );
add_action( 'wp_footer', array(&$this, 'output_inline_js'), 25 );
}
2012-03-20 14:08:29 +00:00
// Actions
add_action( 'the_post', array( &$this, 'setup_product_data' ) );
add_action( 'admin_footer', array( &$this, 'output_inline_js' ), 25 );
2012-03-20 14:08:29 +00:00
// Email Actions
$email_actions = array( 'woocommerce_low_stock', 'woocommerce_no_stock', 'woocommerce_product_on_backorder', 'woocommerce_order_status_pending_to_processing', 'woocommerce_order_status_pending_to_completed', 'woocommerce_order_status_pending_to_on-hold', 'woocommerce_order_status_failed_to_processing', 'woocommerce_order_status_failed_to_completed', 'woocommerce_order_status_pending_to_processing', 'woocommerce_order_status_pending_to_on-hold', 'woocommerce_order_status_completed', 'woocommerce_new_customer_note' );
foreach ( $email_actions as $action ) add_action( $action, array( &$this, 'send_transactional_email') );
2012-03-20 14:08:29 +00:00
// Actions for SSL
if ( ! is_admin() || defined('DOING_AJAX') ) {
add_action( 'get_header', array( &$this, 'ssl_redirect' ) );
2012-03-20 14:08:29 +00:00
$filters = array( 'post_thumbnail_html', 'widget_text', 'wp_get_attachment_url', 'wp_get_attachment_image_attributes', 'wp_get_attachment_url', 'option_siteurl', 'option_homeurl', 'option_home', 'option_url', 'option_wpurl', 'option_stylesheet_url', 'option_template_url', 'script_loader_src', 'style_loader_src', 'template_directory_uri', 'stylesheet_directory_uri', 'site_url' );
2012-05-24 11:20:11 +00:00
foreach ( $filters as $filter )
add_filter( $filter, array( &$this, 'force_ssl') );
}
2012-03-20 14:08:29 +00:00
// Register globals for WC environment
$this->register_globals();
// Init user roles
$this->init_user_roles();
// Init WooCommerce taxonomies
$this->init_taxonomy();
// Init Images sizes
$this->init_image_sizes();
// Init styles
2012-03-20 13:22:35 +00:00
if ( ! is_admin() ) $this->init_styles();
// Trigger API requests
$this->api_requests();
// Init action
do_action( 'woocommerce_init' );
}
2012-01-24 16:56:10 +00:00
/**
* API request - Trigger any API requests (handy for third party plugins/gateways)
**/
function api_requests() {
2012-03-20 13:22:35 +00:00
if ( isset( $_GET['wc-api'] ) ) {
$api = strtolower( esc_attr( $_GET['wc-api'] ) );
do_action( 'woocommerce_api_' . $api );
}
}
2012-02-15 20:42:00 +00:00
/**
* Localisation
**/
function load_plugin_textdomain() {
// Note: the first-loaded translation file overrides any following ones if the same translation is present
$locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce' );
$variable_lang = ( get_option( 'woocommerce_informal_localisation_type' ) == 'yes' ) ? 'informal' : 'formal';
load_textdomain( 'woocommerce', WP_LANG_DIR.'/woocommerce/woocommerce-'.$locale.'.mo' );
load_plugin_textdomain( 'woocommerce', false, dirname( plugin_basename( __FILE__ ) ).'/languages/'.$variable_lang );
load_plugin_textdomain( 'woocommerce', false, dirname( plugin_basename( __FILE__ ) ).'/languages' );
2012-02-15 20:42:00 +00:00
}
2012-03-20 18:59:12 +00:00
/**
* template_loader
*
* Handles template usage so that we can use our own templates instead of the themes.
*
* Templates are in the 'templates' folder. woocommerce looks for theme
* overides in /theme/woocommerce/ by default
*
* For beginners, it also looks for a woocommerce.php template first. If the user adds
* this to the theme (containing a woocommerce() inside) this will be used for all
* woocommerce templates.
*/
function template_loader( $template ) {
$find = array( 'woocommerce.php' );
$file = '';
if ( is_single() && get_post_type() == 'product' ) {
$file = 'single-product.php';
$find[] = $file;
$find[] = $this->template_url . $file;
} elseif ( is_tax('product_cat') || is_tax('product_tag') ) {
$term = get_queried_object();
$file = 'taxonomy-' . $term->taxonomy . '.php';
$find[] = 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php';
$find[] = $this->template_url . 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php';
$find[] = $file;
$find[] = $this->template_url . $file;
2012-06-14 09:20:49 +00:00
} elseif ( is_post_type_archive('product') || is_page( woocommerce_get_page_id('shop') ) ) {
2012-03-20 18:59:12 +00:00
$file = 'archive-product.php';
$find[] = $file;
$find[] = $this->template_url . $file;
}
if ( $file ) {
$template = locate_template( $find );
if ( ! $template ) $template = $this->plugin_path() . '/templates/' . $file;
}
return $template;
}
function comments_template_loader( $template ) {
if( get_post_type() !== 'product' ) return $template;
if (file_exists( STYLESHEETPATH . '/' . $this->template_url . 'single-product-reviews.php' ))
return STYLESHEETPATH . '/' . $this->template_url . 'single-product-reviews.php';
else
return $this->plugin_path() . '/templates/single-product-reviews.php';
}
2012-01-24 16:56:10 +00:00
/**
* Output buffering on the checkout allows gateways to do header redirects
**/
function buffer_checkout() {
2012-03-20 13:22:35 +00:00
if ( is_checkout() ) ob_start();
2012-01-24 16:56:10 +00:00
}
/**
* Register WC environment globals
**/
function register_globals() {
$GLOBALS['product'] = null;
}
/**
* When the_post is called, get product data too
**/
function setup_product_data( $post ) {
2012-03-27 12:20:47 +00:00
if ( is_int( $post ) ) $post = get_post( $post );
2012-03-20 13:22:35 +00:00
if ( $post->post_type !== 'product' ) return;
unset( $GLOBALS['product'] );
2012-01-27 16:38:39 +00:00
$GLOBALS['product'] = new WC_Product( $post->ID );
2012-01-03 17:23:42 +00:00
return $GLOBALS['product'];
}
/**
* Add Compatibility for various bits
**/
function compatibility() {
// Post thumbnail support
2012-03-20 13:22:35 +00:00
if ( ! current_theme_supports( 'post-thumbnails' ) ) {
add_theme_support( 'post-thumbnails' );
remove_post_type_support( 'post', 'thumbnail' );
remove_post_type_support( 'page', 'thumbnail' );
2012-03-20 13:22:35 +00:00
} else {
add_post_type_support( 'product', 'thumbnail' );
2012-03-20 13:22:35 +00:00
}
// IIS
2012-03-20 13:22:35 +00:00
if ( ! isset($_SERVER['REQUEST_URI'] ) ) {
$_SERVER['REQUEST_URI'] = substr( $_SERVER['PHP_SELF'], 1 );
if ( isset( $_SERVER['QUERY_STRING'] ) )
$_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING'];
}
2012-04-23 12:41:11 +00:00
// NGINX Proxy
if ( ! isset( $_SERVER['REMOTE_ADDR'] ) && isset( $_SERVER['HTTP_REMOTE_ADDR'] ) )
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_REMOTE_ADDR'];
if ( ! isset( $_SERVER['HTTPS'] ) && ! empty( $_SERVER['HTTP_HTTPS'] ) )
$_SERVER['HTTPS'] = $_SERVER['HTTP_HTTPS'];
}
/**
* Redirect to https if Force SSL is enabled
**/
function ssl_redirect() {
2012-03-20 13:22:35 +00:00
if ( get_option('woocommerce_force_ssl_checkout') == 'no' ) return;
2012-02-27 11:18:55 +00:00
2012-03-20 13:22:35 +00:00
if ( ! is_ssl() ) {
if ( is_checkout() ) {
2012-03-12 08:34:49 +00:00
wp_redirect( str_replace('http:', 'https:', get_permalink(woocommerce_get_page_id('checkout'))), 301 );
exit;
2012-03-20 13:22:35 +00:00
} elseif ( is_account_page() ) {
2012-03-12 08:34:49 +00:00
if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
2012-03-20 13:22:35 +00:00
wp_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) );
2012-03-12 08:34:49 +00:00
exit;
} else {
2012-03-20 13:22:35 +00:00
wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
2012-03-12 08:34:49 +00:00
exit;
}
exit;
}
} else {
// Break out of SSL if we leave the checkout/my accounts (anywhere but thanks)
2012-03-20 13:22:35 +00:00
if ( get_option('woocommerce_unforce_ssl_checkout') == 'yes' && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_page( woocommerce_get_page_id('thanks') ) && ! is_ajax() && ! is_account_page() ) {
2012-03-12 08:34:49 +00:00
if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
2012-03-20 13:22:35 +00:00
wp_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) );
2012-03-12 08:34:49 +00:00
exit;
} else {
2012-03-20 13:22:35 +00:00
wp_redirect( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
2012-03-12 08:34:49 +00:00
exit;
}
}
}
}
/**
2012-02-27 13:14:33 +00:00
* Output generator to aid debugging
**/
2012-02-27 13:14:33 +00:00
function generator() {
echo "\n\n" . '<!-- WooCommerce Version -->' . "\n" . '<meta name="generator" content="WooCommerce ' . $this->version . '" />' . "\n\n";
2012-02-27 13:14:33 +00:00
}
/**
* Add body classes
**/
function wp_head() {
$theme_name = ( function_exists( 'wp_get_theme' ) ) ? wp_get_theme() : get_current_theme();
$this->add_body_class( "theme-{$theme_name}" );
2012-03-20 13:22:35 +00:00
if ( is_woocommerce() ) $this->add_body_class('woocommerce');
2012-03-20 13:22:35 +00:00
if ( is_checkout() ) $this->add_body_class('woocommerce-checkout');
2011-08-10 17:11:11 +00:00
2012-03-20 13:22:35 +00:00
if ( is_cart() ) $this->add_body_class('woocommerce-cart');
2012-03-20 13:22:35 +00:00
if ( is_account_page() ) $this->add_body_class('woocommerce-account');
2012-03-20 13:22:35 +00:00
if ( is_woocommerce() || is_checkout() || is_cart() || is_account_page() || is_page( woocommerce_get_page_id('order_tracking') ) || is_page( woocommerce_get_page_id('thanks') ) ) $this->add_body_class('woocommerce-page');
2011-08-10 17:11:11 +00:00
}
/**
* Init WooCommerce user roles
**/
function init_user_roles() {
global $wp_roles;
2012-03-20 13:22:35 +00:00
if ( class_exists('WP_Roles') ) if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles();
2012-03-20 13:22:35 +00:00
if ( is_object($wp_roles) ) {
2011-08-10 17:11:11 +00:00
// Customer role
2012-03-20 13:22:35 +00:00
add_role( 'customer', __('Customer', 'woocommerce'), array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false
2012-03-20 13:22:35 +00:00
) );
// Shop manager role
2012-03-20 13:22:35 +00:00
add_role( 'shop_manager', __('Shop Manager', 'woocommerce'), array(
'read' => true,
'read_private_pages' => true,
'read_private_posts' => true,
'edit_posts' => true,
'edit_pages' => true,
'edit_published_posts' => true,
'edit_published_pages' => true,
'edit_private_pages' => true,
'edit_private_posts' => true,
'edit_others_posts' => true,
'edit_others_pages' => true,
'publish_posts' => true,
'publish_pages' => true,
'delete_posts' => true,
'delete_pages' => true,
'delete_private_pages' => true,
'delete_private_posts' => true,
'delete_published_pages' => true,
'delete_published_posts' => true,
'delete_others_posts' => true,
'delete_others_pages' => true,
'manage_categories' => true,
'manage_links' => true,
'moderate_comments' => true,
'unfiltered_html' => true,
'upload_files' => true,
'export' => true,
'import' => true,
'manage_woocommerce' => true,
'manage_woocommerce_orders' => true,
'manage_woocommerce_coupons' => true,
'manage_woocommerce_products' => true,
'view_woocommerce_reports' => true
2012-03-20 13:22:35 +00:00
) );
// Main Shop capabilities for admin
$wp_roles->add_cap( 'administrator', 'manage_woocommerce' );
$wp_roles->add_cap( 'administrator', 'manage_woocommerce_orders' );
$wp_roles->add_cap( 'administrator', 'manage_woocommerce_coupons' );
$wp_roles->add_cap( 'administrator', 'manage_woocommerce_products' );
$wp_roles->add_cap( 'administrator', 'view_woocommerce_reports' );
2012-03-20 13:22:35 +00:00
}
}
/**
* Init WooCommerce taxonomies
**/
function init_taxonomy() {
2012-03-20 13:22:35 +00:00
if ( post_type_exists('product') ) return;
/**
* Slugs
**/
2012-01-06 17:14:31 +00:00
$shop_page_id = woocommerce_get_page_id('shop');
2012-03-20 13:22:35 +00:00
$base_slug = ( $shop_page_id > 0 && get_page( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : 'shop';
2012-03-20 13:22:35 +00:00
$category_base = ( get_option('woocommerce_prepend_shop_page_to_urls') == "yes" ) ? trailingslashit($base_slug) : '';
2012-03-20 13:22:35 +00:00
$category_slug = ( get_option('woocommerce_product_category_slug') ) ? get_option('woocommerce_product_category_slug') : _x('product-category', 'slug', 'woocommerce');
2012-03-20 13:22:35 +00:00
$tag_slug = ( get_option('woocommerce_product_tag_slug') ) ? get_option('woocommerce_product_tag_slug') : _x('product-tag', 'slug', 'woocommerce');
2012-03-20 13:22:35 +00:00
$product_base = ( get_option('woocommerce_prepend_shop_page_to_products') == 'yes' ) ? trailingslashit($base_slug) : trailingslashit(_x('product', 'slug', 'woocommerce'));
2012-03-20 13:22:35 +00:00
if ( get_option('woocommerce_prepend_category_to_products') == 'yes' ) $product_base .= trailingslashit('%product_cat%');
$product_base = untrailingslashit($product_base);
2012-03-20 13:22:35 +00:00
if ( current_user_can('manage_woocommerce') ) $show_in_menu = 'woocommerce'; else $show_in_menu = true;
/**
* Taxonomies
**/
2012-03-20 13:22:35 +00:00
$admin_only_query_var = ( is_admin() ) ? true : false;
register_taxonomy( 'product_type',
array('product'),
array(
'hierarchical' => false,
2012-05-08 23:53:23 +00:00
'update_count_callback' => '_update_post_term_count',
'show_ui' => false,
'show_in_nav_menus' => false,
'query_var' => $admin_only_query_var,
'rewrite' => false
)
);
register_taxonomy( 'product_cat',
array('product'),
array(
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
2012-01-05 11:31:22 +00:00
'label' => __( 'Product Categories', 'woocommerce'),
'labels' => array(
2012-01-05 11:31:22 +00:00
'name' => __( 'Product Categories', 'woocommerce'),
'singular_name' => __( 'Product Category', 'woocommerce'),
'menu_name' => _x( 'Product Categories', 'Admin menu name', 'woocommerce' ),
2012-01-05 11:31:22 +00:00
'search_items' => __( 'Search Product Categories', 'woocommerce'),
'all_items' => __( 'All Product Categories', 'woocommerce'),
'parent_item' => __( 'Parent Product Category', 'woocommerce'),
'parent_item_colon' => __( 'Parent Product Category:', 'woocommerce'),
'edit_item' => __( 'Edit Product Category', 'woocommerce'),
'update_item' => __( 'Update Product Category', 'woocommerce'),
'add_new_item' => __( 'Add New Product Category', 'woocommerce'),
'new_item_name' => __( 'New Product Category Name', 'woocommerce')
),
'show_ui' => true,
'query_var' => true,
'capabilities' => array(
'manage_terms' => 'manage_woocommerce_products',
'edit_terms' => 'manage_woocommerce_products',
'delete_terms' => 'manage_woocommerce_products',
'assign_terms' => 'manage_woocommerce_products',
),
2012-02-21 11:55:57 +00:00
'rewrite' => array( 'slug' => $category_base . $category_slug, 'with_front' => false, 'hierarchical' => true ),
)
);
register_taxonomy( 'product_tag',
array('product'),
array(
'hierarchical' => false,
2012-04-18 11:36:16 +00:00
'update_count_callback' => '_update_post_term_count',
2012-01-05 11:31:22 +00:00
'label' => __( 'Product Tags', 'woocommerce'),
'labels' => array(
2012-02-20 18:19:27 +00:00
'name' => __( 'Tags', 'woocommerce'),
2012-01-05 11:31:22 +00:00
'singular_name' => __( 'Product Tag', 'woocommerce'),
'menu_name' => _x( 'Tags', 'Admin menu name', 'woocommerce' ),
2012-01-05 11:31:22 +00:00
'search_items' => __( 'Search Product Tags', 'woocommerce'),
'all_items' => __( 'All Product Tags', 'woocommerce'),
'parent_item' => __( 'Parent Product Tag', 'woocommerce'),
'parent_item_colon' => __( 'Parent Product Tag:', 'woocommerce'),
'edit_item' => __( 'Edit Product Tag', 'woocommerce'),
'update_item' => __( 'Update Product Tag', 'woocommerce'),
'add_new_item' => __( 'Add New Product Tag', 'woocommerce'),
'new_item_name' => __( 'New Product Tag Name', 'woocommerce')
),
'show_ui' => true,
'query_var' => true,
'capabilities' => array(
'manage_terms' => 'manage_woocommerce_products',
'edit_terms' => 'manage_woocommerce_products',
'delete_terms' => 'manage_woocommerce_products',
'assign_terms' => 'manage_woocommerce_products',
),
'rewrite' => array( 'slug' => $category_base . $tag_slug, 'with_front' => false ),
)
);
register_taxonomy( 'product_shipping_class',
array('product', 'product_variation'),
array(
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
2012-01-05 11:31:22 +00:00
'label' => __( 'Shipping Classes', 'woocommerce'),
'labels' => array(
2012-01-05 11:31:22 +00:00
'name' => __( 'Shipping Classes', 'woocommerce'),
'singular_name' => __( 'Shipping Class', 'woocommerce'),
'menu_name' => _x( 'Shipping Classes', 'Admin menu name', 'woocommerce' ),
2012-01-05 11:31:22 +00:00
'search_items' => __( 'Search Shipping Classes', 'woocommerce'),
'all_items' => __( 'All Shipping Classes', 'woocommerce'),
'parent_item' => __( 'Parent Shipping Class', 'woocommerce'),
'parent_item_colon' => __( 'Parent Shipping Class:', 'woocommerce'),
'edit_item' => __( 'Edit Shipping Class', 'woocommerce'),
'update_item' => __( 'Update Shipping Class', 'woocommerce'),
'add_new_item' => __( 'Add New Shipping Class', 'woocommerce'),
'new_item_name' => __( 'New Shipping Class Name', 'woocommerce')
),
'show_ui' => true,
'show_in_nav_menus' => false,
'query_var' => $admin_only_query_var,
'capabilities' => array(
'manage_terms' => 'manage_woocommerce_products',
'edit_terms' => 'manage_woocommerce_products',
'delete_terms' => 'manage_woocommerce_products',
'assign_terms' => 'manage_woocommerce_products',
),
'rewrite' => false,
)
);
register_taxonomy( 'shop_order_status',
array('shop_order'),
array(
'hierarchical' => false,
'update_count_callback' => '_update_post_term_count',
'labels' => array(
2012-01-05 11:31:22 +00:00
'name' => __( 'Order statuses', 'woocommerce'),
'singular_name' => __( 'Order status', 'woocommerce'),
'search_items' => __( 'Search Order statuses', 'woocommerce'),
2012-06-06 20:35:40 +00:00
'all_items' => __( 'All Order statuses', 'woocommerce'),
2012-01-05 11:31:22 +00:00
'parent_item' => __( 'Parent Order status', 'woocommerce'),
'parent_item_colon' => __( 'Parent Order status:', 'woocommerce'),
'edit_item' => __( 'Edit Order status', 'woocommerce'),
'update_item' => __( 'Update Order status', 'woocommerce'),
'add_new_item' => __( 'Add New Order status', 'woocommerce'),
'new_item_name' => __( 'New Order status Name', 'woocommerce')
),
'show_ui' => false,
'show_in_nav_menus' => false,
'query_var' => $admin_only_query_var,
'rewrite' => false,
)
);
$attribute_taxonomies = $this->get_attribute_taxonomies();
2012-03-20 13:22:35 +00:00
if ( $attribute_taxonomies ) {
foreach ($attribute_taxonomies as $tax) {
$name = $this->attribute_taxonomy_name($tax->attribute_name);
$hierarchical = true;
2012-03-20 13:22:35 +00:00
if ($name) {
$label = ( isset( $tax->attribute_label ) && $tax->attribute_label ) ? $tax->attribute_label : $tax->attribute_name;
2012-03-04 00:01:56 +00:00
$show_in_nav_menus = apply_filters('woocommerce_attribute_show_in_nav_menus', false, $name);
register_taxonomy( $name,
array('product'),
array(
'hierarchical' => $hierarchical,
2012-05-08 23:53:23 +00:00
'update_count_callback' => '_update_post_term_count',
'labels' => array(
'name' => $label,
'singular_name' => $label,
2012-01-05 11:31:22 +00:00
'search_items' => __( 'Search', 'woocommerce') . ' ' . $label,
'all_items' => __( 'All', 'woocommerce') . ' ' . $label,
'parent_item' => __( 'Parent', 'woocommerce') . ' ' . $label,
'parent_item_colon' => __( 'Parent', 'woocommerce') . ' ' . $label . ':',
'edit_item' => __( 'Edit', 'woocommerce') . ' ' . $label,
'update_item' => __( 'Update', 'woocommerce') . ' ' . $label,
'add_new_item' => __( 'Add New', 'woocommerce') . ' ' . $label,
'new_item_name' => __( 'New', 'woocommerce') . ' ' . $label
),
'show_ui' => false,
'query_var' => true,
'show_in_nav_menus' => $show_in_nav_menus,
'rewrite' => array( 'slug' => $category_base . strtolower(sanitize_title($tax->attribute_name)), 'with_front' => false, 'hierarchical' => $hierarchical ),
)
);
2012-03-20 13:22:35 +00:00
}
}
}
/**
* Post Types
**/
register_post_type( "product",
array(
'labels' => array(
2012-01-05 11:31:22 +00:00
'name' => __( 'Products', 'woocommerce' ),
'singular_name' => __( 'Product', 'woocommerce' ),
'menu_name' => _x( 'Products', 'Admin menu name', 'woocommerce' ),
2012-01-05 11:31:22 +00:00
'add_new' => __( 'Add Product', 'woocommerce' ),
'add_new_item' => __( 'Add New Product', 'woocommerce' ),
'edit' => __( 'Edit', 'woocommerce' ),
'edit_item' => __( 'Edit Product', 'woocommerce' ),
'new_item' => __( 'New Product', 'woocommerce' ),
'view' => __( 'View Product', 'woocommerce' ),
'view_item' => __( 'View Product', 'woocommerce' ),
'search_items' => __( 'Search Products', 'woocommerce' ),
'not_found' => __( 'No Products found', 'woocommerce' ),
'not_found_in_trash' => __( 'No Products found in trash', 'woocommerce' ),
'parent' => __( 'Parent Product', 'woocommerce' )
),
2012-01-05 11:31:22 +00:00
'description' => __( 'This is where you can add new products to your store.', 'woocommerce' ),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'capabilities' => array(
'publish_posts' => 'manage_woocommerce_products',
'edit_posts' => 'manage_woocommerce_products',
'edit_others_posts' => 'manage_woocommerce_products',
'delete_posts' => 'manage_woocommerce_products',
'delete_others_posts' => 'manage_woocommerce_products',
'read_private_posts' => 'manage_woocommerce_products',
'edit_post' => 'manage_woocommerce_products',
'delete_post' => 'manage_woocommerce_products',
'read_post' => 'manage_woocommerce_products'
),
'publicly_queryable' => true,
'exclude_from_search' => false,
'hierarchical' => false, // Hierarcal causes memory issues - WP loads all records!
2011-12-14 23:32:37 +00:00
'rewrite' => array( 'slug' => $product_base, 'with_front' => false, 'feeds' => $base_slug ),
'query_var' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'custom-fields', 'page-attributes' ),
'has_archive' => $base_slug,
'show_in_nav_menus' => true
)
);
register_post_type( "product_variation",
array(
'labels' => array(
2012-01-05 11:31:22 +00:00
'name' => __( 'Variations', 'woocommerce' ),
'singular_name' => __( 'Variation', 'woocommerce' ),
'add_new' => __( 'Add Variation', 'woocommerce' ),
'add_new_item' => __( 'Add New Variation', 'woocommerce' ),
'edit' => __( 'Edit', 'woocommerce' ),
'edit_item' => __( 'Edit Variation', 'woocommerce' ),
'new_item' => __( 'New Variation', 'woocommerce' ),
'view' => __( 'View Variation', 'woocommerce' ),
'view_item' => __( 'View Variation', 'woocommerce' ),
'search_items' => __( 'Search Variations', 'woocommerce' ),
'not_found' => __( 'No Variations found', 'woocommerce' ),
'not_found_in_trash' => __( 'No Variations found in trash', 'woocommerce' ),
'parent' => __( 'Parent Variation', 'woocommerce' )
),
'public' => true,
'show_ui' => false,
'capability_type' => 'post',
'capabilities' => array(
'publish_posts' => 'manage_woocommerce_products',
'edit_posts' => 'manage_woocommerce_products',
'edit_others_posts' => 'manage_woocommerce_products',
'delete_posts' => 'manage_woocommerce_products',
'delete_others_posts' => 'manage_woocommerce_products',
'read_private_posts' => 'manage_woocommerce_products',
'edit_post' => 'manage_woocommerce_products',
'delete_post' => 'manage_woocommerce_products',
'read_post' => 'manage_woocommerce_products'
),
'publicly_queryable' => true,
'exclude_from_search' => true,
'hierarchical' => false,
'rewrite' => false,
'query_var' => true,
'supports' => array( 'title', 'editor', 'custom-fields', 'page-attributes', 'thumbnail' ),
'show_in_nav_menus' => false
)
);
2012-05-28 09:19:29 +00:00
if ( false === ( $order_count = get_transient( 'woocommerce_processing_order_count' ) ) ) {
$order_statuses = get_terms( 'shop_order_status' );
$order_count = false;
foreach ( $order_statuses as $status ) {
if( $status->slug === 'processing' ) {
$order_count += $status->count;
break;
}
}
$order_count = apply_filters( 'woocommerce_admin_menu_count', intval( $order_count ) );
2012-05-28 09:19:29 +00:00
set_transient( 'woocommerce_processing_order_count', $order_count );
}
$menu_name = _x('Orders', 'Admin menu name', 'woocommerce');
if ( $order_count ) {
$menu_name .= " <span class='awaiting-mod count-$order_count'><span class='processing-count'>" . number_format_i18n( $order_count ) . "</span></span>" ;
}
register_post_type( "shop_order",
array(
'labels' => array(
2012-01-05 11:31:22 +00:00
'name' => __( 'Orders', 'woocommerce' ),
'singular_name' => __( 'Order', 'woocommerce' ),
'add_new' => __( 'Add Order', 'woocommerce' ),
'add_new_item' => __( 'Add New Order', 'woocommerce' ),
'edit' => __( 'Edit', 'woocommerce' ),
'edit_item' => __( 'Edit Order', 'woocommerce' ),
'new_item' => __( 'New Order', 'woocommerce' ),
'view' => __( 'View Order', 'woocommerce' ),
'view_item' => __( 'View Order', 'woocommerce' ),
'search_items' => __( 'Search Orders', 'woocommerce' ),
'not_found' => __( 'No Orders found', 'woocommerce' ),
'not_found_in_trash' => __( 'No Orders found in trash', 'woocommerce' ),
2012-05-28 09:19:29 +00:00
'parent' => __( 'Parent Orders', 'woocommerce' ),
'menu_name' => $menu_name
),
2012-01-05 11:31:22 +00:00
'description' => __( 'This is where store orders are stored.', 'woocommerce' ),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'capabilities' => array(
'publish_posts' => 'manage_woocommerce_orders',
'edit_posts' => 'manage_woocommerce_orders',
'edit_others_posts' => 'manage_woocommerce_orders',
'delete_posts' => 'manage_woocommerce_orders',
'delete_others_posts' => 'manage_woocommerce_orders',
'read_private_posts' => 'manage_woocommerce_orders',
'edit_post' => 'manage_woocommerce_orders',
'delete_post' => 'manage_woocommerce_orders',
'read_post' => 'manage_woocommerce_orders'
),
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_in_menu' => $show_in_menu,
'hierarchical' => false,
'show_in_nav_menus' => false,
'rewrite' => false,
'query_var' => true,
'supports' => array( 'title', 'comments', 'custom-fields' ),
2012-05-28 09:19:29 +00:00
'has_archive' => false,
)
);
2011-08-10 17:11:11 +00:00
register_post_type( "shop_coupon",
array(
'labels' => array(
2012-01-05 11:31:22 +00:00
'name' => __( 'Coupons', 'woocommerce' ),
'singular_name' => __( 'Coupon', 'woocommerce' ),
'menu_name' => _x( 'Coupons', 'Admin menu name', 'woocommerce' ),
2012-01-05 11:31:22 +00:00
'add_new' => __( 'Add Coupon', 'woocommerce' ),
'add_new_item' => __( 'Add New Coupon', 'woocommerce' ),
'edit' => __( 'Edit', 'woocommerce' ),
'edit_item' => __( 'Edit Coupon', 'woocommerce' ),
'new_item' => __( 'New Coupon', 'woocommerce' ),
'view' => __( 'View Coupons', 'woocommerce' ),
'view_item' => __( 'View Coupon', 'woocommerce' ),
'search_items' => __( 'Search Coupons', 'woocommerce' ),
'not_found' => __( 'No Coupons found', 'woocommerce' ),
'not_found_in_trash' => __( 'No Coupons found in trash', 'woocommerce' ),
'parent' => __( 'Parent Coupon', 'woocommerce' )
),
2012-01-05 11:31:22 +00:00
'description' => __( 'This is where you can add new coupons that customers can use in your store.', 'woocommerce' ),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'capabilities' => array(
'publish_posts' => 'manage_woocommerce_coupons',
'edit_posts' => 'manage_woocommerce_coupons',
'edit_others_posts' => 'manage_woocommerce_coupons',
'delete_posts' => 'manage_woocommerce_coupons',
'delete_others_posts' => 'manage_woocommerce_coupons',
'read_private_posts' => 'manage_woocommerce_coupons',
'edit_post' => 'manage_woocommerce_coupons',
'delete_post' => 'manage_woocommerce_coupons',
'read_post' => 'manage_woocommerce_coupons'
),
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_in_menu' => $show_in_menu,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title' ),
'show_in_nav_menus' => false
)
);
}
2011-08-10 17:11:11 +00:00
/**
* Init images
*/
function init_image_sizes() {
// Image sizes
$shop_thumbnail_crop = (get_option('woocommerce_thumbnail_image_crop')==1) ? true : false;
$shop_catalog_crop = (get_option('woocommerce_catalog_image_crop')==1) ? true : false;
$shop_single_crop = (get_option('woocommerce_single_image_crop')==1) ? true : false;
2011-08-10 17:11:11 +00:00
add_image_size( 'shop_thumbnail', $this->get_image_size('shop_thumbnail_image_width'), $this->get_image_size('shop_thumbnail_image_height'), $shop_thumbnail_crop );
add_image_size( 'shop_catalog', $this->get_image_size('shop_catalog_image_width'), $this->get_image_size('shop_catalog_image_height'), $shop_catalog_crop );
add_image_size( 'shop_single', $this->get_image_size('shop_single_image_width'), $this->get_image_size('shop_single_image_height'), $shop_single_crop );
}
/**
* Init frontend CSS
*/
function init_styles() {
2012-03-20 13:22:35 +00:00
$chosen_en = ( get_option('woocommerce_enable_chosen') == 'yes' ) ? true : false;
$lightbox_en = ( get_option('woocommerce_enable_lightbox') == 'yes' ) ? true : false;
// Optional front end css
2012-03-20 13:22:35 +00:00
if ( ( defined('WOOCOMMERCE_USE_CSS') && WOOCOMMERCE_USE_CSS ) || ( ! defined('WOOCOMMERCE_USE_CSS') && get_option('woocommerce_frontend_css') == 'yes') ) {
$css = file_exists( get_stylesheet_directory() . '/woocommerce/style.css' ) ? get_stylesheet_directory_uri() . '/woocommerce/style.css' : $this->plugin_url() . '/assets/css/woocommerce.css';
wp_register_style( 'woocommerce_frontend_styles', $css );
wp_enqueue_style( 'woocommerce_frontend_styles' );
2012-03-20 13:22:35 +00:00
}
if ($lightbox_en) wp_enqueue_style( 'woocommerce_fancybox_styles', $this->plugin_url() . '/assets/css/fancybox.css' );
if ($chosen_en) wp_enqueue_style( 'woocommerce_chosen_styles', $this->plugin_url() . '/assets/css/chosen.css' );
}
/**
* Register/queue frontend scripts
*/
function frontend_scripts() {
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
2012-03-20 13:22:35 +00:00
$lightbox_en = ( get_option('woocommerce_enable_lightbox') == 'yes' ) ? true : false;
$chosen_en = ( get_option('woocommerce_enable_chosen') == 'yes' ) ? true : false;
$jquery_ui_en = ( get_option('woocommerce_enable_jquery_ui') == 'yes' ) ? true : false;
$scripts_position = ( get_option('woocommerce_scripts_position') == 'yes' ) ? true : false;
2012-01-07 15:36:12 +00:00
// Woocommerce.min.js is minified and contains woocommerce_plugins
wp_enqueue_script( 'woocommerce', $this->plugin_url() . '/assets/js/woocommerce'.$suffix.'.js', array('jquery'), '1.0', $scripts_position );
2012-03-20 13:22:35 +00:00
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG )
wp_enqueue_script( 'woocommerce_plugins', $this->plugin_url() . '/assets/js/woocommerce_plugins.js', array('jquery'), '1.0', $scripts_position );
2012-01-07 15:36:12 +00:00
if ($lightbox_en)
wp_enqueue_script( 'fancybox', $this->plugin_url() . '/assets/js/fancybox'.$suffix.'.js', array('jquery'), '1.0', $scripts_position );
// Chosen.jquery.min.js is minified and contains the frontend code for chosen selects
2012-03-20 13:22:35 +00:00
if ( $chosen_en && is_checkout() ) {
2012-01-07 15:36:12 +00:00
wp_enqueue_script( 'chosen', $this->plugin_url() . '/assets/js/chosen.jquery'.$suffix.'.js', array('jquery'), '1.0' );
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
wp_enqueue_script( 'chosen_frontend', $this->plugin_url() . '/assets/js/chosen.frontend.js', array('chosen'), '1.0', $scripts_position );
}
}
2012-03-20 13:22:35 +00:00
if ($jquery_ui_en) {
2012-01-07 15:36:12 +00:00
wp_enqueue_script( 'jqueryui', $this->plugin_url() . '/assets/js/jquery-ui'.$suffix.'.js', array('jquery'), '1.0', $scripts_position );
wp_enqueue_script( 'wc_price_slider', $this->plugin_url() . '/assets/js/price_slider'.$suffix.'.js', array('jqueryui'), '1.0', $scripts_position );
$woocommerce_price_slider_params = array(
2012-01-07 15:36:12 +00:00
'currency_symbol' => get_woocommerce_currency_symbol(),
'currency_pos' => get_option('woocommerce_currency_pos'),
'min_price' => isset($_SESSION['min_price']) ? $_SESSION['min_price'] : '',
'max_price' => isset($_SESSION['max_price']) ? $_SESSION['max_price'] : ''
);
wp_localize_script( 'wc_price_slider', 'woocommerce_price_slider_params', $woocommerce_price_slider_params );
2012-03-20 13:22:35 +00:00
}
/* Script variables */
$states = json_encode( $this->countries->states );
$woocommerce_params = array(
'countries' => $states,
2012-01-05 11:31:22 +00:00
'select_state_text' => __('Select an option&hellip;', 'woocommerce'),
2012-02-23 11:19:45 +00:00
'required_text' => esc_attr__( 'required', 'woocommerce' ),
2012-03-12 15:58:13 +00:00
'required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
'review_rating_required' => get_option('woocommerce_review_rating_required'),
'plugin_url' => $this->plugin_url(),
2012-03-29 00:50:20 +00:00
'ajax_url' => $this->ajax_url(),
'get_variation_nonce' => wp_create_nonce("get-variation"),
'add_to_cart_nonce' => wp_create_nonce("add-to-cart"),
'update_order_review_nonce' => wp_create_nonce("update-order-review"),
'update_shipping_method_nonce' => wp_create_nonce("update-shipping-method"),
2012-06-10 11:40:07 +00:00
'apply_coupon_nonce' => wp_create_nonce("apply-coupon"),
'option_guest_checkout' => get_option('woocommerce_enable_guest_checkout'),
2012-03-29 00:50:20 +00:00
'checkout_url' => add_query_arg( 'action', 'woocommerce-checkout', $this->ajax_url() ),
2012-01-07 15:36:12 +00:00
'option_ajax_add_to_cart' => get_option('woocommerce_enable_ajax_add_to_cart'),
'is_checkout' => ( is_page(woocommerce_get_page_id('checkout')) ) ? 1 : 0,
'is_pay_page' => ( is_page(woocommerce_get_page_id('pay')) ) ? 1 : 0,
'is_cart' => ( is_cart() ) ? 1 : 0
);
2012-03-20 13:22:35 +00:00
if ( is_checkout() || is_cart() )
$woocommerce_params['locale'] = json_encode( $this->countries->get_country_locale() );
2012-01-06 17:14:31 +00:00
wp_localize_script( 'woocommerce', 'woocommerce_params', apply_filters('woocommerce_params', $woocommerce_params) );
}
2011-12-09 21:55:49 +00:00
/** Load Instances on demand **********************************************/
2011-12-09 21:55:49 +00:00
/**
* Get Checkout Class
2011-12-09 21:55:49 +00:00
*/
function checkout() {
2012-03-20 13:22:35 +00:00
if ( ! class_exists('WC_Checkout') ) {
2012-01-27 16:38:39 +00:00
include( 'classes/class-wc-checkout.php' );
$this->checkout = new WC_Checkout();
2012-03-20 13:22:35 +00:00
}
2012-01-12 00:54:45 +00:00
return $this->checkout;
2011-12-09 21:55:49 +00:00
}
2011-12-09 21:55:49 +00:00
/**
* Get Logging Class
2011-12-09 21:55:49 +00:00
*/
function logger() {
2012-03-20 13:22:35 +00:00
if ( ! class_exists('WC_Logger') ) include( 'classes/class-wc-logger.php' );
2012-01-27 16:38:39 +00:00
return new WC_Logger();
2011-12-09 21:55:49 +00:00
}
/**
* Get Validation Class
*/
function validation() {
2012-03-20 13:22:35 +00:00
if ( ! class_exists('WC_Validation') ) include( 'classes/class-wc-validation.php' );
2012-01-27 16:38:39 +00:00
return new WC_Validation();
}
/**
* Email Class
*/
function send_transactional_email( $args = array() ) {
$this->mailer();
do_action( current_filter() . '_notification' , $args );
}
function mailer() {
// Init mail class
2012-03-20 13:22:35 +00:00
if ( ! class_exists('WC_Email') ) {
2012-01-27 16:38:39 +00:00
include( 'classes/class-wc-email.php' );
$this->woocommerce_email = new WC_Email();
2012-03-20 13:22:35 +00:00
}
return $this->woocommerce_email;
}
2011-12-09 21:55:49 +00:00
/** Helper functions ******************************************************/
2011-12-09 21:55:49 +00:00
/**
* Get the plugin url
*/
function plugin_url() {
2012-03-20 13:22:35 +00:00
if ( $this->plugin_url ) return $this->plugin_url;
return $this->plugin_url = plugins_url( basename( plugin_dir_path(__FILE__) ), basename( __FILE__ ) );
2011-12-09 21:55:49 +00:00
}
/**
* Get the plugin path
*/
function plugin_path() {
2012-03-20 13:22:35 +00:00
if ( $this->plugin_path ) return $this->plugin_path;
2012-04-11 10:02:35 +00:00
return $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
2012-03-29 00:50:20 +00:00
}
/**
2012-03-29 09:32:19 +00:00
* Ajax URL
2012-03-29 00:50:20 +00:00
*/
function ajax_url() {
2012-04-24 17:32:17 +00:00
return str_replace( array('https:', 'http:'), '', admin_url( 'admin-ajax.php' ) );
2012-03-29 00:50:20 +00:00
}
2011-12-09 21:55:49 +00:00
/**
* Return the URL with https if SSL is on
*/
function force_ssl( $content ) {
2012-03-20 13:22:35 +00:00
if ( is_ssl() ) {
if ( is_array($content) )
$content = array_map( array( &$this, 'force_ssl' ) , $content );
else
$content = str_replace( 'http:', 'https:', $content );
}
2011-12-09 21:55:49 +00:00
return $content;
}
/**
* Get an image size
*
* Variable is filtered by woocommerce_get_image_size_{image_size}
*/
function get_image_size( $image_size ) {
$return = '';
2012-03-20 13:22:35 +00:00
switch ( $image_size ) {
2011-12-09 21:55:49 +00:00
case "shop_thumbnail_image_width" : $return = get_option('woocommerce_thumbnail_image_width'); break;
case "shop_thumbnail_image_height" : $return = get_option('woocommerce_thumbnail_image_height'); break;
case "shop_catalog_image_width" : $return = get_option('woocommerce_catalog_image_width'); break;
case "shop_catalog_image_height" : $return = get_option('woocommerce_catalog_image_height'); break;
case "shop_single_image_width" : $return = get_option('woocommerce_single_image_width'); break;
case "shop_single_image_height" : $return = get_option('woocommerce_single_image_height'); break;
2012-03-20 13:22:35 +00:00
}
2011-12-09 21:55:49 +00:00
return apply_filters( 'woocommerce_get_image_size_'.$image_size, $return );
}
/** Messages ****************************************************************/
/**
* Load Messages
*/
function load_messages() {
2012-03-20 13:22:35 +00:00
if ( isset( $_SESSION['errors'] ) ) $this->errors = $_SESSION['errors'];
if ( isset( $_SESSION['messages'] ) ) $this->messages = $_SESSION['messages'];
2011-10-06 10:46:19 +00:00
2012-03-20 13:22:35 +00:00
unset( $_SESSION['messages'] );
unset( $_SESSION['errors'] );
2012-02-22 16:36:23 +00:00
// Load errors from querystring
2012-03-20 13:22:35 +00:00
if ( isset( $_GET['wc_error'] ) ) {
2012-02-22 16:36:23 +00:00
$this->add_error( esc_attr( $_GET['wc_error'] ) );
}
2011-12-09 21:55:49 +00:00
}
2011-12-09 21:55:49 +00:00
/**
* Add an error
*/
2012-06-06 19:28:15 +00:00
function add_error( $error ) { $this->errors[] = apply_filters( 'woocommerce_add_error', $error ); }
2011-12-09 21:55:49 +00:00
/**
* Add a message
*/
2012-06-06 19:28:15 +00:00
function add_message( $message ) { $this->messages[] = apply_filters( 'woocommerce_add_message', $message ); }
2011-12-09 21:55:49 +00:00
/** Clear messages and errors from the session data */
function clear_messages() {
$this->errors = $this->messages = array();
2012-03-20 13:22:35 +00:00
unset( $_SESSION['messages'], $_SESSION['errors'] );
2011-12-09 21:55:49 +00:00
}
/**
* Get error count
*/
function error_count() { return sizeof($this->errors); }
/**
* Get message count
*/
function message_count() { return sizeof($this->messages); }
2012-02-08 15:20:32 +00:00
/**
* Get errors
*/
function get_errors() { return (array) $this->errors; }
/**
* Get messages
*/
function get_messages() { return (array) $this->messages; }
2011-12-09 21:55:49 +00:00
/**
* Output the errors and messages
*/
2012-02-08 15:20:32 +00:00
function show_messages() { woocommerce_show_messages(); }
2011-12-09 21:55:49 +00:00
/**
* Set session data for messages
*/
function set_messages() {
$_SESSION['errors'] = $this->errors;
$_SESSION['messages'] = $this->messages;
}
2011-12-09 21:55:49 +00:00
/**
* Redirection hook which stores messages into session data
*/
function redirect( $location, $status ) {
global $is_IIS;
2011-12-20 13:44:18 +00:00
$this->set_messages();
2011-12-20 13:44:18 +00:00
// IIS fix
2012-03-20 13:22:35 +00:00
if ( $is_IIS ) session_write_close();
2011-12-20 13:44:18 +00:00
2012-03-20 13:22:35 +00:00
return apply_filters( 'woocommerce_redirect', $location );
2011-12-09 21:55:49 +00:00
}
2011-12-09 21:55:49 +00:00
/** Attribute Helpers ****************************************************************/
/**
* Get attribute taxonomies
*/
function get_attribute_taxonomies() {
global $wpdb;
2012-03-20 13:22:35 +00:00
if ( ! $this->attribute_taxonomies )
2011-12-09 21:55:49 +00:00
$this->attribute_taxonomies = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."woocommerce_attribute_taxonomies;");
return $this->attribute_taxonomies;
}
/**
* Get a product attributes name
*/
function attribute_taxonomy_name( $name ) {
return 'pa_'.sanitize_title($name);
}
2011-12-09 21:55:49 +00:00
/**
* Get a product attributes label
*/
function attribute_label( $name ) {
global $wpdb;
2012-03-20 13:22:35 +00:00
if ( strstr( $name, 'pa_' ) ) {
2011-12-09 21:55:49 +00:00
$name = str_replace( 'pa_', '', sanitize_title( $name ) );
2012-03-23 21:06:49 +00:00
$label = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_label FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) );
2012-03-23 21:06:49 +00:00
if ( ! $label ) $label = ucfirst( $name );
2012-03-20 13:22:35 +00:00
} else {
$label = $name;
2012-03-20 13:22:35 +00:00
}
2012-03-23 21:06:49 +00:00
return apply_filters( 'woocommerce_attribute_label', $label, $name );
2011-12-09 21:55:49 +00:00
}
2012-03-05 14:43:55 +00:00
/**
* Get an array of product attribute taxonomies
*/
function get_attribute_taxonomy_names() {
$taxonomy_names = array();
$attribute_taxonomies = $this->get_attribute_taxonomies();
if ( $attribute_taxonomies ) {
2012-03-20 13:22:35 +00:00
foreach ( $attribute_taxonomies as $tax ) {
$taxonomy_names[] = $this->attribute_taxonomy_name( strtolower( sanitize_title( $tax->attribute_name ) ) );
2012-03-05 14:43:55 +00:00
}
}
return $taxonomy_names;
}
2011-12-09 21:55:49 +00:00
/** Coupon Helpers ********************************************************/
2011-10-06 10:46:19 +00:00
2011-12-09 21:55:49 +00:00
/**
* Get coupon types
*/
function get_coupon_discount_types() {
2012-03-20 13:22:35 +00:00
if ( ! isset($this->coupon_discount_types ) ) {
$this->coupon_discount_types = apply_filters( 'woocommerce_coupon_discount_types', array(
2012-01-05 11:31:22 +00:00
'fixed_cart' => __('Cart Discount', 'woocommerce'),
'percent' => __('Cart % Discount', 'woocommerce'),
'fixed_product' => __('Product Discount', 'woocommerce'),
'percent_product' => __('Product % Discount', 'woocommerce')
2012-03-20 13:22:35 +00:00
) );
}
2011-12-09 21:55:49 +00:00
return $this->coupon_discount_types;
}
/**
* Get a coupon type's name
*/
function get_coupon_discount_type( $type = '' ) {
$types = (array) $this->get_coupon_discount_types();
2012-03-20 13:22:35 +00:00
if ( isset( $types[$type] ) ) return $types[$type];
2011-12-09 21:55:49 +00:00
}
/** Nonces ****************************************************************/
/**
* Return a nonce field
*/
2012-03-20 13:22:35 +00:00
function nonce_field ( $action, $referer = true , $echo = true ) { return wp_nonce_field('woocommerce-' . $action, '_n', $referer, $echo ); }
/**
* Return a url with a nonce appended
*/
2012-03-20 13:22:35 +00:00
function nonce_url ( $action, $url = '' ) { return add_query_arg( '_n', wp_create_nonce( 'woocommerce-' . $action ), $url ); }
/**
* Check a nonce and sets woocommerce error in case it is invalid
* To fail silently, set the error_message to an empty string
*
* @param string $name the nonce name
* @param string $action then nonce action
* @param string $method the http request method _POST, _GET or _REQUEST
* @param string $error_message custom error message, or false for default message, or an empty string to fail silently
*
* @return bool
*/
2012-03-20 13:22:35 +00:00
function verify_nonce( $action, $method='_POST', $error_message = false ) {
2011-10-06 10:46:19 +00:00
$name = '_n';
$action = 'woocommerce-' . $action;
2011-10-06 10:46:19 +00:00
2012-03-20 13:22:35 +00:00
if ( $error_message === false ) $error_message = __('Action failed. Please refresh the page and retry.', 'woocommerce');
2012-03-20 13:22:35 +00:00
if ( ! in_array( $method, array( '_GET', '_POST', '_REQUEST' ) ) ) $method = '_POST';
2012-03-20 13:22:35 +00:00
if ( isset($_REQUEST[$name] ) && wp_verify_nonce( $_REQUEST[$name], $action ) ) return true;
2012-03-20 13:22:35 +00:00
if ( $error_message ) $this->add_error( $error_message );
return false;
}
2012-04-11 20:07:46 +00:00
/** Shortcode Helpers *********************************************************/
/**
2012-04-11 20:07:46 +00:00
* Shortcode Wrapper
*/
2012-04-11 20:07:46 +00:00
function shortcode_wrapper( $function, $atts = array() ) {
ob_start();
2012-03-20 13:22:35 +00:00
call_user_func( $function, $atts );
2012-04-11 20:07:46 +00:00
return ob_get_clean();
}
2012-04-11 20:07:46 +00:00
/** Cache Helpers *********************************************************/
2012-02-24 21:05:15 +00:00
/**
* nocache
*
* Sets a constant preventing some caching plugins from caching a page. Used on dynamic pages
*/
function nocache() {
2012-03-20 13:22:35 +00:00
if ( ! defined('DONOTCACHEPAGE') ) define("DONOTCACHEPAGE", "true"); // WP Super Cache constant
2012-02-24 21:05:15 +00:00
}
/**
* cookie_cart_has_contents
*
* Sets a cookie when the cart has something in it. Can be used by hosts to prevent caching if set.
*/
function cart_has_contents_cookie( $set ) {
2012-03-20 13:22:35 +00:00
if ( ! headers_sent() ) {
2012-02-24 21:05:15 +00:00
if ($set)
2012-03-20 13:22:35 +00:00
setcookie( "woocommerce_items_in_cart", "1", 0, COOKIEPATH, COOKIE_DOMAIN, false );
2012-02-24 21:05:15 +00:00
else
2012-03-20 13:22:35 +00:00
setcookie( "woocommerce_items_in_cart", "0", time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false );
2012-02-24 21:05:15 +00:00
}
}
/** Transients ************************************************************/
/**
* Clear Product Transients
*/
function clear_product_transients( $post_id = 0 ) {
global $wpdb;
2012-03-16 16:39:16 +00:00
delete_transient('wc_products_onsale');
delete_transient('wc_hidden_product_ids');
delete_transient('wc_hidden_product_ids_search');
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` IN ('wc_products_onsale', 'wc_hidden_product_ids', 'wc_hidden_product_ids_search')");
2012-03-16 16:39:16 +00:00
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_uf_pid_%')");
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_ln_count_%')");
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_ship_%')");
2012-03-20 13:22:35 +00:00
if ($post_id>0) {
$post_id = (int) $post_id;
2012-03-16 16:39:16 +00:00
delete_transient('wc_product_total_stock_'.$post_id);
delete_transient('wc_product_children_ids_'.$post_id);
delete_transient('wc_average_rating_'.$post_id);
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` IN
(
'_transient_wc_product_children_ids_$post_id',
'_transient_wc_product_total_stock_$post_id',
2012-05-26 15:31:58 +00:00
'_transient_wc_average_rating_$post_id',
2012-06-05 11:21:52 +00:00
'_transient_wc_product_type_$post_id'
2012-03-16 16:39:16 +00:00
)");
2012-03-20 13:22:35 +00:00
} else {
2012-03-16 16:39:16 +00:00
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_product_children_ids_%')");
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_product_total_stock_%')");
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_average_rating_%')");
2012-05-26 15:31:58 +00:00
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_product_type_%')");
2012-03-20 13:22:35 +00:00
}
2012-04-24 17:22:18 +00:00
wp_cache_flush();
}
/** Body Classes **********************************************************/
function add_body_class( $class ) {
$this->_body_classes[] = sanitize_html_class( strtolower($class) );
}
function output_body_class( $classes ) {
2012-03-20 13:22:35 +00:00
if ( sizeof( $this->_body_classes ) > 0 ) $classes = array_merge( $classes, $this->_body_classes );
2012-03-20 13:22:35 +00:00
if ( is_singular('product') ) {
$key = array_search( 'singular', $classes );
if ( $key !== false ) unset( $classes[$key] );
}
return $classes;
}
/** Inline JavaScript Helper **********************************************/
function add_inline_js( $code ) {
$this->_inline_js .= "\n" . $code . "\n";
}
function output_inline_js() {
2012-03-20 13:22:35 +00:00
if ($this->_inline_js) {
echo "<!-- WooCommerce JavaScript-->\n<script type=\"text/javascript\">\njQuery(document).ready(function($) {";
echo $this->_inline_js;
echo "});\n</script>\n";
2011-12-15 16:36:11 +00:00
$this->_inline_js = '';
2012-03-20 13:22:35 +00:00
}
}
}
2011-11-06 15:45:22 +00:00
/**
* Init woocommerce class
*/
$GLOBALS['woocommerce'] = new Woocommerce();
2011-11-06 15:45:22 +00:00
} // class_exists check