Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 405 lines (362 sloc) 11.019 kB
748b7ad Continued stubbing of Social.
Andrew Ellis authored
1 <?php
3efdd25 Social connect buttons are now available on the settings page.
Andrew Ellis authored
2 /**
3 * Twitter implementation for Social.
4 *
80d8b64 Formatting changes
Andrew Ellis authored
5 * @package Social
3efdd25 Social connect buttons are now available on the settings page.
Andrew Ellis authored
6 * @subpackage plugins
7 */
8 if (class_exists('Social') and !class_exists('Social_Facebook')) {
748b7ad Continued stubbing of Social.
Andrew Ellis authored
9
10 final class Social_Facebook {
11
12 /**
13 * Registers Facebook to Social.
14 *
15 * @static
37506c8 Added wp-filter and wp-action PHPDoc items
Andrew Ellis authored
16 * @wp-filter social_register_service
80d8b64 Formatting changes
Andrew Ellis authored
17 *
748b7ad Continued stubbing of Social.
Andrew Ellis authored
18 * @param array $services
80d8b64 Formatting changes
Andrew Ellis authored
19 *
748b7ad Continued stubbing of Social.
Andrew Ellis authored
20 * @return array
21 */
22 public static function register_service(array $services) {
23 $services[] = 'facebook';
24 return $services;
25 }
26
63e46e8 Can now correctly add accounts to the profile or universal array.
Andrew Ellis authored
27 /**
28 * Adds the permissions stuff in for Facebook.
29 *
30 * @static
37506c8 Added wp-filter and wp-action PHPDoc items
Andrew Ellis authored
31 * @wp-filter social_authorize_url
63e46e8 Can now correctly add accounts to the profile or universal array.
Andrew Ellis authored
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
96ab414 Fixed the query delimiter on the authorize URL
Andrew Ellis authored
43 $url = $url.'?req_perms='.$perms;
63e46e8 Can now correctly add accounts to the profile or universal array.
Andrew Ellis authored
44 }
45
46 return $url;
47 }
48
4f848f6 Now importing Facebook Likes as comments.
Andrew Ellis authored
49 /**
50 * Quick hook to fix the comment type to service.
51 *
52 * @static
53 * @wp-filter social_comment_type_to_service
54 * @param string $type
55 * @return string
56 */
57 public static function comment_type_to_service($type) {
58 if ($type == 'facebook-like') {
59 $type = 'facebook';
60 }
61
62 return $type;
63 }
64
65 /**
66 * Adds to the avatar comment types array.
67 *
68 * @static
69 * @param array $types
70 * @return array
71 */
72 public static function get_avatar_comment_types(array $types) {
80d8b64 Formatting changes
Andrew Ellis authored
73 return array_merge($types, array(
e607aa4 Introduced the new Facebook Likes design
Andrew Ellis authored
74 'social-facebook',
75 'social-facebook-like'
76 ));
4f848f6 Now importing Facebook Likes as comments.
Andrew Ellis authored
77 }
78
79 /**
80 * Gets the avatar based on the comment type.
81 *
82 * @static
83 * @wp-filter get_avatar
84 * @param string $avatar
85 * @param object $comment
86 * @param int $size
87 * @param string $default
88 * @param string $alt
89 * @return string
90 */
91 public static function get_avatar($avatar, $comment, $size, $default, $alt) {
2830bf3 Now prefixing comment types with social-
Andrew Ellis authored
92 if (is_object($comment) and $comment->comment_type == 'social-facebook-like') {
4f848f6 Now importing Facebook Likes as comments.
Andrew Ellis authored
93 $image = get_comment_meta($comment->comment_ID, 'social_profile_image_url', true);
94 if ($image !== null) {
95 $type = '';
96 if (is_object($comment)) {
97 $type = $comment->comment_type;
98 }
99 return "<img alt='{$alt}' src='{$image}' class='avatar avatar-{$size} photo {$type}' height='25' width='25' />";
100 }
101 }
102 return $avatar;
103 }
104
80d8b64 Formatting changes
Andrew Ellis authored
105 /**
106 * Pre-processor to the comments.
107 *
108 * @wp-filter social_comments_array
109 * @static
110 * @param array $comments
111 * @param int $post_id
112 * @return array
113 */
114 public static function comments_array(array $comments, $post_id) {
e607aa4 Introduced the new Facebook Likes design
Andrew Ellis authored
115 global $wpdb;
116
117 $comment_ids = array();
118 foreach ($comments as $comment) {
119 if (is_object($comment) and $comment->comment_type == 'social-facebook-like') {
120 $comment_ids[] = $comment->comment_ID;
121 }
122 }
123
124 if (count($comment_ids)) {
125 $results = $wpdb->get_results("
126 SELECT meta_key, meta_value, comment_id
127 FROM $wpdb->commentmeta
128 WHERE comment_id IN (".implode(',', $comment_ids).")
129 AND meta_key = 'social_status_id'
130 OR meta_key = 'social_profile_image_url'
dbf718c Fixed comment counts.
Andrew Ellis authored
131 OR meta_key = 'social_comment_type'
e607aa4 Introduced the new Facebook Likes design
Andrew Ellis authored
132 ");
133
134 $social_items = array();
135 if (isset($comments['social_items'])) {
136 $social_items = $comments['social_items'];
e5b7e1b Comments are now ordered by time correctly
Andrew Ellis authored
137 unset($comments['social_items']);
e607aa4 Introduced the new Facebook Likes design
Andrew Ellis authored
138 }
139
140 foreach ($comments as $key => $comment) {
141 if (is_object($comment)) {
142 if ($comment->comment_type == 'social-facebook-like') {
143 foreach ($results as $result) {
144 if ($result->comment_id == $comment->comment_ID) {
145 $comment->{$result->meta_key} = $result->meta_value;
146 }
147 }
148
149 if (!isset($social_items['facebook'])) {
150 $social_items['facebook'] = array();
151 }
152
153 $social_items['facebook'][] = $comment;
e5b7e1b Comments are now ordered by time correctly
Andrew Ellis authored
154 unset($comments[$key]);
e607aa4 Introduced the new Facebook Likes design
Andrew Ellis authored
155 }
156 else {
157 $comments[$key] = $comment;
158 }
159 }
160 else {
161 $comments[$key] = $comment;
162 }
163 }
164
165 if (count($social_items)) {
e5b7e1b Comments are now ordered by time correctly
Andrew Ellis authored
166 $comments['social_items'] = $social_items;
e607aa4 Introduced the new Facebook Likes design
Andrew Ellis authored
167 }
168 }
169
ca86055 Pre-development bug fixes
Andrew Ellis authored
170 return $comments;
80d8b64 Formatting changes
Andrew Ellis authored
171 }
3d44fba Retweets are no longer hidden when tab not in focus, rather set opaci…
Andrew Ellis authored
172
71b5d4b Adding authentication for pages.
Andrew Ellis authored
173 /**
174 * Adds the Facebook Pages checkbox to the button.
175 *
176 * @static
177 * @param string $button
178 * @param Social_Service_Facebook $service
179 * @param bool $profile_page
180 * @return string
181 */
182 public static function social_service_button($button, $service, $profile_page = false) {
183 if ($service->key() == 'facebook') {
184 $label = '<label for="social-facebook-pages">'
185 . ' <input type="checkbox" id="social-facebook-pages" value="true" />'
186 . ' Connect with Pages support'
187 . '</label>';
188
189 if (!$profile_page) {
190 $button = explode('</div>', $button);
191 $button = $button[0].$label.'</div>';
192 }
193 }
194 return $button;
195 }
196
197 /**
198 * Adds the manage pages permission onto the URL.
199 *
200 * @static
201 * @param string $url
202 * @return array|string
203 */
204 public static function social_proxy_url($url) {
205 if (isset($_GET['use_pages']) and strpos($url, 'req_perms') !== false) {
206 $url = explode('req_perms=', $url);
a0b5f8a Added the use pages flag to the authorization response
Andrew Ellis authored
207 $url = $url[0].'req_perms=manage_pages,'.$url[1];
208
209 // Now add the query param to the response URL
210 $url = explode('response_url=', $url);
211 $response_url = add_query_arg(array(
212 'use_pages' => 'true'
213 ), urldecode($url[1]));
214 $url = $url[0].'response_url='.urlencode($response_url);
71b5d4b Adding authentication for pages.
Andrew Ellis authored
215 }
216 return $url;
217 }
218
ece048f Can now save Facebook Pages on the settings page.
Andrew Ellis authored
219 /**
220 * Saves the Facebook pages.
221 *
222 * @wp-action social_settings_save
223 * @static
224 * @param bool $is_personal
225 */
226 public static function social_settings_save($is_personal = false) {
227 $service = Social::instance()->service('facebook');
85d5262 Can now broadcast to facebook pages
Andrew Ellis authored
228 if ($service !== false) {
ece048f Can now save Facebook Pages on the settings page.
Andrew Ellis authored
229 $accounts = $service->accounts();
230 if (count($accounts)) {
231 foreach ($accounts as $account_id => $account) {
232 if (isset($_POST['social_facebook_pages_'.$account->id()])) {
233 $pages = $service->get_pages($account);
234
235 $account->pages(array());
236 if (count($pages)) {
237 foreach ($_POST['social_facebook_pages_'.$account->id()] as $page_id) {
238 if (isset($pages[$page_id])) {
0a33df3 Fixed a bug that caused the settings page to error
Andrew Ellis authored
239 $accounts[$account_id] = $account->page($pages[$page_id]);
ece048f Can now save Facebook Pages on the settings page.
Andrew Ellis authored
240 }
241 }
242 }
243 }
0a33df3 Fixed a bug that caused the settings page to error
Andrew Ellis authored
244
245 $accounts[$account_id] = $accounts[$account_id]->as_object();
ece048f Can now save Facebook Pages on the settings page.
Andrew Ellis authored
246 }
247
248 $service->accounts($accounts)->save($is_personal);
249 }
85d5262 Can now broadcast to facebook pages
Andrew Ellis authored
250 }
251 }
252
253 /**
254 * @static
255 * @param object $account
256 * @param WP_Post $post
257 * @param Social_Service_Facebook $service
258 *
259 * @return object|bool
260 */
261 public static function social_get_broadcast_account($account, $post, $service) {
262 if ($service->key() == 'facebook') {
263 // Load accounts
264 $found = false;
265 $accounts = $service->accounts();
266 foreach ($accounts as $_account) {
267 $pages = $_account->pages(null, 'combined');
268 if (isset($pages[$account->id])) {
269 $found = true;
270 $account = $_account->broadcast_page($pages[$account->id]);
271 }
272 }
273
274 if (!$found) {
275 $personal_accounts = get_user_meta($post->post_author, 'social_accounts', true);
276 if (isset($personal_accounts['facebook'])) {
277 foreach ($personal_accounts['facebook'] as $account_id => $_account) {
278 $_account = new Social_Service_Facebook_Account($_account);
279 $pages = $_account->pages(null, 'combined');
280 if (isset($pages[$account->id])) {
281 $found = true;
282 $account = $_account->broadcast_page($pages[$account->id]);
283 }
284 }
285 }
286 }
287
288 if ($found) {
289 return $account;
290 }
291 }
292
293 return false;
294 }
295
296 /**
297 * Stores the Facebook page data to the broadcasted ID.
298 *
299 * @static
300 *
301 * @param array $data
302 * @param Social_Service_Facebook_Account $account
303 * @param Social_Service_Facebook $service
304 * @param int $post_id
305 *
306 * @return array
307 */
308 public static function social_save_broadcasted_ids_data($data, $account, $service, $post_id) {
309 if ($service == 'facebook') {
310 $broadcast_page = $account->broadcast_page();
311 if ($broadcast_page !== null) {
312 $data['page'] = (object) array(
313 'id' => $broadcast_page->id,
314 'name' => $broadcast_page->name
315 );
316 }
317 }
318
319 return $data;
320 }
321
322 /**
323 * Filter to change the view for Facebook Pages
324 *
325 * @static
326 * @param string $file
327 * @param array $data
328 * @return string
329 */
330 public static function social_view_set_file($file, $data) {
0692737 Fixed the meta box for broadcasted and scheduled accounts
Andrew Ellis authored
331 if (isset($data['service']) and
9ed6579 Comment tab counts are now correct. Comment types by tab are displaye…
Andrew Ellis authored
332 $data['service'] != false and
0692737 Fixed the meta box for broadcasted and scheduled accounts
Andrew Ellis authored
333 $data['service']->key() == 'facebook' and
334 (isset($data['data']) and isset($data['data']['page'])) or
335 (isset($data['account']) and !$data['account'] instanceof Social_Service_Account))
336 {
337 $file = 'wp-admin/post/meta/broadcast/parts/facebook/page';
338 }
339
340 return $file;
341 }
1c9b8eb Facebook Pages are now listed on the meta box for scheduled posts
Andrew Ellis authored
342
f261f6b Broadcast meta box no longer breaks when a page has been scheduled.
Andrew Ellis authored
343 /**
344 * Sets the Social view data.
345 *
346 * @static
347 * @param array $data
348 * @param string $file
349 * @return array
350 */
0692737 Fixed the meta box for broadcasted and scheduled accounts
Andrew Ellis authored
351 public static function social_view_data($data, $file) {
352 if ($file == 'wp-admin/post/meta/broadcast/parts/facebook/page') {
353 if (isset($data['data']) and isset($data['data']['page'])) {
354 $data['account'] = $data['data']['page'];
355 }
f261f6b Broadcast meta box no longer breaks when a page has been scheduled.
Andrew Ellis authored
356 else if ($data['account'] instanceof Social_Service_Account) {
0692737 Fixed the meta box for broadcasted and scheduled accounts
Andrew Ellis authored
357 $data['account'] = (object) array(
358 'id' => $data['account']->id(),
359 'name' => $data['account']->username()
a705ec6 Facebook Pages can now be (de-)selected when editing scheuled posts
Andrew Ellis authored
360 );
1c9b8eb Facebook Pages are now listed on the meta box for scheduled posts
Andrew Ellis authored
361 }
85d5262 Can now broadcast to facebook pages
Andrew Ellis authored
362 }
363
0692737 Fixed the meta box for broadcasted and scheduled accounts
Andrew Ellis authored
364 return $data;
ece048f Can now save Facebook Pages on the settings page.
Andrew Ellis authored
365 }
366
0470bf0 Rewrote how the personal facebook page data is merged into the univer…
Andrew Ellis authored
367 /**
368 * Merges the personal pages into the universal account.
369 *
370 * @static
371 * @param object $universal
372 * @param object $personal
373 * @return object
374 */
375 public static function social_merge_accounts($universal, $personal) {
376 // Merge pages
377 $universal->pages->personal = $personal->pages->personal;
378 $universal->use_personal_pages = $personal->use_personal_pages;
379 return $universal;
380 }
381
748b7ad Continued stubbing of Social.
Andrew Ellis authored
382 } // End Social_Facebook
383
3efdd25 Social connect buttons are now available on the settings page.
Andrew Ellis authored
384 define('SOCIAL_FACEBOOK_FILE', __FILE__);
80d8b64 Formatting changes
Andrew Ellis authored
385
ece048f Can now save Facebook Pages on the settings page.
Andrew Ellis authored
386 // Actions
387 add_action('social_settings_save', array('Social_Facebook', 'social_settings_save'));
388
748b7ad Continued stubbing of Social.
Andrew Ellis authored
389 // Filters
390 add_filter('social_register_service', array('Social_Facebook', 'register_service'));
63e46e8 Can now correctly add accounts to the profile or universal array.
Andrew Ellis authored
391 add_filter('social_authorize_url', array('Social_Facebook', 'social_authorize_url'), 10, 2);
4f848f6 Now importing Facebook Likes as comments.
Andrew Ellis authored
392 add_filter('social_comment_type_to_service', array('Social_Facebook', 'comment_type_to_service'));
393 add_filter('get_avatar', array('Social_Facebook', 'get_avatar'), 10, 5);
394 add_filter('get_avatar_comment_types', array('Social_Facebook', 'get_avatar_comment_types'));
ca86055 Pre-development bug fixes
Andrew Ellis authored
395 add_filter('social_comments_array', array('Social_Facebook', 'comments_array'), 10, 2);
71b5d4b Adding authentication for pages.
Andrew Ellis authored
396 add_filter('social_service_button', array('Social_Facebook', 'social_service_button'), 10, 3);
397 add_filter('social_proxy_url', array('Social_Facebook', 'social_proxy_url'));
85d5262 Can now broadcast to facebook pages
Andrew Ellis authored
398 add_filter('social_get_broadcast_account', array('Social_Facebook', 'social_get_broadcast_account'), 10, 3);
399 add_filter('social_save_broadcasted_ids_data', array('Social_Facebook', 'social_save_broadcasted_ids_data'), 10, 4);
400 add_filter('social_view_set_file', array('Social_Facebook', 'social_view_set_file'), 10, 2);
0692737 Fixed the meta box for broadcasted and scheduled accounts
Andrew Ellis authored
401 add_filter('social_view_data', array('Social_Facebook', 'social_view_data'), 10, 2);
0470bf0 Rewrote how the personal facebook page data is merged into the univer…
Andrew Ellis authored
402 add_filter('social_merge_accounts', array('Social_Facebook', 'social_merge_accounts'), 10, 2);
748b7ad Continued stubbing of Social.
Andrew Ellis authored
403
80d8b64 Formatting changes
Andrew Ellis authored
404 }
Something went wrong with that request. Please try again.