Advertisement
randf

Implement "Tweet.js-Mod" (Twitter API 1.1) with WordPress

Jun 17th, 2013
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.94 KB | None | 0 0
  1. <?php
  2.  
  3. // get theme options
  4.  
  5. $wp_include = "../../../../../wp-load.php";
  6. $i = 0;
  7. while (!file_exists($wp_include) && $i++ < 10) {
  8.     $wp_include = "../$wp_include";
  9. }
  10.  
  11. require($wp_include);
  12.  
  13. $twitter_app_consumer_key = theme_get_option('consumer_key');
  14. $twitter_app_consumer_secret = theme_get_option('consumer_secret');
  15. $twitter_app_user_token = theme_get_option('access_token');
  16. $twitter_app_user_secret = theme_get_option('access_token_secret');
  17.  
  18. $parameters = array(
  19.     'consumer_key' => $twitter_app_consumer_key,
  20.     'consumer_secret' => $twitter_app_consumer_secret,
  21.     'user_token' => $twitter_app_user_token,
  22.     'user_secret' => $twitter_app_user_secret
  23. );
  24.  
  25. if(empty($_POST)) { die(); }
  26.  
  27. class ezTweet {
  28.     /*************************************** config ***************************************/
  29.  
  30.     // Your Twitter App Consumer Key
  31.     private $consumer_key    = '';
  32.  
  33.     // Your Twitter App Consumer Secret
  34.     private $consumer_secret = '';
  35.  
  36.     // Your Twitter App Access Token
  37.     private $user_token      = '';
  38.  
  39.     // Your Twitter App Access Token Secret
  40.     private $user_secret     = '';
  41.  
  42.     // Path to tmhOAuth libraries
  43.     private $lib             = './lib/';
  44.  
  45.     // Enable caching
  46.     private $cache_enabled   = true;
  47.  
  48.     // Cache interval (minutes)
  49.     private $cache_interval  = 1;
  50.  
  51.     // Path to writable cache directory
  52.     private $cache_dir       = './';
  53.  
  54.     // Enable debugging
  55.     private $debug           = false;
  56.  
  57.     public function __construct($parameters) {
  58.     // Set Twitter parameters
  59.     $this->consumer_key = $parameters['consumer_key'];
  60.     $this->consumer_secret = $parameters['consumer_secret'];
  61.     $this->user_token = $parameters['user_token'];
  62.     $this->user_secret = $parameters['user_secret'];
  63.  
  64.         // Initialize paths and etc.
  65.         $this->pathify($this->cache_dir);
  66.         $this->pathify($this->lib);
  67.         $this->message = '';
  68.  
  69.         // Set server-side debug params
  70.         if($this->debug === true) {
  71.             error_reporting(-1);
  72.         } else {
  73.             error_reporting(0);
  74.         }
  75.     }
  76.  
  77.     public function fetch() {
  78.         echo json_encode(
  79.             array(
  80.                 'response' => json_decode($this->getJSON(), true),
  81.                 'message' => ($this->debug) ? $this->message : false
  82.             )
  83.         );
  84.     }
  85.  
  86.     private function getJSON() {
  87.         if($this->cache_enabled === true) {
  88.             $CFID = $this->generateCFID();
  89.             $cache_file = $this->cache_dir.$CFID;
  90.  
  91.             if(file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * intval($this->cache_interval)))) {
  92.                 return file_get_contents($cache_file, FILE_USE_INCLUDE_PATH);
  93.             } else {
  94.  
  95.                 $JSONraw = $this->getTwitterJSON();
  96.                 $JSON = $JSONraw['response'];
  97.  
  98.                 // Don't write a bad cache file if there was a CURL error
  99.                 if($JSONraw['errno'] != 0) {
  100.                     $this->consoleDebug($JSONraw['error']);
  101.                     return $JSON;
  102.                 }
  103.  
  104.                 if($this->debug === true) {
  105.                     // Check for twitter-side errors
  106.                     $pj = json_decode($JSON, true);
  107.                     if(isset($pj['errors'])) {
  108.                         foreach($pj['errors'] as $error) {
  109.                             $message = 'Twitter Error: "'.$error['message'].'", Error Code #'.$error['code'];
  110.                             $this->consoleDebug($message);
  111.                         }
  112.                         return false;
  113.                     }
  114.                 }
  115.  
  116.                 if(is_writable($this->cache_dir) && $JSONraw) {
  117.                     if(file_put_contents($cache_file, $JSON, LOCK_EX) === false) {
  118.                         $this->consoleDebug("Error writing cache file");
  119.                     }
  120.                 } else {
  121.                     $this->consoleDebug("Cache directory is not writable");
  122.                 }
  123.                 return $JSON;
  124.             }
  125.         } else {
  126.             $JSONraw = $this->getTwitterJSON();
  127.  
  128.             if($this->debug === true) {
  129.                 // Check for CURL errors
  130.                 if($JSONraw['errno'] != 0) {
  131.                     $this->consoleDebug($JSONraw['error']);
  132.                 }
  133.  
  134.                 // Check for twitter-side errors
  135.                 $pj = json_decode($JSONraw['response'], true);
  136.                 if(isset($pj['errors'])) {
  137.                     foreach($pj['errors'] as $error) {
  138.                         $message = 'Twitter Error: "'.$error['message'].'", Error Code #'.$error['code'];
  139.                         $this->consoleDebug($message);
  140.                     }
  141.                     return false;
  142.                 }
  143.             }
  144.             return $JSONraw['response'];
  145.         }
  146.     }
  147.  
  148.     private function getTwitterJSON() {
  149.         require $this->lib.'tmhOAuth.php';
  150.         require $this->lib.'tmhUtilities.php';
  151.  
  152.         $tmhOAuth = new tmhOAuth(array(
  153.             'consumer_key'          => $this->consumer_key,
  154.             'consumer_secret'       => $this->consumer_secret,
  155.             'user_token'            => $this->user_token,
  156.             'user_secret'           => $this->user_secret,
  157.             'curl_ssl_verifypeer'   => false
  158.         ));
  159.  
  160.         $url = $_POST['request']['url'];
  161.         $params = $_POST['request']['parameters'];
  162.  
  163.         $tmhOAuth->request('GET', $tmhOAuth->url($url), $params);
  164.         return $tmhOAuth->response;
  165.     }
  166.  
  167.     private function generateCFID() {
  168.         // The unique cached filename ID
  169.         return md5(serialize($_POST)).'.json';
  170.     }
  171.  
  172.     private function pathify(&$path) {
  173.         // Ensures our user-specified paths are up to snuff
  174.         $path = realpath($path).'/';
  175.     }
  176.  
  177.     private function consoleDebug($message) {
  178.         if($this->debug === true) {
  179.             $this->message .= 'tweet.js: '.$message."\n";
  180.         }
  181.     }
  182. }
  183.  
  184. $ezTweet = new ezTweet($parameters);
  185. $ezTweet->fetch();
  186.  
  187.  
  188. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement