Plugin Directory

Changeset 588909


Ignore:
Timestamp:
08/22/2012 03:53:12 PM (14 years ago)
Author:
ArnaudBan
Message:

add authentification

Location:
widget-embed-lastest-tweets/trunk
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • widget-embed-lastest-tweets/trunk/README.md

    r586780 r588909  
    22
    33A Widget to show your latest tweets. Use the oEmbed methode and some cache. It is simple, elegant and it works.
    4 Just type your Twitter username.
     4
     5You can authentified your widget and get rid of the Twitter's API limitation from anonymous request. See the option page of the plugin : plugins -> Widget Embed Latest Tweets
    56
    67Options :
     8
    79* number of tweet to display
    810* Maximum width
  • widget-embed-lastest-tweets/trunk/readme.txt

    r586839 r588909  
    1414
    1515A Widget to show your latest tweets. Use the oEmbed methode and some cache. It is simple, elegant and it works.
    16 Just type your Twitter username.
     16
     17You can authentified your widget and get rid of the Twitter's API limitation from anonymous request. See the option page of the plugin : plugins -> Widget Embed Latest Tweets
    1718
    1819Options :
     20
    1921* number of tweet to display
    2022* Maximum width
    2123* Alignment (left, right, center, none)
     24* Choose your language
     25* Hide the original message in the case that the embedded Tweet is a reply
    2226
    2327Note that with the oEmbed method you can not customise easily the display of your tweet.
     
    4549== Changelog ==
    4650
     51= 0.3 =
     52
     53* You can authentified your widget and get read of the Twitter's API limitation from anonymous request
     54* Choose your language
     55* Hide the original message in the case that the embedded Tweet is a reply
     56
    4757= 0.2 =
    48 Don't show Warning in case of error
     58
     59* Don't show Warning in case of error
    4960
    5061= 0.1 =
    51 This is the first version.
    52 Choose the number of tweet, the maximum width and alignment
     62
     63* This is the first version.
     64* Choose the number of tweet, the maximum width and alignment
    5365
    5466== Upgrade Notice ==
  • widget-embed-lastest-tweets/trunk/widget-embed-latest-tweets.php

    r586839 r588909  
    11<?php
    22/*
    3  * Plugin Name: Widget embed lastest Tweets
     3 * Plugin Name: Widget embed latest Tweets
    44 * Plugin URI: http://www.arnaudbanvillet.com/blog/portfolio/widget-embed-latest-tweets/
    55 * Description: A Widget to show your latest tweets. Use the oEmbed methode and some cache. It is simple, elegant and it works. Just type your user name and the numbers of tweets you want to show.
    6  * Version: 0.2
     6 * Version: 0.3
    77 * Author: Arnaud Banvillet
    88 * Author URI: http://www.arnaudbanvillet.com
     
    2929
    3030    var $defaut = array(
    31             'nb-tweets' => 3,
    32             'align'         => 'none'
     31            'title'             => 'Last Tweet',
     32            'count'             => 3,
     33            'align'             => 'none',
     34            'hide_thread' => true,
     35            'lang'              => 'en',
     36            'include_rts'   => true
    3337    );
    34     var $align_possible_value = array('left', 'right', 'center', 'none');
     38
     39    var $align_possible_value = array('none', 'left', 'right', 'center');
    3540
    3641    /**
     
    4045        parent::__construct(
    4146                        'last_tweets', // Base ID
    42                         'Widget embed lastet Tweets', // Name
    43                         array('description' => __('Show your last tweets', 'ab-welt-locales'))// Args
     47                        'Widget embed latest Tweets', // Name
     48                        array('description' => __('Show your latest tweets', 'ab-welt-locales'))// Args
    4449        );
    4550    }
     
    6772            echo $before_title . $title . $after_title;
    6873
    69         if ($user_name) {
     74        if( !empty( $screen_name ) ){
    7075
    7176            $last_tweet = get_transient('last_tweet');
     
    7378            if (false === $last_tweet) {
    7479
    75                 $options = array(
    76                                 'nb_tweets' => $nb_tweets,
    77                                 'maxwidth'  => $maxwidth,
    78                                 'align'         => $align
    79                             );
    80 
    81 
    82                 $this->welt_set_tweet_transient($user_name, $options);
     80
     81                $this->welt_set_tweet_transient( $instance );
    8382
    8483
     
    120119        $instance = wp_parse_args($instance, $this->defaut);
    121120
    122         $nb_tweet = strip_tags($new_instance['nb_tweets']);
     121        $instance['title'] = strip_tags($new_instance['title']);
     122
     123        $instance['screen_name'] = strip_tags($new_instance['screen_name']);
     124
     125        $count = strip_tags($new_instance['count']);
     126
     127        if( is_numeric($count))
     128            $instance['count'] = $count ;
     129
    123130        $maxwidth = strip_tags($new_instance['maxwidth']);
    124 
    125         $instance['title'] = strip_tags($new_instance['title']);
    126         $instance['user_name'] = strip_tags($new_instance['user_name']);
    127 
    128         if( is_numeric( $nb_tweet ) )
    129             $instance['nb_tweets'] = $nb_tweet;
    130131        if( is_numeric( $maxwidth ) )
    131132            $instance['maxwidth'] = $maxwidth;
    132133
     134
    133135        if(in_array($new_instance['align'], $this->align_possible_value ))
    134136            $instance['align'] = $new_instance['align'];
    135137
    136         $options = array(
    137                                 'nb_tweets' => $instance['nb_tweets'],
    138                                 'maxwidth'  => $instance['maxwidth'],
    139                                 'align'         => $instance['align']
    140                             );
    141         $this->welt_set_tweet_transient( $instance['user_name'], $options , true);
     138        $instance['hide_thread'] = $new_instance['hide_thread'] == 'hide_thread';
     139
     140        $instance['lang'] = strip_tags($new_instance['lang']);
     141
     142        $this->welt_set_tweet_transient( $instance , true);
    142143
    143144        return $instance;
     
    152153     */
    153154    public function form($instance) {
    154         if ($instance) {
    155             $title = esc_attr($instance['title']);
    156             $user_name = esc_attr($instance['user_name']);
    157             $nb_tweets = esc_attr($instance['nb_tweets']);
    158             $maxwidth = esc_attr($instance['maxwidth']);
    159             $align = $instance['align'];
    160         } else {
    161             $title = 'Last Tweet';
    162         }
     155
     156        $instance = wp_parse_args($instance, $this->defaut);
     157        extract($instance);
     158
    163159        ?>
    164160        <p>
     
    168164
    169165        <p>
    170             <label for="<?php echo $this->get_field_id('user_name'); ?>"><?php _e('Twitter Usernam :', 'ab-welt-locales') ?></label>
    171             <input class="widefat" id="<?php echo $this->get_field_id('user_name'); ?>" name="<?php echo $this->get_field_name('user_name'); ?>" type="text" value="<?php echo $user_name; ?>" />
    172         </p>
    173 
    174         <p>
    175             <label for="<?php echo $this->get_field_id('nb_tweets'); ?>"><?php _e('Number of tweet to dislay :', 'ab-welt-locales') ?></label>
    176             <input id="<?php echo $this->get_field_id('nb_tweets'); ?>" name="<?php echo $this->get_field_name('nb_tweets'); ?>" type="number" step="1" min="1" max="20" value="<?php echo $nb_tweets; ?>" />
     166            <label for="<?php echo $this->get_field_id('screen_name'); ?>"><?php _e('Twitter Usernam :', 'ab-welt-locales') ?></label>
     167            <input class="widefat" id="<?php echo $this->get_field_id('screen_name'); ?>" name="<?php echo $this->get_field_name('screen_name'); ?>" type="text" value="<?php echo $screen_name; ?>" />
     168        </p>
     169
     170        <p>
     171            <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Number of tweet to dislay :', 'ab-welt-locales') ?></label>
     172            <input id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="number" step="1" min="1" max="20" value="<?php echo $count; ?>" />
    177173        </p>
    178174
     
    190186            </select>
    191187        </p>
     188
     189        <p>
     190            <label for="<?php echo $this->get_field_id('hide_thread'); ?>"><?php _e('Hide Thread :', 'ab-welt-locales') ?></label>
     191            <input id="<?php echo $this->get_field_id('hide_thread'); ?>" name="<?php echo $this->get_field_name('hide_thread'); ?>" type="checkbox" <?php checked( $hide_thread ) ?> value="hide_thread"/>
     192            <br />
     193            <span class="description"><?php _e('Hide the original message in the case that the embedded Tweet is a reply') ?></span>
     194        </p>
     195
     196        <p>
     197            <label for="<?php echo $this->get_field_id('lang'); ?>"><?php _e('Language :', 'ab-welt-locales') ?></label>
     198            <input id="<?php echo $this->get_field_id('lang'); ?>" name="<?php echo $this->get_field_name('lang'); ?>" type="text" value="<?php echo $lang; ?>" size="2"/>
     199            <br />
     200            <span class="description"><?php _e('Two firsts caractere only. Example : "fr" for french') ?></span>
     201        </p>
     202        <?php if( get_option('welt_twitter_authentification') ) { ?>
     203            <p class="description">
     204                <?php _e('You are authentified', 'ab-welt-locales'); ?>
     205            </p>
    192206        <?php
     207        }
     208
     209        //var_dump( get_option('welt_twitter_http_hreader') );
    193210    }
    194211
     
    197214     * And the page load faster with this cache.
    198215     *
    199      * @param string $user_name
    200216     * @param array $options
    201217     * @param boolean $update
    202218     */
    203     private function welt_set_tweet_transient( $user_name, $options, $update = false){
    204 
    205         extract( $options );
    206 
    207         $embed_options = '';
    208 
    209         if( !empty( $maxwidth ) )
    210             $embed_options .= '&maxwidth=' . $maxwidth;
    211         if( !empty( $align ) )
    212             $embed_options .= '&align=' . $align;
    213 
    214         // We use the GET statuses/user_timeline to get the latest tweet
    215         // https://dev.twitter.com/docs/api/1/get/statuses/oembed
    216         $last_tweet = @file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $user_name . '&count=' . $nb_tweets . '&include_rts=1');
    217 
    218 
    219         if( $last_tweet == false ){
     219    private function welt_set_tweet_transient( $options, $update = false){
     220
     221        extract($options);
     222
     223        $twitter_oauth_var = get_option('welt_twitter_oauth_var');
     224
     225
     226        //Check if wee use the authentification methode. We need to have all the key and secret.
     227        $oauth_methode = ! in_array("", $twitter_oauth_var);
     228
     229
     230        //The authentification methode
     231        if( $oauth_methode ){
     232            $connection = new TwitterOAuth($twitter_oauth_var[consumer_key], $twitter_oauth_var[consumer_secret], $twitter_oauth_var[token_key],$twitter_oauth_var[token_secret]);
     233
     234            $last_tweet = $connection->get('http://api.twitter.com/1/statuses/user_timeline.json', $options );
     235
     236        } else {
     237            // We use the GET statuses/user_timeline to get the latest tweet
     238            // https://dev.twitter.com/docs/api/1/get/statuses/oembed
     239            $last_tweet = @file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $screen_name . '&count=' . $count . '&include_rts=1');
     240            $last_tweet = json_decode($last_tweet);
     241        }
     242
     243        if( $last_tweet == false || empty( $last_tweet )){
    220244            delete_transient( 'last_tweet' );
    221245            return;
    222246        }
    223247
    224         $last_tweet = json_decode($last_tweet);
    225 
    226 
    227248        set_transient('last_tweet', $last_tweet, 60 * 5);
    228249
    229250        foreach ($last_tweet as $tweet) {
    230251
     252            $id = $tweet->id_str;
     253
    231254
    232255            if( $update || get_transient('last_tweet_html_' . $id)){
    233256
    234                 $id = $tweet->id_str;
    235 
    236                 // We use the GET statuses/oembed API to get the html to display
    237                 // https://dev.twitter.com/docs/api/1/get/statuses/oembed
    238                 $last_tweet_html = @file_get_contents('https://api.twitter.com/1/statuses/oembed.json?id=' . $id . $embed_options);
    239                 $last_tweet_html = json_decode($last_tweet_html);
     257
     258                if( $oauth_methode ){
     259
     260                    $options['id'] = $id;
     261                    $last_tweet_html = $connection->get('https://api.twitter.com/1/statuses/oembed.json', $options);
     262
     263                    $welt_twitter_http_hreader = $connection->http_info;
     264
     265                    update_option('welt_twitter_authentification', true);
     266
     267                } else {
     268
     269                    // We use the GET statuses/oembed API to get the html to display
     270                    // https://dev.twitter.com/docs/api/1/get/statuses/oembed
     271                    $last_tweet_html = @file_get_contents('https://api.twitter.com/1/statuses/oembed.json?id=' . $id . $embed_options);
     272                    $last_tweet_html = json_decode($last_tweet_html);
     273
     274                    $welt_twitter_http_hreader = $http_response_header;
     275
     276                    update_option('welt_twitter_authentification', false);
     277                }
     278
    240279                set_transient('last_tweet_html_' . $id, $last_tweet_html, 60 * 60 * 24);
    241280
     
    243282
    244283        }
    245     }
    246 
     284
     285        update_option('welt_twitter_http_hreader', $welt_twitter_http_hreader);
     286
     287    }
    247288}
    248289
    249290add_action('widgets_init', create_function('', 'register_widget( "Widget_Embed_Latest_Tweets" );'));
     291
     292//Files needed for the Twitter authentification
     293require_once 'twitteroauth/OAuth.php';
     294require_once 'twitteroauth/twitteroauth.php';
     295require_once 'welt-option.php';
Note: See TracChangeset for help on using the changeset viewer.