<?php
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
// Update shipment hooks
add_action( 'plugins_loaded', 'wpcfe_initialize_hooks' );
function wpcfe_initialize_hooks(){  

    // Frontend Manager Sidebar Hook
    add_action( 'wpcfe_before_add_shipment', 'wpcfe_shipment_dashboard_menu_callback' );

    // Add actions
	add_action( 'before_wpcfe_shipment_form_submit', 'wpcfe_shipment_history_template', 60, 1 );
    add_action( 'before_wpcfe_shipment_form_submit', 'wpcfe_assigned_shipment_template', 40, 1 );
    add_action( 'after_wpcfe_shipment_form_fields', 'wpcfe_shipment_multipackage_template', 10, 1 );
    add_action( 'after_wpcfe_shipment_form_fields', 'wpcfe_shipment_history_table_template', 10, 2 );
		
	add_action( 'after_wpcfe_save_shipment', 'wpcfe_save_shipment_history', 10, 2 );
	add_action( 'after_wpcfe_save_shipment', 'wpcfe_assigned_shipment_save', 10, 2 );
    add_action( 'after_wpcfe_save_shipment', 'wpcfe_shipment_multipackage_save', 10, 2 );
    add_action( 'after_wpcfe_save_shipment', 'wpcfe_shipment_status_save', 20, 2 );
    // Print BOL
    add_filter( 'wpcfe_print_options', 'wpcfe_print_options_callback', 10, 1 );
    // Fonts
    add_filter( 'wpcargo_print_fonts', 'wpcfe_print_fonts_callback', 10, 1 );
    // After User Registration
    add_action( 'wpcfe_after_user_registration_success', 'wpcfe_user_registration_success', 10, 2 );
    add_filter( 'wp_new_user_notification_email', 'wpcfe_wp_new_user_notification_email', 10, 3 );
    add_filter( 'wp_new_user_notification_email_admin', 'wpcfe_wp_new_user_notification_email_admin', 10, 3 );
}
//  Remove Access Employee to ener wp-admin page
add_action( 'admin_head','wpcfe_restrict_employee_to_wpadmin' );
function wpcfe_restrict_employee_to_wpadmin(){
    $current_user = wp_get_current_user();
    $user_role    =  $current_user->roles;
    if( is_admin() && in_array('wpcargo_employee', $user_role) ){
        if( wpcfe_admin_page() ){
            wp_redirect( get_permalink( wpcfe_admin_page() ) );
        }else{
            wp_redirect(home_url());
        }
        exit;
    }
}
// Hooks Dashboard
function wpcfe_shipment_dashboard_menu_callback(){
    $active_class = ( get_the_ID() == wpcfe_admin_page() && isset( $_GET['wpcfe']) && $_GET['wpcfe'] == 'dashboard' ) ? 'active' : '' ;
    ?>
    <a href="<?php echo get_the_permalink( wpcfe_admin_page() ); ?>/?wpcfe=dashboard" class="wpcfe-dashboard list-group-item waves-effect <?php echo $active_class; ?>"> <i class="fa fa-line-chart mr-md-3 mr-3"></i><?php echo apply_filters( 'wpcfe_report_menu_label', esc_html__( 'Dashboard', 'wpcargo-frontend-manager') ); ?> </a>
    <?php
}
add_action( 'wpcfe_shipment_table_header_action', 'wpcfe_shipment_table_header_shipment_type_callback' );
function wpcfe_shipment_table_header_shipment_type_callback(){
    echo '<th>'.__('Shipment Type', 'wpcargo-frontend-manager' ).'</th>';
}
add_action( 'wpcfe_shipment_table_action', 'wpcfe_shipment_type_action_callback', 10, 1 );
function wpcfe_shipment_type_action_callback( $shipment_id ){
    echo '<td class="shipment-type '.wpcfe_to_slug( wpcfe_get_shipment_type( $shipment_id ) ).'">'.wpcfe_get_shipment_type( $shipment_id ).'</td>';
}
add_filter( 'login_redirect', 'wpcfe_custom_login_redirect', 10, 3 );
function wpcfe_custom_login_redirect( $redirect_to, $request, $user ) {   
    $wpcargo_roles = array(
        'wpcargo_employee', 'cargo_agent', 'wpcargo_branch_manager', 'wpcargo_driver', 'wpcargo_client'
    );
    $wpcargo_roles = apply_filters( 'wpcfe_login_redirect_dashboard_role', $wpcargo_roles );
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        if ( in_array( 'administrator', $user->roles ) ) {
            return get_admin_url();
        } elseif( array_intersect( $wpcargo_roles , $user->roles ) ) {
            $redirect_to = get_permalink( wpcfe_admin_page() );
            return $redirect_to;
        }else{
            return get_admin_url();
        }
    } else {
        return $redirect_to;
    }

}
add_filter( 'nav_menu_css_class' , 'wpcfe_current_nav_class' , 10 , 2);
function wpcfe_current_nav_class ($classes, $item) {
    if (in_array('current-menu-item', $classes) ){
        $classes[] = 'active ';
    }
    return $classes;
}
add_action( 'before_wpcfe_shipment_form_fields', 'wpcfe_shipment_title_field', 1 );
function wpcfe_shipment_title_field( $shipment_id ){  
    global $wpcargo;
    if( array_key_exists( 'wpcargo_title_prefix_action', $wpcargo->settings ) ){
        return false;
    }
    $shipment_title = $shipment_id ? get_the_title( $shipment_id ) : '';
    ?>
    <div id="shipment-number" class="col-md-12 mb-4">
        <div class="card">
            <div class="card-body">
                <div class="col-auto p-0">
                <!-- Default input -->
                <label class="sr-only" for="wpcfe_shipment_title"><?php echo apply_filters( 'wpcfe_shipment_number_label', __( 'Shipment Number', 'wpcargo-frontend-manager' ) ); ?></label>
                <div class="input-group">
                    <div class="input-group-prepend">
                    <div class="input-group-text"><i class="fa fa-barcode mr-3"></i><?php echo apply_filters( 'wpcfe_shipment_number_label', __( 'Shipment Number', 'wpcargo-frontend-manager' ) ); ?></div>
                    </div>
                    <input type="text" class="form-control py-0" id="wpcfe_shipment_title" name="wpcfe_shipment_title" value="<?php echo $shipment_title; ?>" >
                </div>
                </div>
            </div>
        </div> 
    </div>
    <?php
}
add_action( 'before_wpcfe_shipment_form_fields', 'wpcfe_form_shipment_title', 1 );
function wpcfe_form_shipment_title( $shipment_id ){
    global $wpcargo;
    if( !array_key_exists( 'wpcargo_title_prefix_action', $wpcargo->settings ) || !(int)$shipment_id ){
        return false;
    }
    ?>
    <div class="col-md-12 mb-3">
        <div class="card">
            <div class="card-header text-center">
                <?php echo apply_filters( 'wpcfe_shipment_number_label', __('Shipment Number', 'wpcargo-frontend-manager' ) ); ?>
                <h5><?php echo get_the_title( $shipment_id ); ?></h5>
            </div>
        </div>
    </div>
    <?php
}
add_filter( 'wpcfe_shipment_number_update', 'wpcfe_shipment_title_update', 10, 2 );
add_filter( 'wpcfe_shipment_number', 'wpcfe_shipment_title_update', 10, 2 );
function  wpcfe_shipment_title_update( $shipment_title, $request ){
    if( isset( $request['wpcfe_shipment_title'] ) ){
        return  sanitize_text_field( $request['wpcfe_shipment_title'] );
    }
    return  $shipment_title;
}
// Bulk update callback
add_action( 'wpcfe_before_after_shipment_table', 'wpcfe_bulk_update_action_callback', 10 );
function wpcfe_bulk_update_action_callback(){
     // Restriction
    $user_roles = wpcfe_current_user_role();
    if( !wpcfe_is_super_admin() && !can_wpcfe_add_shipment() && !can_wpcfe_update_shipment() ){
        return false;
    }
	if( !in_array( 'wpcargo_client', $user_roles ) ){
		?>
		<!-- Button trigger modal -->
		<button id="shipmentBulkUpdate" type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#shipmentBulkUpdateModal"><i class="fa fa-edit text-white"></i> <?php esc_html_e('Bulk Assign', 'wpcargo-frontend-manager'); ?></button>
		<!-- Modal -->
		<?php
	}
}
add_action( 'wpcfe_after_shipment_data', 'wpcfe_bulk_update_modal_action_callback', 10 );
function wpcfe_bulk_update_modal_action_callback(){
    global $wpcargo;
	$user_roles 		= wpcfe_current_user_role();
	$wpcargo_employees 	= wpcfe_get_users('wpcargo_employee');
    $wpcargo_agents 	= wpcfe_get_users('cargo_agent');
    $wpcargo_clients 	= wpcfe_get_users('wpcargo_client');
    // Restriction
    if( !wpcfe_is_super_admin() && !can_wpcfe_add_shipment() && !can_wpcfe_update_shipment() ){
        return false;
    }
	if( !in_array( 'wpcargo_client', $user_roles ) ){
		$template = wpcfe_include_template( 'bulk-assign-shipment.tpl' );
		require_once( $template );
	}
}
/*
 * Save updated data
 */
add_action( 'wp', 'wpcfe_add_shipment' );
function wpcfe_add_shipment(){
    if ( isset( $_POST['wpcfe_add_form_fields'] ) && wp_verify_nonce( $_POST['wpcfe_add_form_fields'], 'wpcfe_add_action' ) ) {
    	wpcfe_save_shipment( $_POST );
    }else{
        return false;
    }
}
add_action( 'wp', 'wpcfe_update_shipment' );
function wpcfe_update_shipment(){
    global $WPCCF_Fields;
    if ( isset( $_POST['wpcfe_form_fields'] ) && wp_verify_nonce( $_POST['wpcfe_form_fields'], 'wpcfe_edit_action' ) && isset( $_POST['shipment_id'] ) && is_wpcfe_shipment($_POST['shipment_id'] )  ) {
    	wpcfe_save_shipment( $_POST, $_POST['shipment_id'] ); 
    }else{
        return false;
    }
}
add_action( 'wp_login_failed', 'wpcfe_login_fail_redirect' );  // hook failed login
function wpcfe_login_fail_redirect( $username ) {
	$referrer          = $_SERVER['HTTP_REFERER'];  // where did the post submission come from?
	$wpcfe_dashboard   = get_the_permalink( wpcfe_admin_page() );
	// if there's a valid referrer, and it's not the default log-in screen
	if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') && wpcfe_admin_page() ) {
		$redirect = apply_filters( 'wpcfe_login_failed_redirect', $wpcfe_dashboard. '?login=failed', $username );
		wp_redirect( $redirect);  // let's append some information (login=failed) to the URL for the theme to use
		exit;
	}
}
// Registration page redirection when user is Logged In
add_action( 'template_redirect', 'wpcfe_registration_page_template_redirect' );
function wpcfe_registration_page_template_redirect(){
    global $post;
    if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'wpcfe_registration') && is_user_logged_in() ){
        $wpcfe_admin = get_option( 'wpcfe_admin', 0 );
        $dashboardURL = ( $wpcfe_admin ) ? get_the_permalink( $wpcfe_admin ) : home_url( ) ;
        wp_redirect( $dashboardURL );
        die;
    }
}
add_filter('views_edit-wpcargo_shipment','wpcfe_download_waybill_callback');
function wpcfe_download_waybill_callback( $views ){
	$views['wpcfe-download-waybill'] = '<button id="wpcfe-download_waybill" class="button button-primary button-small">'.__('Print Selected Waybills', 'wpcargo-frontend-manager').'</button>';
	return $views;
}
add_filter( 'wpcargo_history_fields', 'wpcfe_require_history_fields' );
function wpcfe_require_history_fields( $history_fields ){
	if( isset( $_GET['wpcfe'] ) && $_GET['wpcfe'] == 'add' ){
		foreach( $history_fields as $history_key => $history_field ){
			$history_fields[$history_key]['required'] = 'true';
		}
	}
	return $history_fields;
}
// Fonts
function wpcfe_print_fonts_callback( $fonts ){
    $fonts['amiri'] = array(
        'url' => 'https://fonts.googleapis.com/css2?family=Amiri&display=swap',
        'fontfamily' => "'Amiri', serif"
    );
    $fonts['rubik'] = array(
        'url' => 'https://fonts.googleapis.com/css2?family=Rubik&display=swap',
        'fontfamily' => "'Rubik', sans-serif"
    );
    return $fonts;
}
// BOL callback
function wpcfe_print_options_callback( $options ){
    if( wpcfe_bol_enable() ){
        $options['bol'] = esc_html__('BOL', 'wpcargo-frontend-manager');
    }
    return $options;
}
// Register Hook
function wpcfe_after_login_form_registration(){
    ?>
    <p><?php esc_html_e('Not a member?', 'wpcargo-frontend-manager' ); ?> <a href="#" data-toggle="modal" data-target="#wpcfe-registration" title="<?php esc_html_e( 'Registration', 'wpcargo-frontend-manager' ); ?>"><?php esc_html_e('Register', 'wpcargo-frontend-manager' ); ?></a></p>
    <?php
}
add_action( 'wpcfe_after_login_form', 'wpcfe_after_login_form_registration', 10);
// Frontend Manager hooks
// Registered Shipper
function wpcfe_assigned_shipment_template( $shipment_id ){
    $shipment       = new stdClass();
    $shipment->ID   = $shipment_id;
	$template = wpcfe_include_template( 'assign-shipment.tpl' );
	require_once( $template );
}
function wpcfe_assigned_shipment_save( $shipment_id, $data ){
    if( isset( $data['registered_shipper'] ) 
        && (int)$data['registered_shipper'] 
        && ( can_wpcfe_add_shipment() || wpcfe_can_edit_fields() )
    ){
        
        if( can_wpcfe_assign_client() ){
            $old_client = get_post_meta( $shipment_id, 'registered_shipper', true );
            if( $old_client != (int)$data['registered_shipper'] && wpc_can_send_email_client() ){
                wpcargo_assign_shipment_email( $shipment_id, (int)$data['registered_shipper'], __('Client', 'wpcargo-frontend-manager' ) );
            }
        }
        update_post_meta( $shipment_id, 'registered_shipper', (int)$data['registered_shipper'] );
    }
    if( isset( $data['registered_employee'] ) && (int)$data['registered_employee'] && in_array( 'administrator', wpcfe_current_user_role() ) ){
        $old_employee = get_post_meta( $shipment_id, 'wpcargo_employee', true );
        update_post_meta( $shipment_id, 'wpcargo_employee', (int)$data['registered_employee'] );
        // check if the employee is changed Send email notification
        if( $old_employee != (int)$data['registered_employee'] && wpc_can_send_email_employee() ){
            wpcargo_assign_shipment_email( $shipment_id, (int)$data['registered_employee'], __('Employee', 'wpcargo-frontend-manager' ) );
        }   
    }
    if( isset( $data['registered_agent'] ) && (int)$data['registered_agent'] && can_wpcfe_assign_agent() ){
        $old_agent = get_post_meta( $shipment_id, 'agent_fields', true );
        update_post_meta( $shipment_id, 'agent_fields', (int)$data['registered_agent'] );
        // check if the agent is changed Send email notification
        if( $old_agent != (int)$data['registered_agent'] && wpc_can_send_email_agent() ){
            wpcargo_assign_shipment_email( $shipment_id, (int)$data['registered_agent'], __('Agent', 'wpcargo-frontend-manager' ) );
        }
    }
    do_action( 'wpcfe_assigned_shipment_save', $shipment_id, $data );
}
// Multiple Package
function wpcfe_shipment_multipackage_template( $shipment_id ){

    $wpcargo_settings = !empty( get_option('wpc_mp_settings') ) ? get_option('wpc_mp_settings') : array();
    if( !array_key_exists( 'wpc_mp_enable_admin', $wpcargo_settings ) ){
        return false;
    }
	$user_roles = wpcfe_current_user_role();
	if( !( in_array( 'cargo_agent', (array)$user_roles ) ) && !( in_array( 'wpcargo_driver', (array)$user_roles ) ) ){
		$shipment       = new stdClass();
		$shipment->ID   = $shipment_id;
		$template = wpcfe_include_template( 'multiple-package.tpl' );
		require_once( $template );
	}
}
// Shipment History
function wpcfe_shipment_history_table_template( $shipment_id ){
	global $wpdb, $wpcargo;
	$current_user 			= wp_get_current_user();
	$gen_settings 			= $wpcargo->settings;
	$edit_history_role 		= ( array_key_exists( 'wpcargo_edit_history_role', $gen_settings ) ) ? $gen_settings['wpcargo_edit_history_role'] : array();
	$role_intersected 		= array_intersect( $current_user->roles, $edit_history_role );
	$shipment       = new stdClass();
	$shipment->ID   = $shipment_id;
    $shipments = maybe_unserialize( get_post_meta( $shipment->ID, 'wpcargo_shipments_update', true ) );
	$template = wpcfe_include_template( 'shipment-history.tpl' );
	require_once( $template );
}
// Save Multiple Package data
function wpcfe_shipment_multipackage_save( $post_id, $data ){
   $packages = array_key_exists( 'wpc-multiple-package', $data ) ? maybe_serialize( $data['wpc-multiple-package'] ) : maybe_serialize( array() );
   update_post_meta( $post_id, 'wpc-multiple-package', $packages );
}
// Save shipment Status
function wpcfe_shipment_status_save( $post_id, $data ){
    // Save Shipment Type
    $shipment_type = get_post_meta( $post_id, '__shipment_type', true );
    if( empty( $shipment_type ) ){
        update_post_meta( $post_id, '__shipment_type', 'wpcargo_default' );
    }
    //  Save the shipment Status
    if( isset( $data['status'] ) &&  $data['status'] != '' ){
        return;
    }
    if( 
        isset( $data['wpcfe_add_form_fields'] ) 
        && wp_verify_nonce( $data['wpcfe_add_form_fields'], 'wpcfe_add_action' ) 
        && can_wpcfe_add_shipment()
    ){
        $__default_status = wpcfe_default_status();
        update_post_meta( $post_id, 'wpcargo_status', sanitize_text_field( $__default_status ) );
        wpcargo_send_email_notificatio( $post_id, sanitize_text_field( $__default_status ) );
    }
}
// Shipment history
function wpcfe_shipment_history_template( $shipment_id ){
    $current_user 			= wp_get_current_user();
    $user_roles             = $current_user->roles;
    if( in_array( 'wpcargo_client', $user_roles ) ){
        return false;
    }
    if( empty( array_intersect( $user_roles, wpcfe_add_shipment_role() ) ) ){
        return false;
    }
    $shipment       = new stdClass();
    $shipment->ID   = $shipment_id;
	$template = wpcfe_include_template( 'history.tpl' );
	require_once( $template );
}
// Save Shipment history
function wpcfe_save_shipment_history( $post_id, $data ) {
	global $wpdb, $wpcargo;
	$current_user 			= wp_get_current_user();
	$gen_settings 			= $wpcargo->settings;
	$edit_history_role 		= ( array_key_exists( 'wpcargo_edit_history_role', $gen_settings ) ) ? $gen_settings['wpcargo_edit_history_role'] : array();
    $role_intersected 		= array_intersect( $current_user->roles, $edit_history_role );
    $old_status             = get_post_meta($post_id, 'wpcargo_status', true);
    
	
	$user_id        = get_current_user_id();
    $full_name      = $wpcargo->user_fullname( $user_id );
	
	if( $role_intersected ){
        $history = array();
        $shipments_update = array();
        if( isset( $data['wpcargo_shipments_update'] ) ){
            $shipments_update = $data['wpcargo_shipments_update'];
        }   
        if( !empty( $shipments_update ) ){
            foreach ($shipments_update as $h_record ) {
                if( array_key_exists( 'updated-name', $h_record ) && empty($h_record['updated-name']) ){
                    $h_record['updated-name'] = $full_name;
                }
                $history[] = $h_record;
            }
        }
        
		if( isset( $data['status'] ) &&  $data['status'] != '' ){
            wpcfe_save_report( $post_id, $old_status, sanitize_text_field( $data['status'] ) );
            update_post_meta( $post_id, 'wpcargo_status', sanitize_text_field( $data['status'] ) );
			update_post_meta( $post_id, 'location', sanitize_text_field( $data['location'] ) );
			$new_history = array();
			foreach( wpcargo_history_fields() as $history_name => $history_fields ){
				if( $history_name != 'updated-name' ){
                    $value = array_key_exists( $history_name, $data ) ? sanitize_text_field( $data[$history_name] ) : '' ;
					$new_history[$history_name] = $value;
				}
			}
			$new_history['updated-name'] = $full_name;
			$history[] = $new_history;
			if( $data['status'] != $old_status ){
                wpcargo_send_email_notificatio( $post_id, $data['status'] );
                do_action( 'wpc_add_sms_shipment_history', $post_id );
			}
		}
		$history = maybe_serialize( $history );
		update_post_meta( $post_id, 'wpcargo_shipments_update', $history );
	}else{
		if( isset( $data['status'] ) &&  $data['status'] != '' ){
            wpcfe_save_report( $post_id, $old_status, sanitize_text_field( $data['status'] ) );
            update_post_meta( $post_id, 'wpcargo_status', sanitize_text_field( $data['status'] ) );
			update_post_meta( $post_id, 'location', sanitize_text_field( $data['location'] ) );
			$history        = get_post_meta( $post_id, 'wpcargo_shipments_update', true ) ? maybe_unserialize( get_post_meta( $post_id, 'wpcargo_shipments_update', true ) ) : array() ;
			$new_history = array();
			foreach( wpcargo_history_fields() as $history_name => $history_fields ){
				if( $history_name != 'updated-name' ){
                    $value = array_key_exists( $history_name, $data ) ? sanitize_text_field( $data[$history_name] ) : '' ;
					$new_history[$history_name] = $value;
				}
			}
			$new_history['updated-name'] = $full_name;
			$history[] = $new_history;
			$history = maybe_serialize( $history );
			update_post_meta( $post_id, 'wpcargo_shipments_update', $history );
			if( $data['status'] != $old_status ){
				wpcargo_send_email_notificatio( $post_id, $data['status'] );
			}
		}
	}
}
function wpcfe_update_shipment_action( $shipment_id, $page_url ){
    return '<a href="'.$page_url.'?wpcfe=update&id='.$shipment_id.'" title="'.__('Update', 'wpcargo-frontend-manager').'"><i class="fa fa-edit text-info"></i></a>';
}
function wpcfe_waybill_template( $shipment_id ){
	global $WPCCF_Fields, $wpcargo;
	$template = wpcfe_include_template( 'waybill.tpl' );
    return $template;
}
// User registration hook
function wpcfe_user_registration_success( $user_id, $data ){
    global $WPCCF_Fields;
    $shipper_fields 			= $WPCCF_Fields->get_field_key('shipper_info');
    if( !empty( $shipper_fields ) && post_type_exists( 'wpc_address_book' ) ){
        $insert_address = array(
            'post_status'   => 'publish',
            'post_type'		=> 'wpc_address_book',
            'post_author'   => $user_id,
        );   
        // Insert the Address into the database.
        $addressID = wp_insert_post( $insert_address );  
        if( !$addressID ){
            return false;
        }
        update_post_meta( $addressID, 'book', 'shipper' );
        foreach( $shipper_fields as $field ){
            $assigned_fields = get_option( 'wpcfe_regmap_'.trim($field['field_key']) );
            if( !$assigned_fields ){
                continue;
            }
            if( !array_key_exists( $assigned_fields, $data ) ){
                continue;
            }
            update_post_meta( $addressID, trim($field['field_key']), sanitize_text_field( $data[$assigned_fields] ) );
        }
    }
}
function wpcfe_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    $user_login = stripslashes( $user->user_login );
    $user_email = stripslashes( $user->user_email );
    $headers    = array('Content-Type: text/html; charset=UTF-8');
    $headers[]  = __('From: ', 'wpcargo-frontend-manager' ) . get_bloginfo('name') .' <admin@no-reply.com>';
    $login_url  = wp_login_url();
    $message  = "<p>".__( 'Hi there,', 'wpcargo-frontend-manager' ) . "</p>";
    $message .= "<p>".sprintf( __( "Welcome to %s! Here's how to log in:", 'wpcargo-frontend-manager' ), get_option('blogname') ) . "</p>";
    $message .= "<p>".wp_login_url() . "</p>";
    $message .= "<p>".sprintf( __('Username: %s', 'wpcargo-frontend-manager'), $user_login ) . "</p>";
    $message .= "<p>".sprintf( __('Email: %s', 'wpcargo-frontend-manager'), $user_email ) . "</p>";
    $message .= "<p>".__( 'Password: The one you entered in the registration form. (For security reason, we save encripted password)', 'wpcargo-frontend-manager' ) . "</p>";
    $message .= "<p>".sprintf( __('If you have any problems, please contact me at %s.', 'wpcargo-frontend-manager'), get_option('admin_email') ) . "</p>";
    $message .= "<p>".__( 'Thank you for trusting us!', 'wpcargo-frontend-manager' ). "</p>";
 
    $wp_new_user_notification_email['subject'] = sprintf( '[%s] Your credentials.', $blogname );
    $wp_new_user_notification_email['headers'] = $headers;
    $wp_new_user_notification_email['message'] = $message;
 
    return $wp_new_user_notification_email;
}
function wpcfe_wp_new_user_notification_email_admin( $wp_new_user_notification_email, $user, $blogname ) {
    $user_login = stripslashes( $user->user_login );
    $user_email = stripslashes( $user->user_email );
    $headers    = array('Content-Type: text/html; charset=UTF-8');
    $headers[]  = __('From: ', 'wpcargo-frontend-manager' ) . get_bloginfo('name') .' <admin@no-reply.com>';
    $login_url  = wp_login_url();
    $message  = "<p>".__( 'Hi Admin,', 'wpcargo-frontend-manager' ) . "</p>";
    $message .= "<p>".sprintf( __( "New user registration on your site %s:", 'wpcargo-frontend-manager' ), get_option('blogname') ) . "</p>";
    $message .= "<p>".sprintf( __('Username: %s', 'wpcargo-frontend-manager'), $user_login ) . "</p>";
    $message .= "<p>".sprintf( __('Email: %s', 'wpcargo-frontend-manager'), $user_email ) . "</p>";
 
    $wp_new_user_notification_email['subject'] = sprintf( '[%s] New Registered User.', $blogname );
    $wp_new_user_notification_email['headers'] = $headers;
    $wp_new_user_notification_email['message'] = $message;
 
    return $wp_new_user_notification_email;
}
add_action( 'woocommerce_thankyou', 'wpcfe_woocommerce_thankyou' );
function wpcfe_woocommerce_thankyou( $order_id ){
    if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) && !is_user_logged_in() ) {
        $wpcfe_print_options = wpcfe_print_options();
        $wpcfe_print_checkout = get_option('wpcfe_checkout_print') ? : array();
        $order = wc_get_order( $order_id );
        $items = $order->get_items(); 
        foreach( $items as $item ){
            $shipment_title = $item['Shipment']; 
        }
        $shipment_id = wpcfe_shipment_id( $shipment_title );           
        if( !empty( $wpcfe_print_options ) ): ?>
            <div class="text-center print-shipment">
                <?php foreach( $wpcfe_print_options as $print_key => $print_label ): ?>
                    <?php if( in_array( $print_key, $wpcfe_print_checkout ) ): ?>
                        <a class="shipment-checkout print-<?php echo $print_key; ?> py-1 wpcargo-btn wpcargo-btn-primary" data-id="<?php echo $shipment_id; ?>" data-type="<?php echo $print_key; ?>" href="#">
                            <?php 
                                echo sprintf(
                                    '%s %s',
                                    esc_html__('Print', 'wpcargo-frontend-manager'),
                                    $print_label
                                );
                            ?>
                        </a>
                    <?php endif; ?>
                <?php endforeach; ?>
            </div>
        <?php endif;
    }
}
add_action( 'wp_head', 'wpcfe_user_sort' );
function wpcfe_user_sort(){
	$wpcfesort_list = array( 10, 25, 50, 100 );
	if( isset( $_GET['wpcfesort'] ) && in_array( $_GET['wpcfesort'], $wpcfesort_list ) ){
		update_user_meta( get_current_user_id(), 'user_wpcfesort', $_GET['wpcfesort'] );
	}
}