PHPCS fixes in includes/class-wc-shipping.php

This commit is contained in:
Rodrigo Primo 2018-03-14 17:42:52 -03:00
parent 202e527a83
commit e90ce56a50
1 changed files with 59 additions and 31 deletions

View File

@ -4,11 +4,9 @@
* *
* Handles shipping and loads shipping methods via hooks. * Handles shipping and loads shipping methods via hooks.
* *
* @class WC_Shipping * @class WC_Shipping
* @version 2.6.0 * @version 2.6.0
* @package WooCommerce/Classes/Shipping * @package WooCommerce/Classes/Shipping
* @category Class
* @author WooThemes
*/ */
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
@ -20,20 +18,38 @@ if ( ! defined( 'ABSPATH' ) ) {
*/ */
class WC_Shipping { class WC_Shipping {
/** @var bool True if shipping is enabled. */ /**
public $enabled = false; * True if shipping is enabled.
*
/** @var array|null Stores methods loaded into woocommerce. */ * @var bool
public $shipping_methods = null; */
public $enabled = false;
/** @var array Stores the shipping classes. */
public $shipping_classes = array();
/** @var array Stores packages to ship and to get quotes for. */
public $packages = array();
/** /**
* @var WC_Shipping The single instance of the class * Stores methods loaded into woocommerce.
*
* @var array|null
*/
public $shipping_methods = null;
/**
* Stores the shipping classes.
*
* @var array
*/
public $shipping_classes = array();
/**
* Stores packages to ship and to get quotes for.
*
* @var array
*/
public $packages = array();
/**
* The single instance of the class
*
* @var WC_Shipping
* @since 2.1 * @since 2.1
*/ */
protected static $_instance = null; protected static $_instance = null;
@ -80,10 +96,10 @@ class WC_Shipping {
*/ */
public function __get( $name ) { public function __get( $name ) {
// Grab from cart for backwards compatibility with versions prior to 3.2. // Grab from cart for backwards compatibility with versions prior to 3.2.
if ( 'shipping_total' === $name ){ if ( 'shipping_total' === $name ) {
return wc()->cart->get_shipping_total(); return wc()->cart->get_shipping_total();
} }
if ( 'shipping_taxes' === $name ){ if ( 'shipping_taxes' === $name ) {
return wc()->cart->get_shipping_taxes(); return wc()->cart->get_shipping_taxes();
} }
} }
@ -108,17 +124,18 @@ class WC_Shipping {
/** /**
* Shipping methods register themselves by returning their main class name through the woocommerce_shipping_methods filter. * Shipping methods register themselves by returning their main class name through the woocommerce_shipping_methods filter.
*
* @return array * @return array
*/ */
public function get_shipping_method_class_names() { public function get_shipping_method_class_names() {
// Unique Method ID => Method Class name // Unique Method ID => Method Class name.
$shipping_methods = array( $shipping_methods = array(
'flat_rate' => 'WC_Shipping_Flat_Rate', 'flat_rate' => 'WC_Shipping_Flat_Rate',
'free_shipping' => 'WC_Shipping_Free_Shipping', 'free_shipping' => 'WC_Shipping_Free_Shipping',
'local_pickup' => 'WC_Shipping_Local_Pickup', 'local_pickup' => 'WC_Shipping_Local_Pickup',
); );
// For backwards compatibility with 2.5.x we load any ENABLED legacy shipping methods here // For backwards compatibility with 2.5.x we load any ENABLED legacy shipping methods here.
$maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' ); $maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' );
foreach ( $maybe_load_legacy_methods as $method ) { foreach ( $maybe_load_legacy_methods as $method ) {
@ -135,7 +152,7 @@ class WC_Shipping {
* Loads all shipping methods which are hooked in. * Loads all shipping methods which are hooked in.
* If a $package is passed some methods may add themselves conditionally and zones will be used. * If a $package is passed some methods may add themselves conditionally and zones will be used.
* *
* @param array $package * @param array $package Package information.
* @return array * @return array
*/ */
public function load_shipping_methods( $package = array() ) { public function load_shipping_methods( $package = array() ) {
@ -144,7 +161,7 @@ class WC_Shipping {
$shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package ); $shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );
$this->shipping_methods = $shipping_zone->get_shipping_methods( true ); $this->shipping_methods = $shipping_zone->get_shipping_methods( true );
// Debug output // Debug output.
if ( $debug_mode && ! defined( 'WOOCOMMERCE_CHECKOUT' ) && ! defined( 'WC_DOING_AJAX' ) && ! wc_has_notice( 'Customer matched zone "' . $shipping_zone->get_zone_name() . '"' ) ) { if ( $debug_mode && ! defined( 'WOOCOMMERCE_CHECKOUT' ) && ! defined( 'WC_DOING_AJAX' ) && ! wc_has_notice( 'Customer matched zone "' . $shipping_zone->get_zone_name() . '"' ) ) {
wc_add_notice( 'Customer matched zone "' . $shipping_zone->get_zone_name() . '"' ); wc_add_notice( 'Customer matched zone "' . $shipping_zone->get_zone_name() . '"' );
} }
@ -160,7 +177,7 @@ class WC_Shipping {
// Methods can register themselves manually through this hook if necessary. // Methods can register themselves manually through this hook if necessary.
do_action( 'woocommerce_load_shipping_methods', $package ); do_action( 'woocommerce_load_shipping_methods', $package );
// Return loaded methods // Return loaded methods.
return $this->get_shipping_methods(); return $this->get_shipping_methods();
} }
@ -212,7 +229,12 @@ class WC_Shipping {
*/ */
public function get_shipping_classes() { public function get_shipping_classes() {
if ( empty( $this->shipping_classes ) ) { if ( empty( $this->shipping_classes ) ) {
$classes = get_terms( 'product_shipping_class', array( 'hide_empty' => '0', 'orderby' => 'name' ) ); $classes = get_terms(
'product_shipping_class', array(
'hide_empty' => '0',
'orderby' => 'name',
)
);
$this->shipping_classes = ! is_wp_error( $classes ) ? $classes : array(); $this->shipping_classes = ! is_wp_error( $classes ) ? $classes : array();
} }
return apply_filters( 'woocommerce_get_shipping_classes', $this->shipping_classes ); return apply_filters( 'woocommerce_get_shipping_classes', $this->shipping_classes );
@ -220,7 +242,8 @@ class WC_Shipping {
/** /**
* Get the default method. * Get the default method.
* @param array $available_methods *
* @param array $available_methods
* @param boolean $current_chosen_method * @param boolean $current_chosen_method
* @return string * @return string
*/ */
@ -292,7 +315,7 @@ class WC_Shipping {
} }
$allowed = array_keys( WC()->countries->get_shipping_countries() ); $allowed = array_keys( WC()->countries->get_shipping_countries() );
return in_array( $package['destination']['country'], $allowed ); return in_array( $package['destination']['country'], $allowed, true );
} }
/** /**
@ -338,10 +361,12 @@ class WC_Shipping {
$package['rates'] = apply_filters( 'woocommerce_package_rates', $package['rates'], $package ); $package['rates'] = apply_filters( 'woocommerce_package_rates', $package['rates'], $package );
// Store in session to avoid recalculation. // Store in session to avoid recalculation.
WC()->session->set( $session_key, array( WC()->session->set(
'package_hash' => $package_hash, $session_key, array(
'rates' => $package['rates'], 'package_hash' => $package_hash,
) ); 'rates' => $package['rates'],
)
);
} else { } else {
$package['rates'] = $stored_rates['rates']; $package['rates'] = $stored_rates['rates'];
} }
@ -351,6 +376,7 @@ class WC_Shipping {
/** /**
* Get packages. * Get packages.
*
* @return array * @return array
*/ */
public function get_packages() { public function get_packages() {
@ -368,6 +394,8 @@ class WC_Shipping {
} }
/** /**
* Deprecated
*
* @deprecated 2.6.0 Was previously used to determine sort order of methods, but this is now controlled by zones and thus unused. * @deprecated 2.6.0 Was previously used to determine sort order of methods, but this is now controlled by zones and thus unused.
*/ */
public function sort_shipping_methods() { public function sort_shipping_methods() {