Fixed includes/wc-cart-functions.php PHPCS violations
This commit is contained in:
parent
fdab6665c0
commit
bea2ec7fba
|
@ -4,21 +4,17 @@
|
|||
*
|
||||
* Functions for cart specific things.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category Core
|
||||
* @package WooCommerce/Functions
|
||||
* @version 2.5.0
|
||||
* @package WooCommerce/Functions
|
||||
* @version 2.5.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Prevent password protected products being added to the cart.
|
||||
*
|
||||
* @param bool $passed
|
||||
* @param int $product_id
|
||||
* @param bool $passed Validation.
|
||||
* @param int $product_id Product ID.
|
||||
* @return bool
|
||||
*/
|
||||
function wc_protected_product_add_to_cart( $passed, $product_id ) {
|
||||
|
@ -43,8 +39,8 @@ function wc_empty_cart() {
|
|||
/**
|
||||
* Load the persistent cart.
|
||||
*
|
||||
* @param string $user_login
|
||||
* @param WP_User $user
|
||||
* @param string $user_login User login.
|
||||
* @param WP_User $user User data.
|
||||
* @deprecated 2.3
|
||||
*/
|
||||
function wc_load_persistent_cart( $user_login, $user ) {
|
||||
|
@ -58,7 +54,9 @@ function wc_load_persistent_cart( $user_login, $user ) {
|
|||
return;
|
||||
}
|
||||
|
||||
if ( empty( WC()->session->cart ) || ! is_array( WC()->session->cart ) || 0 === sizeof( WC()->session->cart ) ) {
|
||||
$cart = WC()->session->cart;
|
||||
|
||||
if ( empty( $cart ) || ! is_array( $cart ) || 0 === count( $cart ) ) {
|
||||
WC()->session->cart = $saved_cart['cart'];
|
||||
}
|
||||
}
|
||||
|
@ -76,10 +74,10 @@ function wc_get_raw_referer() {
|
|||
return wp_get_raw_referer();
|
||||
}
|
||||
|
||||
if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
|
||||
return wp_unslash( $_REQUEST['_wp_http_referer'] );
|
||||
} elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
|
||||
return wp_unslash( $_SERVER['HTTP_REFERER'] );
|
||||
if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { // WPCS: input var ok, CSRF ok.
|
||||
return wp_unslash( $_REQUEST['_wp_http_referer'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
|
||||
} elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) { // WPCS: input var ok, CSRF ok.
|
||||
return wp_unslash( $_SERVER['HTTP_REFERER'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -88,11 +86,11 @@ function wc_get_raw_referer() {
|
|||
/**
|
||||
* Add to cart messages.
|
||||
*
|
||||
* @param int|array $products
|
||||
* @param bool $show_qty Should qty's be shown? Added in 2.6.0
|
||||
* @param bool $return Return message rather than add it.
|
||||
* @param int|array $products Product ID list or single product ID.
|
||||
* @param bool $show_qty Should qty's be shown? Added in 2.6.0.
|
||||
* @param bool $return Return message rather than add it.
|
||||
*
|
||||
* @return mixed|void
|
||||
* @return mixed
|
||||
*/
|
||||
function wc_add_to_cart_message( $products, $show_qty = false, $return = false ) {
|
||||
$titles = array();
|
||||
|
@ -108,19 +106,21 @@ function wc_add_to_cart_message( $products, $show_qty = false, $return = false )
|
|||
}
|
||||
|
||||
foreach ( $products as $product_id => $qty ) {
|
||||
/* translators: %s: product name */
|
||||
$titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
|
||||
$count += $qty;
|
||||
$count += $qty;
|
||||
}
|
||||
|
||||
$titles = array_filter( $titles );
|
||||
$titles = array_filter( $titles );
|
||||
/* translators: %s: product name */
|
||||
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) );
|
||||
|
||||
// Output success messages
|
||||
// Output success messages.
|
||||
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
|
||||
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
|
||||
$message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
|
||||
} else {
|
||||
$message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
|
||||
$message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
|
||||
}
|
||||
|
||||
if ( has_filter( 'wc_add_to_cart_message' ) ) {
|
||||
|
@ -138,8 +138,9 @@ function wc_add_to_cart_message( $products, $show_qty = false, $return = false )
|
|||
}
|
||||
|
||||
/**
|
||||
* Comma separate a list of item names, and replace final comma with 'and'
|
||||
* @param array $items
|
||||
* Comma separate a list of item names, and replace final comma with 'and'.
|
||||
*
|
||||
* @param array $items Cart items.
|
||||
* @return string
|
||||
*/
|
||||
function wc_format_list_of_items( $items ) {
|
||||
|
@ -148,9 +149,9 @@ function wc_format_list_of_items( $items ) {
|
|||
foreach ( $items as $key => $item ) {
|
||||
$item_string .= $item;
|
||||
|
||||
if ( sizeof( $items ) === $key + 2 ) {
|
||||
if ( count( $items ) === $key + 2 ) {
|
||||
$item_string .= ' ' . __( 'and', 'woocommerce' ) . ' ';
|
||||
} elseif ( sizeof( $items ) !== $key + 1 ) {
|
||||
} elseif ( count( $items ) !== $key + 1 ) {
|
||||
$item_string .= ', ';
|
||||
}
|
||||
}
|
||||
|
@ -160,8 +161,6 @@ function wc_format_list_of_items( $items ) {
|
|||
|
||||
/**
|
||||
* Clear cart after payment.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function wc_clear_cart_after_payment() {
|
||||
global $wp;
|
||||
|
@ -169,7 +168,7 @@ function wc_clear_cart_after_payment() {
|
|||
if ( ! empty( $wp->query_vars['order-received'] ) ) {
|
||||
|
||||
$order_id = absint( $wp->query_vars['order-received'] );
|
||||
$order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
|
||||
$order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // WPCS: input var ok, CSRF ok.
|
||||
|
||||
if ( $order_id > 0 ) {
|
||||
$order = wc_get_order( $order_id );
|
||||
|
@ -184,7 +183,7 @@ function wc_clear_cart_after_payment() {
|
|||
$order = wc_get_order( WC()->session->order_awaiting_payment );
|
||||
|
||||
if ( $order && $order->get_id() > 0 ) {
|
||||
// If the order has not failed, or is not pending, the order must have gone through
|
||||
// If the order has not failed, or is not pending, the order must have gone through.
|
||||
if ( ! $order->has_status( array( 'failed', 'pending', 'cancelled' ) ) ) {
|
||||
WC()->cart->empty_cart();
|
||||
}
|
||||
|
@ -195,17 +194,13 @@ add_action( 'get_header', 'wc_clear_cart_after_payment' );
|
|||
|
||||
/**
|
||||
* Get the subtotal.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function wc_cart_totals_subtotal_html() {
|
||||
echo WC()->cart->get_cart_subtotal();
|
||||
echo WC()->cart->get_cart_subtotal(); // WPCS: XSS ok.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get shipping methods.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function wc_cart_totals_shipping_html() {
|
||||
$packages = WC()->shipping->get_packages();
|
||||
|
@ -215,25 +210,26 @@ function wc_cart_totals_shipping_html() {
|
|||
$chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : '';
|
||||
$product_names = array();
|
||||
|
||||
if ( sizeof( $packages ) > 1 ) {
|
||||
if ( count( $packages ) > 1 ) {
|
||||
foreach ( $package['contents'] as $item_id => $values ) {
|
||||
$product_names[ $item_id ] = $values['data']->get_name() . ' ×' . $values['quantity'];
|
||||
}
|
||||
$product_names = apply_filters( 'woocommerce_shipping_package_details_array', $product_names, $package );
|
||||
}
|
||||
|
||||
wc_get_template( 'cart/cart-shipping.php', array(
|
||||
'package' => $package,
|
||||
'available_methods' => $package['rates'],
|
||||
'show_package_details' => sizeof( $packages ) > 1,
|
||||
'show_shipping_calculator' => is_cart() && $first,
|
||||
'package_details' => implode( ', ', $product_names ),
|
||||
// @codingStandardsIgnoreStart
|
||||
'package_name' => apply_filters( 'woocommerce_shipping_package_name', sprintf( _nx( 'Shipping', 'Shipping %d', ( $i + 1 ), 'shipping packages', 'woocommerce' ), ( $i + 1 ) ), $i, $package ),
|
||||
// @codingStandardsIgnoreEnd
|
||||
'index' => $i,
|
||||
'chosen_method' => $chosen_method,
|
||||
) );
|
||||
wc_get_template(
|
||||
'cart/cart-shipping.php', array(
|
||||
'package' => $package,
|
||||
'available_methods' => $package['rates'],
|
||||
'show_package_details' => count( $packages ) > 1,
|
||||
'show_shipping_calculator' => is_cart() && $first,
|
||||
'package_details' => implode( ', ', $product_names ),
|
||||
/* translators: %d: shipping package number */
|
||||
'package_name' => apply_filters( 'woocommerce_shipping_package_name', ( ( $i + 1 ) > 1 ) ? sprintf( _x( 'Shipping %d', 'shipping packages', 'woocommerce' ), ( $i + 1 ) ) : _x( 'Shipping', 'shipping package', 'woocommerce' ), $i, $package ),
|
||||
'index' => $i,
|
||||
'chosen_method' => $chosen_method,
|
||||
)
|
||||
);
|
||||
|
||||
$first = false;
|
||||
}
|
||||
|
@ -241,20 +237,16 @@ function wc_cart_totals_shipping_html() {
|
|||
|
||||
/**
|
||||
* Get taxes total.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function wc_cart_totals_taxes_total_html() {
|
||||
echo apply_filters( 'woocommerce_cart_totals_taxes_total_html', wc_price( WC()->cart->get_taxes_total() ) );
|
||||
echo apply_filters( 'woocommerce_cart_totals_taxes_total_html', wc_price( WC()->cart->get_taxes_total() ) ); // WPCS: XSS ok.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a coupon label.
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param string $coupon
|
||||
* @param bool $echo or return
|
||||
* @param string|WC_Coupon $coupon Coupon data or code.
|
||||
* @param bool $echo Echo or return.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
@ -263,10 +255,11 @@ function wc_cart_totals_coupon_label( $coupon, $echo = true ) {
|
|||
$coupon = new WC_Coupon( $coupon );
|
||||
}
|
||||
|
||||
/* translators: %s: coupon code */
|
||||
$label = apply_filters( 'woocommerce_cart_totals_coupon_label', sprintf( esc_html__( 'Coupon: %s', 'woocommerce' ), $coupon->get_code() ), $coupon );
|
||||
|
||||
if ( $echo ) {
|
||||
echo $label;
|
||||
echo $label; // WPCS: XSS ok.
|
||||
} else {
|
||||
return $label;
|
||||
}
|
||||
|
@ -275,7 +268,7 @@ function wc_cart_totals_coupon_label( $coupon, $echo = true ) {
|
|||
/**
|
||||
* Get coupon display HTML.
|
||||
*
|
||||
* @param string $coupon
|
||||
* @param string|WC_Coupon $coupon Coupon data or code.
|
||||
*/
|
||||
function wc_cart_totals_coupon_html( $coupon ) {
|
||||
if ( is_string( $coupon ) ) {
|
||||
|
@ -284,7 +277,7 @@ function wc_cart_totals_coupon_html( $coupon ) {
|
|||
|
||||
$discount_amount_html = '';
|
||||
|
||||
$amount = WC()->cart->get_coupon_discount_amount( $coupon->get_code(), WC()->cart->display_cart_ex_tax );
|
||||
$amount = WC()->cart->get_coupon_discount_amount( $coupon->get_code(), WC()->cart->display_cart_ex_tax );
|
||||
$discount_amount_html = '-' . wc_price( $amount );
|
||||
|
||||
if ( $coupon->get_free_shipping() ) {
|
||||
|
@ -292,15 +285,13 @@ function wc_cart_totals_coupon_html( $coupon ) {
|
|||
}
|
||||
|
||||
$discount_amount_html = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_amount_html, $coupon );
|
||||
$coupon_html = $discount_amount_html . ' <a href="' . esc_url( add_query_arg( 'remove_coupon', urlencode( $coupon->get_code() ), defined( 'WOOCOMMERCE_CHECKOUT' ) ? wc_get_checkout_url() : wc_get_cart_url() ) ) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr( $coupon->get_code() ) . '">' . __( '[Remove]', 'woocommerce' ) . '</a>';
|
||||
$coupon_html = $discount_amount_html . ' <a href="' . esc_url( add_query_arg( 'remove_coupon', rawurlencode( $coupon->get_code() ), defined( 'WOOCOMMERCE_CHECKOUT' ) ? wc_get_checkout_url() : wc_get_cart_url() ) ) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr( $coupon->get_code() ) . '">' . __( '[Remove]', 'woocommerce' ) . '</a>';
|
||||
|
||||
echo wp_kses( apply_filters( 'woocommerce_cart_totals_coupon_html', $coupon_html, $coupon, $discount_amount_html ), array_replace_recursive( wp_kses_allowed_html( 'post' ), array( 'a' => array( 'data-coupon' => true ) ) ) );
|
||||
echo wp_kses( apply_filters( 'woocommerce_cart_totals_coupon_html', $coupon_html, $coupon, $discount_amount_html ), array_replace_recursive( wp_kses_allowed_html( 'post' ), array( 'a' => array( 'data-coupon' => true ) ) ) ); // phpcs:ignore PHPCompatibility.PHP.NewFunctions.array_replace_recursiveFound
|
||||
}
|
||||
|
||||
/**
|
||||
* Get order total html including inc tax if needed.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function wc_cart_totals_order_total_html() {
|
||||
$value = '<strong>' . WC()->cart->get_total() . '</strong> ';
|
||||
|
@ -310,7 +301,7 @@ function wc_cart_totals_order_total_html() {
|
|||
$tax_string_array = array();
|
||||
$cart_tax_totals = WC()->cart->get_tax_totals();
|
||||
|
||||
if ( get_option( 'woocommerce_tax_total_display' ) == 'itemized' ) {
|
||||
if ( get_option( 'woocommerce_tax_total_display' ) === 'itemized' ) {
|
||||
foreach ( $cart_tax_totals as $code => $tax ) {
|
||||
$tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
|
||||
}
|
||||
|
@ -320,30 +311,31 @@ function wc_cart_totals_order_total_html() {
|
|||
|
||||
if ( ! empty( $tax_string_array ) ) {
|
||||
$taxable_address = WC()->customer->get_taxable_address();
|
||||
$estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping()
|
||||
? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
|
||||
: '';
|
||||
/* translators: %s: country name */
|
||||
$estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping() ? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] ) : '';
|
||||
/* translators: %s: tax information */
|
||||
$value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';
|
||||
}
|
||||
}
|
||||
|
||||
echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value );
|
||||
echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value ); // WPCS: XSS ok.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the fee value.
|
||||
*
|
||||
* @param object $fee
|
||||
* @param object $fee Fee data.
|
||||
*/
|
||||
function wc_cart_totals_fee_html( $fee ) {
|
||||
$cart_totals_fee_html = WC()->cart->display_prices_including_tax() ? wc_price( $fee->total + $fee->tax ) : wc_price( $fee->total );
|
||||
|
||||
echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee );
|
||||
echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee ); // WPCS: XSS ok.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a shipping methods full label including price.
|
||||
* @param WC_Shipping_Rate $method
|
||||
*
|
||||
* @param WC_Shipping_Rate $method Shipping method rate data.
|
||||
* @return string
|
||||
*/
|
||||
function wc_cart_totals_shipping_method_label( $method ) {
|
||||
|
@ -375,7 +367,7 @@ function wc_cart_totals_shipping_method_label( $method ) {
|
|||
*/
|
||||
function wc_cart_round_discount( $value, $precision ) {
|
||||
if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
|
||||
return round( $value, $precision, WC_DISCOUNT_ROUNDING_MODE );
|
||||
return round( $value, $precision, WC_DISCOUNT_ROUNDING_MODE ); // phpcs:ignore PHPCompatibility.PHP.NewFunctionParameters.round_modeFound
|
||||
} elseif ( 2 === WC_DISCOUNT_ROUNDING_MODE ) {
|
||||
return wc_legacy_round_half_down( $value, $precision );
|
||||
} else {
|
||||
|
@ -412,8 +404,8 @@ function wc_get_chosen_shipping_method_for_package( $key, $package ) {
|
|||
$chosen_method = isset( $chosen_methods[ $key ] ) ? $chosen_methods[ $key ] : false;
|
||||
$changed = wc_shipping_methods_have_changed( $key, $package );
|
||||
|
||||
// This is deprecated but here for BW compat. TODO: Remove in 4.0.0
|
||||
$method_counts = WC()->session->get( 'shipping_method_counts' );
|
||||
// This is deprecated but here for BW compat. TODO: Remove in 4.0.0.
|
||||
$method_counts = WC()->session->get( 'shipping_method_counts' );
|
||||
|
||||
if ( ! empty( $method_counts[ $key ] ) ) {
|
||||
$method_count = absint( $method_counts[ $key ] );
|
||||
|
@ -422,10 +414,10 @@ function wc_get_chosen_shipping_method_for_package( $key, $package ) {
|
|||
}
|
||||
|
||||
// If not set, not available, or available methods have changed, set to the DEFAULT option.
|
||||
if ( ! $chosen_method || $changed || ! isset( $package['rates'][ $chosen_method ] ) || sizeof( $package['rates'] ) !== $method_count ) {
|
||||
if ( ! $chosen_method || $changed || ! isset( $package['rates'][ $chosen_method ] ) || count( $package['rates'] ) !== $method_count ) {
|
||||
$chosen_method = wc_get_default_shipping_method_for_package( $key, $package, $chosen_method );
|
||||
$chosen_methods[ $key ] = $chosen_method;
|
||||
$method_counts[ $key ] = sizeof( $package['rates'] );
|
||||
$method_counts[ $key ] = count( $package['rates'] );
|
||||
|
||||
WC()->session->set( 'chosen_shipping_methods', $chosen_methods );
|
||||
WC()->session->set( 'shipping_method_counts', $method_counts );
|
||||
|
@ -491,8 +483,14 @@ function wc_shipping_methods_have_changed( $key, $package ) {
|
|||
* @return string
|
||||
*/
|
||||
function wc_get_cart_item_data_hash( $product ) {
|
||||
return md5( wp_json_encode( apply_filters( 'woocommerce_cart_item_data_to_validate', array(
|
||||
'type' => $product->get_type(),
|
||||
'attributes' => 'variation' === $product->get_type() ? $product->get_variation_attributes() : '',
|
||||
) ) ) );
|
||||
return md5(
|
||||
wp_json_encode(
|
||||
apply_filters(
|
||||
'woocommerce_cart_item_data_to_validate', array(
|
||||
'type' => $product->get_type(),
|
||||
'attributes' => 'variation' === $product->get_type() ? $product->get_variation_attributes() : '',
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue