-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-wp-twitter-api.php
More file actions
161 lines (124 loc) · 4.32 KB
/
class-wp-twitter-api.php
File metadata and controls
161 lines (124 loc) · 4.32 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
<?php
/**
* Class WordPress Twitter API
*
* @version 1.0.0
*/
class Wp_Twitter_Api {
var $bearer_token,
// Default credentials
$args = array(
'consumer_key' => 'default_consumer_key',
'consumer_secret' => 'default_consumer_secret'
),
// Default type of the resource and cache duration
$query_args = array(
'type' => 'statuses/user_timeline',
'cache' => 1800
),
$has_error = false;
/**
* WordPress Twitter API Constructor
*
* @param array $args
*/
public function __construct( $args = array() ) {
if ( is_array( $args ) && !empty( $args ) )
$this->args = array_merge( $this->args, $args );
if ( ! $this->bearer_token = get_option( 'twitter_bearer_token' ) )
$this->bearer_token = $this->get_bearer_token();
}
/**
* Get the token from oauth Twitter API
*
* @return string Oauth Token
*/
private function get_bearer_token() {
$bearer_token_credentials = $this->args['consumer_key'] . ':' . $this->args['consumer_secret'];
$bearer_token_credentials_64 = base64_encode( $bearer_token_credentials );
$args = array(
'method' => 'POST',
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(
'Authorization' => 'Basic ' . $bearer_token_credentials_64,
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
'Accept-Encoding' => 'gzip'
),
'body' => array( 'grant_type' => 'client_credentials' ),
'cookies' => array()
);
$response = wp_remote_post( 'https://api.twitter.com/oauth2/token', $args );
if ( is_wp_error( $response ) || 200 != $response['response']['code'] )
return $this->bail( __( 'Can\'t get the bearer token, check your credentials', 'wp_twitter_api' ), $response );
$result = json_decode( $response['body'] );
update_option( 'twitter_bearer_token', $result->access_token );
return $result->access_token;
}
/**
* Query twitter's API
*
* @uses $this->get_bearer_token() to retrieve token if not working
*
* @param string $query Insert the query in the format "count=1&include_entities=true&include_rts=true&screen_name=micc1983!
* @param array $query_args Array of arguments: Resource type (string) and cache duration (int)
* @param bool $stop Stop the query to avoid infinite loop
*
* @return bool|object Return an object containing the result
*/
public function query( $query, $query_args = array(), $stop = false ) {
if ( $this->has_error )
return false;
if ( is_array( $query_args ) && !empty( $query_args ) )
$this->query_args = array_merge( $this->query_args, $query_args );
$transient_name = 'wta_' . md5( $query );
if ( false !== ( $data = get_transient( $transient_name ) ) ) {
$data = json_decode( $data );
return $data;
}
$args = array(
'method' => 'GET',
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(
'Authorization' => 'Bearer ' . $this->bearer_token,
'Accept-Encoding' => 'gzip'
),
'body' => null,
'cookies' => array()
);
$response = wp_remote_get( 'https://api.twitter.com/1.1/' . $this->query_args['type'] . '.json?' . $query, $args );
if ( is_wp_error( $response ) || 200 != $response['response']['code'] ){
if ( !$stop ){
$this->bearer_token = $this->get_bearer_token();
return $this->query( $query, $this->query_args, true );
} else {
return $this->bail( __( 'Bearer Token is good, check your query', 'wp_twitter_api' ), $response );
}
}
set_transient( $transient_name, $response['body'], $this->query_args['cache'] );
$data = json_decode( $response['body'] );
return $data;
}
/**
* Let's manage errors
*
* WP_DEBUG has to be set to true to show errors
*
* @param string $error_text Error message
* @param string $error_object Server response or wp_error
*/
private function bail( $error_text, $error_object = '' ) {
$this->has_error = true;
if ( is_wp_error( $error_object ) ){
$error_text .= ' - Wp Error: ' . $error_object->get_error_message();
} elseif ( !empty( $error_object ) && isset( $error_object['response']['message'] ) ) {
$error_text .= ' ( Response: ' . $error_object['response']['message'] . ' )';
}
trigger_error( $error_text , E_USER_NOTICE );
}
}