Plugin Directory

Changeset 590367


Ignore:
Timestamp:
08/25/2012 09:50:27 PM (14 years ago)
Author:
MartyThornley
Message:

Cleaner code and added cache for tweets

Location:
wp-list-tweets/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-list-tweets/trunk/readme.txt

    r227304 r590367  
    33Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=11225299
    44Tags: twitter
    5 Tested up to: 2.9.1
     5Tested up to: 3.4.2
    66Stable tag: trunk
    77
     
    8686Yes. It was programmed with this in mind. Each instance adds a class of the username to the containing ul element.
    8787
    88 Add the follwing to your CSS to style each user:
     88Add the following to your CSS to style each user:
    8989
    9090    ul.username { }
     
    9494== Changelog ==
    9595
     96= 1.3 =
     97* Tested up to 3.4.2
     98* Cleaned up code
     99* CACHING! Added caching for tweets. Tweets will now only be grabbed once per 30 minutes.
     100
    96101= 1.2 =
    97102* Added a quick check to make sure the plugin was actually being used on each page. Before, it was trying to talk to twitter with empty user info ion every page and it really slowed down page loads. Much faster now. :)
  • wp-list-tweets/trunk/wp_list_tweets.php

    r227304 r590367  
    44Plugin URI: http://martythornley.com/downloads/wp-list-tweets
    55Description: Easily List any number of latest Tweets from any user.
    6 Version: 1.2
     6Version: 1.3
    77Author: Marty Thornley
    88Author URI: http://martythornley.com
     
    8787/       this plugin has been used on a given page.
    8888/
    89 *********************************************************************/?>
    90 <?php
    91 
    92 // Empty the constant on each page load to store this page's info:
    93 
    94 function empty_twitter_info() {
    95     $GLOBALS['twitter_info_store'] = '';
    96     $GLOBALS['twitter_list_count'] = 1;
    97 
    98 };
    99 
    100 add_action ('wp_head','empty_twitter_info');
    101 add_action ('admin_head','empty_twitter_info');
    102 
    103 function wp_list_tweets($user='', $number='3', $element = '') {
    104     ob_start();
    105 
    106 // get needed global info
    107    
    108     global $post;
    109    
    110     $defaultUser = get_option('wp_list_tweets_user');
    111    
    112     if ($user != '') :
    113         $tweet_user = $user;
    114     elseif ($defaultUser != '') :
    115         $tweet_user = $defaultUser;
    116     else :
    117         $tweet_user = 'martythornley';
    118     endif;
    119 
    120     $tweet_number = $number;
    121    
    122     if ($element == '') :
    123         $tweet_element = 'twitterList-' . $tweet_user . '-' . $GLOBALS['twitter_list_count'];
    124     else :
    125         $tweet_element = 'twitterList-' . $tweet_user . '-' . $element;
    126     endif;
    127    
    128     echo '<ul id="' . $tweet_element. '" class="twitterList ' . $user . '"></ul>';
    129    
    130     $wp_twitter_info['tweet_user'] = $tweet_user;
    131     $wp_twitter_info['tweet_number'] = $tweet_number;
    132     $wp_twitter_info['tweet_element'] = $tweet_element;
    133    
    134     $old = $GLOBALS['twitter_info_store'];
    135    
    136     if ( $old == '' ) :
    137         $new = serialize($wp_twitter_info);
    138     else :
    139         $new = $old . ',' . serialize($wp_twitter_info);
    140     endif;
    141    
    142     $GLOBALS['twitter_info_store'] = $new; 
    143 
    144     $GLOBALS['twitter_list_count'] ++;
    145    
    146     $output_string=ob_get_contents();;
    147     ob_end_clean();
    148 
    149     echo $output_string;
    150    
    151 };
    152 
    153 function add_twitter_js_to_footer () {
    154 
    155     $wp_twitter_infos = $GLOBALS['twitter_info_store'];
    156    
    157     $wp_twitter_infos = explode( ',' ,  $wp_twitter_infos );
    158    
    159     foreach ($wp_twitter_infos as $wp_twitter_info) {
    160         $instances[] = unserialize ($wp_twitter_info);
     89*********************************************************************/
     90
     91    add_action ( 'wp_head',         'empty_twitter_info' );
     92    add_action ( 'admin_head',      'empty_twitter_info' );
     93    add_action ( 'admin_menu' ,     'wp_list_tweets_admin_menu');
     94   
     95    add_action ( 'wp_footer' ,      'add_twitter_js_to_footer' );
     96    add_action ( 'admin_footer' ,   'add_twitter_js_to_footer' );
     97    add_filter ( 'wp_head',         'default_wp_list_tweets_style' );
     98    add_filter ( 'admin_head',      'default_wp_list_tweets_style' );
     99
     100    add_shortcode('wp_list_tweets', 'wp_list_tweets_shortcode');
     101       
     102    /*
     103     * Empty the constant on each page load to store this page's info
     104     */     
     105    function empty_twitter_info() {
     106        $GLOBALS['twitter_info_store'] = '';
     107        $GLOBALS['twitter_list_count'] = 1;
     108    };
     109   
     110    /*
     111     * Function that creates ul holder for tweets
     112     */
     113    function wp_list_tweets($user='', $number='3', $element = '') {
     114        ob_start();
     115   
     116        // get needed global info
     117        global $post;
     118       
     119        $defaultUser = get_option('wp_list_tweets_user');
     120       
     121        if ($user != '') :
     122            $tweet_user = $user;
     123        elseif ($defaultUser != '') :
     124            $tweet_user = $defaultUser;
     125        else :
     126            $tweet_user = 'martythornley';
     127        endif;
     128   
     129        $tweet_number = $number;
     130       
     131        if ($element == '') :
     132            $tweet_element = 'twitterList-' . $tweet_user . '-' . $GLOBALS['twitter_list_count'];
     133        else :
     134            $tweet_element = 'twitterList-' . $tweet_user . '-' . $element;
     135        endif;
     136       
     137        echo '<ul id="' . $tweet_element. '" class="twitterList ' . $user . '"></ul>';
     138       
     139        $wp_twitter_info['tweet_user'] = $tweet_user;
     140        $wp_twitter_info['tweet_number'] = $tweet_number;
     141        $wp_twitter_info['tweet_element'] = $tweet_element;
     142       
     143        $old = $GLOBALS['twitter_info_store'];
     144       
     145        if ( $old == '' ) :
     146            $new = serialize($wp_twitter_info);
     147        else :
     148            $new = $old . ',' . serialize($wp_twitter_info);
     149        endif;
     150       
     151        $GLOBALS['twitter_info_store'] = $new; 
     152   
     153        $GLOBALS['twitter_list_count'] ++;
     154       
     155        $output_string=ob_get_contents();;
     156        ob_end_clean();
     157   
     158        echo $output_string;
     159       
     160    };
     161   
     162    /*
     163     * Adds Javascript to footer on front end
     164     */     
     165    function add_twitter_js_to_footer () {
     166   
     167        $wp_twitter_infos = $GLOBALS['twitter_info_store'];
     168       
     169        $wp_twitter_infos = explode( ',' ,  $wp_twitter_infos );
     170       
     171        foreach ($wp_twitter_infos as $wp_twitter_info) {
     172            $instances[] = unserialize ($wp_twitter_info);
     173        }
     174       
     175        foreach ( $instances as $instance ) {
     176       
     177            $tweet_user = $instance['tweet_user'];
     178            $tweet_number  = $instance['tweet_number'];
     179            $tweet_element  = $instance['tweet_element'];
     180   
     181            if ($instance['tweet_user'] !='') {
     182            ?>
     183       
     184                <script type="text/javascript">
     185                /* <![CDATA[ */
     186               
     187                    function twitterCallback<?php echo $tweet_user; ?>(C) {
     188                        var A=[];
     189                        for(var D=0;D<C.length;D++) {
     190                        var E=C[D].user.screen_name;
     191                        var B=C[D].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,
     192               
     193                        function(F) { return'<a href="'+F+'">'+F+"</a>"}).replace(/\B@([_a-z0-9]+)/ig, function(F) { return F.charAt(0)+'<a href="http://www.twitter.com/'+F.substring(1)+'">'+F.substring(1)+"</a>" } );
     194               
     195                        A.push("<li><span>"+B+'</span> <a style="font-size:85%" href="http://twitter.com/'+E+"/statuses/"+C[D].id+'">'+relative_time(C[D].created_at)+"</a></li>")}document.getElementById("<?php echo $tweet_element; ?>").innerHTML=A.join("")}
     196                                               
     197                        function relative_time(C) {
     198                        var B=C.split(" ");C=B[1]+" "+B[2]+", "+B[5]+" "+B[3];
     199                        var A=Date.parse(C);
     200                        var D=(arguments.length>1)?arguments[1]:new Date();
     201                        var E=parseInt((D.getTime()-A)/1000);
     202                        E=E+(D.getTimezoneOffset()*60);
     203               
     204                        if (E<60) { return"less than a minute ago" }
     205               
     206                        else {
     207                            if (E<120) { return"about a minute ago" }
     208               
     209                        else {
     210                            if(E<(60*60)) { return(parseInt(E/60)).toString()+" minutes ago" }
     211                 
     212                        else {
     213                            if(E<(120*60)) {return"about an hour ago" }
     214               
     215                        else {
     216                            if (E<(24*60*60)){return"about "+(parseInt(E/3600)).toString()+" hours ago" }
     217                   
     218                        else {
     219                            if (E<(48*60*60)){return"1 day ago"}else{return(parseInt(E/86400)).toString()+" days ago"}}}}}
     220                        }
     221                    };
     222                   
     223                    function setCookie(name, value, expires) {
     224                        document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
     225                    }
     226                     
     227                    function getCookie(name) {
     228                        if (document.cookie.length > 0) {
     229                          var start = document.cookie.indexOf(name + "=");
     230                          if (start != -1) {
     231                            start = start + name.length + 1;
     232                            var end = document.cookie.indexOf(";", start);
     233                            if (end == -1) {
     234                              end = document.cookie.length;
     235                            }
     236                            return unescape(document.cookie.substring(start, end));
     237                          }
     238                        }
     239                        return "";
     240                    }
     241                     
     242                    function twitterCachedCallback<?php echo $tweet_user; ?>(c) {
     243                       
     244                        var cachedContent = getCookie('twitter_content_<?php echo $tweet_user; ?>_<?php echo $tweet_number; ?>');
     245                        // set content immediately if cached
     246                        if (cachedContent) {
     247                            document.getElementById("<?php echo $tweet_element; ?>").innerHTML = cachedContent;
     248                        } else {
     249                            twitterCallback<?php echo $tweet_user; ?>(c);
     250                            var content = document.getElementById("<?php echo $tweet_element; ?>").innerHTML;
     251                            // expire cookie after 30 minutes
     252                            var exp = new Date();
     253                            exp.setTime(exp.getTime() + (1000 * 60 * 30));
     254                            setCookie('twitter_content_<?php echo $tweet_user; ?>_<?php echo $tweet_number; ?>', content, exp);
     255                        }
     256                    }
     257                               
     258                /* ]]> */
     259                </script>
     260   
     261            <?php
     262           
     263                $js = '<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/';
     264                $js .= $tweet_user;
     265                $js .= '.json?callback=twitterCachedCallback' . $tweet_user .'&amp;count=' . $tweet_number . '"></script>';
     266           
     267                echo $js;
     268            }
     269        }
     270    };
     271   
     272   
     273   
     274    /*
     275     * Adds Shortcode
     276     */
     277    function wp_list_tweets_shortcode($atts) {
     278           
     279        extract(shortcode_atts(array(
     280            'user' => get_option('wp_list_tweets_user'),
     281            'num' => '1',
     282            'element' => $GLOBALS['twitter_list_count'],
     283        ), $atts));
     284       
     285        ob_start();
     286   
     287        wp_list_tweets($user, $num, $element);
     288        $output_string=ob_get_contents();;
     289        ob_end_clean();
     290   
     291        return $output_string;
     292    };
     293   
     294    /*
     295     * Adds default styling
     296     */
     297    function default_wp_list_tweets_style (){
     298        echo '
     299    <style type="text/css">
     300        ul.twitterList {
     301            background:#E5E5E5 none repeat scroll 0 0;
     302            border:1px solid #C5C5C5;
     303            float:left;
     304            height:auto;
     305            margin:10px 20px 10px 0;
     306            padding:0;
     307            overflow:hidden;
     308            width:300px;
     309            text-align:left;
     310            width:200px;
     311        }
     312   
     313        ul.twitterList li {
     314            padding: 6px 10px;
     315            text-indent:0;
     316            margin:0;
     317        }   
     318    </style>';
     319    };
     320
     321    /*
     322     * Adds menu item and creates settings page
     323     */
     324    function wp_list_tweets_admin_menu() {
     325        add_options_page('WP List Tweets', 'WP List Tweets', 'edit_themes', __FILE__, 'wp_list_tweets_admin');
    161326    }
    162    
    163     foreach ( $instances as $instance ) {
    164         $tweet_user = $instance['tweet_user'];
    165         $tweet_number  = $instance['tweet_number'];
    166         $tweet_element  = $instance['tweet_element'];
    167 
    168     if ($instance['tweet_user'] !='') {
    169    
    170    
     327
     328    /*
     329     * Adds admin page
     330     */
     331    function wp_list_tweets_admin() {
     332
     333        if ( strtoupper( $_POST['Action'] ) == 'UPDATE' ) {
     334            if ( !wp_verify_nonce( $_POST[ 'wp_list_tweets_admin' ], plugin_basename(__FILE__) ) ) {
     335                echo '<div class="updated"><p>There was a problem. Please try again.</p></div>';
     336            } else {
     337                $tweet_user = esc_html( $_POST['tweet_user'] );
     338                update_option('wp_list_tweets_user', $tweet_user );
     339            };     
     340        }
     341       
     342        $tweet_user = get_option('wp_list_tweets_user');
    171343    ?>
    172344   
    173         <script type="text/javascript">
    174         /* <![CDATA[ */
    175         function twitterCallback<?php echo $tweet_user; ?>(C) {
    176 
    177         var A=[];
    178         for(var D=0;D<C.length;D++) {
    179         var E=C[D].user.screen_name;
    180         var B=C[D].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,
    181 
    182         function(F) { return'<a href="'+F+'">'+F+"</a>"}).replace(/\B@([_a-z0-9]+)/ig, function(F) { return F.charAt(0)+'<a href="http://www.twitter.com/'+F.substring(1)+'">'+F.substring(1)+"</a>" } );
    183 
    184         A.push("<li><span>"+B+'</span> <a style="font-size:85%" href="http://twitter.com/'+E+"/statuses/"+C[D].id+'">'+relative_time(C[D].created_at)+"</a></li>")}document.getElementById("<?php echo $tweet_element; ?>").innerHTML=A.join("")}
    185 
    186         function relative_time(C) {
    187         var B=C.split(" ");C=B[1]+" "+B[2]+", "+B[5]+" "+B[3];
    188         var A=Date.parse(C);
    189         var D=(arguments.length>1)?arguments[1]:new Date();
    190         var E=parseInt((D.getTime()-A)/1000);
    191         E=E+(D.getTimezoneOffset()*60);
    192 
    193         if (E<60) { return"less than a minute ago" }
    194 
    195         else {
    196             if (E<120) { return"about a minute ago" }
    197 
    198         else {
    199             if(E<(60*60)) { return(parseInt(E/60)).toString()+" minutes ago" }
    200  
    201         else {
    202             if(E<(120*60)) {return"about an hour ago" }
    203 
    204         else {
    205             if (E<(24*60*60)){return"about "+(parseInt(E/3600)).toString()+" hours ago" }
    206    
    207         else {
    208             if (E<(48*60*60)){return"1 day ago"}else{return(parseInt(E/86400)).toString()+" days ago"}}}}}}};
    209         /* ]]> */
    210         </script>
    211 
     345    <h2>WP List Tweets</h2>
     346   
     347    <form method="post" action="">
     348    <?php wp_nonce_field( plugin_basename( __FILE__ ), 'wp_list_tweets_admin', false, true ); ?>
     349   
     350    <table class="form-table">
     351      <tr valign="top">
     352        <th scope="row"><label for="Username">Default Username:</label></th>
     353        <td><input type="text" name="tweet_user" id="tweet_user" value="<?php echo $tweet_user; ?>"/></td>
     354      </tr>
     355    </table>
     356   
     357    <p align="center"><input type="submit" name="Action" value="Update" class="button-primary"/></p>
     358   
     359    </form>
     360   
     361    <h2>Current Default Tweets Display</h2>
     362    <p>This is what you would see using wp_list_tweets(); or shortcode [wp_list_tweets] without any variables.</p>
     363   
     364    <?php wp_list_tweets(); ?>
     365   
    212366    <?php
    213        
    214         echo '<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/';
    215         echo $tweet_user;
    216         echo '.json?callback=twitterCallback' . $tweet_user .'&amp;count=' . $tweet_number . '"></script>';
    217     }
    218     }
    219 };
    220 
    221 add_action ('wp_footer' , 'add_twitter_js_to_footer' );
    222 add_action ('admin_footer' , 'add_twitter_js_to_footer' );
    223 
    224 
    225 // SHORTCODE *************************************************
    226 
    227 function wp_list_tweets_shortcode($atts) {
    228        
    229     extract(shortcode_atts(array(
    230         'user' => 'martythornley',
    231         'num' => '1',
    232         'element' => $GLOBALS['twitter_list_count'],
    233     ), $atts));
    234    
    235     ob_start();
    236 
    237     wp_list_tweets($user, $num, $element);
    238     $output_string=ob_get_contents();;
    239     ob_end_clean();
    240 
    241     return $output_string;
    242 };
    243 
    244 add_shortcode('wp_list_tweets', 'wp_list_tweets_shortcode');
    245 
    246 // Default style *********************************************
    247 
    248 function default_wp_list_tweets_style (){
    249     echo '
    250 <style type="text/css">
    251 ul.twitterList {
    252     background:#E5E5E5 none repeat scroll 0 0;
    253     border:1px solid #C5C5C5;
    254     float:left;
    255     height:auto;
    256     margin:10px 20px 10px 0;
    257     padding:0;
    258     overflow:hidden;
    259     width:300px;
    260     text-align:left;
    261     width:200px;
    262 }
    263 
    264 ul.twitterList li {
    265     padding: 6px 10px;
    266     text-indent:0;
    267     margin:0;
    268 }   
    269 </style>';
    270 
    271 };
    272 
    273 add_filter ('wp_head', 'default_wp_list_tweets_style');
    274 add_filter ('admin_head', 'default_wp_list_tweets_style');
    275 
    276 // ADMIN MENU ****************************************************
    277 
    278 add_action('admin_menu' , 'wp_list_tweets_admin_menu');
    279 
    280 function wp_list_tweets_admin_menu() {
    281     add_options_page('WP List Tweets', 'WP List Tweets', 'edit_themes', __FILE__, 'wp_list_tweets_admin');
    282 }
    283 
    284 function wp_list_tweets_admin() {
    285     if (strtoupper($_POST['Action']) == 'UPDATE') {
    286     if ( !wp_verify_nonce( $_POST[ 'wp_list_tweets_admin' ], plugin_basename(__FILE__) ) ) : ?>
    287         <div class="updated">
    288             <p>There was a problem. Please try again.</p>
    289         </div>
    290     <?php else :
    291    
    292         $tweet_user = $_POST['tweet_user'];
    293         update_option('wp_list_tweets_user', $tweet_user );
    294     endif;     
    295367    }
    296368?>
    297 <h2>WP List Tweets</h2>
    298 
    299 <form method="post" action="">
    300 <?php wp_nonce_field( plugin_basename( __FILE__ ), 'wp_list_tweets_admin', false, true ); ?>
    301 
    302 <table class="form-table">
    303   <tr valign="top">
    304     <th scope="row"><label for="Username">Default Username:</label></th>
    305     <td><input type="text" name="tweet_user" id="tweet_user" value="<?php echo $tweet_user; ?>"/></td>
    306   </tr>
    307 </table>
    308 
    309 <p align="center"><input type="submit" name="Action" value="Update" class="button-primary"/></p>
    310 
    311 </form>
    312 
    313 <h2>Current Default Tweets Display</h2>
    314 <p>This is what you would see using wp_list_tweets(); or shortcode [wp_list_tweets] without any variables.</p>
    315 
    316 <?php wp_list_tweets(); ?>
    317 
    318 <?php
    319        
    320 }
    321 
    322 ?>
Note: See TracChangeset for help on using the changeset viewer.