Repository navigation
Expand file tree
/
Copy pathtwitter-api.php
More file actions
204 lines (153 loc) · 5.15 KB
/
Copy pathtwitter-api.php
File metadata and controls
204 lines (153 loc) · 5.15 KB
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
<?php
/**
* Twitter API Wordpress library.
* @author Tim Whitlock <@timwhitlock>
*/
/**
* Call a Twitter API GET method.
*
* @param string endpoint/method, e.g. "users/show"
* @param array Request arguments, e.g. array( 'screen_name' => 'timwhitlock' )
* @return array raw, deserialised data from Twitter
* @throws TwitterApiException
*/
function twitter_api_get( $path, array $args = array() ){
$Client = twitter_api_client();
return $Client->call( $path, $args, 'GET' );
}
/**
* Call a Twitter API POST method.
*
* @param string endpoint/method, e.g. "users/show"
* @param array Request arguments, e.g. array( 'screen_name' => 'timwhitlock' )
* @return array raw, deserialised data from Twitter
* @throws TwitterApiException
*/
function twitter_api_post( $path, array $args = array() ){
$Client = twitter_api_client();
return $Client->call( $path, $args, 'POST' );
}
/**
* Enable caching of Twitter API responses using APC
* @param int Cache lifetime in seconds
* @return TwitterApiClient
*/
function twitter_api_enable_cache( $ttl ){
$Client = twitter_api_client();
return $Client->enable_cache( $ttl );
}
/**
* Disable caching of Twitter API responses
* @return TwitterApiClient
*/
function twitter_api_disable_cache(){
$Client = twitter_api_client();
return $Client->disable_cache();
}
/**
* Include a component from the lib directory.
* @param string $component e.g. "core", or "admin"
* @return void fatal error on failure
*/
function twitter_api_include(){
foreach( func_get_args() as $component ){
require_once twitter_api_basedir().'/lib/twitter-api-'.$component.'.php';
}
}
/**
* Get plugin local base directory in case __DIR__ isn't available (php<5.3)
*/
function twitter_api_basedir(){
static $dir;
isset($dir) or $dir = dirname(__FILE__);
return $dir;
}
/**
* Test if system-configured client is authed and ready to use
*/
function twitter_api_configured(){
function_exists('_twitter_api_config') or twitter_api_include('core');
extract( _twitter_api_config() );
return $consumer_key && $consumer_secret && $access_key && $access_secret;
}
/**
* Get fully configured and authenticated Twitter API client.
* @return TwitterApiClient
*/
function twitter_api_client( $id = null ){
static $clients = array();
if( ! isset($clients[$id]) ){
twitter_api_include('core');
$clients[$id] = TwitterApiClient::create_instance( is_null($id) );
}
return $clients[$id];
}
/**
* Contact Twitter for a request token, which will be exchanged for an access token later.
* @return TwitterOAuthToken Request token
*/
function twitter_api_oauth_request_token( $consumer_key, $consumer_secret, $oauth_callback = 'oob' ){
$Client = twitter_api_client('oauth');
$Client->set_oauth( $consumer_key, $consumer_secret );
$params = $Client->oauth_exchange( TWITTER_OAUTH_REQUEST_TOKEN_URL, compact('oauth_callback') );
return new TwitterOAuthToken( $params['oauth_token'], $params['oauth_token_secret'] );
}
/**
* Exchange request token for an access token after authentication/authorization by user
* @return TwitterOAuthToken Access token
*/
function twitter_api_oauth_access_token( $consumer_key, $consumer_secret, $request_key, $request_secret, $oauth_verifier ){
$Client = twitter_api_client('oauth');
$Client->set_oauth( $consumer_key, $consumer_secret, $request_key, $request_secret );
$params = $Client->oauth_exchange( TWITTER_OAUTH_ACCESS_TOKEN_URL, compact('oauth_verifier') );
return new TwitterOAuthToken( $params['oauth_token'], $params['oauth_token_secret'] );
}
// Include application settings panel if in admin area
if( is_admin() ){
twitter_api_include('core','admin');
}
/**
* Enable localisation
* @internal
*/
function twitter_api_load_textdomain( $locale = null, $domain = 'twitter-api' ){
static $current_locale;
if( is_null($locale) ){
$locale = get_locale();
}
if( ! $locale || 0 === strpos($locale,'en') ){
$current_locale and unload_textdomain( $domain );
$locale = 'en_US';
}
else if( $current_locale !== $locale ){
// purposefully not calling load_plugin_textdomain, due to symlinking
// and not knowing what plugin this could be called from.
$mofile = realpath( twitter_api_basedir().'/lang/'.$domain.'-'.$locale.'.mo' );
if( ! load_textdomain( $domain, $mofile ) ){
$mofile = WP_LANG_DIR . '/plugins/'.$domain.'-'.$locale.'.mo';
load_textdomain( $domain, $mofile );
}
}
// detect changes in plugin locale, binding once only
if( ! isset($current_locale) ){
add_filter( 'plugin_locale', '_twitter_api_filter_plugin_locale', 10 , 2 );
}
$current_locale = $locale;
}
/**
* Support locale switching mid execution
* @internal
*/
function _twitter_api_filter_plugin_locale( $locale, $domain ){
if( $domain === 'twitter-api' ){
twitter_api_load_textdomain( $locale );
}
return $locale;
}
/**
* legacy function call
* @ignore
*/
function _twitter_api_init_l10n( $locale = null ){
return twitter_api_load_textdomain( $locale );
}