Skip to content

Commit

Permalink
Merge pull request #78 from luizbills/patch-3
Browse files Browse the repository at this point in the history
  • Loading branch information
luizbills authored Sep 19, 2023
2 parents 4f37c16 + b8b1d95 commit 8d16b7d
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 190 deletions.
8 changes: 8 additions & 0 deletions classes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public function __start () {

// plugin action links
add_filter( 'plugin_action_links_' . plugin_basename( h::config_get( 'FILE' ) ), [ $this, 'add_plugin_action_links' ] );

// Maybe enable integration: Autofill Addresses
add_filter( 'wc_shipping_simulator_integration_autofill_br_addresses_enabled', [ $this, 'enable_autofill_addresses' ] );
}

public static function get_option ( $key ) {
Expand Down Expand Up @@ -77,4 +80,9 @@ public function add_plugin_action_links ( $actions ) {
$actions
);
}

public function enable_autofill_addresses ( $value ) {
$value = 'yes' === self::get_option( 'autofill_addresses' );
return $value;
}
}
8 changes: 8 additions & 0 deletions classes/Admin/inc/settings_fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
'desc_tip' => esc_html__( 'Disable this option to allow customers simulate shipping rates even when a variation is not selected on variable products. However, always make sure that the variable product has a defined weight.', 'wc-shipping-simulator' ),
'default' => 'yes'
],
[
'id' => $prefix . 'autofill_addresses',
'type' => 'checkbox',
'name' => esc_html__( 'Display full address', 'wc-shipping-simulator' ),
'desc' => esc_html__( 'Enable', 'wc-shipping-simulator' ),
'desc_tip' => esc_html__( 'When this option is activated, the street, neighborhood and city will be displayed in the shipping simulator.', 'wc-shipping-simulator' ),
'default' => 'yes'
],
[
'id' => $prefix . 'update_address',
'type' => 'radio',
Expand Down
13 changes: 9 additions & 4 deletions classes/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public static function user_is_admin ( $user_id = null ) {
}

public static function sanitize_postcode ( $postcode ) {
return preg_replace( '/[^0-9]/', '', $postcode );
$sanitized = preg_replace( '/[^0-9]/', '', $postcode );
return (string) apply_filters(
'wc_shipping_simulator_sanitize_postcode',
$sanitized,
$postcode
);
}

public static function product_needs_shipping ( $product ) {
Expand Down Expand Up @@ -63,8 +68,8 @@ public static function product_needs_shipping ( $product ) {
);
}

public static function logger ( $args = [] ) {
return \apply_filters( h::prefix( 'get_logger' ), null, $args );
public static function logger () {
return \apply_filters( h::prefix( 'get_logger' ), null );
}

public static function get_estimating_delivery ( $days ) {
Expand All @@ -76,6 +81,6 @@ public static function get_estimating_delivery ( $days ) {
$result = sprintf( _n( 'Delivery within %d working day', 'Delivery within %d working days', $days, 'wc-shipping-simulator' ), $days );
} //woocommerce-correios

return apply_filters( 'woocommerce_correios_get_estimating_delivery', $result, $days );
return apply_filters( 'wc_shipping_simulator_get_estimating_delivery', $result, $days );
}
}
147 changes: 147 additions & 0 deletions classes/Integration/Autofill_Brazilian_Addresses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Shipping_Simulator\Integration;

use Shipping_Simulator\Helpers as h;
use function wp_remote_get;

final class Autofill_Brazilian_Addresses {

protected static $instance = null;

/**
* @var array|null
*/
private $address_cache = null;

public static function instance () {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}

public function __start () {
add_action( 'wc_shipping_simulator_load_integrations', [ $this, 'add_hooks' ] );
}

public function is_enabled () {
return apply_filters(
'wc_shipping_simulator_integration_autofill_br_addresses_enabled',
false
);
}

public function add_hooks () {
if ( $this->is_enabled() ) {
add_filter( 'wc_shipping_simulator_package_data', [ $this, 'fill_package_destination' ] );
add_filter( 'wc_shipping_simulator_results_title_address', [ $this, 'results_title_address' ], 20, 2 );
}
}

public function fill_package_destination ( $package ) {
$dest = $package['destination'];

if ( 'BR' !== h::get( $dest['country'] ) ) return $package;

$postcode = h::get( $dest['postcode'] );
$address = $this->get_address( $postcode );

if ( $address ) {
h::log( $package['destination'] );
$package['destination'] = [
'postcode' => $address['postcode'],
'address' => implode(
', ',
array_filter( [
$address['address_1'],
$address['address_2'],
$address['neighborhood'],
] )
),
'address_1' => $address['address_1'],
'address_2' => $address['address_2'],
'city' => $address['city'],
'state' => $address['state'],
'country' => 'BR',
];
h::log( $package['destination'] );
}

return $package;
}

public function results_title_address ( $address_string, $data ) {
$postcode = h::get( $data['postcode'] );
$address = $this->get_address( $postcode );
if ( $address ) {
$parts = [
$address['address_1'],
$address['address_2'],
$address['city'],
$address['state']
];
$address_string = '<strong>' . apply_filters(
'wc_shipping_simulator_integration_correios_results_address',
implode( ', ', array_filter( $parts ) ),
$address
) . '</strong>';
}
return $address_string;
}

/**
* @param string $postcode
* @return array|false
*/
private function get_address ( $postcode ) {
$address = $this->address_cache;
$postcode = h::sanitize_postcode( $postcode );

if ( ! $postcode ) return false;
if ( $address && $postcode === $address['postcode'] ) return $address;

$url = 'https://opencep.com/v1/' . $postcode;
$response = wp_remote_get( $url );

h::logger()->info( "Requesting address to {$url}" );

if ( is_wp_error( $response ) ) {
h::logger()->error( "Request failed: " . $response->get_error_message() );
return false;
}

$status = (int) $response['response']['code'];
$body = (string) $response['body'];

h::logger()->info( "Response status code: {$status}" );
h::logger()->info( "Response body: {$body}" );

if ( 200 === $status ) {
$address = json_decode( $body, true );
if ( is_array( $address ) ) {
$address = $this->format_address( $address );
$this->address_cache = $address;
return $address;
}
}

return false;
}

/**
* @param array $address
* @return array
*/
private function format_address ( $address ) {
return [
'postcode' => h::sanitize_postcode( h::get( $address['cep'], '' ) ),
'address_1' => h::get( $address['logradouro'], '' ),
'address_2' => str_replace( [ '(', ')' ], '', h::get( $address['complemento'], '' ) ),
'neighborhood' => h::get( $address['bairro'], '' ),
'city' => h::get( $address['localidade'], '' ),
'state' => h::get( $address['uf'], '' ),
'country' => 'BR'
];
}
}
23 changes: 23 additions & 0 deletions classes/Integration/Brazil.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function add_hooks () {
add_filter( 'wc_shipping_simulator_wrapper_css_class', [ $this, 'wrapper_css_class' ] );

add_filter( 'wc_shipping_simulator_form_input_type', [ $this, 'form_input_type' ] );

add_filter( 'wc_shipping_simulator_results_title_address', [ $this, 'results_title_address' ], 10, 2 );
}
}

Expand Down Expand Up @@ -98,10 +100,30 @@ public function add_cep_finder_link () {
<?php
}

public function results_title_address ( $address_string, $data ) {
$country = $data['country'] ?? '';
$postcode = $data['postcode'] ?? '';

if ( 'BR' === $country ) {
$state = $this->get_state_by_postcode( $postcode );
if ( $state ) {
$address_string = '<strong>' . $this->format_cep( $postcode ) . ', ' . $state . ', Brasil</strong>';
}
}

return $address_string;
}

protected function is_cep ( $postcode ) {
return h::str_length( $postcode ) === 8;
}

protected function format_cep ( $postcode ) {
$postcode = h::sanitize_postcode( $postcode );
$mask = $this->form_input_mask( null );
return $mask ? h::str_mask( $postcode, $mask ) : $postcode;
}

protected function get_state_by_postcode ( $postcode ) {
$result = null;

Expand All @@ -128,4 +150,5 @@ protected function get_states_postcode_range () {

return $this->state_list;
}

}
95 changes: 0 additions & 95 deletions classes/Integration/Correios.php

This file was deleted.

2 changes: 1 addition & 1 deletion classes/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __start () {
add_filter( h::prefix( 'get_logger' ), [ $this, 'get_logger' ] );
}

public function get_logger ( $logger = null, $args = null ) {
public function get_logger () {
return $this;
}

Expand Down
8 changes: 7 additions & 1 deletion classes/Tweaks.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function results_after () {

public function update_customer_address ( $rates, $package ) {
$opt_update_address = (int) Settings::get_option( 'update_address' );
$dest = $package['destination'];
$dest = $package['destination'] ?? [];
$enabled = apply_filters(
'wc_shipping_simulator_update_shipping_address',
// option enabled AND has shipping options AND has country
Expand All @@ -94,6 +94,9 @@ public function update_customer_address ( $rates, $package ) {
h::get( $dest['postcode'] ),
h::get( $dest['city'] )
);
$customer->set_shipping_address_1( $dest['address_1'] ?? '' );
$customer->set_shipping_address_2( $dest['address_1'] ?? '' );

h::logger()->info( 'Customer shipping address updated to ' . wp_json_encode( $dest ) );

$should_update_billing_address = apply_filters(
Expand All @@ -109,6 +112,9 @@ public function update_customer_address ( $rates, $package ) {
h::get( $dest['postcode'] ),
h::get( $dest['city'] )
);
$customer->set_billing_address_1( $dest['address_1'] ?? '' );
$customer->set_billing_address_2( $dest['address_2'] ?? '' );

h::logger()->info( 'Customer billing address updated to ' . wp_json_encode( $dest ) );
}

Expand Down
Loading

0 comments on commit 8d16b7d

Please sign in to comment.