/**
  * 
  * @see            https://dev.twitter.com/docs/api/1.1/get/application/rate_limit_status
  */
 public function getStatus()
 {
     // Return the cached response if available.
     $_sCacheID = FetchTweets_Commons::TransientPrefix . '_' . md5(serialize(array($this->sConsumerKey, $this->sConsumerSecret, $this->sAccessToken, $this->sAccessSecret)));
     $_vData = FetchTweets_WPUtilities::getTransient($_sCacheID);
     if (false !== $_vData) {
         return $_vData;
     }
     // Perform the requests.
     $_oConnect = new FetchTweets_TwitterOAuth($this->sConsumerKey, $this->sConsumerSecret, $this->sAccessToken, $this->sAccessSecret);
     $_aUser = $_oConnect->get('account/verify_credentials');
     // If the user id could not be retrieved, it means it failed.
     if (!isset($_aUser['id']) || !$_aUser['id']) {
         return array();
     }
     // Otherwise, it is okay. Retrieve the current status.
     $_aStatusKeys = apply_filters('fetch_tweets_filter_request_rate_limit_status_keys', array('statuses', 'search', 'lists'));
     // keys can be added such as 'help', 'users' etc
     $_aStatus = $_oConnect->get('https://api.twitter.com/1.1/application/rate_limit_status.json?resources=' . implode(',', $_aStatusKeys));
     // Set the cache.
     $_aData = is_array($_aStatus) ? $_aUser + $_aStatus : $_aUser;
     FetchTweets_WPUtilities::setTransient($_sCacheID, $_aData, 60);
     // stores the cache only for 60 seconds.
     return $_aData;
 }
 /**
  * Redirects to the twitter to get authenticated.
  * 
  * @since       1.3.0
  * @since       2.4.5       Moved from the admin page class.
  * @remark      This is redirected from the "Connect to Twitter" button.
  */
 private function _redirect()
 {
     /* Build TwitterOAuth object with client credentials. */
     $_oConnect = new FetchTweets_TwitterOAuth(FetchTweets_Commons::ConsumerKey, FetchTweets_Commons::ConsumerSecret);
     /* Get temporary credentials - Requesting authentication tokens, the parameter is the URL we will be redirected to. */
     $_aRequestToken = $_oConnect->getRequestToken(add_query_arg(array('post_type' => 'fetch_tweets', 'page' => 'fetch_tweets_settings', 'tab' => 'twitter_callback'), admin_url($GLOBALS['pagenow'])));
     /* Save temporary credentials to transient. */
     $_aTemporaryTokens = array();
     $_aTemporaryTokens['oauth_token'] = $_aRequestToken['oauth_token'];
     $_aTemporaryTokens['oauth_token_secret'] = $_aRequestToken['oauth_token_secret'];
     FetchTweets_WPUtilities::setTransient(FetchTweets_Commons::TransientPrefix . '_oauth', $_aTemporaryTokens, 60 * 10);
     // 10 minutes
     /* If last connection failed don't display authorization link. */
     switch ($_oConnect->http_code) {
         case 200:
             /* Build authorize URL and redirect user to Twitter. */
             wp_redirect($_oConnect->getAuthorizeURL($_aTemporaryTokens['oauth_token']));
             // goes to twitter.com
             break;
         default:
             /* Show notification if something went wrong. */
             die(__('Could not connect to Twitter. Refresh the page or try again later.', 'fetch-tweets'));
     }
     exit;
 }
 /**
  * Receives the callback from Twitter authentication and saves the access token.
  * 
  * @remark      This method is triggered when the user get redirected back to the admin page
  * @since       1.3.0       
  * @since       2.4.5       Moved from the admin page class.
  */
 private function _handleAuthenticationCallback()
 {
     /* If the oauth_token is old redirect to the authentication page. */
     $_aTemporaryTokens = FetchTweets_WPUtilities::getTransient(FetchTweets_Commons::TransientPrefix . '_oauth');
     if (false === $_aTemporaryTokens || !isset($_aTemporaryTokens['oauth_token'], $_aTemporaryTokens['oauth_token_secret'])) {
         exit(wp_redirect(add_query_arg(array('post_type' => 'fetch_tweets', 'page' => 'fetch_tweets_settings', 'tab' => 'authentication'), admin_url($GLOBALS['pagenow']))));
     }
     $_oOption =& $GLOBALS['oFetchTweets_Option'];
     /* Create TwitterOAuth object with app key/secret and token key/secret from default phase */
     $_oConnect = new FetchTweets_TwitterOAuth(FetchTweets_Commons::ConsumerKey, FetchTweets_Commons::ConsumerSecret, $_aTemporaryTokens['oauth_token'], $_aTemporaryTokens['oauth_token_secret']);
     /* Request access tokens from twitter */
     $_aAccessTokens = $_oConnect->getAccessToken($_REQUEST['oauth_verifier']);
     /* $_aAccessTokens Looks like this
        'oauth_token' => string 'asxxx-sxxx...' (length=50)
        'oauth_token_secret' => string 'xxx....' (length=41)
        'user_id' => string '132.....' (length=10)
        'screen_name' => string 'my_screen_name' (length=9) */
     /* Save the access tokens. Normally these would be saved in a database for future use. */
     $_oOption->saveCredentials(array('access_token' => $_aAccessTokens['oauth_token'], 'access_secret' => $_aAccessTokens['oauth_token_secret'], 'screen_name' => $_aAccessTokens['screen_name'], 'user_id' => $_aAccessTokens['user_id'], 'is_connected' => true, 'connect_method' => 'oauth'));
     /* If HTTP response is 200 continue otherwise send to connect page to retry */
     if (200 == $_oConnect->http_code) {
         /* The user has been verified */
         $_sRediretURL = add_query_arg(array('post_type' => 'fetch_tweets', 'page' => 'fetch_tweets_settings', 'tab' => 'twitter_connect'), admin_url($GLOBALS['pagenow']));
     } else {
         /* Save HTTP status for error dialogue on authentication page.*/
         // Let the user set authentication keys manually
         $_sRediretURL = add_query_arg(array('post_type' => 'fetch_tweets', 'page' => 'fetch_tweets_settings', 'tab' => 'authentication'), admin_url($GLOBALS['pagenow']));
     }
     exit(wp_redirect($_sRediretURL));
 }
 /**
  * Checks the API credential is valid or not.
  * 	 
  * @since			1.3.0
  * @return			array			the retrieved data.
  * @remark			The returned data is a merged result of 'account/verify_credientials' and 'rate_limit_status'.
  */
 protected function verifyCrediential($strConsumerKey, $strConsumerSecret, $strAccessToken, $strAccessSecret)
 {
     // Return the cached response if available.
     $strCachID = FetchTweets_Commons::TransientPrefix . '_' . md5(serialize(array($strConsumerKey, $strConsumerSecret, $strAccessToken, $strAccessSecret)));
     $vData = get_transient($strCachID);
     if ($vData !== false) {
         return $vData;
     }
     // Perform the requests.
     $oTwitterOAuth = new FetchTweets_TwitterOAuth($strConsumerKey, $strConsumerSecret, $strAccessToken, $strAccessSecret);
     $arrUser = $oTwitterOAuth->get('account/verify_credentials');
     // If the user id could not be retrieved, it means it failed.
     if (!isset($arrUser['id']) || !$arrUser['id']) {
         return array();
     }
     // Otherwise, it is okay. Retrieve the current status.
     $arrStatus = $oTwitterOAuth->get('https://api.twitter.com/1.1/application/rate_limit_status.json?resources=help,users,search,statuses');
     // Set the cache.
     $arrData = is_array($arrStatus) ? $arrUser + $arrStatus : $arrUser;
     set_transient($strCachID, $arrData, 60 * 2);
     // stores the cache only for 120 seconds. It is allowed 15 requests in 15 minutes.
     return $arrData;
 }