prefix . "addressya_credential";
$wpdb->get_results($dropTable);
}
// Add css and js for admin and client side
add_action('wp_enqueue_scripts', 'afw_add_custom_files');
add_action('admin_enqueue_scripts', 'afw_add_custom_files');
function afw_add_custom_files() {
wp_enqueue_script('wp-util');
wp_register_style('afw_register_custom_css', plugins_url('assets/css/index.css', __FILE__), array(), '2.0');
wp_enqueue_style('afw_register_custom_css');
wp_enqueue_script('afw_register_custom_script', plugins_url('assets/js/index.js', __FILE__), array('jquery'), '2.1', true);
$translation_array = array( 'afw_image_data' => plugins_url('assets/images/addressya_logo.png', __FILE__));
wp_localize_script( 'afw_register_custom_script', 'afw_image_url', $translation_array );
$translation_array_for_logo = array( 'afw_addressya_logo_image' => plugins_url('assets/images/logo_no_text.png', __FILE__));
wp_localize_script( 'afw_register_custom_script', 'afw_image_url_for_logo', $translation_array_for_logo );
}
// For Adding Menu ( Addressya Login ) in Admin
add_action('admin_menu', 'afw_add_addressya_login_menu');
function afw_add_addressya_login_menu() {
add_menu_page(
'Addressya Login', //Page Title
'Addressya Login', //Menu Title
'manage_options', //Capability
'addressya-login', //Page slug
'afw_addressya_login_html', //Callback to print html
'dashicons-migrate' //Icon
);
}
function afw_addressya_login_html() {
$admin = new AFW_AdminModule();
$admin->getAddressyaLoginMenu();
}
// For Ajax Call To Verify Admin Credential
add_action('wp_ajax_nopriv_verify_credential', 'afw_verify_credential');
add_action('wp_ajax_verify_credential', 'afw_verify_credential');
function afw_verify_credential() {
$e_mail = sanitize_text_field($_REQUEST['e_mail']);
$password = sanitize_text_field($_REQUEST['password']);
$client_id = sanitize_text_field($_REQUEST['client_id']);
$apiEndPoint = "https://api.addressya.com/v1/oauth/token";
$response = wp_remote_request($apiEndPoint, array(
'method' => 'POST',
'redirection' => 10,
'httpversion' => '1.1',
'body' => array(
'grant_type' => 'password',
'email' => $e_mail,
'password' => $password,
'return_refresh_token' => 'true',
'client_id' => $client_id
)
));
$body = wp_remote_retrieve_body($response);
$results = json_decode($body, true);
wp_send_json_success($results);
}
// For Ajax Call To Save Admin Credential
add_action('wp_ajax_nopriv_verify_save_credential', 'afw_verify_save_credential');
add_action('wp_ajax_verify_save_credential', 'afw_verify_save_credential');
function afw_verify_save_credential() {
global $wpdb;
$e_mail = sanitize_text_field($_REQUEST['e_mail']);
$password = sanitize_text_field($_REQUEST['password']);
$client_id = sanitize_text_field($_REQUEST['client_id']);
$organization_id = sanitize_text_field($_REQUEST['organization_id']);
$organization_name = sanitize_text_field($_REQUEST['organization_name']);
$insertCredential = "INSERT INTO " . $wpdb->prefix . "addressya_credential" .
"(business_name, email, password, clientid, organizationid) " .
"VALUES ( '$organization_name' , '$e_mail' , '$password' , '$client_id', '$organization_id' )";
$wpdb->get_results($insertCredential);
wp_send_json_success('Success');
}
// For Ajax Call To Remove Admin Credential
add_action('wp_ajax_nopriv_remove_addressya_credential', 'afw_remove_addressya_credential');
add_action('wp_ajax_remove_addressya_credential', 'afw_remove_addressya_credential');
function afw_remove_addressya_credential() {
global $wpdb;
$sql = "TRUNCATE TABLE " . $wpdb->prefix . "addressya_credential";
$result = $wpdb->get_results($sql);
wp_send_json_success($result);
}
// Increase TimeOut Limit In Http
add_filter('http_request_timeout', 'afw_timeout_extend');
function afw_timeout_extend($time) {
return 20;
}
// Check if business crendential found then client side functionality works
$client = new AFW_ClientModule();
$results = $client->getCredential();
if (isset($results->email) && isset($results->password) && isset($results->clientid)) {
// Add fields for addressya user and show/hide address field
add_filter('woocommerce_checkout_fields', 'afw_add_fields_for_addressya_user');
function afw_add_fields_for_addressya_user($fields) {
$client = new AFW_ClientModule();
$country_calling_code = $client->getCountryCallingCode();
$fields['billing']['addressya_user_yes'] = array(
'type' => 'radio',
'options' => array('yes' => 'I want to use my Addressya address'),
'default' => 'yes',
'label_class' => array('afw_radio_label'),
'priority' => '25'
);
$fields['billing']['addressya_user'] = array(
'type' => 'text',
'label' => 'Addressya User',
'required' => true,
'label_class' => array('afw_required_field'),
'placeholder' => 'Username',
'priority' => '27'
);
$fields['billing']['addressya_user_no'] = array(
'type' => 'radio',
'options' => array('no' => 'I want to use a new address'),
'label_class' => array('afw_radio_label'),
'priority' => '30'
);
$fields['billing']['create_addressya_credential'] = array(
'type' => 'checkbox',
'label' => 'Save this address on Addressya for easier checkout next time! Create your account below. Learn more',
'priority' => '102',
'default' => 1
);
$fields['billing']['create_addressya_email'] = array(
'type' => 'text',
'label' => 'E-Mail',
'placeholder' => 'E-Mail',
'label_class' => array('afw_required_field'),
'priority' => '105'
);
$fields['billing']['create_addressya_password'] = array(
'type' => 'password',
'label' => 'Password',
'placeholder' => 'Password',
'label_class' => array('afw_required_field'),
'priority' => '107'
);
$fields['billing']['create_addressya_username'] = array(
'type' => 'text',
'label' => 'Username',
'placeholder' => 'Username',
'label_class' => array('afw_required_field'),
'priority' => '108'
);
$fields['billing']['create_addressya_country_code'] = array(
'type' => 'select',
'label' => 'Country Code',
'label_class' => array('afw_required_field'),
'options' => $country_calling_code,
'priority' => '109',
'input_class' => array('searchable-select-country-code')
);
$fields['billing']['create_addressya_telephone'] = array(
'type' => 'text',
'label' => 'Telephone',
'placeholder' => 'Telephone',
'label_class' => array('afw_required_field'),
'priority' => '111'
);
$fields['billing']['create_addressya_credential_tc'] = array(
'type' => 'checkbox',
'label' => 'I accept Addressya\'s terms and conditions',
'required' => true,
'label_class' => array('afw_required_field'),
'priority' => '113',
'default' => 1
);
$fields['billing']['create_addressya_share_location'] = array(
'type' => 'checkbox',
'label' => 'Yes, please share my address details for this service delivery. I understand that I can revoke the permission at any time.',
'required' => true,
'label_class' => array('afw_required_field'),
'priority' => '115',
'default' => 1
);
$fields['shipping']['addressya_user_yes_ship'] = array(
'type' => 'radio',
'options' => array('yes' => 'I want to use my Addressya address'),
'default' => 'yes',
'label_class' => array('afw_radio_label'),
'priority' => '0'
);
$fields['shipping']['addressya_user_ship'] = array(
'type' => 'text',
'label' => 'Addressya User',
'required' => true,
'label_class' => array('afw_required_field'),
'placeholder' => 'Username',
'priority' => '3'
);
$fields['shipping']['addressya_user_no_ship'] = array(
'type' => 'radio',
'options' => array('no' => 'I want to use a new address'),
'label_class' => array('afw_radio_label'),
'priority' => '6'
);
$fields['billing']['billing_phone']['priority'] = '21';
$fields['billing']['billing_email']['priority'] = '23';
$fields['billing']['billing_company']['priority'] = '31';
if (isset($_POST['addressya_user_yes'])) {
$fields['billing']['billing_state']['required'] = false;
$fields['billing']['billing_address_1']['required'] = false;
$fields['billing']['billing_city']['required'] = false;
$fields['billing']['billing_postcode']['required'] = false;
$fields['billing']['addressya_user']['required'] = true;
} else {
$fields['billing']['billing_address_1']['required'] = true;
$fields['billing']['billing_city']['required'] = true;
$fields['billing']['billing_postcode']['required'] = true;
$fields['billing']['addressya_user']['required'] = false;
}
if (isset($_POST['addressya_user_yes_ship'])) {
$fields['shipping']['shipping_first_name']['required'] = false;
$fields['shipping']['shipping_last_name']['required'] = false;
$fields['shipping']['shipping_state']['required'] = false;
$fields['shipping']['shipping_address_1']['required'] = false;
$fields['shipping']['shipping_city']['required'] = false;
$fields['shipping']['shipping_postcode']['required'] = false;
$fields['shipping']['addressya_user_ship']['required'] = true;
} else {
$fields['shipping']['shipping_first_name']['required'] = true;
$fields['shipping']['shipping_last_name']['required'] = true;
$fields['shipping']['shipping_address_1']['required'] = true;
$fields['shipping']['shipping_city']['required'] = true;
$fields['shipping']['shipping_postcode']['required'] = true;
$fields['shipping']['addressya_user_ship']['required'] = false;
}
if (isset($_POST['create_addressya_credential']) && isset($_POST['addressya_user_no'])) {
$fields['billing']['create_addressya_email']['required'] = true;
$fields['billing']['create_addressya_password']['required'] = true;
$fields['billing']['create_addressya_username']['required'] = true;
$fields['billing']['create_addressya_telephone']['required'] = true;
$fields['billing']['create_addressya_country_code']['required'] = true;
$fields['billing']['create_addressya_credential_tc']['required'] = true;
$fields['billing']['create_addressya_share_location']['required'] = true;
} else {
$fields['billing']['create_addressya_email']['required'] = false;
$fields['billing']['create_addressya_password']['required'] = false;
$fields['billing']['create_addressya_username']['required'] = false;
$fields['billing']['create_addressya_telephone']['required'] = false;
$fields['billing']['create_addressya_country_code']['required'] = false;
$fields['billing']['create_addressya_credential_tc']['required'] = false;
$fields['billing']['create_addressya_share_location']['required'] = false;
}
return $fields;
}
// Add image and link for addressya
add_filter('woocommerce_form_field', 'afw_add_addressya_image_and_link', 10, 4);
function afw_add_addressya_image_and_link($field, $key, $args, $value = null) {
switch ($key) {
case 'addressya_user':
$before_custom_style = '
';
$after_custom_style = '
Don\'t have Addressya yet ? Get it on
Google Play or use
Addressya Web App
';
return $before_custom_style . $field . $after_custom_style;
break;
case 'addressya_user_ship':
$before_custom_style_ship = '
';
$after_custom_style_ship = '
Yes, please share my address details for this service delivery. I understand that I can revoke the permission at any time.
Don\'t have Addressya yet ? Get it on
Google Play or use
Addressya Web App
';
return $before_custom_style_ship . $field . $after_custom_style_ship;
break;
case 'create_addressya_credential':
$before_custom_style_create = '
';
return $before_custom_style_create . $field;
break;
case 'create_addressya_share_location':
$after_custom_style_create_button = '
';
return $field . $after_custom_style_create_button;
break;
default:
return $field;
break;
}
}
// To register the session
add_action('init', 'afw_register_session');
function afw_register_session() {
if ( !session_id() ) {
session_start();
}
}
// Check user is verify the addressya username or not
add_action('woocommerce_checkout_process', 'afw_check_and_verify_addressya_username');
function afw_check_and_verify_addressya_username($checkout) {
if (!session_id()) {
session_start();
}
if (isset($_POST['addressya_user_yes']) && !empty($_POST['addressya_user'])) {
if ((isset($_SESSION['is_user_verified_billing']) && sanitize_text_field($_SESSION['is_user_verified_billing']) == 'false') || !isset($_SESSION['is_user_verified_billing'])) {
wc_add_notice('Please verify billing addressya username before continue', 'error');
}
}
if (isset($_POST['addressya_user_yes_ship']) && !empty($_POST['addressya_user_ship']) && isset($_POST['ship_to_different_address'])) {
if ((isset($_SESSION['is_user_verified_shipping']) && sanitize_text_field($_SESSION['is_user_verified_shipping']) == 'false') || !isset($_SESSION['is_user_verified_shipping'])) {
wc_add_notice('Please verify shipping addressya username before continue', 'error');
}
}
}
// Save Addressya User fields to order meta data
add_action('woocommerce_checkout_update_order_meta', 'afw_save_addressya_user_added');
function afw_save_addressya_user_added($order_id) {
if (isset($_POST['addressya_user_yes']) && !empty($_POST['addressya_user']) && !empty($_SESSION['user_location_id_billing']) && !empty($_SESSION['user_uid_billing'])) {
update_post_meta($order_id, 'addressya_user_bill', sanitize_text_field($_POST['addressya_user']));
update_post_meta($order_id, 'addressya_location_bill', sanitize_text_field($_SESSION['user_location_id_billing']));
update_post_meta($order_id, 'addressya_user_uid_bill', sanitize_text_field($_SESSION['user_uid_billing']));
}
if (isset($_POST['addressya_user_yes_ship']) && !empty($_POST['addressya_user_ship']) && isset($_POST['ship_to_different_address']) && !empty($_SESSION['user_location_id_shipping']) && !empty($_SESSION['user_uid_shipping'])) {
update_post_meta($order_id, 'addressya_user_ship', sanitize_text_field($_POST['addressya_user_ship']));
update_post_meta($order_id, 'addressya_location_ship', sanitize_text_field($_SESSION['user_location_id_shipping']));
update_post_meta($order_id, 'addressya_user_uid_ship', sanitize_text_field($_SESSION['user_uid_shipping']));
}
if (isset($_POST['create_addressya_credential']) && !empty($_SESSION['is_new_addressya_user']) && sanitize_text_field($_SESSION['is_new_addressya_user']) == 'true' && !empty($_SESSION['new_user_location_id']) && !empty($_SESSION['new_user_uid'])) {
update_post_meta($order_id, 'addressya_user_bill', sanitize_text_field($_POST['create_addressya_username']));
update_post_meta($order_id, 'addressya_location_bill', sanitize_text_field($_SESSION['new_user_location_id']));
update_post_meta($order_id, 'addressya_user_uid_bill', sanitize_text_field($_SESSION['new_user_uid']));
}
}
// For Sending Notifications
add_filter('woocommerce_thankyou_order_received_text', 'afw_send_notification', 20, 2);
function afw_send_notification($default_title, $order) {
$client = new AFW_ClientModule();
$location_id = get_post_meta($order->get_id(), 'addressya_location_bill', true);
$customer_username = get_post_meta($order->get_id(), 'addressya_user_bill', true);
$recipient_id = get_post_meta($order->get_id(), 'addressya_user_uid_bill', true);
$message = 'You have placed an order.';
$notification_text = '
You have placed an order.
';
if((isset($_SESSION['is_user_verified_billing']) && sanitize_text_field($_SESSION['is_user_verified_billing']) == 'true') || (isset($_SESSION['is_user_verified_shipping']) && sanitize_text_field($_SESSION['is_user_verified_shipping']) == 'true')) {
$client->sendNotification($location_id, $recipient_id, '', $customer_username, 'order-placed', $message);
$_SESSION['is_user_verified_billing'] = 'false';
$_SESSION['is_user_verified_shipping'] = 'false';
$_SESSION['user_location_id_billing'] = '';
$_SESSION['user_location_id_shipping'] = '';
$_SESSION['user_uid_billing'] = '';
$_SESSION['user_uid_shipping'] = '';
$default_title = $notification_text . $default_title;
}
else if(isset($_SESSION['is_new_addressya_user']) && sanitize_text_field($_SESSION['is_new_addressya_user']) == 'true') {
$client->sendNotification($location_id, $recipient_id, '', $customer_username, 'order-placed', $message);
$_SESSION['is_new_addressya_user'] = 'false';
$_SESSION['new_user_location_id'] = '';
$_SESSION['new_user_uid'] = '';
$default_title = $notification_text . $default_title;
}
return $default_title;
}
//For showing addressya detail on thank you (order placed)
add_filter('woocommerce_order_details_after_order_table', 'afw_addressya_detail_on_thank_you', 10, 1);
function afw_addressya_detail_on_thank_you($order) {
$user_login = is_user_logged_in();
if($user_login) {
$client = new AFW_ClientModule();
$client->showing_address_in_order_history($order->get_id());
}
}
// For adding addressya username in admin order detail for billing
add_action('woocommerce_admin_order_data_after_billing_address', 'afw_add_addressya_username_admin_bill');
function afw_add_addressya_username_admin_bill($order) {
$admin = new AFW_AdminModule();
$admin->add_addressya_username_admin_bill($order);
}
// For adding addressya username in admin order detail for shipping
add_action('woocommerce_admin_order_data_after_shipping_address', 'afw_add_addressya_username_admin_ship');
function afw_add_addressya_username_admin_ship($order) {
$admin = new AFW_AdminModule();
$admin->add_addressya_username_admin_ship($order);
}
// Remove optional text from addressya billing and shipping field
add_filter('woocommerce_form_field', 'afw_remove_optional_text', 9999, 4);
function afw_remove_optional_text($field, $key, $args, $value) {
if (is_checkout() && !is_wc_endpoint_url() && ($key == 'addressya_user' || $key == 'addressya_user_ship' || $key == 'create_addressya_credential' || $key == 'create_addressya_credential_tc' || $key == 'create_addressya_share_location'
|| $key == 'create_addressya_email' || $key == 'create_addressya_password' || $key == 'create_addressya_username' || $key == 'create_addressya_telephone' || $key == 'create_addressya_country_code')) {
$optional = ' (' . esc_html__('optional', 'woocommerce') . ')';
$field = str_replace($optional, '', $field);
}
return $field;
}
// For showing address in order history if user login
add_action('woocommerce_view_order', 'afw_showing_address_in_order_history', 20);
function afw_showing_address_in_order_history($order_id) {
$client = new AFW_ClientModule();
$client->showing_address_in_order_history($order_id);
}
// For Adding Menu ( Drivers ) in Admin
add_action('admin_menu', 'afw_add_drivers_menu');
function afw_add_drivers_menu() {
add_menu_page(
'Drivers', //Page Title
'Drivers', //Menu Title
'manage_options', //Capability
'drivers', //Page slug
'afw_drivers_html', //Callback to print html
'dashicons-car' //Icon
);
}
function afw_drivers_html() {
$admin = new AFW_AdminModule();
$admin->add_driver_module_html();
}
// For Adding Menu ( Vehicle Master ) in Admin
add_action('admin_menu', 'afw_vehicle_master');
function afw_vehicle_master() {
add_menu_page(
'Vehicles', //Page Title
'Vehicles', //Menu Title
'manage_options', //Capability
'vehicles', //Page slug
'afw_vehicle_master_html', //Callback to print html
'dashicons-car' //Icon
);
}
function afw_vehicle_master_html() {
$admin = new AFW_AdminModule();
$admin->vehicleMasterModule();
}
// For Ajax Call To Verify Driver User For Admin
add_action('wp_ajax_nopriv_verify_alias', 'afw_verify_alias');
add_action('wp_ajax_verify_alias', 'afw_verify_alias');
function afw_verify_alias() {
$client = new AFW_ClientModule();
$results = $client->searchAlias(sanitize_text_field($_REQUEST['alias']));
wp_send_json_success($results);
}
// For Ajax Call To Add/Edit Driver Details For Admin
add_action('wp_ajax_nopriv_add_edit_driver_details', 'afw_add_edit_driver_details');
add_action('wp_ajax_add_edit_driver_details', 'afw_add_edit_driver_details');
function afw_add_edit_driver_details() {
global $wpdb;
$driver_username = sanitize_text_field($_REQUEST['driver_username']);
$driver_firstname = sanitize_text_field($_REQUEST['driver_firstname']);
$driver_lastname = sanitize_text_field($_REQUEST['driver_lastname']);
$driver_email = sanitize_text_field($_REQUEST['driver_email']);
$driver_mobile = sanitize_text_field($_REQUEST['driver_mobile']);
$driver_status = sanitize_text_field($_REQUEST['driver_status']);
$driver_vehicle = sanitize_text_field($_REQUEST['driver_vehicle']);
$driver_vehiclenumber = sanitize_text_field($_REQUEST['driver_vehiclenumber']);
$driver_id = sanitize_text_field($_REQUEST['driver_id']);
$member_id = sanitize_text_field($_REQUEST['member_id']);
if (!empty($driver_id)) {
$wpdb->get_results("UPDATE `" . $wpdb->prefix . "addressya_driver` SET username = '" . $driver_username . "', firstname = '" . $driver_firstname . "', lastname = '" . $driver_lastname . "', email = '" . $driver_email . "', mobile = '" . $driver_mobile . "', vehicle_id = '" . $driver_vehicle . "', vehicle_number = '" . $driver_vehiclenumber . "', member_id = '" . $member_id . "', status = '" . (int)$driver_status . "' WHERE driver_id = '" . (int)$driver_id . "'");
} else {
$wpdb->get_results("INSERT INTO `" . $wpdb->prefix . "addressya_driver` SET username = '" . $driver_username . "', mobile = '" . $driver_mobile . "', firstname = '" . $driver_firstname . "', lastname = '" . $driver_lastname . "', email = '" . $driver_email . "', vehicle_id = '" . $driver_vehicle . "', vehicle_number = '" . $driver_vehiclenumber . "', member_id = '" . $member_id . "', status = '" . (int)$driver_status . "'");
}
$admin = new AFW_AdminModule();
$result = $admin->addDriverUnderOrganization($member_id);
wp_send_json_success($result);
}
// For Ajax Call To Add/Edit Vehicle Details For Admin
add_action('wp_ajax_nopriv_add_edit_vehicle_details', 'afw_add_edit_vehicle_details');
add_action('wp_ajax_add_edit_vehicle_details', 'afw_add_edit_vehicle_details');
function afw_add_edit_vehicle_details() {
global $wpdb;
$vehicle_name = sanitize_text_field($_REQUEST['vehicle_name']);
$vehicle_id = sanitize_text_field($_REQUEST['vehicle_id']);
if (!empty($vehicle_id)) {
$result = $wpdb->get_results("UPDATE `" . $wpdb->prefix . "addressya_vehicle` SET vehicle_name = '" . $vehicle_name . "' WHERE vehicle_id = '" . (int)$vehicle_id . "'");
} else {
$result = $wpdb->get_results("INSERT INTO `" . $wpdb->prefix . "addressya_vehicle` SET vehicle_name = '" . $vehicle_name . "'");
}
wp_send_json_success($result);
}
// For Ajax Call To Delete Driver Details For Admin
add_action('wp_ajax_nopriv_delete_drivers', 'afw_delete_drivers');
add_action('wp_ajax_delete_drivers', 'afw_delete_drivers');
function afw_delete_drivers() {
global $wpdb;
$admin = new AFW_AdminModule();
$client = new AFW_ClientModule();
$temp = $client->getCredential();
$driver_ids = array_map("strip_tags", $_REQUEST['driver_id']);
foreach ($driver_ids as $value) {
$driver_billing = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE meta_key = 'driver_id_billing' AND meta_value = '" . (int)$value . "'");
foreach ($driver_billing as $print_billing) {
$memberId = get_post_meta($print_billing->post_id, 'driver_member_id_billing', true);
$connectionId = get_post_meta($print_billing->post_id, 'addressya_user_uid_bill', true);
$locationId = get_post_meta($print_billing->post_id, 'addressya_location_bill', true);
$customer_username = get_post_meta($print_billing->post_id, 'addressya_user_bill', true);
$admin->deleteInheritedConnectionFromDriver($memberId, $connectionId, $locationId);
$message = $temp->business_name . " unassigned you from this order reference : " . $print_billing->post_id . ". You do no longer have access to the customers address details.";
$client->sendNotification($locationId, $memberId, '', $customer_username, 'unassigned-customer', $message);
delete_post_meta($print_billing->post_id, 'driver_member_id_billing');
delete_post_meta($print_billing->post_id, 'driver_firstname_billing');
delete_post_meta($print_billing->post_id, 'driver_id_billing');
}
$driver_shipping = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE meta_key = 'driver_id_shipping' AND meta_value = '" . (int)$value . "'");
foreach ($driver_shipping as $print_shipping) {
$memberIdShip = get_post_meta($print_shipping->post_id, 'driver_member_id_shipping', true);
$connectionIdShip = get_post_meta($print_shipping->post_id, 'addressya_user_uid_ship', true);
$locationIdShip = get_post_meta($print_shipping->post_id, 'addressya_location_ship', true);
$customer_username_ship = get_post_meta($print_shipping->post_id, 'addressya_user_ship', true);
$admin->deleteInheritedConnectionFromDriver($memberIdShip, $connectionIdShip, $locationIdShip);
$message = $temp->business_name . " unassigned you from this order reference : " . $print_shipping->post_id . ". You do no longer have access to the customers address details.";
$client->sendNotification($locationIdShip, $memberIdShip, '', $customer_username_ship, 'unassigned-customer', $message);
delete_post_meta($print_shipping->post_id, 'driver_member_id_shipping');
delete_post_meta($print_shipping->post_id, 'driver_firstname_shipping');
delete_post_meta($print_shipping->post_id, 'driver_id_shipping');
}
$result = $wpdb->get_results("DELETE FROM `" . $wpdb->prefix . "addressya_driver` WHERE driver_id = '" . (int)$value . "'");
}
wp_send_json_success($result);
}
// For Ajax Call To Delete Vehicle Details For Admin
add_action('wp_ajax_nopriv_delete_vehicles', 'afw_delete_vehicles');
add_action('wp_ajax_delete_vehicles', 'afw_delete_vehicles');
function afw_delete_vehicles() {
global $wpdb;
$vehicle_ids = array_map("strip_tags", $_REQUEST['vehicle_id']);
foreach ($vehicle_ids as $value) {
$result = $wpdb->get_results("DELETE FROM `" . $wpdb->prefix . "addressya_vehicle` WHERE vehicle_id = '" . (int)$value . "'");
}
wp_send_json_success($result);
}
// For Ajax Call To Assign Driver In Orders For Admin
add_action('wp_ajax_nopriv_assign_driver', 'afw_assign_driver');
add_action('wp_ajax_assign_driver', 'afw_assign_driver');
function afw_assign_driver() {
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "addressya_driver WHERE member_id='" . sanitize_text_field($_REQUEST['driver_member_id']) . "'");
foreach ($result as $print) {
$firstname = $print->firstname;
$driver_id = $print->driver_id;
$lastname = $print->lastname;
$mobile = $print->mobile;
$username = $print->username;
}
$meta_member_id = 'driver_member_id_' . sanitize_text_field($_REQUEST['value']);
$meta_firstname = 'driver_firstname_' . sanitize_text_field($_REQUEST['value']);
$meta_id = 'driver_id_' . sanitize_text_field($_REQUEST['value']);
if (isset($_REQUEST['value']) && sanitize_text_field($_REQUEST['value']) == 'billing') {
$customer_username = get_post_meta(sanitize_text_field($_REQUEST['order_id']), 'addressya_user_bill', true);
$customer_uid = get_post_meta(sanitize_text_field($_REQUEST['order_id']), 'addressya_user_uid_bill', true);
$location_id = get_post_meta(sanitize_text_field($_REQUEST['order_id']), 'addressya_location_bill', true);
} else {
$customer_username = get_post_meta(sanitize_text_field($_REQUEST['order_id']), 'addressya_user_ship', true);
$customer_uid = get_post_meta(sanitize_text_field($_REQUEST['order_id']), 'addressya_user_uid_ship', true);
$location_id = get_post_meta(sanitize_text_field($_REQUEST['order_id']), 'addressya_location_ship', true);
}
$admin = new AFW_AdminModule();
$client = new AFW_ClientModule();
$temp = $client->getCredential();
$result = $admin->copyConnectionToDriver(sanitize_text_field($_REQUEST['driver_member_id']), $customer_uid, $location_id);
if(isset($result['message']) && sanitize_text_field($result['message']) == 'Connection Copied') {
update_post_meta(sanitize_text_field($_REQUEST['order_id']), $meta_member_id, sanitize_text_field($_REQUEST['driver_member_id']));
update_post_meta(sanitize_text_field($_REQUEST['order_id']), $meta_firstname, $firstname);
update_post_meta(sanitize_text_field($_REQUEST['order_id']), $meta_id, $driver_id);
$message = $temp->business_name . " assigned you for a delivery to " . $customer_username . ". Find " . $customer_username . " in your Connections list for the delivery address. Order reference : " . sanitize_text_field($_REQUEST['order_id']);
$client->sendNotification($location_id, sanitize_text_field($_REQUEST['driver_member_id']), $username, $customer_username, 'assigned-customer', $message);
$message = "Your order from " . $temp->business_name . " will be delivered by " . $firstname . " " . $lastname . " (Contact No. : " . $mobile . "). Order reference " . sanitize_text_field($_REQUEST['order_id']);
$client->sendNotification($location_id, $customer_uid, $username, $customer_username, 'assigned-driver', $message);
}
wp_send_json_success($result);
}
// For Ajax Call To UnAssign Driver In Orders For Admin
add_action('wp_ajax_nopriv_unassign_driver', 'afw_unassign_driver');
add_action('wp_ajax_unassign_driver', 'afw_unassign_driver');
function afw_unassign_driver() {
$meta_username = 'driver_member_id_' . sanitize_text_field($_REQUEST['value']);
$meta_firstname = 'driver_firstname_' . sanitize_text_field($_REQUEST['value']);
$meta_id = 'driver_id_' . sanitize_text_field($_REQUEST['value']);
$result = delete_post_meta(sanitize_text_field($_REQUEST['order_id']), $meta_username);
$result = delete_post_meta(sanitize_text_field($_REQUEST['order_id']), $meta_firstname);
$result = delete_post_meta(sanitize_text_field($_REQUEST['order_id']), $meta_id);
$admin = new AFW_AdminModule();
$client = new AFW_ClientModule();
$temp = $client->getCredential();
$memberId = sanitize_text_field($_REQUEST['driver_member_id']);
$connectionId = sanitize_text_field($_REQUEST['user_uid']);
$locationId = sanitize_text_field($_REQUEST['location_id']);
$customer_username = sanitize_text_field($_REQUEST['customer_username']);
$admin->deleteInheritedConnectionFromDriver($memberId, $connectionId, $locationId);
$message = $temp->business_name . " unassigned you from this order reference : " . sanitize_text_field($_REQUEST['order_id']) . ". You do no longer have access to the customers address details.";
$client->sendNotification($locationId, $memberId, '', $customer_username, 'unassigned-customer', $message);
wp_send_json_success($result);
}
// For unassigning the driver when order is deleted from admin
add_action('wp_trash_post', 'afw_on_order_delete', 10, 1);
function afw_on_order_delete($orderId) {
$post_type = get_post_type($orderId);
$admin = new AFW_AdminModule();
$client = new AFW_ClientModule();
$temp = $client->getCredential();
if ($post_type !== 'shop_order') {
return;
}
if (!empty(get_post_meta($orderId, 'driver_member_id_billing', true)) && !empty(get_post_meta($orderId, 'driver_firstname_billing', true))) {
$memberId = get_post_meta($orderId, 'driver_member_id_billing', true);
$customer_username = get_post_meta($orderId, 'addressya_user_bill', true);
$connectionId = get_post_meta($orderId, 'addressya_user_uid_bill', true);
$locationId = get_post_meta($orderId, 'addressya_location_bill', true);
$admin->deleteInheritedConnectionFromDriver($memberId, $connectionId, $locationId);
$message = $temp->business_name . " unassigned you from this order reference : " . $orderId . ". You do no longer have access to the customers address details.";
$client->sendNotification($locationId, $memberId, '', $customer_username, 'unassigned-customer', $message);
delete_post_meta($orderId, 'driver_member_id_billing');
delete_post_meta($orderId, 'driver_firstname_billing');
delete_post_meta($orderId, 'driver_id_billing');
}
if (!empty(get_post_meta($orderId, 'driver_member_id_shipping', true)) && !empty(get_post_meta($orderId, 'driver_firstname_shipping', true))) {
$memberIdShip = get_post_meta($orderId, 'driver_member_id_shipping', true);
$customer_username_ship = get_post_meta($orderId, 'addressya_user_ship', true);
$connectionIdShip = get_post_meta($orderId, 'addressya_user_uid_ship', true);
$locationIdShip = get_post_meta($orderId, 'addressya_location_ship', true);
$admin->deleteInheritedConnectionFromDriver($memberIdShip, $connectionIdShip, $locationIdShip);
$message = $temp->business_name . " unassigned you from this order reference : " . $orderId . ". You do no longer have access to the customers address details.";
$client->sendNotification($locationId, $memberId, '', $customer_username_ship, 'unassigned-customer', $message);
delete_post_meta($orderId, 'driver_member_id_shipping');
delete_post_meta($orderId, 'driver_firstname_shipping');
delete_post_meta($orderId, 'driver_id_shipping');
}
}
// For unassigning the driver when order status is changed
add_action('woocommerce_order_status_changed', 'afw_on_order_status_changed', 99, 3);
function afw_on_order_status_changed($orderId, $old_status, $new_status) {
if ($new_status == "completed") {
$admin = new AFW_AdminModule();
$client = new AFW_ClientModule();
$temp = $client->getCredential();
if (!empty(get_post_meta($orderId, 'driver_member_id_billing', true)) && !empty(get_post_meta($orderId, 'driver_firstname_billing', true))) {
$memberId = get_post_meta($orderId, 'driver_member_id_billing', true);
$customer_username = get_post_meta($orderId, 'addressya_user_bill', true);
$connectionId = get_post_meta($orderId, 'addressya_user_uid_bill', true);
$locationId = get_post_meta($orderId, 'addressya_location_bill', true);
$admin->deleteInheritedConnectionFromDriver($memberId, $connectionId, $locationId);
$message = $temp->business_name . " unassigned you from this order reference : " . $orderId . ". You do no longer have access to the customers address details.";
$client->sendNotification($locationId, $memberId, '', $customer_username, 'unassigned-customer', $message);
delete_post_meta($orderId, 'driver_member_id_billing');
delete_post_meta($orderId, 'driver_firstname_billing');
delete_post_meta($orderId, 'driver_id_billing');
}
if (!empty(get_post_meta($orderId, 'driver_member_id_shipping', true)) && !empty(get_post_meta($orderId, 'driver_firstname_shipping', true))) {
$memberIdShip = get_post_meta($orderId, 'driver_member_id_shipping', true);
$customer_username_ship = get_post_meta($orderId, 'addressya_user_ship', true);
$connectionIdShip = get_post_meta($orderId, 'addressya_user_uid_ship', true);
$locationIdShip = get_post_meta($orderId, 'addressya_location_ship', true);
$admin->deleteInheritedConnectionFromDriver($memberIdShip, $connectionIdShip, $locationIdShip);
$message = $temp->business_name . " unassigned you from this order reference : " . $orderId . ". You do no longer have access to the customers address details.";
$client->sendNotification($locationId, $memberId, '', $customer_username_ship, 'unassigned-customer', $message);
delete_post_meta($orderId, 'driver_member_id_shipping');
delete_post_meta($orderId, 'driver_firstname_shipping');
delete_post_meta($orderId, 'driver_id_shipping');
}
}
}
// For Ajax Call To Verify User In Admin
add_action('wp_ajax_nopriv_verify_admin_username', 'afw_verify_admin_username');
add_action('wp_ajax_verify_admin_username', 'afw_verify_admin_username');
function afw_verify_admin_username() {
$client = new AFW_ClientModule();
$results = $client->searchAlias(sanitize_text_field($_REQUEST['alias']));
$session_key = 'addressya_admin_' . sanitize_text_field($_REQUEST['type']);
if (!empty($results)) {
$_SESSION[$session_key] = sanitize_text_field($_REQUEST['alias']);
}
wp_send_json_success($results);
}
// For storing the username when admin create order
add_action('woocommerce_process_shop_order_meta', 'afw_save_addressya_details');
function afw_save_addressya_details($order_id) {
if (isset($_SESSION['addressya_admin_billing']) && !empty($_SESSION['addressya_admin_billing'])) {
update_post_meta($order_id, 'addressya_user_bill', sanitize_text_field($_SESSION['addressya_admin_billing']));
$_SESSION['addressya_admin_billing'] = '';
}
if (isset($_SESSION['addressya_admin_shipping']) && !empty($_SESSION['addressya_admin_shipping'])) {
update_post_meta($order_id, 'addressya_user_ship', sanitize_text_field($_SESSION['addressya_admin_shipping']));
$_SESSION['addressya_admin_shipping'] = '';
}
}
// For Ajax Call To Create New Addressya User
add_action('wp_ajax_nopriv_create_new_user_credential', 'afw_create_new_user_credential');
add_action('wp_ajax_create_new_user_credential', 'afw_create_new_user_credential');
function afw_create_new_user_credential() {
$newUserObj = array(
'email' => sanitize_text_field($_REQUEST['create_addressya_email']),
'password' => sanitize_text_field($_REQUEST['create_addressya_password']),
'displayEmail' => sanitize_text_field($_REQUEST['create_addressya_email']),
'firstName' => sanitize_text_field($_REQUEST['firstname']),
'lastName' => sanitize_text_field($_REQUEST['lastname']),
'phoneNumber' => sanitize_text_field($_REQUEST['create_addressya_telephone']),
'userName' => sanitize_text_field($_REQUEST['create_addressya_username']),
'isd' => sanitize_text_field($_REQUEST['create_addressya_country_code']),
'city' => sanitize_text_field($_REQUEST['city']),
'country' => sanitize_text_field($_REQUEST['country_name']),
'region' => sanitize_text_field($_REQUEST['state']),
'streetName' => sanitize_text_field($_REQUEST['address_2']),
'houseNumber' => sanitize_text_field($_REQUEST['address_1'])
);
$client = new AFW_ClientModule();
$results = $client->createNewAddressyaUser($newUserObj);
wp_send_json_success($results);
}
// For Ajax Call To Send Otp Code In Checkout
add_action('wp_ajax_nopriv_send_otp_code', 'afw_send_otp_code');
add_action('wp_ajax_send_otp_code', 'afw_send_otp_code');
function afw_send_otp_code() {
$client = new AFW_ClientModule();
$result = $client->sendOtp(sanitize_text_field($_REQUEST['alias']));
wp_send_json_success($result);
}
// For Ajax Call To Verify Otp Code In Checkout
add_action('wp_ajax_nopriv_verify_otp_code', 'afw_verify_otp_code');
add_action('wp_ajax_verify_otp_code', 'afw_verify_otp_code');
function afw_verify_otp_code() {
$client = new AFW_ClientModule();
$result = $client->verifyOtp(sanitize_text_field($_REQUEST['alias']),sanitize_text_field($_REQUEST['otp']),sanitize_text_field($_REQUEST['requestId']),sanitize_text_field($_REQUEST['locationId']));
$session_key = 'is_user_verified_' . sanitize_text_field($_REQUEST['type']);
$session_location_key = 'user_location_id_' . sanitize_text_field($_REQUEST['type']);
$session_user_id_key = 'user_uid_' . sanitize_text_field($_REQUEST['type']);;
$_SESSION[$session_key] = 'false';
if(isset($result) && $result != 'Error') {
$_SESSION[$session_key] = 'true';
$_SESSION[$session_location_key] = sanitize_text_field($_REQUEST['locationId']);
$_SESSION[$session_user_id_key] = sanitize_text_field($_REQUEST['userId']);
}
wp_send_json_success($result);
}
}
}