Plugin Directory

Changeset 668718


Ignore:
Timestamp:
02/15/2013 10:11:55 PM (13 years ago)
Author:
ArnaudBan
Message:

New version 0.4

Location:
widget-embed-lastest-tweets
Files:
4 added
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • widget-embed-lastest-tweets/tags/0.4/readme.txt

    r624528 r668718  
    33Tags: twitter, widget, embed
    44Requires at least: 3
    5 Tested up to: 3.4.2
     5Tested up to: 3.5.1
    66Stable tag: trunk
    77License: GPLv2
     
    99
    1010A Widget to show your latest Tweets.
    11 Just type your Twitter username.
     11Visit the option page "Plugins->Widget Embed Last Plugin" to authentify yourself
     12
    1213
    1314== Description ==
     
    1516A Widget to show your latest tweets. Use the oEmbed methode and some cache. It is simple, elegant and it works.
    1617
    17 You can authentify 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
     18This plugin uses the Twitter API version 1.1. You have to authentify yourself !
     19Visit the option page "Plugins->Widget Embed Last Plugin" to do so.
    1820
    1921Options :
     
    2426* Choose your language
    2527* Hide the original message in the case that the embedded Tweet is a reply
     28* Hide images in the Tweet
    2629
    2730Note that with the oEmbed method you can not customise easily the display of your Tweet.
     
    5659
    5760== Changelog ==
     61
     62= 0.4 =
     63
     64**Importante update**
     65Be careful, the plugin uses the new API version 1.1
     66This mean that you have to authentify yourself
     67
     68* update to API version 1.1
     69* use AJAX to load the page faster
    5870
    5971= 0.3.7 =
  • widget-embed-lastest-tweets/tags/0.4/widget-embed-latest-tweets.php

    r624528 r668718  
    33 * Plugin Name: Widget embed latest Tweets
    44 * Plugin URI: http://www.arnaudbanvillet.com/blog/portfolio/widget-embed-latest-tweets/
    5  * Description: A Widget to show your latest Tweets. Use the oEmbed methode and some cache.
    6  * Version: 0.3.7
     5 * Description: A Widget to show your latest Tweets. Use the oEmbed methode and some cache. Visit the option page "Plugins->Widget Embed Last Plugin" to authentify yourself
     6 * Version: 0.4
    77 * Author: Arnaud Banvillet
    88 * Author URI: http://www.arnaudbanvillet.com
     
    2929
    3030    var $defaut = array(
    31             'title'         => 'Last Tweet',
    32             'count'         => 3,
    33             'align'         => 'none',
    34             'hide_thread'   => true,
    35             'lang'          => 'en',
    36             'include_rts'   => true
     31            'title'             => 'Last Tweet',
     32            'count'             => 3,
     33            'align'             => 'none',
     34            'hide_thread'   => true,
     35            'lang'              => 'en',
     36            'include_rts'   => true,
     37            'hide_media'    => true
    3738    );
    3839
     
    4445    public function __construct() {
    4546        parent::__construct(
    46                         'last_tweets', // Base ID
     47                        'welt_last_tweets', // Base ID
    4748                        'Widget embed latest Tweets', // Name
    4849                        array('description' => __('Show your latest Tweets', 'ab-welt-locales'))// Args
     
    6970        echo $before_widget;
    7071
    71         if (!empty($title))
     72        if ( !empty( $title ) )
    7273            echo $before_title . $title . $after_title;
    7374
    74         if( !empty( $screen_name ) ){
    75 
    76             $last_tweet = get_transient('last_tweet_' . $this->id);
    77 
    78             if (false === $last_tweet) {
    79 
    80 
    81                 $this->welt_set_tweet_transient( $instance, false );
    82 
    83 
    84                 $last_tweet = get_transient('last_tweet_' .$this->id);
    85 
    86             }
    87 
    88             if( $last_tweet != false ){
    89 
    90                 foreach ($last_tweet as $tweet) {
    91 
    92                     $tweet_id = $tweet->id_str;
    93 
    94                     $last_tweet_html = get_transient('last_tweet_html_' . $tweet_id);
    95 
    96                     echo $last_tweet_html->html;
    97                 }
    98             } else {
    99                 _e('Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'ab-welt-locales');
    100             }
    101         }
     75        if( !empty( $screen_name ) )
     76            echo '<div id="welt-' . $this->id . '" class="welt-tweet-wrapper"></div>';
    10277
    10378        echo $after_widget;
     
    137112
    138113        $instance['hide_thread'] = $new_instance['hide_thread'] == 'hide_thread';
     114        $instance['hide_media'] = $new_instance['hide_media'] == 'hide_media';
    139115
    140116        $instance['lang'] = strip_tags($new_instance['lang']);
    141117
    142         $this->welt_set_tweet_transient( $instance , true );
     118        //When everythings is check, set the transient
     119        welt_set_tweet_transient( $this->id, $instance , true );
    143120
    144121        return $instance;
     
    194171            <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"/>
    195172            <br />
    196             <span class="description"><?php _e('Hide the original message in the case that the embedded Tweet is a reply') ?></span>
     173            <span class="description"><?php _e('Hide the original message in the case that the embedded Tweet is a reply', 'ab-welt-locales') ?></span>
     174        </p>
     175        <p>
     176            <label for="<?php echo $this->get_field_id('hide_media'); ?>"><?php _e('Hide Media', 'ab-welt-locales') ?> :</label>
     177            <input id="<?php echo $this->get_field_id('hide_media'); ?>" name="<?php echo $this->get_field_name('hide_media'); ?>" type="checkbox" <?php checked( $hide_media ) ?> value="hide_media"/>
     178            <br />
     179            <span class="description"><?php _e('Hide the images in the Tweet' , 'ab-welt-locales') ?></span>
    197180        </p>
    198181
     
    203186            <span class="description"><?php _e('Two firsts caractere only. Example : "fr" for french') ?></span>
    204187        </p>
    205         <?php if( get_option('welt_twitter_authentification') ) { ?>
    206             <p class="description">
    207                 <?php _e('You are authentified', 'ab-welt-locales'); ?>
    208             </p>
    209188        <?php
    210         }
    211     }
    212 
    213     /**
    214      * Cache the twitter json file. Twitter say "Store API responses in your application or on your site"
    215      * And the page load faster with this cache.
    216      *
    217      * @param array $options
    218      * @param boolean $update
    219      */
    220     private function welt_set_tweet_transient( $options, $update = false){
    221 
    222         extract($options);
    223 
    224         $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    225 
    226 
    227         //Check if wee use the authentification methode. We need to have all the key and secret.
    228         $oauth_methode = ($twitter_oauth_var == false) ? false : ! in_array("", $twitter_oauth_var);
    229 
    230 
    231         //The authentification methode
    232         if( $oauth_methode ){
    233             $connection = new TwitterOAuth($twitter_oauth_var['consumer_key'], $twitter_oauth_var['consumer_secret'], $twitter_oauth_var['token_key'],$twitter_oauth_var['token_secret']);
    234             $last_tweet = $connection->get('http://api.twitter.com/1/statuses/user_timeline.json', $options );
    235 
     189    }
     190
     191}
     192
     193add_action('widgets_init', create_function('', 'register_widget( "Widget_Embed_Latest_Tweets" );'));
     194
     195
     196/**
     197    * Cache the twitter json file. Twitter say "Store API responses in your application or on your site"
     198    * And the page load faster with this cache.
     199    *
     200    * @param array $options
     201    * @param boolean $update
     202    */
     203function welt_set_tweet_transient( $widget_id, $options, $update = false){
     204
     205    $return_value = false;
     206
     207    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
     208
     209    //Check if wee use the authentification methode. We need to have all the key and secret.
     210    if( is_array( $twitter_oauth_var ) && count($twitter_oauth_var) == 4 ){
     211
     212        $connection = new TwitterOAuth($twitter_oauth_var['consumer_key'], $twitter_oauth_var['consumer_secret'], $twitter_oauth_var['token_key'],$twitter_oauth_var['token_secret']);
     213        $last_tweet = $connection->get('https://api.twitter.com/1.1/statuses/user_timeline.json', $options );
     214
     215        // if there is an error
     216        if( isset( $last_tweet->errors ) ){
     217
     218            delete_transient( 'last_tweet_' . $widget_id );
     219            $return_value = $last_tweet->errors[0]->message;
     220
     221        // If there is nothing
     222        } elseif( $last_tweet == false || empty( $last_tweet )){
     223
     224            delete_transient( 'last_tweet_' . $widget_id );
     225            $return_value = false;
     226
     227        // It should be ok
    236228        } 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 )){
    244             delete_transient( 'last_tweet' );
    245             return;
    246         }
    247 
    248         set_transient('last_tweet_' . $this->id , $last_tweet, 60 * 5);
    249 
    250         foreach ($last_tweet as $tweet) {
    251 
    252             $id = $tweet->id_str;
    253 
    254 
    255             if( $update || ! get_transient('last_tweet_html_' . $id)){
    256 
    257 
    258                 if( $oauth_methode ){
     229
     230            $return_value = $last_tweet;
     231
     232            set_transient('last_tweet_' . $widget_id , $last_tweet, 60 * 5);
     233
     234            foreach ($last_tweet as $tweet) {
     235
     236                $id = $tweet->id_str;
     237
     238
     239                if( $update || ! get_transient('last_tweet_html_' . $id)){
     240
    259241
    260242                    $options['id'] = $id;
     
    264246                    }
    265247
    266                     $last_tweet_html = $connection->get('https://api.twitter.com/1/statuses/oembed.json', $options);
    267 
    268 
    269                     update_option('welt_twitter_authentification', true);
    270 
    271                 } else {
    272 
    273                     // We use the GET statuses/oembed API to get the html to display
    274                     // https://dev.twitter.com/docs/api/1/get/statuses/oembed
    275                     $option_string = 'id=' . $id . '&align=' . $align . '&hide_thread='. $hide_thread .'&lang=' . $lang;
    276 
    277                     if( is_numeric( $maxwidth) ){
    278                         $option_string .= '&maxwidth=' . $maxwidth;
    279                     }
    280 
    281                     $last_tweet_html = @file_get_contents('https://api.twitter.com/1/statuses/oembed.json?' . $option_string);
    282                     $last_tweet_html = json_decode($last_tweet_html);
    283 
    284                     update_option('welt_twitter_authentification', false);
     248                    $last_tweet_html = $connection->get('https://api.twitter.com/1.1/statuses/oembed.json', $options);
     249
     250
     251                    set_transient('last_tweet_html_' . $id, $last_tweet_html );
     252
    285253                }
    286 
    287                 set_transient('last_tweet_html_' . $id, $last_tweet_html, 60 * 60 * 24);
    288 
    289254            }
    290 
    291255        }
    292 
    293     }
    294 }
    295 
    296 add_action('widgets_init', create_function('', 'register_widget( "Widget_Embed_Latest_Tweets" );'));
     256    }
     257
     258    return $return_value;
     259}
     260
     261
     262/**
     263 * The function callback in ajax to display Tweet
     264 */
     265function welt_display_tweets( ){
     266
     267    $widget_id = $_POST['widget_id'];
     268
     269    $tweet_html = '';
     270
     271    $last_tweet = get_transient('last_tweet_' . $widget_id);
     272
     273
     274    if( false === $last_tweet ) {
     275
     276        // Get the widget instance
     277        $all_instance_widget = get_option('widget_welt_last_tweets');
     278
     279        $widget_real_id = str_replace('welt_last_tweets-', '', $widget_id);
     280
     281        $instance = $all_instance_widget[$widget_real_id];
     282
     283        // Set the transient for this widget
     284        $last_tweet = welt_set_tweet_transient( $widget_id, $instance, false );
     285
     286    }
     287
     288    if( is_string( $last_tweet ) ){ // It is a error
     289
     290        echo $last_tweet;
     291
     292    } elseif ( $last_tweet != false ){
     293
     294        foreach ($last_tweet as $tweet) {
     295
     296            $tweet_id = $tweet->id_str;
     297
     298            $last_tweet_html = get_transient('last_tweet_html_' . $tweet_id);
     299
     300            $tweet_html .= $last_tweet_html->html;
     301
     302        }
     303    } else {
     304        $tweet_html = __('Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'ab-welt-locales');
     305    }
     306
     307
     308    echo $tweet_html;
     309    die;
     310}
     311
     312add_action('wp_ajax_welt_display_tweets', 'welt_display_tweets');
     313add_action('wp_ajax_nopriv_welt_display_tweets', 'welt_display_tweets');
     314
     315
     316/**
     317 * Enqueue welt script and Twitter Script
     318 */
     319function welt_enqueue_scripts(){
     320    // welt
     321    wp_enqueue_script('welt_script', plugins_url('/js/welt-scripts.js', __FILE__) , array( 'jquery' ), '20130129', true );
     322    wp_localize_script( 'welt_script', 'ajaxurl', admin_url('admin-ajax.php') );
     323
     324}
     325add_action('wp_enqueue_scripts', 'welt_enqueue_scripts');
    297326
    298327//Files needed for the Twitter authentification
  • widget-embed-lastest-tweets/trunk/readme.txt

    r624528 r668718  
    33Tags: twitter, widget, embed
    44Requires at least: 3
    5 Tested up to: 3.4.2
     5Tested up to: 3.5.1
    66Stable tag: trunk
    77License: GPLv2
     
    99
    1010A Widget to show your latest Tweets.
    11 Just type your Twitter username.
     11Visit the option page "Plugins->Widget Embed Last Plugin" to authentify yourself
     12
    1213
    1314== Description ==
     
    1516A Widget to show your latest tweets. Use the oEmbed methode and some cache. It is simple, elegant and it works.
    1617
    17 You can authentify 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
     18This plugin uses the Twitter API version 1.1. You have to authentify yourself !
     19Visit the option page "Plugins->Widget Embed Last Plugin" to do so.
    1820
    1921Options :
     
    2426* Choose your language
    2527* Hide the original message in the case that the embedded Tweet is a reply
     28* Hide images in the Tweet
    2629
    2730Note that with the oEmbed method you can not customise easily the display of your Tweet.
     
    5659
    5760== Changelog ==
     61
     62= 0.4 =
     63
     64**Importante update**
     65Be careful, the plugin uses the new API version 1.1
     66This mean that you have to authentify yourself
     67
     68* update to API version 1.1
     69* use AJAX to load the page faster
    5870
    5971= 0.3.7 =
  • widget-embed-lastest-tweets/trunk/widget-embed-latest-tweets.php

    r624528 r668718  
    33 * Plugin Name: Widget embed latest Tweets
    44 * Plugin URI: http://www.arnaudbanvillet.com/blog/portfolio/widget-embed-latest-tweets/
    5  * Description: A Widget to show your latest Tweets. Use the oEmbed methode and some cache.
    6  * Version: 0.3.7
     5 * Description: A Widget to show your latest Tweets. Use the oEmbed methode and some cache. Visit the option page "Plugins->Widget Embed Last Plugin" to authentify yourself
     6 * Version: 0.4
    77 * Author: Arnaud Banvillet
    88 * Author URI: http://www.arnaudbanvillet.com
     
    2929
    3030    var $defaut = array(
    31             'title'         => 'Last Tweet',
    32             'count'         => 3,
    33             'align'         => 'none',
    34             'hide_thread'   => true,
    35             'lang'          => 'en',
    36             'include_rts'   => true
     31            'title'             => 'Last Tweet',
     32            'count'             => 3,
     33            'align'             => 'none',
     34            'hide_thread'   => true,
     35            'lang'              => 'en',
     36            'include_rts'   => true,
     37            'hide_media'    => true
    3738    );
    3839
     
    4445    public function __construct() {
    4546        parent::__construct(
    46                         'last_tweets', // Base ID
     47                        'welt_last_tweets', // Base ID
    4748                        'Widget embed latest Tweets', // Name
    4849                        array('description' => __('Show your latest Tweets', 'ab-welt-locales'))// Args
     
    6970        echo $before_widget;
    7071
    71         if (!empty($title))
     72        if ( !empty( $title ) )
    7273            echo $before_title . $title . $after_title;
    7374
    74         if( !empty( $screen_name ) ){
    75 
    76             $last_tweet = get_transient('last_tweet_' . $this->id);
    77 
    78             if (false === $last_tweet) {
    79 
    80 
    81                 $this->welt_set_tweet_transient( $instance, false );
    82 
    83 
    84                 $last_tweet = get_transient('last_tweet_' .$this->id);
    85 
    86             }
    87 
    88             if( $last_tweet != false ){
    89 
    90                 foreach ($last_tweet as $tweet) {
    91 
    92                     $tweet_id = $tweet->id_str;
    93 
    94                     $last_tweet_html = get_transient('last_tweet_html_' . $tweet_id);
    95 
    96                     echo $last_tweet_html->html;
    97                 }
    98             } else {
    99                 _e('Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'ab-welt-locales');
    100             }
    101         }
     75        if( !empty( $screen_name ) )
     76            echo '<div id="welt-' . $this->id . '" class="welt-tweet-wrapper"></div>';
    10277
    10378        echo $after_widget;
     
    137112
    138113        $instance['hide_thread'] = $new_instance['hide_thread'] == 'hide_thread';
     114        $instance['hide_media'] = $new_instance['hide_media'] == 'hide_media';
    139115
    140116        $instance['lang'] = strip_tags($new_instance['lang']);
    141117
    142         $this->welt_set_tweet_transient( $instance , true );
     118        //When everythings is check, set the transient
     119        welt_set_tweet_transient( $this->id, $instance , true );
    143120
    144121        return $instance;
     
    194171            <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"/>
    195172            <br />
    196             <span class="description"><?php _e('Hide the original message in the case that the embedded Tweet is a reply') ?></span>
     173            <span class="description"><?php _e('Hide the original message in the case that the embedded Tweet is a reply', 'ab-welt-locales') ?></span>
     174        </p>
     175        <p>
     176            <label for="<?php echo $this->get_field_id('hide_media'); ?>"><?php _e('Hide Media', 'ab-welt-locales') ?> :</label>
     177            <input id="<?php echo $this->get_field_id('hide_media'); ?>" name="<?php echo $this->get_field_name('hide_media'); ?>" type="checkbox" <?php checked( $hide_media ) ?> value="hide_media"/>
     178            <br />
     179            <span class="description"><?php _e('Hide the images in the Tweet' , 'ab-welt-locales') ?></span>
    197180        </p>
    198181
     
    203186            <span class="description"><?php _e('Two firsts caractere only. Example : "fr" for french') ?></span>
    204187        </p>
    205         <?php if( get_option('welt_twitter_authentification') ) { ?>
    206             <p class="description">
    207                 <?php _e('You are authentified', 'ab-welt-locales'); ?>
    208             </p>
    209188        <?php
    210         }
    211     }
    212 
    213     /**
    214      * Cache the twitter json file. Twitter say "Store API responses in your application or on your site"
    215      * And the page load faster with this cache.
    216      *
    217      * @param array $options
    218      * @param boolean $update
    219      */
    220     private function welt_set_tweet_transient( $options, $update = false){
    221 
    222         extract($options);
    223 
    224         $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    225 
    226 
    227         //Check if wee use the authentification methode. We need to have all the key and secret.
    228         $oauth_methode = ($twitter_oauth_var == false) ? false : ! in_array("", $twitter_oauth_var);
    229 
    230 
    231         //The authentification methode
    232         if( $oauth_methode ){
    233             $connection = new TwitterOAuth($twitter_oauth_var['consumer_key'], $twitter_oauth_var['consumer_secret'], $twitter_oauth_var['token_key'],$twitter_oauth_var['token_secret']);
    234             $last_tweet = $connection->get('http://api.twitter.com/1/statuses/user_timeline.json', $options );
    235 
     189    }
     190
     191}
     192
     193add_action('widgets_init', create_function('', 'register_widget( "Widget_Embed_Latest_Tweets" );'));
     194
     195
     196/**
     197    * Cache the twitter json file. Twitter say "Store API responses in your application or on your site"
     198    * And the page load faster with this cache.
     199    *
     200    * @param array $options
     201    * @param boolean $update
     202    */
     203function welt_set_tweet_transient( $widget_id, $options, $update = false){
     204
     205    $return_value = false;
     206
     207    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
     208
     209    //Check if wee use the authentification methode. We need to have all the key and secret.
     210    if( is_array( $twitter_oauth_var ) && count($twitter_oauth_var) == 4 ){
     211
     212        $connection = new TwitterOAuth($twitter_oauth_var['consumer_key'], $twitter_oauth_var['consumer_secret'], $twitter_oauth_var['token_key'],$twitter_oauth_var['token_secret']);
     213        $last_tweet = $connection->get('https://api.twitter.com/1.1/statuses/user_timeline.json', $options );
     214
     215        // if there is an error
     216        if( isset( $last_tweet->errors ) ){
     217
     218            delete_transient( 'last_tweet_' . $widget_id );
     219            $return_value = $last_tweet->errors[0]->message;
     220
     221        // If there is nothing
     222        } elseif( $last_tweet == false || empty( $last_tweet )){
     223
     224            delete_transient( 'last_tweet_' . $widget_id );
     225            $return_value = false;
     226
     227        // It should be ok
    236228        } 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 )){
    244             delete_transient( 'last_tweet' );
    245             return;
    246         }
    247 
    248         set_transient('last_tweet_' . $this->id , $last_tweet, 60 * 5);
    249 
    250         foreach ($last_tweet as $tweet) {
    251 
    252             $id = $tweet->id_str;
    253 
    254 
    255             if( $update || ! get_transient('last_tweet_html_' . $id)){
    256 
    257 
    258                 if( $oauth_methode ){
     229
     230            $return_value = $last_tweet;
     231
     232            set_transient('last_tweet_' . $widget_id , $last_tweet, 60 * 5);
     233
     234            foreach ($last_tweet as $tweet) {
     235
     236                $id = $tweet->id_str;
     237
     238
     239                if( $update || ! get_transient('last_tweet_html_' . $id)){
     240
    259241
    260242                    $options['id'] = $id;
     
    264246                    }
    265247
    266                     $last_tweet_html = $connection->get('https://api.twitter.com/1/statuses/oembed.json', $options);
    267 
    268 
    269                     update_option('welt_twitter_authentification', true);
    270 
    271                 } else {
    272 
    273                     // We use the GET statuses/oembed API to get the html to display
    274                     // https://dev.twitter.com/docs/api/1/get/statuses/oembed
    275                     $option_string = 'id=' . $id . '&align=' . $align . '&hide_thread='. $hide_thread .'&lang=' . $lang;
    276 
    277                     if( is_numeric( $maxwidth) ){
    278                         $option_string .= '&maxwidth=' . $maxwidth;
    279                     }
    280 
    281                     $last_tweet_html = @file_get_contents('https://api.twitter.com/1/statuses/oembed.json?' . $option_string);
    282                     $last_tweet_html = json_decode($last_tweet_html);
    283 
    284                     update_option('welt_twitter_authentification', false);
     248                    $last_tweet_html = $connection->get('https://api.twitter.com/1.1/statuses/oembed.json', $options);
     249
     250
     251                    set_transient('last_tweet_html_' . $id, $last_tweet_html );
     252
    285253                }
    286 
    287                 set_transient('last_tweet_html_' . $id, $last_tweet_html, 60 * 60 * 24);
    288 
    289254            }
    290 
    291255        }
    292 
    293     }
    294 }
    295 
    296 add_action('widgets_init', create_function('', 'register_widget( "Widget_Embed_Latest_Tweets" );'));
     256    }
     257
     258    return $return_value;
     259}
     260
     261
     262/**
     263 * The function callback in ajax to display Tweet
     264 */
     265function welt_display_tweets( ){
     266
     267    $widget_id = $_POST['widget_id'];
     268
     269    $tweet_html = '';
     270
     271    $last_tweet = get_transient('last_tweet_' . $widget_id);
     272
     273
     274    if( false === $last_tweet ) {
     275
     276        // Get the widget instance
     277        $all_instance_widget = get_option('widget_welt_last_tweets');
     278
     279        $widget_real_id = str_replace('welt_last_tweets-', '', $widget_id);
     280
     281        $instance = $all_instance_widget[$widget_real_id];
     282
     283        // Set the transient for this widget
     284        $last_tweet = welt_set_tweet_transient( $widget_id, $instance, false );
     285
     286    }
     287
     288    if( is_string( $last_tweet ) ){ // It is a error
     289
     290        echo $last_tweet;
     291
     292    } elseif ( $last_tweet != false ){
     293
     294        foreach ($last_tweet as $tweet) {
     295
     296            $tweet_id = $tweet->id_str;
     297
     298            $last_tweet_html = get_transient('last_tweet_html_' . $tweet_id);
     299
     300            $tweet_html .= $last_tweet_html->html;
     301
     302        }
     303    } else {
     304        $tweet_html = __('Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'ab-welt-locales');
     305    }
     306
     307
     308    echo $tweet_html;
     309    die;
     310}
     311
     312add_action('wp_ajax_welt_display_tweets', 'welt_display_tweets');
     313add_action('wp_ajax_nopriv_welt_display_tweets', 'welt_display_tweets');
     314
     315
     316/**
     317 * Enqueue welt script and Twitter Script
     318 */
     319function welt_enqueue_scripts(){
     320    // welt
     321    wp_enqueue_script('welt_script', plugins_url('/js/welt-scripts.js', __FILE__) , array( 'jquery' ), '20130129', true );
     322    wp_localize_script( 'welt_script', 'ajaxurl', admin_url('admin-ajax.php') );
     323
     324}
     325add_action('wp_enqueue_scripts', 'welt_enqueue_scripts');
    297326
    298327//Files needed for the Twitter authentification
Note: See TracChangeset for help on using the changeset viewer.