SHOW:
|
|
- or go back to the newest paste.
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // multi instance twitter widget | |
3 | /////////////////////////////////////////////////////////////////////////////// | |
4 | function theme_twitter_js_handler($unique_id,$twitter_username,$twitter_count) { | |
5 | ?> | |
6 | <script type="text/javascript"> | |
7 | <!--//--><![CDATA[//><!-- | |
8 | ||
9 | function twitterCallback2(twitters) { | |
10 | var statusHTML = []; | |
11 | for (var i=0; i<twitters.length; i++){ | |
12 | var username = twitters[i].user.screen_name; | |
13 | var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) { | |
14 | return '<a href="'+url+'">'+url+'</a>'; | |
15 | }).replace(/\B@([_a-z0-9]+)/ig, function(reply) { | |
16 | return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>'; | |
17 | }); | |
18 | statusHTML.push('<li><span class="">'+status+'</span> <a style="font-size:85%" class="time" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>'); | |
19 | } | |
20 | document.getElementById('twitter_update_list_<?php echo $unique_id; ?>').innerHTML = statusHTML.join(''); | |
21 | } | |
22 | ||
23 | function relative_time(time_value) { | |
24 | var values = time_value.split(" "); | |
25 | time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3]; | |
26 | var parsed_date = Date.parse(time_value); | |
27 | var relative_to = (arguments.length > 1) ? arguments[1] : new Date(); | |
28 | var delta = parseInt((relative_to.getTime() - parsed_date) / 1000); | |
29 | delta = delta + (relative_to.getTimezoneOffset() * 60); | |
30 | ||
31 | if (delta < 60) { | |
32 | return 'less than a minute ago'; | |
33 | } else if(delta < 120) { | |
34 | return 'about a minute ago'; | |
35 | } else if(delta < (60*60)) { | |
36 | return (parseInt(delta / 60)).toString() + ' minutes ago'; | |
37 | } else if(delta < (120*60)) { | |
38 | return 'about an hour ago'; | |
39 | } else if(delta < (24*60*60)) { | |
40 | return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago'; | |
41 | } else if(delta < (48*60*60)) { | |
42 | return '1 day ago'; | |
43 | } else { | |
44 | return (parseInt(delta / 86400)).toString() + ' days ago'; | |
45 | } | |
46 | } | |
47 | //-->!]]> | |
48 | </script> | |
49 | <script type="text/javascript" src="http://api.twitter.com/1/statuses/user_timeline/<?php echo $twitter_username; ?>.json?callback=twitterCallback2&count=<?php echo $twitter_count; ?>&include_rts=t"></script> | |
50 | <?php | |
51 | } | |
52 | ||
53 | ||
54 | class My_THEME_Twitter_Widget extends WP_Widget { | |
55 | function My_THEME_Twitter_Widget() { | |
56 | //Constructor | |
57 | parent::WP_Widget(false, $name = TEMPLATE_DOMAIN . ' | Twitter', array( | |
58 | 'description' => __('Display your latest twitter.', TEMPLATE_DOMAIN) | |
59 | )); | |
60 | } | |
61 | ||
62 | function widget($args, $instance) { | |
63 | // outputs the content of the widget | |
64 | extract($args); // Make before_widget, etc available. | |
65 | ||
66 | $twitter_username = $instance['twitter_username']; | |
67 | $twitter_count = $instance['twitter_count']; | |
68 | ||
69 | $twitter_title = empty($instance['title']) ? __('Twitter', TEMPLATE_DOMAIN) : apply_filters('widget_title', $instance['title']); | |
70 | $unique_id = $args['widget_id']; | |
71 | ||
72 | echo $before_widget; | |
73 | echo $before_title . $twitter_title . $after_title; ?> | |
74 | <ul class="twitterbox" id="twitter_update_list_<?php echo $unique_id; ?>"> | |
75 | <?php echo theme_twitter_js_handler($unique_id,$twitter_username,$twitter_count); //Javascript output function ?> | |
76 | <li class="followme"><?php echo '<a style="font-weight: normal; letter-spacing: normal; font-size: 11px;" href="http://twitter.com/' . $twitter_username . '">'; ?><?php printf( __( 'Follow %1$s in Twitter', TEMPLATE_DOMAIN ), ucfirst($twitter_username) ); ?><?php echo '</a>'; ?> | |
77 | </li> | |
78 | </ul> | |
79 | ||
80 | ||
81 | ||
82 | <?php echo $after_widget; | |
83 | } | |
84 | ||
85 | function update($new_instance, $old_instance) { | |
86 | //update and save the widget | |
87 | return $new_instance; | |
88 | } | |
89 | ||
90 | function form($instance) { | |
91 | // Get the options into variables, escaping html characters on the way | |
92 | $twitter_username = $instance['twitter_username']; | |
93 | $twitter_title = $instance['title']; | |
94 | $twitter_count = $instance['twitter_count']; | |
95 | ?> | |
96 | ||
97 | <p> | |
98 | <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e("Twitter Title:",TEMPLATE_DOMAIN); ?></label> | |
99 | <input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $twitter_title; ?>" /> | |
100 | </p> | |
101 | ||
102 | <p><label for="<?php echo $this->get_field_id('twitter_username'); ?>"> | |
103 | <?php echo __('Twitter ID:', TEMPLATE_DOMAIN)?></label> | |
104 | <input class="widefat" type="text" id="<?php echo $this->get_field_id('twitter_username'); ?>" name="<?php echo $this->get_field_name('twitter_username'); ?>" value="<?php echo $twitter_username; ?>" /> | |
105 | </p> | |
106 | <p> | |
107 | <label for="<?php echo $this->get_field_id('twitter_count'); ?>"><?php echo __('Number Of Tweets:', TEMPLATE_DOMAIN)?></label> | |
108 | <input class="widefat" type="text" id="<?php echo $this->get_field_id('twitter_count'); ?>" name="<?php echo $this->get_field_name('twitter_count'); ?>" value="<?php echo $twitter_count; ?>" /> | |
109 | </p> | |
110 | ||
111 | <?php | |
112 | } | |
113 | } | |
114 | register_widget('My_THEME_Twitter_Widget'); |