From 1e7a3ae6d3dd4f01ddbce32417ec33501ac090e8 Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:14:32 +0545 Subject: [PATCH 01/11] indendation Improvement for WC_Cart --- includes/class-wc-cart.php | 43 ++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 912cda9d9d8..333951367b1 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -120,32 +120,35 @@ class WC_Cart { add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 ); // Set cookies before shutdown and ob flushing } - /** + /** * Loads the cart data from the PHP session during WordPress init and hooks in other methods. - * - * @access public - * @return void - */ - public function init() { + * + * @access public + * @return void + */ + public function init() { $this->get_cart_from_session(); - + add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_items' ), 1 ); add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_coupons' ), 1 ); add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1 ); - } + } - /** - * Will set cart cookies if needed, once, during WP hook - */ - public function maybe_set_cart_cookies() { - if ( ! headers_sent() ) { - if ( sizeof( $this->cart_contents ) > 0 ) { - $this->set_cart_cookies( true ); - } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { - $this->set_cart_cookies( false ); - } - } - } + /** + * Will set cart cookies if needed, once, during WP hook + * + * @access public + * @return void + */ + public function maybe_set_cart_cookies() { + if ( ! headers_sent() ) { + if ( sizeof( $this->cart_contents ) > 0 ) { + $this->set_cart_cookies( true ); + } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { + $this->set_cart_cookies( false ); + } + } + } /** * Set cart hash cookie and items in cart. From a2ab230efb81a6f3aede797df61c0f7699b06118 Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:17:14 +0545 Subject: [PATCH 02/11] Use of time constant --- includes/class-wc-cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 333951367b1..6f6a3215d1e 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -162,7 +162,7 @@ class WC_Cart { wc_setcookie( 'woocommerce_items_in_cart', 1 ); wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $this->get_cart() ) ) ); } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { - wc_setcookie( 'woocommerce_items_in_cart', 0, time() - 3600 ); + wc_setcookie( 'woocommerce_items_in_cart', 0, time() - HOUR_IN_SECONDS ); wc_setcookie( 'woocommerce_cart_hash', '', time() - 3600 ); } do_action( 'woocommerce_set_cart_cookies', $set ); From cf421b1c1257f0899869d2707dfaa5daee2cc5f0 Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:18:46 +0545 Subject: [PATCH 03/11] OOPS! again time constant --- includes/class-wc-cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 6f6a3215d1e..1f0577dd705 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -163,7 +163,7 @@ class WC_Cart { wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $this->get_cart() ) ) ); } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { wc_setcookie( 'woocommerce_items_in_cart', 0, time() - HOUR_IN_SECONDS ); - wc_setcookie( 'woocommerce_cart_hash', '', time() - 3600 ); + wc_setcookie( 'woocommerce_cart_hash', '', time() - HOUR_IN_SECONDS ); } do_action( 'woocommerce_set_cart_cookies', $set ); } From ccf15ff06be351531f4a650b3f7b683183163204 Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:22:48 +0545 Subject: [PATCH 04/11] Better docblock ;) --- includes/class-wc-cart.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 1f0577dd705..9357a3fbf08 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -236,6 +236,9 @@ class WC_Cart { /** * Sets the php session data for the cart and coupons. + * + * @access public + * @return void */ public function set_session() { // Set cart and coupon session data From f829e53a96ff966ff6dae5fbd6f56aedd31fdaaa Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:24:50 +0545 Subject: [PATCH 05/11] New line for docblock --- includes/class-wc-cart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 9357a3fbf08..1c83417d25c 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -398,6 +398,7 @@ class WC_Cart { /** * Looks through cart items and checks the posts are not trashed or deleted. + * * @return bool|WP_Error */ public function check_cart_item_validity() { From a4f6b0f9f962a981f8c32661084e924575d3d01d Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:29:56 +0545 Subject: [PATCH 06/11] Converted Indentations to tabs --- includes/class-wc-cart.php | 140 ++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 1c83417d25c..1302c1bcad2 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -128,7 +128,7 @@ class WC_Cart { */ public function init() { $this->get_cart_from_session(); - + add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_items' ), 1 ); add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_coupons' ), 1 ); add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1 ); @@ -136,7 +136,7 @@ class WC_Cart { /** * Will set cart cookies if needed, once, during WP hook - * + * * @access public * @return void */ @@ -168,7 +168,7 @@ class WC_Cart { do_action( 'woocommerce_set_cart_cookies', $set ); } - /*-----------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ /* Cart Session Handling */ /*-----------------------------------------------------------------------------------*/ @@ -279,7 +279,7 @@ class WC_Cart { do_action( 'woocommerce_cart_emptied' ); } - /*-----------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ /* Persistent cart handling */ /*-----------------------------------------------------------------------------------*/ @@ -305,7 +305,7 @@ class WC_Cart { delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart' ); } - /*-----------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ /* Cart Data Functions */ /*-----------------------------------------------------------------------------------*/ @@ -398,7 +398,7 @@ class WC_Cart { /** * Looks through cart items and checks the posts are not trashed or deleted. - * + * * @return bool|WP_Error */ public function check_cart_item_validity() { @@ -522,15 +522,15 @@ class WC_Cart { $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) ); // If this is a term slug, get the term's nice name - if ( taxonomy_exists( $taxonomy ) ) { - $term = get_term_by( 'slug', $value, $taxonomy ); - if ( ! is_wp_error( $term ) && $term && $term->name ) { - $value = $term->name; - } - $label = wc_attribute_label( $taxonomy ); + if ( taxonomy_exists( $taxonomy ) ) { + $term = get_term_by( 'slug', $value, $taxonomy ); + if ( ! is_wp_error( $term ) && $term && $term->name ) { + $value = $term->name; + } + $label = wc_attribute_label( $taxonomy ); - // If this is a custom option slug, get the options name - } else { + // If this is a custom option slug, get the options name + } else { $value = apply_filters( 'woocommerce_variation_option_name', $value ); $product_attributes = $cart_item['data']->get_attributes(); if ( isset( $product_attributes[ str_replace( 'attribute_', '', $name ) ] ) ) { @@ -707,7 +707,7 @@ class WC_Cart { $tax_totals[ $code ]->amount = 0; } - $tax_totals[ $code ]->tax_rate_id = $key; + $tax_totals[ $code ]->tax_rate_id = $key; $tax_totals[ $code ]->is_compound = $this->tax->is_compound( $key ); $tax_totals[ $code ]->label = $this->tax->get_rate_label( $key ); $tax_totals[ $code ]->amount += wc_round_tax_total( $tax ); @@ -723,60 +723,60 @@ class WC_Cart { /*-----------------------------------------------------------------------------------*/ /** - * Check if product is in the cart and return cart item key. - * - * Cart item key will be unique based on the item and its properties, such as variations. - * - * @param mixed id of product to find in the cart - * @return string cart item key - */ - public function find_product_in_cart( $cart_id = false ) { - if ( $cart_id !== false ) { - if ( is_array( $this->cart_contents ) ) { - foreach ( $this->cart_contents as $cart_item_key => $cart_item ) { - if ( $cart_item_key == $cart_id ) { - return $cart_item_key; - } - } - } - } + * Check if product is in the cart and return cart item key. + * + * Cart item key will be unique based on the item and its properties, such as variations. + * + * @param mixed id of product to find in the cart + * @return string cart item key + */ + public function find_product_in_cart( $cart_id = false ) { + if ( $cart_id !== false ) { + if ( is_array( $this->cart_contents ) ) { + foreach ( $this->cart_contents as $cart_item_key => $cart_item ) { + if ( $cart_item_key == $cart_id ) { + return $cart_item_key; + } + } + } + } return ''; } /** - * Generate a unique ID for the cart item being added. - * - * @param int $product_id - id of the product the key is being generated for - * @param int $variation_id of the product the key is being generated for - * @param array $variation data for the cart item - * @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart - * @return string cart item key - */ - public function generate_cart_id( $product_id, $variation_id = 0, $variation = array(), $cart_item_data = array() ) { - $id_parts = array( $product_id ); + * Generate a unique ID for the cart item being added. + * + * @param int $product_id - id of the product the key is being generated for + * @param int $variation_id of the product the key is being generated for + * @param array $variation data for the cart item + * @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart + * @return string cart item key + */ + public function generate_cart_id( $product_id, $variation_id = 0, $variation = array(), $cart_item_data = array() ) { + $id_parts = array( $product_id ); - if ( $variation_id && 0 != $variation_id ) - $id_parts[] = $variation_id; + if ( $variation_id && 0 != $variation_id ) + $id_parts[] = $variation_id; - if ( is_array( $variation ) && ! empty( $variation ) ) { - $variation_key = ''; - foreach ( $variation as $key => $value ) { - $variation_key .= trim( $key ) . trim( $value ); - } - $id_parts[] = $variation_key; - } + if ( is_array( $variation ) && ! empty( $variation ) ) { + $variation_key = ''; + foreach ( $variation as $key => $value ) { + $variation_key .= trim( $key ) . trim( $value ); + } + $id_parts[] = $variation_key; + } - if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) { - $cart_item_data_key = ''; - foreach ( $cart_item_data as $key => $value ) { - if ( is_array( $value ) ) $value = http_build_query( $value ); - $cart_item_data_key .= trim($key) . trim($value); - } - $id_parts[] = $cart_item_data_key; - } + if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) { + $cart_item_data_key = ''; + foreach ( $cart_item_data as $key => $value ) { + if ( is_array( $value ) ) $value = http_build_query( $value ); + $cart_item_data_key .= trim($key) . trim($value); + } + $id_parts[] = $cart_item_data_key; + } - return md5( implode( '_', $id_parts ) ); - } + return md5( implode( '_', $id_parts ) ); + } /** * Add a product to the cart. @@ -901,8 +901,8 @@ class WC_Cart { } if ( did_action( 'wp' ) ) { - $this->set_cart_cookies( sizeof( $this->cart_contents ) > 0 ); - } + $this->set_cart_cookies( sizeof( $this->cart_contents ) > 0 ); + } do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ); @@ -932,7 +932,7 @@ class WC_Cart { } } - /*-----------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ /* Cart Calculation Functions */ /*-----------------------------------------------------------------------------------*/ @@ -1152,7 +1152,7 @@ class WC_Cart { // Tax rows - merge the totals we just got foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) { - $this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); + $this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); } /** @@ -1178,7 +1178,7 @@ class WC_Cart { // Tax rows - merge the totals we just got foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) { - $this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); + $this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); } } @@ -1280,7 +1280,7 @@ class WC_Cart { return apply_filters( 'woocommerce_cart_needs_payment', $this->total > 0, $this ); } - /*-----------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ /* Shipping related functions */ /*-----------------------------------------------------------------------------------*/ @@ -1449,7 +1449,7 @@ class WC_Cart { return ''; } - /*-----------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ /* Coupons/Discount related functions */ /*-----------------------------------------------------------------------------------*/ @@ -1837,7 +1837,7 @@ class WC_Cart { $this->coupon_applied_count[ $code ] += $count; } - /*-----------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ /* Fees API to add additional costs to orders */ /*-----------------------------------------------------------------------------------*/ @@ -1919,7 +1919,7 @@ class WC_Cart { } } - /*-----------------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------------*/ /* Get Formatted Totals */ /*-----------------------------------------------------------------------------------*/ From 8e0989206445b45ee45204f42e7cae4cbc14e9cd Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:33:13 +0545 Subject: [PATCH 07/11] better at get_option() @ L91 --- includes/class-wc-cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 1302c1bcad2..e69fd0e64d2 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -88,7 +88,7 @@ class WC_Cart { public function __construct() { $this->tax = new WC_Tax(); $this->prices_include_tax = get_option( 'woocommerce_prices_include_tax' ) == 'yes'; - $this->round_at_subtotal = get_option('woocommerce_tax_round_at_subtotal') == 'yes'; + $this->round_at_subtotal = get_option( 'woocommerce_tax_round_at_subtotal' ) == 'yes'; $this->tax_display_cart = get_option( 'woocommerce_tax_display_cart' ); $this->dp = absint( get_option( 'woocommerce_price_num_decimals' ) ); $this->display_totals_ex_tax = $this->tax_display_cart == 'excl'; From 40ec40a4111e692b250446272808fc2dabe5718c Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:35:22 +0545 Subject: [PATCH 08/11] Found and fix docblock comment --- includes/class-wc-cart.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index e69fd0e64d2..6cd232c3fd2 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -135,11 +135,11 @@ class WC_Cart { } /** - * Will set cart cookies if needed, once, during WP hook - * - * @access public - * @return void - */ + * Will set cart cookies if needed, once, during WP hook + * + * @access public + * @return void + */ public function maybe_set_cart_cookies() { if ( ! headers_sent() ) { if ( sizeof( $this->cart_contents ) > 0 ) { From 40fe9ffd2f38eddce5011baf6fe8921118bbef91 Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:37:51 +0545 Subject: [PATCH 09/11] check_cart_item_validity() should have have public --- includes/class-wc-cart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 6cd232c3fd2..fb6e242c4d5 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -399,6 +399,7 @@ class WC_Cart { /** * Looks through cart items and checks the posts are not trashed or deleted. * + * @access public * @return bool|WP_Error */ public function check_cart_item_validity() { From 6e58c45196e6365953faa1ae377531243abb1846 Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 01:40:24 +0545 Subject: [PATCH 10/11] @access public in docblock --- includes/class-wc-cart.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index fb6e242c4d5..b62567df211 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -588,6 +588,7 @@ class WC_Cart { /** * Gets cross sells based on the items in the cart. * + * @access public * @return array cross_sells (item ids) */ public function get_cross_sells() { @@ -608,6 +609,7 @@ class WC_Cart { /** * Gets the url to the cart page. * + * @access public * @return string url to page */ public function get_cart_url() { @@ -618,6 +620,7 @@ class WC_Cart { /** * Gets the url to the checkout page. * + * @access public * @return string url to page */ public function get_checkout_url() { @@ -636,6 +639,7 @@ class WC_Cart { /** * Gets the url to remove an item from the cart. * + * @access public * @param string cart_item_key contains the id of the cart item * @return string url to page */ @@ -647,6 +651,7 @@ class WC_Cart { /** * Returns the contents of the cart in an array. * + * @access public * @return array contents of the cart */ public function get_cart() { @@ -656,6 +661,7 @@ class WC_Cart { /** * Returns the contents of the cart in an array without the 'data' element. * + * @access public * @return array contents of the cart */ private function get_cart_for_session() { @@ -675,6 +681,7 @@ class WC_Cart { /** * Returns the cart and shipping taxes, merged. * + * @access public * @return array merged taxes */ public function get_taxes() { From 0b58d335b8932003b09ae8fa3242d1b7a3847ed5 Mon Sep 17 00:00:00 2001 From: Shiva Poudel Date: Mon, 15 Sep 2014 08:17:19 +0545 Subject: [PATCH 11/11] Reverted access public Thanks! @tamarazuk --- includes/class-wc-cart.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index b62567df211..ff5734683da 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -588,7 +588,6 @@ class WC_Cart { /** * Gets cross sells based on the items in the cart. * - * @access public * @return array cross_sells (item ids) */ public function get_cross_sells() { @@ -609,7 +608,6 @@ class WC_Cart { /** * Gets the url to the cart page. * - * @access public * @return string url to page */ public function get_cart_url() { @@ -620,7 +618,6 @@ class WC_Cart { /** * Gets the url to the checkout page. * - * @access public * @return string url to page */ public function get_checkout_url() { @@ -639,7 +636,6 @@ class WC_Cart { /** * Gets the url to remove an item from the cart. * - * @access public * @param string cart_item_key contains the id of the cart item * @return string url to page */ @@ -651,7 +647,6 @@ class WC_Cart { /** * Returns the contents of the cart in an array. * - * @access public * @return array contents of the cart */ public function get_cart() { @@ -661,7 +656,6 @@ class WC_Cart { /** * Returns the contents of the cart in an array without the 'data' element. * - * @access public * @return array contents of the cart */ private function get_cart_for_session() { @@ -681,7 +675,6 @@ class WC_Cart { /** * Returns the cart and shipping taxes, merged. * - * @access public * @return array merged taxes */ public function get_taxes() { @@ -959,8 +952,6 @@ class WC_Cart { /** * Calculate totals for the items in the cart. - * - * @access public */ public function calculate_totals() { @@ -1260,7 +1251,6 @@ class WC_Cart { /** * remove_taxes function. * - * @access public * @return void */ public function remove_taxes() {