-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom-tweet-button.php
168 lines (147 loc) · 6.51 KB
/
custom-tweet-button.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
Project name: Custom Tweet Button for Wordpress
Project URI: http://nicolasgallagher.com/custom-tweet-button-for-wordpress/
Description: A fully customisable HTML and CSS Tweet Button for Wordpress build using PHP and the bit.ly and Twitter APIs.
Version: 0.2
Author: Nicolas Gallagher
Author URI: http://nicolasgallagher.com
Copyright 2010 Nicolas Gallagher
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function tweet_button($url) {
// Your bit.ly API credentials
$bitly_login = "";
$bitly_key = "";
// Your twitter account names
$twitter_via = "";
// optional: add a related account
// $twitter_related = "";
// Count display
// 0 - never
// 1 - when greater than zero
// 2 - always
$count_display = 2;
global $post;
$cache_interval = 60;
$refresh_interval = 3660;
$retweet_count = null;
$count = 0;
if (get_post_status($post->ID) == 'publish') {
$title = $post->post_title;
if ((function_exists('curl_init') || function_exists('file_get_contents')) && function_exists('json_decode')) {
// shorten url
if (get_post_meta($post->ID, 'bitly_short_url', true) == '') {
$short_url = null;
$short_url = shorten_bitly($url, $bitly_key, $bitly_login);
if ($short_url) {
add_post_meta($post->ID, 'bitly_short_url', $short_url);
}
}
else {
$short_url = get_post_meta($post->ID, 'bitly_short_url', true);
}
// retweet data (twitter API)
$retweet_meta = get_post_meta($post->ID, 'retweet_cache', true);
if ($retweet_meta != '') {
$retweet_pieces = explode(':', $retweet_meta);
$retweet_timestamp = (int)$retweet_pieces[0];
$retweet_count = (int)$retweet_pieces[1];
}
// expire retweet cache
if ($retweet_count === null || time() > $retweet_timestamp + $cache_interval) {
$retweet_response = urlopen('http://urls.api.twitter.com/1/urls/count.json?url=' . urlencode($url));
if ($retweet_response) {
$retweet_data = json_decode($retweet_response, true);
if (isset($retweet_data['count']) && isset($retweet_data['url']) && $retweet_data['url'] === $url) {
if ((int)$retweet_data['count'] >= $retweet_count || time() > $retweet_timestamp + $refresh_interval) {
$retweet_count = $retweet_data['count'];
if ($retweet_meta == '') {
add_post_meta($post->ID, 'retweet_cache', time() . ':' . $retweet_count);
} else {
update_post_meta($post->ID, 'retweet_cache', time() . ':' . $retweet_count);
}
}
}
}
}
// optional:
// manually set the starting number of retweets for a post that existed before the Tweet Button was created
// the number can be roughly calculated by subtracting the twitter API's retweet count
// from the estimated number of retweets according to the topsy, backtype, or tweetmeme services
// this will check for the value of a Wordpress custom field called "retweet_count_start"
$retweet_count_start = get_post_meta($post->ID, 'retweet_count_start', true);
// calculate the total count to display
$count = $retweet_count + (int)$retweet_count_start;
if ($count > 9999) {
$count = $count / 1000;
$count = number_format($count, 1) . 'K';
} else {
$count = number_format($count);
}
}
// construct the tweet button query string
$twitter_params =
'?text=' . urlencode($title) . '+-' .
'&url=' . urlencode($short_url) .
'&counturl=' . urlencode($url) .
'&via=' . $twitter_via .
//'&related=' . $twitter_related .
''
;
if ($count_display == 1 && $count > 0 || $count_display == 2) {
$counter = '<a class="twitter-count" href="http://twitter.com/search?q=' . urlencode($url) . '">' . $count . '</a>';
}
// HTML for the tweet button (add "vcount" to "twitter-share" for vertical count)
$twitter_share = '
<div class="twitter-share">
<a class="twitter-button"
rel="external nofollow"
title="Share this on Twitter"
href="http://twitter.com/share' . $twitter_params . '"
target="_blank">Tweet</a>
' . $counter . '
</div>
';
echo $twitter_share;
}
}
// convert file contents into string
function urlopen($url) {
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
} else {
return file_get_contents($url);
}
}
// bit.ly url shortening
function shorten_bitly($url, $bitly_key, $bitly_login) {
if ($bitly_key && $bitly_login && function_exists('json_decode')) {
$bitly_params = '?login=' . $bitly_login . '&apiKey=' .$bitly_key . '&longUrl=' . urlencode($url);
$bitly_response = urlopen('http://api.j.mp/v3/shorten' . $bitly_params);
if ($bitly_response) {
$bitly_data = json_decode($bitly_response, true);
if (isset($bitly_data['data']['url'])) {
$bitly_url = $bitly_data['data']['url'];
}
}
}
return $bitly_url;
}
?>