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' );
|
2016-11-18 14:07:21 +00:00
|
|
|
include( 'wc-order-item-functions.php' );
|
2013-08-09 16:11:15 +00:00
|
|
|
include( 'wc-page-functions.php' );
|
|
|
|
include( 'wc-product-functions.php' );
|
2016-11-02 18:50:42 +00:00
|
|
|
include( 'wc-stock-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' );
|
2016-03-29 22:52:24 +00:00
|
|
|
include( 'wc-rest-functions.php' );
|
2017-02-16 11:46:01 +00:00
|
|
|
include( 'wc-widget-functions.php' );
|
|
|
|
include( 'wc-webhook-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()
|
2017-04-10 22:21:22 +00:00
|
|
|
add_filter( 'woocommerce_short_description', 'wc_format_product_short_description', 9999999 );
|
2017-03-17 18:39:17 +00:00
|
|
|
add_filter( 'woocommerce_short_description', 'wc_do_oembeds' );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2016-11-24 14:20:58 +00:00
|
|
|
/**
|
|
|
|
* Define a constant if it is not already defined.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-24 14:20:58 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
*/
|
|
|
|
function wc_maybe_define_constant( $name, $value ) {
|
|
|
|
if ( ! defined( $name ) ) {
|
|
|
|
define( $name, $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2016-08-24 15:02:19 +00:00
|
|
|
* @return WC_Order|WP_Error
|
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(
|
2016-08-09 13:02:40 +00:00
|
|
|
'status' => null,
|
2014-06-11 14:10:03 +00:00
|
|
|
'customer_id' => null,
|
|
|
|
'customer_note' => null,
|
2016-08-09 13:02:40 +00:00
|
|
|
'parent' => null,
|
|
|
|
'created_via' => null,
|
|
|
|
'cart_hash' => null,
|
2015-04-01 13:21:07 +00:00
|
|
|
'order_id' => 0,
|
2014-06-06 14:51:09 +00:00
|
|
|
);
|
|
|
|
|
2016-08-24 14:26:35 +00:00
|
|
|
try {
|
2016-08-24 15:02:19 +00:00
|
|
|
$args = wp_parse_args( $args, $default_args );
|
2016-08-24 14:26:35 +00:00
|
|
|
$order = new WC_Order( $args['order_id'] );
|
2014-06-06 14:51:09 +00:00
|
|
|
|
2016-08-24 14:26:35 +00:00
|
|
|
// Update props that were set (not null)
|
|
|
|
if ( ! is_null( $args['parent'] ) ) {
|
|
|
|
$order->set_parent_id( absint( $args['parent'] ) );
|
|
|
|
}
|
2014-06-11 14:10:03 +00:00
|
|
|
|
2016-08-24 14:26:35 +00:00
|
|
|
if ( ! is_null( $args['status'] ) ) {
|
|
|
|
$order->set_status( $args['status'] );
|
|
|
|
}
|
2014-06-06 14:51:09 +00:00
|
|
|
|
2016-08-24 14:26:35 +00:00
|
|
|
if ( ! is_null( $args['customer_note'] ) ) {
|
|
|
|
$order->set_customer_note( $args['customer_note'] );
|
|
|
|
}
|
2014-06-06 14:51:09 +00:00
|
|
|
|
2016-08-24 14:26:35 +00:00
|
|
|
if ( ! is_null( $args['customer_id'] ) ) {
|
|
|
|
$order->set_customer_id( is_numeric( $args['customer_id'] ) ? absint( $args['customer_id'] ) : 0 );
|
|
|
|
}
|
2014-06-11 14:10:03 +00:00
|
|
|
|
2016-08-24 14:26:35 +00:00
|
|
|
if ( ! is_null( $args['created_via'] ) ) {
|
|
|
|
$order->set_created_via( sanitize_text_field( $args['created_via'] ) );
|
|
|
|
}
|
2014-06-06 14:51:09 +00:00
|
|
|
|
2016-08-24 14:26:35 +00:00
|
|
|
if ( ! is_null( $args['cart_hash'] ) ) {
|
|
|
|
$order->set_cart_hash( sanitize_text_field( $args['cart_hash'] ) );
|
|
|
|
}
|
2014-06-24 18:56:53 +00:00
|
|
|
|
2016-08-24 14:26:35 +00:00
|
|
|
// Update other order props set automatically
|
|
|
|
$order->set_currency( get_woocommerce_currency() );
|
|
|
|
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
|
|
|
|
$order->set_customer_ip_address( WC_Geolocation::get_ip_address() );
|
|
|
|
$order->set_customer_user_agent( wc_get_user_agent() );
|
|
|
|
$order->save();
|
2016-08-24 15:02:19 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
return new WP_Error( 'error', $e->getMessage() );
|
2016-08-24 14:26:35 +00:00
|
|
|
}
|
2016-08-24 15:02:19 +00:00
|
|
|
|
2016-08-09 13:02:40 +00:00
|
|
|
return $order;
|
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'] ) {
|
2016-10-11 01:39:13 +00:00
|
|
|
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 = '' ) {
|
2016-06-06 18:39:23 +00:00
|
|
|
if ( ! empty( $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 ) ) {
|
2016-11-23 16:15:00 +00:00
|
|
|
wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'woocommerce' ), '<code>' . $located . '</code>' ), '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 );
|
|
|
|
}
|
|
|
|
|
2017-05-15 11:50:52 +00:00
|
|
|
|
2015-12-04 13:26:24 +00:00
|
|
|
/**
|
|
|
|
* Like wc_get_template, but returns the HTML instead of outputting.
|
2017-05-23 18:00:24 +00:00
|
|
|
*
|
2015-12-04 13:26:24 +00:00
|
|
|
* @see wc_get_template
|
|
|
|
* @since 2.5.0
|
2016-06-06 16:50:18 +00:00
|
|
|
* @param string $template_name
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param array $args
|
|
|
|
* @param string $template_path
|
|
|
|
* @param string $default_path
|
|
|
|
*
|
|
|
|
* @return string
|
2015-12-04 13:26:24 +00:00
|
|
|
*/
|
|
|
|
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,
|
2016-08-27 02:08:49 +00:00
|
|
|
$template_name,
|
2013-09-12 13:42:57 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
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() {
|
2016-09-02 01:51:31 +00:00
|
|
|
return apply_filters( 'woocommerce_currency', get_option( 'woocommerce_currency' ) );
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
2016-04-18 23:21:14 +00:00
|
|
|
'AED' => __( 'United Arab Emirates dirham', 'woocommerce' ),
|
|
|
|
'AFN' => __( 'Afghan afghani', 'woocommerce' ),
|
|
|
|
'ALL' => __( 'Albanian lek', 'woocommerce' ),
|
|
|
|
'AMD' => __( 'Armenian dram', 'woocommerce' ),
|
|
|
|
'ANG' => __( 'Netherlands Antillean guilder', 'woocommerce' ),
|
|
|
|
'AOA' => __( 'Angolan kwanza', 'woocommerce' ),
|
|
|
|
'ARS' => __( 'Argentine peso', 'woocommerce' ),
|
|
|
|
'AUD' => __( 'Australian dollar', 'woocommerce' ),
|
|
|
|
'AWG' => __( 'Aruban florin', 'woocommerce' ),
|
|
|
|
'AZN' => __( 'Azerbaijani manat', 'woocommerce' ),
|
|
|
|
'BAM' => __( 'Bosnia and Herzegovina convertible mark', 'woocommerce' ),
|
|
|
|
'BBD' => __( 'Barbadian dollar', 'woocommerce' ),
|
|
|
|
'BDT' => __( 'Bangladeshi taka', 'woocommerce' ),
|
|
|
|
'BGN' => __( 'Bulgarian lev', 'woocommerce' ),
|
|
|
|
'BHD' => __( 'Bahraini dinar', 'woocommerce' ),
|
|
|
|
'BIF' => __( 'Burundian franc', 'woocommerce' ),
|
|
|
|
'BMD' => __( 'Bermudian dollar', 'woocommerce' ),
|
|
|
|
'BND' => __( 'Brunei dollar', 'woocommerce' ),
|
|
|
|
'BOB' => __( 'Bolivian boliviano', 'woocommerce' ),
|
|
|
|
'BRL' => __( 'Brazilian real', 'woocommerce' ),
|
|
|
|
'BSD' => __( 'Bahamian dollar', 'woocommerce' ),
|
2016-04-18 23:28:04 +00:00
|
|
|
'BTC' => __( 'Bitcoin', 'woocommerce' ),
|
2016-04-18 23:21:14 +00:00
|
|
|
'BTN' => __( 'Bhutanese ngultrum', 'woocommerce' ),
|
|
|
|
'BWP' => __( 'Botswana pula', 'woocommerce' ),
|
|
|
|
'BYR' => __( 'Belarusian ruble', 'woocommerce' ),
|
|
|
|
'BZD' => __( 'Belize dollar', 'woocommerce' ),
|
|
|
|
'CAD' => __( 'Canadian dollar', 'woocommerce' ),
|
|
|
|
'CDF' => __( 'Congolese franc', 'woocommerce' ),
|
|
|
|
'CHF' => __( 'Swiss franc', 'woocommerce' ),
|
|
|
|
'CLP' => __( 'Chilean peso', 'woocommerce' ),
|
|
|
|
'CNY' => __( 'Chinese yuan', 'woocommerce' ),
|
|
|
|
'COP' => __( 'Colombian peso', 'woocommerce' ),
|
|
|
|
'CRC' => __( 'Costa Rican colón', 'woocommerce' ),
|
|
|
|
'CUC' => __( 'Cuban convertible peso', 'woocommerce' ),
|
|
|
|
'CUP' => __( 'Cuban peso', 'woocommerce' ),
|
|
|
|
'CVE' => __( 'Cape Verdean escudo', 'woocommerce' ),
|
|
|
|
'CZK' => __( 'Czech koruna', 'woocommerce' ),
|
|
|
|
'DJF' => __( 'Djiboutian franc', 'woocommerce' ),
|
|
|
|
'DKK' => __( 'Danish krone', 'woocommerce' ),
|
|
|
|
'DOP' => __( 'Dominican peso', 'woocommerce' ),
|
|
|
|
'DZD' => __( 'Algerian dinar', 'woocommerce' ),
|
|
|
|
'EGP' => __( 'Egyptian pound', 'woocommerce' ),
|
|
|
|
'ERN' => __( 'Eritrean nakfa', 'woocommerce' ),
|
|
|
|
'ETB' => __( 'Ethiopian birr', 'woocommerce' ),
|
|
|
|
'EUR' => __( 'Euro', 'woocommerce' ),
|
|
|
|
'FJD' => __( 'Fijian dollar', 'woocommerce' ),
|
|
|
|
'FKP' => __( 'Falkland Islands pound', 'woocommerce' ),
|
|
|
|
'GBP' => __( 'Pound sterling', 'woocommerce' ),
|
|
|
|
'GEL' => __( 'Georgian lari', 'woocommerce' ),
|
|
|
|
'GGP' => __( 'Guernsey pound', 'woocommerce' ),
|
|
|
|
'GHS' => __( 'Ghana cedi', 'woocommerce' ),
|
|
|
|
'GIP' => __( 'Gibraltar pound', 'woocommerce' ),
|
|
|
|
'GMD' => __( 'Gambian dalasi', 'woocommerce' ),
|
|
|
|
'GNF' => __( 'Guinean franc', 'woocommerce' ),
|
|
|
|
'GTQ' => __( 'Guatemalan quetzal', 'woocommerce' ),
|
|
|
|
'GYD' => __( 'Guyanese dollar', 'woocommerce' ),
|
|
|
|
'HKD' => __( 'Hong Kong dollar', 'woocommerce' ),
|
|
|
|
'HNL' => __( 'Honduran lempira', 'woocommerce' ),
|
|
|
|
'HRK' => __( 'Croatian kuna', 'woocommerce' ),
|
|
|
|
'HTG' => __( 'Haitian gourde', 'woocommerce' ),
|
|
|
|
'HUF' => __( 'Hungarian forint', 'woocommerce' ),
|
|
|
|
'IDR' => __( 'Indonesian rupiah', 'woocommerce' ),
|
|
|
|
'ILS' => __( 'Israeli new shekel', 'woocommerce' ),
|
|
|
|
'IMP' => __( 'Manx pound', 'woocommerce' ),
|
|
|
|
'INR' => __( 'Indian rupee', 'woocommerce' ),
|
|
|
|
'IQD' => __( 'Iraqi dinar', 'woocommerce' ),
|
2016-12-19 15:11:25 +00:00
|
|
|
'IRR' => __( 'Iranian rial', 'woocommerce' ),
|
2017-01-03 02:26:34 +00:00
|
|
|
'IRT' => __( 'Iranian toman', 'woocommerce' ),
|
2016-04-18 23:21:14 +00:00
|
|
|
'ISK' => __( 'Icelandic króna', 'woocommerce' ),
|
|
|
|
'JEP' => __( 'Jersey pound', 'woocommerce' ),
|
|
|
|
'JMD' => __( 'Jamaican dollar', 'woocommerce' ),
|
|
|
|
'JOD' => __( 'Jordanian dinar', 'woocommerce' ),
|
|
|
|
'JPY' => __( 'Japanese yen', 'woocommerce' ),
|
2015-12-09 15:06:54 +00:00
|
|
|
'KES' => __( 'Kenyan shilling', 'woocommerce' ),
|
2016-04-18 23:21:14 +00:00
|
|
|
'KGS' => __( 'Kyrgyzstani som', 'woocommerce' ),
|
|
|
|
'KHR' => __( 'Cambodian riel', 'woocommerce' ),
|
|
|
|
'KMF' => __( 'Comorian franc', 'woocommerce' ),
|
|
|
|
'KPW' => __( 'North Korean won', 'woocommerce' ),
|
|
|
|
'KRW' => __( 'South Korean won', 'woocommerce' ),
|
|
|
|
'KWD' => __( 'Kuwaiti dinar', 'woocommerce' ),
|
|
|
|
'KYD' => __( 'Cayman Islands dollar', 'woocommerce' ),
|
|
|
|
'KZT' => __( 'Kazakhstani tenge', 'woocommerce' ),
|
|
|
|
'LAK' => __( 'Lao kip', 'woocommerce' ),
|
|
|
|
'LBP' => __( 'Lebanese pound', 'woocommerce' ),
|
|
|
|
'LKR' => __( 'Sri Lankan rupee', 'woocommerce' ),
|
|
|
|
'LRD' => __( 'Liberian dollar', 'woocommerce' ),
|
|
|
|
'LSL' => __( 'Lesotho loti', 'woocommerce' ),
|
|
|
|
'LYD' => __( 'Libyan dinar', 'woocommerce' ),
|
|
|
|
'MAD' => __( 'Moroccan dirham', 'woocommerce' ),
|
|
|
|
'MDL' => __( 'Moldovan leu', 'woocommerce' ),
|
|
|
|
'MGA' => __( 'Malagasy ariary', 'woocommerce' ),
|
|
|
|
'MKD' => __( 'Macedonian denar', 'woocommerce' ),
|
|
|
|
'MMK' => __( 'Burmese kyat', 'woocommerce' ),
|
|
|
|
'MNT' => __( 'Mongolian tögrög', 'woocommerce' ),
|
|
|
|
'MOP' => __( 'Macanese pataca', 'woocommerce' ),
|
|
|
|
'MRO' => __( 'Mauritanian ouguiya', 'woocommerce' ),
|
|
|
|
'MUR' => __( 'Mauritian rupee', 'woocommerce' ),
|
|
|
|
'MVR' => __( 'Maldivian rufiyaa', 'woocommerce' ),
|
|
|
|
'MWK' => __( 'Malawian kwacha', 'woocommerce' ),
|
|
|
|
'MXN' => __( 'Mexican peso', 'woocommerce' ),
|
|
|
|
'MYR' => __( 'Malaysian ringgit', 'woocommerce' ),
|
|
|
|
'MZN' => __( 'Mozambican metical', 'woocommerce' ),
|
|
|
|
'NAD' => __( 'Namibian dollar', 'woocommerce' ),
|
|
|
|
'NGN' => __( 'Nigerian naira', 'woocommerce' ),
|
|
|
|
'NIO' => __( 'Nicaraguan córdoba', 'woocommerce' ),
|
|
|
|
'NOK' => __( 'Norwegian krone', 'woocommerce' ),
|
|
|
|
'NPR' => __( 'Nepalese rupee', 'woocommerce' ),
|
|
|
|
'NZD' => __( 'New Zealand dollar', 'woocommerce' ),
|
|
|
|
'OMR' => __( 'Omani rial', 'woocommerce' ),
|
|
|
|
'PAB' => __( 'Panamanian balboa', 'woocommerce' ),
|
|
|
|
'PEN' => __( 'Peruvian nuevo sol', 'woocommerce' ),
|
|
|
|
'PGK' => __( 'Papua New Guinean kina', 'woocommerce' ),
|
|
|
|
'PHP' => __( 'Philippine peso', 'woocommerce' ),
|
|
|
|
'PKR' => __( 'Pakistani rupee', 'woocommerce' ),
|
|
|
|
'PLN' => __( 'Polish złoty', 'woocommerce' ),
|
|
|
|
'PRB' => __( 'Transnistrian ruble', 'woocommerce' ),
|
|
|
|
'PYG' => __( 'Paraguayan guaraní', 'woocommerce' ),
|
|
|
|
'QAR' => __( 'Qatari riyal', 'woocommerce' ),
|
|
|
|
'RON' => __( 'Romanian leu', 'woocommerce' ),
|
|
|
|
'RSD' => __( 'Serbian dinar', 'woocommerce' ),
|
|
|
|
'RUB' => __( 'Russian ruble', 'woocommerce' ),
|
|
|
|
'RWF' => __( 'Rwandan franc', 'woocommerce' ),
|
|
|
|
'SAR' => __( 'Saudi riyal', 'woocommerce' ),
|
|
|
|
'SBD' => __( 'Solomon Islands dollar', 'woocommerce' ),
|
|
|
|
'SCR' => __( 'Seychellois rupee', 'woocommerce' ),
|
|
|
|
'SDG' => __( 'Sudanese pound', 'woocommerce' ),
|
|
|
|
'SEK' => __( 'Swedish krona', 'woocommerce' ),
|
|
|
|
'SGD' => __( 'Singapore dollar', 'woocommerce' ),
|
|
|
|
'SHP' => __( 'Saint Helena pound', 'woocommerce' ),
|
|
|
|
'SLL' => __( 'Sierra Leonean leone', 'woocommerce' ),
|
|
|
|
'SOS' => __( 'Somali shilling', 'woocommerce' ),
|
|
|
|
'SRD' => __( 'Surinamese dollar', 'woocommerce' ),
|
|
|
|
'SSP' => __( 'South Sudanese pound', 'woocommerce' ),
|
|
|
|
'STD' => __( 'São Tomé and Príncipe dobra', 'woocommerce' ),
|
|
|
|
'SYP' => __( 'Syrian pound', 'woocommerce' ),
|
|
|
|
'SZL' => __( 'Swazi lilangeni', 'woocommerce' ),
|
|
|
|
'THB' => __( 'Thai baht', 'woocommerce' ),
|
|
|
|
'TJS' => __( 'Tajikistani somoni', 'woocommerce' ),
|
|
|
|
'TMT' => __( 'Turkmenistan manat', 'woocommerce' ),
|
|
|
|
'TND' => __( 'Tunisian dinar', 'woocommerce' ),
|
|
|
|
'TOP' => __( 'Tongan paʻanga', 'woocommerce' ),
|
|
|
|
'TRY' => __( 'Turkish lira', 'woocommerce' ),
|
|
|
|
'TTD' => __( 'Trinidad and Tobago dollar', 'woocommerce' ),
|
|
|
|
'TWD' => __( 'New Taiwan dollar', 'woocommerce' ),
|
|
|
|
'TZS' => __( 'Tanzanian shilling', 'woocommerce' ),
|
|
|
|
'UAH' => __( 'Ukrainian hryvnia', 'woocommerce' ),
|
|
|
|
'UGX' => __( 'Ugandan shilling', 'woocommerce' ),
|
|
|
|
'USD' => __( 'United States dollar', 'woocommerce' ),
|
|
|
|
'UYU' => __( 'Uruguayan peso', 'woocommerce' ),
|
|
|
|
'UZS' => __( 'Uzbekistani som', 'woocommerce' ),
|
|
|
|
'VEF' => __( 'Venezuelan bolívar', 'woocommerce' ),
|
|
|
|
'VND' => __( 'Vietnamese đồng', 'woocommerce' ),
|
|
|
|
'VUV' => __( 'Vanuatu vatu', 'woocommerce' ),
|
|
|
|
'WST' => __( 'Samoan tālā', 'woocommerce' ),
|
|
|
|
'XAF' => __( 'Central African CFA franc', 'woocommerce' ),
|
|
|
|
'XCD' => __( 'East Caribbean dollar', 'woocommerce' ),
|
|
|
|
'XOF' => __( 'West African CFA franc', 'woocommerce' ),
|
|
|
|
'XPF' => __( 'CFP franc', 'woocommerce' ),
|
|
|
|
'YER' => __( 'Yemeni rial', 'woocommerce' ),
|
2015-10-28 18:24:06 +00:00
|
|
|
'ZAR' => __( 'South African rand', 'woocommerce' ),
|
2016-04-18 23:21:14 +00:00
|
|
|
'ZMW' => __( 'Zambian kwacha', '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(
|
2016-04-18 23:21:14 +00:00
|
|
|
'AED' => 'د.إ',
|
|
|
|
'AFN' => '؋',
|
|
|
|
'ALL' => 'L',
|
|
|
|
'AMD' => 'AMD',
|
|
|
|
'ANG' => 'ƒ',
|
|
|
|
'AOA' => 'Kz',
|
2015-12-16 15:23:44 +00:00
|
|
|
'ARS' => '$',
|
|
|
|
'AUD' => '$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'AWG' => 'ƒ',
|
|
|
|
'AZN' => 'AZN',
|
|
|
|
'BAM' => 'KM',
|
|
|
|
'BBD' => '$',
|
2015-12-16 15:23:44 +00:00
|
|
|
'BDT' => '৳ ',
|
|
|
|
'BGN' => 'лв.',
|
2016-04-18 23:21:14 +00:00
|
|
|
'BHD' => '.د.ب',
|
|
|
|
'BIF' => 'Fr',
|
|
|
|
'BMD' => '$',
|
|
|
|
'BND' => '$',
|
|
|
|
'BOB' => 'Bs.',
|
2015-12-16 15:23:44 +00:00
|
|
|
'BRL' => 'R$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'BSD' => '$',
|
2016-04-18 23:28:04 +00:00
|
|
|
'BTC' => '฿',
|
2016-04-18 23:21:14 +00:00
|
|
|
'BTN' => 'Nu.',
|
|
|
|
'BWP' => 'P',
|
|
|
|
'BYR' => 'Br',
|
|
|
|
'BZD' => '$',
|
2015-12-16 15:23:44 +00:00
|
|
|
'CAD' => '$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'CDF' => 'Fr',
|
2015-12-16 15:23:44 +00:00
|
|
|
'CHF' => 'CHF',
|
|
|
|
'CLP' => '$',
|
|
|
|
'CNY' => '¥',
|
|
|
|
'COP' => '$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'CRC' => '₡',
|
|
|
|
'CUC' => '$',
|
|
|
|
'CUP' => '$',
|
|
|
|
'CVE' => '$',
|
2015-12-16 15:23:44 +00:00
|
|
|
'CZK' => 'Kč',
|
2016-04-18 23:21:14 +00:00
|
|
|
'DJF' => 'Fr',
|
2015-12-16 15:23:44 +00:00
|
|
|
'DKK' => 'DKK',
|
|
|
|
'DOP' => 'RD$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'DZD' => 'د.ج',
|
2015-12-16 15:23:44 +00:00
|
|
|
'EGP' => 'EGP',
|
2016-04-18 23:21:14 +00:00
|
|
|
'ERN' => 'Nfk',
|
|
|
|
'ETB' => 'Br',
|
2015-12-16 15:23:44 +00:00
|
|
|
'EUR' => '€',
|
2016-04-18 23:21:14 +00:00
|
|
|
'FJD' => '$',
|
|
|
|
'FKP' => '£',
|
2015-12-16 15:23:44 +00:00
|
|
|
'GBP' => '£',
|
2016-04-18 23:21:14 +00:00
|
|
|
'GEL' => 'ლ',
|
|
|
|
'GGP' => '£',
|
|
|
|
'GHS' => '₵',
|
|
|
|
'GIP' => '£',
|
|
|
|
'GMD' => 'D',
|
|
|
|
'GNF' => 'Fr',
|
|
|
|
'GTQ' => 'Q',
|
|
|
|
'GYD' => '$',
|
2015-12-16 15:23:44 +00:00
|
|
|
'HKD' => '$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'HNL' => 'L',
|
2015-12-16 15:23:44 +00:00
|
|
|
'HRK' => 'Kn',
|
2016-04-18 23:21:14 +00:00
|
|
|
'HTG' => 'G',
|
2015-12-16 15:23:44 +00:00
|
|
|
'HUF' => 'Ft',
|
|
|
|
'IDR' => 'Rp',
|
|
|
|
'ILS' => '₪',
|
2016-04-18 23:21:14 +00:00
|
|
|
'IMP' => '£',
|
2015-12-16 15:23:44 +00:00
|
|
|
'INR' => '₹',
|
2016-04-18 23:21:14 +00:00
|
|
|
'IQD' => 'ع.د',
|
|
|
|
'IRR' => '﷼',
|
2016-12-19 12:40:11 +00:00
|
|
|
'IRT' => 'تومان',
|
2016-10-19 13:49:20 +00:00
|
|
|
'ISK' => 'kr.',
|
2016-04-18 23:21:14 +00:00
|
|
|
'JEP' => '£',
|
|
|
|
'JMD' => '$',
|
|
|
|
'JOD' => 'د.ا',
|
2015-12-16 15:23:44 +00:00
|
|
|
'JPY' => '¥',
|
|
|
|
'KES' => 'KSh',
|
2016-09-21 18:15:51 +00:00
|
|
|
'KGS' => 'сом',
|
2016-04-18 23:21:14 +00:00
|
|
|
'KHR' => '៛',
|
|
|
|
'KMF' => 'Fr',
|
|
|
|
'KPW' => '₩',
|
2015-12-16 15:23:44 +00:00
|
|
|
'KRW' => '₩',
|
2016-04-18 23:21:14 +00:00
|
|
|
'KWD' => 'د.ك',
|
|
|
|
'KYD' => '$',
|
|
|
|
'KZT' => 'KZT',
|
2016-01-25 22:59:19 +00:00
|
|
|
'LAK' => '₭',
|
2016-04-18 23:21:14 +00:00
|
|
|
'LBP' => 'ل.ل',
|
|
|
|
'LKR' => 'රු',
|
|
|
|
'LRD' => '$',
|
|
|
|
'LSL' => 'L',
|
|
|
|
'LYD' => 'ل.د',
|
|
|
|
'MAD' => 'د.م.',
|
2017-01-30 09:35:47 +00:00
|
|
|
'MDL' => 'MDL',
|
2016-04-18 23:21:14 +00:00
|
|
|
'MGA' => 'Ar',
|
|
|
|
'MKD' => 'ден',
|
|
|
|
'MMK' => 'Ks',
|
|
|
|
'MNT' => '₮',
|
|
|
|
'MOP' => 'P',
|
|
|
|
'MRO' => 'UM',
|
|
|
|
'MUR' => '₨',
|
|
|
|
'MVR' => '.ރ',
|
|
|
|
'MWK' => 'MK',
|
2015-12-16 15:23:44 +00:00
|
|
|
'MXN' => '$',
|
|
|
|
'MYR' => 'RM',
|
2016-04-18 23:21:14 +00:00
|
|
|
'MZN' => 'MT',
|
|
|
|
'NAD' => '$',
|
2015-12-16 15:23:44 +00:00
|
|
|
'NGN' => '₦',
|
2016-04-18 23:21:14 +00:00
|
|
|
'NIO' => 'C$',
|
2015-12-16 15:23:44 +00:00
|
|
|
'NOK' => 'kr',
|
|
|
|
'NPR' => '₨',
|
|
|
|
'NZD' => '$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'OMR' => 'ر.ع.',
|
|
|
|
'PAB' => 'B/.',
|
|
|
|
'PEN' => 'S/.',
|
|
|
|
'PGK' => 'K',
|
2015-12-16 15:23:44 +00:00
|
|
|
'PHP' => '₱',
|
|
|
|
'PKR' => '₨',
|
|
|
|
'PLN' => 'zł',
|
2016-04-18 23:21:14 +00:00
|
|
|
'PRB' => 'р.',
|
2015-12-16 15:23:44 +00:00
|
|
|
'PYG' => '₲',
|
2016-04-18 23:21:14 +00:00
|
|
|
'QAR' => 'ر.ق',
|
2015-12-16 15:23:44 +00:00
|
|
|
'RMB' => '¥',
|
|
|
|
'RON' => 'lei',
|
2016-04-18 23:21:14 +00:00
|
|
|
'RSD' => 'дин.',
|
2016-01-27 17:22:43 +00:00
|
|
|
'RUB' => '₽',
|
2016-04-18 23:21:14 +00:00
|
|
|
'RWF' => 'Fr',
|
2016-01-25 22:59:19 +00:00
|
|
|
'SAR' => 'ر.س',
|
2016-04-18 23:21:14 +00:00
|
|
|
'SBD' => '$',
|
|
|
|
'SCR' => '₨',
|
|
|
|
'SDG' => 'ج.س.',
|
2015-12-16 15:23:44 +00:00
|
|
|
'SEK' => 'kr',
|
|
|
|
'SGD' => '$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'SHP' => '£',
|
|
|
|
'SLL' => 'Le',
|
|
|
|
'SOS' => 'Sh',
|
|
|
|
'SRD' => '$',
|
|
|
|
'SSP' => '£',
|
|
|
|
'STD' => 'Db',
|
|
|
|
'SYP' => 'ل.س',
|
|
|
|
'SZL' => 'L',
|
2015-12-16 15:23:44 +00:00
|
|
|
'THB' => '฿',
|
2016-04-18 23:21:14 +00:00
|
|
|
'TJS' => 'ЅМ',
|
|
|
|
'TMT' => 'm',
|
|
|
|
'TND' => 'د.ت',
|
|
|
|
'TOP' => 'T$',
|
2015-12-16 15:23:44 +00:00
|
|
|
'TRY' => '₺',
|
2016-04-18 23:21:14 +00:00
|
|
|
'TTD' => '$',
|
2015-12-16 15:23:44 +00:00
|
|
|
'TWD' => 'NT$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'TZS' => 'Sh',
|
2015-12-16 15:23:44 +00:00
|
|
|
'UAH' => '₴',
|
2016-04-02 11:11:35 +00:00
|
|
|
'UGX' => 'UGX',
|
2015-12-16 15:23:44 +00:00
|
|
|
'USD' => '$',
|
2016-04-18 23:21:14 +00:00
|
|
|
'UYU' => '$',
|
|
|
|
'UZS' => 'UZS',
|
|
|
|
'VEF' => 'Bs F',
|
2015-12-16 15:23:44 +00:00
|
|
|
'VND' => '₫',
|
2016-04-18 23:21:14 +00:00
|
|
|
'VUV' => 'Vt',
|
|
|
|
'WST' => 'T',
|
|
|
|
'XAF' => 'Fr',
|
|
|
|
'XCD' => '$',
|
|
|
|
'XOF' => 'Fr',
|
|
|
|
'XPF' => 'Fr',
|
|
|
|
'YER' => '﷼',
|
2015-12-16 15:23:44 +00:00
|
|
|
'ZAR' => 'R',
|
2016-04-18 23:21:14 +00:00
|
|
|
'ZMW' => 'ZK',
|
2015-12-16 15:23:44 +00:00
|
|
|
) );
|
|
|
|
|
|
|
|
$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
|
|
|
*
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param array|string $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,
|
2016-08-27 01:46:45 +00:00
|
|
|
'crop' => $crop,
|
2015-02-14 01:18:07 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$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',
|
2016-08-27 01:46:45 +00:00
|
|
|
'crop' => 1,
|
2013-09-12 13:41:02 +00:00
|
|
|
);
|
|
|
|
}
|
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.
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param bool $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 ) {
|
2016-06-07 10:03:16 +00:00
|
|
|
$version = defined( 'WC_API_REQUEST_VERSION' ) ? WC_API_REQUEST_VERSION : substr( WC_API::VERSION, 0, 1 );
|
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
|
|
|
|
2017-01-12 19:39:39 +00:00
|
|
|
/**
|
|
|
|
* Get a log file path.
|
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*
|
|
|
|
* @param string $handle name.
|
|
|
|
* @return string the log file path.
|
|
|
|
*/
|
|
|
|
function wc_get_log_file_path( $handle ) {
|
|
|
|
return WC_Log_Handler_File::get_log_file_path( $handle );
|
|
|
|
}
|
|
|
|
|
2014-06-02 20:00:44 +00:00
|
|
|
/**
|
2016-07-26 12:39:31 +00:00
|
|
|
* Recursively get page children.
|
|
|
|
* @param int $page_id
|
|
|
|
* @return int[]
|
2014-06-04 06:46:21 +00:00
|
|
|
*/
|
2016-07-26 12:39:31 +00:00
|
|
|
function wc_get_page_children( $page_id ) {
|
|
|
|
$page_ids = get_posts( array(
|
|
|
|
'post_parent' => $page_id,
|
|
|
|
'post_type' => 'page',
|
|
|
|
'numberposts' => -1,
|
|
|
|
'post_status' => 'any',
|
|
|
|
'fields' => 'ids',
|
|
|
|
) );
|
|
|
|
|
|
|
|
if ( ! empty( $page_ids ) ) {
|
|
|
|
foreach ( $page_ids as $page_id ) {
|
|
|
|
$page_ids = array_merge( $page_ids, wc_get_page_children( $page_id ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $page_ids;
|
|
|
|
}
|
2014-06-04 06:46:21 +00:00
|
|
|
|
2016-07-26 12:39:31 +00:00
|
|
|
/**
|
|
|
|
* Flushes rewrite rules when the shop page (or it's children) gets saved.
|
|
|
|
*/
|
2017-04-27 00:51:30 +00:00
|
|
|
function flush_rewrite_rules_on_shop_page_save() {
|
2017-04-27 15:42:16 +00:00
|
|
|
$screen = get_current_screen();
|
|
|
|
$screen_id = $screen ? $screen->id : '';
|
|
|
|
|
|
|
|
// Check if this is the edit page.
|
|
|
|
if ( 'page' !== $screen_id ) {
|
2017-04-27 00:51:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if page is edited.
|
|
|
|
if ( empty( $_GET['post'] ) || empty( $_GET['action'] ) || ( isset( $_GET['action'] ) && 'edit' !== $_GET['action'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$post_id = intval( $_GET['post'] );
|
2016-07-26 12:39:31 +00:00
|
|
|
$shop_page_id = wc_get_page_id( 'shop' );
|
2017-04-27 00:51:30 +00:00
|
|
|
|
2016-07-26 12:39:31 +00:00
|
|
|
if ( $shop_page_id === $post_id || in_array( $post_id, wc_get_page_children( $shop_page_id ) ) ) {
|
2016-10-21 14:29:50 +00:00
|
|
|
do_action( 'woocommerce_flush_rewrite_rules' );
|
2014-06-04 06:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-27 00:51:30 +00:00
|
|
|
add_action( 'admin_footer', 'flush_rewrite_rules_on_shop_page_save' );
|
2014-06-04 06:46:21 +00:00
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
|
2017-02-07 16:16:45 +00:00
|
|
|
$permalinks = wc_get_permalink_structure();
|
2014-06-02 20:00:44 +00:00
|
|
|
|
2015-10-31 19:10:45 +00:00
|
|
|
// Fix the rewrite rules when the product permalink have %product_cat% flag.
|
2017-02-07 16:16:45 +00:00
|
|
|
if ( preg_match( '`/(.+)(/%product_cat%)`' , $permalinks['product_rewrite_slug'], $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 ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 12:39:31 +00:00
|
|
|
// If the shop page is used as the base, we need to handle shop page subpages to avoid 404s.
|
2017-02-07 16:16:45 +00:00
|
|
|
if ( $permalinks['use_verbose_page_rules'] && ( $shop_page_id = wc_get_page_id( 'shop' ) ) ) {
|
2016-07-26 12:39:31 +00:00
|
|
|
$page_rewrite_rules = array();
|
|
|
|
$subpages = wc_get_page_children( $shop_page_id );
|
|
|
|
|
|
|
|
// Subpage rules
|
|
|
|
foreach ( $subpages as $subpage ) {
|
|
|
|
$uri = get_page_uri( $subpage );
|
|
|
|
$page_rewrite_rules[ $uri . '/?$' ] = 'index.php?pagename=' . $uri;
|
|
|
|
$wp_generated_rewrite_rules = $wp_rewrite->generate_rewrite_rules( $uri, EP_PAGES, true, true, false, false );
|
|
|
|
foreach ( $wp_generated_rewrite_rules as $key => $value ) {
|
|
|
|
$wp_generated_rewrite_rules[ $key ] = $value . '&pagename=' . $uri;
|
|
|
|
}
|
|
|
|
$page_rewrite_rules = array_merge( $page_rewrite_rules, $wp_generated_rewrite_rules );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge with rules
|
|
|
|
$rules = array_merge( $page_rewrite_rules, $rules );
|
2014-06-04 06:46:21 +00:00
|
|
|
}
|
|
|
|
|
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 ) {
|
|
|
|
$post = get_post( $post_id );
|
|
|
|
if ( 'product' === get_post_type( $post->post_parent ) ) {
|
2017-02-07 16:16:45 +00:00
|
|
|
$permalinks = wc_get_permalink_structure();
|
|
|
|
if ( preg_match( '/\/(.+)(\/%product_cat%)$/', $permalinks['product_rewrite_slug'], $matches ) ) {
|
2014-08-06 10:50:03 +00:00
|
|
|
$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() {
|
2016-11-25 11:57:42 +00:00
|
|
|
return array( 'twentyseventeen', '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
|
|
|
|
|
|
|
/**
|
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,
|
2016-08-27 01:46:45 +00:00
|
|
|
'state' => $state,
|
2014-11-18 14:56:13 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
* @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
|
|
|
* @since 2.3.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_get_customer_default_location() {
|
2016-04-01 10:14:24 +00:00
|
|
|
$location = array();
|
|
|
|
|
2014-12-23 17:33:51 +00:00
|
|
|
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-04-01 10:14:24 +00:00
|
|
|
// Exclude common bots from geolocation by user agent.
|
2016-08-09 13:02:40 +00:00
|
|
|
$ua = wc_get_user_agent();
|
2014-12-23 18:50:05 +00:00
|
|
|
|
2016-04-01 10:14:24 +00:00
|
|
|
if ( ! strstr( $ua, 'bot' ) && ! strstr( $ua, 'spider' ) && ! strstr( $ua, 'crawl' ) ) {
|
|
|
|
$location = WC_Geolocation::geolocate_ip( '', true, false );
|
2014-12-23 18:50:05 +00:00
|
|
|
}
|
2016-04-18 13:51:39 +00:00
|
|
|
|
|
|
|
// Base fallback.
|
|
|
|
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
|
|
|
|
2016-08-09 13:02:40 +00:00
|
|
|
/**
|
|
|
|
* Get user agent string.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-08-09 13:02:40 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_get_user_agent() {
|
|
|
|
return isset( $_SERVER['HTTP_USER_AGENT'] ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : '';
|
|
|
|
}
|
|
|
|
|
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 );
|
2016-09-07 22:32:24 +00:00
|
|
|
if ( strlen( $b ) !== $a_length ) {
|
2015-01-19 05:11:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
// Do not attempt to "optimize" this.
|
|
|
|
for ( $i = 0; $i < $a_length; $i++ ) {
|
|
|
|
$result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
|
|
|
|
}
|
|
|
|
|
2016-09-07 22:32:24 +00:00
|
|
|
return 0 === $result;
|
2015-01-19 05:11:41 +00:00
|
|
|
}
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2016-05-03 14:16:20 +00:00
|
|
|
if ( ! function_exists( 'wc_get_shipping_zone' ) ) {
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
}
|
2015-12-18 17:10:58 +00:00
|
|
|
}
|
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-11-04 15:41:51 +00:00
|
|
|
echo '<small class="wc-admin-breadcrumb"><a href="' . esc_url( $url ) . '" aria-label="' . esc_attr( $label ) . '">⤴</a></small>';
|
2016-02-17 16:02:22 +00:00
|
|
|
}
|
2016-03-31 14:30:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a WooCommerce help tip.
|
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
*
|
|
|
|
* @param string $tip Help tip text
|
|
|
|
* @param bool $allow_html Allow sanitized HTML if true or escape
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_help_tip( $tip, $allow_html = false ) {
|
|
|
|
if ( $allow_html ) {
|
|
|
|
$tip = wc_sanitize_tooltip( $tip );
|
|
|
|
} else {
|
|
|
|
$tip = esc_attr( $tip );
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<span class="woocommerce-help-tip" data-tip="' . $tip . '"></span>';
|
|
|
|
}
|
2016-04-22 13:02:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of potential postcodes for wildcard searching.
|
2016-04-22 15:47:54 +00:00
|
|
|
* @since 2.6.0
|
2016-04-22 13:02:36 +00:00
|
|
|
* @param string $postcode
|
2016-06-16 10:28:53 +00:00
|
|
|
* @param string $country to format postcode for matching.
|
2016-06-06 16:50:18 +00:00
|
|
|
* @return string[]
|
2016-04-22 13:02:36 +00:00
|
|
|
*/
|
2016-06-16 10:28:53 +00:00
|
|
|
function wc_get_wildcard_postcodes( $postcode, $country = '' ) {
|
2016-10-12 10:52:39 +00:00
|
|
|
$formatted_postcode = wc_format_postcode( $postcode, $country );
|
2017-01-22 01:24:20 +00:00
|
|
|
$length = function_exists( 'mb_strlen' ) ? mb_strlen( $formatted_postcode ) : strlen( $formatted_postcode );
|
2016-10-12 10:52:39 +00:00
|
|
|
$postcodes = array(
|
|
|
|
$postcode,
|
|
|
|
$formatted_postcode,
|
|
|
|
$formatted_postcode . '*',
|
|
|
|
);
|
2016-04-22 13:02:36 +00:00
|
|
|
|
2016-10-12 10:52:39 +00:00
|
|
|
for ( $i = 0; $i < $length; $i ++ ) {
|
2017-01-22 01:24:20 +00:00
|
|
|
$postcodes[] = ( function_exists( 'mb_substr' ) ? mb_substr( $formatted_postcode, 0, ( $i + 1 ) * -1 ) : substr( $formatted_postcode, 0, ( $i + 1 ) * -1 ) ) . '*';
|
2016-04-22 13:02:36 +00:00
|
|
|
}
|
2016-06-16 10:28:53 +00:00
|
|
|
|
2016-04-22 13:02:36 +00:00
|
|
|
return $postcodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by shipping zones and taxes to compare a given $postcode to stored
|
|
|
|
* postcodes to find matches for numerical ranges, and wildcards.
|
2016-04-22 15:47:54 +00:00
|
|
|
* @since 2.6.0
|
2016-04-22 13:02:36 +00:00
|
|
|
* @param string $postcode Postcode you want to match against stored postcodes
|
2016-06-16 10:28:53 +00:00
|
|
|
* @param array $objects Array of postcode objects from Database
|
2016-06-10 23:26:07 +00:00
|
|
|
* @param string $object_id_key DB column name for the ID.
|
2016-04-22 13:02:36 +00:00
|
|
|
* @param string $object_compare_key DB column name for the value.
|
2016-06-16 10:28:53 +00:00
|
|
|
* @param string $country Country from which this postcode belongs. Allows for formatting.
|
2016-06-10 23:26:07 +00:00
|
|
|
* @return array Array of matching object ID and matching values.
|
2016-04-22 13:02:36 +00:00
|
|
|
*/
|
2016-06-16 10:28:53 +00:00
|
|
|
function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key, $country = '' ) {
|
2016-04-28 16:51:43 +00:00
|
|
|
$postcode = wc_normalize_postcode( $postcode );
|
2016-06-16 10:28:53 +00:00
|
|
|
$wildcard_postcodes = array_map( 'wc_clean', wc_get_wildcard_postcodes( $postcode, $country ) );
|
2016-04-27 23:43:48 +00:00
|
|
|
$matches = array();
|
2016-04-22 13:02:36 +00:00
|
|
|
|
2016-06-10 23:26:07 +00:00
|
|
|
foreach ( $objects as $object ) {
|
|
|
|
$object_id = $object->$object_id_key;
|
|
|
|
$compare_against = $object->$object_compare_key;
|
2016-04-22 13:02:36 +00:00
|
|
|
|
2016-04-27 23:43:48 +00:00
|
|
|
// Handle postcodes containing ranges.
|
2016-05-23 16:30:37 +00:00
|
|
|
if ( strstr( $compare_against, '...' ) ) {
|
|
|
|
$range = array_map( 'trim', explode( '...', $compare_against ) );
|
2016-04-22 13:02:36 +00:00
|
|
|
|
|
|
|
if ( 2 !== sizeof( $range ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
list( $min, $max ) = $range;
|
|
|
|
|
2016-04-27 23:43:48 +00:00
|
|
|
// If the postcode is non-numeric, make it numeric.
|
2016-04-22 13:02:36 +00:00
|
|
|
if ( ! is_numeric( $min ) || ! is_numeric( $max ) ) {
|
2016-06-10 23:26:07 +00:00
|
|
|
$compare = wc_make_numeric_postcode( $postcode );
|
2016-05-26 14:09:03 +00:00
|
|
|
$min = str_pad( wc_make_numeric_postcode( $min ), strlen( $compare ), '0' );
|
|
|
|
$max = str_pad( wc_make_numeric_postcode( $max ), strlen( $compare ), '0' );
|
2016-06-10 23:26:07 +00:00
|
|
|
} else {
|
|
|
|
$compare = $postcode;
|
2016-04-22 13:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $compare >= $min && $compare <= $max ) {
|
2016-06-10 23:26:07 +00:00
|
|
|
$matches[ $object_id ] = isset( $matches[ $object_id ] ) ? $matches[ $object_id ]: array();
|
|
|
|
$matches[ $object_id ][] = $compare_against;
|
2016-04-22 13:02:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 23:43:48 +00:00
|
|
|
// Wildcard and standard comparison.
|
2016-04-22 13:02:36 +00:00
|
|
|
} elseif ( in_array( $compare_against, $wildcard_postcodes ) ) {
|
2016-06-10 23:26:07 +00:00
|
|
|
$matches[ $object_id ] = isset( $matches[ $object_id ] ) ? $matches[ $object_id ]: array();
|
|
|
|
$matches[ $object_id ][] = $compare_against;
|
2016-04-22 13:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matches;
|
|
|
|
}
|
2016-05-25 11:04:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets number of shipping methods currently enabled. Used to identify if
|
|
|
|
* shipping is configured.
|
2016-05-25 19:06:06 +00:00
|
|
|
*
|
2016-05-25 11:04:42 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param bool $include_legacy Count legacy shipping methods too.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function wc_get_shipping_method_count( $include_legacy = false ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$transient_name = 'wc_shipping_method_count_' . ( $include_legacy ? 1 : 0 ) . '_' . WC_Cache_Helper::get_transient_version( 'shipping' );
|
2016-05-25 11:36:17 +00:00
|
|
|
$method_count = get_transient( $transient_name );
|
2016-05-25 11:04:42 +00:00
|
|
|
|
|
|
|
if ( false === $method_count ) {
|
|
|
|
$method_count = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" ) );
|
|
|
|
|
|
|
|
if ( $include_legacy ) {
|
2016-05-26 11:54:14 +00:00
|
|
|
// Count activated methods that don't support shipping zones.
|
2016-05-25 21:29:32 +00:00
|
|
|
$methods = WC()->shipping->get_shipping_methods();
|
2016-05-26 11:54:14 +00:00
|
|
|
|
2016-05-25 19:06:06 +00:00
|
|
|
foreach ( $methods as $method ) {
|
2016-05-26 11:54:14 +00:00
|
|
|
if ( isset( $method->enabled ) && 'yes' === $method->enabled && ! $method->supports( 'shipping-zones' ) ) {
|
2016-05-25 19:06:06 +00:00
|
|
|
$method_count++;
|
2016-05-25 11:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_transient( $transient_name, $method_count, DAY_IN_SECONDS * 30 );
|
|
|
|
}
|
|
|
|
|
2016-05-25 11:36:17 +00:00
|
|
|
return absint( $method_count );
|
2016-05-25 11:04:42 +00:00
|
|
|
}
|
2016-06-06 15:55:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for set_time_limit to see if it is enabled.
|
2016-06-06 16:24:31 +00:00
|
|
|
* @since 2.6.0
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
|
|
|
* @param int $limit
|
2016-06-06 15:55:27 +00:00
|
|
|
*/
|
|
|
|
function wc_set_time_limit( $limit = 0 ) {
|
|
|
|
if ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
|
|
|
|
@set_time_limit( $limit );
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 16:24:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to sort products attributes with uasort.
|
|
|
|
* @since 2.6.0
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
|
|
|
* @param array $a
|
|
|
|
* @param array $b
|
|
|
|
*
|
|
|
|
* @return int
|
2016-06-06 16:24:31 +00:00
|
|
|
*/
|
|
|
|
function wc_product_attribute_uasort_comparison( $a, $b ) {
|
2016-10-26 17:02:50 +00:00
|
|
|
if ( $a['position'] === $b['position'] ) {
|
2016-06-06 16:24:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ( $a['position'] < $b['position'] ) ? -1 : 1;
|
|
|
|
}
|
2016-07-11 15:26:54 +00:00
|
|
|
|
2016-07-21 13:30:05 +00:00
|
|
|
/**
|
2016-07-21 14:30:07 +00:00
|
|
|
* Used to sort shipping zone methods with uasort.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
|
|
|
* @param array $a
|
|
|
|
* @param array $b
|
|
|
|
*
|
|
|
|
* @return int
|
2016-07-21 13:30:05 +00:00
|
|
|
*/
|
|
|
|
function wc_shipping_zone_method_order_uasort_comparison( $a, $b ) {
|
|
|
|
if ( $a->method_order === $b->method_order ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ( $a->method_order < $b->method_order ) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-11 15:26:54 +00:00
|
|
|
/**
|
2016-07-21 09:14:12 +00:00
|
|
|
* Get rounding precision for internal WC calculations.
|
|
|
|
* Will increase the precision of wc_get_price_decimals by 2 decimals, unless WC_ROUNDING_PRECISION is set to a higher number.
|
2016-07-11 15:26:54 +00:00
|
|
|
*
|
|
|
|
* @since 2.6.3
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function wc_get_rounding_precision() {
|
2016-07-21 09:14:12 +00:00
|
|
|
$precision = wc_get_price_decimals() + 2;
|
|
|
|
if ( absint( WC_ROUNDING_PRECISION ) > $precision ) {
|
|
|
|
$precision = absint( WC_ROUNDING_PRECISION );
|
2016-07-11 15:26:54 +00:00
|
|
|
}
|
2016-07-21 09:14:12 +00:00
|
|
|
return $precision;
|
2016-07-11 15:26:54 +00:00
|
|
|
}
|
2016-08-08 12:59:23 +00:00
|
|
|
|
|
|
|
/**
|
2017-01-14 17:06:10 +00:00
|
|
|
* Get a shared logger instance.
|
|
|
|
*
|
2017-01-15 12:29:10 +00:00
|
|
|
* Use the woocommerce_logging_class filter to change the logging class. You may provide one of the following:
|
|
|
|
* - a class name which will be instantiated as `new $class` with no arguments
|
|
|
|
* - an instance which will be used directly as the logger
|
|
|
|
* In either case, the class or instance *must* implement WC_Logger_Interface.
|
2017-01-14 17:06:10 +00:00
|
|
|
*
|
|
|
|
* @see WC_Logger_Interface
|
|
|
|
*
|
2016-08-08 12:59:23 +00:00
|
|
|
* @return WC_Logger
|
|
|
|
*/
|
|
|
|
function wc_get_logger() {
|
2016-12-15 19:52:24 +00:00
|
|
|
static $logger = null;
|
|
|
|
if ( null === $logger ) {
|
|
|
|
$class = apply_filters( 'woocommerce_logging_class', 'WC_Logger' );
|
2017-01-14 17:06:10 +00:00
|
|
|
$implements = class_implements( $class );
|
|
|
|
if ( is_array( $implements ) && in_array( 'WC_Logger_Interface', $implements ) ) {
|
2017-01-15 12:29:10 +00:00
|
|
|
if ( is_object( $class ) ) {
|
|
|
|
$logger = $class;
|
|
|
|
} else {
|
|
|
|
$logger = new $class;
|
|
|
|
}
|
2017-01-14 17:06:10 +00:00
|
|
|
} else {
|
|
|
|
wc_doing_it_wrong(
|
|
|
|
__FUNCTION__,
|
|
|
|
sprintf(
|
|
|
|
__( 'The class <code>%s</code> provided by woocommerce_logging_class filter must implement <code>WC_Logger_Interface</code>.', 'woocommerce' ),
|
2017-01-15 12:29:10 +00:00
|
|
|
esc_html( is_object( $class ) ? get_class( $class ) : $class )
|
2017-01-14 17:06:10 +00:00
|
|
|
),
|
2017-03-15 16:36:53 +00:00
|
|
|
'3.0'
|
2017-01-14 17:06:10 +00:00
|
|
|
);
|
|
|
|
$logger = new WC_Logger();
|
|
|
|
}
|
2016-08-08 12:59:23 +00:00
|
|
|
}
|
2016-12-15 19:52:24 +00:00
|
|
|
return $logger;
|
2016-08-08 12:59:23 +00:00
|
|
|
}
|
2016-08-15 10:02:49 +00:00
|
|
|
|
2016-11-20 21:10:04 +00:00
|
|
|
/**
|
2016-12-16 21:51:20 +00:00
|
|
|
* Prints human-readable information about a variable.
|
2016-11-20 21:10:04 +00:00
|
|
|
*
|
|
|
|
* Some server environments blacklist some debugging functions. This function provides a safe way to
|
2016-12-16 21:51:20 +00:00
|
|
|
* turn an expression into a printable, readable form without calling blacklisted functions.
|
2016-11-20 21:10:04 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2016-12-16 21:51:20 +00:00
|
|
|
*
|
2016-11-20 21:10:04 +00:00
|
|
|
* @param mixed $expression The expression to be printed.
|
2016-12-16 21:51:20 +00:00
|
|
|
* @param bool $return Optional. Default false. Set to true to return the human-readable string.
|
|
|
|
* @return string|bool False if expression could not be printed. True if the expression was printed.
|
|
|
|
* If $return is true, a string representation will be returned.
|
2016-11-20 21:10:04 +00:00
|
|
|
*/
|
2016-12-16 21:51:20 +00:00
|
|
|
function wc_print_r( $expression, $return = false ) {
|
2016-11-20 21:10:04 +00:00
|
|
|
$alternatives = array(
|
|
|
|
array( 'func' => 'print_r', 'args' => array( $expression, true ) ),
|
|
|
|
array( 'func' => 'var_export', 'args' => array( $expression, true ) ),
|
|
|
|
array( 'func' => 'json_encode', 'args' => array( $expression ) ),
|
|
|
|
array( 'func' => 'serialize', 'args' => array( $expression ) ),
|
|
|
|
);
|
|
|
|
|
2016-12-16 21:51:20 +00:00
|
|
|
$alternatives = apply_filters( 'woocommerce_print_r_alternatives', $alternatives, $expression );
|
2016-11-20 21:10:04 +00:00
|
|
|
|
|
|
|
foreach ( $alternatives as $alternative ) {
|
|
|
|
if ( function_exists( $alternative['func'] ) ) {
|
2016-12-16 21:51:20 +00:00
|
|
|
$res = call_user_func_array( $alternative['func'], $alternative['args'] );
|
|
|
|
if ( $return ) {
|
|
|
|
return $res;
|
|
|
|
} else {
|
|
|
|
echo $res;
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-20 21:10:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:42:32 +00:00
|
|
|
/**
|
2016-12-03 17:23:52 +00:00
|
|
|
* Registers the default log handler.
|
2016-11-12 18:42:32 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2016-11-12 18:42:32 +00:00
|
|
|
* @param array $handlers
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-12-03 17:23:52 +00:00
|
|
|
function wc_register_default_log_handler( $handlers ) {
|
2016-12-22 21:29:47 +00:00
|
|
|
|
|
|
|
if ( defined( 'WC_LOG_HANDLER' ) && class_exists( WC_LOG_HANDLER ) ) {
|
|
|
|
$handler_class = WC_LOG_HANDLER;
|
|
|
|
$default_handler = new $handler_class();
|
2016-12-03 17:23:52 +00:00
|
|
|
} else {
|
2016-12-22 21:29:47 +00:00
|
|
|
$default_handler = new WC_Log_Handler_File();
|
2016-11-12 18:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 21:29:47 +00:00
|
|
|
array_push( $handlers, $default_handler );
|
|
|
|
|
2016-11-12 18:42:32 +00:00
|
|
|
return $handlers;
|
|
|
|
}
|
2016-12-22 21:29:47 +00:00
|
|
|
add_filter( 'woocommerce_register_log_handlers', 'wc_register_default_log_handler' );
|
2016-11-12 18:42:32 +00:00
|
|
|
|
2016-09-12 21:11:05 +00:00
|
|
|
/**
|
|
|
|
* Store user agents. Used for tracker.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
|
|
|
* @param string $user_login
|
|
|
|
* @param int|object $user
|
2016-09-12 21:11:05 +00:00
|
|
|
*/
|
|
|
|
function wc_maybe_store_user_agent( $user_login, $user ) {
|
|
|
|
if ( 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) && user_can( $user, 'manage_woocommerce' ) ) {
|
|
|
|
$admin_user_agents = array_filter( (array) get_option( 'woocommerce_tracker_ua', array() ) );
|
|
|
|
$admin_user_agents[] = wc_get_user_agent();
|
|
|
|
update_option( 'woocommerce_tracker_ua', array_unique( $admin_user_agents ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_action( 'wp_login', 'wc_maybe_store_user_agent', 10, 2 );
|
2016-10-27 13:08:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on wp_list_pluck, this calls a method instead of returning a property.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-10-27 13:08:16 +00:00
|
|
|
* @param array $list List of objects or arrays
|
|
|
|
* @param int|string $callback_or_field Callback method from the object to place instead of the entire object
|
|
|
|
* @param int|string $index_key Optional. Field from the object to use as keys for the new array.
|
|
|
|
* Default null.
|
|
|
|
* @return array Array of values.
|
|
|
|
*/
|
|
|
|
function wc_list_pluck( $list, $callback_or_field, $index_key = null ) {
|
|
|
|
// Use wp_list_pluck if this isn't a callback
|
|
|
|
$first_el = current( $list );
|
|
|
|
if ( ! is_object( $first_el ) || ! is_callable( array( $first_el, $callback_or_field ) ) ) {
|
|
|
|
return wp_list_pluck( $list, $callback_or_field, $index_key );
|
|
|
|
}
|
|
|
|
if ( ! $index_key ) {
|
|
|
|
/*
|
|
|
|
* This is simple. Could at some point wrap array_column()
|
|
|
|
* if we knew we had an array of arrays.
|
|
|
|
*/
|
|
|
|
foreach ( $list as $key => $value ) {
|
|
|
|
$list[ $key ] = $value->{$callback_or_field}();
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When index_key is not set for a particular item, push the value
|
|
|
|
* to the end of the stack. This is how array_column() behaves.
|
|
|
|
*/
|
|
|
|
$newlist = array();
|
|
|
|
foreach ( $list as $value ) {
|
|
|
|
if ( isset( $value->$index_key ) ) {
|
|
|
|
$newlist[ $value->$index_key ] = $value->{$callback_or_field}();
|
|
|
|
} else {
|
|
|
|
$newlist[] = $value->{$callback_or_field}();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $newlist;
|
|
|
|
}
|
2017-02-07 16:16:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get permalink settings for WooCommerce independent of the user locale.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-02-07 16:16:45 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_get_permalink_structure() {
|
2017-02-21 12:20:47 +00:00
|
|
|
if ( function_exists( 'switch_to_locale' ) && did_action( 'admin_init' ) ) {
|
2017-02-07 16:16:45 +00:00
|
|
|
switch_to_locale( get_locale() );
|
|
|
|
}
|
|
|
|
|
|
|
|
$permalinks = wp_parse_args( (array) get_option( 'woocommerce_permalinks', array() ), array(
|
|
|
|
'product_base' => '',
|
|
|
|
'category_base' => '',
|
|
|
|
'tag_base' => '',
|
|
|
|
'attribute_base' => '',
|
|
|
|
'use_verbose_page_rules' => false,
|
|
|
|
) );
|
|
|
|
|
|
|
|
// Ensure rewrite slugs are set.
|
|
|
|
$permalinks['product_rewrite_slug'] = untrailingslashit( empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'] );
|
|
|
|
$permalinks['category_rewrite_slug'] = untrailingslashit( empty( $permalinks['category_base'] ) ? _x( 'product-category', 'slug', 'woocommerce' ) : $permalinks['category_base'] );
|
|
|
|
$permalinks['tag_rewrite_slug'] = untrailingslashit( empty( $permalinks['tag_base'] ) ? _x( 'product-tag', 'slug', 'woocommerce' ) : $permalinks['tag_base'] );
|
|
|
|
$permalinks['attribute_rewrite_slug'] = untrailingslashit( empty( $permalinks['attribute_base'] ) ? '' : $permalinks['attribute_base'] );
|
|
|
|
|
2017-02-21 12:20:47 +00:00
|
|
|
if ( function_exists( 'restore_current_locale' ) && did_action( 'admin_init' ) ) {
|
2017-02-07 16:16:45 +00:00
|
|
|
restore_current_locale();
|
|
|
|
}
|
|
|
|
return $permalinks;
|
|
|
|
}
|