<?php
global $message;
$message = '';
function twitter_authenticate($force = false) {
		$api_key = get_option( 'TWITTER_CONSUMER_KEY' );
		$api_secret = get_option( 'TWITTER_CONSUMER_SECRET' );
		$token = get_option( 'TWITTER_BEARER_TOKEN' );
		$cache_limit_time = get_option( 'TWITTER_CACHE_LIMIT_TIME' );
		$tweet_num_post = get_option( 'TWITTER_NUM_POST' );
		$access_token = get_option( 'TWITTER_ACCESS_TOKEN' );
		$access_token_secret = get_option( 'ACCESS_TOKEN_SECRET' );
		$twitter_datetime_formate = get_option( 'twitter_datetime_formate' );
		
		
		
		if($api_key && $api_secret && ( !$token || $force )) {
			$bearer_token_credential = $api_key . ':' . $api_secret;
			$credentials = base64_encode($bearer_token_credential);
			
			$args = array(
				'method' => 'POST',
				'httpversion' => '1.1',
				'blocking' => true,
				'headers' => array( 
					'Authorization' => 'Basic ' . $credentials,
					'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'
				),
				'body' => array( 'grant_type' => 'client_credentials' )
			);
			$keys = new stdClass();
			add_filter('https_ssl_verify', '__return_false');
			$response = wp_remote_post( 'https://api.twitter.com/oauth2/token', $args );
			if(!is_wp_error($response)){
				$keys = array();
				$keys = json_decode($response['body']);
				if($keys) {
					global $message;
					
						//update_option( 'TWITTER_BEARER_TOKEN', $keys->{'access_token'} );
						echo $message = "<div class='form-msgs'><div class='to-notif success-box'><span class='tick'>&nbsp;</span><p>Twitter API Settings Successfully Saved</p><a class='close-it' onclick='tab_close()'>&nbsp;</a></div></div>";
					
					echo $message = "<script>slideout();</script>";
				}
			}else{
				echo $message = "<div class='form-msgs'><div class='to-notif error-box'><span class='error'>&nbsp;</span><p>".$response->errors['http_failure'][0]."</p><a class='close-it' onclick='tab_close()'>&nbsp;</a></div></div>";
				echo $message = "<script>slideout();</script>";
			}
		}
	}

function twitter_settings() {
	$twitter_datetime_formate = '';
?>
 <script type="text/javascript">
	jQuery().ready(function($) {
			var container = $('div.container');
			// validate the form when it is submitted
			var validator = $("#frm1").validate({
				errorContainer: container,
				errorLabelContainer: $(container),
				errorElement:'span',
				errorClass:'ele-error',				
				meta: "validate"
			});
		});
	</script>
<div class="theme-wrap fullwidth">
  <?php include "theme_leftnav.php";?>
  <!-- Right Column Start -->
  <div class="col2 left"> 
    <!-- Header Start -->
    <div class="wrap-header">
      <h4 class="bold">Twitter API Settings</h4>
      <div class="clear"></div>
    </div>
    <!-- Header End --> 
    <!-- Content Section Start -->
    <div class="tab-section">
      <div class="tab_menu_container">
        <ul id="tab_menu">
          <li><a href="#uploadlang" class="" rel="tab-uploadlang"><span>Twitter Widget Settings</span></a></li>
        </ul>
        <div class="clear"></div>
      </div>
      <div class="tab_container">
        <div class="tab_container_in">
          <div id="tab-uploadlang" class="tab-list">
            <div class="option-sec">
              <div class="opt-conts">
              <?php 
			  if ( $_SERVER["REQUEST_METHOD"] == "POST" ){			
					update_option( 'TWITTER_CONSUMER_KEY', $_POST['consumer_key'] );
					update_option( 'TWITTER_CONSUMER_SECRET', $_POST['consumer_secret'] );
					update_option( 'TWITTER_ACCESS_TOKEN', $_POST['access_token'] );
					update_option( 'TWITTER_SCREEN_NAME', $_POST['screen_name'] );
					update_option( 'TWITTER_CACHE_LIMIT_TIME', $_POST['cache_limit_time'] );
					update_option( 'TWITTER_NUM_POST', $_POST['tweet_num_post'] );
					update_option( 'ACCESS_TOKEN_SECRET', $_POST['access_token_secret'] );
					update_option( 'twitter_datetime_formate', $_POST['twitter_datetime_formate'] );
					
					twitter_authenticate(true);
				}
				 
			  ?>
                <form name="options" id="frm1" method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
                
                  <ul class="form-elements">
                    <li class="to-label">
                      <label for="screen_name">Twitter Username : </label>
                    </li>
                    <li class="to-field">
                      <input type="text" name="screen_name" value="<?php echo get_option( 'TWITTER_SCREEN_NAME', '' ); ?>"  class="{validate:{required:true}}">
                    </li>
                    <li class="to-desc">
                      <p>Please type your twitter username here.</p>
                    </li>
                  </ul>
                  <ul class="form-elements">
                    <li class="to-label">
                      <label for="screen_name"><?php _e('Cache Time Limit',CSDOMAIN)?> </label>
                    </li>
                    <li class="to-field">
                      <input type="text" name="cache_limit_time" value="<?php echo get_option( 'TWITTER_CACHE_LIMIT_TIME', '' ); ?>" >
                    </li>
                    <li class="to-desc">
                      <p><?php _e('Please type your Cache Time Limit here',CSDOMAIN)?></p>
                    </li>
                  </ul>
                  <ul class="form-elements">
                    <li class="to-label">
                      <label for="cache_limit_time"><?php _e('Number of tweet',CSDOMAIN)?> </label>
                    </li>
                    <li class="to-field">
                      <input type="text" name="tweet_num_post" value="<?php echo get_option( 'TWITTER_NUM_POST', '' ); ?>" >
                    </li>
                    <li class="to-desc">
                      <p><?php _e('Please enter Number of tweet here',CSDOMAIN)?></p>
                    </li>
                  </ul>
                  <ul class="form-elements">
              <li class="to-label">
                <label for="tweet_num_post"><?php _e('Date Time Formate',CSDOMAIN)?></label>
              </li>
              <li class="to-field">
                <select name="twitter_datetime_formate" class="dropdown" id="cs_twitter_datetime_formate" onchange="javascript:home_breadcrumb_toggle(this.value)">
                  <option <?php if(get_option( 'twitter_datetime_formate', '' ) == "default"){echo "selected";}?> value="default" ><?php _e('Displays November 06 2012','Faith')?></option>
                  <option <?php if(get_option( 'twitter_datetime_formate', '' ) == "eng_suff"){echo "selected";}?> value="eng_suff" ><?php _e('Displays 6th November','Faith')?></option>
                  <option <?php if(get_option( 'twitter_datetime_formate', '' ) == "ddmm"){echo "selected";}?> value="ddmm" ><?php _e('Displays 06 Nov','Faith')?></option>
                  <option <?php if(get_option( 'twitter_datetime_formate', '' ) == "ddmmyy"){echo "selected";}?> value="ddmmyy" ><?php _e('Displays 06 Nov 2012','Faith')?></option>
                  <option <?php if(get_option( 'twitter_datetime_formate', '' ) == "full_date"){echo "selected";}?> value="full_date" ><?php _e('Displays Tues 06 Nov 2012','Faith')?></option>
                  <option <?php if(get_option( 'twitter_datetime_formate', '' ) == "time_since"){echo "selected";}?> value="time_since" ><?php _e('Displays in hours, minutes etc','Faith')?></option>
                </select>
              </li>
               <li class="to-desc">
                      <p><?php _e('Please choose your Date Time Formate here',CSDOMAIN)?></p>
                    </li>
            </ul>
                  <ul class="form-elements">
                    <li class="to-label">
                      <label for="consumer_key">Consumer Key : </label>
                    </li>
                    <li class="to-field">
                      <input type="text" name="consumer_key" value="<?php echo get_option( 'TWITTER_CONSUMER_KEY', '' ); ?>"  class="{validate:{required:true}}">
                    </li>
                    <li class="to-desc">
                      <p>Please type your twitter Consumer Key here.</p>
                    </li>
                  </ul>
                  <ul class="form-elements">
                    <li class="to-label">
                      <label for="consumer_secret">Consumer Secret : </label>
                    </li>
                    <li class="to-field">
                      <input type="text" name="consumer_secret" value="<?php echo get_option( 'TWITTER_CONSUMER_SECRET', '' ); ?>" class="{validate:{required:true}}">
                    </li>
                     <li class="to-desc">
                      <p>Please type your twitter Consumer Secret here.</p>
                    </li>
                  </ul>
                   <ul class="form-elements">
                    <li class="to-label">
                      <label for="consumer_secret"><?php _e('Access Token', CSDOMAIN); ?> </label>
                    </li>
                    <li class="to-field">
                      <input type="text" name="access_token" value="<?php echo get_option( 'TWITTER_ACCESS_TOKEN', '' ); ?>" >
                    </li>
                     <li class="to-desc">
                      <p><?php _e('Insert Twitter Access Token for permissions. When you create your Twitter App, you get this Token
', CSDOMAIN); ?></p>
                    </li>
                  </ul>
                 <!-- <ul class="form-elements">
                    <li class="to-label">
                      <label for="bearer_token">Bearer Token: </label>
                    </li>
                    <li class="to-field">
                      <input type="text" disabled value="<?php echo get_option( 'TWITTER_BEARER_TOKEN', '' ); ?>" >
                    </li>
                    <li class="to-desc">
                      <p>Bearer Token will automatically generated leave it blank.</p>
                    </li>
                  </ul>-->
                  <ul class="form-elements">
                    <li class="to-label">
                      <label for="access_token_secret">Access Token Secret: </label>
                    </li>
                    <li class="to-field">
                      <input type="text" name="access_token_secret" id="access_token_secret"  value="<?php echo get_option( 'ACCESS_TOKEN_SECRET', '' ); ?>" >
                    </li>
                    <li class="to-desc">
                      <p>Insert Twitter Access Token Secret here. When you create your Twitter App, you get this Token.</p>
                    </li>
                  </ul>
                  <ul class="form-elements">
                    <li class="to-label"> </li>
                    <li class="to-field">
                      <input class="button-primary" id="gs_tab_save1"  type="submit" name="save" value="save" />
                    </li>
                    <li class="to-desc">
                    <p>You can sign up for a API.</p>
                    </li>
                  </ul>
                </form>
                </ul>
              </div>
            </div>
          </div>
          <!-- Upload Cufon End -->
          <div class="clear"></div>
        </div>
      </div>
      <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <!-- Content Section End --> 
  </div>
  <div class="clear"></div>
  <!-- Right Column End --> 
</div>
<?php } ?>
