|
1 <?php |
|
2 /** |
|
3 * Twitter implementation for Social. |
|
4 * |
|
5 * @package Social |
|
6 * @subpackage plugins |
|
7 */ |
|
8 if (class_exists('Social') and !class_exists('Social_Facebook')) { |
|
9 |
|
10 final class Social_Facebook { |
|
11 |
|
12 /** |
|
13 * Registers Facebook to Social. |
|
14 * |
|
15 * @static |
|
16 * @wp-filter social_register_service |
|
17 * |
|
18 * @param array $services |
|
19 * |
|
20 * @return array |
|
21 */ |
|
22 public static function register_service(array $services) { |
|
23 $services[] = 'facebook'; |
|
24 return $services; |
|
25 } |
|
26 |
|
27 /** |
|
28 * Adds the permissions stuff in for Facebook. |
|
29 * |
|
30 * @static |
|
31 * @wp-filter social_authorize_url |
|
32 * @param string $url authorization url |
|
33 * @param string $key service key |
|
34 * @return string |
|
35 */ |
|
36 public static function social_authorize_url($url, $key) { |
|
37 if ($key == 'facebook') { |
|
38 $perms = 'publish_stream'; |
|
39 if (is_admin()) { |
|
40 $perms .= ',read_stream,offline_access'; |
|
41 } |
|
42 |
|
43 $url = $url.'?req_perms='.$perms; |
|
44 } |
|
45 |
|
46 return $url; |
|
47 } |
|
48 |
|
49 /** |
|
50 * Adds to the avatar comment types array. |
|
51 * |
|
52 * @static |
|
53 * @param array $types |
|
54 * @return array |
|
55 */ |
|
56 public static function get_avatar_comment_types(array $types) { |
|
57 return array_merge($types, Social_Service_Facebook::comment_types()); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Gets the avatar based on the comment type. |
|
62 * |
|
63 * @static |
|
64 * @wp-filter get_avatar |
|
65 * @param string $avatar |
|
66 * @param object $comment |
|
67 * @param int $size |
|
68 * @param string $default |
|
69 * @param string $alt |
|
70 * @return string |
|
71 */ |
|
72 public static function get_avatar($avatar, $comment, $size, $default, $alt) { |
|
73 if (is_object($comment) and in_array($comment->comment_type, Social_Service_Facebook::comment_types())) { |
|
74 $image = esc_url(get_comment_meta($comment->comment_ID, 'social_profile_image_url', true)); |
|
75 if ($image !== null) { |
|
76 $size = esc_attr($size); |
|
77 $type = esc_attr($comment->comment_type); |
|
78 return '<img alt="'.$alt.'" src="'.$image.'" class="avatar avatar-'.$size.' photo '.$type.'" height="'.$size.'" width="'.$size.'" />'; |
|
79 } |
|
80 } |
|
81 return $avatar; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Pre-processor to the comments. |
|
86 * |
|
87 * @wp-filter social_comments_array |
|
88 * @static |
|
89 * @param array $comments |
|
90 * @param int $post_id |
|
91 * @return array |
|
92 */ |
|
93 public static function comments_array(array $comments, $post_id) { |
|
94 // pre-load the hashes for broadcasted tweets |
|
95 $broadcasted_ids = get_post_meta($post_id, '_social_broadcasted_ids', true); |
|
96 if (empty($broadcasted_ids) or empty($broadcasted_ids['facebook'])) { |
|
97 $broadcasted_ids = array(); |
|
98 } |
|
99 global $wpdb; |
|
100 |
|
101 // we need comments to be keyed by ID, check for Facebook comments |
|
102 $facebook_comments = $facebook_likes = $_comments = $comment_ids = array(); |
|
103 foreach ($comments as $key => $comment) { |
|
104 if (is_object($comment)) { |
|
105 $_comments['id_'.$comment->comment_ID] = $comment; |
|
106 if (in_array($comment->comment_type, Social_Service_Facebook::comment_types())) { |
|
107 $comment_ids[] = $comment->comment_ID; |
|
108 $facebook_comments['id_'.$comment->comment_ID] = $comment; |
|
109 } |
|
110 } |
|
111 else { // social items |
|
112 $_comments[$key] = $comment; |
|
113 } |
|
114 } |
|
115 |
|
116 // if no Facebook comments, get out now |
|
117 if (!count($facebook_comments)) { |
|
118 return $comments; |
|
119 } |
|
120 |
|
121 // use our keyed array |
|
122 $comments = $_comments; |
|
123 unset($_comments); |
|
124 |
|
125 // Load the comment meta |
|
126 $results = $wpdb->get_results(" |
|
127 SELECT meta_key, meta_value, comment_id |
|
128 FROM $wpdb->commentmeta |
|
129 WHERE comment_id IN (".implode(',', $comment_ids).") |
|
130 AND ( |
|
131 meta_key = 'social_status_id' |
|
132 OR meta_key = 'social_profile_image_url' |
|
133 OR meta_key = 'social_comment_type' |
|
134 ) |
|
135 "); |
|
136 |
|
137 // Set up social data for facebook comments |
|
138 foreach ($facebook_comments as $key => &$comment) { |
|
139 $comment->social_items = array(); |
|
140 |
|
141 // Attach meta |
|
142 foreach ($results as $result) { |
|
143 if ($comment->comment_ID == $result->comment_id) { |
|
144 $comment->{$result->meta_key} = $result->meta_value; |
|
145 } |
|
146 } |
|
147 } |
|
148 |
|
149 // merge data so that $comments has the data we've set up |
|
150 $comments = array_merge($comments, $facebook_comments); |
|
151 |
|
152 // set-up the likes |
|
153 foreach ($facebook_comments as $key => &$comment) { |
|
154 if (is_object($comment) and isset($broadcasted_ids['facebook'])) { |
|
155 foreach ($broadcasted_ids['facebook'] as $account_id => $broadcasted) { |
|
156 if (isset($comment->social_status_id) and isset($broadcasted[$comment->social_status_id]) and $comment->comment_type == 'social-facebook-like') { |
|
157 $facebook_likes[] = $comment; |
|
158 unset($comments['id_'.$comment->comment_ID]); |
|
159 } |
|
160 } |
|
161 } |
|
162 } |
|
163 |
|
164 // Add the likes |
|
165 if (!isset($comments['social_items'])) { |
|
166 $comments['social_items'] = array(); |
|
167 } |
|
168 |
|
169 if (count($facebook_likes)) { |
|
170 $comments['social_items']['facebook'] = $facebook_likes; |
|
171 } |
|
172 |
|
173 return $comments; |
|
174 } |
|
175 |
|
176 /** |
|
177 * Filters the groups. |
|
178 * |
|
179 * @static |
|
180 * @param array $groups |
|
181 * @param array $comments |
|
182 * @return array |
|
183 */ |
|
184 public static function comments_array_groups(array $groups, array $comments) { |
|
185 if (isset($groups['social-facebook-like'])) { |
|
186 if (!isset($groups['social-facebook'])) { |
|
187 $groups['social-facebook'] = 0; |
|
188 } |
|
189 |
|
190 $groups['social-facebook'] = $groups['social-facebook'] + $groups['social-facebook-like']; |
|
191 unset($groups['social-facebook-like']); |
|
192 } |
|
193 |
|
194 return $groups; |
|
195 } |
|
196 |
|
197 /** |
|
198 * Adds the Facebook Pages checkbox to the button. |
|
199 * |
|
200 * @static |
|
201 * @param string $button |
|
202 * @param Social_Service_Facebook $service |
|
203 * @param bool $profile_page |
|
204 * @return string |
|
205 */ |
|
206 public static function social_service_button($button, $service, $profile_page = false) { |
|
207 if ($service->key() == 'facebook') { |
|
208 $label = '<input type="checkbox" id="social-facebook-pages" value="true" />' |
|
209 . '<label for="social-facebook-pages">'.__('Connect with Pages support', 'social').'</label>'; |
|
210 |
|
211 if (!$profile_page) { |
|
212 $button = explode('</div>', $button); |
|
213 $button = $button[0].$label.'</div>'; |
|
214 } |
|
215 } |
|
216 return $button; |
|
217 } |
|
218 |
|
219 /** |
|
220 * Adds the manage pages permission onto the URL. |
|
221 * |
|
222 * @static |
|
223 * @param string $url |
|
224 * @return array|string |
|
225 */ |
|
226 public static function social_proxy_url($url) { |
|
227 if (isset($_GET['use_pages']) and strpos($url, 'req_perms') !== false) { |
|
228 $url = explode('req_perms=', $url); |
|
229 $url = $url[0].'req_perms=manage_pages,'.$url[1]; |
|
230 |
|
231 // Now add the query param to the response URL |
|
232 $url = explode('response_url=', $url); |
|
233 $response_url = add_query_arg(array( |
|
234 'use_pages' => 'true' |
|
235 ), urldecode($url[1])); |
|
236 $url = $url[0].'response_url='.urlencode($response_url); |
|
237 } |
|
238 return $url; |
|
239 } |
|
240 |
|
241 /** |
|
242 * Saves the Facebook pages. |
|
243 * |
|
244 * @wp-action social_settings_save |
|
245 * @static |
|
246 * @param bool $is_personal |
|
247 */ |
|
248 public static function social_settings_save($is_personal = false) { |
|
249 $service = Social::instance()->service('facebook'); |
|
250 if ($service !== false) { |
|
251 $accounts = $service->accounts(); |
|
252 if (count($accounts)) { |
|
253 foreach ($accounts as $account_id => $account) { |
|
254 if (isset($_POST['social_facebook_pages_'.$account->id()])) { |
|
255 $pages = $service->get_pages($account); |
|
256 |
|
257 $account->pages(array()); |
|
258 if (count($pages)) { |
|
259 foreach ($_POST['social_facebook_pages_'.$account->id()] as $page_id) { |
|
260 if (isset($pages[$page_id])) { |
|
261 $accounts[$account_id] = $account->page($pages[$page_id]); |
|
262 } |
|
263 } |
|
264 } |
|
265 } |
|
266 |
|
267 if (defined('IS_PROFILE_PAGE')) { |
|
268 $accounts[$account_id]->universal(false); |
|
269 $accounts[$account_id]->use_pages(false, false); |
|
270 $accounts[$account_id]->pages(array(), false); |
|
271 } |
|
272 else { |
|
273 $accounts[$account_id]->personal(false); |
|
274 $accounts[$account_id]->use_pages(true, false); |
|
275 $accounts[$account_id]->pages(array(), true); |
|
276 } |
|
277 |
|
278 $accounts[$account_id] = $accounts[$account_id]->as_object(); |
|
279 } |
|
280 |
|
281 $service->accounts($accounts)->save($is_personal); |
|
282 } |
|
283 } |
|
284 } |
|
285 |
|
286 /** |
|
287 * @static |
|
288 * @param object $account |
|
289 * @param WP_Post $post |
|
290 * @param Social_Service_Facebook $service |
|
291 * |
|
292 * @return object|bool |
|
293 */ |
|
294 public static function social_get_broadcast_account($account, $post, $service) { |
|
295 if ($service->key() == 'facebook') { |
|
296 // Load accounts |
|
297 $found = false; |
|
298 $accounts = $service->accounts(); |
|
299 foreach ($accounts as $_account) { |
|
300 $pages = $_account->pages(null, 'combined'); |
|
301 if (isset($pages[$account->id])) { |
|
302 $found = true; |
|
303 $account = $_account->broadcast_page($pages[$account->id]); |
|
304 break; |
|
305 } |
|
306 } |
|
307 |
|
308 if (!$found) { |
|
309 $personal_accounts = get_user_meta($post->post_author, 'social_accounts', true); |
|
310 if (isset($personal_accounts['facebook'])) { |
|
311 foreach ($personal_accounts['facebook'] as $account_id => $_account) { |
|
312 $_account = new Social_Service_Facebook_Account($_account); |
|
313 $pages = $_account->pages(null, 'combined'); |
|
314 if (isset($pages[$account->id])) { |
|
315 $found = true; |
|
316 $account = $_account->broadcast_page($pages[$account->id]); |
|
317 break; |
|
318 } |
|
319 } |
|
320 } |
|
321 } |
|
322 |
|
323 if ($found) { |
|
324 return $account; |
|
325 } |
|
326 } |
|
327 |
|
328 return false; |
|
329 } |
|
330 |
|
331 /** |
|
332 * Sets the raw data for the broadcasted post. |
|
333 * |
|
334 * @wp-filter social_broadcast_response |
|
335 * @static |
|
336 * @param array $data |
|
337 * @param Social_Service_Account $account |
|
338 * @param string $service_key |
|
339 * @param int $post_id |
|
340 * @param Social_Response $response |
|
341 * @return array |
|
342 */ |
|
343 public static function social_save_broadcasted_ids_data(array $data, Social_Service_Account $account, $service_key, $post_id, Social_Response $response = null) { |
|
344 if ($service_key == 'facebook') { |
|
345 $broadcast_page = $account->broadcast_page(); |
|
346 if ($broadcast_page !== null) { |
|
347 $data['page'] = (object) array( |
|
348 'id' => $broadcast_page->id, |
|
349 'name' => $broadcast_page->name |
|
350 ); |
|
351 } |
|
352 |
|
353 $data['account'] = (object) array( |
|
354 'user' => $account->as_object()->user |
|
355 ); |
|
356 } |
|
357 |
|
358 return $data; |
|
359 } |
|
360 |
|
361 /** |
|
362 * Filter to change the view for Facebook Pages |
|
363 * |
|
364 * @static |
|
365 * @param string $file |
|
366 * @param array $data |
|
367 * @return string |
|
368 */ |
|
369 public static function social_view_set_file($file, $data) { |
|
370 if (isset($data['service']) and |
|
371 $data['service'] != false and |
|
372 $data['service']->key() == 'facebook' and |
|
373 (isset($data['data']) and isset($data['data']['page'])) or |
|
374 (isset($data['account']) and !$data['account'] instanceof Social_Service_Account)) |
|
375 { |
|
376 $file = 'wp-admin/post/meta/broadcast/parts/facebook/page'; |
|
377 } |
|
378 |
|
379 return $file; |
|
380 } |
|
381 |
|
382 /** |
|
383 * Sets the Social view data. |
|
384 * |
|
385 * @static |
|
386 * @param array $data |
|
387 * @param string $file |
|
388 * @return array |
|
389 */ |
|
390 public static function social_view_data($data, $file) { |
|
391 if ($file == 'wp-admin/post/meta/broadcast/parts/facebook/page') { |
|
392 if (isset($data['data']) and isset($data['data']['page'])) { |
|
393 $data['account'] = $data['data']['page']; |
|
394 } |
|
395 else if ($data['account'] instanceof Social_Service_Account) { |
|
396 $data['account'] = (object) array( |
|
397 'id' => $data['account']->id(), |
|
398 'name' => $data['account']->username() |
|
399 ); |
|
400 } |
|
401 } |
|
402 |
|
403 return $data; |
|
404 } |
|
405 |
|
406 /** |
|
407 * Merges the personal pages into the universal account. |
|
408 * |
|
409 * @static |
|
410 * @param object $universal |
|
411 * @param object $personal |
|
412 * @param string $service_key |
|
413 * @return object |
|
414 */ |
|
415 public static function social_merge_accounts($universal, $personal, $service_key) { |
|
416 // Merge pages |
|
417 if ($service_key == 'facebook') { |
|
418 $universal->pages->personal = $personal->pages->personal; |
|
419 $universal->use_personal_pages = $personal->use_personal_pages; |
|
420 } |
|
421 return $universal; |
|
422 } |
|
423 |
|
424 /** |
|
425 * Adds messaging to the title. |
|
426 * |
|
427 * @static |
|
428 * @param string $title |
|
429 * @param string $key |
|
430 * @return string |
|
431 */ |
|
432 public static function social_item_output_title($title, $key) { |
|
433 if ($key == 'facebook') { |
|
434 $title = sprintf(__('%s liked this', 'social'), $title); |
|
435 } |
|
436 |
|
437 return $title; |
|
438 } |
|
439 |
|
440 /** |
|
441 * Output the link to be sent to Facebook. |
|
442 * |
|
443 * @static |
|
444 * @param object $post |
|
445 * @param object $service |
|
446 * @param object $account |
|
447 * @return void |
|
448 */ |
|
449 public static function social_broadcast_form_item_content($post, $service, $account) { |
|
450 if ($service->key() != 'facebook' || get_post_format($post) == 'status') { |
|
451 return; |
|
452 } |
|
453 remove_filter('social_view_set_file', array('Social_Facebook', 'social_view_set_file'), 10, 2); |
|
454 echo Social_View::factory( |
|
455 'wp-admin/post/broadcast/facebook-link-preview', |
|
456 compact('post', 'service', 'account') |
|
457 )->render(); |
|
458 add_filter('social_view_set_file', array('Social_Facebook', 'social_view_set_file'), 10, 2); |
|
459 } |
|
460 |
|
461 /** |
|
462 * Don't output URL in format since we're sending a link as well. |
|
463 * |
|
464 * @static |
|
465 * @param string $format |
|
466 * @param object $post |
|
467 * @param object $service |
|
468 * @return string |
|
469 */ |
|
470 public static function social_broadcast_format($format, $post, $service) { |
|
471 if ($service->key() == 'facebook' && get_post_format($post) != 'status') { |
|
472 $format = trim(str_replace('{url}', '', $format)); |
|
473 } |
|
474 return $format; |
|
475 } |
|
476 |
|
477 |
|
478 } // End Social_Facebook |
|
479 |
|
480 define('SOCIAL_FACEBOOK_FILE', __FILE__); |
|
481 |
|
482 // Actions |
|
483 add_action('social_settings_save', array('Social_Facebook', 'social_settings_save')); |
|
484 add_action('social_broadcast_form_item_content', array('Social_Facebook', 'social_broadcast_form_item_content'), 10, 3); |
|
485 |
|
486 // Filters |
|
487 add_filter('social_register_service', array('Social_Facebook', 'register_service')); |
|
488 add_filter('social_authorize_url', array('Social_Facebook', 'social_authorize_url'), 10, 2); |
|
489 add_filter('get_avatar', array('Social_Facebook', 'get_avatar'), 10, 5); |
|
490 add_filter('get_avatar_comment_types', array('Social_Facebook', 'get_avatar_comment_types')); |
|
491 add_filter('social_comments_array', array('Social_Facebook', 'comments_array'), 10, 2); |
|
492 add_filter('social_comments_array_groups', array('Social_Facebook', 'comments_array_groups'), 10, 2); |
|
493 add_filter('social_service_button', array('Social_Facebook', 'social_service_button'), 10, 3); |
|
494 add_filter('social_proxy_url', array('Social_Facebook', 'social_proxy_url')); |
|
495 add_filter('social_get_broadcast_account', array('Social_Facebook', 'social_get_broadcast_account'), 10, 3); |
|
496 add_filter('social_save_broadcasted_ids_data', array('Social_Facebook', 'social_save_broadcasted_ids_data'), 10, 5); |
|
497 add_filter('social_view_set_file', array('Social_Facebook', 'social_view_set_file'), 10, 2); |
|
498 add_filter('social_view_data', array('Social_Facebook', 'social_view_data'), 10, 2); |
|
499 add_filter('social_merge_accounts', array('Social_Facebook', 'social_merge_accounts'), 10, 3); |
|
500 add_filter('social_item_output_title', array('Social_Facebook', 'social_item_output_title'), 10, 2); |
|
501 add_filter('social_broadcast_format', array('Social_Facebook', 'social_broadcast_format'), 11, 3); |
|
502 |
|
503 } |