|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// Exit if accessed directly |
|
4
|
|
|
if ( !defined( 'ABSPATH' ) ) { |
|
5
|
|
|
exit; |
|
6
|
|
|
} |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Twitter Class |
|
10
|
|
|
* |
|
11
|
|
|
* Handles all twitter functions |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
if( !class_exists( 'PPP_Twitter_User' ) ) { |
|
15
|
|
|
|
|
16
|
|
|
class PPP_Twitter_User { |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( $_user_id = 0 ) { |
|
19
|
|
|
ppp_maybe_start_session(); |
|
20
|
|
|
$this->user_id = $_user_id; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
if ( ! empty( $this->user_id ) ) { |
|
23
|
|
|
$this->verify_credentials(); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Include Twitter Class |
|
29
|
|
|
* |
|
30
|
|
|
* Handles to load twitter class |
|
31
|
|
|
*/ |
|
32
|
|
|
public function load() { |
|
33
|
|
|
if( !class_exists( 'TwitterOAuth' ) ) { |
|
34
|
|
|
require_once ( PPP_PATH . '/includes/libs/twitter/twitteroauth.php' ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
ppp_set_social_tokens(); |
|
38
|
|
|
|
|
39
|
|
|
if ( ! defined( 'PPP_TW_CONSUMER_KEY' ) || ! defined( 'PPP_TW_CONSUMER_SECRET' ) ) { |
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->twitter = new TwitterOAuth( PPP_TW_CONSUMER_KEY, PPP_TW_CONSUMER_SECRET ); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function revoke_access() { |
|
49
|
|
|
delete_user_meta( $this->user_id, '_ppp_twitter_data' ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initializes Twitter API |
|
54
|
|
|
* |
|
55
|
|
|
*/ |
|
56
|
|
|
public function init() { |
|
57
|
|
|
|
|
58
|
|
|
//when user is going to logged in in twitter and verified successfully session will create |
|
59
|
|
|
if ( isset( $_REQUEST['oauth_verifier'] ) ) { |
|
60
|
|
|
|
|
61
|
|
|
//load twitter class |
|
62
|
|
|
$twitter = $this->load(); |
|
|
|
|
|
|
63
|
|
|
$this->twitter = new TwitterOAuth( PPP_TW_CONSUMER_KEY, PPP_TW_CONSUMER_SECRET, $_SESSION['ppp_user_twt_oauth_token'], $_SESSION['ppp_user_twt_oauth_token_secret'] ); |
|
64
|
|
|
|
|
65
|
|
|
// Request access tokens from twitter |
|
66
|
|
|
$ppp_tw_access_token = $this->twitter->getAccessToken( $_REQUEST['oauth_verifier'] ); |
|
67
|
|
|
|
|
68
|
|
|
//session for verifier |
|
69
|
|
|
$verifier['oauth_verifier'] = $_REQUEST['oauth_verifier']; |
|
|
|
|
|
|
70
|
|
|
$_SESSION[ 'ppp_twt_user_cache' ] = $verifier; |
|
71
|
|
|
|
|
72
|
|
|
//getting user data from twitter |
|
73
|
|
|
$response = $this->twitter->get( 'account/verify_credentials' ); |
|
74
|
|
|
|
|
75
|
|
|
//if user data get successfully |
|
76
|
|
|
if ( $response->id_str ) { |
|
77
|
|
|
$data['user'] = $response; |
|
|
|
|
|
|
78
|
|
|
$data['user']->accessToken = $ppp_tw_access_token; |
|
79
|
|
|
|
|
80
|
|
|
update_user_meta( $this->user_id, '_ppp_twitter_data', $data ); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function verify_credentials() { |
|
86
|
|
|
$this->load(); |
|
87
|
|
|
|
|
88
|
|
|
$user_settings = get_user_meta( $this->user_id, '_ppp_twitter_data', true ); |
|
89
|
|
|
if ( ! empty( $user_settings ) ) { |
|
90
|
|
|
|
|
91
|
|
|
$this->twitter = new TwitterOAuth( |
|
92
|
|
|
PPP_TW_CONSUMER_KEY, |
|
93
|
|
|
PPP_TW_CONSUMER_SECRET, |
|
94
|
|
|
$user_settings['user']->accessToken['oauth_token'], |
|
95
|
|
|
$user_settings['user']->accessToken['oauth_token_secret'] |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
$response = $this->twitter->get('account/verify_credentials'); |
|
99
|
|
|
if ( is_object( $response ) && property_exists( $response, 'errors' ) && count( $response->errors ) > 0 ) { |
|
100
|
|
|
foreach ( $response->errors as $error ) { |
|
101
|
|
|
if ( $error->code == 89 ) { // Expired or revoked tokens |
|
102
|
|
|
|
|
103
|
|
|
$this->revoke_access(); |
|
104
|
|
|
|
|
105
|
|
|
return array( 'error' => __( 'Post Promoter Pro has been removed from your Twitter account. Please reauthorize to continue promoting your content.', 'ppp-txt' ) ); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get auth url for twitter |
|
116
|
|
|
* |
|
117
|
|
|
*/ |
|
118
|
|
|
public function get_auth_url ( $return_url = '' ) { |
|
119
|
|
|
|
|
120
|
|
|
if ( empty( $return_url ) ) { |
|
121
|
|
|
$return_url = admin_url( 'admin.php?page=ppp-social-settings' ); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
//load twitter class |
|
125
|
|
|
$twitter = $this->load(); |
|
|
|
|
|
|
126
|
|
|
$request_token = $this->twitter->getRequestToken( 'oob' ); |
|
127
|
|
|
|
|
128
|
|
|
// If last connection failed don't display authorization link. |
|
129
|
|
|
switch( $this->twitter->http_code ) { |
|
130
|
|
|
case 200: |
|
131
|
|
|
$_SESSION['ppp_user_twt_oauth_token'] = $request_token['oauth_token']; |
|
132
|
|
|
$_SESSION['ppp_user_twt_oauth_token_secret'] = $request_token['oauth_token_secret']; |
|
133
|
|
|
|
|
134
|
|
|
$token = $request_token['oauth_token']; |
|
135
|
|
|
$url = $this->twitter->getAuthorizeURL( $token, NULL ); |
|
136
|
|
|
break; |
|
137
|
|
|
default: |
|
138
|
|
|
$url = ''; |
|
139
|
|
|
break; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $url; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function send_tweet( $message = '', $media = null ) { |
|
146
|
|
|
if ( empty( $message ) ) { |
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$verify = $this->verify_credentials(); |
|
151
|
|
|
if ( $verify === true ) { |
|
152
|
|
|
$args = array(); |
|
153
|
|
|
if ( ! empty( $media ) ) { |
|
154
|
|
|
$endpoint = 'statuses/update_with_media'; |
|
155
|
|
|
$args['media[]'] = wp_remote_retrieve_body( wp_remote_get( $media ) ); |
|
156
|
|
|
} else { |
|
157
|
|
|
$endpoint = 'statuses/update'; |
|
158
|
|
|
} |
|
159
|
|
|
$args['status'] = $message; |
|
160
|
|
|
|
|
161
|
|
|
return $this->twitter->post( $endpoint, $args, true ); |
|
162
|
|
|
} else { |
|
163
|
|
|
return false; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function retweet( $tweet_id ) { |
|
168
|
|
|
if ( empty( $tweet_id ) ) { |
|
169
|
|
|
return false; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$verify = $this->verify_credentials(); |
|
173
|
|
|
if ( $verify === true ) { |
|
174
|
|
|
$endpoint = 'statuses/retweet/' . $tweet_id; |
|
175
|
|
|
|
|
176
|
|
|
return $this->twitter->post( $endpoint, array(), true ); |
|
177
|
|
|
} else { |
|
178
|
|
|
return false; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: