/** * Post the title of Question or Answer to Twitter * Usually this method is called as a shutdown_function * @todo if space allows add "prefixes" to Tweets * Prefixes will be strings, in translation... * * @todo if space allows also add "via @ourname" to tweet if the * value of TWITTER_USERNAME exists if setting * * @param \Lampcms\Twitter $oTwitter * @param \Lampcms\Bitly $oBitly * @param object $Resource object of type Question or Answer * @return mixed null if exception was caught or array returned * by Twitter API */ public function post(\Lampcms\Twitter $oTwitter, \Lampcms\Bitly $oBitly, $Resource) { d('cp'); if (!$Resource instanceof \Lampcms\Question && !$Resource instanceof \Lampcms\Answer) { e('Resource not Question and not Answer'); return; } $ret = null; /** * $title is already guaranteed to be * in utf-8 */ $title = $Resource['title']; $url = $Resource->getUrl(); /** * Short url from bit.ly is guaranteed * to be in utf-8 */ $short = $oBitly->getShortUrl($url); /** * Our own url is in utf8 unless... * Unless this site is on some weird international * domain name that includes non-utf8 chars * This is super unlikely * We can assume that all components of * the tweet is already in utf-8 */ $url = $short ? $short : $Resource->getUrl(true); /** * Test what the length of tweet will be * if we concatenate title + space + url * * @var int */ $testLength = \mb_strlen($url . ' ' . $title, 'utf-8'); if ($testLength > 140) { d('need to shorten title'); $title = Utf8String::stringFactory($title, 'utf-8', true)->truncate(139 - \mb_strlen($url, 'utf-8'))->valueOf(); $text = $title . ' ' . $url; } else { $text = $title . ' ' . $url; } d('going to tweet this text: ' . $text); try { $ret = $oTwitter->postMessage($text); } catch (\Exception $e) { e('Tweet not sent because of exception: ' . $e->getMessage() . ' in file: ' . $e->getFile() . ' on line: ' . $e->getLine()); } return $ret; }
/** * */ protected function main() { if (!Request::isAjax()) { e('Tweet called as non-ajax'); throw new \Lampcms\Exception('This page can only be accessed using XHR request (ajax)'); } try { $oTwitter = new Twitter($this->Registry); $aResponse = $oTwitter->prepareAndPost($this->Request->getUTF8('tweet')); Responder::sendJSON(array('tweet' => 'done')); } catch (\Lampcms\Exception $e) { e('Unable to post message to Twitter: ' . $e->getFile() . ' line: ' . $e->getLine() . ' ' . $e->getMessage()); Responder::sendJSON(array('tweet' => 'error')); } }
/** * Post tweet like * "Joined this site" * Also can and probably should add * the person to follow * our site's account */ protected function postTweetStatus() { //return $this; $sToFollow = $this->aTW['TWITTER_USERNAME']; $follow = !empty($sToFollow) ? ' #follow @' . $sToFollow : ''; $siteName = $this->Registry->Ini->SITE_TITLE; $ourTwitterUsername = $this->Registry->Ini->SITE_URL . $follow; $oTwitter = new Twitter($this->Registry); if (!empty($ourTwitterUsername)) { register_shutdown_function(function () use($oTwitter, $siteName, $ourTwitterUsername) { try { $oTwitter->followUser(); } catch (\Lampcms\TwitterException $e) { $message = 'Error in: ' . $e->getFile() . ' line: ' . $e->getLine() . ' message: ' . $e->getMessage(); //d($message); } /*try{ $oTwitter->postMessage('I Joined '.$siteName. ' '.$stuff); } catch (\Lampcms\TwitterException $e){ $message = 'Exception in: '.$e->getFile(). ' line: '.$e->getLine().' message: '.$e->getMessage(); }*/ }); } return $this; }
/** * Post tweet like * "Joined this site" * Also can and probably should add * the person to follow * our site's account */ protected function postTweetStatus() { $sToFollow = $this->aTW['TWITTER_USERNAME']; d('$sToFollow: ' . $sToFollow); if (empty($sToFollow)) { return $this; } $follow = !empty($sToFollow) ? ' #follow @' . $sToFollow : ''; $siteName = $this->Registry->Ini->SITE_TITLE; $ourTwitterUsername = $this->Registry->Ini->SITE_URL . $follow; $oTwitter = new Twitter($this->Registry); if (!empty($ourTwitterUsername)) { register_shutdown_function(function () use($oTwitter, $siteName, $ourTwitterUsername, $sToFollow) { try { $oTwitter->followUser($sToFollow); } catch (\Exception $e) { $message = 'Error in: ' . $e->getFile() . ' line: ' . $e->getLine() . ' message: ' . $e->getMessage(); if (function_exists('d')) { d($message); } } /** * Auto-posting tweet on user signup is a bad idea * and may anger some users. * Don't do this unless you really need this feature! */ /*try{ $oTwitter->postMessage('I Joined '.$siteName. ' '.$stuff); } catch (\Exception $e){ $message = 'Exception in: '.$e->getFile(). ' line: '.$e->getLine().' message: '.$e->getMessage(); if (function_exists('d')) { d($message); } }*/ }); } return $this; }