private function _getScreenName($aCredentials) { /* Create TwitteroAuth object with app key/secret and token key/secret from default phase */ $_oConnect = new FetchTweets_TwitterAPI_Verification($aCredentials['consumer_key'], $aCredentials['consumer_secret'], $aCredentials['access_token'], $aCredentials['access_secret']); /* Request access tokens from twitter */ $_aResponse = $_oConnect->getStatus(); return isset($_aResponse['screen_name']) ? $_aResponse['screen_name'] : null; }
/** * Triggered when the manual keys are set and submitted. * * @since 2 * @since 2.4.5 Moved from the admin page class. * @remark valiudation_{class name}_{section id} */ public function replyToValidate($aInput, $aOldInput, $oFactory, $aSubmitInfo) { // Sanitize $aInput['consumer_key'] = trim($aInput['consumer_key']); $aInput['consumer_secret'] = trim($aInput['consumer_secret']); $aInput['access_token'] = trim($aInput['access_token']); $aInput['access_secret'] = trim($aInput['access_secret']); // Check the connection $_oConnect = new FetchTweets_TwitterAPI_Verification($aInput['consumer_key'], $aInput['consumer_secret'], $aInput['access_token'], $aInput['access_secret']); $_aStatus = $_oConnect->getStatus(); // If it's connected, add the connection status if (isset($_aStatus['id_str'])) { $aInput['user_id'] = $_aStatus['id_str']; $aInput['screen_name'] = $_aStatus['screen_name']; $aInput['is_connected'] = true; $aInput['connect_method'] = 'manual'; } else { $aInput['is_connected'] = false; $aInput['connect_method'] = 'manual'; } do_action('fetch_tweets_action_updated_credentials', $aInput); return $aInput; }
/** * Performs the API request and sets the cache. * * @access public * @remark The scope is public since the cache renewal event also uses it. * @param string $sRawRequestURI The request URI that MAY contain embedded access keys in the query keys. * @param string $sArrayKey The array key in the response array. For the search API request, a certain key is need to be set. * @param array $aRateLimitKeys The representation of dimensional keys for the rate limit status-resource array. */ public function setAPIGETRequestCache($sRawRequestURI, $sArrayKey = null, $aRateLimitKeys = array()) { /** * Stores requested URIs to prevent multiple requests per a page load which causes the rate limit to exceed. */ static $_aRequestedURIs = array(); $sRawRequestURI = trim($sRawRequestURI); $_sRequestURI = $this->_sanitizeRequstURI($sRawRequestURI); // Check if it has been already requested. If so return an error. if (in_array($_sRequestURI, $_aRequestedURIs)) { return array('error' => __('Excessive requests have been made. Please reload the page.', 'fetch-tweets')); } $_aRequestedURIs[$_sRequestURI] = $_sRequestURI; // Check if a custom access keys are set. $_aAccessKeys = $this->_getAccessKeysFromQueryURI($sRawRequestURI); $_oOriginalTwitterOAuth = $this->oTwitterOAuth; if (!empty($_aAccessKeys)) { $this->oTwitterOAuth = new FetchTweets_TwitterOAuth($_aAccessKeys['consumer_key'], $_aAccessKeys['consumer_secret'], $_aAccessKeys['access_token'], $_aAccessKeys['access_secret']); } // Check the rate limit. if (!empty($aRateLimitKeys)) { $_oRateLimit = new FetchTweets_TwitterAPI_Verification(isset($_aAccessKeys['consumer_key']) ? $_aAccessKeys['consumer_key'] : $this->_aApplicationKeys['consumer_key'], isset($_aAccessKeys['consumer_secret']) ? $_aAccessKeys['consumer_secret'] : $this->_aApplicationKeys['consumer_secret'], isset($_aAccessKeys['access_token']) ? $_aAccessKeys['access_token'] : $this->_aApplicationKeys['access_token'], isset($_aAccessKeys['access_secret']) ? $_aAccessKeys['access_secret'] : $this->_aApplicationKeys['access_secret']); $_iRemaining = $_oRateLimit->getRemaining($aRateLimitKeys); if (!$_iRemaining) { return array('error' => __('The number of API requests exceeded the rate limit. Please try it later.', 'fetch-tweets')); } } // Perform the API request. $_aTweets = $this->oTwitterOAuth->get($_sRequestURI); // Restore the original Twitter oAuth object. $this->oTwitterOAuth = $_oOriginalTwitterOAuth; // If the array key is specified, return the contents of the key element. Otherwise, return the retrieved array intact. if (!is_null($sArrayKey) && isset($_aTweets[$sArrayKey])) { $_aTweets = $_aTweets[$sArrayKey]; } // If empty, return an empty array. if (empty($_aTweets)) { return array(); } // If the result is not an array, something went wrong. if (!is_array($_aTweets)) { return (array) $_aTweets; } // If an error occurs, do not set the cache. if (!$this->oOption->aOptions['cache_settings']['cache_for_errors']) { if (isset($_aTweets['errors'][0]['message'], $_aTweets['errors'][0]['code'])) { $_aTweets['errors'][0]['message'] .= "<!-- Request URI: {$_sRequestURI} -->"; return (array) $_aTweets; } } // Save the cache $this->setTransient($_sRequestURI, $_aTweets); return (array) $_aTweets; }
/** * Checks if the plugin is connected to Twitter. * * @since 2 */ public function isConnected() { // The keys are manually set if ($this->isAuthKeysManuallySet()) { if (isset($this->aOptions['authentication_keys']['is_connected'])) { return $this->aOptions['authentication_keys']['is_connected']; } $_oConnect = new FetchTweets_TwitterAPI_Verification($this->aOptions['authentication_keys']['consumer_key'], $this->aOptions['authentication_keys']['consumer_secret'], $this->aOptions['authentication_keys']['access_token'], $this->aOptions['authentication_keys']['access_secret']); $_aStatus = $_oConnect->getStatus(); return isset($_aStatus['screen_name']); } // The keys are automatically retrieved if (isset($this->aOptions['twitter_connect']['is_connected'])) { return $this->aOptions['twitter_connect']['is_connected']; } $_oConnect = new FetchTweets_TwitterAPI_Verification(FetchTweets_Commons::ConsumerKey, FetchTweets_Commons::ConsumerSecret, $this->aOptions['twitter_connect']['access_token'], $this->aOptions['twitter_connect']['access_secret']); $_aStatus = $_oConnect->getStatus(); return isset($_aStatus['screen_name']); }
/** * Checks the API credential is valid or not. * * @since 1.3.0 * @since 2.4.5 Moved from the admin page class. * @return array the retrieved data. * @remark The returned data is a merged result of 'account/verify_credientials' and 'rate_limit_status'. */ private function _getAuthenticationStatus($sConsumerKey, $sConsumerSecret, $sAccessToken, $sAccessSecret) { $_oTwitterOAuth_Verification = new FetchTweets_TwitterAPI_Verification($sConsumerKey, $sConsumerSecret, $sAccessToken, $sAccessSecret); return $_oTwitterOAuth_Verification->getStatus(); }