<?php 
/*
 Controller name: AppSettings
 Controller description: all Mobile app setting 
 
 * Plugin URI: http://ionicecommerce.com/
 * Description: A wordpress Plugin for ionicecommerce.com  app template  
 * Author: vectorcoder
 * Author URI: http://vectorcoder.com
 */

class json_api_appsettings_controller
{
    public function __construct(){
        global $json_api , $wpdb;
        if (empty($_SERVER['HTTPS']) ||
            (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'off')) {
            if (empty($_REQUEST['insecure']) || $_REQUEST['insecure'] != 'cool') {
                $json_api->error("SSL is not enabled. Either use _https_ or provide 'insecure' var as insecure=cool to confirm you want to use http protocol.");
            }
        }
				
	}
  	public function get_all_settings(){
		global $json_api , $wpdb; 

		if(get_option( 'ionic_ecommerce_app_settings')) { 
			$all_settings = get_option( 'ionic_ecommerce_app_settings');
		}
		$all_settings['privacy_page'] = nl2br($all_settings['privacy_page']);	
		$all_settings['refund_page']  = nl2br($all_settings['refund_page']);
		$all_settings['terms_page']   = nl2br($all_settings['terms_page']);	
		$all_settings['about_page']   = nl2br($all_settings['about_page']);
		$all_settings['custom_css_for_checkout'] = stripslashes($all_settings['custom_css_for_checkout']);	
		$all_settings['checkout_process'] = get_option('woocommerce_enable_guest_checkout');	
		
		return $all_settings;		
		}
	
	public function get_all_banners(){
        global $json_api , $wpdb; 	
		$get_data_qry =  "SELECT * FROM `".IONIC_APP_BANNERS_SETTINGS."` order by `banners_order` asc";
		$all_banners  = $wpdb->get_results($get_data_qry);		
		return array('data'=>$all_banners);		
    }
	

	
	public function create_product_review(){
        global $json_api , $wpdb; 
		
		if (!$json_api->query->nonce) {
		  $json_api->error("You must include a 'nonce' value to create posts. Use the `get_nonce` Core API method.", 403);
		}
		
		$nonce_id = $json_api->get_nonce_id('AppSettings', 'create_product_review');
		
		if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
		  $json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.", 403);
		}
		
		if(!$json_api->query->product_id){$json_api->error("product_id var is required.");}		
		if(!$json_api->query->rate_star){$json_api->error("rate_star var is required.");}						
		if(!$json_api->query->author_name){$json_api->error("author_name var is required.");}			
		if(!$json_api->query->author_email){$json_api->error("author_email var  is required.");}					
		if(!$json_api->query->author_content){$json_api->error("author_content var is required.");}	
		
		$ratestar = $json_api->query->rate_star;
		$ratestar = floatval($ratestar);
		if ( $ratestar < 1 || $ratestar > 5 ) {
				$json_api->error("require_valid_ratestar. Please input 1 to 5 in ratestar.", 403);
			}
		if($json_api->query->user_id){	
			$user_id        = $json_api->query->user_id;
		}else {
			$user_id ='';
		}
		$author_content = $json_api->query->author_content;	
		$checkcontent = str_replace(" ","",$author_content);
		$lengcontent  = strlen($checkcontent);
		if ( $lengcontent == 0 ) {
				$json_api->error("require_valid_comment. Please type a comment.", 403);
			}
		$author_name	=	$json_api->query->author_name;
		$author_email	=	$json_api->query->author_email;
		if(!is_email($author_email)){$json_api->error(" This email not valid ");}				
				
		$checkapproved  = get_option('comment_moderation');	
		$checkwhitelist = get_option('comment_whitelist');	
		
		if($checkapproved == 1){
				$approved = 0;
		}else{
			if($checkwhitelist == 1){
				if(!empty($author_email)){
					global $wpdb;
					$table = $wpdb->prefix.'comments';
					$checks = $wpdb->get_results("SELECT * FROM $table WHERE comment_author_email = '$author_email' AND comment_approved = 1",OBJECT);
					if(!empty($checks)){
						$approved = 1;
					}else{
						$approved = 0;
					}
				}else{
					$approved = 0;
				}
			}else{
				$approved = 1;		
			}
		}
		
		$author_ip = $_SERVER['REMOTE_ADDR'];
		$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : false;		
			
		$productid	= $json_api->query->product_id;
		$product 	= wc_get_product($productid);
		$reviews_allowed = $product->get_reviews_allowed();
		$time = current_time('mysql');	
		if(isset($reviews_allowed)){
			
			$data = array(
					'comment_post_ID' => $productid,
					'comment_author' => $author_name,
					'comment_author_email' => $author_email,
					'comment_author_url' => '',
					'comment_content' => $author_content,
					'comment_type' => '',
					'comment_parent' => 0,
					'comment_karma' => 1,
					'user_id' => $user_id,
					'comment_author_IP' => $author_ip,
					'comment_agent' => $user_agent,
					'comment_date' => $time,
					'comment_approved' => $approved,
					'comment_meta' => array(
						'rating' => $ratestar,
						'verified' => 0
					)
				);		
				
			wp_insert_comment($data);
			
			$reviews_done =  array(
				'status' => 'ok',
				'message' => 'Added a comment successfully'
			);			
			return $reviews_done;			
		}else{
			$json_api->error("This Products is not allowed to comment.", 403);
		}		
	 } 

	public function ionic_filter_products(){
		global $json_api , $wpdb; 		
		
		$metaquery 	   = array();
		$taxquery 	   = array();	

		$page	   	   = $json_api->query->page;
		$order	   	   = $json_api->query->order;
		$orderby	   = $json_api->query->order_by;	

		if(isset($order)) {$order_li = $order;}else{$order_li = 'desc';}
		if(isset($orderby)) {$orderby_li = $orderby;}else{$orderby_li = 'date';}
		if(isset($page)){$post_per_page = $page;}else{$post_per_page = '1';}

		$url           = parse_url($_SERVER['REQUEST_URI']);
		$querya        = wp_parse_args($url['query']);
		
		$tquery     = array('taxonomy' => 'product_visibility' , 'field' => 'name'  , 'terms' => 'exclude-from-catalog' , 'operator' => 'NOT IN',);
		$taxquery[] = $tquery;
		
		foreach($querya as $key => $value){
			if($key != 'insecure'){ 
				if($key == 'min_price'){
					$mquery = array('key' => '_price','value' => $value, 'type' => 'numeric', 'compare' => '>=');
					$metaquery[]= $mquery;
				}
				if($key == 'max_price') {
					$mquery = array('key' => '_price','value' => $value, 'type' => 'numeric', 'compare' => '<=');
					$metaquery[] = $mquery;
				}
				if($key == 'on_sale') {
					$mquery = array('key' => '_sale_price', 'value' => 0, 'type' => 'numeric', 'compare' => '>');
					$metaquery[] = $mquery;
				}	
				if($key == 'cat_id') { $cat_id = $value;
					$cquery = array('taxonomy'  => 'product_cat', 'field' => 'id', 'terms' => $cat_id);
					$taxquery[] = $cquery;					
				}
				if($key == 'featured') {
					$fquery  = array('taxonomy' => 'product_visibility' , 'field' => 'name'  , 'terms' => 'featured' , 'operator' => 'IN');
					$taxquery[] = $fquery;
				}
								
				$fil_att = explode('_',$key);
				
				if ($fil_att[0] == 'pa') { $get_val = explode(",",$value);
					$tquery  = array('taxonomy' => $key , 'field' => 'slug'  , 'terms' => $get_val);
					$taxquery[] = $tquery;
				}
			}
		}			
		
		$args = array(
			'post_type'              => array('product'),
			'orderby'                => $orderby_li,
			'order'                  => $order_li,		
			'paged' 		 		 => $post_per_page,
			'posts_per_page'		 => 10,
			'post_status' 			 => 'publish',
			'meta_query'             => $metaquery,
			'tax_query'				 => $taxquery,
		);	
		 
		$loop = new WP_Query( $args );
		
	 	$count = $loop->post_count;	
		$product_ids = array(); 
		while ( $loop->have_posts() ) : $loop->the_post();
				$product_ids[] =  $loop->post->ID;
		endwhile; 		
		
		return array('data'=>$product_ids);
 
	}	

	public function ionic_get_attributes(){
		
		global $json_api , $woocommerce, $post, $product , $wpdb;
		
		$metaquery 	   = array();
		$taxquery 	   = array();
		
		$url           = parse_url($_SERVER['REQUEST_URI']);
		$querya        = wp_parse_args($url['query']);
		
		$tquery     = array('taxonomy' => 'product_visibility' , 'field' => 'name'  , 'terms' => 'exclude-from-catalog' , 'operator' => 'NOT IN',);
		$taxquery[] = $tquery;		
						
		foreach($querya as $key => $value){
			if($key != 'insecure'){ 
				if($key == 'min_price'){
					$mquery = array('key' => '_price','value' => $value, 'type' => 'numeric', 'compare' => '>=');
					$metaquery[]= $mquery;
				}
				if($key == 'max_price') {
					$mquery = array('key' => '_price','value' => $value, 'type' => 'numeric', 'compare' => '<=');
					$metaquery[] = $mquery;
				}
				if($key == 'on_sale') {
					$mquery = array('key' => '_sale_price', 'value' => 0, 'type' => 'numeric', 'compare' => '>');
					$metaquery[] = $mquery;
				}	
				if($key == 'cat_id') { $cat_id = $value;
					$cquery = array('taxonomy'  => 'product_cat', 'field' => 'id', 'terms' => $cat_id);
					$taxquery[] = $cquery;					
				}
				if($key == 'featured') {
					$fquery  = array('taxonomy' => 'product_visibility' , 'field' => 'name'  , 'terms' => 'featured' , 'operator' => 'IN');
					$taxquery[] = $fquery;
				}
								
				$fil_att = explode('_',$key);
				
				if ($fil_att[0] == 'pa') { $get_val = explode(",",$value);
					$tquery  = array('taxonomy' => $key , 'field' => 'slug'  , 'terms' => $get_val);
					$taxquery[] = $tquery;
				}
			}
		}	
		
		$args = array(
			'post_type' 		=> 'product',
			'orderby'   		=> 'title',				
			'posts_per_page' 	=> -1,
			'post_status' 		=> 'publish',
			'meta_query'        => $metaquery,
			'tax_query'			=> $taxquery,
		);
		
		
		 
		$loop   = new WP_Query( $args );			
	 	$count  = $loop->post_count;	
		$attributes_lists  	= array();
		$attrib_list_array  = array();
		$price_lists = array();
		while ( $loop->have_posts() ) : $loop->the_post(); 
		global $product; 	
		
		if($product->is_on_sale()){$have_on_sale = "True";}
		if($product->is_featured()){$have_featured = "True";}
		if($product->is_type( 'variable')){
			$all_children_id = $product->get_children();
			if(isset($all_children_id)){
				foreach($all_children_id as $key => $value){
					if($product->is_on_sale()){
						$price_lists[] = get_post_meta($value , '_sale_price' , true);
					}else{
						$price_lists[] = get_post_meta($value , '_regular_price' , true);
					}
				}
			}
		}else {
			if($product->is_on_sale()){
				$price_lists[] = get_post_meta($loop->post->ID , '_sale_price' , true);
			}else{
				$price_lists[] = get_post_meta($loop->post->ID , '_regular_price' , true);
			}
		}
		
		if($product->is_type( 'variable')){ 		
			$pid[] = $loop->post->ID;			
			$attributes_lists = get_post_meta($loop->post->ID , '_product_attributes' , true);			
			foreach($attributes_lists as $attrib_names){
				if($attrib_names['is_taxonomy'] == 1){
					$attrib_list_array[] = $attrib_names['name'];
				}
			}			
		}				
		endwhile;	
		
			
		$attrib_unique_val_list = array();
		$attribute = array();
		
		$price_lists =  array_filter($price_lists);
		sort($price_lists); 
		$pricelength = count($price_lists)-1;
		$min_price = $price_lists[0];
		$max_price = $price_lists[$pricelength];
		
		$count_products    = count($pid);
		$count_attribut    = count($attrib_list_array);
		$attrib_unique_val = array_unique($attrib_list_array);
		
		if(!isset($have_on_sale) && $have_on_sale == ""){$have_on_sale = "False";}
		if(!isset($have_featured) && $have_featured == ""){$have_on_sale = "False";}
		
		$attrib_unique_val_list['min_price']		  =	$min_price;
		$attrib_unique_val_list['max_price']		  =	$max_price;
		$attrib_unique_val_list['on_sale']			  =	$have_on_sale;
		$attrib_unique_val_list['featured']			  =	$have_featured;
		
			
		if($count_products >= 1){				
			foreach($attrib_unique_val as $att_name){		
				$terms_all = array();
				foreach($pid as $pro_id){
					$terms_all[] = wp_get_post_terms($pro_id , $att_name);				
				}			
				 $aterms    = array();
				 $aterm_lst = array();
				foreach($terms_all as $term_data){
					foreach($term_data as $terms_uni){
						if(in_array($terms_uni->name, $aterms)){
						}
						else{
							$aterms[]    = $terms_uni->name;
							$aterm_lst[] = $terms_uni;
						}
					}
				}
				$str_ex = explode('pa_' , $att_name); 
				$all_attributes_list_here = wc_get_attribute_taxonomies();
				foreach($all_attributes_list_here as $atti_lst_match){
					if($atti_lst_match->attribute_name == $str_ex[1]){
						$att_label = $atti_lst_match->attribute_label; 
					}
				}
				$attribute_list['attribute_name']     = $att_label;
				$attribute_list['attribute_slug']     = $att_name;
				$attribute_list['attribute_terms']    = $aterm_lst;
				$attrib_unique_val_list['attributes'][] = $attribute_list;
				
			}		
		}else {
			$attrib_unique_val_list['status'] = "ok";
			$attrib_unique_val_list['message'] = "Sorry No Attribute Found";
		}
		
		return $attrib_unique_val_list;		
	}


	public function ionic_data_link(){
		global $json_api  , $wpdb;		
		$data_link = addslashes(file_get_contents("php://input"));
		$insert_data_link = "insert into `".IONIC_SETTINGS_DATA."` set `data_link`='".$data_link."'";		
		$wpdb->query($insert_data_link);
		if(isset($wpdb->insert_id)){ $data_id = $wpdb->insert_id; }	
		return $data_id;
	}
	
	public function ionic_get_tax(){
		global $json_api , $wpdb , $woocommerce , $product;
		
		$calculate_tax = get_option('woocommerce_calc_taxes');
		
		if(isset($calculate_tax) && $calculate_tax == 'yes'){
		
			if(isset($_GET['order'])){
				$order_data = urldecode($_GET['order']);	
				$order_data = "[".stripslashes($_GET['order'])."]";
				$order_data = json_decode($order_data,false);
			}
			
			foreach($order_data[0] as $key => $value){
				$$key = $value;
			}
			
			if (isset($shipping_ids)) {
				foreach($shipping_ids as $ship_id){
					$shipping_methods = $ship_id->method_id;
					$shipping_rate    = $ship_id->rate;
				}
			}
			
			$tax_based_on  = get_option('woocommerce_tax_based_on');
			
			$tax = new WC_Tax();
			if(isset($tax_based_on) && $tax_based_on == 'shipping'){
				$country_code = $shipping_info->country;
				$post_code    = $shipping_info->postcode;
				$city_code    = $shipping_info->city;
				$state_code   = $shipping_info->state;
			}elseif(isset($tax_based_on) && $tax_based_on == 'billing') {
				$country_code = $billing_info->country;
				$post_code    = $billing_info->postcode;
				$city_code    = $billing_info->city;
				$state_code   = $billing_info->state;			
			}elseif(isset($tax_based_on) && $tax_based_on == 'base'){ 
				$default_cou  = get_option('woocommerce_default_country');
				$get_co_st    = explode(':',$default_cou);
				$country_code = $get_co_st[0];
				$post_code    = get_option('woocommerce_store_postcode');
				$city_code    = get_option('woocommerce_store_city');
				$state_code   = $get_co_st[1];							
			}
			
			$prices_inc_tax   = get_option('woocommerce_prices_include_tax');
			
			if(isset($prices_inc_tax) && $prices_inc_tax == 'yes'){
					$default_st_add    = get_option('woocommerce_default_country');
					$get_cou_sta       = explode(':',$default_st_add);
					$country_code_base = $get_cou_sta[0];
					$post_code_base    = get_option('woocommerce_store_postcode');
					$city_code_base    = get_option('woocommerce_store_city');
					$state_code_base   = $get_cou_sta[1];		
				}	
			
			$rates_base = $tax->find_rates( array( 'country' => $country_code_base , 'city' => $city_code_base , 'postcode' => $post_code_base , 'state' => $state_code_base) );
			//print_r($rates_base);			
			$count_base_rates = count($rates_base);
				
			if(isset($count_base_rates) && $count_base_rates >= 1){
				
				foreach( $rates_base as $rate_ba ){
					$tax_rate_name_base  = $rate_ba['label'];
					$tax_rate_value_base = $rate_ba['rate'];
					$tax_ship_value_base = $rate_ba['shipping'];
				}
			}
				
						
			$rates = $tax->find_rates( array( 'country' => $country_code , 'city' => $city_code , 'postcode' => $post_code , 'state' => $state_code) );	
			//print_r($rates);		
			$count_rates = count($rates);
			
			if(isset($count_rates) && $count_rates >= 1){
				
				foreach( $rates as $rate ){
					$tax_rate_name  = $rate['label'];
					$tax_rate_value = $rate['rate'];
					$tax_ship_value = $rate['shipping'];
				}
						
			
				$total_tax = "";
				if(isset($prices_inc_tax) && $prices_inc_tax == 'no'){
					foreach ($products as $single_item) { 
						$product_id  = $single_item->product_id;	
						$product_qt  = $single_item->quantity;
						$product_pri = $single_item->price;	
						$product_vr  = $single_item->variation_id;
						
						if(isset($product_vr)){
							$product_id = $product_vr;
						}							
					
						$sin_product = wc_get_product($product_id); 
										
						$tax_status   = $sin_product->tax_status;
						$tax_class    = $sin_product->tax_class;
					
						if($tax_status == "taxable"){
							if(isset($tax_class) && $tax_class != "" && $tax_class != 'null'){
								$pro_taxes = $tax->get_rates($tax_class); 
								foreach($pro_taxes as $sprotax){
									$tax_rate_value1 =  $sprotax['rate'];					
								}
								$total_tax += ($product_pri*$product_qt)*$tax_rate_value1/100;
							}else {
								$total_tax += ($product_pri*$product_qt)*$tax_rate_value/100;
							}
						}else{
							$total_tax += ($product_pri*$product_qt)*$tax_rate_value/100;
						}						
					}	
					
					if(isset($tax_ship_value) && $tax_ship_value == "yes"){
						$total_tax += $shipping_rate*$tax_rate_value/100;
					}							
				}
				elseif(isset($prices_inc_tax) && $prices_inc_tax == 'yes'){ 
				
					foreach ($products as $single_item) { 
						$product_id = $single_item->product_id;	
						$product_qt = $single_item->quantity;
						$product_pri = $single_item->price;	
						$product_vr = $single_item->variation_id;
						
						if(isset($product_vr)){
							$product_id = $product_vr;
						}								
					
						$sin_product = wc_get_product($product_id); 
										
						$tax_status   = $sin_product->tax_status;
						$tax_class    = $sin_product->tax_class;
						
						if(isset($tax_based_on) && $tax_based_on == 'base'){
							if($tax_status == "taxable"){
								if(isset($tax_class) && $tax_class != "" && $tax_class != 'null'){
									$pro_taxes = $tax->get_rates($tax_class); 
									foreach($pro_taxes as $sprotax){
											$tax_rate_value1 =  $sprotax['rate'];					
									}
									$total_tax += ($product_pri*$product_qt) - (($product_pri / (( $tax_rate_value1/100)+1))*$product_qt);
								}else {
									$total_tax += ($product_pri*$product_qt) - (($product_pri / (( $tax_rate_value/100)+1))*$product_qt);
								}
							}else{
								$total_tax += ($product_pri*$product_qt) - (($product_pri / (( $tax_rate_value/100)+1))*$product_qt);
							}							
						}
						elseif(isset($tax_based_on) && ($tax_based_on == 'billing' || $tax_based_on == 'shipping' )){
							
							if($tax_status == "taxable"){
								if(isset($tax_class) && $tax_class != "" && $tax_class != 'null'){
									$pro_taxes = $tax->get_rates($tax_class); 
									foreach($pro_taxes as $sprotax){
											$tax_rate_value2 =  $sprotax['rate'];					
									}
									
									$pro_taxes_base = $tax->get_rates($tax_class); 
									foreach($pro_taxes_base as $sbaseprotax){
											$tax_rate_value1 =  $sbaseprotax['rate'];					
									}
									
									$product_price_cal = (($product_pri / (( $tax_rate_value2/100)+1))*$product_qt);
									$total_tax += ($product_price_cal*$product_qt) * $tax_rate_value1/100;
								}else {
									$product_price_cal = (($product_pri / (( $tax_rate_value_base/100)+1))*$product_qt);
									$total_tax += ($product_price_cal*$product_qt) * $tax_rate_value/100;
								}
							}else{
								$product_price_cal = (($product_pri / (( $tax_rate_value_base/100)+1))*$product_qt);
								$total_tax += ($product_price_cal*$product_qt) * $tax_rate_value/100;
							}							
						}
					}
					
					if(isset($tax_based_on) && $tax_based_on == 'base'){
						if(isset($tax_ship_value_base) && $tax_ship_value_base == "yes"){
							$total_tax += $shipping_rate*$tax_rate_value/100;
						}
					}elseif(isset($tax_based_on) && ($tax_based_on == 'billing' || $tax_based_on == 'shipping' )){
						if(isset($tax_ship_value_base) && $tax_ship_value_base == "yes"){
							$total_tax += $shipping_rate*$tax_rate_value_base/100;
						}
					}
					
									
				}
				
				return round($total_tax ,2);		
			
			}else {
				$response = array("status" => "ok" , "message" => "Tax rates not defined");
				return 	$response;
			}		
		}else{
			$response = array("status" => "ok" , "message" => "Please Enable tax rates and calculations in woocommerce settings");
			return 	$response;	
		}	
	}

 
 
 public function ionic_vendor_products(){

		global $json_api , $wpdb;
		
		if(!$json_api->query->post_author){$json_api->error("post_author is required.");}

		$post_author = $json_api->query->post_author;
		$page  = $json_api->query->page;
		if(isset($page)){$post_per_page = $page;}else{$post_per_page = '1';}

		$args = array(
			'post_type'              => array('product'),		
			'posts_per_page'  		 => 10,
			'post_status' 			 => 'publish',
			'author'			     => $post_author,
			'paged' 		 		 => $post_per_page
		);	
		 
		$loop_vendor = new WP_Query( $args );
			
		$product_ids = array(); 
		while ( $loop_vendor->have_posts() ) : $loop_vendor->the_post();
				$product_ids[] =  $loop_vendor->post->ID;
		endwhile; 		
		
		return  array('data'=>$product_ids);
 
	}	

	public function ionic_vendor_bank_info(){
			global $json_api , $wpdb;

			if(!$json_api->query->vendor_id){$json_api->error("vendor_id var is required.");}
			if(!$json_api->query->ac_name){$json_api->error("ac_name var is required.");}
			if(!$json_api->query->ac_number){$json_api->error("ac_number var is required.");}
			if(!$json_api->query->bank_name){$json_api->error("bank_name var is required.");}
			if(!$json_api->query->iban){$json_api->error("iban var is required.");}
			if(!$json_api->query->swift){$json_api->error("swift var is required.");}
	


			$ac_name           = $json_api->query->ac_name;
			$ac_number        = $json_api->query->ac_number;
			$bank_name       = $json_api->query->bank_name;
			$bank_addr         = $json_api->query->bank_addr;
			$routing_number  = $json_api->query->routing_number;
			$iban                  = $json_api->query->iban;
			$swift                 = $json_api->query->swift;
			$vendor_id		  = $json_api->query->vendor_id;

			$bank  =  array();
			$payment = array();

			$bank['ac_name'] = $ac_name;
			$bank['ac_number'] = $ac_number;
			$bank['bank_name'] = $bank_name;
			$bank['bank_addr'] = $bank_addr;
			$bank['routing_number'] = $routing_number;
			$bank['iban'] = $iban;
			$bank['swift'] = $swift;
			  
			$payment['bank'] = $bank;		

 			$pometa               = get_user_meta( $vendor_id, 'dokan_profile_settings', true );  
			$uns 					=  maybe_unserialize($pometa);

			$uns['payment'] = $payment;      
			 maybe_serialize($uns);

			$done = update_user_meta( $vendor_id, 'dokan_profile_settings',$uns );  
			if($done){
				$response = array("status" => "ok" , "message" => "User Info Updated Successfully");
			}else {
				$response = array("status" => "ok" , "message" => "Sorry! Here is Something Wrong");
			}
			return  $response;
	 
		}	


		public function get_all_currencies(){
		
		global $json_api , $wpdb;

		$get_all_curr = get_option( 'woocs');
			
		$display_curr_all = array();
			
		foreach($get_all_curr as $gcurr){
			$display_curr_all[] = $gcurr;
		}
		
		return  array('data'=>$display_curr_all);
	}

	public function get_vendor_info(){
		global $json_api , $wpdb , $post , $product , $woocommerce;
	    if(!$json_api->query->product_id){$json_api->error("product_id var is required.");}
		
		$product_id = $json_api->query->product_id;	
		
		$postdata = get_postdata($product_id);
		$authorID = $postdata['Author_ID'];

		  $user_id  =    get_userdata($authorID);
		  $fuser['user_id']    = $user_id->ID;
		  $fuser['username']   = $user_id->user_login;
		  $fuser['first_name'] = $user_id->first_name;
		  $fuser['last_name']  = $user_id->last_name;
		  $fuser['user_email']  = $user_id->user_email;
		  $fuser['user_nicename']  = $user_id->user_nicename;
		  $fuser['display_name']  = $user_id->display_name;
		  $allm       = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id->ID ) ); 
	      $banner_id  = get_user_meta ($user_id->ID , 'wp_user_avatar');
		  $fuser['banner']  = wp_get_attachment_url( $banner_id[0] );
          $fuser['meta']    = $allm;
		  $fusers[] = $fuser;
		
		   return array('data'=>$fusers);			
	}

	public function ionic_get_geofencing_posts() {
		global $json_api , $wpdb , $post;
		$args = array( 'post_type' => 'geofencing', 'posts_per_page' => 10 );
		$geofencing_ie_data = array();
		$the_query_ie_geo_fencing = new WP_Query( $args ); 
			if ( $the_query_ie_geo_fencing->have_posts() ) : 	
				 while ( $the_query_ie_geo_fencing->have_posts() ) : $the_query_ie_geo_fencing->the_post(); 	
					$geofencing_ie_data_dis['id'] = get_the_id(); 
					$geofencing_ie_data_dis['title'] = get_the_title();
					$geofencing_ie_data_dis['content'] =  apply_filters('the_content', get_the_content()); 
					$geofencing_ie_data_dis['latitude'] = get_post_meta(get_the_id() , 'ae_gf_latitude' , 'false'); 
					$geofencing_ie_data_dis['longitude'] = get_post_meta(get_the_id() , 'ae_gf_longitude' , 'false'); 
					$geofencing_ie_data_dis['radius'] = get_post_meta(get_the_id() , 'ae_gf_radius' , 'false'); 
					$geofencing_ie_data[] = $geofencing_ie_data_dis;
				endwhile;		
		 endif; 
		 return array('data'=>$geofencing_ie_data);
	}

	public function get_featured_dokan_vendors_list(){
		global $json_api , $wpdb;
		$args = array(
			'role'     => 'seller',
			'order'    => 'ASC',
			'meta_key' => 'dokan_feature_seller',
			'meta_value' => 'yes'
		);
		$users = get_users( $args );
		foreach($users as $user_id){
	      $fuser['user_id']    = $user_id->ID;
		  $fuser['username']   = $user_id->user_login;
		  $fuser['first_name'] = $user_id->first_name;
		  $fuser['last_name']  = $user_id->last_name;
		  $fuser['user_email']  = $user_id->user_email;
		  $fuser['user_nicename']  = $user_id->user_nicename;
		  $fuser['display_name']  = $user_id->display_name;
		  $allm       = get_user_meta ($user_id->ID , 'dokan_profile_settings');
	      $banner_id  = $allm[0]['banner'];
		  $fuser['banner']  = wp_get_attachment_url( $banner_id );
          $fuser['meta']    = $allm;
		  $fusers[] = $fuser;
	    }

		
			return array('data'=>$fusers);		
	}


	
	public function get_featured_wcvendors_list(){
		global $json_api , $wpdb;
		$args = array(
			'role'     => 'vendor',
			'order'    => 'ASC',
			'meta_key' => 'feature_wcvendor',
			'meta_value' => 'yes'
		);
		$users = get_users( $args );
		foreach($users as $user_id){
	      $fuser['user_id']    = $user_id->ID;
		  $fuser['username']   = $user_id->user_login;
		  $fuser['first_name'] = $user_id->first_name;
		  $fuser['last_name']  = $user_id->last_name;
		  $fuser['user_email']  = $user_id->user_email;
		  $fuser['user_nicename']  = $user_id->user_nicename;
		  $fuser['display_name']  = $user_id->display_name;
		  $allm       = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id->ID ) ); 
	      $banner_id  = get_user_meta ($user_id->ID , 'wp_user_avatar');
		  $fuser['banner']  = wp_get_attachment_url( $banner_id[0] );
          $fuser['meta']    = $allm;
		  $fusers[] = $fuser;
	    }

		
			return array('data'=>$fusers);		
	}
	
	
 
 } ?>