1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Exit if accessed directly |
4
|
|
|
if ( !defined( 'ABSPATH' ) ) { |
5
|
|
|
exit; |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Facebook Class |
10
|
|
|
* |
11
|
|
|
* Handles all facebook functions |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
if( !class_exists( 'PPP_Facebook' ) ) { |
15
|
|
|
|
16
|
|
|
class PPP_Facebook { |
17
|
|
|
|
18
|
|
|
var $facebook; |
19
|
|
|
|
20
|
|
|
public function __construct(){ |
21
|
|
|
ppp_maybe_start_session(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Include Facebook Class |
26
|
|
|
* |
27
|
|
|
* Handles to load facebook class |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
public function ppp_load_facebook() { |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
if( !class_exists( 'Facebook' ) ) { |
34
|
|
|
require_once ( PPP_PATH . '/includes/libs/facebook/facebook.php' ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
ppp_set_social_tokens(); |
38
|
|
|
|
39
|
|
|
$this->facebook = new Facebook( array( |
40
|
|
|
'appId' => PPP_FB_APP_ID, |
41
|
|
|
'secret' => PPP_FB_APP_SECRET, |
42
|
|
|
'cookie' => true |
43
|
|
|
)); |
44
|
|
|
|
45
|
|
|
return true; |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initializes Facebook API |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
|
|
public function ppp_initialize_facebook() { |
54
|
|
|
//load facebook class |
55
|
|
|
$facebook = $this->ppp_load_facebook(); |
56
|
|
|
|
57
|
|
|
//when user is going to logged in and verified successfully session will create |
58
|
|
|
if ( isset( $_REQUEST['fb_access_token'] ) && isset( $_REQUEST['expires_in'] ) ) { |
59
|
|
|
|
60
|
|
|
$access_token = $_REQUEST['fb_access_token']; |
61
|
|
|
$expires_in = $_REQUEST['expires_in']; |
62
|
|
|
|
63
|
|
|
} elseif ( isset( $_GET['state'] ) && strpos( $_GET['state'], 'ppp-local-keys-fb' ) !== false ) { |
64
|
|
|
$access_code = isset( $_GET['code'] ) ? $_GET['code'] : false; |
65
|
|
|
|
66
|
|
|
if ( empty( $access_code ) ) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$params = '?client_id=' . PPP_FB_APP_ID; |
71
|
|
|
$params .= '&client_secret=' . PPP_FB_APP_SECRET; |
72
|
|
|
$params .= '&code=' . $access_code; |
73
|
|
|
$params .= '&redirect_uri=' . admin_url( 'admin.php?page=ppp-social-settings' ); |
74
|
|
|
$url = 'https://graph.facebook.com/oauth/access_token' . $params; |
75
|
|
|
|
76
|
|
|
parse_str( wp_remote_retrieve_body( wp_remote_post( $url ) ), $result ); |
77
|
|
|
|
78
|
|
|
$access_token = ! empty( $result['access_token'] ) ? $result['access_token'] : false; |
79
|
|
|
$expires_in = ! empty( $result['expires'] ) ? $result['expires'] : false; |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ( ! empty( $access_token ) ) { |
84
|
|
|
global $ppp_social_settings; |
85
|
|
|
$ppp_social_settings = get_option( 'ppp_social_settings' ); |
86
|
|
|
|
87
|
|
|
//check facebook class is loaded or not |
88
|
|
|
if( !$facebook ) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$data = new stdClass(); |
93
|
|
|
$data->access_token = $access_token; |
94
|
|
|
|
95
|
|
|
$expires_in = empty( $expires_in ) ? 60 * 24 * 60 * 60 : $expires_in; // days * hours * minutes * seconds |
96
|
|
|
$data->expires_on = current_time( 'timestamp' ) + $expires_in; |
97
|
|
|
|
98
|
|
|
update_option( '_ppp_facebook_refresh', current_time( 'timestamp' ) + round( $expires_in/1.25 ) ); |
99
|
|
|
|
100
|
|
|
// Now that we have a valid auth, get some user info |
101
|
|
|
$user_info = $this->ppp_get_fb_user( $data->access_token ); |
102
|
|
|
|
103
|
|
|
if ( $user_info ) { |
104
|
|
|
if ( !empty( $user_info->name ) ) { |
105
|
|
|
$data->name = $user_info->name; |
106
|
|
|
} else { |
107
|
|
|
$parsed_name = $user_info->first_name . ' ' . $user_info->last_name; |
108
|
|
|
$data->name = $parsed_name; |
109
|
|
|
} |
110
|
|
|
$data->userid = $user_info->id; |
111
|
|
|
$data->avatar = $this->ppp_fb_get_profile_picture( array( 'type' => 'square' ), $data->userid ); |
112
|
|
|
|
113
|
|
|
if ( ! empty( $ppp_social_settings['facebook']->page ) ) { |
114
|
|
|
$current_page = $ppp_social_settings['facebook']->page; |
115
|
|
|
$page_parts = explode( '|', $current_page ); |
116
|
|
|
|
117
|
|
|
$pages = $this->ppp_get_fb_user_pages( $data->access_token ); |
118
|
|
|
|
119
|
|
|
foreach ( $pages as $page ) { |
120
|
|
|
if ( $page->id == $page_parts[2] ) { |
121
|
|
|
$data->page = $page->name . '|' . $page->access_token . '|' . $page->id; |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$ppp_social_settings['facebook'] = $data; |
129
|
|
|
|
130
|
|
|
update_option( 'ppp_social_settings', $ppp_social_settings ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
$url = remove_query_arg( array( 'fb_access_token' , 'expires_in' ) ); |
135
|
|
|
wp_redirect( $url ); |
136
|
|
|
die(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get Facebook User |
142
|
|
|
* |
143
|
|
|
* Handles to return facebook user id |
144
|
|
|
* |
145
|
|
|
*/ |
146
|
|
|
public function ppp_get_fb_user( $access_token ) { |
147
|
|
|
|
148
|
|
|
//load facebook class |
149
|
|
|
$facebook = $this->ppp_load_facebook(); |
150
|
|
|
|
151
|
|
|
//check facebook class is exis or not |
152
|
|
|
if( !$facebook ) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
global $ppp_social_settings; |
157
|
|
|
$user = json_decode( wp_remote_retrieve_body( wp_remote_get( 'https://graph.facebook.com/me?access_token=' . $access_token ) ) ); |
158
|
|
|
|
159
|
|
|
return $user; |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function ppp_get_fb_user_pages( $access_token ) { |
164
|
|
|
|
165
|
|
|
// load facebook class |
166
|
|
|
$facebook = $this->ppp_load_facebook(); |
167
|
|
|
|
168
|
|
|
// check facebook cleast is exists or not |
169
|
|
|
if( !$facebook ) { |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
global $ppp_social_settings; |
174
|
|
|
$facebook_settings = $ppp_social_settings['facebook']; |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
if ( ! isset( $facebook_settings->available_pages ) || |
178
|
|
|
! isset( $facebook_settings->pages_last_updated ) || |
179
|
|
|
$facebook_settings->pages_last_updated < current_time( 'timestamp' ) ) { |
180
|
|
|
|
181
|
|
|
$all_pages = json_decode( wp_remote_retrieve_body( wp_remote_get( 'https://graph.facebook.com/me/accounts?access_token=' . $access_token ) ) ); |
182
|
|
|
$pages = array(); |
183
|
|
|
|
184
|
|
|
if ( !empty( $all_pages ) ) { |
185
|
|
|
foreach ( $all_pages->data as $page ) { |
186
|
|
|
if ( in_array( 'CREATE_CONTENT', $page->tasks ) ) { |
187
|
|
|
$pages[] = $page; |
188
|
|
|
|
189
|
|
|
if ( ! empty( $ppp_social_settings['facebook']->page ) && strpos( $ppp_social_settings['facebook']->page, $page->id ) ) { |
190
|
|
|
$ppp_social_settings['facebook']->page = $page->name . '|' . $page->access_token . '|' . $page->id; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} else { |
195
|
|
|
$pages = false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
$pages = (object) $pages; |
200
|
|
|
$ppp_social_settings['facebook']->available_pages = $pages; |
201
|
|
|
$ppp_social_settings['facebook']->pages_last_updated = current_time( 'timestamp' ) + ( HOUR_IN_SECONDS / 4 ); |
202
|
|
|
update_option( 'ppp_social_settings', $ppp_social_settings ); |
203
|
|
|
} else { |
204
|
|
|
$pages = $facebook_settings->available_pages; |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $pages; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Access Token |
213
|
|
|
* |
214
|
|
|
* Getting the access token from Facebook. |
215
|
|
|
* |
216
|
|
|
*/ |
217
|
|
|
public function ppp_fb_getaccesstoken() { |
218
|
|
|
|
219
|
|
|
//load facebook class |
220
|
|
|
$facebook = $this->ppp_load_facebook(); |
221
|
|
|
|
222
|
|
|
//check facebook class is exis or not |
223
|
|
|
if( !$facebook ) { |
224
|
|
|
return false; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $this->facebook->getAccessToken(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get auth url for facebook |
232
|
|
|
* |
233
|
|
|
*/ |
234
|
|
|
public function ppp_get_facebook_auth_url ( $return_url ) { |
235
|
|
|
|
236
|
|
|
//load facebook class |
237
|
|
|
$facebook = $this->ppp_load_facebook(); |
238
|
|
|
|
239
|
|
|
//check facebook class is exis or not |
240
|
|
|
if( !$facebook ) { |
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if ( ! PPP_LOCAL_TOKENS ) { |
245
|
|
|
$base_url = 'https://postpromoterpro.com/?ppp-social-auth'; |
246
|
|
|
$url = $base_url . '&ppp-service=fb&ppp-license-key=' . trim( get_option( '_ppp_license_key' ) ); |
247
|
|
|
$url .= '&nocache'; |
248
|
|
|
$url .= '&return_url=' . esc_url( $return_url ); |
249
|
|
|
} else { |
250
|
|
|
$url = 'https://graph.facebook.com/oauth/authorize?'; |
251
|
|
|
$url .= 'client_id=' . PPP_FB_APP_ID; |
252
|
|
|
$url .= '&scope=public_profile,publish_actions,manage_pages,publish_pages'; |
253
|
|
|
$url .= '&state=ppp-local-keys-fb'; |
254
|
|
|
$url .= '&redirect_uri=' . esc_url( $return_url ) . '&nocache'; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $url; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* User Image |
262
|
|
|
* |
263
|
|
|
* Getting the the profile image of the connected Facebook user. |
264
|
|
|
* |
265
|
|
|
*/ |
266
|
|
|
public function ppp_fb_get_profile_picture( $args=array(), $user ) { |
267
|
|
|
|
268
|
|
|
if( isset( $args['type'] ) && !empty( $args['type'] ) ) { |
269
|
|
|
$type = $args['type']; |
270
|
|
|
} else { |
271
|
|
|
$type = 'large'; |
272
|
|
|
} |
273
|
|
|
$url = 'https://graph.facebook.com/' . $user . '/picture?type=' . $type; |
274
|
|
|
return $url; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function ppp_fb_share_link( $link, $message, $image ) { |
278
|
|
|
global $ppp_social_settings; |
279
|
|
|
$facebook_settings = $ppp_social_settings['facebook']; |
280
|
|
|
|
281
|
|
|
if ( !isset( $facebook_settings->page ) || strtolower( $facebook_settings->page ) === 'me' ) { |
282
|
|
|
$account = 'me'; |
283
|
|
|
$access_token = $facebook_settings->access_token; |
284
|
|
|
} else { |
285
|
|
|
$page_info = explode( '|', $facebook_settings->page ); |
286
|
|
|
$account = $page_info[2]; |
287
|
|
|
$access_token = $page_info[1]; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$url = 'https://graph.facebook.com/' . $account . '/feed?access_token=' . $access_token; |
291
|
|
|
$args = array( 'link' => $link, 'message' => $message ); |
292
|
|
|
if ( !empty( $image ) ) { |
293
|
|
|
$args['picture'] = $image; |
294
|
|
|
} |
295
|
|
|
$results = json_decode( wp_remote_retrieve_body( wp_remote_post( $url, array( 'body' => $args ) ) ) ); |
296
|
|
|
|
297
|
|
|
return $results; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Get Data From URL |
302
|
|
|
* |
303
|
|
|
* Handles to get data from url |
304
|
|
|
* via CURL |
305
|
|
|
* |
306
|
|
|
*/ |
307
|
|
|
|
308
|
|
|
public function ppp_get_data_from_url( $url ) { |
309
|
|
|
|
310
|
|
|
//Use wp_remote_post and wp_remote_get |
311
|
|
|
$data = wp_remote_retrieve_body( wp_remote_get( $url ) ); |
312
|
|
|
|
313
|
|
|
return $data; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Flush the Open Graph cache for a given post_id |
318
|
|
|
* |
319
|
|
|
* @param $post_id The post ID to flush the OG cache for |
320
|
|
|
*/ |
321
|
|
|
public function clear_og_cache( $post_id ) { |
322
|
|
|
$post_url = get_permalink( $post_id ); |
323
|
|
|
if ( ! empty( $post_url ) ) { |
324
|
|
|
$args = array( 'body' => array( 'id' => $post_url, 'scrape' => true ) ); |
325
|
|
|
$response = json_decode( wp_remote_retrieve_body( wp_remote_post( 'https://graph.facebook.com/', $args ) ) ); |
|
|
|
|
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.