Plugin Directory

Changeset 907818


Ignore:
Timestamp:
05/04/2014 05:39:00 PM (12 years ago)
Author:
ArnaudBan
Message:

new version 0.6

Location:
widget-embed-lastest-tweets
Files:
8 edited
1 copied

Legend:

Unmodified
Added
Removed
  • widget-embed-lastest-tweets/tags/0.6/js/welt-scripts.js

    r768501 r907818  
    33 * The script to load tweet in ajax
    44 */
    5 
    65
    76jQuery(document).ready( function(){
     
    1211
    1312        var widget_id = current_widget.attr('id');
     13        var widget_data = current_widget.data();
    1414        widget_id = widget_id.replace('welt-', '');
    1515
    1616        var data = {
    1717            action: 'welt_display_tweets',
    18             widget_id: widget_id
     18            widget_id: widget_id,
     19            widget_data: widget_data
    1920        };
    2021
     
    2425            function(response) {
    2526                current_widget.append(response);
     27                twttr.widgets.load();
    2628            }
    2729        );
     
    2931    });
    3032});
     33
  • widget-embed-lastest-tweets/tags/0.6/readme.txt

    r768501 r907818  
    22Contributors: ArnaudBan
    33Tags: twitter, widget, embed
    4 Requires at least: 3
    5 Tested up to: 3.6
     4Requires at least: 3.5
     5Tested up to: 3.9
    66Stable tag: trunk
    77License: GPLv2
     
    99
    1010A Widget to show your latest Tweets.
    11 Visit the option page "Plugins->Widget Embed Latest Tweet" to authentify yourself
     11Visit the option page "Settings->Widget Embed Latest Tweet" to authentify yourself
    1212
    1313
     
    1717
    1818This plugin uses the Twitter API version 1.1. You have to authentify yourself !
    19 Visit the option page "Plugins->Widget Embed Latest Tweet" to do so.
     19Visit the option page "Settings->Widget Embed Latest Tweet" to do so.
    2020
    2121Options :
     
    43431. Upload `/widget-embed-latest-tweets/` to the `/wp-content/plugins/` directory
    44442. Activate the plugin through the 'Plugins' menu in WordPress
    45 3. Visit the option page "Plugins->Widget Embed Latest Tweet" and follow the instructions
     453. Visit the option page "Settings->Widget Embed Latest Tweet" and follow the instructions
    46464. Go to the Widget page, drag and drop the "Widget embed lastest Tweets" widget where you want it to show
    47475. Enter at least your Twitter username
     
    6060
    6161== Changelog ==
     62
     63= 0.6 =
     64
     65* Change text domaine to be ready for wordpress.org "language packs"
     66* Less scripts downloded
     67* Make the widget usable with the widget preview manager
    6268
    6369= 0.5 =
  • widget-embed-lastest-tweets/tags/0.6/welt-option.php

    r768501 r907818  
    1010function welt_add_option() {
    1111    //add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function);
    12     add_plugins_page('Widget Embed Latest Tweet', 'Widget Embed Latest Tweet', 'manage_options', 'welt_options_page', 'welt_options_page_display');
     12    add_options_page('Widget Embed Latest Tweet', 'Widget Embed Latest Tweet', 'manage_options', 'welt_options_page', 'welt_options_page_display');
    1313}
    1414
     
    1717    ?>
    1818    <div class="wrap">
    19         <?php screen_icon(); ?>
     19
    2020        <h2>Widget Embed Latest Tweet</h2>
    2121
    2222        <form action="options.php" method="post">
    2323            <?php
    24             //Output Error
    25             settings_errors();
    2624
    2725            //Output nonce, action, and option_page
     
    4644    register_setting('welt_options_group', 'welt_twitter_oauth_var', 'welt_twitter_oauth_var_validate');
    4745
    48     //On créer une section dans nos options
    4946    //add_settings_section( $id, $title, $callback, $page );
    50     add_settings_section('welt_twitter_oauth_section', __('Twitter connection', 'ab-welt-locales'), 'welt_twitter_oauth_section_text', 'welt_options_page');
     47    add_settings_section('welt_twitter_oauth_section', __('Twitter connection', 'widget-embed-lastest-tweets'), 'welt_twitter_oauth_section_text', 'welt_options_page');
    5148
    5249    //Register a settings field to a settings page and section.
    5350    //add_settings_field( $id, $title, $callback, $page, $section, $args );
    54     add_settings_field('welt_twitter_consumer_key', 'Twitter consumer Key', 'welt_twitter_consumer_key_display', 'welt_options_page', 'welt_twitter_oauth_section');
    55     add_settings_field('welt_twitter_consumer_secret', 'Twitter consumer Secret', 'welt_twitter_consumer_secret_display', 'welt_options_page', 'welt_twitter_oauth_section');
    56     add_settings_field('welt_twitter_access_token', 'Twitter Access Token', 'welt_twitter_access_token_display', 'welt_options_page', 'welt_twitter_oauth_section');
    57     add_settings_field('welt_twitter_access_token_secret', 'Twitter Access Token Secret', 'welt_twitter_access_token_secret_display', 'welt_options_page', 'welt_twitter_oauth_section');
     51    add_settings_field('welt_twitter_consumer_key', 'API key', 'welt_twitter_consumer_key_display', 'welt_options_page', 'welt_twitter_oauth_section');
     52    add_settings_field('welt_twitter_consumer_secret', 'API secret', 'welt_twitter_consumer_secret_display', 'welt_options_page', 'welt_twitter_oauth_section');
     53    add_settings_field('welt_twitter_access_token', 'Access token', 'welt_twitter_access_token_display', 'welt_options_page', 'welt_twitter_oauth_section');
     54    add_settings_field('welt_twitter_access_token_secret', 'Access token secret', 'welt_twitter_access_token_secret_display', 'welt_options_page', 'welt_twitter_oauth_section');
    5855}
    5956
     
    6158function welt_twitter_oauth_section_text() {
    6259    ?>
    63     <h4><?php _e('Why', 'ab-welt-locales'); ?> ?</h4>
     60    <h4><?php _e('Why', 'widget-embed-lastest-tweets'); ?> ?</h4>
    6461    <p>
    65         <?php _e('Twitter\'s API version 1.1 require it', 'ab-welt-locales'); ?>
     62        <?php _e('Twitter\'s API version 1.1 require it', 'widget-embed-lastest-tweets'); ?>
    6663    </p>
    6764
    68     <h4><?php _e('How', 'ab-welt-locales'); ?> ?</h4>
     65    <h4><?php _e('How', 'widget-embed-lastest-tweets'); ?> ?</h4>
    6966
    7067    <p>
    71         <?php _e('Create a new application with your Twitter account. It is very simple, go to this page :', 'ab-welt-locales'); ?>
     68        <?php _e('Create a new application with your Twitter account. It is very simple, go to this page :', 'widget-embed-lastest-tweets'); ?>
    7269        <a href="https://dev.twitter.com/apps" title="dev Twitter apps">https://dev.twitter.com/apps</a>
    7370    </p>
    7471    <p>
    75         <?php _e('Once your application is created ask to create an access token ( Click the blue button at the bottom of the page of your Twitter application ).', 'ab-welt-locales') ?>
     72        <?php _e('Once your application is created ask to create an access token ( Click the blue button at the bottom of the page of your Twitter application ).', 'widget-embed-lastest-tweets') ?>
    7673    </p>
    7774    <p>
    78         <?php _e('You should now have all the information asked bellow !', 'ab-welt-locales') ?>
     75        <?php _e('You should now have all the information asked bellow !', 'widget-embed-lastest-tweets') ?>
    7976    </p>
    8077    <?php
     
    8481
    8582    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    86     $consumer_key = $twitter_oauth_var['consumer_key'];
    87     //Attention le "name" du input doit correspondre au nom de l'option
     83    $consumer_key = isset( $twitter_oauth_var['consumer_key'] ) ? $twitter_oauth_var['consumer_key'] : '';
     84
    8885    ?>
    8986    <input id='welt_twitter_oauth_var[consumer_key]' name='welt_twitter_oauth_var[consumer_key]' type='text' value='<?php echo $consumer_key; ?>' class="widefat"/>
     
    9390
    9491    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    95     $consumer_secret = $twitter_oauth_var['consumer_secret'];
    96     //Attention le "name" du input doit correspondre au nom de l'option
     92    $consumer_secret = isset( $twitter_oauth_var['consumer_secret'] ) ? $twitter_oauth_var['consumer_secret'] : '';
    9793    ?>
    98     <input id='welt_twitter_oauth_var[consumer_secret]' name='welt_twitter_oauth_var[consumer_secret]' type='text' value='<?php echo $consumer_secret; ?>' class="widefat"/>
     94    <input id='welt_twitter_oauth_var[consumer_secret]' name='welt_twitter_oauth_var[consumer_secret]' type='password' value='<?php echo $consumer_secret; ?>' class="widefat"/>
    9995    <?php
    10096}
     
    10298
    10399    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    104     $token_key = $twitter_oauth_var['token_key'];
    105     //Attention le "name" du input doit correspondre au nom de l'option
     100    $token_key = isset( $twitter_oauth_var['token_key'] ) ? $twitter_oauth_var['token_key'] : '';
    106101    ?>
    107102    <input id='welt_twitter_oauth_var[token_key]' name='welt_twitter_oauth_var[token_key]' type='text' value='<?php echo $token_key; ?>' class="widefat"/>
     
    111106
    112107    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    113     $token_secret = $twitter_oauth_var['token_secret'];
    114     //Attention le "name" du input doit correspondre au nom de l'option
     108    $token_secret = isset( $twitter_oauth_var['token_secret'] ) ? $twitter_oauth_var['token_secret'] : '';
    115109    ?>
    116110    <input id='welt_twitter_oauth_var[token_secret]' name='welt_twitter_oauth_var[token_secret]' type='text' value='<?php echo $token_secret; ?>' class="widefat"/>
     
    123117
    124118    foreach ($twitter_variable as $option => $value) {
     119
    125120        if( !empty( $value ) ){
    126             $valid_option[$option] = $value;
     121
     122            $valid_option[$option] = sanitize_text_field( $value );
    127123        }
     124
    128125    }
    129126
  • widget-embed-lastest-tweets/tags/0.6/widget-embed-latest-tweets.php

    r768501 r907818  
    44 * Plugin URI: http://arnaudban.me/blog/portfolio/widget-embed-latest-tweets/
    55 * 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.5
     6 * Version: 0.6
    77 * Author: Arnaud Banvillet
    88 * Author URI: http://arnaudban.me
    99 * License: GPL2
     10 *
     11 * Text Domain: widget-embed-lastest-tweets
     12 * Domain Path: /languages
    1013 *
    1114 * Copyright 2012  Arnaud Banvillet  (email : arnaud.banvillet@gmail.com )
     
    3134            'title'            => 'Latest Tweets',
    3235            'count'            => 3,
     36            'maxwidth'         => '',
    3337            'align'            => 'none',
    3438            'hide_thread'      => true,
     
    4852                        'welt_last_tweets', // Base ID
    4953                        'Widget embed latest Tweets', // Name
    50                         array('description' => __('Show your latest Tweets', 'ab-welt-locales'))// Args
     54                        array('description' => __('Show your latest Tweets', 'widget-embed-lastest-tweets'))// Args
    5155        );
    5256    }
     
    7579        echo $before_widget;
    7680
    77         if ( !empty( $title ) )
     81        if ( !empty( $title ) ){
    7882            echo $before_title . $title . $after_title;
    79 
    80         if( !empty( $screen_name ) )
    81             echo '<div id="welt-' . $this->id . '" class="welt-tweet-wrapper"></div>';
     83        }
     84
     85        if( !empty( $screen_name ) ){
     86
     87            if( $this->is_preview() ){
     88
     89                $data = ' ';
     90                foreach ($instance as $key => $value) {
     91
     92                    if( !empty( $value ) ){
     93                        $data .= "data-{$key}='$value' ";
     94                    }
     95                }
     96
     97                echo "<div id='welt-{$this->id}' class='welt-tweet-wrapper'$data></div>";
     98
     99            } else {
     100
     101                echo "<div id='welt-{$this->id}' class='welt-tweet-wrapper'></div>";
     102            }
     103        }
    82104
    83105        echo $after_widget;
     
    99121        $instance = wp_parse_args($instance, $this->defaut);
    100122
    101         $instance['title'] = strip_tags($new_instance['title']);
    102 
    103         $instance['screen_name'] = strip_tags($new_instance['screen_name']);
    104 
    105         $count = strip_tags($new_instance['count']);
    106 
    107         if( is_numeric($count))
     123        $instance['title'] = sanitize_text_field($new_instance['title']);
     124
     125        $instance['screen_name'] = sanitize_text_field($new_instance['screen_name']);
     126
     127        $count = intval( $new_instance['count'] );
     128        if( $count != 0 ){
    108129            $instance['count'] = $count ;
    109 
    110         $maxwidth = strip_tags($new_instance['maxwidth']);
    111         if( is_numeric( $maxwidth ) || empty( $maxwidth ))
    112             $instance['maxwidth'] = $maxwidth;
    113 
    114 
    115         if(in_array($new_instance['align'], $this->align_possible_value ))
     130        }
     131
     132        $maxwidth = intval($new_instance['maxwidth']);
     133        $instance['maxwidth'] = $maxwidth != 0 ? $maxwidth : '';
     134
     135        if( in_array($new_instance['align'], $this->align_possible_value ) ){
    116136            $instance['align'] = $new_instance['align'];
    117 
    118         $instance['hide_thread'] = $new_instance['hide_thread'] == 'hide_thread';
    119         $instance['hide_media'] = $new_instance['hide_media'] == 'hide_media';
    120         $instance['exclude_replies'] = $new_instance['exclude_replies'] == 'exclude_replies';
    121 
    122         $instance['lang'] = strip_tags($new_instance['lang']);
    123 
    124         //When everythings is check, set the transient
    125         welt_set_tweet_transient( $this->id, $instance , true );
     137        }
     138
     139        $instance['hide_thread'] = isset( $new_instance['hide_thread'] ) && $new_instance['hide_thread'] == 'hide_thread';
     140        $instance['hide_media'] = isset( $new_instance['hide_media'] ) && $new_instance['hide_media'] == 'hide_media';
     141        $instance['exclude_replies'] = isset( $new_instance['exclude_replies'] ) && $new_instance['exclude_replies'] == 'exclude_replies';
     142
     143        $instance['lang'] = sanitize_text_field($new_instance['lang']);
     144
     145        // When everythings is check and we are not in the customizer, set the transient
     146        if( ! $this->is_preview() ){
     147            welt_set_tweet_transient( $this->id, $instance , true );
     148        }
     149
    126150
    127151        return $instance;
     
    140164        extract($instance);
    141165
    142         ?>
    143         <p>
    144             <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?> :</label>
    145             <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; ?>" />
    146         </p>
    147 
    148         <p>
    149             <label for="<?php echo $this->get_field_id('screen_name'); ?>"><?php _e('Twitter Username', 'ab-welt-locales') ?> :</label>
    150             <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 if( isset($screen_name) ) echo $screen_name; ?>" />
    151         </p>
    152 
    153         <p>
    154             <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Number of Tweet to display', 'ab-welt-locales') ?> :</label>
    155             <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; ?>" />
    156         </p>
    157 
    158         <p>
    159             <label for="<?php echo $this->get_field_id('maxwidth'); ?>"><?php _e('Width') ?> :</label>
    160             <input id="<?php echo $this->get_field_id('maxwidth'); ?>" name="<?php echo $this->get_field_name('maxwidth'); ?>" type="number" step="1" min="250" max="550" value="<?php echo $maxwidth; ?>" />
    161             <br />
    162             <span class="description"><?php _e('Twitter says :This value is constrained to be between 250 and 550 pixels', 'ab-welt-locales') ?></span>
    163 
    164         </p>
    165 
    166         <p>
    167             <label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('Alignment') ?> :</label>
    168             <select id="<?php echo $this->get_field_id('align'); ?>" name="<?php echo $this->get_field_name('align'); ?>">
    169                 <?php foreach( $this->align_possible_value as $value ) { ?>
    170                     <option value="<?php echo $value ?>" <?php selected($value, $align, true) ?>><?php echo $value ?></option>
    171                 <?php } ?>
    172             </select>
    173         </p>
    174 
    175         <p>
    176             <label for="<?php echo $this->get_field_id('hide_thread'); ?>"><?php _e('Hide Thread', 'ab-welt-locales') ?> :</label>
    177             <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"/>
    178             <br />
    179             <span class="description"><?php _e('Hide the original message in the case that the embedded Tweet is a reply', 'ab-welt-locales') ?></span>
    180         </p>
    181         <p>
    182             <label for="<?php echo $this->get_field_id('hide_media'); ?>"><?php _e('Hide Media', 'ab-welt-locales') ?> :</label>
    183             <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"/>
    184             <br />
    185             <span class="description"><?php _e('Hide the images in the Tweet' , 'ab-welt-locales') ?></span>
    186         </p>
    187 
    188         <p>
    189             <label for="<?php echo $this->get_field_id('exclude_replies'); ?>"><?php _e('Exclude replies', 'ab-welt-locales') ?> :</label>
    190             <input id="<?php echo $this->get_field_id('exclude_replies'); ?>" name="<?php echo $this->get_field_name('exclude_replies'); ?>" type="checkbox" <?php checked( $exclude_replies ) ?> value="exclude_replies"/>
    191             <br />
    192             <span class="description"><?php _e('They will not show but they will count in the number of tweets' , 'ab-welt-locales') ?></span>
    193         </p>
    194 
    195         <p>
    196             <label for="<?php echo $this->get_field_id('lang'); ?>"><?php _e('Language', 'ab-welt-locales') ?> :</label>
    197             <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"/>
    198             <br />
    199             <span class="description"><?php _e('Two firsts caractere only. Example : "fr" for french', 'ab-welt-locales') ?></span>
    200         </p>
    201         <?php
     166        $twitter_oauth_var = get_option('welt_twitter_oauth_var');
     167
     168        // Are all the options there ?
     169        if( is_array( $twitter_oauth_var ) && count($twitter_oauth_var) == 4 ){
     170
     171            ?>
     172            <p>
     173                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?> :</label>
     174                <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; ?>" />
     175            </p>
     176
     177            <p>
     178                <label for="<?php echo $this->get_field_id('screen_name'); ?>"><?php _e('Twitter Username', 'widget-embed-lastest-tweets') ?> :</label>
     179                <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 if( isset($screen_name) ) echo $screen_name; ?>" />
     180            </p>
     181
     182            <p>
     183                <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Number of Tweet to display', 'widget-embed-lastest-tweets') ?> :</label>
     184                <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; ?>" />
     185            </p>
     186
     187            <p>
     188                <label for="<?php echo $this->get_field_id('maxwidth'); ?>"><?php _e('Width') ?> :</label>
     189                <input id="<?php echo $this->get_field_id('maxwidth'); ?>" name="<?php echo $this->get_field_name('maxwidth'); ?>" type="number" step="1" min="250" max="550" value="<?php echo $maxwidth; ?>" />
     190                <br />
     191                <span class="description"><?php _e('Twitter says :This value is constrained to be between 250 and 550 pixels', 'widget-embed-lastest-tweets') ?></span>
     192
     193            </p>
     194
     195            <p>
     196                <label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('Alignment') ?> :</label>
     197                <select id="<?php echo $this->get_field_id('align'); ?>" name="<?php echo $this->get_field_name('align'); ?>">
     198                    <?php foreach( $this->align_possible_value as $value ) { ?>
     199                        <option value="<?php echo $value ?>" <?php selected($value, $align, true) ?>><?php echo $value ?></option>
     200                    <?php } ?>
     201                </select>
     202            </p>
     203
     204            <p>
     205                <label for="<?php echo $this->get_field_id('hide_thread'); ?>"><?php _e('Hide Thread', 'widget-embed-lastest-tweets') ?> :</label>
     206                <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"/>
     207                <br />
     208                <span class="description"><?php _e('Hide the original message in the case that the embedded Tweet is a reply', 'widget-embed-lastest-tweets') ?></span>
     209            </p>
     210            <p>
     211                <label for="<?php echo $this->get_field_id('hide_media'); ?>"><?php _e('Hide Media', 'widget-embed-lastest-tweets') ?> :</label>
     212                <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"/>
     213                <br />
     214                <span class="description"><?php _e('Hide the images in the Tweet' , 'widget-embed-lastest-tweets') ?></span>
     215            </p>
     216
     217            <p>
     218                <label for="<?php echo $this->get_field_id('exclude_replies'); ?>"><?php _e('Exclude replies', 'widget-embed-lastest-tweets') ?> :</label>
     219                <input id="<?php echo $this->get_field_id('exclude_replies'); ?>" name="<?php echo $this->get_field_name('exclude_replies'); ?>" type="checkbox" <?php checked( $exclude_replies ) ?> value="exclude_replies"/>
     220                <br />
     221                <span class="description"><?php _e('They will not show but they will count in the number of tweets' , 'widget-embed-lastest-tweets') ?></span>
     222            </p>
     223
     224            <p>
     225                <label for="<?php echo $this->get_field_id('lang'); ?>"><?php _e('Language', 'widget-embed-lastest-tweets') ?> :</label>
     226                <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"/>
     227                <br />
     228                <span class="description"><?php _e('Two firsts caractere only. Example : "fr" for french', 'widget-embed-lastest-tweets') ?></span>
     229            </p>
     230            <?php
     231
     232        } else {
     233            ?>
     234            <p>
     235                <?php printf( __('You have to enter your <a href="%s">Twitter connections information first</a>', 'widget-embed-lastest-tweets'), 'options-general.php?page=welt_options_page' ) ?>
     236            </p>
     237            <?php
     238        }
    202239    }
    203240
     
    214251    * @param boolean $update
    215252    */
    216 function welt_set_tweet_transient( $widget_id, $options, $update = false){
     253function welt_set_tweet_transient( $widget_id, $options ){
    217254
    218255    $return_value = false;
    219256
     257    $last_tweet = welt_get_latest_tweet( $options );
     258
     259    if( $last_tweet && is_array( $last_tweet ) ){
     260
     261        set_transient('last_tweet_' . $widget_id , $last_tweet, 60 * 5);
     262
     263            foreach ($last_tweet as $tweet_id) {
     264
     265                $tweet_html = welt_get_tweet_html( $tweet_id, $options );
     266
     267                if( $tweet_html ){
     268                    set_transient('last_tweet_html_' . $tweet_id, $tweet_html, ( 24 * WEEK_IN_SECONDS ) ); // 6 mouths
     269                }
     270
     271            }
     272    } else {
     273
     274        delete_transient( 'last_tweet_' . $widget_id );
     275    }
     276}
     277
     278/**
     279 * Get twitter connection based on the option
     280 *
     281 */
     282function welt_get_twitter_connection(){
     283
    220284    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    221285
     286    $connection = false;
    222287    //Check if wee use the authentification methode. We need to have all the key and secret.
    223288    if( is_array( $twitter_oauth_var ) && count($twitter_oauth_var) == 4 ){
    224289
    225290        $connection = new TwitterOAuth($twitter_oauth_var['consumer_key'], $twitter_oauth_var['consumer_secret'], $twitter_oauth_var['token_key'],$twitter_oauth_var['token_secret']);
     291
     292    }
     293
     294    return $connection;
     295}
     296
     297/**
     298 * get the lastest tweet
     299 */
     300function welt_get_latest_tweet( $options ){
     301
     302    $connection = welt_get_twitter_connection();
     303
     304    // If the connection is ok let get the twitter information
     305    if( $connection ){
     306
    226307        $last_tweet = $connection->get('https://api.twitter.com/1.1/statuses/user_timeline.json', $options );
    227308
     
    229310        if( isset( $last_tweet->errors ) ){
    230311
    231             delete_transient( 'last_tweet_' . $widget_id );
    232312            $return_value = $last_tweet->errors[0]->message;
     313            error_log($last_tweet->errors[0]->message);
    233314
    234315        // If there is nothing
    235316        } elseif( $last_tweet == false || empty( $last_tweet )){
    236317
    237             delete_transient( 'last_tweet_' . $widget_id );
    238318            $return_value = false;
    239319
     
    241321        } else {
    242322
    243             $return_value = $last_tweet;
    244 
    245             set_transient('last_tweet_' . $widget_id , $last_tweet, 60 * 5);
    246 
    247             foreach ($last_tweet as $tweet) {
    248 
    249                 $id = $tweet->id_str;
    250 
    251 
    252                 if( $update || ! get_transient('last_tweet_html_' . $id)){
    253 
    254 
    255                     $options['id'] = $id;
    256 
    257                     if( empty( $maxwidth) ){
    258                         unset( $options['maxwidth']);
    259                     }
    260 
    261                     $last_tweet_html = $connection->get('https://api.twitter.com/1.1/statuses/oembed.json', $options);
    262 
    263 
    264                     set_transient('last_tweet_html_' . $id, $last_tweet_html );
    265 
    266                 }
     323            $return_value = array();
     324            foreach ( $last_tweet as $tweet) {
     325                $return_value[] = $tweet->id_str;
    267326            }
    268         }
     327
     328        }
     329
     330    } else {
     331
     332        $return_value = 'Connection error';
     333
     334    }
     335
     336    return $return_value;
     337}
     338
     339function welt_get_tweet_html( $tweet_id, $options ){
     340
     341    $connection = welt_get_twitter_connection();
     342
     343    // If the connection is ok let get the twitter information
     344    if( $connection ){
     345        $options['id'] = $tweet_id;
     346        $options['omit_script'] = true;
     347
     348        if( empty( $maxwidth) ){
     349            unset( $options['maxwidth']);
     350        }
     351
     352        $tweet = $connection->get('https://api.twitter.com/1.1/statuses/oembed.json', $options);
     353
     354        if( isset( $tweet->errors ) ){
     355
     356            $return_value = 'false';
     357            error_log( $last_tweet->errors[0]->message );
     358
     359        } else {
     360
     361            $return_value = $tweet->html;
     362        }
     363
     364    } else {
     365
     366        $return_value = 'false';
    269367    }
    270368
     
    282380    $tweet_html = '';
    283381
    284     $last_tweet = get_transient('last_tweet_' . $widget_id);
    285 
    286 
    287     if( false === $last_tweet ) {
    288 
    289         // Get the widget instance
    290         $all_instance_widget = get_option('widget_welt_last_tweets');
    291 
    292         $widget_real_id = str_replace('welt_last_tweets-', '', $widget_id);
    293 
    294         $instance = $all_instance_widget[$widget_real_id];
    295 
    296         // Set the transient for this widget
    297         $last_tweet = welt_set_tweet_transient( $widget_id, $instance, false );
    298 
    299     }
    300 
    301     if( is_string( $last_tweet ) ){ // It is a error
    302 
    303         echo $last_tweet;
    304 
    305     } elseif ( $last_tweet != false ){
    306 
    307         foreach ($last_tweet as $tweet) {
    308 
    309             $tweet_id = $tweet->id_str;
    310 
    311             $last_tweet_html = get_transient('last_tweet_html_' . $tweet_id);
    312 
    313             $tweet_html .= $last_tweet_html->html;
    314 
    315         }
     382    // In preview mode we passe all instance in data html attrribut
     383    if( isset( $_POST['widget_data'] ) && ! empty( $_POST['widget_data'] ) ){
     384
     385        $instance = $_POST['widget_data'];
     386
     387        $last_tweet = welt_get_latest_tweet( $instance );
     388
     389        foreach ($last_tweet as $tweet_id) {
     390
     391            $tweet_html .= welt_get_tweet_html( $tweet_id, $instance );
     392
     393        }
     394
     395
    316396    } else {
    317         $tweet_html = __('Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'ab-welt-locales');
    318     }
    319 
     397
     398
     399        $last_tweet = get_transient('last_tweet_' . $widget_id);
     400
     401        if( false === $last_tweet ) {
     402
     403            // Get the widget instance
     404            $all_instance_widget = get_option('widget_welt_last_tweets');
     405
     406            $widget_real_id = str_replace('welt_last_tweets-', '', $widget_id);
     407
     408            $instance = $all_instance_widget[$widget_real_id];
     409
     410
     411            // Set the transient for this widget
     412            $last_tweet = welt_get_latest_tweet( $instance );
     413        }
     414
     415        if( $last_tweet && is_array( $last_tweet ) ){
     416
     417            set_transient('last_tweet_' . $widget_id , $last_tweet, 60 * 5);
     418
     419            foreach ($last_tweet as $tweet_id) {
     420
     421                // retrocompatibility
     422                if( is_object( $tweet_id ) ){
     423                    $tweet_id = $tweet_id->id_str;
     424                }
     425
     426                $tweet_html_transient = get_transient('last_tweet_html_' . $tweet_id);
     427
     428                if( false === $tweet_html_transient ){
     429
     430                    $tweet_html_transient = welt_get_tweet_html( $tweet_id, $options );
     431
     432                    if( $tweet_html_transient ){
     433                        set_transient('last_tweet_html_' . $tweet_id, $tweet_html, ( 24 * WEEK_IN_SECONDS ) ); // 6 mouths
     434                        $tweet_html .= $tweet_html_transient;
     435                    }
     436
     437                } else {
     438
     439                    // retrocompatibility
     440                    if( is_object( $tweet_html_transient ) ){
     441                        $tweet_html_transient = $tweet_html_transient->html;
     442                    }
     443                    $tweet_html .= $tweet_html_transient;
     444                }
     445
     446            }
     447        }
     448
     449    }
    320450
    321451    echo $tweet_html;
     
    332462function welt_enqueue_scripts(){
    333463    // welt
    334     wp_register_script('welt_script', plugins_url('/js/welt-scripts.js', __FILE__) , array( 'jquery' ), '20130129', true );
     464    wp_register_script('welt_twitter_script', '//platform.twitter.com/widgets.js' , array(), '1.1', true );
     465    wp_register_script('welt_script', plugins_url('/js/welt-scripts.js', __FILE__) , array( 'jquery', 'welt_twitter_script' ), '20130129', true );
    335466
    336467}
     
    347478    }
    348479
    349   load_plugin_textdomain( 'ab-welt-locales', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
     480  load_plugin_textdomain( 'widget-embed-lastest-tweets', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
    350481}
    351482add_action('plugins_loaded', 'welt_plugin_init');
     
    358489// Add settings link on plugin page
    359490function welt_add_settings_link($links) {
    360   $settings_link = '<a href="plugins.php?page=welt_options_page">' . __('Settings') . '</a>';
     491  $settings_link = '<a href="options-general.php?page=welt_options_page">' . __('Settings') . '</a>';
    361492  array_unshift($links, $settings_link);
    362493  return $links;
  • widget-embed-lastest-tweets/trunk/js/welt-scripts.js

    r768501 r907818  
    33 * The script to load tweet in ajax
    44 */
    5 
    65
    76jQuery(document).ready( function(){
     
    1211
    1312        var widget_id = current_widget.attr('id');
     13        var widget_data = current_widget.data();
    1414        widget_id = widget_id.replace('welt-', '');
    1515
    1616        var data = {
    1717            action: 'welt_display_tweets',
    18             widget_id: widget_id
     18            widget_id: widget_id,
     19            widget_data: widget_data
    1920        };
    2021
     
    2425            function(response) {
    2526                current_widget.append(response);
     27                twttr.widgets.load();
    2628            }
    2729        );
     
    2931    });
    3032});
     33
  • widget-embed-lastest-tweets/trunk/readme.txt

    r768501 r907818  
    22Contributors: ArnaudBan
    33Tags: twitter, widget, embed
    4 Requires at least: 3
    5 Tested up to: 3.6
     4Requires at least: 3.5
     5Tested up to: 3.9
    66Stable tag: trunk
    77License: GPLv2
     
    99
    1010A Widget to show your latest Tweets.
    11 Visit the option page "Plugins->Widget Embed Latest Tweet" to authentify yourself
     11Visit the option page "Settings->Widget Embed Latest Tweet" to authentify yourself
    1212
    1313
     
    1717
    1818This plugin uses the Twitter API version 1.1. You have to authentify yourself !
    19 Visit the option page "Plugins->Widget Embed Latest Tweet" to do so.
     19Visit the option page "Settings->Widget Embed Latest Tweet" to do so.
    2020
    2121Options :
     
    43431. Upload `/widget-embed-latest-tweets/` to the `/wp-content/plugins/` directory
    44442. Activate the plugin through the 'Plugins' menu in WordPress
    45 3. Visit the option page "Plugins->Widget Embed Latest Tweet" and follow the instructions
     453. Visit the option page "Settings->Widget Embed Latest Tweet" and follow the instructions
    46464. Go to the Widget page, drag and drop the "Widget embed lastest Tweets" widget where you want it to show
    47475. Enter at least your Twitter username
     
    6060
    6161== Changelog ==
     62
     63= 0.6 =
     64
     65* Change text domaine to be ready for wordpress.org "language packs"
     66* Less scripts downloded
     67* Make the widget usable with the widget preview manager
    6268
    6369= 0.5 =
  • widget-embed-lastest-tweets/trunk/welt-option.php

    r768501 r907818  
    1010function welt_add_option() {
    1111    //add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function);
    12     add_plugins_page('Widget Embed Latest Tweet', 'Widget Embed Latest Tweet', 'manage_options', 'welt_options_page', 'welt_options_page_display');
     12    add_options_page('Widget Embed Latest Tweet', 'Widget Embed Latest Tweet', 'manage_options', 'welt_options_page', 'welt_options_page_display');
    1313}
    1414
     
    1717    ?>
    1818    <div class="wrap">
    19         <?php screen_icon(); ?>
     19
    2020        <h2>Widget Embed Latest Tweet</h2>
    2121
    2222        <form action="options.php" method="post">
    2323            <?php
    24             //Output Error
    25             settings_errors();
    2624
    2725            //Output nonce, action, and option_page
     
    4644    register_setting('welt_options_group', 'welt_twitter_oauth_var', 'welt_twitter_oauth_var_validate');
    4745
    48     //On créer une section dans nos options
    4946    //add_settings_section( $id, $title, $callback, $page );
    50     add_settings_section('welt_twitter_oauth_section', __('Twitter connection', 'ab-welt-locales'), 'welt_twitter_oauth_section_text', 'welt_options_page');
     47    add_settings_section('welt_twitter_oauth_section', __('Twitter connection', 'widget-embed-lastest-tweets'), 'welt_twitter_oauth_section_text', 'welt_options_page');
    5148
    5249    //Register a settings field to a settings page and section.
    5350    //add_settings_field( $id, $title, $callback, $page, $section, $args );
    54     add_settings_field('welt_twitter_consumer_key', 'Twitter consumer Key', 'welt_twitter_consumer_key_display', 'welt_options_page', 'welt_twitter_oauth_section');
    55     add_settings_field('welt_twitter_consumer_secret', 'Twitter consumer Secret', 'welt_twitter_consumer_secret_display', 'welt_options_page', 'welt_twitter_oauth_section');
    56     add_settings_field('welt_twitter_access_token', 'Twitter Access Token', 'welt_twitter_access_token_display', 'welt_options_page', 'welt_twitter_oauth_section');
    57     add_settings_field('welt_twitter_access_token_secret', 'Twitter Access Token Secret', 'welt_twitter_access_token_secret_display', 'welt_options_page', 'welt_twitter_oauth_section');
     51    add_settings_field('welt_twitter_consumer_key', 'API key', 'welt_twitter_consumer_key_display', 'welt_options_page', 'welt_twitter_oauth_section');
     52    add_settings_field('welt_twitter_consumer_secret', 'API secret', 'welt_twitter_consumer_secret_display', 'welt_options_page', 'welt_twitter_oauth_section');
     53    add_settings_field('welt_twitter_access_token', 'Access token', 'welt_twitter_access_token_display', 'welt_options_page', 'welt_twitter_oauth_section');
     54    add_settings_field('welt_twitter_access_token_secret', 'Access token secret', 'welt_twitter_access_token_secret_display', 'welt_options_page', 'welt_twitter_oauth_section');
    5855}
    5956
     
    6158function welt_twitter_oauth_section_text() {
    6259    ?>
    63     <h4><?php _e('Why', 'ab-welt-locales'); ?> ?</h4>
     60    <h4><?php _e('Why', 'widget-embed-lastest-tweets'); ?> ?</h4>
    6461    <p>
    65         <?php _e('Twitter\'s API version 1.1 require it', 'ab-welt-locales'); ?>
     62        <?php _e('Twitter\'s API version 1.1 require it', 'widget-embed-lastest-tweets'); ?>
    6663    </p>
    6764
    68     <h4><?php _e('How', 'ab-welt-locales'); ?> ?</h4>
     65    <h4><?php _e('How', 'widget-embed-lastest-tweets'); ?> ?</h4>
    6966
    7067    <p>
    71         <?php _e('Create a new application with your Twitter account. It is very simple, go to this page :', 'ab-welt-locales'); ?>
     68        <?php _e('Create a new application with your Twitter account. It is very simple, go to this page :', 'widget-embed-lastest-tweets'); ?>
    7269        <a href="https://dev.twitter.com/apps" title="dev Twitter apps">https://dev.twitter.com/apps</a>
    7370    </p>
    7471    <p>
    75         <?php _e('Once your application is created ask to create an access token ( Click the blue button at the bottom of the page of your Twitter application ).', 'ab-welt-locales') ?>
     72        <?php _e('Once your application is created ask to create an access token ( Click the blue button at the bottom of the page of your Twitter application ).', 'widget-embed-lastest-tweets') ?>
    7673    </p>
    7774    <p>
    78         <?php _e('You should now have all the information asked bellow !', 'ab-welt-locales') ?>
     75        <?php _e('You should now have all the information asked bellow !', 'widget-embed-lastest-tweets') ?>
    7976    </p>
    8077    <?php
     
    8481
    8582    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    86     $consumer_key = $twitter_oauth_var['consumer_key'];
    87     //Attention le "name" du input doit correspondre au nom de l'option
     83    $consumer_key = isset( $twitter_oauth_var['consumer_key'] ) ? $twitter_oauth_var['consumer_key'] : '';
     84
    8885    ?>
    8986    <input id='welt_twitter_oauth_var[consumer_key]' name='welt_twitter_oauth_var[consumer_key]' type='text' value='<?php echo $consumer_key; ?>' class="widefat"/>
     
    9390
    9491    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    95     $consumer_secret = $twitter_oauth_var['consumer_secret'];
    96     //Attention le "name" du input doit correspondre au nom de l'option
     92    $consumer_secret = isset( $twitter_oauth_var['consumer_secret'] ) ? $twitter_oauth_var['consumer_secret'] : '';
    9793    ?>
    98     <input id='welt_twitter_oauth_var[consumer_secret]' name='welt_twitter_oauth_var[consumer_secret]' type='text' value='<?php echo $consumer_secret; ?>' class="widefat"/>
     94    <input id='welt_twitter_oauth_var[consumer_secret]' name='welt_twitter_oauth_var[consumer_secret]' type='password' value='<?php echo $consumer_secret; ?>' class="widefat"/>
    9995    <?php
    10096}
     
    10298
    10399    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    104     $token_key = $twitter_oauth_var['token_key'];
    105     //Attention le "name" du input doit correspondre au nom de l'option
     100    $token_key = isset( $twitter_oauth_var['token_key'] ) ? $twitter_oauth_var['token_key'] : '';
    106101    ?>
    107102    <input id='welt_twitter_oauth_var[token_key]' name='welt_twitter_oauth_var[token_key]' type='text' value='<?php echo $token_key; ?>' class="widefat"/>
     
    111106
    112107    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    113     $token_secret = $twitter_oauth_var['token_secret'];
    114     //Attention le "name" du input doit correspondre au nom de l'option
     108    $token_secret = isset( $twitter_oauth_var['token_secret'] ) ? $twitter_oauth_var['token_secret'] : '';
    115109    ?>
    116110    <input id='welt_twitter_oauth_var[token_secret]' name='welt_twitter_oauth_var[token_secret]' type='text' value='<?php echo $token_secret; ?>' class="widefat"/>
     
    123117
    124118    foreach ($twitter_variable as $option => $value) {
     119
    125120        if( !empty( $value ) ){
    126             $valid_option[$option] = $value;
     121
     122            $valid_option[$option] = sanitize_text_field( $value );
    127123        }
     124
    128125    }
    129126
  • widget-embed-lastest-tweets/trunk/widget-embed-latest-tweets.php

    r768501 r907818  
    44 * Plugin URI: http://arnaudban.me/blog/portfolio/widget-embed-latest-tweets/
    55 * 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.5
     6 * Version: 0.6
    77 * Author: Arnaud Banvillet
    88 * Author URI: http://arnaudban.me
    99 * License: GPL2
     10 *
     11 * Text Domain: widget-embed-lastest-tweets
     12 * Domain Path: /languages
    1013 *
    1114 * Copyright 2012  Arnaud Banvillet  (email : arnaud.banvillet@gmail.com )
     
    3134            'title'            => 'Latest Tweets',
    3235            'count'            => 3,
     36            'maxwidth'         => '',
    3337            'align'            => 'none',
    3438            'hide_thread'      => true,
     
    4852                        'welt_last_tweets', // Base ID
    4953                        'Widget embed latest Tweets', // Name
    50                         array('description' => __('Show your latest Tweets', 'ab-welt-locales'))// Args
     54                        array('description' => __('Show your latest Tweets', 'widget-embed-lastest-tweets'))// Args
    5155        );
    5256    }
     
    7579        echo $before_widget;
    7680
    77         if ( !empty( $title ) )
     81        if ( !empty( $title ) ){
    7882            echo $before_title . $title . $after_title;
    79 
    80         if( !empty( $screen_name ) )
    81             echo '<div id="welt-' . $this->id . '" class="welt-tweet-wrapper"></div>';
     83        }
     84
     85        if( !empty( $screen_name ) ){
     86
     87            if( $this->is_preview() ){
     88
     89                $data = ' ';
     90                foreach ($instance as $key => $value) {
     91
     92                    if( !empty( $value ) ){
     93                        $data .= "data-{$key}='$value' ";
     94                    }
     95                }
     96
     97                echo "<div id='welt-{$this->id}' class='welt-tweet-wrapper'$data></div>";
     98
     99            } else {
     100
     101                echo "<div id='welt-{$this->id}' class='welt-tweet-wrapper'></div>";
     102            }
     103        }
    82104
    83105        echo $after_widget;
     
    99121        $instance = wp_parse_args($instance, $this->defaut);
    100122
    101         $instance['title'] = strip_tags($new_instance['title']);
    102 
    103         $instance['screen_name'] = strip_tags($new_instance['screen_name']);
    104 
    105         $count = strip_tags($new_instance['count']);
    106 
    107         if( is_numeric($count))
     123        $instance['title'] = sanitize_text_field($new_instance['title']);
     124
     125        $instance['screen_name'] = sanitize_text_field($new_instance['screen_name']);
     126
     127        $count = intval( $new_instance['count'] );
     128        if( $count != 0 ){
    108129            $instance['count'] = $count ;
    109 
    110         $maxwidth = strip_tags($new_instance['maxwidth']);
    111         if( is_numeric( $maxwidth ) || empty( $maxwidth ))
    112             $instance['maxwidth'] = $maxwidth;
    113 
    114 
    115         if(in_array($new_instance['align'], $this->align_possible_value ))
     130        }
     131
     132        $maxwidth = intval($new_instance['maxwidth']);
     133        $instance['maxwidth'] = $maxwidth != 0 ? $maxwidth : '';
     134
     135        if( in_array($new_instance['align'], $this->align_possible_value ) ){
    116136            $instance['align'] = $new_instance['align'];
    117 
    118         $instance['hide_thread'] = $new_instance['hide_thread'] == 'hide_thread';
    119         $instance['hide_media'] = $new_instance['hide_media'] == 'hide_media';
    120         $instance['exclude_replies'] = $new_instance['exclude_replies'] == 'exclude_replies';
    121 
    122         $instance['lang'] = strip_tags($new_instance['lang']);
    123 
    124         //When everythings is check, set the transient
    125         welt_set_tweet_transient( $this->id, $instance , true );
     137        }
     138
     139        $instance['hide_thread'] = isset( $new_instance['hide_thread'] ) && $new_instance['hide_thread'] == 'hide_thread';
     140        $instance['hide_media'] = isset( $new_instance['hide_media'] ) && $new_instance['hide_media'] == 'hide_media';
     141        $instance['exclude_replies'] = isset( $new_instance['exclude_replies'] ) && $new_instance['exclude_replies'] == 'exclude_replies';
     142
     143        $instance['lang'] = sanitize_text_field($new_instance['lang']);
     144
     145        // When everythings is check and we are not in the customizer, set the transient
     146        if( ! $this->is_preview() ){
     147            welt_set_tweet_transient( $this->id, $instance , true );
     148        }
     149
    126150
    127151        return $instance;
     
    140164        extract($instance);
    141165
    142         ?>
    143         <p>
    144             <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?> :</label>
    145             <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; ?>" />
    146         </p>
    147 
    148         <p>
    149             <label for="<?php echo $this->get_field_id('screen_name'); ?>"><?php _e('Twitter Username', 'ab-welt-locales') ?> :</label>
    150             <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 if( isset($screen_name) ) echo $screen_name; ?>" />
    151         </p>
    152 
    153         <p>
    154             <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Number of Tweet to display', 'ab-welt-locales') ?> :</label>
    155             <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; ?>" />
    156         </p>
    157 
    158         <p>
    159             <label for="<?php echo $this->get_field_id('maxwidth'); ?>"><?php _e('Width') ?> :</label>
    160             <input id="<?php echo $this->get_field_id('maxwidth'); ?>" name="<?php echo $this->get_field_name('maxwidth'); ?>" type="number" step="1" min="250" max="550" value="<?php echo $maxwidth; ?>" />
    161             <br />
    162             <span class="description"><?php _e('Twitter says :This value is constrained to be between 250 and 550 pixels', 'ab-welt-locales') ?></span>
    163 
    164         </p>
    165 
    166         <p>
    167             <label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('Alignment') ?> :</label>
    168             <select id="<?php echo $this->get_field_id('align'); ?>" name="<?php echo $this->get_field_name('align'); ?>">
    169                 <?php foreach( $this->align_possible_value as $value ) { ?>
    170                     <option value="<?php echo $value ?>" <?php selected($value, $align, true) ?>><?php echo $value ?></option>
    171                 <?php } ?>
    172             </select>
    173         </p>
    174 
    175         <p>
    176             <label for="<?php echo $this->get_field_id('hide_thread'); ?>"><?php _e('Hide Thread', 'ab-welt-locales') ?> :</label>
    177             <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"/>
    178             <br />
    179             <span class="description"><?php _e('Hide the original message in the case that the embedded Tweet is a reply', 'ab-welt-locales') ?></span>
    180         </p>
    181         <p>
    182             <label for="<?php echo $this->get_field_id('hide_media'); ?>"><?php _e('Hide Media', 'ab-welt-locales') ?> :</label>
    183             <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"/>
    184             <br />
    185             <span class="description"><?php _e('Hide the images in the Tweet' , 'ab-welt-locales') ?></span>
    186         </p>
    187 
    188         <p>
    189             <label for="<?php echo $this->get_field_id('exclude_replies'); ?>"><?php _e('Exclude replies', 'ab-welt-locales') ?> :</label>
    190             <input id="<?php echo $this->get_field_id('exclude_replies'); ?>" name="<?php echo $this->get_field_name('exclude_replies'); ?>" type="checkbox" <?php checked( $exclude_replies ) ?> value="exclude_replies"/>
    191             <br />
    192             <span class="description"><?php _e('They will not show but they will count in the number of tweets' , 'ab-welt-locales') ?></span>
    193         </p>
    194 
    195         <p>
    196             <label for="<?php echo $this->get_field_id('lang'); ?>"><?php _e('Language', 'ab-welt-locales') ?> :</label>
    197             <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"/>
    198             <br />
    199             <span class="description"><?php _e('Two firsts caractere only. Example : "fr" for french', 'ab-welt-locales') ?></span>
    200         </p>
    201         <?php
     166        $twitter_oauth_var = get_option('welt_twitter_oauth_var');
     167
     168        // Are all the options there ?
     169        if( is_array( $twitter_oauth_var ) && count($twitter_oauth_var) == 4 ){
     170
     171            ?>
     172            <p>
     173                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?> :</label>
     174                <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; ?>" />
     175            </p>
     176
     177            <p>
     178                <label for="<?php echo $this->get_field_id('screen_name'); ?>"><?php _e('Twitter Username', 'widget-embed-lastest-tweets') ?> :</label>
     179                <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 if( isset($screen_name) ) echo $screen_name; ?>" />
     180            </p>
     181
     182            <p>
     183                <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Number of Tweet to display', 'widget-embed-lastest-tweets') ?> :</label>
     184                <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; ?>" />
     185            </p>
     186
     187            <p>
     188                <label for="<?php echo $this->get_field_id('maxwidth'); ?>"><?php _e('Width') ?> :</label>
     189                <input id="<?php echo $this->get_field_id('maxwidth'); ?>" name="<?php echo $this->get_field_name('maxwidth'); ?>" type="number" step="1" min="250" max="550" value="<?php echo $maxwidth; ?>" />
     190                <br />
     191                <span class="description"><?php _e('Twitter says :This value is constrained to be between 250 and 550 pixels', 'widget-embed-lastest-tweets') ?></span>
     192
     193            </p>
     194
     195            <p>
     196                <label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('Alignment') ?> :</label>
     197                <select id="<?php echo $this->get_field_id('align'); ?>" name="<?php echo $this->get_field_name('align'); ?>">
     198                    <?php foreach( $this->align_possible_value as $value ) { ?>
     199                        <option value="<?php echo $value ?>" <?php selected($value, $align, true) ?>><?php echo $value ?></option>
     200                    <?php } ?>
     201                </select>
     202            </p>
     203
     204            <p>
     205                <label for="<?php echo $this->get_field_id('hide_thread'); ?>"><?php _e('Hide Thread', 'widget-embed-lastest-tweets') ?> :</label>
     206                <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"/>
     207                <br />
     208                <span class="description"><?php _e('Hide the original message in the case that the embedded Tweet is a reply', 'widget-embed-lastest-tweets') ?></span>
     209            </p>
     210            <p>
     211                <label for="<?php echo $this->get_field_id('hide_media'); ?>"><?php _e('Hide Media', 'widget-embed-lastest-tweets') ?> :</label>
     212                <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"/>
     213                <br />
     214                <span class="description"><?php _e('Hide the images in the Tweet' , 'widget-embed-lastest-tweets') ?></span>
     215            </p>
     216
     217            <p>
     218                <label for="<?php echo $this->get_field_id('exclude_replies'); ?>"><?php _e('Exclude replies', 'widget-embed-lastest-tweets') ?> :</label>
     219                <input id="<?php echo $this->get_field_id('exclude_replies'); ?>" name="<?php echo $this->get_field_name('exclude_replies'); ?>" type="checkbox" <?php checked( $exclude_replies ) ?> value="exclude_replies"/>
     220                <br />
     221                <span class="description"><?php _e('They will not show but they will count in the number of tweets' , 'widget-embed-lastest-tweets') ?></span>
     222            </p>
     223
     224            <p>
     225                <label for="<?php echo $this->get_field_id('lang'); ?>"><?php _e('Language', 'widget-embed-lastest-tweets') ?> :</label>
     226                <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"/>
     227                <br />
     228                <span class="description"><?php _e('Two firsts caractere only. Example : "fr" for french', 'widget-embed-lastest-tweets') ?></span>
     229            </p>
     230            <?php
     231
     232        } else {
     233            ?>
     234            <p>
     235                <?php printf( __('You have to enter your <a href="%s">Twitter connections information first</a>', 'widget-embed-lastest-tweets'), 'options-general.php?page=welt_options_page' ) ?>
     236            </p>
     237            <?php
     238        }
    202239    }
    203240
     
    214251    * @param boolean $update
    215252    */
    216 function welt_set_tweet_transient( $widget_id, $options, $update = false){
     253function welt_set_tweet_transient( $widget_id, $options ){
    217254
    218255    $return_value = false;
    219256
     257    $last_tweet = welt_get_latest_tweet( $options );
     258
     259    if( $last_tweet && is_array( $last_tweet ) ){
     260
     261        set_transient('last_tweet_' . $widget_id , $last_tweet, 60 * 5);
     262
     263            foreach ($last_tweet as $tweet_id) {
     264
     265                $tweet_html = welt_get_tweet_html( $tweet_id, $options );
     266
     267                if( $tweet_html ){
     268                    set_transient('last_tweet_html_' . $tweet_id, $tweet_html, ( 24 * WEEK_IN_SECONDS ) ); // 6 mouths
     269                }
     270
     271            }
     272    } else {
     273
     274        delete_transient( 'last_tweet_' . $widget_id );
     275    }
     276}
     277
     278/**
     279 * Get twitter connection based on the option
     280 *
     281 */
     282function welt_get_twitter_connection(){
     283
    220284    $twitter_oauth_var = get_option('welt_twitter_oauth_var');
    221285
     286    $connection = false;
    222287    //Check if wee use the authentification methode. We need to have all the key and secret.
    223288    if( is_array( $twitter_oauth_var ) && count($twitter_oauth_var) == 4 ){
    224289
    225290        $connection = new TwitterOAuth($twitter_oauth_var['consumer_key'], $twitter_oauth_var['consumer_secret'], $twitter_oauth_var['token_key'],$twitter_oauth_var['token_secret']);
     291
     292    }
     293
     294    return $connection;
     295}
     296
     297/**
     298 * get the lastest tweet
     299 */
     300function welt_get_latest_tweet( $options ){
     301
     302    $connection = welt_get_twitter_connection();
     303
     304    // If the connection is ok let get the twitter information
     305    if( $connection ){
     306
    226307        $last_tweet = $connection->get('https://api.twitter.com/1.1/statuses/user_timeline.json', $options );
    227308
     
    229310        if( isset( $last_tweet->errors ) ){
    230311
    231             delete_transient( 'last_tweet_' . $widget_id );
    232312            $return_value = $last_tweet->errors[0]->message;
     313            error_log($last_tweet->errors[0]->message);
    233314
    234315        // If there is nothing
    235316        } elseif( $last_tweet == false || empty( $last_tweet )){
    236317
    237             delete_transient( 'last_tweet_' . $widget_id );
    238318            $return_value = false;
    239319
     
    241321        } else {
    242322
    243             $return_value = $last_tweet;
    244 
    245             set_transient('last_tweet_' . $widget_id , $last_tweet, 60 * 5);
    246 
    247             foreach ($last_tweet as $tweet) {
    248 
    249                 $id = $tweet->id_str;
    250 
    251 
    252                 if( $update || ! get_transient('last_tweet_html_' . $id)){
    253 
    254 
    255                     $options['id'] = $id;
    256 
    257                     if( empty( $maxwidth) ){
    258                         unset( $options['maxwidth']);
    259                     }
    260 
    261                     $last_tweet_html = $connection->get('https://api.twitter.com/1.1/statuses/oembed.json', $options);
    262 
    263 
    264                     set_transient('last_tweet_html_' . $id, $last_tweet_html );
    265 
    266                 }
     323            $return_value = array();
     324            foreach ( $last_tweet as $tweet) {
     325                $return_value[] = $tweet->id_str;
    267326            }
    268         }
     327
     328        }
     329
     330    } else {
     331
     332        $return_value = 'Connection error';
     333
     334    }
     335
     336    return $return_value;
     337}
     338
     339function welt_get_tweet_html( $tweet_id, $options ){
     340
     341    $connection = welt_get_twitter_connection();
     342
     343    // If the connection is ok let get the twitter information
     344    if( $connection ){
     345        $options['id'] = $tweet_id;
     346        $options['omit_script'] = true;
     347
     348        if( empty( $maxwidth) ){
     349            unset( $options['maxwidth']);
     350        }
     351
     352        $tweet = $connection->get('https://api.twitter.com/1.1/statuses/oembed.json', $options);
     353
     354        if( isset( $tweet->errors ) ){
     355
     356            $return_value = 'false';
     357            error_log( $last_tweet->errors[0]->message );
     358
     359        } else {
     360
     361            $return_value = $tweet->html;
     362        }
     363
     364    } else {
     365
     366        $return_value = 'false';
    269367    }
    270368
     
    282380    $tweet_html = '';
    283381
    284     $last_tweet = get_transient('last_tweet_' . $widget_id);
    285 
    286 
    287     if( false === $last_tweet ) {
    288 
    289         // Get the widget instance
    290         $all_instance_widget = get_option('widget_welt_last_tweets');
    291 
    292         $widget_real_id = str_replace('welt_last_tweets-', '', $widget_id);
    293 
    294         $instance = $all_instance_widget[$widget_real_id];
    295 
    296         // Set the transient for this widget
    297         $last_tweet = welt_set_tweet_transient( $widget_id, $instance, false );
    298 
    299     }
    300 
    301     if( is_string( $last_tweet ) ){ // It is a error
    302 
    303         echo $last_tweet;
    304 
    305     } elseif ( $last_tweet != false ){
    306 
    307         foreach ($last_tweet as $tweet) {
    308 
    309             $tweet_id = $tweet->id_str;
    310 
    311             $last_tweet_html = get_transient('last_tweet_html_' . $tweet_id);
    312 
    313             $tweet_html .= $last_tweet_html->html;
    314 
    315         }
     382    // In preview mode we passe all instance in data html attrribut
     383    if( isset( $_POST['widget_data'] ) && ! empty( $_POST['widget_data'] ) ){
     384
     385        $instance = $_POST['widget_data'];
     386
     387        $last_tweet = welt_get_latest_tweet( $instance );
     388
     389        foreach ($last_tweet as $tweet_id) {
     390
     391            $tweet_html .= welt_get_tweet_html( $tweet_id, $instance );
     392
     393        }
     394
     395
    316396    } else {
    317         $tweet_html = __('Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'ab-welt-locales');
    318     }
    319 
     397
     398
     399        $last_tweet = get_transient('last_tweet_' . $widget_id);
     400
     401        if( false === $last_tweet ) {
     402
     403            // Get the widget instance
     404            $all_instance_widget = get_option('widget_welt_last_tweets');
     405
     406            $widget_real_id = str_replace('welt_last_tweets-', '', $widget_id);
     407
     408            $instance = $all_instance_widget[$widget_real_id];
     409
     410
     411            // Set the transient for this widget
     412            $last_tweet = welt_get_latest_tweet( $instance );
     413        }
     414
     415        if( $last_tweet && is_array( $last_tweet ) ){
     416
     417            set_transient('last_tweet_' . $widget_id , $last_tweet, 60 * 5);
     418
     419            foreach ($last_tweet as $tweet_id) {
     420
     421                // retrocompatibility
     422                if( is_object( $tweet_id ) ){
     423                    $tweet_id = $tweet_id->id_str;
     424                }
     425
     426                $tweet_html_transient = get_transient('last_tweet_html_' . $tweet_id);
     427
     428                if( false === $tweet_html_transient ){
     429
     430                    $tweet_html_transient = welt_get_tweet_html( $tweet_id, $options );
     431
     432                    if( $tweet_html_transient ){
     433                        set_transient('last_tweet_html_' . $tweet_id, $tweet_html, ( 24 * WEEK_IN_SECONDS ) ); // 6 mouths
     434                        $tweet_html .= $tweet_html_transient;
     435                    }
     436
     437                } else {
     438
     439                    // retrocompatibility
     440                    if( is_object( $tweet_html_transient ) ){
     441                        $tweet_html_transient = $tweet_html_transient->html;
     442                    }
     443                    $tweet_html .= $tweet_html_transient;
     444                }
     445
     446            }
     447        }
     448
     449    }
    320450
    321451    echo $tweet_html;
     
    332462function welt_enqueue_scripts(){
    333463    // welt
    334     wp_register_script('welt_script', plugins_url('/js/welt-scripts.js', __FILE__) , array( 'jquery' ), '20130129', true );
     464    wp_register_script('welt_twitter_script', '//platform.twitter.com/widgets.js' , array(), '1.1', true );
     465    wp_register_script('welt_script', plugins_url('/js/welt-scripts.js', __FILE__) , array( 'jquery', 'welt_twitter_script' ), '20130129', true );
    335466
    336467}
     
    347478    }
    348479
    349   load_plugin_textdomain( 'ab-welt-locales', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
     480  load_plugin_textdomain( 'widget-embed-lastest-tweets', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
    350481}
    351482add_action('plugins_loaded', 'welt_plugin_init');
     
    358489// Add settings link on plugin page
    359490function welt_add_settings_link($links) {
    360   $settings_link = '<a href="plugins.php?page=welt_options_page">' . __('Settings') . '</a>';
     491  $settings_link = '<a href="options-general.php?page=welt_options_page">' . __('Settings') . '</a>';
    361492  array_unshift($links, $settings_link);
    362493  return $links;
Note: See TracChangeset for help on using the changeset viewer.