=')) { return; } } deactivate_plugins(basename(__FILE__)); // Deactivate ourself wp_die("The base SFC plugin must be activated before this plugin will run."); } register_activation_hook(__FILE__, 'sfc_publish_activation_check'); // add the meta boxes add_action('admin_menu', 'sfc_publish_meta_box_add'); function sfc_publish_meta_box_add() { add_meta_box('sfc-publish-div', 'Facebook Publisher', 'sfc_publish_meta_box', 'post', 'side'); add_meta_box('sfc-publish-div', 'Facebook Publisher', 'sfc_publish_meta_box', 'page', 'side'); } // add the admin sections to the sfc page add_action('admin_init', 'sfc_publish_admin_init'); function sfc_publish_admin_init() { add_settings_section('sfc_publish', 'Publish Settings', 'sfc_publish_section_callback', 'sfc'); add_settings_field('sfc_publish_flags', 'Automatic Publishing', 'sfc_publish_auto_callback', 'sfc', 'sfc_publish'); add_settings_field('sfc_publish_extended_permissions', 'Extended Permissions', 'sfc_publish_extended_callback', 'sfc', 'sfc_publish'); wp_enqueue_script('jquery'); } function sfc_publish_section_callback() { echo "
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.
"; } function sfc_publish_auto_callback() { $options = get_option('sfc_options'); if (!$options['autopublish_app']) $options['autopublish_app'] = false; if (!$options['autopublish_profile']) $options['autopublish_profile'] = false; ?>Facebook bug.)'; ?>
In order for the SFC-Publish plugin to be able to publish your posts automatically, you must grant some "Extended Permissions" to the plugin.
User ID and Session Key found! Automatic publishing is ready to go!
Be sure to click the "Save Settings" button on this page after granting these permissions! This will allow SFC to save your user id and session key, for usage by the plugin when publishing posts to your profile and/or page.
', ']]>', $text); $text = wp_strip_all_tags($text); $text = str_replace(array("\r\n","\r","\n"),' ',$text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', '[...]'); $words = explode(' ', $text, $excerpt_length + 1); if (count($words) > $excerpt_length) { array_pop($words); array_push($words, $excerpt_more); $text = implode(' ', $words); } $text = html_entity_decode($text); // added to try to fix annoying &entity; stuff return $text; } function sfc_publish_meta_box( $post ) { $options = get_option('sfc_options'); if ($post->post_status == 'private') { echo 'Why would you put private posts on Facebook, for all to see?
'; return; } if ($post->post_status !== 'publish') { echo 'After publishing the post, you can send it to Facebook from here.
'; 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 = array(); // get the post thumbnail, put it first in the image list if (current_theme_supports('post-thumbnails')) { if ( has_post_thumbnail($post->ID) ) { $thumbid = get_post_thumbnail_id($post->ID); $att = wp_get_attachment_image_src($thumbid, 'full'); if (!empty($att[0])) $images[] = $att[0]; } } // look for any images in the content if ( preg_match_all('//', $content, $matches) ) { foreach ($matches[1] as $match) { foreach ( wp_kses_hair($match, array('http')) as $attr) $img[$attr['name']] = $attr['value']; if ( isset($img['src']) ) { if (!isset($img['class']) || (isset($img['class']) && false === strpos($img['class'], 'wp-smiley')) ) { // ignore smilies $images[] = $img['src']; } } } } // build the attachment $permalink = get_permalink($post->ID); $attachment['name'] = $post->post_title; $attachment['href'] = $permalink; $attachment['description'] = sfc_publish_make_excerpt($post->post_content); $attachment['comments_xid'] = urlencode($permalink); // 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); ?> ID, $post); } } 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; // load facebook platform include_once 'facebook-platform/facebook.php'; $fb=new Facebook($options['api_key'], $options['app_secret']); // to do this autopublish, we might need to switch users if ($options['user'] && $options['session_key']) { $tempuser = $fb->user; $tempkey = $fb->api_client->session_key = $session_key; $fb->set_user($options['user'], $options['session_key']); } else { return; // safety net: if we don't have a user and session key, we can't publish properly. } // 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 = array(); // get the post thumbnail, put it first in the image list if (current_theme_supports('post-thumbnails')) { if ( has_post_thumbnail($post->ID) ) { $thumbid = get_post_thumbnail_id($post->ID); $att = wp_get_attachment_image_src($thumbid, 'full'); if (!empty($att[0])) $images[] = $att[0]; } } // look for any images in the content if ( preg_match_all('//', $content, $matches) ) { foreach ($matches[1] as $match) { foreach ( wp_kses_hair($match, array('http')) as $attr) $img[$attr['name']] = $attr['value']; if ( isset($img['src']) ) { if ( isset( $img['class'] ) && false === strpos( $img['class'], 'wp-smiley' ) ) { // ignore smilies $images[] = $img['src']; } } } } // build the attachment $permalink = get_permalink($post->ID); $attachment['name'] = $post->post_title; $attachment['href'] = $permalink; $attachment['description'] = sfc_publish_make_excerpt($post->post_content); $attachment['comments_xid'] = urlencode($permalink); // 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); // publish to page if ($options['autopublish_app'] && !get_post_meta($id,'_fb_post_id_app',true) && $options['fanpage'] // TODO eliminate this when Facebook fixes the bug ) { if ($options['fanpage']) $who = $options['fanpage']; else $who = $options['appid']; // check to see if we can send to FB at all $result = $fb->api_client->users_hasAppPermission('publish_stream', $who); if (!$result) break; $fb_post_id = $fb->api_client->stream_publish(null, json_encode($attachment), json_encode($action_links), null, $who); if ($fb_post_id) { // update the post id so as to prevent automatically posting it twice update_post_meta($id,'_fb_post_id_app',$fb_post_id); } } // publish to profile if ($options['autopublish_profile'] && !get_post_meta($id,'_fb_post_id_profile',true)) { // check to see if we can send to FB at all $result = $fb->api_client->users_hasAppPermission('publish_stream'); if (!$result) break; $fb_post_prof_id = $fb->api_client->stream_publish(null, json_encode($attachment), json_encode($action_links)); if ($fb_post_prof_id) { // update the post id so as to prevent automatically posting it twice update_post_meta($id,'_fb_post_id_profile',$fb_post_prof_id); } } // switch users back, just in case if ($tempuser) { $fb->set_user($tempuser, $tempkey); } } add_filter('sfc_validate_options','sfc_publish_validate_options'); function sfc_publish_validate_options($input) { $options = get_option('sfc_options'); if ($input['autopublish_app'] != 1) $input['autopublish_app'] = 0; if ($input['autopublish_profile'] != 1) $input['autopublish_profile'] = 0; // find the infinite session key and save it if it's there if ($_COOKIE[$options['api_key'].'_expires'] == 0) { // save the user and session key $input['user'] = $_COOKIE[$options['api_key'].'_user']; $input['session_key'] = $_COOKIE[$options['api_key'].'_session_key']; } return $input; }