From 19d978ce1c9a811d7e10b66e75c6adc390c8d8fd Mon Sep 17 00:00:00 2001 From: Philipp Bammes <8144115+tyrann0us@users.noreply.github.com> Date: Fri, 11 Dec 2020 08:23:22 +0100 Subject: [PATCH] =?UTF-8?q?Invert=20=E2=80=9Cif=E2=80=9D=20statement=20to?= =?UTF-8?q?=20reduce=20nesting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-wc-shipping.php | 115 +++++++++++++++++---------------- 1 file changed, 59 insertions(+), 56 deletions(-) diff --git a/includes/class-wc-shipping.php b/includes/class-wc-shipping.php index 6dcc53a3ea3..d9157983a44 100644 --- a/includes/class-wc-shipping.php +++ b/includes/class-wc-shipping.php @@ -313,63 +313,66 @@ class WC_Shipping { $package['rates'] = array(); // If the package is not shippable, e.g. trying to ship to an invalid country, do not calculate rates. - if ( $this->is_package_shippable( $package ) ) { - // Check if we need to recalculate shipping for this package. - $package_to_hash = $package; - - // Remove data objects so hashes are consistent. - foreach ( $package_to_hash['contents'] as $item_id => $item ) { - unset( $package_to_hash['contents'][ $item_id ]['data'] ); - } - - // Get rates stored in the WC session data for this package. - $wc_session_key = 'shipping_for_package_' . $package_key; - $stored_rates = WC()->session->get( $wc_session_key ); - - // Calculate the hash for this package so we can tell if it's changed since last calculation. - $package_hash = 'wc_ship_' . md5( wp_json_encode( $package_to_hash ) . WC_Cache_Helper::get_transient_version( 'shipping' ) ); - - if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ) { - foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) { - if ( ! $shipping_method->supports( 'shipping-zones' ) || $shipping_method->get_instance_id() ) { - /** - * Fires before getting shipping rates for a package. - * - * @since 4.3.0 - * @param array $package Package of cart items. - * @param WC_Shipping_Method $shipping_method Shipping method instance. - */ - do_action( 'woocommerce_before_get_rates_for_package', $package, $shipping_method ); - - // Use + instead of array_merge to maintain numeric keys. - $package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package ); - - /** - * Fires after getting shipping rates for a package. - * - * @since 4.3.0 - * @param array $package Package of cart items. - * @param WC_Shipping_Method $shipping_method Shipping method instance. - */ - do_action( 'woocommerce_after_get_rates_for_package', $package, $shipping_method ); - } - } - - // Filter the calculated rates. - $package['rates'] = apply_filters( 'woocommerce_package_rates', $package['rates'], $package ); - - // Store in session to avoid recalculation. - WC()->session->set( - $wc_session_key, - array( - 'package_hash' => $package_hash, - 'rates' => $package['rates'], - ) - ); - } else { - $package['rates'] = $stored_rates['rates']; - } + if ( ! $this->is_package_shippable( $package ) ) { + return $package; } + + // Check if we need to recalculate shipping for this package. + $package_to_hash = $package; + + // Remove data objects so hashes are consistent. + foreach ( $package_to_hash['contents'] as $item_id => $item ) { + unset( $package_to_hash['contents'][ $item_id ]['data'] ); + } + + // Get rates stored in the WC session data for this package. + $wc_session_key = 'shipping_for_package_' . $package_key; + $stored_rates = WC()->session->get( $wc_session_key ); + + // Calculate the hash for this package so we can tell if it's changed since last calculation. + $package_hash = 'wc_ship_' . md5( wp_json_encode( $package_to_hash ) . WC_Cache_Helper::get_transient_version( 'shipping' ) ); + + if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ) { + foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) { + if ( ! $shipping_method->supports( 'shipping-zones' ) || $shipping_method->get_instance_id() ) { + /** + * Fires before getting shipping rates for a package. + * + * @since 4.3.0 + * @param array $package Package of cart items. + * @param WC_Shipping_Method $shipping_method Shipping method instance. + */ + do_action( 'woocommerce_before_get_rates_for_package', $package, $shipping_method ); + + // Use + instead of array_merge to maintain numeric keys. + $package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package ); + + /** + * Fires after getting shipping rates for a package. + * + * @since 4.3.0 + * @param array $package Package of cart items. + * @param WC_Shipping_Method $shipping_method Shipping method instance. + */ + do_action( 'woocommerce_after_get_rates_for_package', $package, $shipping_method ); + } + } + + // Filter the calculated rates. + $package['rates'] = apply_filters( 'woocommerce_package_rates', $package['rates'], $package ); + + // Store in session to avoid recalculation. + WC()->session->set( + $wc_session_key, + array( + 'package_hash' => $package_hash, + 'rates' => $package['rates'], + ) + ); + } else { + $package['rates'] = $stored_rates['rates']; + } + return $package; }