forked from DefProc/TweetUpdater
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweetupdater.php
399 lines (286 loc) · 10.1 KB
/
tweetupdater.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
/*
Plugin Name: TweetUpdater
Description: Update your Twitter status when you publish or update a post. Based on TwitterUpdater v1.0 by <a href="http://blog.victoriac.net/ja/geek/twitter-updater">Victoria Chan</a>
Version: 3.0
Author: Patrick Fenner (Def-Proc.co.uk)
Author URI: http://www.deferredprocrastination.co.uk/
Plugin URI: http://www.deferredprocrastination.co.uk/projects/tweetupdater
Help & Support: http://github.com/DefProc/TweetUpdater/issues
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
require_once('twitteroauth.php');
require_once('tweetupdater_manager.php');
/*** TweetUpdater Actions ***/
// checks if the post has either a given custom field key/value
// or is part of a selected category
function tweet_updater_is_tweetable($post)
{
// retrieve the options
$options = get_option('tweet_updater_options');
if($options['limit_activate']) {
// limiter is activated, check if the post is part of
// a category which is tweetable
if( $options['limit_to_category'] > 0 ) {
$post_categories = wp_get_post_categories($post->ID);
if( is_array($post_categories) && sizeof($post_categories) > 0 ) {
if( in_array($options['limit_to_category'], $post_categories) ) {
echo "in cat: TRUE";
return true;
}
}
}
// Ok, no category found so continue with checking for the customfields
if(! empty($options['limit_to_customfield_key']) && ! empty($options['limit_to_customfield_val']) ) {
$customfield_val = get_post_meta($post->ID, $options['limit_to_customfield_key'], true);
if( ! empty($customfield_val) && $customfield_val == $options['limit_to_customfield_val'] ) {
echo "fields match: true";
return true;
}
}
// in all other cases return false
return false;
} else {
// limit is not active so everything is tweetable
return true;
}
}
/* Plugin action when status changes to publish */
function tweet_updater_published($post) //$post_ID)
{
//load plugin preferences
$options = get_option('tweet_updater_options');
if ( $options['newpost_update'] == "1" && tweet_updater_is_tweetable($post) )
{
$post_ID = $post->ID;
$title = $post->post_title;
$link = get_permalink($post_ID);
$tweet = '';
// Format the message
$tweet = tweet_updater_format_tweet( $options['newpost_format'], $title, $link, $post_ID, $options['use_curl'], $options['url_method'] );
if($tweet != '')
{
// Send the message
$result = tweet_updater_update_status($tweet);
}
}
return $post;
}
/* Plugin action when published is (re)published (i.e. updated) */
function tweet_updater_edited($post) //$post_ID)
{
//load plugin preferences
$options = get_option('tweet_updater_options');
if ( $options['edited_update'] == "1" && tweet_updater_is_tweetable() )
{
$post_ID = $post->ID;
$title = $post->post_title;
$link = get_permalink($post_ID);
$tweet = '';
// Format the message
$tweet = tweet_updater_format_tweet( $options['edited_format'], $title, $link, $post_ID, $options['use_curl'], $options['url_method'] );
if($tweet != '')
{
// Send the message
$result = tweet_updater_update_status($tweet);
}
}
return $post;
}
/*** Additional Functions ***/
/* Single function to output a formatted tweet */
function tweet_updater_format_tweet( $tweet_format, $title, $link, $post_ID, $use_curl, $url_method )
{
//initialise tweet
$tweet = $tweet_format;
//retieve the short url
$short_url = tu_get_shorturl($use_curl,$url_method,$link,$post_ID);
// Error handling: If plugin is deacitvated, repeat to use default link supplier
if( $short_url['error_code'] == '1' )
{
$short_url = tu_get_shorturl($use_curl,$short_url['url_method'],$link,$post_ID);
}
// Additional error handing is possible: returning $tweet = ''; will cause sending to be aborted.
//do the placeholder string replace
$tweet = str_replace ( '#title#', $title, $tweet);
$tweet = str_replace ( '#url#', $short_url, $tweet);
return $tweet;
}
/* Get the selected short url */
function tu_get_shorturl( $use_curl, $url_method, $link, $post_ID )
{
if ( $url_method == 'zzgd' )
{
$target_url = "http://zz.gd/api-create.php?url=" . $link;
if ( $use_curl == '1' )
{
$short_url = file_get_contents_curl($target_url);
}
else
{
$short_url = file_get_contents($target_url);
}
}
else if ( $url_method == 'tinyurl' )
{
$target_url = "http://tinyurl.com/api-create.php?url=" . $link;
if ( $use_curl == '1' )
{
$short_url = file_get_contents_curl($target_url);
}
else
{
$short_url = file_get_contents($target_url);
}
}
else if ( $url_method == 'bitly' )
{
$options = get_option('tweet_updater_options');
$short_url = tu_make_bitly_url($link,$options['bitly_username'],$options['bitly_appkey'],$use_curl);
}
else if ( $url_method == 'petite' )
{
if(function_exists('get_la_petite_url_permalink'))
{
// return short link from la_petite_url plugin function
$short_url = get_la_petite_url_permalink($post_ID);
}
else
{
// send error message
$short_url = array(
'error_code' => '1',
'error_message' => 'Function deactivated. Repeat with another method.',
'url_method' => 'tinyurl',
);
}
}
else if ( $url_method == 'permalink' )
{
$short_url = $link;
}
return $short_url;
}
/* get a bit.ly url */
function tu_make_bitly_url($link,$login,$appkey,$use_curl)
{
$bitly = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&format=json&history=1&longUrl='.urlencode($link);
//get the url
if ($use_curl == '1')
{
$response = file_get_contents_curl($bitly);
}
else
{
$response = file_get_contents($bitly);
}
$json = @json_decode($response,true);
$short_url = $json['data']['url'];
//error handling hook
$status = array( 'status_code' => $json['status_test'], 'status_txt' => $json['status_txt'] );
return $short_url;
}
/* alternative funtion to file_get_contents(), using cURL */
function file_get_contents_curl($target_url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $target_url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/*** Twitter OAuth Functions ***/
/* Get Request Tokens */
function tweet_updater_register($tokens)
{
// Build TwitterOAuth object with client credentials.
$connection = new TwitterOAuth($tokens['consumer_key'], $tokens['consumer_secret']);
// Get the request tokens
$request = $connection->getRequestToken();
// Retrive tokens from request and store in array
$tokens['request_key'] = $request["oauth_token"];
$tokens['request_secret'] = $request["oauth_token_secret"];
// Generate a request link and output it
$tokens['request_link'] = $connection->getAuthorizeURL($request);
return $tokens;
}
/* Get Access Tokens */
function tweet_updater_authorise($tokens)
{
// Initiate a new TwitterOAuth object. Provide the request token and request token secret
$connection = new TwitterOAuth($tokens['consumer_key'], $tokens['consumer_secret'], $tokens['request_key'], $tokens['request_secret']);
// Get the access tokens
$request = $connection->getAccessToken();
/*
------------------------------------------------------------------------------------------
*** A failed request (e.g. not authorised by user in twitter) is not handled at all ***
*** and outputs an error array from getAccessToken() ***
------------------------------------------------------------------------------------------
*/
// Retrieve access token from request:
$tokens['access_key'] = $request['oauth_token'];
$tokens['access_secret'] = $request['oauth_token_secret'];
return $tokens;
}
/* Validate Access */
function tweet_updater_verify($tokens)
{
// Initiate TwitterOAuth using access tokens
$connection = new TwitterOAuth($tokens['consumer_key'], $tokens['consumer_secret'], $tokens['access_key'], $tokens['access_secret']);
$result = $connection->get('account/verify_credentials');
$verify = array(
'exit_code' => '',
'user_name' => '',
);
if ($result->id)
{
$verify['exit_code'] = "1";
$verify['user_name'] = $result->screen_name;
}
else
{
$verify['exit_code'] = "3";
}
return $verify;
}
/* Send a Tweet */
function tweet_updater_update_status($tweet)
{
$tokens = get_option('tweet_updater_auth');
if( $tokens['auth3_flag'] == '1' )
{
// Initiate a TwitterOAuth with access tokens
$connection = new TwitterOAuth($tokens['consumer_key'], $tokens['consumer_secret'], $tokens['access_key'], $tokens['access_secret']);
// Post an update to Twitter via your application:
$result = $connection->post('statuses/update', array('status' => $tweet));
}
else
{
$result = array(
'plugin_error' => 'auth',
'error_description' => 'TweetUpdater is not linked to a twitter account'
);
}
return $result;
}
/*** WordPress Hooks ***/
/* Action for when a post is published */
add_action( 'draft_to_publish', 'tweet_updater_published', 1, 1 );
add_action( 'new_to_publish', 'tweet_updater_published', 1, 1 );
add_action( 'pending_to_publish', 'tweet_updater_published', 1, 1 );
add_action( 'future_to_publish', 'tweet_updater_published', 1, 1 );
/* Action when post is updated */
add_action( 'publish_to_publish', 'tweet_updater_edited', 1, 1 );
/* Add the admin options page */
add_action( 'admin_menu', 'tweet_updater_admin_add_page' );
/* Intialise on first activation */
register_activation_hook( __FILE__, 'tweet_updater_activate' );
//add hook to include settings link on plugins page
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'tweet_updater_add_settings_link' );
?>