<?php
namespace Elex\MultiVendor;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

use Elex\MultiVendor\Controller\AdminProductController;
use Elex\MultiVendor\Controller\AdminDashboard;
use Elex\MultiVendor\Controller\ProductController;
use Elex\MultiVendor\Controller\CategoryController;
use Elex\MultiVendor\Controller\AdminInstallerController;
use Elex\MultiVendor\Controller\AdminPaymentController;
use Elex\MultiVendor\Controller\OrderController;
use Elex\MultiVendor\Controller\PaymentController;
use Elex\MultiVendor\Controller\AdminSettings;
use Elex\MultiVendor\Controller\SiteSettingsController;
use Elex\MultiVendor\Controller\VendorInstallerController;
use Elex\MultiVendor\Controller\VendorDashboardController;
use Elex\MultiVendor\Controller\ManageVendors;
use Elex\MultiVendor\Controller\ReviewController;
use Elex\MultiVendor\Controller\VendorSettings;

use Elex\MultiVendor\Model\SiteSettings;
use Elex\MultiVendor\Model\PreInstaller;
use Elex\MultiVendor\Model\Vendor;
use Elex\MultiVendor\Model\Store;

use Elex\MultiVendor\Controller\CouponController;
use Elex\MultiVendor\Controller\HelpAndSupportController;
use Elex\MultiVendor\Controller\ShippingController;
use Elex\MultiVendor\Model\HelpAndSupport;

use Elex\MultiVendor\TroubleShoot\Elex_Mv_Review_Components;

use WP_Query;

class ElexMultiVendor {


	const VERSION = '2.0.3';

	public $plugin_basename;
	public $main_slug;

	public $store_id;

	public function with_basename( $basename ) {
		$this->plugin_basename = $basename;

		return $this;
	}

	public function boot() {
		$this->register_hooks();
		$this->register_custom_post_status();
		$this->include_review_component();
	}


	public function include_review_component() {
		if ( ! function_exists( 'get_plugin_data' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php' ;
		}
		include_once dirname( __DIR__ ) . '/src/review_and_troubleshoot_notify/review-and-troubleshoot-notify-class.php';
		$data                      = get_plugin_data( WP_PLUGIN_DIR . '/' . $this->plugin_basename, false, false );
		$data['name']              = $data['Name'];
		$data['basename']          = $this->plugin_basename;
		$data['rating_url']        = 'https://elextensions.com/plugin/multi-vendor-plugin-for-woocommerce/#reviews';
		$data['documentation_url'] = 'https://elextensions.com/knowledge-base/how-to-set-up-the-elex-woocommerce-multi-vendor-plugin/';
		$data['support_url']       = 'https://support.elextensions.com/';

		new Elex_Mv_Review_Components( $data );
	}



	public function register_hooks() {
		add_filter( 'woocommerce_product_review_comment_form_args', array( $this, 'hide_review_form_if_not_purchased' ) );

		add_action( 'woocommerce_admin_process_product_object', array( $this, 'elex_mv_custom_new_product_action' ) );

		add_action( 'activated_plugin', array( $this, 'admin_page_setup_redirect_for_elex_mv' ) );

		add_action( 'admin_menu', array( $this, 'Elex_Mv_Add_menu' ) );

	
		

		add_action( 'init', array( $this, 'give_prev_orders_store_value' ) );


		add_action( 'init', array( $this, 'enqueue_scripts' ) );

		add_action( 'init', array( $this, 'enqueue_admin_scripts' ) );

		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_admin' ) );

		add_action( 'init', array( $this, 'register_routes' ) );

		

		add_action( 'woocommerce_init', array( $this, 'elex_mv_shipping_methods_add_extra_field' ) );

		// add_action('admin_footer', array( $this, 'wc_custom_field_js' ) );

		add_action( 'init', array( $this, 'vendor_capabilities_initialize' ) );

		add_action( 'init', array( $this, 'elex_rewrite_rule_for_store_page' ) );

		add_action( 'elex_mv_redirect_to_activation_page', array( $this, 'elex_mv_activation_redirect' ) );

		add_action( 'wp_footer', array( $this, 'create_page_for_vendor_stores' ) );

		add_action( 'wp_logout', array( $this, 'delete_dashboard_link' ) );

		add_action( 'woocommerce_account_content', array( $this, 'show_notice_for_vendor_dash_page' ) );
		add_action( 'init', array( $this, 'show_notice_for_vendor_dash_page_init' ) );

		add_shortcode( 'elex-page-content', array( $this, 'Elex_Mv_Dashboard_page' ) );

		add_shortcode( 'elex-vendor-store-page', array( $this, 'Elex_Mv_Store_Pages' ) );

		add_action( 'admin_head', array( $this, 'remove_admin_onboard_page' ) );
		add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
		add_action( 'init', array( $this, 'elex_vendor_custom_post_type' ) );       
	
		add_action( 'woocommerce_register_form', array( $this, 'wooc_extra_register_fields' ) );    
		add_filter( 'admin_menu', array( $this, 'rename_labels' ) );

		add_action( 'woocommerce_created_customer', array( $this, 'while_user_is_registered' ), 999, 3 );

		add_action( 'user_register', array( $this, 'while_user_is_registered_from_admin_end' ) );

		add_action( 'woocommerce_process_registration_errors', array( $this, 'wooc_validate_extra_register_fields' ), 10, 3 );
		add_action( 'woocommerce_register_form_tag', array( $this, 'elex_mv_enctype_custom_registration_forms' ) );


		add_action( 'wp_footer', array( $this, 'elex_add_script_to_product_page' ) );

		add_action( 'show_user_profile', array( $this, 'output_checklist' ) );
		add_action( 'edit_user_profile', array( $this, 'output_checklist' ) );
		add_action( 'user_new_form', array( $this, 'output_checklist_elex_add_user' ) );
		add_action( 'edit_user_profile_update', array( $this, 'process_checklist' ) );
		add_action( 'personal_options_update', array( $this, 'process_checklist' ) );
		add_action( 'woocommerce_edit_account_form', array( $this, 'add_vendor_checkbox_to_account_details' ) );
	
		add_action( 'woocommerce_save_account_details', array( $this, 'save_vendor_checkbox_value' ) );
		// add_action( 'edit_admin_profile_update', array( $this, 'process_checklist' ) );

		add_action( 'woocommerce_product_options_general_product_data', array( $this, 'add_custom_product_general_field' ) );
		add_action( 'woocommerce_admin_process_product_object', array( $this, 'save_custom_product_general_field' ) );

		add_action( 'woocommerce_after_shop_loop_item_title', array( $this, 'woocommerce_after_shop_loop_item_title_short_description' ), 20 );

		add_action( 'woocommerce_before_add_to_cart_form', array( $this, 'encourage_product_purchase' ), 10 );

		add_filter( 'plugin_action_links_' . plugin_basename( ELEX_MULTI_VENDOR_MAIN_FILE ), array( $this, 'plugin_action_links' ) );
		
		if ( ! empty( filter_input( INPUT_GET, 'page' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) && 'wc-orders' === filter_input( INPUT_GET, 'page' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) {
			add_action( 'woocommerce_order_query_args', array( $this, 'elex_mv_hide_orders_on_shop_order_or_product_page_of_other_vendors_hpos' ) );
		} else {
			add_action( 'pre_get_posts', array( $this, 'elex_mv_hide_orders_on_shop_order_or_product_page_of_other_vendors' ) );
		}
		
		if ( ! empty( filter_input( INPUT_GET, 'page' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) && 'wc-orders' === filter_input( INPUT_GET, 'page' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) {
			add_filter( 'woocommerce_shop_order_list_table_order_count', array( $this, 'elex_mv_count_vendor_orders' ), 10, 2 );
		} else {
			add_filter( 'wp_count_posts', array( $this, 'count_vendor_orders' ), 10, 3 );
		}
		
		add_filter( 'elex_mv_whether_to_generate_package', array( $this, 'check_if_to_generate_package' ), 10, 2 ); 

		add_action( 'before_woocommerce_init', array( $this, 'elex_mv_hpos_update' ) );

		add_action( 'delete_user', array( $this, 'elex_mv_user_delete_function' ), 10, 3 );

		add_filter( 'woocommerce_form_field_multiselect', array( $this, 'custom_multiselect_handler' ) );

	
		add_action( 'woocommerce_edit_account_form_tag', array( $this, 'add_multipart_to_woocommerce_edit_account_form_tag' ) );

		/**
		 * Exclude rejected products from shop query.
		 *
		 * @since 1.1.4
		 */
		add_action( 'woocommerce_product_query', array( $this, 'exclude_rejected_products_from_shop' ) );
	}

	public function save_vendor_checkbox_value( $customer_id ) {

		// Verify the nonce against the default WooCommerce action 'save_account_details'.
		if ( ! isset( $_POST['save-account-details-nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['save-account-details-nonce'] ) ), 'save_account_details' ) ) {
        	return; 
    	}
		
		// Check if the user has permission to edit this profile
		// vendor_settings_manage is capability is for all vendors.
		if ( ! current_user_can( 'vendor_settings_manage', $customer_id ) ) {
			return;
		}

		$site_settings     = SiteSettings::load();
		$commission_amount = $site_settings->get( 'commission_for_each_sale' );
		if ( 'on' === filter_input( INPUT_POST, 'elex_mv_wanabe_a_vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) {
			update_user_meta( $customer_id, 'elex_mv_wanabe_a_vendor', sanitize_text_field( filter_input( INPUT_POST, 'elex_mv_wanabe_a_vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
			// Assign "vendor" role to the user
			$user = new \WP_User( $customer_id );
			$user->add_role( 'elex_mv_vendor' );
		} else {
			delete_user_meta( $customer_id, 'elex_mv_wanabe_a_vendor' );
	
			// Remove "vendor" role from the user
			$user = new \WP_User( $customer_id );
			$user->remove_role( 'elex_mv_vendor' );
		}
		$new_vendor_array = array();
		$current_user     = get_user_by( 'ID', $customer_id );

		$vendor_settings = Vendor::load( $current_user );
	
		// phpcs:disable WordPress.Security.NonceVerification.Missing
		if ( isset( $_POST['save-account-details-nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['save-account-details-nonce'] ) ), 'save_account_details' ) ) {
			$new_vendor_array['image_name'] = isset( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['name'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['name'] ) : '';
			$new_vendor_array['image_path'] = isset( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['tmp_name'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['tmp_name'] ) : '';
			$new_vendor_array['image_type'] = isset( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['type'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['type'] ) : '';
		}
		// phpcs:enable WordPress.Security.NonceVerification.Missing
	
		if ( $vendor_settings->has_store() ) {
			$store = $vendor_settings->get_store();
		} else {
			$store = new Store();
		}
		$store->set_vendor( $vendor_settings );

	

		if ( isset( $new_vendor_array['image_name'] ) ) {
			$store->set_image( $new_vendor_array );
		}
		$store->set_user_id( $customer_id );
		$store->set_user_name( $current_user->display_name );

		if ( ! empty( $store->get_commission() ) ) {
			$store->set_commission( sanitize_text_field( $store->get_commission() ) );
		} else {
			$store->set_commission( sanitize_text_field( $commission_amount ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'store_url', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_title( sanitize_text_field( filter_input( INPUT_POST, 'store_url', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'account_email', FILTER_SANITIZE_EMAIL ) ) ) {
			$store->set_email( sanitize_text_field( filter_input( INPUT_POST, 'account_email', FILTER_SANITIZE_EMAIL ) ) );
		}        
		if ( ! empty( filter_input( INPUT_POST, 'store_full_addr1', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_address1( sanitize_text_field( filter_input( INPUT_POST, 'store_full_addr1', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'store_full_addr2', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_address2( sanitize_text_field( filter_input( INPUT_POST, 'store_full_addr2', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'city', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_city( sanitize_text_field( filter_input( INPUT_POST, 'city', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'state', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_state( sanitize_text_field( filter_input( INPUT_POST, 'state', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'postal_code', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_postal_code( sanitize_text_field( filter_input( INPUT_POST, 'postal_code', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'country', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_country( filter_input( INPUT_POST, 'country', FILTER_SANITIZE_SPECIAL_CHARS ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'phone_number', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_phone_number( filter_input( INPUT_POST, 'phone_number', FILTER_SANITIZE_SPECIAL_CHARS ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'dial_code', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_dial_code( filter_input( INPUT_POST, 'dial_code', FILTER_SANITIZE_SPECIAL_CHARS ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'country_iso', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_country_iso( filter_input( INPUT_POST, 'country_iso', FILTER_SANITIZE_SPECIAL_CHARS ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'paypal_email', FILTER_SANITIZE_EMAIL ) ) ) {
			$store->set_paypal_email( sanitize_text_field( filter_input( INPUT_POST, 'paypal_email', FILTER_SANITIZE_EMAIL ) ) );
		}     
		
		$site_settings = get_option( 'elex_mv_admin_installation_params', false );
		if ( empty( $site_settings['approve_vendor_onboard'] ) || '' === $site_settings['approve_vendor_onboard'] ) {
			$store->set_status( 'publish' );
		} elseif ( $store->get_status() !== 'publish' ) {
			$store->set_status( 'pending' );
		}

		if ( ! empty( filter_input( INPUT_POST, 'elex_mv_wanabe_a_vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ) {
			$store->create();
			if ( ! empty( $site_settings['approve_vendor_onboard'] ) || 'on' === $site_settings['approve_vendor_onboard'] ) {

				$display_name = $current_user->display_name;
				$email        = $current_user->user_email;
				$user_login   = $current_user->user_login;
				
		  
				$subject = ' Welcome to ' . get_bloginfo( 'name' ) . ' - Your Journey Starts Here!';
				$body    = 'Dear ' . $display_name . ',<br>
	
				We are delighted to welcome you to ' . $store->get_title() . ", where your journey as a seller begins! 🚀 <br>

				Congratulations on successfully onboarding as a vendor with us. We are thrilled to have you as part of our vibrant and growing community of sellers. With your dedication and unique products, we believe you will add tremendous value to our platform.<br><br>
				
				We have equipped you with a powerful Vendor Dashboard, where you can effortlessly manage every aspect of your business. Your dashboard includes the following essential features:<br><br>
				
				1. **Order Management:** Keep track of incoming orders, process them efficiently and ensure timely deliveries to your customers.<br>
				
				2. **Product & Inventory Management:** Easily list your products, update product details, and manage your inventory levels to prevent overselling.<br>
				
				3. **Product Reviews:** Monitor and respond to customer reviews for your products, fostering trust and improving your products based on feedback.<br>
				
				4. **Shipping Options:** Explore various shipping methods and set your preferences to offer flexible shipping solutions to your customers.<br>
				
				5. **Settlement History:** Access historical settlement data to keep tabs on your earnings and financial transactions.<br><br>
				
				This is just the beginning of an exciting journey, and we are confident that your dedication and passion will drive your success on Admin Site. We can't wait to see your business thrive and grow.<br>
				
				Thank you once again for choosing us as your partner. We are here to support you in achieving your goals and reaching new heights.<br>
				
				Welcome aboard, and here's to a prosperous journey ahead!
				
				Best regards,<br>";
		  
				$headers   = array();
				$headers[] = 'Content-type: text/html; charset=utf-8';
				$headers[] = 'From:' . get_bloginfo( 'admin_email' );

				wp_mail( $email, $subject, $body, $headers );

			}
		}
	}

	public function add_multipart_to_woocommerce_edit_account_form_tag() {
		echo 'enctype="multipart/form-data"';
	} 

	public function add_vendor_checkbox_to_account_details() { 
		$user     = wp_get_current_user();      
		$vendor   = Vendor::load( $user ); 
		$store_id = '';
		$status   = '';
		if ( $vendor->has_store() ) {
			$store    = $vendor->get_store();
			$store_id = $store->get_id();
			$status   = $store->get_status();
		} 

		if ( get_option( 'elex_mv_admin_onboarding_done' ) ) { 
			?>


		
		<?php 
			if ( empty( $status ) || 'publish' !== $status ) {
				?>
		<input type="checkbox" name="elex_mv_wanabe_a_vendor" id="elex_wanabe_a_vendor" data-attr-status="<?php echo esc_attr( $status ); ?>"
		<?php 
				if ( ! empty( get_user_meta( $user->ID, 'elex_mv_wanabe_a_vendor' , true ) ) ) {
					echo esc_html( 'checked' );
				}
				?>
		>
			<label for="elex_wanabe_a_vendor" class="label_wanna_be_a_vendor">
			<span><?php echo esc_html( 'I want to be a Vendor', 'elex-multi-vendor-for-woocommerce' ); ?></span>
		</label>
			<?php
			}
			?>
		
		
		<?php } ?>
		<div class="elex_admin_login_details_cl" id="elex_admin_login_details_id" hidden>
		<p class="form-row form-row-wide">
				<div class="elex-multi-vendor-for-woocommerce-wrap elex-multi-vendor-for-woocommerce-wrap-front" style="min-height: auto !important;">

				
				<label>Store Profile Image</label>
				<div class="d-flex align-items-center gap-4">
					<img id="elexPreviewImageUploadLogin" src="<?php echo ( ! empty( $store_id ) && has_post_thumbnail( $store_id ) ) ? esc_url( get_the_post_thumbnail_url( $store_id ) ) : esc_url( ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'images/default.jpg' ); ?>" alt="" width="100px">
						<div class="position-relative">
						<button class="btn fw-bold text-info" id="elex-multi-vendor-for-woocommerce-upload-image-login-btn">Upload</button>
						<input type="file" id="elex-multi-vendor-for-woocommerce-filetag-login" name="elex-multi-vendor-for-woocommerce-filetag-login" accept="image/x-png,image/gif,image/jpeg" class="opacity-0 position-absolute w-100 h-100 top-0 start-0">
						</div>

					
				</div>
				<div  class="elex-multi-vendor-for-woocommerce-prod-img-name-div-login d-none" >
						<div class = "d-flex gap-2 align-items-center" >
							<div class="elex-multi-vendor-for-woocommerce-prod-img-name-login"></div>
								<button type="button" class=" btn btn-sm rounded-circle p-1 elex-multi-vendor-for-woocommerce-product-form-remove-image-login" ><svg xmlns="http://www.w3.org/2000/svg" height="1.5em" viewBox="0 0 512 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M256 48a208 208 0 1 1 0 416 208 208 0 1 1 0-416zm0 464A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM175 175c-9.4 9.4-9.4 24.6 0 33.9l47 47-47 47c-9.4 9.4-9.4 24.6 0 33.9s24.6 9.4 33.9 0l47-47 47 47c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-47-47 47-47c9.4-9.4 9.4-24.6 0-33.9s-24.6-9.4-33.9 0l-47 47-47-47c-9.4-9.4-24.6-9.4-33.9 0z"/></svg></button>
							</div>
						</div>
				</div>
			</p>

			<p class="form-row form-row-wide">
				<label for="elex_store_name"><?php echo esc_html( 'Store name', 'elex-multi-vendor-for-woocommerce' ); ?> <span class="text-danger">*</span></label>
				<input type="text" class="input-text" name="store_url" id="elex_store_name" value="<?php echo esc_attr( get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-store-name-vendor', true ) ); ?>" />
			</p>
			<p class="form-row form-row-wide">
				<label for="reg_billing_last_name"><?php echo esc_html( 'Store Address 1', 'elex-multi-vendor-for-woocommerce' ); ?></label>
				<input type="text" class="input-text" name="store_full_addr1" id="elex_store_address1" value="<?php echo esc_attr( get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-address-one-vendor', true ) ); ?>"/>
			</p>
			<p class="form-row form-row-wide">
				<label for="reg_billing_last_name"><?php echo esc_html( 'Store Address 2', 'elex-multi-vendor-for-woocommerce' ); ?></label>
				<input type="text" class="input-text" name="store_full_addr2" id="elex_store_address2" value="<?php echo esc_attr( get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-address-two-vendor', true ) ); ?>"/>
			</p>
			<p class="form-row form-row-wide">
				<label for="reg_billing_last_name"><?php echo esc_html( 'Country', 'elex-multi-vendor-for-woocommerce' ); ?></label>
				<select type="text" class="input-text" name="country" id="elex_store_country"  >
				<option value="">--Select Store Country-- </option>
				<?php 
					$wc_countries     = new \WC_Countries();
					$countries        = $wc_countries->get_countries();
					$selected_country = get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-country-vendor', true );
				
				foreach ( $countries  as $country_code => $country_name ) :
					?>
					<option value="<?php echo esc_html( $country_code ); ?>"  
					<?php 
					if ( ! empty( $country_code ) && ! empty( $selected_country ) && $country_code === $selected_country ) {
						echo 'selected';
					} 
					?>
					><?php echo esc_html( $country_name ); ?></option>
					<?php endforeach ; ?>
				</select>
			</p>
			<?php
				$countries = WC()->countries;
				// Retrieve state name using country code and state code
			if ( ! empty( get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-country-vendor', true ) && ! empty( get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-state-vendor', true ) ) ) ) {
				$state_name = $countries->get_states( get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-country-vendor', true ) )[ get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-state-vendor', true ) ];
			} else {
				$state_name = '';
			}
			?>
			<p class="form-row form-row-wide">
				<label for="reg_billing_last_name"><?php echo esc_html( 'State / County', 'elex-multi-vendor-for-woocommerce' ); ?></label>
				<select type="text"style="width:100%;" class="input-text" name="state" id="elex_store_state" data-val="<?php echo esc_attr( get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-state-vendor', true ) ); ?>" data-state-name="<?php echo esc_attr( $state_name ); ?>"></select>
			</p>
			<p class="form-row form-row-wide">
				<label for="reg_billing_last_name"><?php echo esc_html( 'City', 'elex-multi-vendor-for-woocommerce' ); ?></label>
				<input type="text" class="input-text" name="city" id="elex_store_city" value="<?php echo esc_attr( get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-city-vendor', true ) ); ?>" />
			</p>
			<p class="form-row form-row-wide">
				<label for="reg_billing_last_name"><?php echo esc_html( 'Postcode / Zip', 'elex-multi-vendor-for-woocommerce' ); ?></label>
				<input type="text" class="input-text" name="postal_code" id="elex_store_postal" value="<?php echo esc_attr( get_user_meta( $user->ID, 'elex-multi-vendor-for-woocommerce-post-code-vendor', true ) ); ?>" />
			</p>


			<p class="form-row form-row-wide">
				<label for="reg_billing_phone"><?php echo esc_html( 'Phone', 'elex-multi-vendor-for-woocommerce' ); ?></label>
				<input type="hidden" class="input-text" name="dial_code" id="elex_store_dial_code" value="<?php echo ( ! empty( $store_id ) && ! empty( get_post_meta( $store_id, 'elex_mv_dial_code', true ) ) ) ? esc_attr( get_post_meta( $store_id, 'elex_mv_dial_code', true ) ) : '1'; ?>" />
				<input type="hidden" class="input-text" name="country_iso" id="elex_store_country_iso" value="<?php echo ( ! empty( $store_id ) && ! empty( get_post_meta( $store_id, 'elex_mv_country_iso', true ) ) ) ? esc_attr( get_post_meta( $store_id, 'elex_mv_country_iso', true ) ) : 'us'; ?>"/>
				<input type="text" class="input-text" name="phone_number" id="elex_store_phone" value="<?php echo ( ! empty( $store_id ) ) ? esc_attr( get_post_meta( $store_id, 'elex_mv_phone_number', true ) ) : ''; ?>" />
			</p>
			<p class="form-row form-row-wide">
				<label for="reg_billing_phone"><?php echo esc_html( 'Paypal Email Id(Optional)', 'elex-multi-vendor-for-woocommerce' ); ?></label>
				<input type="text" class="input-text" name="paypal_email" id="elex_store_paypal_email" value="<?php echo ! empty( $store_id ) ? esc_attr( get_post_meta( $store_id, 'elex_mv_paypal_email', true ) ) : ''; ?>"  />
			</p>
		</div>
		<div class="clear"></div>
		
		<?php
	}

	public function custom_multiselect_handler( $field, $key, $args, $value ) {
	
		if ( ! empty( $args['options'] ) ) {
			
			$field = '<p class="form-row ' . implode( ' ', $args['class'] ) . '" id="' . $key . '_field">
				<label for="' . $key . '" class="' . implode( ' ', $args['label_class'] ) . '">' . $args['label'] . $required . '</label>
				<select name="' . $key . '" id="' . $key . '" class="select" multiple="multiple">
				</select>
			</p>' . $args['after'];
		}
	
		return $field;
	}

	public function elex_mv_shipping_methods_add_extra_field() {
		
		$shipping_methods = WC()->shipping->get_shipping_methods();
		foreach ( $shipping_methods as $shipping_method ) {
			add_filter( 'woocommerce_shipping_instance_form_fields_' . $shipping_method->id, array( $this, 'elex_add_extra_field_assigned_vendor' ) );
		}
	}

	public function elex_add_extra_field_assigned_vendor( $settings ) {

	
		$vendor_stores     = Store::get_stores();
		$vendor_stores     = json_decode( json_encode( $vendor_stores ), true );
		$vendor_stores_arr = array( '' => '-- Select Vendor --' );
		foreach ( $vendor_stores as $obj ) {
			$vendor_stores_arr[ $obj['ID'] ] = $obj['post_title'] ;
		}
	
		
		$sett_array1 = array_slice( $settings, 0, 1, true );
		$sett_array2 = array_slice( $settings, 1 );

		$sett_array1['elex_extra_field_assigned_vendor'] = array(
			'title'       => esc_html__( 'Assigned Vendor', 'elex-multi-vendor-for-woocommerce' ),
			'type'        => 'multiselect',
			'placeholder' => esc_html__( 'Please Assign Vendor', 'elex-multi-vendor-for-woocommerce' ),
			'description' => '',
			'class'       => 'shipping_method_for_vendors_assignment',
			'default'     => '',
			'options'     => $vendor_stores_arr,
		);
		$sett_array                                      = array_merge( $sett_array1, $sett_array2 );

		return $sett_array;
	}

	
	public function give_prev_orders_store_value() {


		if ( empty( get_option( 'elex_mv_admin_orders_value_set' ) ) ) {
			$store = '';
	
			$current_user = get_current_user_id();


			$user_meta = get_userdata( $current_user );

			$vendor_settings = Vendor::load( $current_user );
		

			if ( empty( $user_meta ) ) {
				return;
			}
			$user_roles = $user_meta->roles;

			if ( $vendor_settings->has_store() && in_array( 'elex_mv_vendor', $user_roles ) ) {

				$store          = $vendor_settings->get_store();
				$this->store_id = $store->get_id();
			}
	

			if ( in_array( 'administrator', $user_roles ) && in_array( 'elex_mv_vendor', $user_roles ) ) {

				$page     = 1;
				$per_page = 10;

				do {
					// Get the products with pagination
					$products = wc_get_products(
						array(
							'limit' => $per_page,
							'page'  => $page,
						)
					);
				
					if ( $products ) {
						// Process the products on this page
						foreach ( $products as $product ) {
							// Your product processing code here
							// Example: Get product ID
							$store_id = $product->get_meta_data( 'elex_mv_store_id', true );
							if ( empty( $store_id ) ) {
								$product->update_meta_data( 'elex_mv_store_id', $store->get_id() );

							}
							$product->save();
						}
				
						// Increment the page to retrieve the next set of products
						++$page;
					} else {
						// No more products to retrieve, exit the loop
						break;
					}               
				} while ( true );
		
				update_option( 'elex_mv_admin_orders_value_set', 'true' ); 
				update_option( 'elex_mv_admin_detail_set_as_vendor_date', gmdate( get_option( 'date_format' ) ) ); 

			} else {
				return;
			}
		}
	}

	public function elex_mv_user_delete_function( $user_id, $reassign, $user_obj ) {

		$vendor = Vendor::load( $user_obj );

		if ( isset( $vendor ) && $vendor->has_store() ) {
			$store = $vendor->get_store();
			wp_delete_post( $store->get_id() ); 
		}
	}

	public function elex_mv_hpos_update() {
		if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', ELEX_MULTI_VENDOR_PLUGIN_PATH, true );
		}
	} 


	
	public function check_if_to_generate_package( $generate_package, $order ) {
		
		$has_suborder = $order->get_meta( 'elex_mv_has_sub_order' );
		if ( $has_suborder ) {
			$generate_package = false;
		}
		return $generate_package;
	}

	public function elex_mv_count_vendor_orders( $count, $status ) {
		
		$current_user = wp_get_current_user();
		$user_id      = $current_user->ID;
		$user_meta    = get_userdata( $user_id );
		if ( empty( $user_meta ) ) {
			return;
		}
		$user_roles = $user_meta->roles;

		if ( current_user_can( 'manage_options' ) && ! in_array( 'administrator', $user_roles ) ) {

		$vendor = Vendor::load( wp_get_current_user() );
		$store  = $vendor->get_store();
			if ( ! empty( $store ) ) {
				$vendor_store_id = $store->get_id();
				foreach ( $status as $key => $val ) {

					if ( ! empty( $vendor_store_id ) ) {
						$count += $this->get_vendor_order_count( $vendor_store_id, $val );
					}
				}
			}   
		}
		return $count;
	}
	public function count_vendor_orders( $counts, $type, $term ) {
		
		$current_user = wp_get_current_user();
		$user_id      = $current_user->ID;
		$user_meta    = get_userdata( $user_id );
		if ( empty( $user_meta ) ) {
			return;
		}
		$user_roles = $user_meta->roles;

		if ( 'shop_order' === $type && current_user_can( 'manage_options' ) && ! in_array( 'administrator', $user_roles ) ) {
			// Check if the user is an admin in the admin dashboard.

			$vendor = Vendor::load( wp_get_current_user() );
			$store  = $vendor->get_store();
			if ( ! empty( $store ) ) {
				$vendor_store_id = $store->get_id();
				foreach ( $counts as $status => $count ) {
					if ( ! empty( $vendor_store_id ) ) {
						$counts->$status = $this->get_vendor_order_count( $vendor_store_id, $status );
					}
				}           
			}       
		}
		return $counts;
	}

	public function get_vendor_order_count( $vendor_store_id, $status ) {
	
	

		$orders = wc_get_orders(
			array(
				'status'     => $status,
				'meta_key'   => 'elex_mv_store_id',
				'meta_value' => $vendor_store_id,
			)
		);
		

		$order_count = count( $orders );

		return $order_count;
	}

	public function elex_mv_hide_orders_on_shop_order_or_product_page_of_other_vendors_hpos( $args ) {
		$current_user = wp_get_current_user();
		$user_id      = $current_user->ID;
		$user_meta    = get_userdata( $user_id );
		if ( empty( $user_meta ) ) {
			return;
		}
		$user_roles = $user_meta->roles;
		if ( ! ( in_array( 'administrator', $user_roles ) ) && $user_id ) {
			$args['meta_query'][] = array(
				'key'     => 'elex_mv_store_id',
				'value'   => $this->store_id,
				'compare' => '=',
			);
		}

		return $args;
	}

	public function elex_mv_hide_orders_on_shop_order_or_product_page_of_other_vendors( $query ) {
		$current_user = wp_get_current_user();
		$user_id      = $current_user->ID;
		$user_meta    = get_userdata( $user_id );
		if ( empty( $user_meta ) ) {
			return;
		}
		$user_roles = $user_meta->roles;
		if ( ! ( in_array( 'administrator', $user_roles ) ) && $query->is_main_query() && $user_id && $query->get( 'post_type' ) === 'shop_order' ) {
			$meta_queries   = array();
			$meta_queries[] = array(
				'key'     => 'elex_mv_store_id',
				'value'   => $this->store_id,
				'compare' => '=',
			);
	

			$meta_relation = 'AND'; // You can use 'AND' or 'OR'.
			
			if ( count( $meta_queries ) > 1 ) {
				$query->set( 'meta_query', array( 'relation' => $meta_relation ) + $meta_queries );
			} elseif ( count( $meta_queries ) === 1 ) {
				$query->set( 'meta_query', $meta_queries );
			}       
		}
	}

	/**
	 * Exclude rejected products from WooCommerce shop query catalog on frontend.
	 *
	 * @param WC_Product_Query $q WooCommerce product query object.
	 *
	 * @since 1.1.4
	 */
	public function exclude_rejected_products_from_shop( $q ) {
		if ( ! is_admin() && $q->is_main_query() ) {
			$post_status = $q->get( 'post_status' );
			if ( empty( $post_status ) ) {
				$post_status = get_post_stati( array( 'public' => true ) );
			}
			$post_status = (array) $post_status;
			$key = array_search( 'reject', $post_status, true );
			if ( false !== $key ) {
				unset( $post_status[ $key ] );
			}
			$q->set( 'post_status', array_values( $post_status ) );
		}
	}

	public static function elex_mv_custom_new_product_action( $product_id ) {

		$vendor = Vendor::load( wp_get_current_user() );
		if ( isset( $vendor ) && $vendor->has_store() ) {
			$store   = $vendor->get_store();
			$product = wc_get_product( $product_id );
			$product->update_meta_data( 'elex_mv_store_id', $store->get_id() );
			$product->save();
		}
	}
	
	
	public function elex_mv_activation_redirect() {
		$site_settings = SiteSettings::load();

		if ( get_option( 'elex_mv_redirect_after_activation_option', false ) ) {
			delete_option( 'elex_mv_redirect_after_activation_option' );

			if ( $site_settings->is_setup_finished( 'is_setup_finished' ) ) {
				wp_safe_redirect( admin_url( '/admin.php?page=elex-multi-vendor-for-woocommerce-dashboard' ) );
				exit();
			}
			wp_safe_redirect( admin_url( '/admin.php?page=elex-multi-vendor-for-woocommerce-setup' ) );
			exit();
		}
	}

	public function wf_get_settings_url() {
			return 'woocommerce_settings';
	}



	public function hide_review_form_if_not_purchased( $comment_form_args ) {

		global $product;
	
		$store_id = '';

		// Get the current user
		$current_user = wp_get_current_user();
	
		$verify_user_review = '';

		$product_id    = $product->get_id();
		$prod_store_id = $product->get_meta( 'elex_mv_store_id', true );
		$vendor        = Vendor::load( $current_user );
		if ( $vendor->has_store() ) {
			$store    = $vendor->get_store();
			$store_id = $store->get_id();
		}   

		if ( $prod_store_id ) {

			$store_post = get_post( $prod_store_id );

	
				
			$verify_user_review = get_post_meta( $prod_store_id, 'elex_mv_verify_user_review', true );

	
			if ( 'on' === $verify_user_review ) {
	
				if ( ( (string) $prod_store_id !== (string) $store_id ) && ! wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {

					$comment_form_args['comment_field'] = '';

					wp_add_inline_style( 'elex-mv-review-hide-form', '.comment-respond { display: none; } .form-submit { display: none; } #submit { display: none; }' );
					wp_print_styles( 'elex-mv-review-hide-form' );

					// Set the 'must_log_in' argument to true
					$comment_form_args['must_log_in'] = true;
				}

				if ( ( (string) $prod_store_id !== (string) $store_id ) && wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {
					echo '';

				} elseif ( ! $current_user->ID ) {


					$comment_form_args['comment_field'] = '';

					wp_add_inline_style( 'elex-mv-review-hide-form', '.comment-respond { display: none; } .form-submit { display: none; } #submit { display: none; }' );
					wp_print_styles( 'elex-mv-review-hide-form' );

					// Set the 'must_log_in' argument to true
					$comment_form_args['must_log_in'] = true;

				}           
			}       
		}

		return $comment_form_args;
	}

	public function plugin_action_links( $links ) {
			$plugin_links = array(
				'<a href="' . admin_url( 'admin.php?page=elex-multi-vendor-for-woocommerce-settings' ) . '">' . __( 'Settings', 'elex-multi-vendor-for-woocommerce' ) . '</a>',

				'<a href="https://elextensions.com/knowledge-base/how-to-set-up-the-elex-woocommerce-multi-vendor-plugin/" target="_blank">' . __( 'Documentation', 'elex-multi-vendor-for-woocommerce' ) . '</a>',
				'<a href="https://elextensions.com/support/" target="_blank">' . __( 'Support', 'elex-multi-vendor-for-woocommerce' ) . '</a>',
			);
			return array_merge( $plugin_links, $links );
	}

	public function elex_rewrite_rule_for_store_page() {

		$admin_params = get_option( 'elex_mv_admin_installation_params', false );
		if ( isset( $admin_params['elex_mv_store_slug'] ) && ! empty( $admin_params['elex_mv_store_slug'] ) ) {
			add_rewrite_rule( $admin_params['elex_mv_store_slug'] . '/([^/]+)/?$', 'index.php?pagename=' . $admin_params['elex_mv_store_slug'] . '&store_title=$matches[1]', 'top' );
		}
		flush_rewrite_rules();
	}

	public function encourage_product_purchase() {
		$site_settings = get_option( 'elex_mv_admin_installation_params', false );

		if ( ! is_product() ) {
			return;
		}
		
			// gets the product object
			global $product;
		if ( ! is_object( $product ) ) {
			$product = wc_get_product( get_the_ID() );
		}
			
			$prod_id   = get_the_ID();
			$author_id = get_post_field( 'post_author', $prod_id );
			// Get the author name using the author ID
			$author_name = get_the_author_meta( 'display_name', $author_id );
			$store_id    = $product->get_meta( 'elex_mv_store_id', true );
			$store_check = false;
		if ( ! empty( $store_id ) ) {
			$store_check = get_post_meta( $store_id , 'elex_mv_show_vendor_detail_on_woocom_product_page', true );
		}
		if ( ! empty( $store_check ) && $store_check ) {

			$store_name = get_post_meta( $store_id, 'elex_mv_title', true ) ? get_post_meta( $store_id, 'elex_mv_title', true ) : $author_name ;
		
			if ( isset( $site_settings['elex_mv_store_slug'] ) && $site_settings['elex_mv_store_slug'] && ! empty( $store_id ) ) {
				
				$url = 'http://';

				if ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) { 
					$url = 'https://';
				}

				$http_host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '' ; 

				printf( 'Sold By: <a href="%1$s/%2$s/%3$s">%3$s</a>', esc_url( $url . $http_host ), esc_html( $site_settings['elex_mv_store_slug'] ), esc_html( $store_name ) );
			} else {
				
				printf( '<div>%1$s: %2$s</div>', esc_html__( 'Sold By', 'elex-multi-vendor-for-woocommerce' ), esc_html( $store_name ) );

			}       
		}
	}

	public function woocommerce_after_shop_loop_item_title_short_description() {
		$site_settings = get_option( 'elex_mv_admin_installation_params', false );
		global $product;
		if ( ! is_object( $product ) ) {
			$product = wc_get_product( get_the_ID() );
		}
		
		$prod_id  = get_the_ID();
		$store_id = $product->get_meta( 'elex_mv_store_id', true );

		$author_id = get_post_field( 'post_author', $prod_id );
		// Get the author name using the author ID
		$author_name = get_the_author_meta( 'display_name', $author_id );
		$store_check = false;
		if ( ! empty( $store_id ) ) {
			$store_check = get_post_meta( $store_id , 'elex_mv_show_vendor_detail_on_woocom_store_page', true );
		}

		if ( ! empty( $store_check ) && $store_check ) {

		$store_name = get_post_meta( $store_id, 'elex_mv_title', true ) ? get_post_meta( $store_id, 'elex_mv_title', true ) : $author_name ;

			if ( isset( $site_settings['elex_mv_store_slug'] ) && $site_settings['elex_mv_store_slug'] && ! empty( $store_id ) ) {
					
			$url = 'http://';

				if ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) { 
					$url = 'https://';
				}
		
			$http_host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '' ; 

			printf( 'Sold By: <a href="%1$s/%2$s/%3$s">%3$s</a>', esc_url( $url . $http_host ), esc_html( $site_settings['elex_mv_store_slug'] ), esc_html( $store_name ) );
			} else {

				printf( '<div>%1$s: %2$s</div>', esc_html__( 'Sold By', 'elex-multi-vendor-for-woocommerce' ), esc_html( $store_name ) );

			}       
		}
	}
	public function create_page_for_vendor_stores() {
		$admin_params = get_option( 'elex_mv_admin_installation_params', false );
		if ( isset( $admin_params['elex_mv_store_slug'] ) && ! empty( $admin_params['elex_mv_store_slug'] ) ) {
			$this->create_elex_mv_page( 'Store List' , $admin_params['elex_mv_store_slug'], '[elex-vendor-store-page]', 'publish' );
		}
	}

	public function elex_mv_get_post_id_from_meta( $meta_key, $meta_value ) {
		global $wpdb;
	
		return $wpdb->get_var(
			$wpdb->prepare(
				"SELECT post_id
				FROM {$wpdb->prefix}postmeta
				WHERE meta_key = %s
				AND meta_value = %s",
				$meta_key,
				$meta_value 
			) 
		);
	}

	public function Elex_Mv_Store_Pages() {

		$site_settings = SiteSettings::load();
		

		$store_title = sanitize_text_field( urldecode( get_query_var( 'store_title' ) ) );

		if ( ! empty( $store_title ) ) {

			$store_id         = $this->elex_mv_get_post_id_from_meta( 'elex_mv_title', $store_title );
			$show_vendor_name = get_post_meta( $store_id, 'elex_mv_show_vendor_detail_on_woocom_store_page', true );
			$current_user     = wp_get_current_user();
			$vendor           = Vendor::load( $current_user );
			if ( $vendor->has_store() && empty( $store_id ) ) {
				$store    = $vendor->get_store();
				$store_id = $store->get_id();
			}  
	
			$catego = ! empty( filter_input( INPUT_GET, 'category' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ? sanitize_text_field( filter_input( INPUT_GET, 'category' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) : '';
	
			$search_term = ! empty( filter_input( INPUT_GET, 'search_term' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ? sanitize_text_field( filter_input( INPUT_GET, 'search_term' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) : '';
	
			$store = get_post( $store_id );
				
			$req_store = Store::load_by_store( $store );
	
			$post_thumbnail_id = get_post_meta( $store_id, 'elex_mv_post_banner_id', true );
		
			$name_to_show_on_product  = get_post_meta( $store_id, 'elex_mv_name_to_show_on_product', true );
			$price_to_show_on_product = get_post_meta( $store_id, 'elex_mv_price_to_show_on_product', true );
			$show_all_categories      = get_post_meta( $store_id, 'elex_mv_show_all_categories', true );
			$banner_image_text        = get_post_meta( $store_id, 'elex_mv_banner_image_text', true );
	
			$thumbnail_url = has_post_thumbnail( $store->ID ) ? esc_url( get_the_post_thumbnail_url( $store->ID ) ) : false;
	
			$args = array(
				'limit'      => 20,
				'order'      => 'DESC',
				'orderby'    => 'date',
				'meta_key'   => 'elex_mv_store_id',
				'meta_value' => $store_id,
				'category'   => $catego,
				's'          => $search_term,
				'status'     => 'publish',
			);
	
			$products_elex_mv = wc_get_products( $args );
	
			$taxonom      = 'product_cat';
			$orderb       = 'id';  
			$show_count   = 0;      // 1 for yes, 0 for no
			$pad_counts   = 0;      // 1 for yes, 0 for no
			$hierarchical = 1;      // 1 for yes, 0 for no  
			$title_li     = '';  
			$empty        = 0;
		   
			$args                 = array(
				'taxonomy'     => $taxonom,
				'orderby'      => $orderb,
				'order'        => 'DESC',
				'show_count'   => $show_count,
				'pad_counts'   => $pad_counts,
				'hierarchical' => $hierarchical,
				'title_li'     => $title_li,
				'hide_empty'   => $empty,
			);
			$categories_of_vendor = get_categories( $args );
			ob_start();
	
			include_once ELEX_MULTI_VENDOR_PLUGIN_PATH . '/templates/store-page/store-page.php';
	
			return ob_get_clean();

		} else {

			$vendor_stores = Store::get_stores();
			$vendor_stores = json_decode( json_encode( $vendor_stores ), true );
			$admin_params  = get_option( 'elex_mv_admin_installation_params', false );

			ob_start();
	
			include_once ELEX_MULTI_VENDOR_PLUGIN_PATH . '/templates/store-page/store-main.php';
	
			return ob_get_clean();

		}
	}

	public function delete_dashboard_link() {

		$posts = get_posts(
			array(
				'post_type'              => 'page',
				'title'                  => 'Vendor Dashboard',
				'post_status'            => 'all',
				'numberposts'            => 1,
				'update_post_term_cache' => false,
				'update_post_meta_cache' => false,           
	
			)
		);
			 
	
		if ( ! empty( $posts ) ) {
			
			wp_delete_post( $posts[0]->ID, true );
		}
	}

	public function show_notice_for_vendor_dash_page() {
		global $current_user; // The WP_User Object
		$store        = '';
		$store_status = '';
		$vendor       = Vendor::load( $current_user );
		if ( $vendor->has_store() ) {
			$store        = $vendor->get_store();
			$store_status = isset( $store ) ? $store->get_status() : '';
		}
	
		$user_meta  = get_userdata( $current_user->ID );
		$user_roles = $user_meta->roles;

		if ( in_array( 'elex_mv_vendor', $user_roles ) && 'publish' === $store_status ) {
			$this->create_elex_mv_page( 'Vendor Dashboard' , 'vendor-dashboard', '[elex-page-content]', 'publish' );
			echo '<div class="woocommerce-message"> <a href=' . esc_url( admin_url() ) . 'vendor-dashboard/?menu_tab=dashboard >' . esc_html__( 'Take me to Vendor Dashboard', 'elex-multi-vendor-for-woocommerce' ) . '</a> </div>';
		} elseif ( in_array( 'elex_mv_vendor', $user_roles ) && 'pending' === $store_status ) {
			$this->hide_elex_mv_page( 'Vendor Dashboard' );
			echo '<div class="woocommerce-message"> <p>' . esc_html__( 'Your vendor status on this site is pending approval from the administrator. Please await confirmation for further details.', 'elex-multi-vendor-for-woocommerce' ) . '</p> </div>';
		} elseif ( in_array( 'elex_mv_vendor', $user_roles ) && 'reject' === $store_status ) {
			$this->hide_elex_mv_page( 'Vendor Dashboard' );
				/* translators: %s: rejection message */
				printf( '<div class="woocommerce-message"> <p>%1$s "%2$s"</p> </div>', esc_html__( 'Your vendor status on this site is rejected from the administrator. Due to this reason:', 'elex-multi-vendor-for-woocommerce' ), esc_html( get_post_meta( $store->get_id(), 'elex_mv_rejection_message', true ) ) );
		}
	}

	public function show_notice_for_vendor_dash_page_init() {
		global $current_user; // The WP_User Object
		$store        = '';
		$store_status = '';

		if ( $current_user ) {

			$vendor = Vendor::load( $current_user );
			if ( $vendor->has_store() ) {
				$store        = $vendor->get_store();
				$store_status = isset( $store ) ? $store->get_status() : '';
			}
		
			$user_meta = get_userdata( $current_user->ID );
			if ( ! empty( $user_meta ) ) {

				$user_roles = $user_meta->roles;

				if ( in_array( 'elex_mv_vendor', $user_roles ) && 'publish' === $store_status ) {
					$this->create_elex_mv_page( 'Vendor Dashboard' , 'vendor-dashboard', '[elex-page-content]', 'publish' );
	
				} elseif ( in_array( 'elex_mv_vendor', $user_roles ) ) {
	
					$this->hide_elex_mv_page( 'Vendor Dashboard' );
				}           
			}       
		}
	}

	public function add_custom_product_general_field() {
		global $post;

		$admin_params = get_option( 'elex_mv_admin_installation_params', false );
		
		$commission      = isset( $admin_params['commission_for_each_sale'] ) ? $admin_params['commission_for_each_sale'] : '';
		$commission_type = isset( $admin_params['commission_type_of_sale'] ) ? $admin_params['commission_type_of_sale'] : '';

		$product = wc_get_product( $post->ID );

		$product_commission_for_admin      = $product->get_meta( 'elex_mv_commission', true );
		$product_commission_type_for_admin = $product->get_meta( 'elex_mv_commission_type', true );



		if ( isset( $product_commission_type_for_admin ) ) {
			$commission_type = $product_commission_type_for_admin ;
		}

		if ( isset( $product_commission_for_admin ) ) {
			$commission = $product_commission_for_admin ;
		}

		

		$store_id   = $product->get_meta( 'elex_mv_store_id', true );
		$store_name = get_post_meta( $store_id, 'elex_mv_title', true );

		// if ( 'simple' === $product->get_type() ) {

			echo '<div class="options_group">';
		woocommerce_wp_select(
			array(
				'id'          => 'elex_vendor_name', // required, will be used as meta_key
				'label'       => __( 'Assigned Vendor', 'elex-multi-vendor-for-woocommerce' ), // Text in the label in the editor.
				'desc_tip'    => 'true',
				'description' => __( 'Assign new Vendor for this product', 'elex-multi-vendor-for-woocommerce' ),
				'options'     => array(),
			)
		);
		woocommerce_wp_text_input(
			array(
				'id'          => 'commission_to_admin', // required, will be used as meta_key
				'label'       => __( 'Commission to Admin (%)', 'elex-multi-vendor-for-woocommerce' ), // Text in the label in the editor.
				'desc_tip'    => 'true',
				'value'       => $commission,
				'description' => __( 'Commission To Admin (%)', 'elex-multi-vendor-for-woocommerce' ),
			) 
		);
	
	
		echo '</div>';

		wp_localize_script(
			'elex-mv-product-vendor-assign',
			'elexMvProductVendorParams',
			array(
				'store_id'   => esc_js( (string) $store_id ),
				'store_name' => esc_js( (string) $store_name ),
			)
		);
	}

		// Save the field value
	public function save_custom_product_general_field( $product ) {

		$action = 'update-post_' . $product->get_id();
		if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), $action ) ) {
			return;
		}

		// Capability check: only admins may update vendor/commission meta.
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			return;
		}

		$store_id          = ! empty( $_POST['elex_vendor_name'] ) ? absint( $_POST['elex_vendor_name'] ) : '';
		$commission        = isset( $_POST['commission_to_admin'] ) ? str_replace( ' ', '', sanitize_text_field( wp_unslash( $_POST['commission_to_admin'] ) ) ) : '';
		$commission_amount = '' !== $commission ? $commission : ( ! empty( $store_id ) ? get_post_meta( $store_id, 'elex_mv_store_commission', true ) : 0 );

		$product->update_meta_data( 'elex_mv_commission', $commission_amount );
		$product->update_meta_data( 'elex_mv_commission_type', 'percent' );

		if ( ! empty( $store_id ) ) {
			$product->update_meta_data( 'elex_mv_store_id', $store_id );
		}

		$product->save();
	}

	public  function output_checklist( $user ) {
		
		$user_roles = ( isset( $user->roles ) ) ? $user->roles : null;
	
		if ( is_array( $user_roles ) && in_array( 'administrator', $user_roles ) ) {
			/**
			 * To change checklist for role
			 *
			 * @since 2.0.0
			 */
			include apply_filters( 'elex_mv_checklist_template', ELEX_MULTI_VENDOR_PLUGIN_PATH . 'src/checklist.html.php' );
		} elseif ( in_array( 'elex_mv_vendor', $user_roles ) ) {
			/**
			 * To change checklist for role
			 *
			 * @since 2.0.0
			 */
			include apply_filters( 'elex_mv_checklist_template', ELEX_MULTI_VENDOR_PLUGIN_PATH . 'src/vendor_mv.html.php' );

		}
	}

	public  function output_checklist_elex_add_user( $user ) {
		
		$user_roles = ( isset( $user->roles ) ) ? $user->roles : null;
	
		if ( is_array( $user_roles ) && in_array( 'administrator', $user_roles ) && in_array( 'elex_mv_vendor', $user_roles ) ) {
			/**
			 * To change checklist for role
			 *
			 * @since 2.0.0
			 */
			include apply_filters( 'elex_mv_checklist_template', ELEX_MULTI_VENDOR_PLUGIN_PATH . 'src/checklist_new_user.php' );
		} 
	}

	public function process_checklist( $user_id ) {
		

		$is_vendor = ! empty( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-is-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ? true : false;
	
		$current_user = get_user_by( 'ID', $user_id );

		$user_meta  = get_userdata( $current_user->ID );
		$user_roles = $user_meta->roles;

		if ( $is_vendor || in_array( 'elex_mv_vendor', $user_roles ) ) {
			$current_user->add_role( 'elex_mv_vendor' );
			$vendor_settings = Vendor::load( $current_user );
			$store           = new Store();
			$store->set_vendor( $vendor_settings );


			if ( $vendor_settings->has_store() || $vendor_settings->had_store() ) {

				$store = $vendor_settings->get_store();
				$store->set_status( 'publish' );
				$store->set_user_id( $user_id );
				$store->set_user_name( $current_user->display_name );
				$store->set_email( $current_user->user_email );
				$store->set_title( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-store-name-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
				$store->set_state( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-state-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
				$store->set_country( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-country-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
				$store->set_address1( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-address-one-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
				$store->set_address2( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-address-two-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
				$store->set_phone_number( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-phone-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
				$store->set_city( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-city-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
				$store->set_postal_code( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-post-code-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
	
				$store->create();
				return;
			}
			$store->set_user_id( $user_id );
			$store->set_user_name( $current_user->display_name );
			$store->set_email( $current_user->user_email );
			$store->set_title( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-store-name-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
			$store->set_state( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-state-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
			$store->set_country( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-country-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
			$store->set_address1( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-address-one-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
			$store->set_address2( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-address-two-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
			$store->set_phone_number( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-phone-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
			$store->set_city( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-city-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
			$store->set_postal_code( sanitize_text_field( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-post-code-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );

			// $store->set_title(  );
			$store->set_status( 'publish' );

			$store->create();

		} elseif ( in_array( 'administrator', $user_roles ) && ( ( empty( filter_input( INPUT_POST, 'elex-multi-vendor-for-woocommerce-is-vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) || false === $is_vendor ) || ! ( in_array( 'elex_mv_vendor', $user_roles ) ) ) ) {

			$vendor_settings = Vendor::load( $current_user );
			$store           = new Store();
			$store->set_vendor( $vendor_settings );

			if ( $vendor_settings->has_store() ) {

				$store = $vendor_settings->get_store();
				$store->set_status( 'private' );
				$store->create();

				return;
			}
			$current_user->remove_role( 'elex_mv_vendor' );

		}
		return true;
	}

	

	public function elex_add_script_to_product_page() {

			// Only on the product page.
		if ( ! is_product() ) {
			return;
		}
			wp_enqueue_script( 'elex_mv_select_2_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/select-2-min.js', array( 'jquery', 'underscore' ), self::VERSION, true );
	}



	public function while_user_is_registered_from_admin_end( $user_id ) {

		$new_vendor_array = array();
		$current_user     = get_user_by( 'ID', $user_id );

		$user_meta  = get_userdata( $current_user->ID );
		$user_roles = $user_meta->roles;

		if ( in_array( 'elex_mv_vendor', $user_roles ) ) {
			$vendor_settings = Vendor::load( $current_user );
			$store           = new Store();
			$store->set_vendor( $vendor_settings );
	
			$store->set_user_id( $user_id );
			$store->set_user_name( $current_user->display_name );
			$store->set_email( $current_user->user_email );

			// $store->set_title(  );
			$store->set_status( 'publish' );

			$display_name = $current_user->display_name;
			$email        = $current_user->user_email;
			$adt_rp_key   = get_password_reset_key( $current_user );
			$user_login   = $current_user->user_login;
			$rp_link      = '<a href="' . network_site_url( "wp-login.php?action=rp&key=$adt_rp_key&login=" . rawurlencode( $user_login ), 'login' ) . '">Set Password</a>';
			

			$subject = 'Your Vendor Account has been Created - Set Your Password';
			$body    = 'Dear ' . $display_name . ',<br>

					We are thrilled to welcome you to ' . get_bloginfo( 'name' ) . "!<br>

					Your vendor account has been successfully created, and we're excited to have you on board. Below are your account details:<br>
					- **Username/Email ID:**" . $store->get_email() . '<br>

					
					To get started, you need to set your password. Click on the button below to set your password and access your vendor dashboard: '

					. $rp_link . "<br>

					If you have any questions or encounter any issues during the password setup process, please don't hesitate to reach out to us. We are here to assist you every step of the way.<br>

					Thank you for joining " . get_bloginfo( 'name' ) . ' We look forward to seeing your store thrive on our platform!<br>

					Best regards, <br>'

					. get_bloginfo( 'name' ) . '<br>'
					. get_bloginfo( 'url' ) . '<br>'
					. get_bloginfo( 'admin_email' ) . '<br>';
		  

			$headers   = array();
			$headers[] = 'Content-type: text/html; charset=utf-8';
			$headers[] = 'From:' . get_bloginfo( 'admin_email' );
		
			
			wp_mail( $email, $subject, $body, $headers );

			$store->create();
		}
	}

	public function while_user_is_registered( $customer_id, $new_customer_data, $password_generated ) {

		$new_vendor_array = array();
		$current_user     = get_user_by( 'ID', $customer_id );

		

		$vendor_settings = Vendor::load( $current_user );

		// phpcs:disable WordPress.Security.NonceVerification.Missing
		if ( isset( $_POST['woocommerce-register-nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['woocommerce-register-nonce'] ) ), 'woocommerce-register' ) ) {
			$new_vendor_array['image_name'] = isset( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['name'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['name'] ) : '';
			$new_vendor_array['image_path'] = isset( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['tmp_name'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['tmp_name'] ) : '';
			$new_vendor_array['image_type'] = isset( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['type'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-filetag-login']['type'] ) : '';
		}
		// phpcs:enable WordPress.Security.NonceVerification.Missing
		

		$store = new Store();
		$store->set_vendor( $vendor_settings );

		$site_settings = get_option( 'elex_mv_admin_installation_params', false );

		if ( isset( $new_vendor_array['image_name'] ) ) {
			$store->set_image( $new_vendor_array );
		}

		$store->set_user_id( $customer_id );
		$store->set_user_name( $current_user->display_name );
		$commission_amount = $site_settings['commission_for_each_sale'];

		if ( ! empty( $store->get_commission() ) ) {
			$store->set_commission( sanitize_text_field( $store->get_commission() ) );
		} else {
			$store->set_commission( $commission_amount );
		}
		if ( ! empty( filter_input( INPUT_POST, 'store_url', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_title( sanitize_text_field( filter_input( INPUT_POST, 'store_url', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL ) ) ) {
			$store->set_email( sanitize_text_field( filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL ) ) );
		}        
		if ( ! empty( filter_input( INPUT_POST, 'store_full_addr1', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_address1( sanitize_text_field( filter_input( INPUT_POST, 'store_full_addr1', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'store_full_addr2', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_address2( sanitize_text_field( filter_input( INPUT_POST, 'store_full_addr2', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'city', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_city( sanitize_text_field( filter_input( INPUT_POST, 'city', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'state', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_state( sanitize_text_field( filter_input( INPUT_POST, 'state', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'postal_code', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_postal_code( sanitize_text_field( filter_input( INPUT_POST, 'postal_code', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'country', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_country( filter_input( INPUT_POST, 'country', FILTER_SANITIZE_SPECIAL_CHARS ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'phone_number', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_phone_number( filter_input( INPUT_POST, 'phone_number', FILTER_SANITIZE_SPECIAL_CHARS ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'dial_code', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_dial_code( filter_input( INPUT_POST, 'dial_code', FILTER_SANITIZE_SPECIAL_CHARS ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'country_iso', FILTER_SANITIZE_SPECIAL_CHARS ) ) ) {
			$store->set_country_iso( filter_input( INPUT_POST, 'country_iso', FILTER_SANITIZE_SPECIAL_CHARS ) );
		}
		if ( ! empty( filter_input( INPUT_POST, 'paypal_email', FILTER_SANITIZE_EMAIL ) ) ) {
			$store->set_paypal_email( sanitize_text_field( filter_input( INPUT_POST, 'paypal_email', FILTER_SANITIZE_EMAIL ) ) );
		}     
		
		
		if ( empty( $site_settings['approve_vendor_onboard'] ) || '' === $site_settings['approve_vendor_onboard'] ) {
			$store->set_status( 'pending' );
		}

		if ( ! empty( filter_input( INPUT_POST, 'elex_mv_wanabe_a_vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ) {
			$user = new \WP_User( $customer_id );
			update_user_meta( $customer_id, 'elex_mv_wanabe_a_vendor', sanitize_text_field( filter_input( INPUT_POST, 'elex_mv_wanabe_a_vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
			$user->add_role( 'elex_mv_vendor' );
			$store->create();
			if ( ! empty( $site_settings['approve_vendor_onboard'] ) || 'on' === $site_settings['approve_vendor_onboard'] ) {

				$display_name = $current_user->display_name;
				$email        = $current_user->user_email;
				$user_login   = $current_user->user_login;
				
		  
				$subject = ' Welcome to ' . get_bloginfo( 'name' ) . ' - Your Journey Starts Here!';
				$body    = 'Dear ' . $display_name . ',<br>
	
				We are delighted to welcome you to ' . $store->get_title() . ", where your journey as a seller begins! 🚀 <br>

				Congratulations on successfully onboarding as a vendor with us. We are thrilled to have you as part of our vibrant and growing community of sellers. With your dedication and unique products, we believe you will add tremendous value to our platform.<br><br>
				
				We have equipped you with a powerful Vendor Dashboard, where you can effortlessly manage every aspect of your business. Your dashboard includes the following essential features:<br><br>
				
				1. **Order Management:** Keep track of incoming orders, process them efficiently and ensure timely deliveries to your customers.<br>
				
				2. **Product & Inventory Management:** Easily list your products, update product details, and manage your inventory levels to prevent overselling.<br>
				
				3. **Product Reviews:** Monitor and respond to customer reviews for your products, fostering trust and improving your products based on feedback.<br>
				
				4. **Shipping Options:** Explore various shipping methods and set your preferences to offer flexible shipping solutions to your customers.<br>
				
				5. **Settlement History:** Access historical settlement data to keep tabs on your earnings and financial transactions.<br><br>
				
				This is just the beginning of an exciting journey, and we are confident that your dedication and passion will drive your success on Admin Site. We can't wait to see your business thrive and grow.<br>
				
				Thank you once again for choosing us as your partner. We are here to support you in achieving your goals and reaching new heights.<br>
				
				Welcome aboard, and here's to a prosperous journey ahead!
				
				Best regards,<br>";
		  
				$headers   = array();
				$headers[] = 'Content-type: text/html; charset=utf-8';
				$headers[] = 'From:' . get_bloginfo( 'admin_email' );

				wp_mail( $email, $subject, $body, $headers );

			}
		}
	}
	public function elex_mv_enctype_custom_registration_forms() {
		echo 'enctype="multipart/form-data"';
	}


	public function wooc_extra_register_fields() {
		
		if ( get_option( 'elex_mv_admin_onboarding_done' ) ) { 
				 
			?>

		<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
			
	   
		<label class="woocommerce-form__label woocommerce-form__label-for-checkbox woocommerce-form-login__wanabe_a_vendor"  >
	
		<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox" name="elex_mv_wanabe_a_vendor" id="elex_wanabe_a_vendor" >
		<span><?php echo esc_html( 'I want to be a Vendor', 'elex-multi-vendor-for-woocommerce' ); ?></span>
		</label>

	 
		</p>
	   
		<?php } ?>
	  

		<div class="elex_admin_login_details_cl" id="elex_admin_login_details_id" hidden >

		<p class="form-row form-row-wide">
			<div class="elex-multi-vendor-for-woocommerce-wrap elex-multi-vendor-for-woocommerce-wrap-front" style="min-height: auto !important;">

			
			<label>Store Profile Image</label>
			<div class="d-flex align-items-center gap-4">
				<img id="elexPreviewImageUploadLogin" src="<?php echo esc_url( ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'images/default.jpg' ); ?>" alt="" width="100px">
					<div class="position-relative">
					<button class="btn fw-bold text-info" id="elex-multi-vendor-for-woocommerce-upload-image-login-btn">Upload</button>
					<input type="file" id="elex-multi-vendor-for-woocommerce-filetag-login" name="elex-multi-vendor-for-woocommerce-filetag-login" class="opacity-0 position-absolute w-100 h-100 top-0 start-0">
					</div>

				
			</div>
			<div  class="elex-multi-vendor-for-woocommerce-prod-img-name-div-login d-none" >
					<div class = "d-flex gap-2 align-items-center" >
						<div class="elex-multi-vendor-for-woocommerce-prod-img-name-login"></div>
							<button type="button" class=" btn btn-sm rounded-circle p-1 elex-multi-vendor-for-woocommerce-product-form-remove-image-login" ><svg xmlns="http://www.w3.org/2000/svg" height="1.5em" viewBox="0 0 512 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M256 48a208 208 0 1 1 0 416 208 208 0 1 1 0-416zm0 464A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM175 175c-9.4 9.4-9.4 24.6 0 33.9l47 47-47 47c-9.4 9.4-9.4 24.6 0 33.9s24.6 9.4 33.9 0l47-47 47 47c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-47-47 47-47c9.4-9.4 9.4-24.6 0-33.9s-24.6-9.4-33.9 0l-47 47-47-47c-9.4-9.4-24.6-9.4-33.9 0z"/></svg></button>
						</div>
					</div>
			</div>
		</p>
	

			<p class="form-row form-row-wide">
			<label for="reg_billing_first_name"><?php echo esc_html( 'Store name', 'elex-multi-vendor-for-woocommerce' ); ?> <span class="text-danger">*</span></label>
			<input type="text" class="input-text" name="store_url" id="elex_store_name"  />
			</p>
			<p class="form-row form-row-wide">
			<label for="reg_billing_last_name"><?php echo esc_html( 'Store Address 1', 'elex-multi-vendor-for-woocommerce' ); ?></label>
			<input type="text" class="input-text" name="store_full_addr1" id="elex_store_address1" />
			</p>
			<p class="form-row form-row-wide">
			<label for="reg_billing_last_name"><?php echo esc_html( 'Store Address 2', 'elex-multi-vendor-for-woocommerce' ); ?></label>
			<input type="text" class="input-text" name="store_full_addr2" id="elex_store_address2" />
			</p>
			<p class="form-row form-row-wide">
			<label for="reg_billing_last_name"><?php echo esc_html( 'Country', 'elex-multi-vendor-for-woocommerce' ); ?></label>
			<select type="text" class="input-text" name="country" id="elex_store_country" >
			<option value="">--Select Store Country-- </option>

			<?php 
												$wc_countries = new \WC_Countries();
												$countries    = $wc_countries->get_countries();
			foreach ( $countries  as $country_code => $country_name ) :
				?>
											<option value="<?php echo esc_html( $country_code ); ?>"  
																
											><?php echo esc_html( $country_name ); ?></option>
											<?php endforeach ; ?>
			</select>
			</p>
			<p class="form-row form-row-wide">
			<label for="reg_billing_last_name"><?php echo esc_html( 'State / County', 'elex-multi-vendor-for-woocommerce' ); ?></label>
			<select type="text"style="width:100%;" class="input-text" name="state" id="elex_store_state" ></select>
			</p>
			<p class="form-row form-row-wide">
			<label for="reg_billing_last_name"><?php echo esc_html( 'City', 'elex-multi-vendor-for-woocommerce' ); ?></label>
			<input type="text" class="input-text" name="city" id="elex_store_city" />
			</p>
			<p class="form-row form-row-wide">
			<label for="reg_billing_last_name"><?php echo esc_html( 'Postcode / Zip', 'elex-multi-vendor-for-woocommerce' ); ?></label>
			<input type="text" class="input-text" name="postal_code" id="elex_store_postal" />
			</p>
			
		
			<p class="form-row form-row-wide">
			<label class="form-label" for="reg_billing_phone"><?php echo esc_html( 'Phone', 'elex-multi-vendor-for-woocommerce' ); ?></label>
			<input type="hidden" class="input-text" name="dial_code" id="elex_store_dial_code"/>
			<input type="hidden" class="input-text" name="country_iso" id="elex_store_country_iso"/>
			<input type="text" class="form-control border-2 border-secondary" name="phone_number" id="elex_store_phone"/>

			</p>
			<p class="form-row form-row-wide">
			<label for="reg_billing_mail"><?php echo esc_html( 'Paypal Email Id(Optional)', 'elex-multi-vendor-for-woocommerce' ); ?></label>
			<input type="text" class="input-text" name="paypal_email" id="elex_store_paypal_email"  />
			</p>
		</div>

		
		<div class="clear"></div>
	

		<?php
	}

	public function wooc_validate_extra_register_fields( $validation_errors ) {


		if ( ! empty( filter_input( INPUT_POST, 'elex_mv_wanabe_a_vendor' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ) {

			global $wpdb;

			if ( ! empty( sanitize_url( filter_input( INPUT_POST, 'store_url' , FILTER_SANITIZE_URL ) ) ) ) {

				$store_url      = sanitize_url( filter_input( INPUT_POST, 'store_url' , FILTER_SANITIZE_URL ) );
				$store_name_arr = $wpdb->get_results( $wpdb->prepare( "select * from $wpdb->postmeta where meta_key = 'title' AND meta_value = %s", array( $store_url ) ) );

				if ( ! empty( $store_name_arr ) ) {
					$validation_errors->add( 'store_url', __( 'Please enter unique name.', 'elex-multi-vendor-for-woocommerce' ) );
				}           
			}
			if ( empty( sanitize_url( filter_input( INPUT_POST, 'store_url' , FILTER_SANITIZE_URL ) ) ) || '' === sanitize_url( filter_input( INPUT_POST, 'store_url' , FILTER_SANITIZE_URL ) ) ) {
				$validation_errors->add( 'store_url', __( 'Please enter store name.', 'elex-multi-vendor-for-woocommerce' ) );
			
			}
		
			// if ( empty( filter_input( INPUT_POST, 'store_full_addr1' ) ) || '' === filter_input( INPUT_POST, 'store_full_addr1' ) ) {
			//  $validation_errors->add( 'store_full_addr1', __( 'Please enter address.', 'elex-multi-vendor-for-woocommerce') );
			// }

	
	
			// if ( empty( filter_input( INPUT_POST, 'city' ) ) || '' === filter_input( INPUT_POST, 'city' ) ) {
			//  $validation_errors->add( 'city', __( 'Please enter City.', 'elex-multi-vendor-for-woocommerce') );
			
			// } 
	
			// if ( empty( filter_input( INPUT_POST, 'postal_code' ) ) || '' === filter_input( INPUT_POST, 'postal_code' ) ) {
			//  $validation_errors->add( 'postal_code', __( 'Please enter Postal Code.', 'elex-multi-vendor-for-woocommerce') );
			// }
		
			// if ( empty( filter_input( INPUT_POST, 'country' ) ) || '' === filter_input( INPUT_POST, 'country' ) ) {
			//  $validation_errors->add( 'country', __( 'Please enter Country.', 'elex-multi-vendor-for-woocommerce') );
			// }
			// if ( ! empty( filter_input( INPUT_POST, 'phone_number' ) ) && ! preg_match( '/^[0-9]*$/', sanitize_text_field( filter_input( INPUT_POST, 'phone_number' ) ) ) ) {
			//  $validation_errors->add( 'phone_number', __( 'Please enter Phone Number in numbers.', 'elex-multi-vendor-for-woocommerce') );

			// }       
		}       
		return $validation_errors;
	}

	public function hide_elex_mv_page( $name ) {
		$posts = get_posts(
			array(
				'post_type'              => 'page',
				'title'                  => $name,
				'post_status'            => 'all',
				'numberposts'            => 1,
				'update_post_term_cache' => false,
				'update_post_meta_cache' => false,           

			)
		);

		if ( ! empty( $posts ) ) {

			$update_data = array(
				'ID'          => $posts[0]->ID,
				'post_status' => 'private',
				'post_parent' => -1,
			);
			wp_update_post( $update_data );

			
		//  wp_delete_post( $posts[0]->ID );
		}
	}
	public function create_elex_mv_page( $name, $slug, $shortcode, $status ) {
		$posts = get_posts(
			array(
				'post_type'              => 'page',
				'title'                  => $name,
				'post_name'              => $slug,
				'post_status'            => 'all',
				'numberposts'            => 1,
				'update_post_term_cache' => false,
				'update_post_meta_cache' => false,           

			)
		);



	
		if ( isset( $posts[0]->ID ) ) {
			$update_data = array(
				'ID'          => $posts[0]->ID,
				'post_parent' => -1,
				'post_status' => 'publish',

			);


			wp_update_post( $update_data );
			return;
		}
		 

		if ( empty( $posts ) ) {
			$page_id = wp_insert_post(
				array(
					'comment_status' => 'close',
					'ping_status'    => 'close',
					'post_author'    => 1, 
					'post_title'     => $name,
					'post_name'      => ! empty( $slug ) ? $slug : strtolower( str_replace( ' ', '-', trim( $name ) ) ),
					'post_status'    => $status,
					'post_content'   => $shortcode,
					'post_type'      => 'page',
				)
			);




			if ( $page_id ) {
				$update_data = array(
					'ID'          => $page_id,
					'post_parent' => -1,
					'post_status' => 'publish',

				);


				wp_update_post( $update_data );
			}
		}
	}


	public function elex_mv_product_vendor_name_display() {
		global $post;
		$product = wc_get_product( $post->ID );

		$store_id   = $product->get_meta( 'elex_mv_store_id', true );
		$store_name = get_post_meta( $store_id, 'elex_mv_title', true );

		echo 'Vendor Name - #'; 
		echo esc_html( $store_id . ' => ' );
		echo esc_html( $store_name );
		?>
		 
		<input type="text" style="width:100% !important" name="vendor_name" list="elex_vendor_name_list" class="elex-admin-add-product-apply-vendor-name form-select border-2 border-secondary " >
		<datalist id="elex_vendor_name_list">
		</datalist>
		<button type="button" class="elex-admin-add-product-apply-vendor-name-btn" >Assign Vendor</button>


		<?php
		wp_localize_script(
			'elex-mv-vendor-name-display',
			'elexMvVendorNameDisplayParams',
			array(
				'product_id' => esc_js( (string) $post->ID ),
			)
		);
	}

	public function elex_vendor_custom_post_type() {
		register_post_type(
			'elex_vendor',
			array(
				'labels'      => array(
					'name'          => 'Elex Vendor',
					'singular_name' => 'Elex Vendor',
				),
				'public'      => false,
				'has_archive' => true,
			)
		);
	}

	public function vendor_capabilities_initialize() {  

		$current_user = wp_get_current_user();
		$user_id      = $current_user->ID;

		
		$vendor_settings = Vendor::load( $current_user );
		

		$user_meta = get_userdata( $user_id );
		if ( empty( $user_meta ) ) {
			return;
		}
		$user_roles = $user_meta->roles;

		if ( $vendor_settings->has_store() && in_array( 'elex_mv_vendor', $user_roles ) ) {

			$store          = $vendor_settings->get_store();
			$this->store_id = $store->get_id();
		}

		$admin_params = get_option( 'elex_mv_admin_installation_params', false );

		$su_admin = get_role( 'administrator' );
		$su_admin->add_cap( 'admin_vendor_access' );
		$vendor_admin = get_role( 'elex_mv_vendor' );

		PreInstaller::user_roles();
		
		if ( isset( $admin_params['allow_admin_dashboard'] ) && 'on' === $admin_params['allow_admin_dashboard'] && ! ( in_array( 'administrator', $user_roles ) ) ) {

			$vendor_admin->add_cap( 'shop_manager' );
			$vendor_admin->add_cap( 'edit_dashboard' );
			$vendor_admin->add_cap( 'manage_options' );
			$vendor_admin->add_cap( 'create_posts' );
			$vendor_admin->add_cap( 'read' );
			$vendor_admin->add_cap( 'manage_woocommerce' );
			$vendor_admin->add_cap( 'edit_shop_order' );
			$vendor_admin->add_cap( 'edit_shop_order_terms' );
			$vendor_admin->add_cap( 'edit_shop_orders' );
			$vendor_admin->add_cap( 'manage_shop_order_terms' );
			$vendor_admin->add_cap( 'publish_shop_orders' );
			$vendor_admin->add_cap( 'read_private_shop_orders' );
			$vendor_admin->add_cap( 'read_shop_order' );
			$vendor_admin->add_cap( 'assign_shop_order_terms' );
			$vendor_admin->add_cap( 'delete_others_shop_orders' );
			$vendor_admin->add_cap( 'delete_private_shop_orders' );
			$vendor_admin->add_cap( 'delete_published_shop_orders' );
			$vendor_admin->add_cap( 'delete_shop_order' );
			$vendor_admin->add_cap( 'delete_shop_order_terms' );
			$vendor_admin->add_cap( 'delete_shop_orders' );
			$vendor_admin->add_cap( 'edit_others_shop_orders' );
			$vendor_admin->add_cap( 'edit_private_shop_orders' );
			$vendor_admin->add_cap( 'edit_published_shop_orders' );
			add_action( 'admin_menu', array( $this, 'disable_wordpress_menu_tabs' ) );
			

		} elseif ( isset( $vendor_admin ) ) {

			$vendor_admin->remove_cap( 'admin_vendor_access' );
			$vendor_admin->remove_cap( 'edit_dashboard' );
			$vendor_admin->remove_cap( 'edit_posts' );
			$vendor_admin->remove_cap( 'edit_pages' );
			$vendor_admin->remove_cap( 'manage_options' );
			$vendor_admin->remove_cap( 'create_posts' );
			$vendor_admin->remove_cap( 'read' );
			$vendor_admin->remove_cap( 'publish_posts' );
			$vendor_admin->remove_cap( 'manage_options' );
			$vendor_admin->remove_cap( 'create_posts' );
			$vendor_admin->remove_cap( 'read' );
			$vendor_admin->remove_cap( 'manage_woocommerce' );
			$vendor_admin->remove_cap( 'manage_woocommerce_orders' );
			$vendor_admin->remove_cap( 'edit_shop_order' );
			$vendor_admin->remove_cap( 'edit_shop_order_terms' );
			$vendor_admin->remove_cap( 'edit_shop_orders' );
			$vendor_admin->remove_cap( 'manage_shop_order_terms' );
			$vendor_admin->remove_cap( 'publish_shop_orders' );
			$vendor_admin->remove_cap( 'read_private_shop_orders' );
			$vendor_admin->remove_cap( 'read_shop_order' );
			$vendor_admin->remove_cap( 'assign_shop_order_terms' );
			$vendor_admin->remove_cap( 'delete_others_shop_orders' );
			$vendor_admin->remove_cap( 'delete_private_shop_orders' );
			$vendor_admin->remove_cap( 'delete_published_shop_orders' );
			$vendor_admin->remove_cap( 'delete_shop_order' );
			$vendor_admin->remove_cap( 'delete_shop_order_terms' );
			$vendor_admin->remove_cap( 'delete_shop_orders' );
			$vendor_admin->remove_cap( 'edit_others_shop_orders' );
			$vendor_admin->remove_cap( 'edit_private_shop_orders' );
			$vendor_admin->remove_cap( 'edit_published_shop_orders' );
			$vendor_admin->remove_cap( 'upload_files' );
			$vendor_admin->remove_cap( 'edit_products' );
			$vendor_admin->remove_cap( 'publish_products' );
			$vendor_admin->remove_cap( 'delete_products' );
			$vendor_admin->remove_cap( 'edit_published_products' );
			$vendor_admin->remove_cap( 'edit_others_products' );
			$vendor_admin->remove_cap( 'delete_published_products' );
			$vendor_admin->remove_cap( 'delete_others_products' );
			$vendor_admin->remove_cap( 'shop_manager' );
			$vendor_admin->remove_cap( 'edit_dashboard' );
			$vendor_admin->remove_cap( 'edit_posts' );
			$vendor_admin->remove_cap( 'edit_pages' );


		}
	}


	public function disable_wordpress_menu_tabs() {
		// List of menu tabs to disable
		$disabled_menu_tabs = array(
			'index.php',        // Dashboard
			'edit.php',         // Posts
			'upload.php',       // Media
			'edit.php?post_type=page', // Pages
			'edit-comments.php', // Comments
			'themes.php',       // Appearance
			'plugins.php',      // Plugins
			'users.php',        // Users
			'tools.php',        // Tools
			'options-general.php', // Settings
		);
	
		// Loop through the menu items and remove them
		foreach ( $disabled_menu_tabs as $tab ) {
			remove_menu_page( $tab );
		}
	}

	public function add_query_vars( $qvars ) {
		$qvars[] = 'tab';
		$qvars[] = 'search_term';
		$qvars[] = 'from_date';
		$qvars[] = 'to_date';
		$qvars[] = 'status';
		$qvars[] = 'enabled';
		$qvars[] = 'stock';
		$qvars[] = 'order_id';
		$qvars[] = 'sort_by';
		$qvars[] = 'order_by';
		$qvars[] = 'page_c';
		$qvars[] = 'page_num';
		$qvars[] = 'elex-advance-filter';
		$qvars[] = 'menu_tab';
		$qvars[] = 'category_search';
		$qvars[] = 'store_title';
		$qvars[] = 'store_id';
		$qvars[] = 'page';

		return $qvars;
	}

	public function admin_page_setup_redirect_for_elex_mv( $plugin ) {

		$site_settings = SiteSettings::load();
		if ( ! $site_settings->is_setup_finished( 'is_setup_finished' ) ) {
			if ( 'elex-multi-vendor-for-woocommerce/elex-multi-vendor-for-woocommerce.php' === $plugin ) {
				wp_safe_redirect( admin_url( 'admin.php?page=elex-multi-vendor-for-woocommerce-setup' ) );
				die();
			}
		}
	}


	public function elex_mv_add_page_setup() {
		include_once ELEX_MULTI_VENDOR_PLUGIN_PATH . 'templates/admin-site-settings/admin-flow.php';
	}

	public function Elex_Mv_Dashboard_page() {


		ob_start();

		$site_settings = get_option( 'elex_mv_admin_installation_params', false );

		$current_user = wp_get_current_user();

		$user_meta = get_userdata( $current_user->ID );
		if ( empty( $user_meta ) ) {
			return;
		}
		$user_roles      = $user_meta->roles;
		$vendor_settings = Vendor::load( $current_user );

		$store = new Store();
		$store->set_vendor( $vendor_settings );


		if ( $vendor_settings->has_store() && in_array( 'elex_mv_vendor', $user_roles ) ) {

			$store = $vendor_settings->get_store();
		}

		

		if ( $store->get_is_setup_finished() ) {
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only, used only to determine the active sidebar tab, no state-changing operation is performed.
			$menuu = isset( $_GET['menu_tab'] ) ? sanitize_text_field( wp_unslash( $_GET['menu_tab'] ) ) : '';
			include_once ELEX_MULTI_VENDOR_PLUGIN_PATH . '/templates/elex-vendor-dashboard-html.php';

		} elseif ( $store && in_array( 'elex_mv_vendor', $user_roles ) && isset( $site_settings['vendor_onboarding_wizard'] ) && 'on' === $site_settings['vendor_onboarding_wizard'] ) {

			wp_enqueue_script( 'elex-mv-vendor-onboard-store-details', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/frontend/vendor-onboard-store-details.js', array( 'jquery', 'elex_mv_select_2_js', 'elex_mv_intl_tel_input_js' ), self::VERSION, true );
			wp_localize_script(
				'elex-mv-vendor-onboard-store-details',
				'elexMvOnboardStoreDetailsParams',
				array(
					'ajax_url'    		=> esc_url( admin_url( 'admin-ajax.php' ) ),
					'state_code'  		=> esc_js( (string) get_post_meta( $store->get_id(), 'state', true ) ),
					'country_iso' 		=> esc_js( (string) get_post_meta( $store->get_id(), 'country_iso', true ) ),
					'load_states_nonce' => wp_create_nonce( 'elex_mv_load_states_nonce' ),
				)
			);
			wp_enqueue_script( 'elex-mv-vendor-onboard-ui', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/frontend/vendor-onboard-ui.js', array( 'jquery', 'elex_mv_intl_tel_input_js' ), self::VERSION, true );
			wp_localize_script(
				'elex-mv-vendor-onboard-ui',
				'elexMvOnboardUiParams',
				array(
					'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
				)
			);

			include_once ELEX_MULTI_VENDOR_PLUGIN_PATH . '/templates/vendor-onboarding/vendor-onboard-ui.php';
		
		} elseif ( in_array( 'administrator', $user_roles ) ) {
			echo '<h3>' . esc_html( 'You are Logged in as Admin.' ) . '</h3>';
		} else {
			echo '<h3>' . esc_html( 'Your are not a vendor yet. Please contact your admin for more information.' ) . '</h3>';

		}

		return ob_get_clean();
	}

	
	public function register_custom_post_status() {
		add_action( 'init', array( $this, 'my_custom_status_creation' ) );
		/* add_action( 'post_submitbox_misc_actions', array( $this, 'add_to_post_status_dropdown' ) ); */
		/* add_action( 'admin_footer-edit.php', array( $this, 'custom_status_add_in_quick_edit' ) ); */
		add_filter( 'display_post_states', array( $this, 'display_archive_state' ) );
	}



	public function redirect_onboard_settings() {
			$elex_mv_redirecting_url = add_query_arg( 'page', 'elex-multi-vendor-for-woocommerce-setup', admin_url( 'admin.php' ) );
			// wp_redirect($elex_mv_redirecting_url);
			die;
	}





	public function register_routes() {

		AdminProductController::init();
		AdminDashboard::init();
		CategoryController::init();
		SiteSettingsController::init();
		VendorInstallerController::init();
		ProductController::init();
		VendorDashboardController::init();
		ManageVendors::init();
		CouponController::init();
		PaymentController::init();
		AdminPaymentController::init();
		AdminSettings::init();
		OrderController::init();
		ShippingController::init();
		HelpAndSupportController::init();
		ReviewController::init();
		VendorSettings::init();
	}
	public function enqueue_scripts_admin() {

		

		wp_enqueue_script( 'elex_mv_select_2_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/select-2-min.js', array( 'jquery', 'underscore' ), self::VERSION, true );

		wp_enqueue_style( 'elex_mv_select_2_css', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'resources/dist/select-2-min.css', array(), self::VERSION );
	}
	public function enqueue_admin_scripts() {

	
		$current_page = ! empty( filter_input( INPUT_GET, 'page' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ? sanitize_text_field( filter_input( INPUT_GET, 'page' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) : ( ! empty( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ? sanitize_text_field( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) : '' );

		wp_enqueue_script( 'chart-js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/lib/chartjs/chart.min.js', array( 'jquery', 'underscore' ), self::VERSION, true );
		wp_enqueue_script( 'leaflet-js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/lib/leaflet/leaflet.min.js', array( 'jquery' ), self::VERSION, true );
		wp_enqueue_script( 'elex-mv-country-centroids', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/elex-mv-country-centroids.js', array(), self::VERSION, true );
		wp_enqueue_script( 'elex_mv_serialize_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/jquery.serializejson.js', array( 'jquery' ), self::VERSION, true );

		if ( 'elex-multi-vendor-for-woocommerce-payments' === $current_page || 'settlement_history' === $current_page ) {
			wp_enqueue_script( 'elex_mv_datatable_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/datatable_cdn.js', array( 'jquery', 'underscore' ), self::VERSION, true );
			wp_enqueue_script( 'elex_mv_vendor_payment_tab_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/vendor-payments.js', array( 'jquery', 'elex_mv_datatable_js' ), self::VERSION, true );
			wp_localize_script(
				'elex_mv_vendor_payment_tab_js',
				'elex_mv_vendor_ajaxurl_object',
				array(
					'ajax_url' => admin_url( 'admin-ajax.php' ),
					'nonce'    => wp_create_nonce( 'mv_nonce' ),
				)
			);
		}
				
		wp_enqueue_script( 'elex_mv_vendor_sidepanel_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/vendor_sidepanel.js', array( 'jquery' ), self::VERSION, true );
		wp_enqueue_script( 'elex_mv_select_2_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/select-2-min.js', array( 'jquery', 'underscore' ), self::VERSION, true );
		wp_enqueue_script( 'elex_mv_coupon_tab_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/coupon-tab.js', array( 'jquery' ), self::VERSION, true );
		wp_enqueue_script( 'elex_mv_bulk_option_variation_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/coupon-tab.js', array( 'jquery' ), self::VERSION, true );

		wp_enqueue_script( 'elex_mv_popper_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/popper.min.js', array( 'jquery' ), self::VERSION, true );
		wp_enqueue_script( 'elex_mv_bootstrap_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/bootstrap.min.js', array( 'jquery' ), self::VERSION, true );
	
		wp_enqueue_script( 'elex_mv_product_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/product-page.js', array( 'jquery' ), self::VERSION, true );
		wp_enqueue_script( 'elex_mv_product_variation_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/product-page-variation.js', array( 'jquery' ), self::VERSION, true );
		
		wp_enqueue_script( 'elex_mv_serialize_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/jquery.serializejson.js', array( 'jquery' ), self::VERSION, true );

		wp_enqueue_script( 'elex_mv_order_tab_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/order.js', array( 'jquery' ), self::VERSION, true );
		wp_enqueue_script( 'elex_mv_review_tab_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/review.js', array( 'jquery', 'wp-i18n' ), self::VERSION, true );
		wp_enqueue_script( 'elex_mv_admin_login_tab_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/admin-login.js', array( 'jquery', 'wp-i18n' ), self::VERSION, true );
		wp_enqueue_script( 'elex_mv_intl_tel_input_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/lib/intl-tel-input/js/intlTelInput-jquery.min.js', array( 'jquery', 'wp-i18n' ), self::VERSION );

		if ( 'elex-multi-vendor-for-woocommerce-dashboard' === $current_page ) {
			wp_enqueue_script( 'elex_mv_admin_dashboard_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/admin-dashboard.js', array( 'jquery' ), self::VERSION, true );
			wp_localize_script(
				'elex_mv_admin_dashboard_js',
				'elex_mv_admin_dashboard_params',
				array(
					'ajax_url' => admin_url( 'admin-ajax.php' ),
					'nonce'    => wp_create_nonce( 'elex_mv_admin_dashboard' ),
				)
			);
		}
		wp_enqueue_script( 'elex_mv_admin_woocommerce_shipping_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/admin-woocommerce-shipping.js', array( 'jquery' ), self::VERSION, true );

		wp_localize_script(
			'elex_mv_bulk_option_variation_js',
			'elex_mv_bulk_option_variation_obj',
			array(
				'plugin_url'                          => WC()->plugin_url(),
				'ajax_url'                            => admin_url( 'admin-ajax.php' ),
				'add_variation_nonce'                 => wp_create_nonce( 'add-variation' ),
				'link_variation_nonce'                => wp_create_nonce( 'link-variations' ),
				'delete_variations_nonce'             => wp_create_nonce( 'delete-variations' ),
				'load_variations_nonce'               => wp_create_nonce( 'load-variations' ),
				'save_variations_nonce'               => wp_create_nonce( 'save-variations' ),
				'bulk_edit_variations_nonce'          => wp_create_nonce( 'bulk-edit-variations' ),
				/* translators: %d: Number of variations */
				'i18n_enter_a_value'                  => esc_js( __( 'Enter a value', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_enter_menu_order'               => esc_js( __( 'Variation menu order (determines position in the list of variations)', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_enter_a_value_fixed_or_percent' => esc_js( __( 'Enter a value (fixed or %)', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_delete_all_variations'          => esc_js( __( 'Are you sure you want to delete all variations? This cannot be undone.', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_last_warning'                   => esc_js( __( 'Last warning, are you sure?', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_choose_image'                   => esc_js( __( 'Choose an image', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_set_image'                      => esc_js( __( 'Set variation image', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_variation_added'                => esc_js( __( '1 variation added', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_variations_added'               => esc_js( __( '%qty% variations added', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_remove_variation'               => esc_js( __( 'Are you sure you want to remove this variation?', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_scheduled_sale_start'           => esc_js( __( 'Sale start date (YYYY-MM-DD format or leave blank)', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_scheduled_sale_end'             => esc_js( __( 'Sale end date (YYYY-MM-DD format or leave blank)', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_edited_variations'              => esc_js( __( 'Save changes before changing page?', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_variation_count_single'         => esc_js( __( '1 variation', 'elex-multi-vendor-for-woocommerce' ) ),
				'i18n_variation_count_plural'         => esc_js( __( '%qty% variations', 'elex-multi-vendor-for-woocommerce' ) ),
			)
		);

		wp_localize_script(
			'elex_mv_coupon_tab_js',
			'elex_mv_coupon_ajaxurl_object',
			array(
				'ajax_url'          => admin_url( 'admin-ajax.php' ),
				'nonce'             => wp_create_nonce( 'elex_mv_prod_page_nonce' ),
				'load_states_nonce' => wp_create_nonce( 'elex_mv_load_states_nonce' ),
				'plugin_path'       => ELEX_MULTI_VENDOR_MAIN_URL_PATH,
			)
		);

		wp_localize_script(
			'elex_mv_review_tab_js',
			'elex_mv_review_ajaxurl_object',
			array(
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'elex_mv_review_page_nonce' ),
			)
		);
		wp_enqueue_script( 'elex_mv_payment_tab_js', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/js/admin-payments.js', array( 'jquery' ), self::VERSION, true );
		wp_localize_script(
			'elex_mv_payment_tab_js',
			'elex_mv_admin_ajaxurl_object',
			array(
				'ajax_url'        => admin_url( 'admin-ajax.php' ),
				'nonce'           => wp_create_nonce( 'mv_nonce' ),
				'currency_symbol' => get_woocommerce_currency_symbol(),
			)
		);

		
		$wc_country = array(
			'country' => json_encode( array_merge( WC()->countries->get_allowed_country_states(), WC()->countries->get_shipping_country_states() ) ),
		);
		wp_localize_script( 'elex-mv-js', 'elex_mv_js', $wc_country );
	}
	public function enqueue_scripts() {
	
		wp_enqueue_style( 'leaflet-css', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/lib/leaflet/leaflet.min.css', array(), self::VERSION );
		wp_enqueue_style( 'fontawesome', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/lib/fontawesome/all.min.css', array(), self::VERSION );
	

		wp_enqueue_style( 'elex_mv_select_2_css', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'resources/dist/select-2-min.css', array(), self::VERSION );
	
		wp_enqueue_style( 'multivendor', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'resources/dist/app.css', array(), self::VERSION );

		wp_enqueue_style( 'intl_tel_input', ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'assets/lib/intl-tel-input/css/intlTelInput.css', array(), self::VERSION );
	}

	public function Elex_Mv_Add_menu() {
		$site_settings = SiteSettings::load();
		if ( ! $site_settings->is_setup_finished( 'is_setup_finished' ) ) {

			$this->main_slug = 'elex-multi-vendor-for-woocommerce-setup';

		} else {

			$this->main_slug = 'elex-multi-vendor-for-woocommerce-dashboard';

		}
		add_menu_page( 'ELEX Multi Vendor', 'ELEX Multi Vendor', 'admin_vendor_access', $this->main_slug, '', ELEX_MULTI_VENDOR_MAIN_URL_PATH . '/images/elex-grey-logo.svg' );    

		if ( $site_settings->is_setup_finished( 'is_setup_finished' ) ) {
			add_submenu_page( $this->main_slug, 'Elex Multi Vendor', 'Manage Vendors', 'admin_vendor_access', 'elex-multi-vendor-for-woocommerce-manage-vendor', array( $this, 'admin_manage_vendor_page' ) );
			add_submenu_page( $this->main_slug, 'Elex Multi Vendor', 'Vendor Products', 'admin_vendor_access', 'elex-multi-vendor-for-woocommerce-products', array( $this, 'admin_product_manage_page' ) );
		}
		add_submenu_page( $this->main_slug, 'Elex Multi Vendor', 'Settings', 'admin_vendor_access', 'elex-multi-vendor-for-woocommerce-settings', array( $this, 'admin_settings_page' ) );
		add_submenu_page( $this->main_slug, 'Elex Multi Vendor', 'Help & Support', 'admin_vendor_access', 'elex-multi-vendor-for-woocommerce-help', array( $this, 'admin_help_and_support_page' ) );
		// add_submenu_page( $this->main_slug, 'Elex Multi Vendor', 'License', 'admin_vendor_access', 'elex-multi-vendor-for-woocommerce-license', array( $this, 'admin_licence_page' ) );
		add_submenu_page( $this->main_slug, 'Elex Multi Vendor', 'Admin-Onboard', 'admin_vendor_access', 'elex-multi-vendor-for-woocommerce-setup', array( $this, 'admin_onboarding_page' ) );
			
		/**
		 * Action to redirect to activation page.
		 *
		 * @since 1.0.0
		 */
		do_action( 'elex_mv_redirect_to_activation_page' );
	}

	public function rename_labels( $labels ) {
		global $submenu;
		unset( $submenu['elex-multi-vendor-for-woocommerce-dashboard'][0] );
	}
	
	public function admin_manage_vendor_page() {
		/**
		 * Add A page
		 *
		 * @since 1.0.0
		 */
		do_action( 'elex_mv_manage_vendor_page' );
	}

	public function remove_admin_onboard_page() {

		remove_submenu_page( $this->main_slug, 'elex-multi-vendor-for-woocommerce-setup' );
		remove_submenu_page( $this->main_slug, 'elex-multi-vendor-for-woocommerce-setup' );
		if ( 'elex-multi-vendor-for-woocommerce-setup' === $this->main_slug ) {
		add_submenu_page( $this->main_slug, 'ELEX Multi Vendor', 'Onboarding', 'admin_vendor_access', 'elex-multi-vendor-for-woocommerce-setup', array( $this, 'admin_onboarding_page' ), 0 );
		}
	}


	public function admin_onboarding_page() {
		include ELEX_MULTI_VENDOR_PLUGIN_PATH . '/templates/admin-site-settings/admin-setup-ui.php';
	}

	public function admin_product_manage_page() {

		/**
		 * Add A page
		 *
		 * @since 1.0.0
		 */
		do_action( 'elex_mv_admin_product_controller_page' );
	}


	public function admin_settings_page() {
	

		if ( isset( $_POST['submit'] ) && isset( $_POST['elex_mv_settings_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['elex_mv_settings_nonce'] ) ), 'elex_mv_save_settings' ) ) {
			$elex_mv_options = AdminSettings::get_widget_settings();
			
			/**
			 * Fires before the settings has been saved to update the settings object
			 *
			 * @param SettingsModel $wschat_options Settings Model Object
			 * 
			 * @since 2.0.0
			 */
			$elex_mv_options = apply_filters( 'elex_mv_settings_saving', $elex_mv_options );

			/**
			 * Fires before the settings has been saved to update the settings object along with the current tab suffix
			 *
			 * @param SettingsModel $wschat_options Settings Model Object
			 * 
			 * @since 2.0.0
			 */
			$elex_mv_options = apply_filters( 'elex_mv_settings_saving_' . AdminSettings::get_current_tab(), $elex_mv_options );

			$elex_mv_options->save();
		}
		$data['menus']      = SiteSettings::get_settings_menu();
		$data['active_tab'] = AdminSettings::get_current_tab();

		SiteSettings::include_file( 'templates/admin-settings/menu.php', $data );
	}

	public function admin_help_and_support_page() {
		$data['menus']      = HelpAndSupport::get_help_and_support_menu();
		$data['active_tab'] = HelpAndSupportController::get_current_tab();

		HelpAndSupport::include_file( 'templates/admin-help&support/helpmenu.php', $data );
	}

	// public function admin_licence_page() {

	//  $plugin_name = 'elex-multi-vendor-for-woocommerce';
	//  $file = ELEX_MULTI_VENDOR_PLUGIN_PATH . 'wf_api_manager/html/html-wf-activation-window-new.php';
	//  include_once $file;
	// }


	public function my_custom_status_creation() {
		register_post_status(
			'reject',
			array(
				'label'                     => 'Rejected',
				/* translators: %s: search term */
				'label_count'               => _n_noop( 'Rejected <span class="count">(%s)</span>', 'Rejected <span class="count">(%s)</span>', 'elex-multi-vendor-for-woocommerce' ),
				'public'                    => true,
				'exclude_from_search'       => false,
				'show_in_admin_all_list'    => true,
				'show_in_admin_status_list' => true,
			)
		);
	}

	public function add_to_post_status_dropdown() {
		global $post;

		$status = ( 'reject' === $post->post_status )
			? "jQuery('#post-status-display').text('Rejected');jQuery('select[name=\"post_status\"]').val('reject');"
			: '';
		$js     = "jQuery(document).ready(function(){jQuery('select[name=\"post_status\"]').append('<option value=\"reject\">Rejected</option>');" . $status . '});';
		wp_add_inline_script( 'jquery', $js );
	}

	public function custom_status_add_in_quick_edit() {
		wp_add_inline_script( 'jquery', "jQuery(document).ready(function(){jQuery('select[name=\"_status\"]').append('<option value=\"reject\">Rejected</option>');});" );
	}

	public function display_archive_state( $states ) {
		global $post;
		$arg = get_query_var( 'post_status' );
		if ( 'reject' !== $arg ) {
			if ( 'reject' === $post->post_status ) {
				static $script_added = false;
				if ( ! $script_added ) {
					wp_add_inline_script( 'jquery', "jQuery(document).ready(function(){jQuery('#post-status-display').text('Rejected');});" );
					$script_added = true;
				}
				return array( 'Rejected' );
			}
		}
		return $states;
	}
}
