2013-08-09 16:11:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WooCommerce Core Functions
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
2013-09-03 12:03:05 +00:00
|
|
|
* General core functions available on both the front-end and admin.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Core
|
|
|
|
* @package WooCommerce/Functions
|
|
|
|
* @version 2.1.0
|
|
|
|
*/
|
|
|
|
|
2014-09-20 19:07:11 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2014-11-20 20:56:24 +00:00
|
|
|
exit;
|
2014-09-20 19:07:11 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// Include core functions (available in both admin and frontend).
|
2013-08-09 16:11:15 +00:00
|
|
|
include( 'wc-conditional-functions.php' );
|
|
|
|
include( 'wc-coupon-functions.php' );
|
2014-04-23 13:18:17 +00:00
|
|
|
include( 'wc-user-functions.php' );
|
2013-08-09 16:11:15 +00:00
|
|
|
include( 'wc-deprecated-functions.php' );
|
|
|
|
include( 'wc-formatting-functions.php' );
|
|
|
|
include( 'wc-order-functions.php' );
|
|
|
|
include( 'wc-page-functions.php' );
|
|
|
|
include( 'wc-product-functions.php' );
|
2016-01-14 19:48:30 +00:00
|
|
|
include( 'wc-account-functions.php' );
|
2013-08-09 16:11:15 +00:00
|
|
|
include( 'wc-term-functions.php' );
|
2013-09-12 13:41:02 +00:00
|
|
|
include( 'wc-attribute-functions.php' );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Filters on data used in admin and frontend.
|
2013-08-09 16:11:15 +00:00
|
|
|
*/
|
2015-02-12 22:50:22 +00:00
|
|
|
add_filter( 'woocommerce_coupon_code', 'html_entity_decode' );
|
2013-08-09 16:11:15 +00:00
|
|
|
add_filter( 'woocommerce_coupon_code', 'sanitize_text_field' );
|
|
|
|
add_filter( 'woocommerce_coupon_code', 'strtolower' ); // Coupons case-insensitive by default
|
|
|
|
add_filter( 'woocommerce_stock_amount', 'intval' ); // Stock amounts are integers by default
|
2015-10-01 15:17:45 +00:00
|
|
|
add_filter( 'woocommerce_shipping_rate_label', 'sanitize_text_field' ); // Shipping rate label
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Short Description (excerpt).
|
2013-08-09 16:11:15 +00:00
|
|
|
*/
|
|
|
|
add_filter( 'woocommerce_short_description', 'wptexturize' );
|
|
|
|
add_filter( 'woocommerce_short_description', 'convert_smilies' );
|
|
|
|
add_filter( 'woocommerce_short_description', 'convert_chars' );
|
|
|
|
add_filter( 'woocommerce_short_description', 'wpautop' );
|
|
|
|
add_filter( 'woocommerce_short_description', 'shortcode_unautop' );
|
|
|
|
add_filter( 'woocommerce_short_description', 'prepend_attachment' );
|
|
|
|
add_filter( 'woocommerce_short_description', 'do_shortcode', 11 ); // AFTER wpautop()
|
|
|
|
|
2014-06-06 14:51:09 +00:00
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Create a new order programmatically.
|
2014-06-06 14:51:09 +00:00
|
|
|
*
|
2014-07-13 01:43:27 +00:00
|
|
|
* Returns a new order object on success which can then be used to add additional data.
|
2014-06-24 18:56:53 +00:00
|
|
|
*
|
2016-01-06 16:11:09 +00:00
|
|
|
* @param array $args
|
|
|
|
*
|
2015-10-31 19:10:45 +00:00
|
|
|
* @return WC_Order on success, WP_Error on failure.
|
2014-06-06 14:51:09 +00:00
|
|
|
*/
|
2014-06-13 15:11:15 +00:00
|
|
|
function wc_create_order( $args = array() ) {
|
2014-06-06 14:51:09 +00:00
|
|
|
$default_args = array(
|
2014-06-11 14:10:03 +00:00
|
|
|
'status' => '',
|
|
|
|
'customer_id' => null,
|
|
|
|
'customer_note' => null,
|
2015-04-01 13:21:07 +00:00
|
|
|
'order_id' => 0,
|
|
|
|
'created_via' => '',
|
2016-02-05 09:41:25 +00:00
|
|
|
'cart_hash' => '',
|
|
|
|
'parent' => 0,
|
2014-06-06 14:51:09 +00:00
|
|
|
);
|
|
|
|
|
2014-06-11 14:10:03 +00:00
|
|
|
$args = wp_parse_args( $args, $default_args );
|
|
|
|
$order_data = array();
|
2014-06-06 14:51:09 +00:00
|
|
|
|
2014-06-11 14:10:03 +00:00
|
|
|
if ( $args['order_id'] > 0 ) {
|
|
|
|
$updating = true;
|
|
|
|
$order_data['ID'] = $args['order_id'];
|
|
|
|
} else {
|
|
|
|
$updating = false;
|
2014-06-11 15:11:28 +00:00
|
|
|
$order_data['post_type'] = 'shop_order';
|
2014-06-11 14:10:03 +00:00
|
|
|
$order_data['post_status'] = 'wc-' . apply_filters( 'woocommerce_default_order_status', 'pending' );
|
|
|
|
$order_data['ping_status'] = 'closed';
|
|
|
|
$order_data['post_author'] = 1;
|
|
|
|
$order_data['post_password'] = uniqid( 'order_' );
|
|
|
|
$order_data['post_title'] = sprintf( __( 'Order – %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) );
|
2015-04-01 13:21:07 +00:00
|
|
|
$order_data['post_parent'] = absint( $args['parent'] );
|
2014-06-11 14:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $args['status'] ) {
|
|
|
|
if ( ! in_array( 'wc-' . $args['status'], array_keys( wc_get_order_statuses() ) ) ) {
|
2014-07-13 01:43:27 +00:00
|
|
|
return new WP_Error( 'woocommerce_invalid_order_status', __( 'Invalid order status', 'woocommerce' ) );
|
2014-06-11 14:10:03 +00:00
|
|
|
}
|
|
|
|
$order_data['post_status'] = 'wc-' . $args['status'];
|
2014-06-06 14:51:09 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 14:10:03 +00:00
|
|
|
if ( ! is_null( $args['customer_note'] ) ) {
|
|
|
|
$order_data['post_excerpt'] = $args['customer_note'];
|
|
|
|
}
|
2014-06-06 14:51:09 +00:00
|
|
|
|
2014-06-11 14:10:03 +00:00
|
|
|
if ( $updating ) {
|
|
|
|
$order_id = wp_update_post( $order_data );
|
|
|
|
} else {
|
|
|
|
$order_id = wp_insert_post( apply_filters( 'woocommerce_new_order_data', $order_data ), true );
|
|
|
|
}
|
2014-06-06 14:51:09 +00:00
|
|
|
|
|
|
|
if ( is_wp_error( $order_id ) ) {
|
|
|
|
return $order_id;
|
2014-06-11 14:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $updating ) {
|
2014-06-06 14:51:09 +00:00
|
|
|
update_post_meta( $order_id, '_order_key', 'wc_' . apply_filters( 'woocommerce_generate_order_key', uniqid( 'order_' ) ) );
|
|
|
|
update_post_meta( $order_id, '_order_currency', get_woocommerce_currency() );
|
|
|
|
update_post_meta( $order_id, '_prices_include_tax', get_option( 'woocommerce_prices_include_tax' ) );
|
2015-09-07 13:23:58 +00:00
|
|
|
update_post_meta( $order_id, '_customer_ip_address', WC_Geolocation::get_ip_address() );
|
2014-06-11 14:10:03 +00:00
|
|
|
update_post_meta( $order_id, '_customer_user_agent', isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '' );
|
|
|
|
update_post_meta( $order_id, '_customer_user', 0 );
|
2015-04-01 13:21:07 +00:00
|
|
|
update_post_meta( $order_id, '_created_via', sanitize_text_field( $args['created_via'] ) );
|
2016-02-05 09:41:25 +00:00
|
|
|
update_post_meta( $order_id, '_cart_hash', sanitize_text_field( $args['cart_hash'] ) );
|
2014-06-11 14:10:03 +00:00
|
|
|
}
|
2014-06-06 14:51:09 +00:00
|
|
|
|
2014-06-11 14:10:03 +00:00
|
|
|
if ( is_numeric( $args['customer_id'] ) ) {
|
|
|
|
update_post_meta( $order_id, '_customer_user', $args['customer_id'] );
|
|
|
|
}
|
2014-06-24 18:56:53 +00:00
|
|
|
|
2015-03-16 12:17:09 +00:00
|
|
|
update_post_meta( $order_id, '_order_version', WC_VERSION );
|
|
|
|
|
2015-07-30 18:07:34 +00:00
|
|
|
return wc_get_order( $order_id );
|
2014-06-11 14:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an order. Uses wc_create_order.
|
2015-11-03 13:31:20 +00:00
|
|
|
*
|
2014-06-11 14:10:03 +00:00
|
|
|
* @param array $args
|
2014-09-07 23:37:55 +00:00
|
|
|
* @return string | WC_Order
|
2014-06-11 14:10:03 +00:00
|
|
|
*/
|
|
|
|
function wc_update_order( $args ) {
|
|
|
|
if ( ! $args['order_id'] ) {
|
|
|
|
return new WP_Error( __( 'Invalid order ID', 'woocommerce' ) );
|
2014-06-06 14:51:09 +00:00
|
|
|
}
|
2014-06-11 14:10:03 +00:00
|
|
|
return wc_create_order( $args );
|
2014-06-06 14:51:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 13:42:57 +00:00
|
|
|
/**
|
|
|
|
* Get template part (for templates like the shop-loop).
|
|
|
|
*
|
2015-12-16 11:28:34 +00:00
|
|
|
* WC_TEMPLATE_DEBUG_MODE will prevent overrides in themes from taking priority.
|
|
|
|
*
|
2013-09-12 13:42:57 +00:00
|
|
|
* @access public
|
|
|
|
* @param mixed $slug
|
|
|
|
* @param string $name (default: '')
|
|
|
|
*/
|
2013-11-25 12:45:04 +00:00
|
|
|
function wc_get_template_part( $slug, $name = '' ) {
|
2013-09-12 13:42:57 +00:00
|
|
|
$template = '';
|
|
|
|
|
|
|
|
// Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php
|
2014-10-03 09:52:11 +00:00
|
|
|
if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) {
|
2014-02-01 14:58:56 +00:00
|
|
|
$template = locate_template( array( "{$slug}-{$name}.php", WC()->template_path() . "{$slug}-{$name}.php" ) );
|
|
|
|
}
|
2013-09-12 13:42:57 +00:00
|
|
|
|
|
|
|
// Get default slug-name.php
|
2014-02-01 14:58:56 +00:00
|
|
|
if ( ! $template && $name && file_exists( WC()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) {
|
2013-09-12 13:42:57 +00:00
|
|
|
$template = WC()->plugin_path() . "/templates/{$slug}-{$name}.php";
|
2014-02-01 14:58:56 +00:00
|
|
|
}
|
2013-09-12 13:42:57 +00:00
|
|
|
|
|
|
|
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php
|
2014-10-03 09:52:11 +00:00
|
|
|
if ( ! $template && ! WC_TEMPLATE_DEBUG_MODE ) {
|
2014-02-01 14:58:56 +00:00
|
|
|
$template = locate_template( array( "{$slug}.php", WC()->template_path() . "{$slug}.php" ) );
|
|
|
|
}
|
2013-09-12 13:42:57 +00:00
|
|
|
|
2015-12-16 11:28:34 +00:00
|
|
|
// Allow 3rd party plugins to filter template file from their plugin.
|
|
|
|
$template = apply_filters( 'wc_get_template_part', $template, $slug, $name );
|
2014-01-27 03:37:03 +00:00
|
|
|
|
2014-02-01 14:58:56 +00:00
|
|
|
if ( $template ) {
|
2013-09-12 13:42:57 +00:00
|
|
|
load_template( $template, false );
|
2014-02-01 14:58:56 +00:00
|
|
|
}
|
2013-09-12 13:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get other templates (e.g. product attributes) passing attributes and including the file.
|
|
|
|
*
|
|
|
|
* @access public
|
2014-04-28 20:04:21 +00:00
|
|
|
* @param string $template_name
|
2013-09-12 13:42:57 +00:00
|
|
|
* @param array $args (default: array())
|
|
|
|
* @param string $template_path (default: '')
|
|
|
|
* @param string $default_path (default: '')
|
|
|
|
*/
|
2013-11-25 12:45:04 +00:00
|
|
|
function wc_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
|
2014-02-01 14:58:56 +00:00
|
|
|
if ( $args && is_array( $args ) ) {
|
2013-09-12 13:42:57 +00:00
|
|
|
extract( $args );
|
2014-02-01 14:58:56 +00:00
|
|
|
}
|
2013-09-12 13:42:57 +00:00
|
|
|
|
2013-11-25 12:45:04 +00:00
|
|
|
$located = wc_locate_template( $template_name, $template_path, $default_path );
|
2013-09-12 13:42:57 +00:00
|
|
|
|
2014-02-11 14:48:28 +00:00
|
|
|
if ( ! file_exists( $located ) ) {
|
2014-02-13 09:25:24 +00:00
|
|
|
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
|
2014-02-11 14:48:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// Allow 3rd party plugin filter template file from their plugin.
|
2014-07-22 01:05:25 +00:00
|
|
|
$located = apply_filters( 'wc_get_template', $located, $template_name, $args, $template_path, $default_path );
|
|
|
|
|
2013-09-12 13:42:57 +00:00
|
|
|
do_action( 'woocommerce_before_template_part', $template_name, $template_path, $located, $args );
|
|
|
|
|
|
|
|
include( $located );
|
|
|
|
|
|
|
|
do_action( 'woocommerce_after_template_part', $template_name, $template_path, $located, $args );
|
|
|
|
}
|
|
|
|
|
2015-12-04 13:26:24 +00:00
|
|
|
/**
|
|
|
|
* Like wc_get_template, but returns the HTML instead of outputting.
|
|
|
|
* @see wc_get_template
|
|
|
|
* @since 2.5.0
|
|
|
|
*/
|
|
|
|
function wc_get_template_html( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
|
|
|
|
ob_start();
|
|
|
|
wc_get_template( $template_name, $args, $template_path, $default_path );
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
|
|
|
|
2013-09-12 13:42:57 +00:00
|
|
|
/**
|
|
|
|
* Locate a template and return the path for inclusion.
|
|
|
|
*
|
|
|
|
* This is the load order:
|
|
|
|
*
|
|
|
|
* yourtheme / $template_path / $template_name
|
|
|
|
* yourtheme / $template_name
|
|
|
|
* $default_path / $template_name
|
|
|
|
*
|
|
|
|
* @access public
|
2014-04-28 20:04:21 +00:00
|
|
|
* @param string $template_name
|
2013-09-12 13:42:57 +00:00
|
|
|
* @param string $template_path (default: '')
|
|
|
|
* @param string $default_path (default: '')
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 12:45:04 +00:00
|
|
|
function wc_locate_template( $template_name, $template_path = '', $default_path = '' ) {
|
2014-02-01 14:58:56 +00:00
|
|
|
if ( ! $template_path ) {
|
|
|
|
$template_path = WC()->template_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $default_path ) {
|
|
|
|
$default_path = WC()->plugin_path() . '/templates/';
|
|
|
|
}
|
2013-09-12 13:42:57 +00:00
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// Look within passed path within the theme - this is priority.
|
2013-09-12 13:42:57 +00:00
|
|
|
$template = locate_template(
|
|
|
|
array(
|
|
|
|
trailingslashit( $template_path ) . $template_name,
|
|
|
|
$template_name
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// Get default template/
|
2014-10-03 09:52:11 +00:00
|
|
|
if ( ! $template || WC_TEMPLATE_DEBUG_MODE ) {
|
2013-09-12 13:42:57 +00:00
|
|
|
$template = $default_path . $template_name;
|
2014-02-01 14:58:56 +00:00
|
|
|
}
|
2013-09-12 13:42:57 +00:00
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// Return what we found.
|
2014-10-03 09:52:11 +00:00
|
|
|
return apply_filters( 'woocommerce_locate_template', $template, $template_name, $template_path );
|
2013-09-12 13:42:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
|
|
|
* Get Base Currency Code.
|
2015-11-03 13:31:20 +00:00
|
|
|
*
|
2013-08-09 16:11:15 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_woocommerce_currency() {
|
|
|
|
return apply_filters( 'woocommerce_currency', get_option('woocommerce_currency') );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get full list of currency codes.
|
2015-11-03 13:31:20 +00:00
|
|
|
*
|
2013-08-09 16:11:15 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_woocommerce_currencies() {
|
|
|
|
return array_unique(
|
|
|
|
apply_filters( 'woocommerce_currencies',
|
|
|
|
array(
|
2014-02-17 13:14:41 +00:00
|
|
|
'AED' => __( 'United Arab Emirates Dirham', 'woocommerce' ),
|
2015-06-25 15:58:58 +00:00
|
|
|
'ARS' => __( 'Argentine Peso', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'AUD' => __( 'Australian Dollars', 'woocommerce' ),
|
2014-03-17 18:33:08 +00:00
|
|
|
'BDT' => __( 'Bangladeshi Taka', 'woocommerce' ),
|
2013-08-21 07:29:44 +00:00
|
|
|
'BGN' => __( 'Bulgarian Lev', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'BRL' => __( 'Brazilian Real', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'CAD' => __( 'Canadian Dollars', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'CHF' => __( 'Swiss Franc', 'woocommerce' ),
|
2014-02-01 15:00:57 +00:00
|
|
|
'CLP' => __( 'Chilean Peso', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'CNY' => __( 'Chinese Yuan', 'woocommerce' ),
|
2014-03-31 20:22:17 +00:00
|
|
|
'COP' => __( 'Colombian Peso', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'CZK' => __( 'Czech Koruna', 'woocommerce' ),
|
|
|
|
'DKK' => __( 'Danish Krone', 'woocommerce' ),
|
2014-06-24 15:08:04 +00:00
|
|
|
'DOP' => __( 'Dominican Peso', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'EGP' => __( 'Egyptian Pound', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'EUR' => __( 'Euros', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'GBP' => __( 'Pounds Sterling', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'HKD' => __( 'Hong Kong Dollar', 'woocommerce' ),
|
2014-02-09 19:31:03 +00:00
|
|
|
'HRK' => __( 'Croatia kuna', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'HUF' => __( 'Hungarian Forint', 'woocommerce' ),
|
|
|
|
'IDR' => __( 'Indonesia Rupiah', 'woocommerce' ),
|
|
|
|
'ILS' => __( 'Israeli Shekel', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'INR' => __( 'Indian Rupee', 'woocommerce' ),
|
|
|
|
'ISK' => __( 'Icelandic krona', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'JPY' => __( 'Japanese Yen', 'woocommerce' ),
|
2015-12-09 15:06:54 +00:00
|
|
|
'KES' => __( 'Kenyan shilling', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'KRW' => __( 'South Korean Won', 'woocommerce' ),
|
2016-01-25 22:59:19 +00:00
|
|
|
'LAK' => __( 'Lao Kip', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'MXN' => __( 'Mexican Peso', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'MYR' => __( 'Malaysian Ringgits', 'woocommerce' ),
|
2014-01-19 19:55:29 +00:00
|
|
|
'NGN' => __( 'Nigerian Naira', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'NOK' => __( 'Norwegian Krone', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'NPR' => __( 'Nepali Rupee', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'NZD' => __( 'New Zealand Dollar', 'woocommerce' ),
|
|
|
|
'PHP' => __( 'Philippine Pesos', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'PKR' => __( 'Pakistani Rupee', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'PLN' => __( 'Polish Zloty', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'PYG' => __( 'Paraguayan Guaraní', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'RON' => __( 'Romanian Leu', 'woocommerce' ),
|
|
|
|
'RUB' => __( 'Russian Ruble', 'woocommerce' ),
|
2016-01-25 22:59:19 +00:00
|
|
|
'SAR' => __( 'Saudi Riyal', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'SEK' => __( 'Swedish Krona', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'SGD' => __( 'Singapore Dollar', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'THB' => __( 'Thai Baht', 'woocommerce' ),
|
|
|
|
'TRY' => __( 'Turkish Lira', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'TWD' => __( 'Taiwan New Dollars', 'woocommerce' ),
|
2014-10-30 12:27:16 +00:00
|
|
|
'UAH' => __( 'Ukrainian Hryvnia', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
'USD' => __( 'US Dollars', 'woocommerce' ),
|
2013-08-21 17:24:51 +00:00
|
|
|
'VND' => __( 'Vietnamese Dong', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'ZAR' => __( 'South African rand', 'woocommerce' ),
|
2013-08-09 16:11:15 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Currency symbol.
|
2015-11-03 13:31:20 +00:00
|
|
|
*
|
2013-08-09 16:11:15 +00:00
|
|
|
* @param string $currency (default: '')
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_woocommerce_currency_symbol( $currency = '' ) {
|
2014-02-01 14:58:56 +00:00
|
|
|
if ( ! $currency ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
$currency = get_woocommerce_currency();
|
2014-02-01 14:58:56 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2015-12-16 16:25:11 +00:00
|
|
|
$symbols = apply_filters( 'woocommerce_currency_symbols', array(
|
2015-12-16 15:23:44 +00:00
|
|
|
'AED' => 'د.إ',
|
|
|
|
'ARS' => '$',
|
|
|
|
'AUD' => '$',
|
|
|
|
'BDT' => '৳ ',
|
|
|
|
'BGN' => 'лв.',
|
|
|
|
'BRL' => 'R$',
|
|
|
|
'CAD' => '$',
|
|
|
|
'CHF' => 'CHF',
|
|
|
|
'CLP' => '$',
|
|
|
|
'CNY' => '¥',
|
|
|
|
'COP' => '$',
|
|
|
|
'CZK' => 'Kč',
|
|
|
|
'DKK' => 'DKK',
|
|
|
|
'DOP' => 'RD$',
|
|
|
|
'EGP' => 'EGP',
|
|
|
|
'EUR' => '€',
|
|
|
|
'GBP' => '£',
|
|
|
|
'HKD' => '$',
|
|
|
|
'HRK' => 'Kn',
|
|
|
|
'HUF' => 'Ft',
|
|
|
|
'IDR' => 'Rp',
|
|
|
|
'ILS' => '₪',
|
|
|
|
'INR' => '₹',
|
|
|
|
'ISK' => 'Kr.',
|
|
|
|
'JPY' => '¥',
|
|
|
|
'KES' => 'KSh',
|
|
|
|
'KRW' => '₩',
|
2016-01-25 22:59:19 +00:00
|
|
|
'LAK' => '₭',
|
2015-12-16 15:23:44 +00:00
|
|
|
'MXN' => '$',
|
|
|
|
'MYR' => 'RM',
|
|
|
|
'NGN' => '₦',
|
|
|
|
'NOK' => 'kr',
|
|
|
|
'NPR' => '₨',
|
|
|
|
'NZD' => '$',
|
|
|
|
'PHP' => '₱',
|
|
|
|
'PKR' => '₨',
|
|
|
|
'PLN' => 'zł',
|
|
|
|
'PYG' => '₲',
|
|
|
|
'RMB' => '¥',
|
|
|
|
'RON' => 'lei',
|
2016-01-27 17:22:43 +00:00
|
|
|
'RUB' => '₽',
|
2016-01-25 22:59:19 +00:00
|
|
|
'SAR' => 'ر.س',
|
2015-12-16 15:23:44 +00:00
|
|
|
'SEK' => 'kr',
|
|
|
|
'SGD' => '$',
|
|
|
|
'THB' => '฿',
|
|
|
|
'TRY' => '₺',
|
|
|
|
'TWD' => 'NT$',
|
|
|
|
'UAH' => '₴',
|
|
|
|
'USD' => '$',
|
|
|
|
'VND' => '₫',
|
|
|
|
'ZAR' => 'R',
|
|
|
|
) );
|
|
|
|
|
|
|
|
$currency_symbol = isset( $symbols[ $currency ] ) ? $symbols[ $currency ] : '';
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_currency_symbol', $currency_symbol, $currency );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Send HTML emails from WooCommerce.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param mixed $to
|
|
|
|
* @param mixed $subject
|
|
|
|
* @param mixed $message
|
|
|
|
* @param string $headers (default: "Content-Type: text/html\r\n")
|
|
|
|
* @param string $attachments (default: "")
|
|
|
|
*/
|
2013-11-25 12:45:04 +00:00
|
|
|
function wc_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) {
|
2013-11-25 14:01:32 +00:00
|
|
|
$mailer = WC()->mailer();
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
$mailer->send( $to, $subject, $message, $headers, $attachments );
|
2013-08-21 07:29:44 +00:00
|
|
|
}
|
2013-09-12 13:41:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an image size.
|
|
|
|
*
|
2015-10-31 19:10:45 +00:00
|
|
|
* Variable is filtered by woocommerce_get_image_size_{image_size}.
|
2013-09-12 13:41:02 +00:00
|
|
|
*
|
2015-02-14 01:18:07 +00:00
|
|
|
* @param mixed $image_size
|
2013-09-12 13:41:02 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_get_image_size( $image_size ) {
|
2015-02-14 01:18:07 +00:00
|
|
|
if ( is_array( $image_size ) ) {
|
|
|
|
$width = isset( $image_size[0] ) ? $image_size[0] : '300';
|
|
|
|
$height = isset( $image_size[1] ) ? $image_size[1] : '300';
|
|
|
|
$crop = isset( $image_size[2] ) ? $image_size[2] : 1;
|
2015-03-06 14:39:46 +00:00
|
|
|
|
2015-02-14 01:18:07 +00:00
|
|
|
$size = array(
|
|
|
|
'width' => $width,
|
|
|
|
'height' => $height,
|
|
|
|
'crop' => $crop
|
|
|
|
);
|
|
|
|
|
|
|
|
$image_size = $width . '_' . $height;
|
|
|
|
|
|
|
|
} elseif ( in_array( $image_size, array( 'shop_thumbnail', 'shop_catalog', 'shop_single' ) ) ) {
|
2013-09-12 13:41:02 +00:00
|
|
|
$size = get_option( $image_size . '_image_size', array() );
|
|
|
|
$size['width'] = isset( $size['width'] ) ? $size['width'] : '300';
|
|
|
|
$size['height'] = isset( $size['height'] ) ? $size['height'] : '300';
|
2014-10-22 13:23:20 +00:00
|
|
|
$size['crop'] = isset( $size['crop'] ) ? $size['crop'] : 0;
|
2015-02-14 01:18:07 +00:00
|
|
|
|
2013-09-12 13:41:02 +00:00
|
|
|
} else {
|
|
|
|
$size = array(
|
|
|
|
'width' => '300',
|
|
|
|
'height' => '300',
|
|
|
|
'crop' => 1
|
|
|
|
);
|
|
|
|
}
|
2014-02-01 14:58:56 +00:00
|
|
|
|
2013-09-12 13:41:02 +00:00
|
|
|
return apply_filters( 'woocommerce_get_image_size_' . $image_size, $size );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue some JavaScript code to be output in the footer.
|
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
*/
|
|
|
|
function wc_enqueue_js( $code ) {
|
|
|
|
global $wc_queued_js;
|
|
|
|
|
2014-02-01 14:58:56 +00:00
|
|
|
if ( empty( $wc_queued_js ) ) {
|
|
|
|
$wc_queued_js = '';
|
|
|
|
}
|
2013-09-12 13:41:02 +00:00
|
|
|
|
|
|
|
$wc_queued_js .= "\n" . $code . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output any queued javascript code in the footer.
|
|
|
|
*/
|
|
|
|
function wc_print_js() {
|
|
|
|
global $wc_queued_js;
|
|
|
|
|
|
|
|
if ( ! empty( $wc_queued_js ) ) {
|
2016-03-14 13:47:25 +00:00
|
|
|
// Sanitize.
|
2013-09-12 13:41:02 +00:00
|
|
|
$wc_queued_js = wp_check_invalid_utf8( $wc_queued_js );
|
|
|
|
$wc_queued_js = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", $wc_queued_js );
|
|
|
|
$wc_queued_js = str_replace( "\r", '', $wc_queued_js );
|
|
|
|
|
2016-03-14 13:47:25 +00:00
|
|
|
$js = "<!-- WooCommerce JavaScript -->\n<script type=\"text/javascript\">\njQuery(function($) { $wc_queued_js });\n</script>\n";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* woocommerce_queued_js filter.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $js JavaScript code.
|
|
|
|
*/
|
|
|
|
echo apply_filters( 'woocommerce_queued_js', $js );
|
2013-09-12 13:41:02 +00:00
|
|
|
|
|
|
|
unset( $wc_queued_js );
|
|
|
|
}
|
2013-10-17 14:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Set a cookie - wrapper for setcookie using WP constants.
|
2013-11-03 23:54:56 +00:00
|
|
|
*
|
2015-10-31 19:10:45 +00:00
|
|
|
* @param string $name Name of the cookie being set.
|
|
|
|
* @param string $value Value of the cookie.
|
|
|
|
* @param integer $expire Expiry of the cookie.
|
|
|
|
* @param string $secure Whether the cookie should be served only over https.
|
2013-10-17 14:29:39 +00:00
|
|
|
*/
|
2014-03-19 09:16:26 +00:00
|
|
|
function wc_setcookie( $name, $value, $expire = 0, $secure = false ) {
|
2013-10-17 14:29:39 +00:00
|
|
|
if ( ! headers_sent() ) {
|
2016-02-01 13:05:36 +00:00
|
|
|
setcookie( $name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure );
|
2013-10-17 14:29:39 +00:00
|
|
|
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
2014-09-17 10:40:51 +00:00
|
|
|
headers_sent( $file, $line );
|
|
|
|
trigger_error( "{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE );
|
2013-10-17 14:29:39 +00:00
|
|
|
}
|
2013-11-03 23:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Get the URL to the WooCommerce REST API.
|
2013-11-03 23:54:56 +00:00
|
|
|
*
|
2013-11-06 06:23:19 +00:00
|
|
|
* @since 2.1
|
2015-10-31 19:10:45 +00:00
|
|
|
* @param string $path an endpoint to include in the URL.
|
|
|
|
* @return string the URL.
|
2013-11-03 23:54:56 +00:00
|
|
|
*/
|
2013-11-06 06:23:19 +00:00
|
|
|
function get_woocommerce_api_url( $path ) {
|
2013-11-03 23:54:56 +00:00
|
|
|
|
2015-05-05 12:01:34 +00:00
|
|
|
$_version = substr( WC_API::VERSION, 0, 1 );
|
|
|
|
$version = defined( 'WC_API_REQUEST_VERSION' ) ? WC_API_REQUEST_VERSION : $_version;
|
2014-07-12 21:51:29 +00:00
|
|
|
|
|
|
|
$url = get_home_url( null, "wc-api/v{$version}/", is_ssl() ? 'https' : 'http' );
|
2013-11-03 23:54:56 +00:00
|
|
|
|
2014-02-01 14:58:56 +00:00
|
|
|
if ( ! empty( $path ) && is_string( $path ) ) {
|
2013-11-06 06:23:19 +00:00
|
|
|
$url .= ltrim( $path, '/' );
|
2014-02-01 14:58:56 +00:00
|
|
|
}
|
2013-11-03 23:54:56 +00:00
|
|
|
|
2013-11-06 06:23:19 +00:00
|
|
|
return $url;
|
2013-11-03 23:54:56 +00:00
|
|
|
}
|
2014-05-28 14:08:33 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Get a log file path.
|
2014-05-28 14:08:33 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
2015-10-31 19:10:45 +00:00
|
|
|
* @param string $handle name.
|
|
|
|
* @return string the log file path.
|
2014-05-28 14:08:33 +00:00
|
|
|
*/
|
|
|
|
function wc_get_log_file_path( $handle ) {
|
|
|
|
return trailingslashit( WC_LOG_DIR ) . $handle . '-' . sanitize_file_name( wp_hash( $handle ) ) . '.log';
|
|
|
|
}
|
2014-06-02 20:00:44 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Init for our rewrite rule fixes.
|
2014-06-04 06:46:21 +00:00
|
|
|
*/
|
|
|
|
function wc_fix_rewrite_rules_init() {
|
2014-11-20 00:43:09 +00:00
|
|
|
$permalinks = get_option( 'woocommerce_permalinks' );
|
2014-06-04 06:46:21 +00:00
|
|
|
|
|
|
|
if ( ! empty( $permalinks['use_verbose_page_rules'] ) ) {
|
|
|
|
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_action( 'init', 'wc_fix_rewrite_rules_init' );
|
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Various rewrite rule fixes.
|
2014-06-02 20:00:44 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @param array $rules
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_fix_rewrite_rules( $rules ) {
|
2014-06-04 06:46:21 +00:00
|
|
|
global $wp_rewrite;
|
|
|
|
|
|
|
|
$permalinks = get_option( 'woocommerce_permalinks' );
|
2014-06-02 20:00:44 +00:00
|
|
|
$product_permalink = empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'];
|
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// Fix the rewrite rules when the product permalink have %product_cat% flag.
|
2014-10-21 13:20:19 +00:00
|
|
|
if ( preg_match( '`/(.+)(/%product_cat%)`' , $product_permalink, $matches ) ) {
|
2014-06-02 20:00:44 +00:00
|
|
|
foreach ( $rules as $rule => $rewrite ) {
|
2014-10-21 13:20:19 +00:00
|
|
|
|
|
|
|
if ( preg_match( '`^' . preg_quote( $matches[1], '`' ) . '/\(`', $rule ) && preg_match( '/^(index\.php\?product_cat)(?!(.*product))/', $rewrite ) ) {
|
2014-06-02 20:00:44 +00:00
|
|
|
unset( $rules[ $rule ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// If the shop page is used as the base, we need to enable verbose rewrite rules or sub pages will 404.
|
2014-06-04 06:46:21 +00:00
|
|
|
if ( ! empty( $permalinks['use_verbose_page_rules'] ) ) {
|
|
|
|
$page_rewrite_rules = $wp_rewrite->page_rewrite_rules();
|
|
|
|
$rules = array_merge( $page_rewrite_rules, $rules );
|
|
|
|
}
|
|
|
|
|
2014-06-02 20:00:44 +00:00
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
add_filter( 'rewrite_rules_array', 'wc_fix_rewrite_rules' );
|
2014-06-04 10:16:19 +00:00
|
|
|
|
2014-08-06 10:50:03 +00:00
|
|
|
/**
|
|
|
|
* Prevent product attachment links from breaking when using complex rewrite structures.
|
2014-08-12 15:13:29 +00:00
|
|
|
*
|
2014-08-06 10:50:03 +00:00
|
|
|
* @param string $link
|
|
|
|
* @param id $post_id
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_fix_product_attachment_link( $link, $post_id ) {
|
|
|
|
global $wp_rewrite;
|
|
|
|
|
|
|
|
$post = get_post( $post_id );
|
|
|
|
if ( 'product' === get_post_type( $post->post_parent ) ) {
|
|
|
|
$permalinks = get_option( 'woocommerce_permalinks' );
|
|
|
|
$product_permalink = empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'];
|
|
|
|
if ( preg_match( '/\/(.+)(\/%product_cat%)$/' , $product_permalink, $matches ) ) {
|
|
|
|
$link = home_url( '/?attachment_id=' . $post->ID );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
add_filter( 'attachment_link', 'wc_fix_product_attachment_link', 10, 2 );
|
|
|
|
|
2014-06-04 10:16:19 +00:00
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Protect downloads from ms-files.php in multisite.
|
2014-06-04 10:16:19 +00:00
|
|
|
*
|
|
|
|
* @param mixed $rewrite
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_ms_protect_download_rewite_rules( $rewrite ) {
|
|
|
|
if ( ! is_multisite() || 'redirect' == get_option( 'woocommerce_file_download_method' ) ) {
|
|
|
|
return $rewrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rule = "\n# WooCommerce Rules - Protect Files from ms-files.php\n\n";
|
|
|
|
$rule .= "<IfModule mod_rewrite.c>\n";
|
|
|
|
$rule .= "RewriteEngine On\n";
|
|
|
|
$rule .= "RewriteCond %{QUERY_STRING} file=woocommerce_uploads/ [NC]\n";
|
|
|
|
$rule .= "RewriteRule /ms-files.php$ - [F]\n";
|
|
|
|
$rule .= "</IfModule>\n\n";
|
|
|
|
|
|
|
|
return $rule . $rewrite;
|
|
|
|
}
|
2014-06-24 18:56:53 +00:00
|
|
|
add_filter( 'mod_rewrite_rules', 'wc_ms_protect_download_rewite_rules' );
|
|
|
|
|
2014-06-27 19:42:23 +00:00
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* WooCommerce Core Supported Themes.
|
2014-06-27 19:42:23 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
2015-07-16 19:29:01 +00:00
|
|
|
* @return string[]
|
2014-06-27 19:42:23 +00:00
|
|
|
*/
|
|
|
|
function wc_get_core_supported_themes() {
|
2015-11-23 11:55:50 +00:00
|
|
|
return array( 'twentysixteen', 'twentyfifteen', 'twentyfourteen', 'twentythirteen', 'twentyeleven', 'twentytwelve', 'twentyten' );
|
2014-06-27 19:42:23 +00:00
|
|
|
}
|
2014-07-30 20:23:22 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Wrapper function to execute the `woocommerce_deliver_webhook_async` cron.
|
|
|
|
* hook, see WC_Webhook::process().
|
2014-07-30 20:23:22 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
2015-10-31 19:10:45 +00:00
|
|
|
* @param int $webhook_id webhook ID to deliver.
|
|
|
|
* @param mixed $arg hook argument.
|
2014-07-30 20:23:22 +00:00
|
|
|
*/
|
|
|
|
function wc_deliver_webhook_async( $webhook_id, $arg ) {
|
|
|
|
|
|
|
|
$webhook = new WC_Webhook( $webhook_id );
|
|
|
|
|
|
|
|
$webhook->deliver( $arg );
|
|
|
|
}
|
|
|
|
add_action( 'woocommerce_deliver_webhook_async', 'wc_deliver_webhook_async', 10, 2 );
|
2014-11-18 14:56:13 +00:00
|
|
|
|
2015-02-11 23:32:27 +00:00
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Enables template debug mode.
|
2015-02-11 23:32:27 +00:00
|
|
|
*/
|
|
|
|
function wc_template_debug_mode() {
|
|
|
|
if ( ! defined( 'WC_TEMPLATE_DEBUG_MODE' ) ) {
|
|
|
|
$status_options = get_option( 'woocommerce_status_options', array() );
|
|
|
|
if ( ! empty( $status_options['template_debug_mode'] ) && current_user_can( 'manage_options' ) ) {
|
|
|
|
define( 'WC_TEMPLATE_DEBUG_MODE', true );
|
|
|
|
} else {
|
|
|
|
define( 'WC_TEMPLATE_DEBUG_MODE', false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_action( 'after_setup_theme', 'wc_template_debug_mode', 20 );
|
|
|
|
|
2014-11-18 14:56:13 +00:00
|
|
|
/**
|
2014-12-23 17:33:51 +00:00
|
|
|
* Formats a string in the format COUNTRY:STATE into an array.
|
2015-11-03 13:31:20 +00:00
|
|
|
*
|
2014-11-18 14:56:13 +00:00
|
|
|
* @since 2.3.0
|
2014-12-23 17:33:51 +00:00
|
|
|
* @param string $country_string
|
2014-11-18 14:56:13 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-12-23 17:33:51 +00:00
|
|
|
function wc_format_country_state_string( $country_string ) {
|
|
|
|
if ( strstr( $country_string, ':' ) ) {
|
|
|
|
list( $country, $state ) = explode( ':', $country_string );
|
2014-11-18 14:56:13 +00:00
|
|
|
} else {
|
2014-12-23 17:33:51 +00:00
|
|
|
$country = $country_string;
|
2014-11-18 14:56:13 +00:00
|
|
|
$state = '';
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'country' => $country,
|
|
|
|
'state' => $state
|
|
|
|
);
|
|
|
|
}
|
2014-12-23 17:33:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the store's base location.
|
2015-11-03 13:31:20 +00:00
|
|
|
*
|
2014-12-23 17:33:51 +00:00
|
|
|
* @todo should the woocommerce_default_country option be renamed to contain 'base'?
|
|
|
|
* @since 2.3.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_get_base_location() {
|
|
|
|
$default = apply_filters( 'woocommerce_get_base_location', get_option( 'woocommerce_default_country' ) );
|
|
|
|
|
|
|
|
return wc_format_country_state_string( $default );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-07 11:25:24 +00:00
|
|
|
* Get the customer's default location.
|
2015-06-17 11:12:49 +00:00
|
|
|
*
|
2015-10-31 19:10:45 +00:00
|
|
|
* Filtered, and set to base location or left blank. If cache-busting,
|
|
|
|
* this should only be used when 'location' is set in the querystring.
|
2015-06-17 11:12:49 +00:00
|
|
|
*
|
2014-12-23 17:33:51 +00:00
|
|
|
* @todo should the woocommerce_default_country option be renamed to contain 'base'?
|
2016-01-07 11:25:24 +00:00
|
|
|
* @todo deprecate woocommerce_customer_default_location and support an array filter only to cover all cases.
|
2014-12-23 17:33:51 +00:00
|
|
|
* @since 2.3.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_get_customer_default_location() {
|
|
|
|
switch ( get_option( 'woocommerce_default_customer_address' ) ) {
|
2015-06-17 11:12:49 +00:00
|
|
|
case 'geolocation_ajax' :
|
2014-12-23 17:33:51 +00:00
|
|
|
case 'geolocation' :
|
2016-03-30 12:02:21 +00:00
|
|
|
$location = WC_Geolocation::geolocate_ip( '', true, false );
|
2014-12-23 18:50:05 +00:00
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// Base fallback.
|
2014-12-23 18:50:05 +00:00
|
|
|
if ( empty( $location['country'] ) ) {
|
|
|
|
$location = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', get_option( 'woocommerce_default_country' ) ) );
|
|
|
|
}
|
2014-12-23 17:33:51 +00:00
|
|
|
break;
|
|
|
|
case 'base' :
|
|
|
|
$location = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', get_option( 'woocommerce_default_country' ) ) );
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
$location = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', '' ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-07 11:25:24 +00:00
|
|
|
return apply_filters( 'woocommerce_customer_default_location_array', $location );
|
2014-12-23 17:33:51 +00:00
|
|
|
}
|
2015-01-19 05:11:41 +00:00
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// This function can be removed when WP 3.9.2 or greater is required.
|
2015-01-19 05:11:41 +00:00
|
|
|
if ( ! function_exists( 'hash_equals' ) ) :
|
|
|
|
/**
|
|
|
|
* Compare two strings in constant time.
|
|
|
|
*
|
|
|
|
* This function was added in PHP 5.6.
|
|
|
|
* It can leak the length of a string.
|
|
|
|
*
|
|
|
|
* @since 3.9.2
|
|
|
|
*
|
|
|
|
* @param string $a Expected string.
|
|
|
|
* @param string $b Actual string.
|
|
|
|
* @return bool Whether strings are equal.
|
|
|
|
*/
|
|
|
|
function hash_equals( $a, $b ) {
|
|
|
|
$a_length = strlen( $a );
|
|
|
|
if ( $a_length !== strlen( $b ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
// Do not attempt to "optimize" this.
|
|
|
|
for ( $i = 0; $i < $a_length; $i++ ) {
|
|
|
|
$result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result === 0;
|
|
|
|
}
|
|
|
|
endif;
|
2015-06-08 15:01:54 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* Generate a rand hash.
|
2015-06-08 15:01:54 +00:00
|
|
|
*
|
|
|
|
* @since 2.4.0
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_rand_hash() {
|
|
|
|
if ( function_exists( 'openssl_random_pseudo_bytes' ) ) {
|
|
|
|
return bin2hex( openssl_random_pseudo_bytes( 20 ) );
|
|
|
|
} else {
|
|
|
|
return sha1( wp_rand() );
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 15:57:19 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-31 19:10:45 +00:00
|
|
|
* WC API - Hash.
|
2015-06-08 15:57:19 +00:00
|
|
|
*
|
|
|
|
* @since 2.4.0
|
|
|
|
* @param string $data
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_api_hash( $data ) {
|
|
|
|
return hash_hmac( 'sha256', $data, 'wc-api' );
|
|
|
|
}
|
2015-11-04 12:14:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all possible combinations of values from the input array and return in a logical order.
|
|
|
|
* @since 2.5.0
|
|
|
|
* @param array $input
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_array_cartesian( $input ) {
|
|
|
|
$input = array_filter( $input );
|
|
|
|
$results = array();
|
|
|
|
$indexes = array();
|
|
|
|
$index = 0;
|
|
|
|
|
|
|
|
// Generate indexes from keys and values so we have a logical sort order
|
|
|
|
foreach ( $input as $key => $values ) {
|
|
|
|
foreach ( $values as $value ) {
|
2016-03-04 10:53:48 +00:00
|
|
|
$indexes[ $key ][ $value ] = $index++;
|
2015-11-04 12:14:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over the 2D array of indexes and generate all combinations
|
|
|
|
foreach ( $indexes as $key => $values ) {
|
|
|
|
// When result is empty, fill with the values of the first looped array
|
|
|
|
if ( empty( $results ) ) {
|
|
|
|
foreach ( $values as $value ) {
|
|
|
|
$results[] = array( $key => $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second and subsequent input sub-array merging.
|
|
|
|
} else {
|
|
|
|
foreach ( $results as $result_key => $result ) {
|
|
|
|
foreach ( $values as $value ) {
|
|
|
|
// If the key is not set, we can set it
|
|
|
|
if ( ! isset( $results[ $result_key ][ $key ] ) ) {
|
|
|
|
$results[ $result_key ][ $key ] = $value;
|
|
|
|
// If the key is set, we can add a new combination to the results array
|
|
|
|
} else {
|
|
|
|
$new_combination = $results[ $result_key ];
|
|
|
|
$new_combination[ $key ] = $value;
|
|
|
|
$results[] = $new_combination;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the indexes
|
|
|
|
arsort( $results );
|
|
|
|
|
|
|
|
// Convert indexes back to values
|
|
|
|
foreach ( $results as $result_key => $result ) {
|
|
|
|
$converted_values = array();
|
|
|
|
|
|
|
|
// Sort the values
|
|
|
|
arsort( $results[ $result_key ] );
|
|
|
|
|
|
|
|
// Convert the values
|
|
|
|
foreach ( $results[ $result_key ] as $key => $value ) {
|
|
|
|
$converted_values[ $key ] = array_search( $value, $indexes[ $key ] );
|
|
|
|
}
|
2015-11-04 14:11:40 +00:00
|
|
|
|
2015-11-04 12:14:48 +00:00
|
|
|
$results[ $result_key ] = $converted_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
2015-11-04 14:11:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a MySQL transaction query, if supported.
|
|
|
|
* @param string $type start (default), commit, rollback
|
|
|
|
* @since 2.5.0
|
|
|
|
*/
|
|
|
|
function wc_transaction_query( $type = 'start' ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2016-01-15 11:43:06 +00:00
|
|
|
$wpdb->hide_errors();
|
2015-11-04 14:11:40 +00:00
|
|
|
|
2016-01-15 11:43:06 +00:00
|
|
|
if ( ! defined( 'WC_USE_TRANSACTIONS' ) ) {
|
|
|
|
define( 'WC_USE_TRANSACTIONS', true );
|
2015-11-04 14:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( WC_USE_TRANSACTIONS ) {
|
|
|
|
switch ( $type ) {
|
|
|
|
case 'commit' :
|
|
|
|
$wpdb->query( 'COMMIT' );
|
|
|
|
break;
|
|
|
|
case 'rollback' :
|
|
|
|
$wpdb->query( 'ROLLBACK' );
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
$wpdb->query( 'START TRANSACTION' );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-18 17:10:58 +00:00
|
|
|
|
2016-02-10 14:40:46 +00:00
|
|
|
/**
|
|
|
|
* Gets the url to the cart page.
|
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
*
|
|
|
|
* @return string Url to cart page
|
|
|
|
*/
|
|
|
|
function wc_get_cart_url() {
|
|
|
|
return apply_filters( 'woocommerce_get_cart_url', wc_get_page_permalink( 'cart' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the url to the checkout page.
|
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
*
|
|
|
|
* @return string Url to checkout page
|
|
|
|
*/
|
|
|
|
function wc_get_checkout_url() {
|
|
|
|
$checkout_url = wc_get_page_permalink( 'checkout' );
|
|
|
|
if ( $checkout_url ) {
|
|
|
|
// Force SSL if needed
|
|
|
|
if ( is_ssl() || 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) ) {
|
|
|
|
$checkout_url = str_replace( 'http:', 'https:', $checkout_url );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_get_checkout_url', $checkout_url );
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:10:58 +00:00
|
|
|
/**
|
|
|
|
* Register a shipping method.
|
|
|
|
*
|
|
|
|
* @since 1.5.7
|
|
|
|
* @param string|object $shipping_method class name (string) or a class object.
|
|
|
|
*/
|
|
|
|
function woocommerce_register_shipping_method( $shipping_method ) {
|
|
|
|
WC()->shipping->register_shipping_method( $shipping_method );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the shipping zone matching a given package from the cart.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
* @uses WC_Shipping_Zones::get_zone_matching_package
|
|
|
|
* @param array $package
|
|
|
|
* @return WC_Shipping_Zone
|
|
|
|
*/
|
|
|
|
function wc_get_shipping_zone( $package ) {
|
|
|
|
return WC_Shipping_Zones::get_zone_matching_package( $package );
|
|
|
|
}
|
2016-02-17 16:02:22 +00:00
|
|
|
|
2016-03-01 18:07:27 +00:00
|
|
|
/**
|
|
|
|
* Get a nice name for credit card providers.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $type Provider Slug/Type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_get_credit_card_type_label( $type ) {
|
|
|
|
// Normalize
|
|
|
|
$type = strtolower( $type );
|
|
|
|
$type = str_replace( '-', ' ', $type );
|
|
|
|
$type = str_replace( '_', ' ', $type );
|
|
|
|
|
|
|
|
$labels = apply_filters( 'wocommerce_credit_card_type_labels', array(
|
|
|
|
'mastercard' => __( 'MasterCard', 'woocommerce' ),
|
|
|
|
'visa' => __( 'Visa', 'woocommerce' ),
|
|
|
|
'discover' => __( 'Discover', 'woocommerce' ),
|
|
|
|
'american express' => __( 'American Express', 'woocommerce' ),
|
|
|
|
'diners' => __( 'Diners', 'woocommerce' ),
|
|
|
|
'jcb' => __( 'JCB', 'woocommerce' ),
|
|
|
|
) );
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_get_credit_card_type_label', ( array_key_exists( $type, $labels ) ? $labels[ $type ] : ucfirst( $type ) ) );
|
|
|
|
}
|
|
|
|
|
2016-02-17 16:02:22 +00:00
|
|
|
/**
|
|
|
|
* Outputs a "back" link so admin screens can easily jump back a page.
|
2016-02-21 04:25:33 +00:00
|
|
|
*
|
|
|
|
* @param string $label Title of the page to return to.
|
|
|
|
* @param string $url URL of the page to return to.
|
2016-02-17 16:02:22 +00:00
|
|
|
*/
|
|
|
|
function wc_back_link( $label, $url ) {
|
2016-03-24 14:27:32 +00:00
|
|
|
echo '<small class="wc-admin-breadcrumb"><a href="' . esc_url( $url ) . '" title="' . esc_attr( $label ) . '">⤴</a></small>';
|
2016-02-17 16:02:22 +00:00
|
|
|
}
|