forked from grange/wordpress-simple-facebook-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfc-photos.php
167 lines (125 loc) · 5.49 KB
/
sfc-photos.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
// photos plugin
add_filter('media_upload_tabs','sfc_photos_upload_tab');
function sfc_photos_upload_tab($tabs) {
$tabs['sfcphotos'] = 'Facebook Images';
return $tabs;
}
add_action('media_upload_sfcphotos', 'sfc_photos_tab');
function sfc_photos_tab() {
$errors = array();
return wp_iframe( 'media_sfc_photos_form', $errors );
}
function media_sfc_photos_form($errors) {
global $redir_tab, $type;
$redir_tab = 'sfcphotos';
media_upload_header();
$post_id = intval($_REQUEST['post_id']);
$user = sfc_cookie_parse();
if (!isset($user['user_id'])) {
?><p><?php _e("You don't appear to be logged into Facebook. Click the button below to login and grant photo access.",'sfc'); ?></p>
<fb:login-button v="2" scope="offline_access,user_photos" onlogin="location.reload();"><fb:intl><?php _e('Connect with Facebook', 'sfc'); ?></fb:intl></fb:login-button><?php
}
if ( isset($_GET['send']) && !preg_match('/^[0-9]+$/i', $_GET['send'])) {
// photo ids are bigints
unset($_GET['send']);
}
if ( isset($_GET['send']) ){
$send_id = $_GET['send'];
$photo = sfc_photo_get_photo($send_id, $user['access_token']);
$photo = apply_filters('sfc_photo_insert',$photo);
list( $width, $height ) = image_constrain_size_for_editor($photo['width'], $photo['height'], 'large');
$alt = '';
if (!empty($photo['name'])) {
$alt = esc_attr($photo['name']);
}
$html = "<a href='{$photo['link']}'><img src='{$photo['source']}' alt='{$alt}' width='{$width}' height='{$height}' class='size-full fb-image-{$photo['id']}'/></a>";
if (!empty($photo['name'])) {
$html = "[caption id='fb_attachment_{$photo['id']}' width='{$width}' caption='{$alt}']" . $html . '[/caption]';
}
return media_send_to_editor($html);
}
if (!empty($_GET['album']) && !preg_match('/^[0-9]+$/i', $_GET['album'])) {
// album ids are bigints
unset($_GET['album']);
}
if (!empty($_GET['album'])) {
// show an album
$album = $_GET['album'];
if ( false === ( $photos = get_transient('sfcphotos-'.$album) ) ) {
$photos = sfc_remote($album, 'photos', array('access_token'=>$user['access_token'], 'timeout' => 60));
if ($photos === false) {
?><p><?php _e('Facebook is being really, really slow and not responding to requests in a reasonable period of time. Try again later.','sfc'); ?></p><?php
return;
}
// cache the data because Facebook's Graph API is slow as hell
if (!empty($photos)) set_transient('sfcphotos-'.$album, $photos, 6*60*60); // 6 hours
}
if ( empty($photos['data']) ) {
?><p><?php _e('This album appears to be empty','sfc'); ?></p><?php
$link = admin_url("media-upload.php?post_id=$post_id&type=$type&tab=$redir_tab");
echo "<p><a href='$link'>".__('Go Back','sfc')."</a></p>";
return;
}
$photos = $photos['data'];
$link = admin_url("media-upload.php?post_id=$post_id&type=$type&tab=$redir_tab");
echo "<p><a href='$link'>".__('Go Back','sfc')."</a></p>";
echo '<table><tr>';
$i=1;
foreach ($photos as $photo) {
echo '<td>';
$link = admin_url("media-upload.php?post_id=$post_id&type=$type&tab=$redir_tab&album={$album['id']}&send={$photo['id']}");
echo "<p><a href='$link'><img src='{$photo['images'][1]['source']}' /></a></p>";
echo '</td>';
if ($i%3 == 0) echo '</tr><tr>';
$i++;
}
echo '</tr></table>';
} else {
if ( false === ( $albums = get_transient('sfcphotos-'.$user['user_id']) ) ) {
$albums = sfc_remote($user['uid'], 'albums', array('access_token'=>$user['access_token'], 'timeout' => 60));
if ($albums === false) {
?><p><?php _e('Facebook is being really, really slow and not responding to requests in a reasonable period of time. Try again later.','sfc'); ?></p><?php
return;
}
// cache the data because Facebook's Graph API is slow as hell
if (!empty($albums['data'])) set_transient('sfcphotos-'.$user['user_id'], $albums, 6*60*60); // 6 hours
}
if ( empty($albums['data']) ) {
?><p><?php _e('Either you have no photo albums on Facebook, or you have not granted the site permission to access them. Either way, click the button below to login and grant access.','sfc'); ?></p>
<fb:login-button v="2" scope="offline_access,user_photos" onlogin="location.reload();"><fb:intl><?php _e('Connect with Facebook', 'sfc'); ?></fb:intl></fb:login-button><?php
}
$albums = $albums['data'];
echo '<table><tr>';
$i=1;
foreach ($albums as $album) {
echo '<td>';
$link = admin_url("media-upload.php?post_id=$post_id&type=$type&tab=$redir_tab&album={$album['id']}");
// retrieve the cover image for the album
if (false !== ($photo = sfc_photo_get_photo($album['cover_photo'], $user['access_token']) ) ) {
echo "<p><a href='$link'><img src='{$photo['images'][1]['source']}' /></a></p>";
} else {
// TODO cover not available
}
echo "<p><a href='$link'>{$album['name']}</a></p>";
echo '</td>';
if ($i%3 == 0) echo '</tr><tr>';
$i++;
}
echo '</tr></table>';
}
?>
<?php
}
// generic function to get a photo's info from FB and store it in a transient cache (speed reasons)
function sfc_photo_get_photo($fbid, $access_token) {
if (false === ($photo = get_transient('sfcphoto-'.$fbid) ) ) {
$photo = sfc_remote( $fbid, '', array('access_token'=>$access_token) );
if (!empty($photo['images'])) {
set_transient('sfcphoto-'.$fbid, $photo, 60*60);
} else {
$photo = false;
}
}
return $photo;
}