<?php
require 'pagination.class.php';
function affi_embeded_block_callback( $attr ) {
	extract( $attr );
	if($attr['productload']=='dynamic'){
		$market = $attr['market'];
		$country = $attr['country'];
		$results = array();
		if($market=='amazon'){
			require 'api/affhub_Amazon.php';
		}elseif($market=='walmart'){
			require 'api/affhub_Walmart.php';
		}elseif($market=='aliexpress'){
			require 'api/affhub_Aliexpress.php';
		}elseif($market=='bestbuy'){
			require 'api/affhub_Bestbuy.php';
		}elseif($market=='ebay'){
			require 'api/affhub_Ebay.php';
		}
	          
		foreach ($attr['items'] as $items) {
		    //echo "<pre>";print_r($items['ASIN']);"</pre>";
        	if($market=='amazon'){
        		$row =  Amazon::get_product_by_id($items['ASIN'],$country);
				if(!empty($row['external_id'])){
			    $results[] = array(
					"ASIN"=> $row['external_id'],
					"DetailPageURL"=> $row['orig_url'],
					"ImageUrl"=> $row['image'],
					"IsPrimeEligible"=> "",
					"LargeImageUrl"=> $row['image'],
					"ListPrice"=> $row['old_price'],
					"Price"=> $row['tmp_price'],
					"Currency"=> $row['currency'],
					"Rating"=> '0',
					"Subtitle"=> "",
					"Title"=> $row['title'],
					"TotalReviews"=> ""
				);
				}        		
        	}elseif($market=='walmart'){
                $row= Walmart::get_product_by_id($items['ASIN']);
				if(!empty($row['external_id'])){
            	$results[] = array(
					"ASIN"=> $row['external_id'],
					"DetailPageURL"=> $row['orig_url'],
					"addtocart"=> $row['addToCartUrl'],
					"ImageUrl"=> $row['image'],
					"IsPrimeEligible"=> "",
					"LargeImageUrl"=> $row['image'],
					"ListPrice"=> $row['original_price'],
					"Price"=> $row['tmp_price'],
					"Currency"=> $row['currency'],
					"Rating"=> $row['rating'],
					"Subtitle"=> "",
					"Title"=> $row['title'],
					"TotalReviews"=> $row['numReviews']
				);
				}
        		//echo"<pre>";print_r($results);echo"</pre>";			
        	}elseif($market=='aliexpress'){
        	    $row=  Affi_aliexpress::getproductbyid($items['ASIN']);	
				if(!empty($row['productId'])){
 		        $results[] = array(
					"ASIN"=> $row['productId'],
					"DetailPageURL"=> $row['DetailUrl'],
					"ImageUrl"=> $row['image'],
					"IsPrimeEligible"=> "",
					"LargeImageUrl"=> $row['image'],
					"ListPrice"=> $row['orig_price'],
					"Price"=> $row['sale_price'],
					"Currency"=> $row['currency'],
					"Rating"=> trim($row['averageStar']),
					"Subtitle"=> $row['storeName'],
					"Title"=> $row['title'],
					"TotalReviews"=>$row['totalValidNum']
				); 
				}				
        		//echo"<pre>";print_r($results);echo"</pre>";			
        	}elseif($market=='bestbuy'){
        	    $row=  Bestbuy::get_product_by_id($items['ASIN']);
        		//echo"<pre>";print_r($results);echo"</pre>";	
                if(!empty($row['external_id'])){
		        $results[] = array(
					"ASIN"=> $row['external_id'],
					"DetailPageURL"=> $row['orig_url'],
					"addtocart"=> $row['addtocart'],
					"ImageUrl"=> $row['image'],
					"IsPrimeEligible"=> "",
					"LargeImageUrl"=> $row['image'],
					"ListPrice"=> $row['old_price'],
					"Price"=> $row['tmp_price'],
					"Currency"=> $row['currency'],
					"Rating"=> $row['rating'],
					"Subtitle"=> "",
					"Title"=> $row['title'],
					"TotalReviews"=> $row['numReviews']
				); 
                }        		
        	}elseif($market=='ebay'){
        		$row= Ebay::get_product_by_id($items['ASIN'],$country);
				//echo "<pre>";print_r($row);"</pre>";
				if(!empty($row['external_id'])){
                $results[] =array(
					"ASIN"=> $row['external_id'],
					"DetailPageURL"=> $row['orig_url'],
					"addtocart"=> $row['orig_url'],
					"ImageUrl"=> $row['image'],
					"IsPrimeEligible"=> "",
					"LargeImageUrl"=> $row['image'],
					"ListPrice"=> $row['old_price'],
					"Price"=> $row['tmp_price'],
					"Currency"=> $row['currency'],
					"Rating"=> '0',
					"Subtitle"=> "",
					"Title"=> $row['title'],
					"TotalReviews"=> ""
				);
				}				
        	}			     
		}
		$attr['items'] ='';
		$attr['items'] = $results;

	}	
	//echo "<pre>";print_r($attr);"</pre>";
	$data = '<style>.affi-numbers-left { text-align:left;max-width:100% !important; }.affi-numbers-center { text-align:center;max-width:100% !important;}
	.affi-numbers-right { text-align:right;max-width:99% !important;}
	.box-content .star-rating i { color: '.$attr['ratingColor'].'; font-size:'.$attr['ratingSize'].'px;}
    #page-numbers li  { width:46px; padding: 0px !important;  }
	#page-numbers li a {display: block;position: relative;width: 100%; font-size: '.$attr['pgnSize'].'px;text-align: center; }
	.columns.is-multiline {max-width:100% !important;}
	.btn-primary:hover {background-color:'.$attr['btnhoverColor'].' !important;}	
	</style>';
	$data .= "<div class='columns is-multiline'>";
	if(isset($attr['productperpage'])){
		$perpage = $attr['productperpage'];
	} else {
		$perpage = '12';
	}
	global $wp;
	if(isset($_GET['mypage'])){
		$cpage  = $_GET['mypage'];
		}
	$buynow = $attr['addtocarttext'];
	if(empty($buynow)){
		$buynow = 'Buy Now';
	}

	if(!empty($attr['items'])){
	    $pagination = new pagination($attr['items'],(isset($cpage) ? $cpage : 1), $perpage);
        // Decide if the first and last links should show
        $pagination->setShowFirstAndLast(false);
        // You can overwrite the default seperator
        $pagination->setMainSeperator(' ');
        // Parse through the pagination class
		$productPages = $pagination->getResults();

        // If we have items 
		if (count($productPages) != 0) {
			// Create the page numbers  

			$pageNumbers = '<div class="affi-numbers-'.$attr['paginationalign'].'"><ul id="page-numbers" class="pagination '.$attr['paginationalign'].' ">'.$pagination->getLinks($_GET).'</ul></div>';
			// Loop through all the items in the array
			foreach ($productPages as $items) {
				//print_r($items);
				$finalurl = affi_custom_deeplink_url($items['DetailPageURL'],$attr['deeplink'],$attr['market'],$items['ASIN']);
				if($attr['market']=='amazon' && $attr['addtocartbtn']=='yes'){
					if(isset($attr['deeplink']) && $attr['deeplink']!=''){
					$deeplnk = explode('=',$attr['deeplink']);
					}else{
					$deeplnk[1]='';	
					}
					$finalurl = 'https://www.amazon.'.$attr['country'].'/gp/aws/cart/add.html?AssociateTag='.$deeplnk[1].'&ASIN.1='.$items['ASIN'].'&Quantity.1=1';
				}
				/* if($attr['market']=='amazon' || $attr['market']=='ebay' || $attr['market']=='aliexpress'){
					$finalurl = affi_custom_deeplink_url($items['DetailPageURL'],$attr['deeplink'],$attr['market']);
				    if($attr['market']=='amazon' && $attr['addtocartbtn']=='yes'){
					    $finalurl = 'https://www.amazon.'.$attr['country'].'/gp/aws/cart/add.html?AssociateTag='.$attr['deeplink'].'&ASIN.1='.$items['ASIN'].'&Quantity.1=1';
					}
					if($attr['market']=='bestbuy' && $attr['addtocartbtn']=='yes'){
						$finalurl = affi_custom_deeplink_url($items['addtocart'],$attr['deeplink'],$attr['market']);
					}
				} else {
					if($attr['market']=='walmart'){
						$W = '&prodsku='.$items['ASIN'].'&u=https://www.walmart.com/ip/'.$items['ASIN'];
					} else {
						$W = '?prodsku='.$items['ASIN'].'&u='.$items['DetailPageURL'];
					}
					$finalurl = affi_custom_deeplink_url($W,$attr['deeplink'],$attr['market']);
				} */
				$data .= "<div id='' class='column is-three-quarters-mobile is-6-tablet is-3-desktop is-".$attr['productlayout']."-widescreen is-one-quarter-fullhd'>
				<div class='box'>
				<div class='box-image'>
				<a href='".$finalurl."'><img src='".$items['ImageUrl']."' class='img-responsive img-fluid' /></a>
				</div>
				<div class='box-content'>
					<h4 style='color:".$attr['titleColor'].";font-size:".$attr['titleSize']."px;'><a href='".$finalurl."' style='color:".$attr['titleColor'].";font-size:".$attr['titleSize']."px;' href=''>".substr($items['Title'],0,70).'...'."</a></h4>";
					if(isset($attr['market']) && $attr['market']=='amazon'){
					$data .= "<p class='item-price' style='color:".$attr['priceColor'].";font-size:".$attr['priceSize']."px;'><span>".$items['Currency'].$items['Price']."</span></p>";
					}else{
					$data .= "<p class='item-price' style='color:".$attr['priceColor'].";font-size:".$attr['priceSize']."px;'><strike>".$items['Currency'].$items['ListPrice']."</strike> <span>".$items['Currency'].$items['Price']."</span></p>";
					}
					if(isset($attr['showrating']) && $attr['showrating']=='show'){
					if(!empty($items['Rating'])) {
						$data .= "<div class='star-rating' title='".$items['Rating']."' style='color:".$attr['ratingColor'].";font-size:".$attr['ratingSize']."px;'>";
						$data .= "<ul class='list-inline'>";
						$totalRating = 5;
						$starRating = $items['Rating'];
						if($attr["market"]=='ebay'){
						$starRating = $starRating/100 * $totalRating;
						} else {
						$starRating =  $items['Rating'];
						}
						for ($i = 1; $i <= $totalRating; $i++) {
							 if($starRating < $i ) {
								if(is_float($starRating) && (round($starRating) == $i)){
									$data .= "<li class='list-inline-item'><i class='fa fa-star-half-o'></i></li>";
								}else{
									$data .= "<li class='list-inline-item'><i class='fa fa-star-o'></i></li>";
								}
							 } else {
								$data .=  "<li class='list-inline-item'><i class='fa fa-star'></i></li>";
							 }
						}
							$data .= "</ul></div>";
					}					
			        }
					$data .= "<div class='btnbuyow'><a href='".$finalurl."' class='btn btn-primary' style='color:".$attr['btntextColor'].";font-size:".$attr['btntextSize']."px;'>".$buynow."</a></div>";
					if($attr['socialicons']=='show'){
						$data .="<div class='share-buttons'>";
						if($attr['socialgm']=='1'){
						$data .="<a target='_blank' href='mailto:?Subject=".encodeURIComponent($items['Title'])."&Body=".encodeURIComponent($finalurl)."' alt='email'>
						<img src='".AFFI_PLUGIN_URL."/images/social/email.png'>
						</a>";
						}
						if($attr['socialfb']=='1'){
						$data .="<a target='_blank' href='http://www.facebook.com/sharer.php?u=".encodeURIComponent($finalurl)."' alt='Facebook'>
						<img src='".AFFI_PLUGIN_URL."/images/social/facebook.png'>
						</a>";
						}
						if($attr['socialli']=='1'){
						$data .="<a target='_blank' href='http://www.linkedin.com/shareArticle?mini=true&amp;url=".encodeURIComponent($finalurl)."' alt='LinkedIn'>
						<img src='".AFFI_PLUGIN_URL."/images/social/linkedin.png'>
						</a>";
						}
						if($attr['socialpr']=='1'){
						$data .="<a target='_blank' href='http://pinterest.com/pin/create/link/?url=".encodeURIComponent($finalurl)."' alt='Pinterest'>
						<img src='".AFFI_PLUGIN_URL."/images/social/pinterest.png'>
						</a>";
						}
						if($attr['socialre']=='1'){
						$data .="<a target='_blank' href='http://reddit.com/submit?url=".encodeURIComponent($finalurl)."' alt='Reddit'>
						<img src='".AFFI_PLUGIN_URL."/images/social/reddit.png'>
						</a>";
						}
						if($attr['socialtb']=='1'){
						$data .="<a target='_blank' href='http://www.tumblr.com/share/link?url=".encodeURIComponent($finalurl)."' alt='Tumblr'>
						<img src='".AFFI_PLUGIN_URL."/images/social/tumblr.png'>
						</a>";
						}
						if($attr['socialtw']=='1'){
						$data .="<a target='_blank' href='https://twitter.com/share?url=".encodeURIComponent($finalurl)."' alt='Twitter'>
						<img src='".AFFI_PLUGIN_URL."/images/social/twitter.png'>
						</a>";
						}
						$data .="</div>";
					} 									
				$data .="</div>
				</div>
			    </div>";
			}
			$data .= "</div>";
			// Show the information about the item
			//  echo '<p><b>'.$items['Product'].'</b> &nbsp; &pound;'.$items['Price'].'</p>';
		}
        // print out the page numbers beneath the results
        $data .= $pageNumbers;
	}
	return $data;
}

function affi_searchblock_callback( $attr ) {
	extract( $attr );
	//echo "<pre>";print_r($attr);"</pre>";
    if($attr['productload']=='dynamic'){
	    $market = $attr['market'];
	    $site = $attr['country'];
	    $keyword = $attr["keyword"];
	    $perpage = $attr["perpage"];
	    $category = $attr["category"];

	    $results = array();
        if($market=='amazon'){
        	require 'api/affhub_Amazon.php';
        }elseif($market=='walmart'){
        	require 'api/affhub_Walmart.php';
        }elseif($market=='aliexpress'){
        	require 'api/affhub_Aliexpress.php';
        }elseif($market=='bestbuy'){
        	require 'api/affhub_Bestbuy.php';
        }elseif($market=='ebay'){
        	require 'api/affhub_Ebay.php';
        }
          
		foreach ($attr['items'] as $items) {
		    //echo "<pre>";print_r($items['ASIN']);"</pre>";
		    //echo "<pre>";print_r($items['ASIN']);"</pre>";
        	if($market=='amazon'){
        		$row =  Amazon::get_product_by_id($items['ASIN'],$country);
				if(!empty($row['external_id'])){
			    $results[] = array(
							"ASIN"=> $row['external_id'],
							"DetailPageURL"=> $row['orig_url'],
							"ImageUrl"=> $row['image'],
							"IsPrimeEligible"=> "",
							"LargeImageUrl"=> $row['image'],
							"ListPrice"=> $row['old_price'],
							"Price"=> $row['tmp_price'],
							"Currency"=> $row['currency'],
							"Rating"=> '0',
							"Subtitle"=> "",
							"Title"=> $row['title'],
							"TotalReviews"=> ""
							); 
                }  							
        	}elseif($market=='walmart'){
                        	$row= Walmart::get_product_by_id($items['ASIN']);
							if(!empty($row['external_id'])){
            				$results[] = array(
							"ASIN"=> $row['external_id'],
							"DetailPageURL"=> $row['orig_url'],
							"addtocart"=> $row['addToCartUrl'],
							"ImageUrl"=> $row['image'],
							"IsPrimeEligible"=> "",
							"LargeImageUrl"=> $row['image'],
							"ListPrice"=> $row['original_price'],
							"Price"=> $row['tmp_price'],
							"Currency"=> $row['currency'],
							"Rating"=> $row['rating'],
							"Subtitle"=> "",
							"Title"=> $row['title'],
							"TotalReviews"=> $row['numReviews']
							);
							}
        		//echo"<pre>";print_r($results);echo"</pre>";			
        	}elseif($market=='aliexpress'){
        	    $row=  Affi_aliexpress::getproductbyid($items['ASIN']);	
				//echo "<pre>";print_r($row);"</pre>";
				if(!empty($row['productId'])){
				$results[] = array(
					"ASIN"=> $row['productId'],
					"DetailPageURL"=> $row['DetailUrl'],
					"ImageUrl"=> $row['image'],
					"IsPrimeEligible"=> "",
					"LargeImageUrl"=> $row['image'],
					"ListPrice"=> $row['orig_price'],
					"Price"=> $row['sale_price'],
					"Currency"=> $row['currency'],
					"Rating"=> trim($row['averageStar']),
					"Subtitle"=> $row['storeName'],
					"Title"=> $row['title'],
					"TotalReviews"=>$row['totalValidNum']
				);  
                } 				
        		//echo"<pre>";print_r($results);echo"</pre>";			
        	}
        	elseif($market=='bestbuy'){
        	$row=  Bestbuy::get_product_by_id($items['ASIN']);
        		//echo"<pre>";print_r($results);echo"</pre>";	
                if(!empty($row['external_id'])){
		        	$results[] = array(
							"ASIN"=> $row['external_id'],
							"DetailPageURL"=> $row['orig_url'],
							"addtocart"=> $row['addtocart'],
							"ImageUrl"=> $row['image'],
							"IsPrimeEligible"=> "",
							"LargeImageUrl"=> $row['image'],
							"ListPrice"=> $row['old_price'],
							"Price"=> $row['tmp_price'],
							"Currency"=> $row['currency'],
							"Rating"=> $row['rating'],
							"Subtitle"=> "",
							"Title"=> $row['title'],
							"TotalReviews"=> $row['numReviews']
							);    
                }  							
        	}
        	elseif($market=='ebay'){
        		$row= Ebay::get_product_by_id($items['ASIN'],$country);
                if(!empty($row['external_id'])){
                  $results[] =array(
							"ASIN"=> $row['external_id'],
							"DetailPageURL"=> $row['orig_url'],
							"addtocart"=> $row['orig_url'],
							"ImageUrl"=> $row['image'],
							"IsPrimeEligible"=> "",
							"LargeImageUrl"=> $row['image'],
							"ListPrice"=> $row['old_price'],
							"Price"=> $row['tmp_price'],
							"Currency"=> $row['currency'],
							"Rating"=> '0',
							"Subtitle"=> "",
							"Title"=> $row['title'],
							"TotalReviews"=> ""
							);
				}							
        	}			     
		}
		
		$attr['items'] ='';
		$attr['items'] = $results;             
    }	
	//echo "<pre>";print_r($attr);"</pre>";
	$data = '<style>
	.box-content .star-rating i { color: '.$attr['ratingColor'].'; font-size:'.$attr['ratingSize'].'px;}
	.affi_wrap {max-width:100% !important;}
	.columns.is-multiline {max-width:100% !important;}
	.btn-primary:hover {background-color:'.$attr['btnhoverColor'].' !important;}
	.affibtn{color:'.$attr['searchbtnColor'].' !important;background-color:'.$attr['searchbtnbgColor'].' !important;border-color:'.$attr['searchbtnbgColor'].' !important;}
	.affibtn:hover{background-color:'.$attr['searchbtnbghoverColor'].' !important;border-color:'.$attr['searchbtnbghoverColor'].' !important;}
	</style>';
	$data .='<div class="affi_wrap affi_wrap_'.$attr['market'].'">';
	if($attr["allowkeyword"]!='no'){
		if(!empty($attr["searchlabeltext"])){
		$data.='<label class="components-base-control__label" style="font-size:'.$attr["searchtitleSize"].'px;color:'.$attr["searchtitleColor"].';">'.$attr["searchlabeltext"].'</label>';
		}
		if($attr["allowcategory"]=='yes'){
			if(isset($attr["market"]) && $attr["market"]=='amazon'){
				require_once 'market/amazon.view.php';
				$data .= amazon_view($attr);
			}elseif(isset($attr["market"]) && $attr["market"]=='aliexpress'){
				require_once 'market/aliexpress.view.php';	
				$data .= aliexpress_view($attr);
			}elseif(isset($attr["market"]) && $attr["market"]=='walmart'){
				require_once 'market/walmart.view.php';
				$data .= walmart_view($attr);	
			}elseif(isset($attr["market"]) && $attr["market"]=='bestbuy'){
				require_once 'market/bestbuy.view.php';	
				$data .= bestbuy_view($attr);
			}elseif(isset($attr["market"]) && $attr["market"]=='ebay'){
				require_once 'market/ebay.view.php';
				$data .= ebay_view($attr);	
			}
		}
	}
	
	$data .= '<div class="outercontainerarea" id="outercontainerarea">
	<div class="outersearcharea" id="outersearcharea">
	<div class="components-base-control__field">';
	if($attr["allowkeyword"]!='no'){
	$data .= '<label class="components-base-control__label" style="font-size:'.$attr["labelSize"].'px;color:'.$attr["labelColor"].';">
	'.$attr["keywordLabel"].'</label>
	<input class="affi_keyword" type="text" value="'.$attr["keyword"].'" />
	<input class="affi_perpage" type="hidden" value="'.$attr["perpage"].'">
	<input class="affi_page" type="hidden" value="1">
	<input class="affi_country" type="hidden" value="'.$attr["country"].'">
	<input class="affi_market" type="hidden" value="'.$attr["market"].'">
	<input class="affi_category" type="hidden"value="'.$attr["category"].'">
	<input class="affi_btntext" type="hidden"value="'.$attr["buttontext"].'">
	<input class="affi_pagitext" type="hidden"value="'.$attr["paginationtext"].'">
	<input class="affi_attributes" type="hidden" value="'.base64_encode(serialize($attr)).'">

	</div>
	<input type="submit" class="affibtn affisearchsubmit" name="affisearchblock" id="affisearchblock" data-market="'.$attr["market"].'" value="'.$attr["buttontext"].'" /></div>';
	}
	$data .= "<div class='columns is-multiline affi_inner_".$attr['market']."' id='outerinnerarea'>";
	if(count($attr['items'])>0){
	foreach ($attr['items'] as $key=> $items) {
		
		$finalurl = affi_custom_deeplink_url($items['DetailPageURL'],$attr['deeplink'],$attr['market'],$items['ASIN']);
		if($attr['market']=='amazon' && $attr['addtocartbtn']=='yes'){
			if(isset($attr['deeplink']) && $attr['deeplink']!=''){
			$deeplnk = explode('=',$attr['deeplink']);
			}else{
			$deeplnk[1]='';	
			}
			$finalurl = 'https://www.amazon.'.$attr['country'].'/gp/aws/cart/add.html?AssociateTag='.$deeplnk[1].'&ASIN.1='.$items['ASIN'].'&Quantity.1=1';
		}		
		$data .= "<div id='".$key."' class='column is-three-quarters-mobile is-6-tablet is-3-desktop is-".$attr['productlayout']."-widescreen is-one-quarter-fullhd'>
		<div class='box'>
		<div class='box-image'>
		<a href='".$finalurl."'><img src='".$items['ImageUrl']."' class='img-responsive img-fluid' /></a>
		</div>
			<div class='box-content'>
			<h4 style='color:".$attr['titleColor'].";font-size:".$attr['titleSize']."px;'><a href='".$finalurl."' style='color:".$attr['titleColor'].";font-size:".$attr['titleSize']."px;' href=''>".substr($items['Title'],0,70).'...'."</a></h4>";
				if(isset($attr['market']) && $attr['market']=='amazon'){
				$data .= "<p class='item-price' style='color:".$attr['priceColor'].";font-size:".$attr['priceSize']."px;'><span>".$items['Currency'].$items['Price']."</span></p>";
				}else{
				$data .= "<p class='item-price' style='color:".$attr['priceColor'].";font-size:".$attr['priceSize']."px;'><strike>".$items['Currency'].$items['ListPrice']."</strike> <span>".$items['Currency'].$items['Price']."</span></p>";
    			}
				if(isset($attr['showrating']) && $attr['showrating']=='show'){
				if(!empty($items['Rating'])) {
				$data .= "<div class='star-rating' style='color:".$attr['ratingColor'].";font-size:".$attr['ratingSize']."px;'>";
				$data .= "<ul class='list-inline'>";
				$totalRating = 5;
				$starRating = $items['Rating'];
				if($attr["market"]=='ebay'){
				$starRating = $starRating/100 * $totalRating;
				} else {
				$starRating =  $items['Rating'];
				}
				for ($i = 1; $i <= $totalRating; $i++) {
					 if($starRating < $i ) {
						if(is_float($starRating) && (round($starRating) == $i)){
							$data .= "<li class='list-inline-item'><i class='fa fa-star-half-o'></i></li>";
						}else{
							$data .= "<li class='list-inline-item'><i class='fa fa-star-o'></i></li>";
						}
					 }else {
						$data .=  "<li class='list-inline-item'><i class='fa fa-star'></i></li>";
					 }
				}
					$data .= "</ul></div>";
			}
	        }
		   $data .= "<div class='btnbuyow'><a href='".$finalurl."' class='btn btn-primary' style='color:".$attr['btntextColor'].";font-size:".$attr['btntextSize']."px;'>".$attr['addtocarttext']."</a></div>";
					if($attr['socialicons']=='show'){
						$data .="<div class='share-buttons'>";
						if($attr['socialgm']=='1'){
						$data .="<a target='_blank' href='mailto:?Subject=".encodeURIComponent($items['Title'])."&Body=".encodeURIComponent($finalurl)."' alt='email'>
						<img src='".AFFI_PLUGIN_URL."/images/social/email.png'>
						</a>";
						}
						if($attr['socialfb']=='1'){
						$data .="<a target='_blank' href='http://www.facebook.com/sharer.php?u=".encodeURIComponent($finalurl)."' alt='Facebook'>
						<img src='".AFFI_PLUGIN_URL."/images/social/facebook.png'>
						</a>";
						}
						if($attr['socialli']=='1'){
						$data .="<a target='_blank' href='http://www.linkedin.com/shareArticle?mini=true&amp;url=".encodeURIComponent($finalurl)."' alt='LinkedIn'>
						<img src='".AFFI_PLUGIN_URL."/images/social/linkedin.png'>
						</a>";
						}
						if($attr['socialpr']=='1'){
						$data .="<a target='_blank' href='http://pinterest.com/pin/create/link/?url=".encodeURIComponent($finalurl)."' alt='Pinterest'>
						<img src='".AFFI_PLUGIN_URL."/images/social/pinterest.png'>
						</a>";
						}
						if($attr['socialre']=='1'){
						$data .="<a target='_blank' href='http://reddit.com/submit?url=".encodeURIComponent($finalurl)."' alt='Reddit'>
						<img src='".AFFI_PLUGIN_URL."/images/social/reddit.png'>
						</a>";
						}
						if($attr['socialtb']=='1'){
						$data .="<a target='_blank' href='http://www.tumblr.com/share/link?url=".encodeURIComponent($finalurl)."' alt='Tumblr'>
						<img src='".AFFI_PLUGIN_URL."/images/social/tumblr.png'>
						</a>";
						}
						if($attr['socialtw']=='1'){
						$data .="<a target='_blank' href='https://twitter.com/share?url=".encodeURIComponent($finalurl)."' alt='Twitter'>
						<img src='".AFFI_PLUGIN_URL."/images/social/twitter.png'>
						</a>";
						}
						$data .="</div>";
					} 									
					$data .="</div>
		</div>
	</div>";
	}
	}
	$data .= "</div></div></div>";
	return $data;
}

function ajax_affi_get_check_result ()
{

	$attr  = unserialize(base64_decode($_POST['affiattr']));
	
	$keyword  = $_POST['keyword'];
	$market   = $_POST['market'];
	$category = $_POST['category'];
	//echo"<pre>";print_r($_POST);echo"</pre>";
	if(isset($_POST['mypage'])){ $page = $_POST['mypage']; } else { $page = 1;  }
	if(isset($_POST['perpage'])){ $perpage = $_POST['perpage']; } else { $perpage = 5;  }
	$site     = $_POST['country'];
	$country  = $_POST['country'];
	
	$socialicons= $attr['socialicons'];
	$socialgm   = $attr['socialgm'];
	$socialfb   = $attr['socialfb'];
	$socialtw   = $attr['socialtw'];
	$socialli   = $attr['socialli'];
	$socialtb   = $attr['socialtb'];
	$socialpr   = $attr['socialpr'];
	$titleSize  = $attr['titleSize'];
	$priceSize  = $attr['priceSize'];
	$ratingSize = $attr['ratingSize'];
	$btntextSize= $attr['btntextSize'];
	$titleColor = $attr['titleColor'];
	$priceColor = $attr['priceColor'];
	$ratingColor= $attr['ratingColor'];
	$btntextColor  = $attr['btntextColor'];
	$productlayout = $attr['productlayout'];
	$productlayout = $attr['productlayout'];
	$allowkeyword  = $attr['allowkeyword'];
	$deeplink      = $attr['deeplink'];
	$searchlabeltext = $attr['searchlabeltext'];
	$buttontext    = $attr['buttontext'];
	$paginationtext= $attr['paginationtext'];

	$ddata=array();
	//echo PLUGIN_PATH;		
	if($market=='amazon'){
		require 'api/affhub_Amazon.php';
		$results = Amazon::get_products($keyword,$category,$site,$page,$perpage);
	}elseif($market=='walmart'){
		require 'api/affhub_Walmart.php';
		$results = Walmart::get_products($keyword,$category,$site,$page,$perpage);	
		//echo"<pre>";print_r($results);echo"</pre>";			
	}elseif($market=='aliexpress'){
		require 'api/affhub_Aliexpress.php';
		$results = Affi_aliexpress::get_products($keyword,$category,$site,$page,$perpage);	
		//echo"<pre>";print_r($results);echo"</pre>";			
	}elseif($market=='bestbuy'){
		require 'api/affhub_Bestbuy.php';
		$results = Bestbuy::get_products($keyword,$category,$site,$page,$perpage);	
		//echo"<pre>";print_r($results);echo"</pre>";			
	}elseif($market=='ebay'){
		require 'api/affhub_Ebay.php';
		$results = Ebay::get_products($keyword,$category,$site,$page,$perpage);	
		//echo"<pre>";print_r($results);echo"</pre>";			
	}		
	foreach($results as $row){
		//echo"<pre>";print_r($row);echo"</pre>";
		if(!empty($row['external_id'])){
		$ddata[] = array(
						"ASIN"=> $row['external_id'],
						"DetailPageURL"=> $row['orig_url'],
						"ImageUrl"=> $row['image'],
						"IsPrimeEligible"=> "",
						"LargeImageUrl"=> $row['image'],
						"ListPrice"=> $row['old_price'],
						"Price"=> $row['tmp_price'],
						"Currency"=> $row['currency'],
						"Rating"=> $row['rating'],
						"Subtitle"=> $row['storeName'],
						"Title"=> $row['title'],
						"TotalReviews"=>$row['totalValidNum']
						);
		}				
		//die;				
	}		

	$data = "";
	if(!empty($ddata)){
		$data .= "";
		if(count($ddata)>0){
		foreach ($ddata as $key=> $items) {
			//echo"<pre>";print_r($items);echo"</pre>";
			$finalurl = affi_custom_deeplink_url($items['DetailPageURL'],$attr['deeplink'],$market,$items['ASIN']);
			if($attr['market']=='amazon' && $attr['addtocartbtn']=='yes'){
				if(isset($attr['deeplink']) && $attr['deeplink']!=''){
				$deeplnk = explode('=',$attr['deeplink']);
				}else{
				$deeplnk[1]='';	
				}
				$finalurl = 'https://www.amazon.'.$attr['country'].'/gp/aws/cart/add.html?AssociateTag='.$deeplnk[1].'&ASIN.1='.$items['ASIN'].'&Quantity.1=1';
			}
			/* if($market=='amazon' || $market=='ebay' || $market=='aliexpress'){
				$finalurl = affi_custom_deeplink_url($items['DetailPageURL'],$deeplink,$market);
			} else {
				if($attr['market']=='walmart'){
				$W = '&prodsku='.$items['ASIN'].'&u=https://www.walmart.com/ip/'.$items['ASIN'];
				} else {
				$W = '?prodsku='.$items['ASIN'].'&u='.$items['DetailPageURL'];
				}
				$finalurl = affi_custom_deeplink_url($W,$deeplink,$market);
			} */		
			$data .= "<div id='".$key."' class='column is-three-quarters-mobile is-6-tablet is-3-desktop is-".$attr['productlayout']."-widescreen is-one-quarter-fullhd'>
			<div class='box'>
				<div class='box-image'>
				<a href='".$finalurl."'><img src='".$items['ImageUrl']."' class='img-responsive img-fluid' /></a>
				</div>
				<div class='box-content'>
				<h4 style='color:".$titleColor.";font-size:".$titleSize."px;'><a href='".$finalurl."' style='color:".$titleColor.";font-size:".$titleSize."px;' href=''>".substr($items['Title'],0,70).'...'."</a></h4>";
				if(isset($attr['market']) && $attr['market']=='amazon'){
				$data .= "<p class='item-price' style='color:".$priceColor.";font-size:".$priceSize."px;'><span>".$items['Currency'].$items['Price']."</span></p>";
				}else{
				$data .= "<p class='item-price' style='color:".$priceColor.";font-size:".$priceSize."px;'><strike>".$items['Currency'].$items['ListPrice']."</strike> <span>".$items['Currency'].$items['Price']."</span></p>";
				}
				if(isset($attr['showrating']) && $attr['showrating']=='show'){
				if(!empty($items['Rating'])) {
					$data .= "<div class='star-rating' title='".$items['Rating']."' style='color:".$attr['ratingColor'].";font-size:".$attr['ratingSize']."px;'>";
					$data .= "<ul class='list-inline'>";
					$totalRating = 5;
					$starRating = $items['Rating'];
					if($attr["market"]=='ebay'){
					$starRating = $starRating/100 * $totalRating;
					} else {
					$starRating =  $items['Rating'];
					}
					for ($i = 1; $i <= $totalRating; $i++) {
						 if($starRating < $i ) {
							if(is_float($starRating) && (round($starRating) == $i)){
								$data .= "<li class='list-inline-item'><i class='fa fa-star-half-o'></i></li>";
							}else{
								$data .= "<li class='list-inline-item'><i class='fa fa-star-o'></i></li>";
							}
						 }else {
							$data .=  "<li class='list-inline-item'><i class='fa fa-star'></i></li>";
						 }
					}
						$data .= "</ul></div>";
				}
	        	}				
				$data .= "<div class='btnbuyow'><a href='".$finalurl."' class='btn btn-primary' style='color:".$btntextColor.";font-size:".$btntextSize."px;'>".$attr['addtocarttext']."</a></div>";
				if($socialicons=='show'){
					$data .="<div class='share-buttons'>";
					if($socialgm=='1'){
					$data .="<a target='_blank' href='mailto:?Subject=".encodeURIComponent($items['Title'])."&Body=".encodeURIComponent($finalurl)."' alt='email'>
					<img src='".AFFI_PLUGIN_URL."/images/social/email.png'>
					</a>";
					}
					if($socialfb=='1'){
					$data .="<a target='_blank' href='http://www.facebook.com/sharer.php?u=".encodeURIComponent($finalurl)."' alt='Facebook'>
					<img src='".AFFI_PLUGIN_URL."/images/social/facebook.png'>
					</a>";
					}
					if($socialli=='1'){
					$data .="<a target='_blank' href='http://www.linkedin.com/shareArticle?mini=true&amp;url=".encodeURIComponent($finalurl)."' alt='LinkedIn'>
					<img src='".AFFI_PLUGIN_URL."/images/social/linkedin.png'>
					</a>";
					}
					if($socialpr=='1'){
					$data .="<a target='_blank' href='http://pinterest.com/pin/create/link/?url=".encodeURIComponent($finalurl)."' alt='Pinterest'>
					<img src='".AFFI_PLUGIN_URL."/images/social/pinterest.png'>
					</a>";
					}
					if($socialre=='1'){
					$data .="<a target='_blank' href='http://reddit.com/submit?url=".encodeURIComponent($finalurl)."' alt='Reddit'>
					<img src='".AFFI_PLUGIN_URL."/images/social/reddit.png'>
					</a>";
					}
					if($socialtb=='1'){
					$data .="<a target='_blank' href='http://www.tumblr.com/share/link?url=".encodeURIComponent($finalurl)."' alt='Tumblr'>
					<img src='".AFFI_PLUGIN_URL."/images/social/tumblr.png'>
					</a>";
					}
					if($socialtw=='1'){
					$data .="<a target='_blank' href='https://twitter.com/share?url=".encodeURIComponent($finalurl)."' alt='Twitter'>
					<img src='".AFFI_PLUGIN_URL."/images/social/twitter.png'>
					</a>";
					}
					$data .="</div>";
				}
				$data .="</div>
				</div>
			</div>";
		}
	    }
		$data .= "<div class='loadmorediv' style='width:100%;margin: 0% 0px 4%;'>";
		$data .= '<h3 class="affi-load-more affibtn" data-pgntext="'.$paginationtext.'" data-keyword="'.$keyword.'" data-market="'.$market.'" data-country="'.$country.'" data-category="'.$category.'" data-page="'.$page.'" data-perpage="'.$perpage.'" data-attributes="'.base64_encode(serialize($attr)).'">'.$attr['paginationtext'].'</h3>';
		$data .= '<input type="hidden" id="pageload" value="1">';
		$data .= '<input  type="hidden" id="affisearchkeyword" value="'.$keyword.'">';
		$data .= '<input  type="hidden" id="afficategory" value="'.$category.'">';
		$data .= '<input type="hidden" id="affiperpage" value="'.$perpage.'">';
		$data .= '<input  type="hidden" id="affipage" value="'.$page.'">';
		$data .= '<input  type="hidden" id="socialgm" value="'.$socialgm.'">';
		$data .= '<input  type="hidden" id="socialfb" value="'.$socialfb.'">';
		$data .= '<input  type="hidden" id="socialtw" value="'.$socialtw.'">';
		$data .= '<input type="hidden" id="socialli" value="'.$socialli.'">';
		$data .= '<input  type="hidden" id="socialre" value="'.$socialre.'">';
		$data .= '<input  type="hidden" id="socialtb" value="'.$socialtb.'">';
		$data .= '<input  type="hidden" id="socialpr" value="'.$socialpr.'">';
		$data .= '<input  type="hidden" id="socialicons" value="'.$socialicons.'">';
		$data .= '<input  type="hidden" id="country" value="'.$country.'">';
		$data .= '<input  type="hidden" id="titleSize" value="'.$titleSiz.'">';
		$data .= '<input  type="hidden" id="priceSize" value="'.$priceSize.'">';
		$data .= '<input  type="hidden" id="ratingSize" value="'.$ratingSiz.'">';
		$data .= '<input type="hidden" id="btntextSize" value="'.$btntextSize.'">';
		$data .= '<input  type="hidden" id="titleColor" value="'.$titleColor.'">';
		$data .= '<input type="hidden" id="priceColor" value="'.$priceColor.'">';
		$data .= '<input type="hidden" id="ratingColor" value="'.$ratingColor.'">';
		$data .= '<input  type="hidden" id="btntextColor" value="'.$btntextColor.'">';
		$data .= '<input  type="hidden" id="productlayout" value="'.$productlayout.'">';
		$data .= '<input  type="hidden" id="allowkeyword" value="'.$allowkeyword.'">';
		$data .= '<input  type="hidden" id="market" value="'.$market.'">';
		$data .= '<input  type="hidden" id="deeplink" value="'.$deeplink.'">';
		$data .= '<input  type="hidden" id="paginationtext" value="'.$paginationtext.'">';

		$data .= '</div>';
		echo $data;
	} 
	die;
	}





	function ajax_affi_get_loader_results ()
	{
		$attr  = unserialize(base64_decode($_POST['attributes']));
		$market   = $_POST['market'];
		$category = $_POST['category'];
		$keyword  = $_POST['keyword'];

		if(isset($_POST['mypage'])){ $page = $_POST['mypage']; } else { $page = 1;  }
		if(isset($_POST['perpage'])){ $perpage = $_POST['perpage']; } else { $perpage = 5;  }
		$site     = $_POST['country'];
		$country  = $_POST['country'];	
		
		$socialicons  = $attr['socialicons'];
		$socialgm   = $attr['socialgm'];
		$socialfb = $attr['socialfb'];
		$socialtw  = $attr['socialtw'];
		$socialli   = $attr['socialli'];
		$socialtb = $attr['socialtb'];
		$socialpr  = $attr['socialpr'];
		$titleSize   = $attr['titleSize'];
		$priceSize = $attr['priceSize'];
		$ratingSize  = $attr['ratingSize'];
		$btntextSize   = $attr['btntextSize'];
		$titleColor = $attr['titleColor'];
		$priceColor = $attr['priceColor'];
		$ratingColor  = $attr['ratingColor'];
		$btntextColor   = $attr['btntextColor'];
		$productlayout = $attr['productlayout'];
		$productlayout = $attr['productlayout'];
		$allowkeyword = $attr['allowkeyword'];
		$deeplink = $attr['deeplink'];
		$paginationtext = $attr['paginationtext'];

		$ddata=array();
		if($market=='amazon'){
			require 'api/affhub_Amazon.php';
			//require_once '/home/netforce/public_html/azon/wp-content/plugins/EmbedderPro/api/affhub_Amazon.php';
			$results = Amazon::get_products($keyword,$category,$site,$page,$perpage);
		}elseif($market=='walmart'){
			require 'api/affhub_Walmart.php';
			$results = Walmart::get_products($keyword,$category,$site,$page,$perpage);	
			//echo"<pre>";print_r($results);echo"</pre>";			
		}
		elseif($market=='aliexpress'){
			require 'api/affhub_Aliexpress.php';
			$results = Affi_aliexpress::get_products($keyword,$category,$site,$page,$perpage);	
			//echo"<pre>";print_r($results);echo"</pre>";			
		}		
		elseif($market=='bestbuy'){
			require 'api/affhub_Bestbuy.php';
			$results = Bestbuy::get_products($keyword,$category,$site,$page,$perpage);	
			//echo"<pre>";print_r($results);echo"</pre>";			
		}
		elseif($market=='ebay'){
			require 'api/affhub_Ebay.php';
			$results = Ebay::get_products($keyword,$category,$site,$page,$perpage);	
			//echo"<pre>";print_r($results);echo"</pre>";			
		}	
		
		foreach($results as $row){
			//echo"<pre>";print_r($row);echo"</pre>";
			if(!empty($row['external_id'])){
			$ddata[] = array(
							"ASIN"=> $row['external_id'],
							"DetailPageURL"=> $row['orig_url'],
							"ImageUrl"=> $row['image'],
							"IsPrimeEligible"=> "",
							"LargeImageUrl"=> $row['image'],
							"ListPrice"=> $row['old_price'],
							"Price"=> $row['tmp_price'],
							"Currency"=> $row['currency'],
							"Rating"=> $row['rating'],
							"Subtitle"=> $row['storeName'],
							"Title"=> $row['title'],
							"TotalReviews"=>$row['totalValidNum']
							);
			}				
		}
		if(!empty($ddata)){
			$data = '';
			foreach ($ddata as $key=> $items) {
				$finalurl = affi_custom_deeplink_url($items['DetailPageURL'],$attr['deeplink'],$market,$items['ASIN']);
				if($attr['market']=='amazon' && $attr['addtocartbtn']=='yes'){
					if(isset($attr['deeplink']) && $attr['deeplink']!=''){
					$deeplnk = explode('=',$attr['deeplink']);
					}else{
					$deeplnk[1]='';	
					}
					$finalurl = 'https://www.amazon.'.$attr['country'].'/gp/aws/cart/add.html?AssociateTag='.$deeplnk[1].'&ASIN.1='.$items['ASIN'].'&Quantity.1=1';
				}
				/* if($market=='amazon' || $market=='ebay' || $market=='aliexpress'){
					$finalurl = affi_custom_deeplink_url($items['DetailPageURL'],$attr['deeplink'],$market);
				} else {
					if($attr['market']=='walmart'){
					$W = '&prodsku='.$items['ASIN'].'&u=https://www.walmart.com/ip/'.$items['ASIN'];
					} else {
					$W = '?prodsku='.$items['ASIN'].'&u='.$items['DetailPageURL'];
					}
					$finalurl = affi_custom_deeplink_url($W,$attr['deeplink'],$market);
				} */					
				$data .= "<div id='".$key."' class='column is-three-quarters-mobile is-6-tablet is-3-desktop is-".$attr['productlayout']."-widescreen is-one-quarter-fullhd'>
				<div class='box'>
				<div class='box-image'>
				<a href='".$finalurl."'><img src='".$items['ImageUrl']."' class='img-responsive img-fluid' /></a>
				</div>
					<div class='box-content'>
						<h4 style='color:".$titleColor.";font-size:".$titleSize."px;'><a href='".$finalurl."' style='color:".$titleColor.";font-size:".$titleSize."px;' href=''>".substr($items['Title'],0,70).'...'."</a></h4>";
						if(isset($attr['market']) && $attr['market']=='amazon'){
						$data .= "<p class='item-price' style='color:".$priceColor.";font-size:".$priceSize."px;'><span>".$items['Currency'].$items['Price']."</span></p>";
						}else{
						$data .= "<p class='item-price' style='color:".$priceColor.";font-size:".$priceSize."px;'><strike>".$items['Currency'].$items['ListPrice']."</strike> <span>".$items['Currency'].$items['Price']."</span></p>";
						}
						if(isset($attr['showrating']) && $attr['showrating']=='show'){
						if(!empty($items['Rating'])) {
						$data .= "<div class='star-rating' style='color:".$attr['ratingColor'].";font-size:".$attr['ratingSize']."px;'>";
						$data .= "<ul class='list-inline'>";
						$totalRating = 5;
						$starRating = $items['Rating'];
						if($market=='ebay'){
						$starRating = $starRating/100 * $totalRating;
						} else {
						$starRating =  $items['Rating'];
						}
						
								for ($i = 1; $i <= $totalRating; $i++) {
									if($starRating < $i ) {
										if(is_float($starRating) && (round($starRating) == $i)){
											$data .= "<li class='list-inline-item'><i class='fa fa-star-half-o'></i></li>";
										}else{
											$data .= "<li class='list-inline-item'><i class='fa fa-star-o'></i></li>";
										}
									}else {
										$data .=  "<li class='list-inline-item'><i class='fa fa-star'></i></li>";
									}
								}
							$data .= "</ul></div>";						
						}
			            }						
						$data .=	"<div class='btnbuyow'><a href='".$items['DetailPageURL']."' class='btn btn-primary' style='color:".$btntextColor.";font-size:".$btntextSize."px;'>".$attr['addtocarttext']."</a></div>";
											if($socialicons=='show'){
												$data .="<div class='share-buttons'>";
												if($socialgm=='1'){
												$data .="<a target='_blank' href='mailto:?Subject=".encodeURIComponent($items['Title'])."&Body=".encodeURIComponent($finalurl)."' alt='email'>
												<img src='".AFFI_PLUGIN_URL."/images/social/email.png'>
												</a>";
												}
												if($socialfb=='1'){
												$data .="<a target='_blank' href='http://www.facebook.com/sharer.php?u=".encodeURIComponent($finalurl)."' alt='Facebook'>
												<img src='".AFFI_PLUGIN_URL."/images/social/facebook.png'>
												</a>";
												}
												if($socialli=='1'){
												$data .="<a target='_blank' href='http://www.linkedin.com/shareArticle?mini=true&amp;url=".encodeURIComponent($finalurl)."' alt='LinkedIn'>
												<img src='".AFFI_PLUGIN_URL."/images/social/linkedin.png'>
												</a>";
												}
												if($socialpr=='1'){
												$data .="<a target='_blank' href='http://pinterest.com/pin/create/link/?url=".encodeURIComponent($finalurl)."' alt='Pinterest'>
												<img src='".AFFI_PLUGIN_URL."/images/social/pinterest.png'>
												</a>";
												}
												if($socialre=='1'){
												$data .="<a target='_blank' href='http://reddit.com/submit?url=".encodeURIComponent($finalurl)."' alt='Reddit'>
												<img src='".AFFI_PLUGIN_URL."/images/social/reddit.png'>
												</a>";
												}
												if($socialtb=='1'){
												$data .="<a target='_blank' href='http://www.tumblr.com/share/link?url=".encodeURIComponent($finalurl)."' alt='Tumblr'>
												<img src='".AFFI_PLUGIN_URL."/images/social/tumblr.png'>
												</a>";
												}
												if($socialtw=='1'){
												$data .="<a target='_blank' href='https://twitter.com/share?url=".encodeURIComponent($finalurl)."' alt='Twitter'>
												<img src='".AFFI_PLUGIN_URL."/images/social/twitter.png'>
												</a>";
												}
												$data .="</div>";
											} 									
										$data .="</div>
				</div>
			</div>";
				}
				$data .= "<div class='loadmorediv' style='width:100%;margin: 0% 0px 4%;'>";
				$data .= '<h3 class="affi-load-more affibtn" data-pgntext="'.$paginationtext.'" data-keyword="'.$keyword.'" data-market="'.$market.'" data-country="'.$country.'" data-category="'.$category.'" data-page="'.$page.'" data-perpage="'.$perpage.'" data-attributes="'.base64_encode(serialize($attr)).'">'.$attr['paginationtext'].'</h3>';
				$data .= '</div>';
		
			echo $data;
		}
		die;
		}



function affi_custom_deeplink_url($url = '',$deeplink = '',$market,$sku){
	/* echo"<pre>";print_r($url);echo"</pre>";
	echo"<pre>";print_r($deeplink);echo"</pre>";
	echo"<pre>";print_r($market);echo"</pre>";
	$market='aliexpress'; */
	
	/* if($attr["market"]=='amazon' || $attr["market"]=='ebay' || $attr["market"]=='aliexpress'){
		$finalurl = affi_custom_deeplink_url($items['DetailPageURL'],$attr['deeplink'],$attr['market']);
	} else {
		if($attr['market']=='walmart'){
		$W = '&prodsku='.$items['ASIN'].'&u=https://www.walmart.com/ip/'.$items['ASIN'];
		} else {
		$W = '?prodsku='.$items['ASIN'].'&u='.$items['DetailPageURL'];
		}
		$finalurl = affi_custom_deeplink_url($W,$attr['deeplink'],$attr['market']);
	} */
	$product_url = '';
	if(isset($deeplink) && $deeplink!=''){

	    if($market=='amazon'){ 
			$query = parse_url($url, PHP_URL_QUERY);
			if(!empty($query)){
				$product_url = $url.'&'.$deeplink.'';
			}else{
				$product_url = $url.'?'.$deeplink.'';
			}
		}elseif($market=='ebay' ){
			$product_url = $url.$deeplink;
			/* $query = parse_url($url, PHP_URL_QUERY);
			if(!empty($query)){
				$product_url = $url.'&'.$deeplink.'';
			}else{
				$product_url = $url.'?'.$deeplink.'';
			} */			
		}elseif($market=='aliexpress'){
			$product_url = '';
			$product_url = $deeplink.'?ulp='.urlencode($url);
		}elseif($market=='bestbuy') {
			$product_url = '';
			$url2 = '?prodsku='.$sku.'&u='.$url;
			$product_url = $deeplink.''.$url2;
		}elseif($market=='walmart') {
			$product_url = '';
			$url2 ='';
			$url2 = '&prodsku='.$sku.'&u=https://www.walmart.com/ip/'.$sku;
			$product_url = $deeplink.''.$url2;
		}
    }else{
		$product_url=$url;
	} 
    return $product_url;
}

function encodeURIComponent($str) {
    $revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
    return strtr(rawurlencode($str), $revert);
}