Skip to content

Commit

Permalink
Merge pull request #12 from UVLabs/development
Browse files Browse the repository at this point in the history
chore: syncing
  • Loading branch information
UVLabs authored Sep 27, 2021
2 parents 6626472 + d94f028 commit 1fb5cfd
Show file tree
Hide file tree
Showing 9 changed files with 635 additions and 367 deletions.
2 changes: 2 additions & 0 deletions class-lpac-uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public static function remove_plugin_settings() {
'lpac_wc_shipping_classes',
'lpac_wc_shipping_classes_show_hide',
'lpac_enable_save_address_feature',
'lpac_wc_shipping_methods',
'lpac_hide_troubleshooting_admin_checkout_notice',
);

foreach ( $option_keys as $key ) {
Expand Down
625 changes: 345 additions & 280 deletions includes/admin/class-lpac-admin-settings.php

Large diffs are not rendered by default.

28 changes: 5 additions & 23 deletions includes/class-lpac.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,36 +197,17 @@ private function define_public_hooks()
$plugin_public = new Lpac_Public( $this->get_plugin_name(), $this->get_version() );
$plugin_public_display = new Lpac_Public_Display();
$functions_helper = new Lpac_Functions_Helper();
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
/*
* WooCommerce
* If plugin not enabled don't continue
*/
$plugin_enabled = get_option( 'lpac_enabled', 'yes' );
if ( $plugin_enabled === 'no' ) {
return;
}
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
$this->loader->add_action( 'wp_head', $plugin_public_display, 'lpac_output_map_custom_styles' );
$checkout_page_map_location = get_option( 'lpac_checkout_map_orientation', 'woocommerce_after_checkout_shipping_form' );
/*
* Update old options.
*/
//TODO Delete this block when confident most users have updated.

if ( $checkout_page_map_location === 'billing_address_area_top' ) {
$checkout_page_map_location = 'woocommerce_before_checkout_billing_form';
update_option( 'lpac_checkout_map_orientation', 'woocommerce_before_checkout_billing_form' );
} elseif ( $checkout_page_map_location === 'billing_address_area_bottom' ) {
$checkout_page_map_location = 'woocommerce_after_checkout_billing_form';
update_option( 'lpac_checkout_map_orientation', 'woocommerce_after_checkout_billing_form' );
} elseif ( $checkout_page_map_location === 'shipping_address_area_top' ) {
$checkout_page_map_location = 'woocommerce_before_checkout_shipping_form';
update_option( 'lpac_checkout_map_orientation', 'woocommerce_before_checkout_shipping_form' );
} elseif ( $checkout_page_map_location === 'shipping_address_area_bottom' ) {
$checkout_page_map_location = 'woocommerce_after_checkout_shipping_form';
update_option( 'lpac_checkout_map_orientation', 'woocommerce_after_checkout_shipping_form' );
}

/*
* Output hidden input fields for latitude and longitude.
*/
Expand Down Expand Up @@ -273,7 +254,8 @@ private function define_public_hooks()
4
);
}


$this->loader->add_action( 'woocommerce_before_checkout_form', $plugin_public_display, 'lpac_add_admin_checkout_notice' );
}

/**
Expand Down
36 changes: 33 additions & 3 deletions includes/helpers/class-lpac-functions-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ public static function lpac_get_qr_codes_directory( $base )
*/
public static function lpac_get_available_shipping_classes()
{
$normalized_shipping_classes = array();

if ( !class_exists( 'WC_Shipping' ) ) {
error_log( 'Location Picker at Checkout for WooCommerce: WC_Shipping() class not found.' );
return $normalized_shipping_classes;
return array();
}

$lpac_wc_shipping_classes = ( new WC_Shipping() )->get_shipping_classes();
if ( !is_array( $lpac_wc_shipping_classes ) ) {
return $normalized_shipping_classes;
return array();
}
$normalized_shipping_classes = array();
foreach ( $lpac_wc_shipping_classes as $shipping_class_object ) {
$iterated_shipping_class = array(
$shipping_class_object->term_id => $shipping_class_object->name,
Expand All @@ -154,6 +154,9 @@ public static function lpac_get_available_shipping_classes()
public static function lpac_get_order_shipping_classes()
{
$cart = WC()->cart->get_cart();
if ( !is_array( $cart ) ) {
return array();
}
$shipping_class_array = array();
foreach ( $cart as $cart_item ) {
$shipping_class_id = $cart_item['data']->get_shipping_class_id();
Expand All @@ -162,5 +165,32 @@ public static function lpac_get_order_shipping_classes()
}
return $shipping_class_array;
}

/**
*
* Get all available shipping methods from the shipping zones created by users.
*
* @return array
*/
public static function lpac_get_available_shipping_methods()
{

if ( !class_exists( 'WC_Shipping_Zones' ) ) {
error_log( 'Location Picker at Checkout for WooCommerce: WC_Shipping_Zones() class not found.' );
return array();
}

$zones = WC_Shipping_Zones::get_zones();
if ( !is_array( $zones ) ) {
return array();
}
$shipping_methods = array_column( $zones, 'shipping_methods' );
$flatten = array_merge( ...$shipping_methods );
$normalized_shipping_methods = array();
foreach ( $flatten as $key => $class ) {
$normalized_shipping_methods[$class->id] = $class->method_title;
}
return $normalized_shipping_methods;
}

}
111 changes: 99 additions & 12 deletions includes/public/class-lpac-public-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ public function __construct() {
private function lpac_expose_map_settings_js( $additional = array() ) {

$js_variables = $this->lpac_get_map_settings();

$js_variables = array_merge( $js_variables, $additional );

$map_options = json_encode( $js_variables );

/**
* Shipping methods Admin has decided to hide the map for.
*/
$disallowed_shipping_methods = get_option( 'lpac_wc_shipping_methods', array() );
$disallowed_shipping_methods = json_encode( $disallowed_shipping_methods );

$global_variables = <<<GLOBALVARS
// LPAC Map Options
var map_options = $map_options;
var saved_disallowed_shipping_methods = $disallowed_shipping_methods;
GLOBALVARS;

$this->lpac_global_map_settings_js = $global_variables;
Expand All @@ -72,15 +78,39 @@ public function lpac_output_map_on_checkout_page() {
$instuctions_text = __( 'Click the "Detect Current Location" button then move the red marker to your desired shipping address.', 'lpac' );
$instuctions_text = apply_filters( 'lpac_map_instuctions_text', $instuctions_text );

$user_id = (int) get_current_user_id();

$saved_addresses_area = apply_filters( 'lpac_saved_addresses', '', $user_id );
$saved_addresses_area = wp_kses_post( $saved_addresses_area );

$before_map_filter = apply_filters( 'lpac_before_map', '', $user_id );
$before_map_filter = wp_kses_post( $before_map_filter );

$after_map_filter = apply_filters( 'lpac_after_map', '', $user_id );
$after_map_filter = wp_kses_post( $after_map_filter );

$before_map_controls_filter = apply_filters( 'lpac_before_map_controls', '', $user_id );
$before_map_controls_filter = wp_kses_post( $before_map_controls_filter );

$after_map_controls_filter = apply_filters( 'lpac_after_map_controls', '', $user_id );
$after_map_controls_filter = wp_kses_post( $after_map_controls_filter );

$markup = <<<MAP
<div class='woocommerce-shipping-fields__field-wrapper lpac-map'></div>
<div class='woocommerce-shipping-fields__field-wrapper'>
<div style="font-size: 10px">$instuctions_text</div>
<button id='lpac-find-location-btn' type='button'>$lpac_find_location_btn_text</button>
<div id="lpac-map-container" class='woocommerce-shipping-fields__field-wrapper'>
$before_map_filter
<div class='lpac-map'></div>
$after_map_filter
<div class='lpac-map-controls'>
$before_map_controls_filter
<div style="font-size: 10px">$instuctions_text</div>
<button id='lpac-find-location-btn' type='button'>$lpac_find_location_btn_text</button>
<div id='lpac-saved-addresses'><ul>$saved_addresses_area</ul></div>
$after_map_controls_filter
</div>
</div>
MAP;

echo $markup;
echo apply_filters( 'lpac_map_markup', $markup, $user_id );

// Add inline global JS so that we can use data fetched using PHP inside JS
wp_add_inline_script( LPAC_PLUGIN_NAME . '-base-map', $this->lpac_global_map_settings_js, 'before' );
Expand All @@ -94,7 +124,7 @@ public function lpac_output_map_on_checkout_page() {
*/
public function lpac_output_map_on_order_details_page() {

global $woocommerce, $wp;
global $wp;

// If this isn't the order received page shown after a purchase, or the view order page shown on the user account, then bail.
if ( ! is_wc_endpoint_url( 'view-order' ) && ! is_wc_endpoint_url( 'order-received' ) ) {
Expand Down Expand Up @@ -139,8 +169,8 @@ public function lpac_output_map_on_order_details_page() {
$this->lpac_expose_map_settings_js( $user_location_collected_during_order );

$markup = <<<MAP
<div class='woocommerce-shipping-fields__field-wrapper lpac-map'></div>
<div class='woocommerce-shipping-fields__field-wrapper'>
<div id="lpac-map-container" class='woocommerce-shipping-fields__field-wrapper'>
<div class='lpac-map'></div>
</div>
MAP;

Expand Down Expand Up @@ -185,15 +215,30 @@ public function lpac_create_lat_and_long_inputs( $fields ) {
*
* @since 1.1.0
* @param array $order_id The order id.
*
* @return void
*/
public function lpac_validate_location_fields( $fields, $errors ) : void {
public function lpac_validate_location_fields( $fields, $errors ) {

/**
* The map visibility might be changed via JS or other conditions
* So we need to check if its actually shown before trying to validate
*/
$map_shown = (bool) $fields['lpac_is_map_shown'];

if ( $map_shown === false ) {
return;
}

/**
* Allow users to override this setting
*/
$custom_override = apply_filters( 'lpac_override_map_validation', false, $fields, $errors );

if ( $custom_override === true ) {
return;
}

$error_msg = '<strong>' . __( 'Please select your location using the Google Map.', 'lpac' ) . '</strong>';

$error_msg = apply_filters( 'lpac_checkout_empty_cords_error_msg', $error_msg );
Expand All @@ -212,13 +257,19 @@ public function lpac_validate_location_fields( $fields, $errors ) : void {
*/
public function lpac_save_cords_order_meta( $order_id ) {

$latitude = ( isset( $_POST['lpac_latitude'] ) ) ? $_POST['lpac_latitude'] : '';
$longitude = ( isset( $_POST['lpac_longitude'] ) ) ? $_POST['lpac_longitude'] : '';
$latitude = $_POST['lpac_latitude'] ?? '';
$longitude = $_POST['lpac_longitude'] ?? '';
$map_shown = $_POST['lpac_is_map_shown'] ?? '';

if ( empty( $latitude ) || empty( $longitude ) ) {
return;
}

// If the map was not shown for this order don't save the coordinates.
if ( empty( $map_shown ) ) {
return;
}

update_post_meta( $order_id, '_lpac_latitude', sanitize_text_field( $_POST['lpac_latitude'] ) );
update_post_meta( $order_id, '_lpac_longitude', sanitize_text_field( $_POST['lpac_longitude'] ) );
}
Expand Down Expand Up @@ -347,4 +398,40 @@ public function lpac_add_delivery_location_link_to_email( $order, $sent_to_admin

}

public function lpac_add_admin_checkout_notice() {

$hide_notice = get_option( 'lpac_hide_troubleshooting_admin_checkout_notice', 'no' );

if ( $hide_notice === 'yes' ) {
return;
}

$user_id = get_current_user_id();

if ( empty( $user_id ) ) {
return;
}

if ( ! current_user_can( 'manage_options' ) ) {
return;
}

$notice_text = esc_html__( 'Hi Admin, some websites might have issues with displaying or using the Google Map. If you\'re having issues then please have a look at your browser console for any errors.' );
$additional = esc_html__( 'Only Admins on your website can see this notice. You can turn it off in the plugin settings if everything works fine.' );

$markup = <<<MARKUP
<div class="lpac-admin-notice" style="background: #246df3; color: #ffffff; text-align: center; margin-bottom: 20px; padding: 10px;">
<p style="font-size:14px; "><span style="font-weight: bold">LPAC: </span>
$notice_text
</p>
<p style="font-size:12px; font-weight: bold;" >
$additional
</p>
</div>
MARKUP;

echo $markup;

}

}
27 changes: 16 additions & 11 deletions includes/public/js/lpac-public.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,31 @@
* Although scripts in the WordPress core, Plugins and Themes may be
* practising this, we should strive to set a better example in our own work.
*/

// TODO Can most likely merge this into checkout-page-map.js
$( document ).ready(

function(){

var map_div = document.querySelector( '.lpac-map' );
var lat = document.querySelector( '#lpac_latitude_field' );
var long = document.querySelector( '#lpac_longitude_field' );
var map_div = document.querySelector( '#lpac-map-container' );
var map_shown = document.querySelector( '#lpac_is_map_shown' );
var map_present = true;
var map_visibility = '';

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

if ( ! map_div ) {
/**
* Detect if map is present and the display property
*/
if( typeof( map_div ) === 'undefined' || map_div === null ){
map_present = false;
}else{
map_visibility = map_div.style.display;
}

if ( lat && long ) {
lat.remove();
long.remove();
}
if ( map_present === false || map_visibility === 'none' ) {

if ( map_shown ) {
map_shown.value = 0
Expand Down
Loading

0 comments on commit 1fb5cfd

Please sign in to comment.