From 8f3abb27cc7b309ec16671591531b616086d401b Mon Sep 17 00:00:00 2001 From: Sakri Koskimies Date: Wed, 2 Feb 2022 10:55:33 +0200 Subject: [PATCH 1/9] Add filters for agent search address --- .../frontend/class-ss-shipping-frontend.php | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php index bc3fe6b..0fc3f67 100644 --- a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php +++ b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php @@ -71,11 +71,27 @@ public function display_ss_pickup_points($method, $index) ($chosen_shipping == $shipping_id) && (stripos($meta_data['smart_send_shipping_method'], 'agent') !== false)) { - if (!empty($_POST['s_country']) && !empty($_POST['s_postcode']) && !empty($_POST['s_address'])) { - $country = wc_clean($_POST['s_country']); - $postal_code = wc_clean($_POST['s_postcode']); - $city = (!empty($_POST['s_city']) ? wc_clean($_POST['s_city']) : null);//not required but preferred - $street = wc_clean($_POST['s_address']); + $country = apply_filters( + 'smart_send_agent_search_country', + wc_clean(isset($_POST['s_country']) ? $_POST['s_country'] : '') + ); + + $postal_code = apply_filters( + 'smart_send_agent_search_postal_code', + wc_clean(isset($_POST['s_postcode']) ? $_POST['s_postcode'] : '') + ); + + if (!empty($country) && !empty($postal_code)) { + $city = isset($_POST['s_city']) ? $_POST['s_city'] : ''; // Not required but preferred + $city = apply_filters( + 'smart_send_agent_search_city', + !empty($city) ? wc_clean($city) : null + ); + + $street = apply_filters( + 'smart_send_agent_search_street', + wc_clean(isset($_POST['s_address']) ? $_POST['s_address'] : '') + ); $carrier = SS_SHIPPING_WC()->get_shipping_method_carrier($meta_data['smart_send_shipping_method']); From 16aa9cc55fa0418d1b423b51cdf4ad7c4efbeb9f Mon Sep 17 00:00:00 2001 From: Sakri Koskimies Date: Wed, 2 Feb 2022 11:15:16 +0200 Subject: [PATCH 2/9] Allow for searching agents without a street address --- .../frontend/class-ss-shipping-frontend.php | 36 ++++++++++++++++++- .../includes/lib/Smartsend/Api.php | 10 +++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php index 0fc3f67..b1ce6eb 100644 --- a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php +++ b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php @@ -95,7 +95,12 @@ public function display_ss_pickup_points($method, $index) $carrier = SS_SHIPPING_WC()->get_shipping_method_carrier($meta_data['smart_send_shipping_method']); - $ss_agents = $this->find_closest_agents_by_address($carrier, $country, $postal_code, $city, $street); + // Production API does not allow street addresses shorter than 5 characters + if(empty($street) || strlen($street) < 5){ + $ss_agents = $this->find_closest_agents_by_postal_code($carrier, $country, $postal_code); + }else{ + $ss_agents = $this->find_closest_agents_by_address($carrier, $country, $postal_code, $city, $street); + } if (!empty($ss_agents)) { @@ -161,6 +166,35 @@ public function find_closest_agents_by_address($carrier, $country, $postal_code, } } + /** + * Find the closest agents by postal code + * + * @param $carrier string Unique carrier code + * @param $country string ISO3166-A2 Country code + * @param $postal_code string + * + * @return array + */ + public function find_closest_agents_by_postal_code($carrier, $country, $postal_code) + { + SS_SHIPPING_WC()->log_msg('Called "findClosestAgentByPostalCode" for website ' . SS_SHIPPING_WC()->get_website_url() . ' with carrier = "' . $carrier . '", country = "' . $country . '", postcode = "' . $postal_code . '"'); + + if (SS_SHIPPING_WC()->get_api_handle()->findClosestAgentByPostalCode($carrier, $country, $postal_code)) { + + $ss_agents = SS_SHIPPING_WC()->get_api_handle()->getData(); + + SS_SHIPPING_WC()->log_msg('Response from "findClosestAgentByPostalCode": ' . SS_SHIPPING_WC()->get_api_handle()->getResponseBody()); + // Save all of the agents in sessions + WC()->session->set('ss_shipping_agents', $ss_agents); + + return $ss_agents; + } else { + SS_SHIPPING_WC()->log_msg( 'Response from "findClosestAgentByPostalCode": ' . SS_SHIPPING_WC()->get_api_handle()->getErrorString() ); + + return array(); + } + } + /** * Get the formatted address to display on the frontend */ diff --git a/smart-send-logistics/includes/lib/Smartsend/Api.php b/smart-send-logistics/includes/lib/Smartsend/Api.php index b4f9fc9..4957118 100644 --- a/smart-send-logistics/includes/lib/Smartsend/Api.php +++ b/smart-send-logistics/includes/lib/Smartsend/Api.php @@ -164,10 +164,12 @@ public function getAgentsInArea($carrier, $country=null, $min_latitude, $max_lat */ public function findClosestAgentByPostalCode($carrier, $country, $postal_code) { - return $this->httpGet( - $method = 'agents/closest/carrier/'.$carrier.'/country/'.$country.'/postalcode/'.$postal_code, - $args = array(), $headers = array(), $body = null, $timeout = $this->getAgentTimeout() - ); + $method = 'agents/closest/carrier/'.$carrier.'/country/'.$country.'/postalcode/'.$postal_code; + + return $this->httpGet( + $method, + $args = array(), $headers = array(), $body = null, $timeout = $this->getAgentTimeout() + ); } /* From c0fe45bb39a0cc91b78a70d6c98f406a5ab48099 Mon Sep 17 00:00:00 2001 From: Sakri Koskimies Date: Wed, 2 Feb 2022 13:05:22 +0200 Subject: [PATCH 3/9] Apply original formatting --- .../frontend/class-ss-shipping-frontend.php | 54 +++++++++---------- .../includes/lib/Smartsend/Api.php | 8 +-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php index b1ce6eb..cf87fbe 100644 --- a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php +++ b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php @@ -71,36 +71,36 @@ public function display_ss_pickup_points($method, $index) ($chosen_shipping == $shipping_id) && (stripos($meta_data['smart_send_shipping_method'], 'agent') !== false)) { - $country = apply_filters( - 'smart_send_agent_search_country', - wc_clean(isset($_POST['s_country']) ? $_POST['s_country'] : '') - ); - - $postal_code = apply_filters( - 'smart_send_agent_search_postal_code', - wc_clean(isset($_POST['s_postcode']) ? $_POST['s_postcode'] : '') - ); - - if (!empty($country) && !empty($postal_code)) { - $city = isset($_POST['s_city']) ? $_POST['s_city'] : ''; // Not required but preferred - $city = apply_filters( - 'smart_send_agent_search_city', - !empty($city) ? wc_clean($city) : null - ); - - $street = apply_filters( - 'smart_send_agent_search_street', - wc_clean(isset($_POST['s_address']) ? $_POST['s_address'] : '') - ); + $country = apply_filters( + 'smart_send_agent_search_country', + wc_clean(isset($_POST['s_country']) ? $_POST['s_country'] : '') + ); + + $postal_code = apply_filters( + 'smart_send_agent_search_postal_code', + wc_clean(isset($_POST['s_postcode']) ? $_POST['s_postcode'] : '') + ); + + if (!empty($country) && !empty($postal_code)) { + $city = isset($_POST['s_city']) ? $_POST['s_city'] : ''; // Not required but preferred + $city = apply_filters( + 'smart_send_agent_search_city', + !empty($city) ? wc_clean($city) : null + ); + + $street = apply_filters( + 'smart_send_agent_search_street', + wc_clean(isset($_POST['s_address']) ? $_POST['s_address'] : '') + ); $carrier = SS_SHIPPING_WC()->get_shipping_method_carrier($meta_data['smart_send_shipping_method']); - // Production API does not allow street addresses shorter than 5 characters - if(empty($street) || strlen($street) < 5){ - $ss_agents = $this->find_closest_agents_by_postal_code($carrier, $country, $postal_code); - }else{ - $ss_agents = $this->find_closest_agents_by_address($carrier, $country, $postal_code, $city, $street); - } + // Production API does not allow street addresses shorter than 5 characters + if(empty($street) || strlen($street) < 5){ + $ss_agents = $this->find_closest_agents_by_postal_code($carrier, $country, $postal_code); + }else{ + $ss_agents = $this->find_closest_agents_by_address($carrier, $country, $postal_code, $city, $street); + } if (!empty($ss_agents)) { diff --git a/smart-send-logistics/includes/lib/Smartsend/Api.php b/smart-send-logistics/includes/lib/Smartsend/Api.php index 4957118..a2b50d6 100644 --- a/smart-send-logistics/includes/lib/Smartsend/Api.php +++ b/smart-send-logistics/includes/lib/Smartsend/Api.php @@ -164,11 +164,11 @@ public function getAgentsInArea($carrier, $country=null, $min_latitude, $max_lat */ public function findClosestAgentByPostalCode($carrier, $country, $postal_code) { - $method = 'agents/closest/carrier/'.$carrier.'/country/'.$country.'/postalcode/'.$postal_code; + $method = 'agents/closest/carrier/'.$carrier.'/country/'.$country.'/postalcode/'.$postal_code; - return $this->httpGet( - $method, - $args = array(), $headers = array(), $body = null, $timeout = $this->getAgentTimeout() + return $this->httpGet( + $method, + $args = array(), $headers = array(), $body = null, $timeout = $this->getAgentTimeout() ); } From 3cb010bb20cfae74f824cf852272147e6a95a734 Mon Sep 17 00:00:00 2001 From: Sakri Koskimies Date: Wed, 2 Feb 2022 13:06:48 +0200 Subject: [PATCH 4/9] Apply original formatting --- smart-send-logistics/includes/lib/Smartsend/Api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart-send-logistics/includes/lib/Smartsend/Api.php b/smart-send-logistics/includes/lib/Smartsend/Api.php index a2b50d6..82e8c1e 100644 --- a/smart-send-logistics/includes/lib/Smartsend/Api.php +++ b/smart-send-logistics/includes/lib/Smartsend/Api.php @@ -169,7 +169,7 @@ public function findClosestAgentByPostalCode($carrier, $country, $postal_code) return $this->httpGet( $method, $args = array(), $headers = array(), $body = null, $timeout = $this->getAgentTimeout() - ); + ); } /* From d481e9fc6d9f37ccf9b5831f98d110e623f474b7 Mon Sep 17 00:00:00 2001 From: Bilfeldt Date: Sun, 6 Feb 2022 11:18:46 +0100 Subject: [PATCH 5/9] Update to single filter Implement a single filter 'smart_send_agent_search_address' --- .../frontend/class-ss-shipping-frontend.php | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php index cf87fbe..f06bd37 100644 --- a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php +++ b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php @@ -70,36 +70,32 @@ public function display_ss_pickup_points($method, $index) ($method_id == 'smart_send_shipping') && ($chosen_shipping == $shipping_id) && (stripos($meta_data['smart_send_shipping_method'], 'agent') !== false)) { - - $country = apply_filters( - 'smart_send_agent_search_country', - wc_clean(isset($_POST['s_country']) ? $_POST['s_country'] : '') - ); - - $postal_code = apply_filters( - 'smart_send_agent_search_postal_code', - wc_clean(isset($_POST['s_postcode']) ? $_POST['s_postcode'] : '') - ); - - if (!empty($country) && !empty($postal_code)) { - $city = isset($_POST['s_city']) ? $_POST['s_city'] : ''; // Not required but preferred - $city = apply_filters( - 'smart_send_agent_search_city', - !empty($city) ? wc_clean($city) : null - ); - - $street = apply_filters( - 'smart_send_agent_search_street', - wc_clean(isset($_POST['s_address']) ? $_POST['s_address'] : '') - ); - + + $agentSearchAddress = apply_filters('smart_send_agent_search_address', [ + 'country' => empty($_POST['s_country']) ? null : wc_clean($_POST['s_country']), + 'postal_code' => empty($_POST['s_postcode']) ? null : wc_clean($_POST['s_postcode']), + 'city' => empty($_POST['s_city']) ? null : wc_clean($_POST['s_city']), + 'address' => empty($_POST['s_address']) ? null : wc_clean($_POST['s_address']), + ]); + + if ($agentSearchAddress['country'] && $agentSearchAddress['postal_code']) { $carrier = SS_SHIPPING_WC()->get_shipping_method_carrier($meta_data['smart_send_shipping_method']); // Production API does not allow street addresses shorter than 5 characters - if(empty($street) || strlen($street) < 5){ - $ss_agents = $this->find_closest_agents_by_postal_code($carrier, $country, $postal_code); - }else{ - $ss_agents = $this->find_closest_agents_by_address($carrier, $country, $postal_code, $city, $street); + if ($agentSearchAddress['address'] && strlen($agentSearchAddress['address']) >= 5) { + $ss_agents = $this->find_closest_agents_by_address( + $carrier, + $agentSearchAddress['country'], + $agentSearchAddress['postal_code'], + $agentSearchAddress['city'], + $agentSearchAddress['address'] + ); + } else { + $ss_agents = $this->find_closest_agents_by_postal_code( + $carrier, + $agentSearchAddress['country'], + $agentSearchAddress['postal_code'], + ); } if (!empty($ss_agents)) { From e2b85e6c661eb153d1df3e15c386e789c5a997af Mon Sep 17 00:00:00 2001 From: Bilfeldt Date: Sun, 6 Feb 2022 11:21:13 +0100 Subject: [PATCH 6/9] Fix indention --- .../frontend/class-ss-shipping-frontend.php | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php index f06bd37..86618dc 100644 --- a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php +++ b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php @@ -162,34 +162,34 @@ public function find_closest_agents_by_address($carrier, $country, $postal_code, } } - /** - * Find the closest agents by postal code - * - * @param $carrier string Unique carrier code - * @param $country string ISO3166-A2 Country code - * @param $postal_code string - * - * @return array - */ - public function find_closest_agents_by_postal_code($carrier, $country, $postal_code) - { - SS_SHIPPING_WC()->log_msg('Called "findClosestAgentByPostalCode" for website ' . SS_SHIPPING_WC()->get_website_url() . ' with carrier = "' . $carrier . '", country = "' . $country . '", postcode = "' . $postal_code . '"'); + /** + * Find the closest agents by postal code + * + * @param $carrier string Unique carrier code + * @param $country string ISO3166-A2 Country code + * @param $postal_code string + * + * @return array + */ + public function find_closest_agents_by_postal_code($carrier, $country, $postal_code) + { + SS_SHIPPING_WC()->log_msg('Called "findClosestAgentByPostalCode" for website ' . SS_SHIPPING_WC()->get_website_url() . ' with carrier = "' . $carrier . '", country = "' . $country . '", postcode = "' . $postal_code . '"'); - if (SS_SHIPPING_WC()->get_api_handle()->findClosestAgentByPostalCode($carrier, $country, $postal_code)) { + if (SS_SHIPPING_WC()->get_api_handle()->findClosestAgentByPostalCode($carrier, $country, $postal_code)) { - $ss_agents = SS_SHIPPING_WC()->get_api_handle()->getData(); + $ss_agents = SS_SHIPPING_WC()->get_api_handle()->getData(); - SS_SHIPPING_WC()->log_msg('Response from "findClosestAgentByPostalCode": ' . SS_SHIPPING_WC()->get_api_handle()->getResponseBody()); - // Save all of the agents in sessions - WC()->session->set('ss_shipping_agents', $ss_agents); + SS_SHIPPING_WC()->log_msg('Response from "findClosestAgentByPostalCode": ' . SS_SHIPPING_WC()->get_api_handle()->getResponseBody()); + // Save all of the agents in sessions + WC()->session->set('ss_shipping_agents', $ss_agents); - return $ss_agents; - } else { - SS_SHIPPING_WC()->log_msg( 'Response from "findClosestAgentByPostalCode": ' . SS_SHIPPING_WC()->get_api_handle()->getErrorString() ); + return $ss_agents; + } else { + SS_SHIPPING_WC()->log_msg( 'Response from "findClosestAgentByPostalCode": ' . SS_SHIPPING_WC()->get_api_handle()->getErrorString() ); - return array(); - } - } + return array(); + } + } /** * Get the formatted address to display on the frontend From 487768538c0022378108a75b813f0b115c76e45f Mon Sep 17 00:00:00 2001 From: Bilfeldt Date: Sun, 6 Feb 2022 11:22:52 +0100 Subject: [PATCH 7/9] Fix docblock --- .../frontend/class-ss-shipping-frontend.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php index 86618dc..f50a69c 100644 --- a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php +++ b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php @@ -163,14 +163,14 @@ public function find_closest_agents_by_address($carrier, $country, $postal_code, } /** - * Find the closest agents by postal code - * - * @param $carrier string Unique carrier code - * @param $country string ISO3166-A2 Country code - * @param $postal_code string - * - * @return array - */ + * Find the closest agents by postal code + * + * @param $carrier string Unique carrier code + * @param $country string ISO3166-A2 Country code + * @param $postal_code string + * + * @return array + */ public function find_closest_agents_by_postal_code($carrier, $country, $postal_code) { SS_SHIPPING_WC()->log_msg('Called "findClosestAgentByPostalCode" for website ' . SS_SHIPPING_WC()->get_website_url() . ' with carrier = "' . $carrier . '", country = "' . $country . '", postcode = "' . $postal_code . '"'); From 772cd6f47d69e28f4b9d8147099f93e051b3227a Mon Sep 17 00:00:00 2001 From: Bilfeldt Date: Sun, 6 Feb 2022 11:24:26 +0100 Subject: [PATCH 8/9] Revert changes to lib --- smart-send-logistics/includes/lib/Smartsend/Api.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/smart-send-logistics/includes/lib/Smartsend/Api.php b/smart-send-logistics/includes/lib/Smartsend/Api.php index 82e8c1e..d01553a 100644 --- a/smart-send-logistics/includes/lib/Smartsend/Api.php +++ b/smart-send-logistics/includes/lib/Smartsend/Api.php @@ -164,10 +164,8 @@ public function getAgentsInArea($carrier, $country=null, $min_latitude, $max_lat */ public function findClosestAgentByPostalCode($carrier, $country, $postal_code) { - $method = 'agents/closest/carrier/'.$carrier.'/country/'.$country.'/postalcode/'.$postal_code; - return $this->httpGet( - $method, + $method = 'agents/closest/carrier/'.$carrier.'/country/'.$country.'/postalcode/'.$postal_code, $args = array(), $headers = array(), $body = null, $timeout = $this->getAgentTimeout() ); } @@ -393,4 +391,4 @@ private function stripEndpointFromLink($url) return $url; } } -} \ No newline at end of file +} From e2963b63be0b0808a4584a281e610453c79c37b5 Mon Sep 17 00:00:00 2001 From: Sakri Koskimies Date: Mon, 7 Feb 2022 11:29:19 +0200 Subject: [PATCH 9/9] Remove comma from fn call --- .../includes/frontend/class-ss-shipping-frontend.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php index f50a69c..c30ee8c 100644 --- a/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php +++ b/smart-send-logistics/includes/frontend/class-ss-shipping-frontend.php @@ -94,7 +94,7 @@ public function display_ss_pickup_points($method, $index) $ss_agents = $this->find_closest_agents_by_postal_code( $carrier, $agentSearchAddress['country'], - $agentSearchAddress['postal_code'], + $agentSearchAddress['postal_code'] ); }