".__('Settings for the SFC-Publish plugin. The manual Facebook Publishing buttons can be found on the Edit Post or Edit Page screen, after you publish a post. If you can\'t find them, try scrolling down or seeing if you have the box disabled in the Options dropdown.', 'sfc')."
";
}
function sfc_publish_auto_callback() {
$options = get_option('sfc_options');
if (!isset($options['autopublish_app'])) $options['autopublish_app'] = false;
if (!isset($options['autopublish_profile'])) $options['autopublish_profile'] = false;
?>
Grant SFC Permissions
post_status == 'private') {
echo '
'.__('Why would you put private posts on Facebook, for all to see?', 'sfc').'
';
return;
}
if ($post->post_status !== 'publish') {
echo '
'.__('After publishing the post, you can send it to Facebook from here.', 'sfc').'
';
return;
}
// apply the content filters, in case some plugin is doing weird image stuff
$content = apply_filters('the_content', $post->post_content);
// look for the images to add with image_src
$images = sfc_base_find_images($post);
// build the attachment
$permalink = apply_filters('sfc_publish_permalink',wp_get_shortlink($post->ID),$post->ID);
$attachment['name'] = $post->post_title;
$attachment['href'] = $permalink;
$attachment['description'] = sfc_base_make_excerpt($post);
// image attachments (up to 5, as that's all FB allows)
$count=0;
foreach ($images as $image) {
$attachment['media'][$count]['type'] = 'image';
$attachment['media'][$count]['src'] = $image;
$attachment['media'][$count]['href'] = $permalink;
$count++; if ($count==5) break;
}
// Share link
$action_links[0]['text'] = 'Share';
$action_links[0]['href'] = 'http://www.facebook.com/share.php?u='.urlencode($permalink);
$ui['method']='stream.publish';
$ui['attachment']=$attachment;
$ui['action_links']=$action_links;
?>
FB.getLoginStatus(function(response) {
if (response.session) {
sfcShowPubButtons();
} else {
jQuery('#sfc-publish-buttons').html('');
FB.XFBML.parse();
}
});
true), 'objects' ) );
foreach ( $post_types as $post_type ) {
if ( $post->post_type == $post_type->name ) {
sfc_publish_automatic($post->ID, $post);
break;
}
}
}
}
function sfc_publish_automatic($id, $post) {
// check to make sure post is published
if ($post->post_status !== 'publish') return;
// check options to see if we need to send to FB at all
$options = get_option('sfc_options');
if (!$options['autopublish_app'] && !$options['autopublish_profile'])
return;
// build the post to send to FB
// apply the content filters, in case some plugin is doing weird image stuff
$content = apply_filters('the_content', $post->post_content);
// look for the images to add with image_src
$images = sfc_base_find_images($post);
// build the attachment
$permalink = apply_filters('sfc_publish_permalink',wp_get_shortlink($post->ID),$post->ID);
$attachment['name'] = $post->post_title;
$attachment['link'] = $permalink;
$attachment['description'] = sfc_base_make_excerpt($post);
// image attachment
if (!empty($images)) $attachment['picture'] = $images[0];
// Actions
$actions[0]['name'] = 'Share';
$actions[0]['link'] = 'http://www.facebook.com/share.php?u='.urlencode($permalink);
$attachment['actions'] = json_encode($actions);
// publish to app or page
if ($options['autopublish_app'] && !get_post_meta($id,'_fb_post_id_app',true) ) {
if ($options['fanpage']) {
$url = "https://graph.facebook.com/{$options['fanpage']}/feed";
$attachment['access_token'] = $options['page_access_token'];
}
else {
$url = "https://graph.facebook.com/{$options['appid']}/feed";
$attachment['access_token'] = $options['app_access_token'];
}
$data = wp_remote_post($url, array('body'=>http_build_query($attachment)));
if (!is_wp_error($data)) {
$resp = json_decode($data['body'],true);
if ($resp['id']) update_post_meta($id,'_fb_post_id_app',$resp['id']);
}
}
// publish to profile
if ($options['autopublish_profile'] && !get_post_meta($id,'_fb_post_id_profile',true)) {
$url = "https://graph.facebook.com/{$options['user']}/feed";
// check the cookie for an access token. If not found, try to use the stored one.
$cookie = sfc_cookie_parse();
if ($cookie['access_token']) $attachment['access_token'] = $cookie['access_token'];
else $attachment['access_token'] = $options['access_token'];
$data = wp_remote_post($url, array('body'=>http_build_query($attachment)));
if (!is_wp_error($data)) {
$resp = json_decode($data['body'],true);
if ($resp['id']) update_post_meta($id,'_fb_post_id_profile',$resp['id']);
}
}
}
add_filter('sfc_validate_options','sfc_publish_validate_options');
function sfc_publish_validate_options($input) {
$options = get_option('sfc_options');
if (isset($input['autopublish_app']) && $input['autopublish_app'] != 1) $input['autopublish_app'] = 0;
if (isset($input['autopublish_profile']) && $input['autopublish_profile'] != 1) $input['autopublish_profile'] = 0;
unset($input['user']);
unset($input['access_token']);
unset($input['page_access_token']);
unset($input['app_access_token']);
// find the access token and save it if it's there
$cookie = sfc_cookie_parse();
if ($cookie && $cookie['expires'] === '0') {
$input['user'] = $cookie['uid'];
$input['access_token'] = $cookie['access_token'];
// for fan pages, we need to go get their access token
if ($input['fanpage']) {
// connect to FB, find a list of the available Pages
$data = wp_remote_get("https://graph.facebook.com/{$options['user']}/accounts?access_token={$input['access_token']}");
if (!is_wp_error($data)) {
$pages = json_decode($data['body'],true);
if (is_array($pages)) foreach ($pages['data'] as $page) {
if ($page['id'] == $input['fanpage']) {
$input['page_access_token'] = $page['access_token'];
break;
}
}
}
}
// get application access token
$data = wp_remote_get("https://graph.facebook.com/oauth/access_token?client_id={$input['appid']}&client_secret={$input['app_secret']}&type=client_cred");
if (!is_wp_error($data)) {
$token = $data['body'];
if (strpos($token,'access_token=') !== false) {
$input['app_access_token'] = str_replace('access_token=','',$token);
}
}
}
return $input;
}
// fix crazy shortlink nonsense
add_filter('sfc_publish_permalink', 'sfc_publish_shortlink_fix', 10, 2);
function sfc_publish_shortlink_fix($link, $id) {
if (empty($link)) $link = get_permalink($id);
return $link;
}