Source of file SocialFeedProviderTwitter.php
Size: 4,058 Bytes - Last Modified: 2021-12-24T05:19:30+00:00
/var/www/docs.ssmods.com/process/src/src/Providers/SocialFeedProviderTwitter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 | <?php namespace IsaacRankin\SocialFeed\Providers; use Abraham\TwitterOAuth\TwitterOAuth; use IsaacRankin\SocialFeed\SocialFeedProviderInterface; use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\RequiredFields; use SilverStripe\ORM\FieldType\DBField; class SocialFeedProviderTwitter extends SocialFeedProvider implements SocialFeedProviderInterface { private static $db = array ( 'ConsumerKey' => 'Varchar(400)', 'ConsumerSecret' => 'Varchar(400)', 'AccessToken' => 'Varchar(400)', 'AccessTokenSecret' => 'Varchar(400)', 'ScreenName' => 'Varchar', 'TweetModeExtended' => 'Boolean', 'ShowReTweetedImages' => 'Boolean', ); private static $field_labels = array ( 'TweetModeExtended' => 'Extended mode (use if images are not showing)', 'ShowReTweetedImages' => 'Show images in re-tweets' ); private static $singular_name = 'Twitter Provider'; private static $plural_name = 'Twitter Providers'; private static $table_name = 'SocialFeedProviderTwitter'; private $type = 'twitter'; public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldsToTab('Root.Main', LiteralField::create('sf_html_1', '<h4>To get the necessary Twitter API credentials you\'ll need to create a <a href="https://apps.twitter.com" target="_blank">Twitter App.</a></h4>'), 'Label'); $fields->addFieldsToTab('Root.Main', LiteralField::create('sf_html_2', '<p>You can manually grant permissions to the Twitter App, this will give you an Access Token and Access Token Secret.</h5><p> </p>'), 'Label'); return $fields; } public function getCMSValidator() { return RequiredFields::create(array('ConsumerKey', 'ConsumerSecret')); } /** * Return the type of provider * * @return string */ public function getType() { return $this->type; } public function getFeedUncached() { // NOTE: Twitter doesn't implement OAuth 2 so we can't use https://github.com/thephpleague/oauth2-client $connection = new TwitterOAuth($this->ConsumerKey, $this->ConsumerSecret, $this->AccessToken, $this->AccessTokenSecret); $parameters = ['count' => 25, 'exclude_replies' => true]; if($this->ScreenName) { $parameters['screen_name'] = $this->ScreenName; } if($this->TweetModeExtended) { $parameters['tweet_mode'] = "extended"; } $result = $connection->get('statuses/user_timeline', $parameters); if (isset($result->error)) { user_error($result->error, E_USER_WARNING); } return $result; } /** * @return HTMLText */ public function getPostContent($post) { $text = isset($post->text) ? $post->text : ''; if($this->TweetModeExtended && isset($post->full_text)) { $text = $post->full_text; } elseif(isset($post->text)) { $text = $post->text; } $text = preg_replace('/(https?:\/\/[a-z0-9\.\/]+)/i', '<a href="$1" target="_blank">$1</a>', $text); $result = DBField::create_field('HTMLText', $text); return $result; } /** * Get the creation time from a post * * @param $post * @return mixed */ public function getPostCreated($post) { return $post->created_at; } /** * Get the post URL from a post * * @param $post * @return mixed */ public function getPostUrl($post) { return 'https://twitter.com/' . (string) $post->user->id .'/status/' . (string) $post->id; } /** * The user's name who tweeted * * @param $post * @return mixed */ public function getUserName($post) { return $post->user->name; } /** * The first image for a Tweet * * @param $post * @return mixed */ public function getImage($post) { if(property_exists($post->entities, 'media') && $post->entities->media[0]->media_url_https) { return $post->entities->media[0]->media_url_https; } elseif($this->ShowReTweetedImages && isset($post->retweeted_status) && property_exists($post->retweeted_status, 'entities')&& property_exists($post->retweeted_status->entities, 'media') && $post->retweeted_status->entities->media[0]->media_url_https) { return $post->retweeted_status->entities->media[0]->media_url_https; } } } |