Advertisement
Guest User

twitget.php

a guest
Jul 18th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 39.10 KB | None | 0 0
  1. <?php
  2.     /*
  3.         Plugin Name: Twitget
  4.         Plugin URI: http://wpplugz.is-leet.com
  5.         Description: A simple widget that shows your recent tweets with fully customizable HTML output, hashtag support and more.
  6.         Version: 3.0
  7.         Author: Bostjan Cigan
  8.         Author URI: http://bostjan.gets-it.net
  9.         License: GPL v2
  10.     */
  11.    
  12.     // Wordpress formalities here ...
  13.    
  14.     // Lets register things
  15.     if(!class_exists('tmhOAuth')) {
  16.         require 'lib/tmhOAuth.php';
  17.     }
  18.     if(!class_exists('tmhUtilities')) {
  19.         require 'lib/tmhUtilities.php';
  20.     }
  21.     if(!class_exists('TwitterAPIExchange')) {
  22.         require 'lib/TwitterAPIExchange.php';
  23.     }
  24.    
  25.     register_activation_hook(__FILE__, 'twitget_install');
  26.     register_deactivation_hook(__FILE__, 'twitget_uninstall');
  27.     add_action('admin_menu', 'twitget_admin_menu_create');
  28.     add_action('widgets_init', create_function('', 'return register_widget("simple_tweet_widget");')); // Register the widget
  29.     add_shortcode('twitget', 'twitget_shortcode_handler');
  30.     add_action('wp_head', 'twitget_js_include');
  31.     add_action('init', 'twitget_jquery_include');
  32.    
  33.     global $twitget_plugin_install_options;
  34.     $twitget_plugin_install_options = array(
  35.         'twitter_username' => '',
  36.         'twitter_data' => NULL,
  37.         'last_access' => time(),
  38.         'time_limit' => 5,
  39.         'number_of_tweets' => 5,
  40.         'show_avatar' => true,
  41.         'time_format' => 'D jS M y H:i',
  42.         'show_powered_by' => false,
  43.         'language' => 'en',
  44.         'version' => '3.0',
  45.         'consumer_key' => '',
  46.         'consumer_secret' => '',
  47.         'user_token' => '',
  48.         'user_secret' => '',
  49.         'twitter_api' => 0, // 0 is Matt Harris's lib, 1 is James Mallisons lib
  50.         'links_new_window' => false,
  51.         'show_retweets' => false,
  52.         'exclude_replies' => false,
  53.         'show_relative_time' => false,
  54.         'truncate_tweet' => false,
  55.         'truncate_tweet_size' => 100,
  56.         'custom_string' => '<img class="alignleft" src="{$profile_image}">
  57. <a href="https://www.twitter.com/{$user_twitter_name}">@{$user_twitter_name}</a>
  58. <br />
  59. {$user_description}
  60. <ul class="pages">
  61. {$tweets_start}
  62.     <li>{$tweet_text}<br />{$tweet_time}</li>
  63. {$tweets_end}
  64. </ul>'
  65.  
  66.     );
  67.    
  68.     global $twitget_language_array;
  69.     $twitget_language_array = array(
  70.             "Arabic" => "ar",
  71.             "Arabic (Moroccan)" => "ar-ma",
  72.             "Bahasa (Indonesia)" => "id",
  73.             "Bahasa (Malaysia)" => "ms-my",
  74.             "Basque" => "eu",
  75.             "Bulgarian" => "bg",
  76.             "Catalan" => "ca",
  77.             "Chuvash" => "cv",
  78.             "Chinese" => "zh-cn",
  79.             "Chinese (Traditional)" => "zh-tw",
  80.             "Czech" => "cs",
  81.             "Danish" => "da",
  82.             "Dutch" => "nl",
  83.             "German" => "de",
  84.             "English" => "en",
  85.             "English (Canadian)" => "en-ca",
  86.             "English (UK)" => "en-gb",
  87.             "Esperanto" => "eo",
  88.             "Spanish" => "es",
  89.             "Finnish" => "fi",
  90.             "French" => "fr",
  91.             "French (Canadian)" => "fr-ca",
  92.             "Galician" => "gl",
  93.             "Hebrew" => "he",
  94.             "Hungarian" => "hu",
  95.             "Icelandic" => "is",
  96.             "Italian" => "it",
  97.             "Japanese" => "ja",
  98.             "Korean" => "ko",
  99.             "Latvian" => "lv",
  100.             "Nepalese" => "ar",
  101.             "Norwegian" => "ar",
  102.             "Polish" => "pl",
  103.             "Portugese" => "pt",
  104.             "Portugese (Brazil)" => "pt-br",
  105.             "Russian" => "ru",
  106.             "Slovenian" => "sl",
  107.             "Swedish" => "sv",
  108.             "Thai" => "th",
  109.             "Turkish" => "tr",
  110.             "Tamaziyt" => "tzm-la",
  111.             "Ukranian" => "uk"
  112.     );
  113.    
  114.     // The array for converting PHP formatted date to moment.js date
  115.     global $twitget_time_array;
  116.     $twitget_time_array = array(
  117.         "d" => "MM", // 01 - 31 day of the month
  118.         "D" => "ddd", // Mon through Sun
  119.         "j" => "M", // 1 to 31 day of the month without zeroes
  120.         "l" => "dddd", // Sunday through Saturday
  121.         "N" => "D", // 1 - 7 (day)
  122.         "S" => "Do", // st, nd, rd, th - Because PHP supports suffix for only day of month (j)
  123.         "w" => "d", // 0 - 6 day of week
  124.         "z" => "DDD", // 0 - 365 day of year
  125.         "W" => "wo", // 42nd week of year
  126.         "F" => "MMMM", // January through December
  127.         "m" => "MM", // 01 - 12
  128.         "M" => "MMM", // Jan through Dec
  129.         "n" => "M", // 1 - 12
  130.         "t" => "", // 28 - 31 (number of days in month)
  131.         "L" => "", // 1 if leap year, 0 otherwise
  132.         "o" => "YYYY", // 1999 or 2003
  133.         "Y" => "YYYY", // 1999 or 2003
  134.         "y" => "YY", // 99 or 03
  135.         "a" => "a", // am or pm
  136.         "A" => "A", // AM or PM
  137.         "B" => "", // 000 to 999 swatch
  138.         "g" => "h", // 1 through 12
  139.         "G" => "H", // 0 through 23
  140.         "h" => "hh", // 01 to 12
  141.         "H" => "HH", // 00 to 23
  142.         "i" => "mm", // 00 to 59 minutes
  143.         "s" => "ss", // 00 to 59 seconds
  144.         "u" => ""  // microseconds
  145.     );
  146.    
  147.     // Get current options
  148.     $plugin_options_settings = get_option('twitget_settings');
  149.  
  150.     // Check if version is smaller and update
  151.     if(is_array($plugin_options_settings) && isset($plugin_options_settings['version'])) {
  152.         if(((float) ($plugin_options_settings['version'])) < ((float) ($twitget_plugin_install_options['version']))) {
  153.             twitget_update();
  154.         }
  155.     }
  156.    
  157.     function twitget_install() {
  158.         global $twitget_plugin_install_options;
  159.         add_option('twitget_settings', $twitget_plugin_install_options);
  160.     }
  161.    
  162.     function twitget_jquery_include() {
  163.         if(!wp_script_is('jquery')) {
  164.             wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js');
  165.             wp_enqueue_script('jquery');
  166.         }  
  167.     }
  168.  
  169.     function twitget_js_include() {
  170.         $moment_js = plugin_dir_url(__FILE__).'js/moment.js';
  171.         $lang_js = plugin_dir_url(__FILE__).'js/langs.min.js';
  172.         wp_register_script('moment.js', $moment_js);
  173.         wp_enqueue_script('moment.js');
  174.         wp_register_script('langs.js', $lang_js);
  175.         wp_enqueue_script('langs.js');
  176.     }
  177.  
  178.    
  179.     function twitget_update() {
  180.        
  181.         global $twitget_plugin_install_options;
  182.         $plugin_options_settings = get_option('twitget_settings');     
  183.        
  184.         // Legacy purposes only, before 3.0, delete these variables from options
  185.         $html_output = array(
  186.             'after_image_html',
  187.             'before_tweets_html',
  188.             'tweet_start_html',
  189.             'tweet_middle_html',
  190.             'tweet_end_html',
  191.             'after_tweets_html',
  192.             'mode',
  193.             'use_custom',
  194.             'use_cookie',
  195.             'cookie_expiration',
  196.             'show_browser_time',
  197.             'show_local_time'
  198.         );
  199.        
  200.         if((float) $plugin_options_settings['version'] < (float) $twitget_plugin_install_options['version']) {
  201.             foreach($twitget_plugin_install_options as $key => $value) {
  202.                 $plugin_options_settings[$key] = (isset($plugin_options_settings[$key]) && strcmp($key, "version") != 0) ? $plugin_options_settings[$key] : $value;
  203.             }
  204.             foreach($html_output as $key) { // Legacy purposes only, before 2.0, delete variables from options
  205.                 unset($plugin_options_settings[$key]);
  206.             }
  207.             $plugin_options_settings['version'] = $twitget_plugin_install_options['version'];
  208.             update_option('twitget_settings', $plugin_options_settings);
  209.         }
  210.        
  211.     }
  212.    
  213.     function twitget_uninstall() {
  214.         delete_option('twitget_settings');
  215.     }
  216.  
  217.     function twitget_admin_menu_create() {
  218.         add_options_page('Twitget Settings', 'Twitget', 'administrator', __FILE__, 'twitget_settings');
  219.     }
  220.  
  221.     // Shortcode function
  222.     function twitget_shortcode_handler($attributes, $content = null) {
  223.         ob_start();
  224.         show_recent_tweets();
  225.         return ob_get_clean();
  226.     }
  227.  
  228.     function twitter_status() {
  229.    
  230.         $options = get_option('twitget_settings');
  231.  
  232.         if($options['twitter_api'] == 0) {
  233.            
  234.             $tmhOAuth = new tmhOAuth(
  235.                                         array(
  236.                                             'consumer_key' => $options['consumer_key'],
  237.                                             'consumer_secret' => $options['consumer_secret'],
  238.                                             'user_token' => $options['user_token'],
  239.                                             'user_secret' => $options['user_secret'],
  240.                                             'curl_ssl_verifypeer' => false
  241.                                         )
  242.                                     );
  243.      
  244.             $request_array = array();
  245.             $request_array['screen_name'] = $options['twitter_username'];
  246.             $request_array['include_rts'] = $options['show_retweets'];
  247.             $request_array['exclude_replies'] = $options['exclude_replies'];
  248.      
  249.             $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), $request_array);
  250.      
  251.             $response = $tmhOAuth->response['response'];
  252.            
  253.             $tweets = json_decode($response, true);
  254.                        
  255.                         for($i = 0; $i < count($tweets); ++$i) {
  256.                             $tweets[$i]['text'] = utf8_encode($tweets[$i]['text']);
  257.                         }
  258.            
  259.             if(is_array($tweets)) {
  260.                 $options['twitter_data'] = (array) $tweets;
  261.             }
  262.             else {
  263.                 $options['twitter_data'] = NULL;
  264.             }
  265.            
  266.         }
  267.         else {
  268.        
  269.             $twitter = new TwitterAPIExchange(
  270.                                         array(
  271.                                             'oauth_access_token' => $options['user_token'],
  272.                                             'oauth_access_token_secret' => $options['user_secret'],
  273.                                             'consumer_key' => $options['consumer_key'],
  274.                                             'consumer_secret' => $options['consumer_secret']
  275.                                         )
  276.                                     );
  277.                                    
  278.             $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
  279.             $getfield = '?screen_name='.$options['twitter_username'];
  280.            
  281.             if($options['show_retweets']) {
  282.                 $getfield .= '&include_rts=true';
  283.             }
  284.             if(!$options['exclude_replies']) {
  285.                 $getfield .= '&exclude_replies=true';
  286.             }
  287.            
  288.             $requestMethod = 'GET';
  289.             $response = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
  290.             $tweets = json_decode($response, true);
  291.  
  292.                         for($i = 0; $i < count($tweets); ++$i) {
  293.                             $tweets[$i]['text'] = utf8_encode($tweets[$i]['text']);
  294.                         }
  295.                        
  296.             if(is_array($tweets)) {
  297.                 $options['twitter_data'] = (array) $tweets;
  298.             }
  299.             else {
  300.                 $options['twitter_data'] = NULL;
  301.             }
  302.                        
  303.                        
  304.         }
  305.  
  306.         update_option('twitget_settings', $options);
  307.     }
  308.    
  309.     function process_links($text, $new) {
  310.  
  311.         if($new) {
  312.             $text = preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>',  $text);
  313.             $text = preg_replace('/@(\w+)/', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $text);
  314.             $text = preg_replace('/\s#(\w+)/', ' <a href="http://twitter.com/search?q=%23$1" target="_blank">#$1</a>', $text);
  315.         }
  316.         else {
  317.             $text = preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>',  $text);
  318.             $text = preg_replace('/@(\w+)/', '<a href="http://twitter.com/$1">@$1</a>', $text);
  319.             $text = preg_replace('/\s#(\w+)/', ' <a href="http://twitter.com/search?q=%23$1">#$1</a>', $text);     
  320.         }
  321.         return $text;
  322.  
  323.     }
  324.  
  325.     function show_recent_tweets() {
  326.  
  327.         $options = get_option('twitget_settings');
  328.         $get_data = false;
  329.        
  330.         if(!isset($options['twitter_data'])) {
  331.             $get_data = true;
  332.         }
  333.        
  334.         if(time() - $options['last_access'] > $options['time_limit'] * 60) {
  335.             $get_data = true;
  336.             $options['last_access'] = time();
  337.             update_option('twitget_settings', $options);
  338.         }
  339.        
  340.         unset($options);
  341.         $options = get_option('twitget_settings');
  342.  
  343.         if($get_data) {
  344.             twitter_status();
  345.         }
  346.        
  347.         unset($options);
  348.         $options = get_option('twitget_settings');
  349.         $tweets = $options['twitter_data'];
  350.        
  351.         if(is_array($tweets) && isset($tweets) && isset($tweets[0]['user'])) {
  352.  
  353.             $limit = $options['number_of_tweets'];
  354.  
  355.             $image_url = $tweets[0]['user']['profile_image_url']; // {$profile_image}
  356.             $twitter_username = $tweets[0]['user']['screen_name']; // {$user_twitter_name}
  357.             $twitter_username_real = $tweets[0]['user']['name']; // {$user_real_name}
  358.             $twitter_user_url = $tweets[0]['user']['url']; // {$url}
  359.             $twitter_user_description = $tweets[0]['user']['description']; // {$user_description}
  360.             $twitter_follower_count = $tweets[0]['user']['followers_count']; // {$follower_count}
  361.             $twitter_friends_count = $tweets[0]['user']['friends_count']; // {$friends_count}
  362.             $twitter_user_location = $tweets[0]['user']['location']; // {$user_location}
  363.  
  364.             $result = "";
  365.  
  366.             $custom_string = $options['custom_string'];
  367.             $feed_string = twitget_get_substring($custom_string, "{\$tweets_start}", "{\$tweets_end}");
  368.            
  369.             $feed_whole_string = "";
  370.  
  371.             $i = 0;
  372.             $tweet_date_array = array();
  373.             foreach($tweets as $tweet) {
  374.                 $tweet_text = utf8_decode($tweet['text']);
  375.                 $tweet_location = $tweet['place']['full_name'];
  376.                 $link_processed = "";
  377.                 if(isset($tweet['retweeted_status'])) {
  378.                     $first = current(explode(":", $tweet_text));
  379.                     $whole_tweet = $first.": ";
  380.                     $whole_tweet .= $tweet['retweeted_status']['text'];
  381.                     $link_processed = process_links($whole_tweet, $options['links_new_window']);
  382.                 }
  383.                 else {
  384.                     $link_processed = process_links($tweet['text'], $options['links_new_window']);
  385.                 }
  386.  
  387.                 $tweet_date_array[$i] = strtotime($tweet['created_at']);
  388.                
  389.                 $tweet_id = $tweet['id_str'];
  390.                
  391.                 if($options["truncate_tweet"]) {
  392.                     $link_processed = twitget_truncate_tweet($link_processed, $options["truncate_tweet_size"], "...", true, true);
  393.                 }
  394.                
  395.                 $feed_string_tmp = str_replace("{\$tweet_text}", $link_processed, $feed_string);
  396.                 $feed_string_tmp = str_replace("{\$tweet_time}", '<span class="'.$i.'_tweet_date"></span>', $feed_string_tmp);             
  397.                 $feed_string_tmp = str_replace("{\$tweet_location}", $tweet_location, $feed_string_tmp);
  398.                 $feed_string_tmp = str_replace("{\$retweet}", '<a href="http://twitter.com/intent/retweet?tweet_id='.$tweet_id.'" target="_blank">Retweet</a>', $feed_string_tmp);
  399.                 $feed_string_tmp = str_replace("{\$reply}", '<a href="http://twitter.com/intent/tweet?in_reply_to='.$tweet_id.'" target="_blank">Reply</a>', $feed_string_tmp);
  400.                 $feed_string_tmp = str_replace("{\$favorite}", '<a href="http://twitter.com/intent/favorite?tweet_id='.$tweet_id.'" target="_blank">Favorite</a>', $feed_string_tmp);
  401.                 $feed_string_tmp = str_replace("{\$retweet_link}", "http://twitter.com/intent/retweet?tweet_id=".$tweet_id, $feed_string_tmp);
  402.                 $feed_string_tmp = str_replace("{\$reply_link}", "http://twitter.com/intent/tweet?in_reply_to=".$tweet_id, $feed_string_tmp);
  403.                 $feed_string_tmp = str_replace("{\$favorite_link}", "http://twitter.com/intent/favorite?tweet_id=".$tweet_id, $feed_string_tmp);
  404.                 $feed_string_tmp = str_replace("{\$tweet_link}", "http://twitter.com/".$options['twitter_username']."/statuses/".$tweet_id, $feed_string_tmp); 
  405.  
  406.                 if(isset($tweet['retweeted_status'])) {
  407.                     $profile_url = $tweet["retweeted_status"]["user"]["profile_image_url"]; // 48x48 px
  408.                     $profile_73_url = str_replace("_normal", "_bigger", $profile_url); // 73x73 px
  409.                     $profile_24_url = str_replace("_normal", "_mini", $profile_url); // 24x24 px
  410.                     $profile_original_url = str_replace("_normal", "", $profile_url); // Original size
  411.                     $feed_string_tmp = str_replace("{\$profile_image_normal_url}", $profile_url, $feed_string_tmp);
  412.                     $feed_string_tmp = str_replace("{\$profile_image_bigger_url}", $profile_73_url, $feed_string_tmp);
  413.                     $feed_string_tmp = str_replace("{\$profile_image_mini_url}", $profile_24_url, $feed_string_tmp);
  414.                     $feed_string_tmp = str_replace("{\$profile_image_original_url}", $profile_original_url, $feed_string_tmp);
  415.                 }
  416.                 else {
  417.                     $profile_url = $tweet['user']['profile_image_url']; // 48x48 px
  418.                     $profile_73_url = str_replace("_normal", "_bigger", $profile_url); // 73x73 px
  419.                     $profile_24_url = str_replace("_normal", "_mini", $profile_url); // 24x24 px
  420.                     $profile_original_url = str_replace("_normal", "", $profile_url); // Original size
  421.                     $feed_string_tmp = str_replace("{\$profile_image_normal_url}", $profile_url, $feed_string_tmp);
  422.                     $feed_string_tmp = str_replace("{\$profile_image_bigger_url}", $profile_73_url, $feed_string_tmp);
  423.                     $feed_string_tmp = str_replace("{\$profile_image_mini_url}", $profile_24_url, $feed_string_tmp);
  424.                     $feed_string_tmp = str_replace("{\$profile_image_original_url}", $profile_original_url, $feed_string_tmp);
  425.                 }
  426.  
  427.                 $feed_whole_string .= $feed_string_tmp;
  428.                 if($i == $limit - 1) {
  429.                     break;
  430.                 }
  431.                 $i = $i + 1;
  432.             }
  433.                
  434.             $feed_start = "{\$tweets_start}";
  435.             $feed_end = "{\$tweets_end}";
  436.             $start_pos = strrpos($custom_string, $feed_start);
  437.             $end_pos = strrpos($custom_string, $feed_end) + strlen($feed_end);
  438.             $tag_length = $end_pos - $start_pos + 1;
  439.  
  440.             $feed_string = substr_replace($custom_string, $feed_whole_string, $start_pos, $tag_length);
  441.             $feed_string = str_replace("{\$profile_image}", $image_url, $feed_string);
  442.             $feed_string = str_replace("{\$user_twitter_name}", $twitter_username, $feed_string);
  443.             $feed_string = str_replace("{\$user_real_name}", $twitter_username_real, $feed_string);
  444.             $feed_string = str_replace("{\$url}", $twitter_user_url, $feed_string);
  445.             $feed_string = str_replace("{\$user_description}", $twitter_user_description, $feed_string);
  446.             $feed_string = str_replace("{\$follower_count}", $twitter_follower_count, $feed_string);
  447.             $feed_string = str_replace("{\$friends_count}", $twitter_friends_count, $feed_string);
  448.             $feed_string = str_replace("{\$user_location}", $twitter_user_location, $feed_string);
  449.             $feed_string = str_replace("{\$tweet_location}", $tweet_location, $feed_string);
  450.  
  451.             $profile_url = $image_url;
  452.             $profile_73_url = str_replace("_normal", "_bigger", $profile_url); // 73x73 px
  453.             $profile_24_url = str_replace("_normal", "_mini", $profile_url); // 24x24 px
  454.             $profile_original_url = str_replace("_normal", "", $profile_url); // Original size
  455.             $feed_string = str_replace("{\$profile_image_normal_url}", $profile_url, $feed_string);
  456.             $feed_string = str_replace("{\$profile_image_bigger_url}", $profile_73_url, $feed_string);
  457.             $feed_string = str_replace("{\$profile_image_mini_url}", $profile_24_url, $feed_string);
  458.             $feed_string = str_replace("{\$profile_image_original_url}", $profile_original_url, $feed_string);
  459.  
  460.             $result = $feed_string;
  461.  
  462.             if(isset($tweets['errors'][0]['code'])) {
  463.                 $result = $options['before_tweets_html'].'<p>The Twitter feed is currently unavailable or the username does not exist.</p>';
  464.             }
  465.            
  466.             if($options['show_powered_by']) {
  467.                 $result = $result.'<p>Powered by <a href="http://wpplugz.is-leet.com">wpPlugz</a></p>';
  468.             }
  469.        
  470.         }
  471.         else {
  472.             $result = "Twitter outputted an error: <br />";
  473.             $result .= $tweets['errors'][0]['message'].".";
  474.         }
  475.  
  476.         echo $result;
  477.        
  478.         $time_format = $options['time_format'];
  479.         $moment_js_time = twitget_convert_from_php_to_momentjs($time_format);
  480.        
  481.        
  482. ?>
  483.  
  484.         <script type="text/javascript">
  485.             jQuery(document).ready(function() {
  486.                 <?php if(strlen($options['language']) > 0) {
  487.                 ?>
  488.                     moment.lang('<?php echo $options['language']; ?>');
  489.                 <?php
  490.                     }
  491.                     else {
  492.                 ?>
  493.                     moment.lang('en');
  494.                 <?php
  495.                     }
  496.                 ?>
  497.                 <?php foreach($tweet_date_array as $c => $val) { ?>
  498.                 var date_val_<?php echo $c; ?> = <?php echo $val; ?>;
  499.                 <?php if($options["show_relative_time"]) { ?>
  500.                 var date_<?php echo $c; ?> = moment.unix(date_val_<?php echo $c; ?>).fromNow();
  501.                 <?php } else { ?>
  502.                 var date_<?php echo $c; ?> = moment.unix(date_val_<?php echo $c; ?>).format("<?php echo $moment_js_time; ?>");
  503.                 <?php } ?>
  504.                 jQuery(".<?php echo $c; ?>_tweet_date").html(date_<?php echo $c; ?>);
  505.                 <?php } ?>
  506.             });
  507.         </script>
  508.  
  509. <?php
  510.  
  511.     }
  512.  
  513.     /**
  514.      *
  515.      * Converts PHP time format to moment.js time format
  516.      * @parameters String PHP date format
  517.      *
  518.     */
  519.     function twitget_convert_from_php_to_momentjs($date_format) {
  520.    
  521.         global $twitget_time_array;
  522.         $time_exploded = explode(" ", $date_format);
  523.         $time_string = "";
  524.         foreach($time_exploded as $time_token) {
  525.             if(strlen($time_token) == 1) {
  526.                 if(array_key_exists($time_token, $twitget_time_array)) {
  527.                     $time_string .= $twitget_time_array[$time_token];
  528.                 }
  529.                 else {
  530.                     $time_string .= $time_token;
  531.                 }
  532.             }
  533.             else {
  534.                 if($time_token == "jS") {
  535.                     $time_string .= $twitget_time_array["S"]." ";
  536.                 }
  537.                 else {
  538.                     for($i=0; $i<strlen($time_token); $i++) {
  539.                         if(array_key_exists($time_token[$i], $twitget_time_array)) {
  540.                             $time_string .= $twitget_time_array[$time_token[$i]];
  541.                         }
  542.                         else {
  543.                             $time_string .= $time_token[$i];
  544.                         }
  545.                     }
  546.                 }
  547.             }
  548.             $time_string .= " ";
  549.         }
  550.        
  551.         return $time_string;
  552.    
  553.     }
  554.    
  555.     // Get substring between two strings
  556.     function twitget_get_substring($string, $start, $end) {
  557.  
  558.         $pos = stripos($string, $start);
  559.         $str = substr($string, $pos);
  560.         $str_two = substr($str, strlen($start));
  561.         $second_pos = stripos($str_two, $end);
  562.         $str_three = substr($str_two, 0, $second_pos);
  563.         $unit = trim($str_three);
  564.        
  565.         return $unit;
  566.     }
  567.  
  568.     /**
  569.      * Truncates text.
  570.      *
  571.      * Cuts a string to the length of $length and replaces the last characters
  572.      * with the ending if the text is longer than length.
  573.      *
  574.      * @param string  $text String to truncate.
  575.      * @param integer $length Length of returned string, including ellipsis.
  576.      * @param string  $ending Ending to be appended to the trimmed string.
  577.      * @param boolean $exact If false, $text will not be cut mid-word
  578.      * @param boolean $considerHtml If true, HTML tags would be handled correctly
  579.      * @return string Trimmed string.
  580.      */
  581.     function twitget_truncate_tweet($text, $length, $ending, $exact, $considerHtml) {
  582.         if ($considerHtml) {
  583.             // if the plain text is shorter than the maximum length, return the whole text
  584.             if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  585.                 return $text;
  586.             }
  587.             // splits all html-tags to scanable lines
  588.             preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
  589.             $total_length = strlen($ending);
  590.             $open_tags = array();
  591.             $truncate = '';
  592.             foreach ($lines as $line_matchings) {
  593.                 // if there is any html-tag in this line, handle it and add it (uncounted) to the output
  594.                 if (!empty($line_matchings[1])) {
  595.                     // if it's an "empty element" with or without xhtml-conform closing slash (f.e. <br/>)
  596.                     if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
  597.                         // do nothing
  598.                     // if tag is a closing tag (f.e. </b>)
  599.                     } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
  600.                         // delete tag from $open_tags list
  601.                         $pos = array_search($tag_matchings[1], $open_tags);
  602.                         if ($pos !== false) {
  603.                             unset($open_tags[$pos]);
  604.                         }
  605.                     // if tag is an opening tag (f.e. <b>)
  606.                     } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
  607.                         // add tag to the beginning of $open_tags list
  608.                         array_unshift($open_tags, strtolower($tag_matchings[1]));
  609.                     }
  610.                     // add html-tag to $truncate'd text
  611.                     $truncate .= $line_matchings[1];
  612.                 }
  613.                 // calculate the length of the plain text part of the line; handle entities as one character
  614.                 $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
  615.                 if ($total_length+$content_length> $length) {
  616.                     // the number of characters which are left
  617.                     $left = $length - $total_length;
  618.                     $entities_length = 0;
  619.                     // search for html entities
  620.                     if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
  621.                         // calculate the real length of all entities in the legal range
  622.                         foreach ($entities[0] as $entity) {
  623.                             if ($entity[1]+1-$entities_length <= $left) {
  624.                                 $left--;
  625.                                 $entities_length += strlen($entity[0]);
  626.                             } else {
  627.                                 // no more characters left
  628.                                 break;
  629.                             }
  630.                         }
  631.                     }
  632.                     $truncate .= substr($line_matchings[2], 0, $left+$entities_length);
  633.                     // maximum lenght is reached, so get off the loop
  634.                     break;
  635.                 } else {
  636.                     $truncate .= $line_matchings[2];
  637.                     $total_length += $content_length;
  638.                 }
  639.                 // if the maximum length is reached, get off the loop
  640.                 if($total_length>= $length) {
  641.                     break;
  642.                 }
  643.             }
  644.         } else {
  645.             if (strlen($text) <= $length) {
  646.                 return $text;
  647.             } else {
  648.                 $truncate = substr($text, 0, $length - strlen($ending));
  649.             }
  650.         }
  651.         // if the words shouldn't be cut in the middle...
  652.         if (!$exact) {
  653.             // ...search the last occurance of a space...
  654.             $spacepos = strrpos($truncate, ' ');
  655.             if (isset($spacepos)) {
  656.                 // ...and cut the text in this position
  657.                 $truncate = substr($truncate, 0, $spacepos);
  658.             }
  659.         }
  660.         // add the defined ending to the text
  661.         $truncate .= $ending;
  662.         if($considerHtml) {
  663.             // close all unclosed html-tags
  664.             foreach ($open_tags as $tag) {
  665.                 $truncate .= '</' . $tag . '>';
  666.             }
  667.         }
  668.         return $truncate;
  669.     }
  670.    
  671.     function twitget_settings() {
  672.    
  673.         $twitget_settings = get_option('twitget_settings');
  674.         $message = '';
  675.        
  676.         if(isset($_POST['twitget_username']) && is_array($twitget_settings) === true) {
  677.        
  678.             $show_powered = $_POST['twitget_show_powered'];
  679.             $show_retweets = $_POST['twitget_retweets'];
  680.             $twitget_exclude = $_POST['twitget_exclude_replies'];
  681.             $twitget_relative = $_POST['twitget_relative_time'];
  682.             $twitget_custom = $_POST['twitget_use_custom'];
  683.             $new_link = $_POST['twitget_links_new_window'];
  684.  
  685.             $twitget_settings['twitter_username'] = stripslashes($_POST['twitget_username']);
  686.             $twitget_settings['time_limit'] = (int) $_POST['twitget_refresh'];
  687.             $twitget_settings['number_of_tweets'] = (int) $_POST['twitget_number'];
  688.             $twitget_settings['time_format'] = stripslashes($_POST['twitget_time']);
  689.             $twitget_settings['show_powered_by'] = (isset($show_powered)) ? true : false;
  690.             $twitget_settings['consumer_key'] = stripslashes($_POST['twitget_consumer_key']);
  691.             $twitget_settings['consumer_secret'] = stripslashes($_POST['twitget_consumer_secret']);
  692.             $twitget_settings['user_token'] = stripslashes($_POST['twitget_user_token']);
  693.             $twitget_settings['user_secret'] = stripslashes($_POST['twitget_user_secret']);
  694.             $twitget_settings['twitter_api'] = (int) ($_POST['twitget_api']);
  695.             $twitget_settings['show_retweets'] = (isset($show_retweets)) ? true : false;
  696.             $twitget_settings['exclude_replies'] = (isset($twitget_exclude)) ? true : false;
  697.             $twitget_settings['show_relative_time'] = (isset($twitget_relative)) ? true : false;
  698.             $twitget_settings['custom_string'] = stripslashes(html_entity_decode($_POST['twitget_custom_output']));
  699.             $twitget_settings['links_new_window'] = (isset($new_link)) ? true : false;
  700.             $twitget_settings['language'] = $_POST['twitget_time_language'];
  701.             $twitget_settings['truncate_tweet'] = (isset($_POST['truncate_tweet'])) ? true : false;
  702.             $twitget_settings['truncate_tweet_size'] = intval($_POST['truncate_tweet_size']);
  703.             $message = "Settings updated.";
  704.  
  705.             update_option('twitget_settings', $twitget_settings);
  706.            
  707.             unset($twitget_settings);
  708.            
  709.         }
  710.  
  711.         $twitget_options = get_option('twitget_settings');
  712.        
  713.         $twitget_options["time_format"];
  714.        
  715. ?>
  716.  
  717.         <div id="icon-options-general" class="icon32"></div><h2>Twitget Settings</h2>
  718. <?php
  719.  
  720.         if(strlen($message) > 0) {
  721.        
  722. ?>
  723.  
  724.             <div id="message" class="updated">
  725.                 <p><strong><?php echo $message; ?></strong></p>
  726.             </div>
  727.  
  728. <?php
  729.            
  730.         }
  731.  
  732. ?>
  733.        
  734.                 <form method="post" action="">
  735.                 <table class="form-table">
  736.                     <tr>
  737.                         <th scope="row"><img src="<?php echo plugin_dir_url(__FILE__).'twitter.png'; ?>" height="96px" width="96px" /></th>
  738.                         <td>
  739.                             <p>Thank you for using this plugin. If you like the plugin, you can <a href="http://gum.co/twitget" target="_blank">buy me a cup of coffee</a> :)</p>
  740.                             <p>Visit the official website @ <a href="http://wpplugz.is-leet.com">wpPlugz</a>.</p>
  741.                             <p>This plugin uses the <a href="https://github.com/themattharris/tmhOAuth">tmhOAuth</a> library by Matt Harris, <a href="http://momentjs.com/">moment.js</a> by Tim Wood and <a href="https://github.com/J7mbo/twitter-api-php">TwitterAPIExchange</a> by James Mallison.</p>
  742.                         </td>
  743.                     </tr>      
  744.                     <tr>
  745.                         <th scope="row"><label for="twitget_username">Twitter username</label></th>
  746.                         <td>
  747.                             <input type="text" name="twitget_username" id="twitget_username" value="<?php echo esc_attr($twitget_options['twitter_username']); ?>" />
  748.                             <br />
  749.                             <span class="description">Your Twitter username.</span>
  750.                         </td>
  751.                     </tr>
  752.                     <tr>
  753.                         <th scope="row"><label for="twitget_api">Twitter Library</label></th>
  754.                         <td>
  755.                             <select name="twitget_api" id="twitget_api">
  756.                                 <option value="0" <?php if($twitget_options['twitter_api'] == 0) { ?> selected="selected" <?php } ?>>tmhOAuth</option>
  757.                                 <option value="1" <?php if($twitget_options['twitter_api'] == 1) { ?> selected="selected" <?php } ?>>Twitter-API-PHP</option>
  758.                             </select>
  759.                             <br />
  760.                             <span class="description">Set the library you will be using. tmhOAuth is the default library.</span>
  761.                         </td>
  762.                     </tr>
  763.                     <tr>
  764.                         <th scope="row"><label for="twitget_data">Twitter API data</label></th>
  765.                         <td>
  766.                             <table class="form-table">
  767.                             <tr>
  768.                                 <th scope="row"><label for="twitget_consumer_key">Consumer key</label></th>
  769.                                 <td>
  770.                                     <input type="text" name="twitget_consumer_key" id="twitget_consumer_key" size="70" value="<?php echo $twitget_options['consumer_key']; ?>" /><br />
  771.                                     <span class="description">Enter your consumer key here.</span>
  772.                                 </td>
  773.                             </tr>
  774.                             <tr>
  775.                                 <th scope="row"><label for="twitget_consumer_secret">Consumer secret</label></th>
  776.                                 <td>
  777.                                     <input type="text" name="twitget_consumer_secret" id="twitget_consumer_secret" size="70" value="<?php echo $twitget_options['consumer_secret']; ?>" /><br />
  778.                                     <span class="description">Enter your consumer secret key here.</span>
  779.                                 </td>
  780.                             </tr>
  781.                             <tr>
  782.                                 <th scope="row"><label for="twitget_user_token">Access token</label></th>
  783.                                 <td>
  784.                                     <input type="text" name="twitget_user_token" id="twitget_user_token" size="70" value="<?php echo $twitget_options['user_token']; ?>" /><br />
  785.                                     <span class="description">Enter your access token key here.</span>
  786.                                 </td>
  787.                             </tr>
  788.                             <tr>
  789.                                 <th scope="row"><label for="twitget_user_secret">Access token secret</label></th>
  790.                                 <td>
  791.                                     <input type="text" name="twitget_user_secret" id="twitget_user_secret" size="70" value="<?php echo $twitget_options['user_secret']; ?>" /><br />
  792.                                     <span class="description">Enter your access token secret key here.</span>
  793.                                 </td>
  794.                             </tr>                          
  795.                             </table>
  796.                             <span class="description">Enter your API keys here. If you don't know how do to that, follow this <a href="http://www.youtube.com/watch?v=noB3P-K-wb4" target="_blank">video tutorial</a>.</span>                          
  797.                         </td>
  798.                     </tr>
  799.                     <tr>
  800.                         <th scope="row"><label for="twitget_refresh">Twitter feed refresh (in minutes)</label></th>
  801.                         <td>
  802.                             <input type="text" name="twitget_refresh" id="twitget_refresh" value="<?php echo $twitget_options['time_limit']; ?>" />
  803.                             <br />
  804.                             <span class="description">In how many minutes does the Twitter feed refresh.</span>
  805.                         </td>
  806.                     </tr>
  807.                     <tr>
  808.                         <th scope="row"><label for="twitget_number">Number of tweets</label></th>
  809.                         <td>
  810.                             <input type="text" name="twitget_number" id="twitget_number" value="<?php echo $twitget_options['number_of_tweets']; ?>" />
  811.                             <br />
  812.                             <span class="description">How many tweets are shown.</span>
  813.                         </td>
  814.                     </tr>
  815.                     <tr>
  816.                         <th scope="row"><label for="twitget_time">Time format</label></th>
  817.                         <td>
  818.                             <input type="text" name="twitget_time" id="twitget_time" value="<?php echo esc_html($twitget_options['time_format']); ?>" />
  819.                             <br />
  820.                             <span class="description">The time format.</span>
  821.                         </td>
  822.                     </tr>
  823.                     <tr>
  824.                         <th scope="row"><label for="twitget_time_language">Time format language</label></th>
  825.                         <td>
  826.                             <select name="twitget_time_language" id="twitget_time_language">
  827.                                 <option value="-1">--- Select time language ---</option>
  828.                                 <?php
  829.                                     global $twitget_language_array;
  830.                                     foreach($twitget_language_array as $lang => $short) {
  831.                                    
  832.                                 ?>
  833.                                         <option value="<?php echo $short; ?>" <?php if($short == $twitget_options['language']) { ?> selected="selected" <?php } ?>><?php echo $lang; ?></option>
  834.                                
  835.                                 <?php
  836.                                
  837.                                     }
  838.                                
  839.                                 ?>
  840.                             </select>
  841.                             <br />
  842.                             <span class="description">The language of the time format.</span>
  843.                         </td>
  844.                     </tr>
  845.                     <tr>
  846.                         <th scope="row"><label for="twitget_retweets">Show retweets</label></th>
  847.                         <td>
  848.                             <input type="checkbox" name="twitget_retweets" id="twitget_retweets" value="true" <?php if($twitget_options['show_retweets'] == true) { ?>checked="checked"<?php } ?> />
  849.                             <br />
  850.                             <span class="description">Check this if you want to include retweets in your feed.</span>
  851.                         </td>
  852.                     </tr>      
  853.                     <tr>
  854.                         <th scope="row"><label for="twitget_links_new_window">Open Twitter feed links in new window</label></th>
  855.                         <td>
  856.                             <input type="checkbox" name="twitget_links_new_window" id="twitget_links_new_window" value="true" <?php if($twitget_options['links_new_window'] == true) { ?>checked="checked"<?php } ?> />
  857.                             <br />
  858.                             <span class="description">Check this if you want URLs in Twitter feed to open in a new window.</span>
  859.                         </td>
  860.                     </tr>
  861.                     <tr>
  862.                         <th scope="row"><label for="twitget_exclude_replies">Exclude replies</label></th>
  863.                         <td>
  864.                             <input type="checkbox" name="twitget_exclude_replies" id="twitget_exclude_replies" value="true" <?php if($twitget_options['exclude_replies'] == true) { ?>checked="checked"<?php } ?> />
  865.                             <br />
  866.                             <span class="description">Check this if you want to exclude replies in your feed.</span>
  867.                         </td>
  868.                     </tr>
  869.                     <tr>
  870.                         <th scope="row"><label for="truncate_tweet">Truncate Tweets</label></th>
  871.                         <td>
  872.                             <input type="checkbox" name="truncate_tweet" id="truncate_tweet" value="true" <?php if($twitget_options['truncate_tweet'] == true) { ?>checked="checked"<?php } ?> />
  873.                             <br />
  874.                             <span class="description">Check this if you want to truncate the size of tweets (set number of characters below).</span>
  875.                         </td>
  876.                     </tr>      
  877.                     <tr>
  878.                         <th scope="row"><label for="truncate_tweet_size">Truncate Tweet size</label></th>
  879.                         <td>
  880.                             <input type="text" name="truncate_tweet_size" id="truncate_tweet_size" value="<?php echo esc_html($twitget_options['truncate_tweet_size']); ?>" />
  881.                             <br />
  882.                             <span class="description">Limit the number of outputted characters of a tweet.</span>
  883.                         </td>
  884.                     </tr>
  885.                     <tr>
  886.                         <th scope="row"><label for="twitget_relative_time">Show relative time</label></th>
  887.                         <td>
  888.                             <input type="checkbox" name="twitget_relative_time" id="twitget_relative_time" value="true" <?php if($twitget_options['show_relative_time'] == true) { ?>checked="checked"<?php } ?> />
  889.                             <br />
  890.                             <span class="description">Show relative time of tweets (for instance 5 minutes ago).</span>
  891.                         </td>
  892.                     </tr>      
  893.                     <tr>
  894.                         <th scope="row"><label for="twitget_show_powered">Show powered by message</label></th>
  895.                         <td>
  896.                             <input type="checkbox" name="twitget_show_powered" id="twitget_show_powered" value="true" <?php if($twitget_options['show_powered_by'] == true) { ?>checked="checked"<?php } ?> />
  897.                             <br />
  898.                             <span class="description">Show powered by message, if you decide not to show it, please consider a <a href="http://gum.co/twitget" target="_blank">donation</a>.</span>
  899.                         </td>
  900.                     </tr>      
  901.                 </table>
  902.  
  903.                 <h3>Advanced options</h3>
  904.                 <table class="form-table">
  905.                     <tr>
  906.                         <th scope="row"><label for="twitget_custom_output">Use custom output</label></th>
  907.                         <td>
  908.                             <textarea rows="10" cols="100" name="twitget_custom_output" id="twitget_custom_output" /><?php echo htmlentities($twitget_options['custom_string']); ?></textarea><br />
  909.                             <span class="description">
  910.                             <p>You can enter custom HTML in the box bellow and achieve the output you want.</p>
  911.                             <p>When marking the output of your twitter feed you must include {$tweets_start} at the start of your twitter feed and {$tweets_end} in the end.</p>
  912.  
  913.                             <strong><p>AVAILABLE VARIABLES</p></strong>
  914.                             <strong><p>Used inside of loop</p></strong>
  915.                             {$tweet_text} - the text of the tweet<br />
  916.                             {$tweet_time} - the time of the tweet<br />
  917.                             {$tweet_location} - the location of the tweet (example: Budapest)<br />
  918.                             {$retweet} - outputs a ready retweet link with the text Retweet, opens in new tab<br />
  919.                             {$reply} - outputs a ready reply link with the text Reply, opens in new tab<br />
  920.                             {$favorite} - outputs a favorite link with the text Favorite, opens in new tab<br />
  921.                             {$retweet_link} - returns URL of retweet link<br />
  922.                             {$reply_link} - returns URL of reply link<br />
  923.                             {$favorite_link} - returns URL of favorite link<br />
  924.                             {$tweet_link} - returns URL of tweet<br />
  925.                             <strong><p>Used outside or inside of loop</p></strong>
  926.                             {$profile_image} - the url to the profile image of the user<br />
  927.                             {$user_real_name} - the real name of the user<br />
  928.                             {$user_twitter_name} - username of the twitter user<br />
  929.                             {$url} - website url of the user<br />
  930.                             {$user_description} - description of the user<br />
  931.                             {$user_location} - user location<br />
  932.                             {$follower_count} - number of followers<br />
  933.                             {$friends_count} - number of friends<br />
  934.                             {$profile_image_normal_url} - return URL of tweet profile image - 48x48 px size (if retweet in loop, returns original tweet profile image)<br />
  935.                             {$profile_image_bigger_url} - return URL of tweet profile image - 73x73 px size (if retweet in loop, returns original tweet profile image)<br />
  936.                             {$profile_image_mini_url} - return URL of tweet profile image - 24x24px size (if retweet in loop, returns original tweet profile image)<br />
  937.                             {$profile_image_original_url} - return URL of tweet profile image - original size (if retweet in loop, returns original tweet profile image)<br /><br />
  938.                             </span>
  939.                         </td>
  940.                     </tr>      
  941.                 </table>
  942.                 <p><input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Update options') ?>" /></p>
  943.                 </form>
  944.  
  945.  
  946. <?php
  947.  
  948.     }
  949.        
  950.     // Here, the widget code begins
  951.     class simple_tweet_widget extends WP_Widget {
  952.        
  953.         function simple_tweet_widget() {
  954.             $widget_ops = array('classname' => 'simple_tweet_widget', 'description' => 'Display your recent tweets.' );        
  955.             $this->WP_Widget('simple_tweet_widget', 'Twitget', $widget_ops);
  956.         }
  957.        
  958.         function widget($args, $instance) {
  959.            
  960.             extract($args);
  961.             $title = apply_filters('widget_title', $instance['title']);
  962.            
  963.             echo $before_widget;
  964.  
  965.             if($title) {
  966.                 echo $before_title . $title . $after_title;
  967.             }
  968.            
  969.             // The widget code and the widgeet output
  970.            
  971.             show_recent_tweets();
  972.            
  973.             // End of widget output
  974.            
  975.             echo $after_widget;
  976.            
  977.         }
  978.        
  979.         function update($new_instance, $old_instance) {    
  980.             $instance = $old_instance;
  981.             $instance['title'] = strip_tags($new_instance['title']);
  982.             return $instance;
  983.         }
  984.        
  985.         function form($instance) { 
  986.  
  987.             $title = esc_attr($instance['title']);
  988.        
  989. ?>
  990.  
  991.             <p>
  992.                 <label for="<?php echo $this->get_field_id('title'); ?>">
  993.                     <?php _e('Title: '); ?>
  994.                 </label>
  995.                 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  996.             </p>
  997.  
  998. <?php
  999.  
  1000.         }
  1001.  
  1002.     }
  1003.    
  1004. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement