<?php 
function ionic_ecommerce_admin_notices(){
	
	include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	
	if ( is_plugin_active('woocommerce/woocommerce.php') == false ) {
		echo '<div id="message" class="error fade"><p style="line-height: 150%">';
		_e('<strong>VC App Settings & Checkout</strong></a> requires the WooCommerce to be activated. Please <a target="_blank" href="plugin-install.php?tab=plugin-information&plugin=woocommerce&TB_iframe=true&width=600&height=550">install / Activate</a> first.', 'VC App');
		echo '</p></div>';
		return;
	}	
	
	if ( is_plugin_active( 'json-api/json-api.php' ) == false ) {
		echo '<div id="message" class="error fade"><p style="line-height: 150%">';
		_e('<strong>VC App Settings & Checkout</strong></a> requires the JSON API plugin to be activated. Please <a target="_blank" href="plugin-install.php?tab=plugin-information&plugin=json-api&TB_iframe=true&width=600&height=550">install / Activate </a> first.', 'VC App');
		echo '</p></div>';		
		return;
	}
}

function ionic_ecommerce_add_app_admin_menu() {
	add_menu_page( __( 'Ionic Ecommerce Banner Settings' ), __( 'Ionic Ecommerce Banner Settings' ), 'manage_options', 'banner-settings', 'ionic_ecommerce_app_banner_setting','' , 62  );	
}

function ionic_ecommerce_add_app_admin_menu_activation() {
	add_menu_page( __( 'Ionic Ecommerce Activate' ), __( 'Ionic Ecommerce Activate' ), 'manage_options', 'ionic-ecommerce-activate', 'ionic_ecommerce_activate','' , 62  );	
}

function curl( $url ) { 
		if ( empty( $url) ) return false;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_VERBOSE, true);
		$result_cal = curl_exec($ch);
		curl_close($ch);
		return $result_cal;		
}

function ionic_ecommerce_activate(){ ?>
<div class="wrap">
<style>
		#poststuff .inside {
			margin: 6px 0 0;
		}
		
		#newImageButton { background:#ccc; box-shadow:none; }
			
		.stuffbox label {
			line-height: 20px;
			font-size: 12px;
			text-align: left;
			padding: 0 15px 10px;
			font-weight: 700;
		}
		
		.inside input,
		.inside select {
			width: 50%;
			height: 35px;
			box-shadow: none;
			background: #fff;
		}
		
		#poststuff {
			padding-top: 10px;
			width: 100%;
			padding: 0 10px;
		}
		
		#poststuff .stuffbox>h3,
		#poststuff h2,
		#poststuff h3.hndle {
			font-size: 14px;
			padding: 8px 12px;
			margin: 0;
			line-height: 1.4;
		}
		
		.postbox,
		.stuffbox {
			margin-bottom: 20px;
			padding: 0;
			line-height: 1;
			background: #fff;
			padding: 8px 0 0 0;
		}
		
		#poststuff {
			width: 70%;
			background: #fff;
			padding: 20px;
		}
		
		#poststuff h4 {
			font-size: 18px;
		}
		
		.msg {
			color: #ff0000;
			text-align: center;
			font-size: 12px;
			line-height: 20px;
		}
		.bps {padding:2px 20px!important; height:34px!important;}
		@media only screen and (max-device-width : 768px) {
			#poststuff {width: 94%; padding: 20px 3%;}
			.inside input, .inside select { width: 100%; }				
		}	
		
		
	</style>
  
  <?php	
  		$api_url = 'http://api.themes-coder.com';
  		if(isset($_REQUEST['purchase_code']) && $_REQUEST['purchase_code'] != ""){			
			$purchase_code    = $_REQUEST['purchase_code'];	
			$current_site_url =	get_site_url();		
			$purchase_code 	 = urlencode( $purchase_code );
			$url = $api_url . '/api.php?code=' . $purchase_code."&url=".$current_site_url;			
	    	$response = 	curl( $url );
			$response = json_decode( $response, true );
			//var_dump($response);
			
			if(isset($response['error']) && $response['error'] == '404'){
				 $msg = $response['description'];
			}elseif(isset($response['purchase_code']) && $response['purchase_count'] >= 1 && $response['buyer'] != "" ){ 
				update_option('ionic_ecommerce_purchase_code' , $purchase_code);
				update_option('ionic_ecommerce_plugin_active' , 'active');
				update_option('ionic_ecommerce_purchase_detail' , $response);
				echo "<script>window.location.href='admin.php?page=ionic-ecommerce'</script>";
			}			
		}  
  
  		if(get_option('ionic_ecommerce_plugin_active') == "active" && get_option('ionic_ecommerce_purchase_code') != ""){			
			$ionic_ecommerce_purchase_code	= get_option('ionic_ecommerce_purchase_code');
			$ionic_ecommerce_plugin_active  = get_option('ionic_ecommerce_plugin_active');
		}			
  ?>
  <h1 class="wp-heading-inline">Plugin Validation</h1>
  <?php if(isset($msg) && $msg != ""){ ?>
  <div id="message" class="notice notice-error is-dismissible">
    <p><?php echo $msg; ?>. </p>    
  </div>
  <?php } ?>
  <div class="poststuff" id="poststuff">
    <form method="post" action="" id="plugin_validate" name="plugin_validate">    
      <div class="stuffbox">
        <label for="name" class="col-sm-2 col-md-3 control-label">Please Enter Purchase Code </label>
        <div class="inside">
          <input required class="form-control field-validate" id="purchase_code" name="purchase_code" type="text" value="<?php if(isset($ionic_ecommerce_purchase_code) && $ionic_ecommerce_purchase_code != ""){ echo $ionic_ecommerce_purchase_code; } ?>">
          <br />
          <span class="help-block" style="font-weight: normal;font-size: 11px;margin-bottom: 0;">Enter Product Purchase Code.</span> </div>
      </div>      
      <?php wp_nonce_field('action_settings_add_edit','add_edit_nonce'); ?>
      <div>
      <?php if(!isset($ionic_ecommerce_purchase_code) && $ionic_ecommerce_purchase_code == ""){ ?>
        <button type="submit" name="submit" id="btnsave" value="Submit" class="button-primary bps">Submit</button>
        &nbsp;&nbsp;
        <button type="button" name="button" onclick="location.href='admin.php?page=ionic-ecommerce-activate'" id="btnsave" value="true" class="button-primary bps">Cancel</button>
        <?php } else {?>
        	 <a href="admin.php?page=ionic-ecommerce"  id="btnsave"  class="button-primary">Settings</a>
        <?php } ?>
      </div>    
    </form>
  </div>
</div>
<?php }


function ionic_ecommerce_wp_rest_allow_all_cors() {
 remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
 add_filter( 'rest_pre_serve_request', function( $value ) {
 header("Cache-Control: no-cache, must-revalidate");
 header('Access-Control-Allow-Origin:  *');
 header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, DELETE');
 header('Access-Control-Allow-Headers:  Content-Type, X-Auth-Token, Origin, Authorization');
	 
		return $value;
	});
}

function ionic_ecommerce_my_app_hook() {
   if(isset($_GET['mob']) && $_GET['mob'] == true ){?>
		<style>
			.woocs_auto_switcher {display:none;}
        <?php if(isset($_GET['platform']) && $_GET['platform'] == 'ios' ) {
         echo "body{padding-top:50px;}";
        }
         global $wpdb;
         if(get_option( 'ionic_ecommerce_app_settings')) {
         $all_settings = get_option( 'ionic_ecommerce_app_settings');
        }
         $custom_css_for_checkout = $all_settings['custom_css_for_checkout'];
         echo $custom_css_for_checkout;
         ?>
        </style>
        <?php if(isset($_GET['one_page']) && $_GET['one_page'] == 2 ){?>
        <script>  
                jQuery( document ).ready( readyFn );
                function readyFn( jQuery ) { jQuery("#place_order").trigger('click'); }
            </script>
        <style>
        body , .checkout {
            display: none;
        }
        </style>
<?php } } 	add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true'); } 


function my_show_extra_profile_fields_for_wcvendor( $user ) { 
$user_meta = get_userdata($user->ID);
$user_roles = $user_meta->roles;

if ( in_array( 'vendor', $user_roles, true ) && current_user_can('administrator')  ) { ?>
    <h3>Extra profile information</h3>
	<table class="form-table">
		<tr>
			<th><label for="twitter">Mark as Featured Vendor</label></th>
			<td> 
				<input type="checkbox" name="feature_wcvendor" id="feature_wcvendor" value="yes" <?php if( get_the_author_meta( 'feature_wcvendor', $user->ID ) == 'yes' ){ ?> checked <?php } ?> />Mark as featured vendor <br />
				<span class="description">This vendor will be marked as a featured vendor.</span>
			</td>
		</tr>
	</table>
<?php } }



function my_save_extra_profile_fields_for_wcvendor( $user_id ) {
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;

if ( in_array( 'vendor', $user_roles, true ) && current_user_can('administrator')  ) { 
	if ( !current_user_can( 'edit_user', $user_id ) )
		return false;
	update_usermeta( $user_id, 'feature_wcvendor', $_POST['feature_wcvendor'] );
}}