Advertisement
Guest User

class.vanillatwitterembed.plugin.php

a guest
Dec 26th, 2011
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Vanilla Twitter Embed: Embed tweets directly into discussion posts by pasting the tweet URL.
  5. * @author Jamie Chung <me@jamiechung.me>
  6. * @homepage http://www.jamiechung.me
  7. * @twitter @jamiechung
  8. * @github https://github.com/JamieChung/VanillaTwitterEmbed
  9. */
  10.  
  11.  
  12. $PluginInfo['VanillaTwitterEmbed'] = array (
  13.     'Name'              =>  'Vanilla Twitter Embed',
  14.     'Description'           =>  'Embed tweets directly into discussion posts by pasting the tweet URL.',
  15.     'Version'               =>  '0.2',
  16.     'RequiredApplications'  =>  array('Vanilla' => '2.0.18'),
  17.     'RequiredPlugins'       =>  FALSE,
  18.     'HasLocale'         =>  FALSE,
  19.     'Author'                =>  'Jamie Chung',
  20.     'AuthorEmail'           =>  'me@jamiechung.me',
  21.     'AuthorUrl'         =>  'http://www.jamiechung.me'
  22. );
  23.  
  24. class VanillaTwitterEmbedPlugin implements Gdn_IPlugin
  25. {
  26.     // Regex for replace callback. Identifies a url to a individual tweet.
  27.     private $twitterStatusRegex = '/<a href="(http|https)\:\/\/twitter.com\/(.*?)\/status\/([0-9]+)" target="_blank" rel="nofollow">(http|https)\:\/\/twitter.com\/(.*?)\/status\/([0-9]+)<\/a>/';
  28.  
  29.  
  30.    
  31.     /**
  32.     * Setup the database structure required to cache the used tweets.
  33.     */
  34.     public function Setup()
  35.     {
  36.         Gdn::Database()->Query("CREATE TABLE IF NOT EXISTS `GDN_TwitterEmbed` (
  37.                               `ID` int(11) NOT NULL AUTO_INCREMENT,
  38.                               `TweetID` varchar(255) NOT NULL,
  39.                               `Response` text NOT NULL,
  40.                               PRIMARY KEY (`ID`),
  41.                               KEY `TweetID` (`TweetID`)
  42.                             )");
  43.     }
  44.    
  45.     /**
  46.     * Destructs the database when we disable. Simple act of clearing the cache.
  47.     */
  48.     public function OnDisable ()
  49.     {
  50.         Gdn::Database()->Query("DROP TABLE IF EXISTS `GDN_TwitterEmbed`"); 
  51.     }
  52.    
  53.     /**
  54.     * If a match is found within the body of a post, the
  55.     * proper code is injected in place of it.
  56.     * @param $matches array Matched results from the twitter status regex.
  57.     */
  58.     protected function CreateEmbed ( $matches )
  59.     {
  60.         // Tweet ID is the third matched result.
  61.         $id = $matches[3];
  62.        
  63.         $tweet = false;
  64.        
  65.         // We cannot force $id to be an int datatype because the value is too large for PHP.
  66.         if ( ctype_digit($id) )
  67.         {
  68.             // Check if the selected tweet is in the database.
  69.             $tweet = Gdn::Database()->SQL()
  70.                         ->Select('*')
  71.                         ->From('TwitterEmbed')
  72.                         ->Where('TweetID = ', $id)
  73.                         ->Limit(1)
  74.                         ->Get()
  75.                         ->FirstRow();
  76.        
  77.         }
  78.                
  79.         // If we don't have the tweet, let's get it from the twitter server.
  80.         if ( !$tweet )
  81.         {
  82.             // Create the twitter embed code based on the Garden Locale
  83.             $locale = explode('-', C('Garden.Locale'));
  84.             $lang = substr($locale[0], 0, 2);
  85.            
  86.             $api = 'http://api.twitter.com/1/statuses/oembed.json?id='.$id.'&omit_script=true&lang='.$lang;
  87.             $response = file_get_contents($api);
  88.            
  89.             // If we can't get the proper response from the server
  90.             // simply return the matched result (url link in post)
  91.             if ( !$response )
  92.             {
  93.                 return $matches[0];
  94.             }
  95.            
  96.             // Insert the matched tweet and the cached results into the database.
  97.             Gdn::Database()->SQL()
  98.                 ->Insert('TwitterEmbed', array(
  99.                     'TweetID' => $id,
  100.                     'Response' => $response,
  101.                 ));
  102.                                
  103.             $response = json_decode($response);
  104.         }
  105.         else
  106.         {
  107.             $response = json_decode($tweet->Response); 
  108.         }
  109.            
  110.         return $response->html;
  111.     }
  112.    
  113.     /**
  114.     * Setup the callback to replace the content of the post with the oembed code.
  115.     */
  116.     protected function TwitterEmbed ( $content )
  117.     {
  118.         $this->content = $content;
  119.         $content = preg_replace_callback($this->twitterStatusRegex, array($this, 'CreateEmbed'), $content);
  120.         return $content;
  121.     }
  122.    
  123.     /**
  124.     * Setup the callback for every page that handles comments to a post.
  125.     */
  126.     public function Base_AfterCommentFormat_Handler ( &$Sender )
  127.     {
  128.         $Object = $Sender->EventArguments['Object'];
  129.        
  130.         $Object->FormatBody = $this->TwitterEmbed($Object->FormatBody);
  131.         $Sender->EventArguments['Object'] = $Object;
  132.     }
  133.    
  134.     /**
  135.     * Injects the twitter widget into all pages.
  136.     */
  137.     public function Base_Render_Before ( $Sender )
  138.     {
  139.         $Sender->AddJsFile('https://platform.twitter.com/widgets.js');
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement