<?php
// function that runs when shortcode is called
function show_weblinkform_shortcode() { 
	if( ! get_current_user_id())
{
	echo "Please login to view the content";
	return;
}

 ?>
<form class="form-horizontal" method = "post" name="weblinkform" onsubmit="return validateForm()">
<fieldset>

<!-- Form Name -->
<legend>Add Link</legend>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="title">Story Title</label>  
  <div class="col-md-5">
  <input id="title" name="title" type="text" placeholder="Add Story Title" class="form-control input-md" required="">
    
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="title">Story Link</label>  
  <div class="col-md-5">
  <input id="link" name="link" type="text" placeholder="Add Story Link" class="form-control input-md" required="">
    
  </div>
</div>

<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="category">Select Category</label>
  <div class="col-md-5">
    <select id="category" name="category" class="form-control">
		<option value="">Please Select</option>
	<?php 
	//$terms = wp_get_post_terms( $post->ID, array( 'category_weblink') ); 
	$terms = get_terms( array(
    'taxonomy' => 'category_weblink',
    'hide_empty' => false
) );	
						foreach ( $terms as $term ) : 
							echo  '<option value=" ' . $term->term_id . '">' . $term->name . '</option>' ;
						endforeach; ?>
    </select>
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="tags">Tags</label>  
  <div class="col-md-5">
  <input id="tags" name="tags" type="text" placeholder="Add Tags separeted by comma(,)" class="form-control input-md">
    
  </div>
</div>

<!-- Textarea -->
<div class="form-group">
  <label class="col-md-4 control-label" for="description">Description</label>
  <div class="col-md-4">                     
    <textarea class="form-control" id="description" name="description"></textarea>
  </div>
</div>

<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label" for="submitlink"></label>
  <div class="col-md-4">
    <button id="submitlink" name="submitlink" class="btn btn-primary">Submit</button>
  </div>
</div>
<!-- We will Add JS Now -->
<script>
function validateForm() {
  var weblink = document.forms["weblinkform"]["link"].value;
  var weblinkcat = document.forms["weblinkform"]["category"].value;
  regexp =  /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
        if (! regexp.test(weblink))
		{
			alert("Please Check The link you provided");
			return false;
		}
		if(weblinkcat == "")
		{
			alert("Please Select Category");
			return false;
		}
		return true;
        
}</script>
<?php wp_nonce_field( 'cpt_nonce_action', 'cpt_nonce_field' ); ?>

</fieldset>
</form>
<?php

 // Now saving the form
	if (isset( $_POST['cpt_nonce_field'] ) && wp_verify_nonce( $_POST['cpt_nonce_field'], 'cpt_nonce_action' ) ) 
	{
		
		//first check if tag term exist or not. If not then create it. 
		$alltags = explode(",",$_POST['tags']);
		$alltags = array_map('strtolower', $alltags);
		$alltagid = array();
		for($i=0;$i<count($alltags);$i++)
		{
			if($check_term = term_exists( $alltags[$i], "tag_weblink" ) )
				$alltagid[ ] = $check_term;
			else
			{
				 $newterm = wp_insert_term( $alltags[$i], "tag_weblink");
				 $alltagid[ ] = $newterm['term_id'];
			}
		}
		// create post object with the form values

		$my_weblink_args = array(

		'post_title'  => $_POST['title'],

		'post_status'  => 'publish',

		'post_type' => 'weblink'
		);
		$weblink_post_id = wp_insert_post( $my_weblink_args);
		update_post_meta( $weblink_post_id, 'weblink_url', $_POST['link'] ); 
		update_post_meta( $weblink_post_id, 'weblink_desc', $_POST['description'] ); 
		wp_set_post_terms( $weblink_post_id, $_POST['tags'], 'tag_weblink' );
		wp_set_post_terms( $weblink_post_id, $_POST['category'], 'category_weblink' );
		if($weblink_post_id)
		{
			echo "<h2 style='color:green;margin-top : 0;'>Link Submitted Successfully.</h2>";
		}
		else
				echo "There is some issues, Please Try Again or Contact Site Administrator";
	}
}



// register shortcode
add_shortcode('show_weblinkform', 'show_weblinkform_shortcode');