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' ) ) { |
15
|
|
|
|
16
|
|
|
class PPP_Twitter { |
17
|
|
|
|
18
|
|
|
var $twitter; |
19
|
|
|
|
20
|
|
|
public function __construct( $_user_id = 0 ) { |
21
|
|
|
ppp_maybe_start_session(); |
22
|
|
|
|
23
|
|
|
$this->user_id = $_user_id; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Include Twitter Class |
28
|
|
|
* |
29
|
|
|
* Handles to load twitter class |
30
|
|
|
*/ |
31
|
|
|
public function ppp_load_twitter() { |
32
|
|
|
if( !class_exists( 'TwitterOAuth' ) ) { |
33
|
|
|
require_once ( PPP_PATH . '/includes/libs/twitter/twitteroauth.php' ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
ppp_set_social_tokens(); |
37
|
|
|
|
38
|
|
|
if ( ! defined( 'PPP_TW_CONSUMER_KEY' ) || ! defined( 'PPP_TW_CONSUMER_SECRET' ) ) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->twitter = new TwitterOAuth( PPP_TW_CONSUMER_KEY, PPP_TW_CONSUMER_SECRET ); |
43
|
|
|
|
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function revoke_access() { |
48
|
|
|
global $ppp_social_settings; |
49
|
|
|
|
50
|
|
|
unset( $ppp_social_settings['twitter'] ); |
51
|
|
|
|
52
|
|
|
update_option( 'ppp_social_settings', $ppp_social_settings ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initializes Twitter API |
57
|
|
|
* |
58
|
|
|
*/ |
59
|
|
|
public function ppp_initialize_twitter() { |
60
|
|
|
|
61
|
|
|
//when user is going to logged in in twitter and verified successfully session will create |
62
|
|
|
if ( ! empty( $_REQUEST['oauth_verifier'] ) ) { |
63
|
|
|
$ppp_social_settings = get_option( 'ppp_social_settings' ); |
64
|
|
|
|
65
|
|
|
//load twitter class |
66
|
|
|
$twitter = $this->ppp_load_twitter(); |
67
|
|
|
|
68
|
|
|
//check twitter class is loaded or not |
69
|
|
|
if( ! $twitter ) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->twitter = new TwitterOAuth( PPP_TW_CONSUMER_KEY, PPP_TW_CONSUMER_SECRET, $_SESSION['ppp_twt_oauth_token'], $_SESSION['ppp_twt_oauth_token_secret'] ); |
74
|
|
|
|
75
|
|
|
// Request access tokens from twitter |
76
|
|
|
$ppp_tw_access_token = $this->twitter->getAccessToken( $_REQUEST['oauth_verifier'] ); |
77
|
|
|
|
78
|
|
|
//session for verifier |
79
|
|
|
$verifier['oauth_verifier'] = $_REQUEST['oauth_verifier']; |
|
|
|
|
80
|
|
|
|
81
|
|
|
$_SESSION[ 'ppp_twt_user_cache' ] = $verifier; |
82
|
|
|
|
83
|
|
|
//getting user data from twitter |
84
|
|
|
$response = $this->twitter->get('account/verify_credentials'); |
85
|
|
|
|
86
|
|
|
//if user data get successfully |
87
|
|
|
if ( $response->id_str ) { |
88
|
|
|
|
89
|
|
|
$data['user'] = $response; |
|
|
|
|
90
|
|
|
$data['user']->accessToken = $ppp_tw_access_token; |
91
|
|
|
|
92
|
|
|
$ppp_social_settings['twitter'] = $data; |
93
|
|
|
update_option( 'ppp_social_settings', $ppp_social_settings ); |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function ppp_verify_twitter_credentials() { |
100
|
|
|
$this->ppp_load_twitter(); |
101
|
|
|
|
102
|
|
|
global $ppp_social_settings; |
103
|
|
|
if ( isset( $ppp_social_settings['twitter'] ) ) { |
104
|
|
|
|
105
|
|
|
$this->twitter = new TwitterOAuth( |
106
|
|
|
PPP_TW_CONSUMER_KEY, |
107
|
|
|
PPP_TW_CONSUMER_SECRET, |
108
|
|
|
$ppp_social_settings['twitter']['user']->accessToken['oauth_token'], |
109
|
|
|
$ppp_social_settings['twitter']['user']->accessToken['oauth_token_secret'] |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$response = $this->twitter->get('account/verify_credentials'); |
113
|
|
|
if ( is_object( $response ) && property_exists( $response, 'errors' ) && count( $response->errors ) > 0 ) { |
114
|
|
|
foreach ( $response->errors as $error ) { |
115
|
|
|
if ( $error->code == 89 ) { // Expired or revoked tokens |
116
|
|
|
unset( $ppp_social_settings['twitter'] ); |
117
|
|
|
update_option( 'ppp_social_settings', $ppp_social_settings ); |
118
|
|
|
|
119
|
|
|
return array( 'error' => __( 'Post Promoter Pro has been removed from your Twitter account. Please reauthorize to continue promoting your content.', 'ppp-txt' ) ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get auth url for twitter |
130
|
|
|
* |
131
|
|
|
*/ |
132
|
|
|
public function ppp_get_twitter_auth_url ( $return_url = '' ) { |
133
|
|
|
|
134
|
|
|
if ( empty( $return_url ) ) { |
135
|
|
|
$return_url = admin_url( 'admin.php?page=ppp-social-settings' ); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
//load twitter class |
138
|
|
|
$twitter = $this->ppp_load_twitter(); |
139
|
|
|
|
140
|
|
|
//check twitter class is loaded or not |
141
|
|
|
if( !$twitter ) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$request_token = $this->twitter->getRequestToken( 'oob' ); |
146
|
|
|
|
147
|
|
|
// If last connection failed don't display authorization link. |
148
|
|
|
switch( $this->twitter->http_code ) { |
149
|
|
|
|
150
|
|
|
case 200: |
151
|
|
|
$_SESSION['ppp_twt_oauth_token'] = $request_token['oauth_token']; |
152
|
|
|
$_SESSION['ppp_twt_oauth_token_secret'] = $request_token['oauth_token_secret']; |
153
|
|
|
|
154
|
|
|
$token = $request_token['oauth_token']; |
155
|
|
|
$url = $this->twitter->getAuthorizeURL( $token, NULL ); |
156
|
|
|
break; |
157
|
|
|
default: |
158
|
|
|
$url = ''; |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
return $url; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function ppp_tweet( $message = '', $media = null ) { |
165
|
|
|
if ( empty( $message ) ) { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$verify = $this->ppp_verify_twitter_credentials(); |
170
|
|
|
if ( $verify === true ) { |
171
|
|
|
$args = array(); |
172
|
|
|
if ( ! empty( $media ) ) { |
173
|
|
|
$endpoint = 'statuses/update_with_media'; |
174
|
|
|
$args['media[]'] = wp_remote_retrieve_body( wp_remote_get( $media ) ); |
175
|
|
|
} else { |
176
|
|
|
$endpoint = 'statuses/update'; |
177
|
|
|
} |
178
|
|
|
$args['status'] = $message; |
179
|
|
|
|
180
|
|
|
return $this->twitter->post( $endpoint, $args, true ); |
181
|
|
|
} else { |
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
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: