Skip to content

Commit

Permalink
Merge pull request #15 from UVLabs/development
Browse files Browse the repository at this point in the history
* [New] Get the customer's last order location and display it on the map at checkout. 
* [New] Option to remove Plus Code from Google Map addresses. 
* [Fix] Periods were being stripped from default coordinates input boxes. 
* [Fix] Wrong text domain for some text strings.
* [Fix] Blank infowindow was showing on order maps when shipping address was not present.
* [Info] Plugin has an [Official Website.](https://lpacwp.com)
  • Loading branch information
UVLabs authored Nov 10, 2021
2 parents 3a4022b + 3feec51 commit 56412e0
Show file tree
Hide file tree
Showing 19 changed files with 324 additions and 127 deletions.
7 changes: 5 additions & 2 deletions assets/admin/js/order-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ function lpacSetupShopOrderMap() {
marker.setDraggable(false);
marker.setCursor('default');

infowindow.setContent(`<p> ${locationDetails.shipping_address_1} <br/> ${locationDetails.shipping_address_2} </p>`);
infowindow.open(map, marker);
// Only open the infowindow if we have a shipping address
if( locationDetails.shipping_address_1 ){
infowindow.setContent(`<p> ${locationDetails.shipping_address_1} <br/> ${locationDetails.shipping_address_2} </p>`);
infowindow.open(map, marker);
}

}

Expand Down
4 changes: 2 additions & 2 deletions assets/public/css/lpac-public.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
/* Google maps info window */
.woocommerce-page .lpac-map .gm-style-iw-d p{
font-size: 1.2em;
margin-bottom: 0;

margin-bottom: 0 !important;
}

.woocommerce-view-order .lpac-map .gm-style-iw-d p, .woocommerce-order-received .lpac-map .gm-style-iw-d p{
font-weight: 800;
text-align: center;
Expand Down
14 changes: 7 additions & 7 deletions assets/public/js/maps/base-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ if (typeof lpac_pro_js !== 'undefined') {
}

/**
* Global map_options variable is set in Lpac\Views\Frontend::lpac_expose_map_settings_js
* Global mapOptions variable is set in Lpac\Views\Frontend::setup_global_js_vars
*/
if ( (typeof (map_options) !== 'undefined' && map_options !== null) || ( typeof (locationDetails) !== 'undefined' && locationDetails !== null ) ) {
if ( (typeof (mapOptions) !== 'undefined' && mapOptions !== null) || ( typeof (locationDetails) !== 'undefined' && locationDetails !== null ) ) {

/**
* <Global Settings>
*/
const map = new google.maps.Map(
document.querySelector(".lpac-map"),
{
center: { lat: map_options.lpac_map_default_latitude, lng: map_options.lpac_map_default_longitude },
zoom: map_options.lpac_map_zoom_level,
center: { lat: mapOptions.lpac_map_default_latitude, lng: mapOptions.lpac_map_default_longitude },
zoom: mapOptions.lpac_map_zoom_level,
streetViewControl: false,
clickableIcons: map_options.lpac_map_clickable_icons,
backgroundColor: map_options.lpac_map_background_color, //loading background color
clickableIcons: mapOptions.lpac_map_clickable_icons,
backgroundColor: mapOptions.lpac_map_background_color, //loading background color
mapId: map_id,
}
);
Expand All @@ -74,5 +74,5 @@ if ( (typeof (map_options) !== 'undefined' && map_options !== null) || ( typeof
*/

} else {
console.log('LPAC: map_options object not present, skipping...')
console.log('LPAC: mapOptions object not present, skipping...')
}
105 changes: 93 additions & 12 deletions assets/public/js/maps/checkout-page-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const map = window.lpac_map;
const marker = window.lpac_marker;
const infowindow = window.lpac_infowindow;

const geocoder = new google.maps.Geocoder()
const geocoder = new google.maps.Geocoder();

const find_location_btn = document.querySelector("#lpac-find-location-btn");

Expand All @@ -24,6 +24,28 @@ if (typeof (find_location_btn) !== 'undefined' && find_location_btn !== null) {
console.log('LPAC: Detect location button not present, skipping...')
}

/**
* Removes the plus code from an address if the option is turned on in the plugin's settings.
*/
function lpacRemovePlusCode( address ){

if( ! mapOptions.lpac_remove_address_plus_code ){
return address;
}

const firstBlock = address.split(' ', 1);

if( firstBlock[0].includes('+') ){
address = address.replace( firstBlock[0], '' ).trim();
}

return address;

}

/**
* Get Lat and Long cords from browser navigator.
*/
function get_navigator_coordinates() {

return new Promise(
Expand Down Expand Up @@ -154,8 +176,10 @@ async function lpac_setup_initial_map_marker_position(latlng) {

marker.setPosition(latlng);

const detected_address = results[ 0 ].formatted_address;
let detected_address = results[ 0 ].formatted_address;

detected_address = lpacRemovePlusCode( detected_address );

infowindow.setContent(detected_address);
infowindow.open(map, marker);

Expand Down Expand Up @@ -192,7 +216,9 @@ function lpac_map_listen_to_clicks() {

marker.setPosition(event.latLng)

const detected_address = results[ 0 ].formatted_address;
let detected_address = results[ 0 ].formatted_address;

detected_address = lpacRemovePlusCode(detected_address);

infowindow.setContent(detected_address);
infowindow.open(map, marker);
Expand Down Expand Up @@ -228,6 +254,9 @@ function lpac_marker_listen_to_drag() {
}

let moved_to_address = results[ 0 ].formatted_address

moved_to_address = lpacRemovePlusCode(moved_to_address);

infowindow.setContent(moved_to_address)

lpac_fill_in_address_fields(results, moved_to_latlng)
Expand Down Expand Up @@ -278,12 +307,12 @@ function lpac_fill_in_address_fields(results, latLng = '') {
lpac_fill_in_shipping_state_county(results)
lpac_fill_in_shipping_zipcode(results)

if (typeof (map_options) === 'undefined' || map_options === null) {
console.log('LPAC: map_options object not present, skipping...')
if (typeof (mapOptions) === 'undefined' || mapOptions === null) {
console.log('LPAC: mapOptions object not present, skipping...')
return;
}

const lpac_autofill_billing_fields = map_options.lpac_autofill_billing_fields
const lpac_autofill_billing_fields = mapOptions.lpac_autofill_billing_fields

if (lpac_autofill_billing_fields) {
lpac_fill_in_billing_country_region(results)
Expand Down Expand Up @@ -323,7 +352,9 @@ function lpac_get_full_address(results) {
return;
}

const full_address = results[ 0 ].formatted_address
let full_address = results[ 0 ].formatted_address;

full_address = lpacRemovePlusCode(full_address);

return full_address;
}
Expand Down Expand Up @@ -503,7 +534,7 @@ function lpac_fill_in_shipping_state_county(results) {
/*
* If we have values in our lpac_get_state_county() function
*/
if (lpac_get_state_county(results)) {
if ( lpac_get_state_county(results) ) {

/*
* This field changes based on the country.
Expand Down Expand Up @@ -614,7 +645,7 @@ function changeMapVisibility(show){
*
* See Lpac\Controllers::Map_Visibility_Controller
*/
function hide_show_map(){
function lpacHideShowMap(){

wp.ajax.post( "lpac_to_be_or_not_to_be", {} )
.done(function(response) {
Expand All @@ -632,18 +663,68 @@ function hide_show_map(){

}

/**
* Set the previous order marker.
*/
function lpacSetLastOrderMarker(){

// Wait for map to load then add our marker
google.maps.event.addListenerOnce( map, 'tilesloaded', function(){

if( typeof (lpacLastOrder) === 'undefined' || lpacLastOrder === null ){
return;
}

const latlng = {
lat: parseFloat(lpacLastOrder.latitude),
lng: parseFloat(lpacLastOrder.longitude),
}

let latitude = document.querySelector('#lpac_latitude');
let longitude = document.querySelector('#lpac_longitude');

if (typeof (latitude) === 'undefined' || latitude === null) {
console.log('LPAC: Can\'t find latitude and longitude input areas. Can\'t insert location coordinates.');
}

if (typeof (longitude) === 'undefined' || longitude === null) {
console.log('LPAC: Can\'t find latitude and longitude input areas. Can\'t insert location coordinates.');
}

latitude.value = lpacLastOrder.latitude
longitude.value = lpacLastOrder.longitude

map.setZoom(16);
map.setCenter(latlng);

marker.setPosition(latlng);

// Only open the infowindow if we have a shipping address from the last order.
if( lpacLastOrder.address ){
infowindow.setContent(lpacLastOrder.address);
infowindow.open(map, marker);
}

lpac_marker_listen_to_drag();
lpac_map_listen_to_clicks();


});

}

/**
* Detect when shipping methods are changed based on WC custom updated_checkout event.
* This event can't be accessed via vanilla JS but is the most reliable for performing this action.
* This event can't be accessed via vanilla JS because it's triggered by jQuery.
*/
(function ($) {
'use strict';

$(document).ready(
function () {

$(document.body).on('updated_checkout', hide_show_map);

$(document.body).on('updated_checkout', lpacHideShowMap);
lpacSetLastOrderMarker();
}
);

Expand Down
17 changes: 10 additions & 7 deletions assets/public/js/maps/order-details-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ function lpac_setup_order_details_map() {
return;
}

if (typeof (map_options) === 'undefined' || map_options === null) {
console.log('LPAC: map_options object not present, skipping...')
if (typeof (mapOptions) === 'undefined' || mapOptions === null) {
console.log('LPAC: mapOptions object not present, skipping...')
return;
}

map.setOptions(
{
center: { lat: map_options.lpac_map_order_latitude, lng: map_options.lpac_map_order_longitude },
center: { lat: mapOptions.lpac_map_order_latitude, lng: mapOptions.lpac_map_order_longitude },
zoom: 16,
draggableCursor: 'default',
keyboardShortcuts: false,
Expand All @@ -25,16 +25,19 @@ function lpac_setup_order_details_map() {
);

const latlng = {
lat: map_options.lpac_map_order_latitude,
lng: map_options.lpac_map_order_longitude,
lat: mapOptions.lpac_map_order_latitude,
lng: mapOptions.lpac_map_order_longitude,
};

marker.setPosition(latlng);
marker.setDraggable(false);
marker.setCursor('default');

infowindow.setContent(`<p> ${map_options.lpac_map_order_shipping_address_1} <br/> ${map_options.lpac_map_order_shipping_address_2} </p>`);
infowindow.open(map, marker);
// Only open the infowindow if we have a shipping address
if( mapOptions.lpac_map_order_shipping_address_1 ){
infowindow.setContent(`<p> ${mapOptions.lpac_map_order_shipping_address_1} <br/> ${mapOptions.lpac_map_order_shipping_address_2} </p>`);
infowindow.open(map, marker);
}

}

Expand Down
17 changes: 10 additions & 7 deletions assets/public/js/maps/order-received-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ function lpac_setup_order_received_map() {
return;
}

if (typeof (map_options) === 'undefined' || map_options === null) {
console.log('LPAC: map_options object not present, skipping...')
if (typeof (mapOptions) === 'undefined' || mapOptions === null) {
console.log('LPAC: mapOptions object not present, skipping...')
return;
}

map.setOptions(
{
center: { lat: map_options.lpac_map_order_latitude, lng: map_options.lpac_map_order_longitude },
center: { lat: mapOptions.lpac_map_order_latitude, lng: mapOptions.lpac_map_order_longitude },
zoom: 16,
draggableCursor: 'default',
keyboardShortcuts: false,
Expand All @@ -25,16 +25,19 @@ function lpac_setup_order_received_map() {
);

const latlng = {
lat: map_options.lpac_map_order_latitude,
lng: map_options.lpac_map_order_longitude,
lat: mapOptions.lpac_map_order_latitude,
lng: mapOptions.lpac_map_order_longitude,
};

marker.setPosition(latlng);
marker.setDraggable(false);
marker.setCursor('default');

infowindow.setContent(`<p> ${map_options.lpac_map_order_shipping_address_1} <br/> ${map_options.lpac_map_order_shipping_address_2} </p>`);
infowindow.open(map, marker);
// Only open the infowindow if we have a shipping address
if( mapOptions.lpac_map_order_shipping_address_1 ){
infowindow.setContent(`<p> ${mapOptions.lpac_map_order_shipping_address_1} <br/> ${mapOptions.lpac_map_order_shipping_address_2} </p>`);
infowindow.open(map, marker);
}

}

Expand Down
1 change: 1 addition & 0 deletions class-lpac-uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static function remove_plugin_settings() {
'lpac_map_anchor_points',
'lpac_admin_view_order_map_id',
'lpac_installed_at_version',
'lpac_remove_address_plus_code',
);

foreach ( $option_keys as $key ) {
Expand Down
2 changes: 2 additions & 0 deletions includes/Bootstrap/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Lpac\Controllers\Emails_Controller ;
use Lpac\Controllers\Map_Visibility_Controller ;
use Lpac\Controllers\Admin_Settings_Controller ;
use Lpac\Controllers\Checkout_Page_Controller ;
use Lpac\Views\Admin as Admin_Display ;
use Lpac\Notices\Admin as Admin_Notices ;
use Lpac\Views\Frontend as Frontend_Display ;
Expand Down Expand Up @@ -181,6 +182,7 @@ private function define_public_hooks()
$plugin_public_display = new Frontend_Display();
$controller_emails = new Emails_Controller();
$controller_map_visibility = new Map_Visibility_Controller();
$controler_checkout_page = new Checkout_Page_Controller();
/*
* If plugin not enabled don't continue
*/
Expand Down
6 changes: 3 additions & 3 deletions includes/Compatibility/WooFunnels/Woo_Funnels.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Woo_Funnels {
public function create_lpac_fields() {

$this->map_shown_field = array(
'label' => __( 'Map Shown', 'woocommerce-email-validation' ),
'label' => __( 'Map Shown', 'map-location-picker-at-checkout-for-woocommerce' ),
'type' => 'text',
'field_type' => 'billing',
'class' => ( LPAC_DEBUG ) ? array( 'wfacp-col-full' ) : array( 'wfacp-col-full', 'hidden' ),
Expand All @@ -59,7 +59,7 @@ public function create_lpac_fields() {
);

$this->latitude_field = array(
'label' => __( 'Latitude', 'woocommerce-email-validation' ),
'label' => __( 'Latitude', 'map-location-picker-at-checkout-for-woocommerce' ),
'type' => 'text',
'field_type' => 'billing',
'class' => ( LPAC_DEBUG ) ? array( 'wfacp-col-full' ) : array( 'wfacp-col-full', 'hidden' ),
Expand All @@ -68,7 +68,7 @@ public function create_lpac_fields() {
);

$this->longitude_field = array(
'label' => __( 'Longitude', 'woocommerce-email-validation' ),
'label' => __( 'Longitude', 'map-location-picker-at-checkout-for-woocommerce' ),
'type' => 'text',
'field_type' => 'billing',
'class' => ( LPAC_DEBUG ) ? array( 'wfacp-col-full' ) : array( 'wfacp-col-full', 'hidden' ),
Expand Down
6 changes: 3 additions & 3 deletions includes/Controllers/Admin_Settings_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class Admin_Settings_Controller {
*/
public function sanitize_default_map_coordinates( $value, $option, $raw_value ) {

// remove letters from input
$value = preg_replace( '/[^0-9,]/', '', $value );
// Remove letters from input
$value = preg_replace( '/[^0-9,.]/', '', $value );

$value = sanitize_text_field( $value );
$value = trim( $value );
$value = trim( $value, ' ,' );

return $value;

Expand Down
Loading

0 comments on commit 56412e0

Please sign in to comment.