This plugin allows you to embed your blog into the Facebook canvas, allows
Facebook users to comment on or share your blog posts, cross-posts your
blog posts to the wall of your profile, a fan page, an application profile page,
or a group page, and enables you to add a tab for your fan page. It also imports
comments made against wall posts which originated in WordPress.
If you are having issues, please begin by Checking '
. 'permissions for stream publishing, reading, and offline access.'; }
?>
Required Settings +
To use this plugin, you must create
a Facebook application. After you've obtained the necessary info, fill in
both your application's App_ID and Secret keys as well as your application's
url and your Facebook profile ID.
Note: Your "Canvas Callback URL" setting in Facebook should be:
' . get_bloginfo('url') . '
';
echo '
Stream/Wall Options +
Once your Facebook application is established by filling out the required information, return to edit streaming options.';
}
else {
echo '
These settings all impact how WPBook publishes to Facebook walls, and depend on appropriate permissions being set in Facebook.
';
echo '
If you are having issues, please begin by Checking '
. 'permissions for stream publishing, reading, and offline access.
Group ID: ';
echo ' (Generally your GroupID should be in your url, like: http://www.facebook.com/group.php?gid=149948248362737 - the gid is the group ID).
';
echo '
Use external permalinks on Walls (applies to profiles, pages, and groups)
';
/* using the new Graph API, there's nowhere to put attribution strings
* So I'm just hiding this for now
*/
//echo '
Attribution line: ';
//echo 'Predefined Strings (you can use these in your attribution line): %author%, %blogname%.
';
echo '
Stream Debug Options Enable WPBook to create a debug file';
echo ' Show errors posting to Facebook Stream
Use WPBook Gravatar Globally (this will overwrite WordPress Gravatar settings and also show Facebook avatars outside of Facebook)';
echo'
';
echo '
Socialize Options:
';
// Here starts the "invite friends" section
echo '
Show Invite Friends Link
';
// removed enabled add-to-profile option as Facebook no longer supports
// this for normal users. You can still add a tab to Fan Pages, but you
// don't need the button for that
// show share option
echo '
Enable "Share This Post" (within Facebook)
';
// show external link option
echo '
Enable "view post at external site" link
';
//links button position for external and share button
//see if share button or external link is enabled first
echo '
Link(s) position for share button and external link button: ';
//top
echo 'Top ';
echo ' Bottom
';
echo'
Page Options:
';
//start show pages option
echo '
Enable pages
';
echo '
';
//show top menu of parent pages
echo'
Display menu of parent pages at top of application
Custom Header and Footer This is where you can'
.' set custom headers and footers for your post. For example if you wanted'
.' to show the post author at the bottom of each post here is where you would set that option.';
//enable custom footer/header
echo '
Sorry, you are not allowed to access ';
echo 'this page.
';
}
}
if (!function_exists('wp_recent_posts')) {
// this is based almost entirely on: Recent Posts
// http://mtdewvirus.com/code/wordpress-plugins/ v. 1.07
// by Nick Momrik, http://mtdewvirus.com/
function wp_recent_posts($count = 5, $before = '
', $after = '
',
$hide_pass_post = true, $skip_posts = 0, $show_excerpts = false,
$where = '', $join = '', $groupby = '') {
global $wpdb;
$time_difference = get_settings('gmt_offset');
$now = gmdate("Y-m-d H:i:s",time());
$join = apply_filters('posts_join', $join);
$where = apply_filters('posts_where', $where);
$groupby = apply_filters('posts_groupby', $groupby);
if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; }
$request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts "
. "$join WHERE post_status = 'publish' AND post_type = 'post' ";
if ($hide_pass_post) $request .= "AND post_password ='' ";
$request .= "AND post_date_gmt < '$now' $where $groupby ORDER BY "
. "post_date DESC LIMIT $skip_posts, $count";
$posts = $wpdb->get_results($request);
$output = '';
if ($posts) {
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$permalink = get_permalink($post->ID);
$output .= $before . ''
. $post_title . '';
if($show_excerpts) {
$post_excerpt = stripslashes($post->post_excerpt);
$output.= ' ' . $post_excerpt;
}
$output .= $after;
}
} else {
$output .= $before . "None found" . $after;
}
echo $output;
}
}
// todo: eliminate this function (not needed anymore?)
// this is a copy of the wp_recent_posts function
// necessary because we don't want to echo output (for profile)
function wpbook_profile_recent_posts($count = 5, $before = '
', $after = '
',
$hide_pass_post = true, $skip_posts = 0, $show_excerpts = false,
$where = '', $join = '', $groupby = '') {
global $wpdb;
$my_options = wpbook_getAdminOptions();
$time_difference = get_settings('gmt_offset');
$now = gmdate("Y-m-d H:i:s",time());
$join = apply_filters('posts_join', $join);
$where = apply_filters('posts_where', $where);
$groupby = apply_filters('posts_groupby', $groupby);
if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; }
$request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts "
. "$join WHERE post_status = 'publish' AND post_type = 'post' ";
if ($hide_pass_post) $request .= "AND post_password ='' ";
$request .= "AND post_date_gmt < '$now' $where $groupby ORDER BY "
. "post_date DESC LIMIT $skip_posts, $count";
$posts = $wpdb->get_results($request);
$output = '';
if ($posts) {
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
if($my_options['promote_external']) {
if(check_facebook()) {
$permalink = get_external_post_url(get_permalink($post->ID)); // external permalink
} else {
$permalink = get_permalink($post->ID);
}
} else {
$permalink = get_permalink($post->ID); // permalink is un-filtered
$my_offset = strlen(get_option('home'));
$app_url = $my_options['fb_app_url'];
$my_link = 'http://apps.facebook.com/' . $app_url
. substr($permalink,$my_offset);
$permalink = $my_link;
}
$output .= $before . ''
. $post_title . '';
if($show_excerpts) {
$post_excerpt = stripslashes($post->post_excerpt);
$output.= ' ' . $post_excerpt;
}
$output .= $after;
}
} else {
$output .= $before . "None found" . $after;
}
return $output;
}
// this checks to see if we are in facebook
function check_facebook() {
if (!isset($_SERVER["HTTP_USER_AGENT"])) {
return false;
}
if (isset($_REQUEST['fb_sig_in_iframe']) || isset($_REQUEST['fb_force_mode'])) {
defined('DONOTCACHEPAGE') or define('DONOTCACHEPAGE', 'true');
return true;
}
/* need to check for signed_request to trap new iframes */
if (isset($_POST['signed_request']) || isset($_REQUEST['signed_request'])) {
defined('DONOTCACHEPAGE') or define('DONOTCACHEPAGE', 'true');
return true;
}
return false;
}
function wpbook_theme_root($path) {
$theme_root = dirname(__FILE__);
if (check_facebook()) {
if(wpbook_check_custom_theme('WPBook')) {
$path = WP_CONTENT_DIR . '/themes/wpbook_theme';
return $path;
}
else{
return $theme_root . '/theme';
}
} else {
return $path;
}
}
function wpbook_theme_root_uri($url) {
if (check_facebook()){
if(wpbook_check_custom_theme('WPBook')) {
//apparently get_theme_dir() returns a 500 error
$dir = WP_CONTENT_URL . '/themes/wpbook_theme';
return $dir;
}
else{
$dir = WP_PLUGIN_ULR . '/wpbook/theme';
return $dir;
}
} else {
return $url;
}
}
function wpbook_stylesheet($url) {
if (check_facebook()){
if(wpbook_check_custom_theme('WPBook')) {
return 'default';
}
} else {
return $url;
}
}
// this function seems to be required by WP 2.6
function wpbook_template_directory($value) {
if (check_facebook()) {
if(wpbook_check_custom_theme('WPBook')){
$theme_root = WP_CONTENT_DIR . '/themes/wpbook_theme';
return $theme_root;
}
else{
$theme_root = WP_PLUGIN_DIR;
return $theme_root . '/wpbook/theme';
}
}
else {
return $value;
}
}
//check to see if the user has a custom theme in their theme folder
function wpbook_check_custom_theme($theme) {
global $wp_version;
if (version_compare($wp_version, '3.1', '>=')) { // get_themes() crashes WP < 3.1
$installed_themes= array();
$installed_themes= get_themes();
$wpbook_theme_check = (!empty($installed_themes[$theme])) ? TRUE : FALSE;
return $wpbook_theme_check;
} else { // this check works before 3.1
if(file_exists(WP_CONTENT_DIR . '/themes/wpbook_theme/style.css')){
return TRUE;
}
}
return FALSE; // no custom theme found
}
// this is the function which adds to the template and stylesheet hooks
// the call to wpbook_template
if (check_facebook()) {
add_filter('template_directory', 'wpbook_template_directory');
add_filter('theme_root', 'wpbook_theme_root');
add_filter('theme_root_uri', 'wpbook_theme_root_uri');
add_filter('stylesheet', 'wpbook_stylesheet');
}
// also have to change permalinks, next/prev links , page links, and archive links
function fb_filter_postlink($postlink) {
if (check_facebook()) {
$my_offset = strlen(get_option('home'));
$my_options = wpbook_getAdminOptions();
$app_url = $my_options['fb_app_url'];
$my_link = 'http://apps.facebook.com/' . $app_url
. substr($postlink,$my_offset);
return $my_link;
} else {
return $postlink;
}
}
function fb_filter_postlink_no_qs($postlink) {
if (check_facebook()) {
$my_offset = strlen(get_option('home'));
$my_options = wpbook_getAdminOptions();
$app_url = $my_options['fb_app_url'];
$my_link = 'http://apps.facebook.com/' . $app_url . substr($postlink,$my_offset);
$my_new_link_pieces = parse_url($my_link);
return parse_url($my_link,PHP_URL_SCHEME) .'://'. parse_url($my_link,PHP_URL_HOST)
. parse_url($my_link,PHP_URL_PATH); // ignoring port
} else {
return $postlink;
}
}
// this version to be called when we're outside facebook too
function wpbook_always_filter_postlink($postlink) {
$my_offset = strlen(get_option('home'));
$my_options = wpbook_getAdminOptions();
$app_url = $my_options['fb_app_url'];
$my_link = 'http://apps.facebook.com/' . $app_url
. substr($postlink,$my_offset);
return $my_link;
}
/*
* This function handles streaming publish to Facebook.
* It includes publish_to_facebook.php
*/
function wpbook_publish_to_facebook($post_ID) {
if (!version_compare(PHP_VERSION, '5.0.0', '<')) {
include(WP_PLUGIN_DIR .'/wpbook/includes/publish_to_facebook.php');
} else {
wp_die("Sorry, but you can't run this plugin, it requires PHP 5 or higher.");
}
wpbook_safe_publish_to_facebook($post_ID);
} // end of function wpbook_publish_to_facebook
function get_external_post_url($my_permalink){
$my_options = wpbook_getAdminOptions();
$app_url = $my_options['fb_app_url'];
// code to get the url of the orginal post for use in the "show external url view"
$permalink_pieces = parse_url($my_permalink);
//get the app_url and the preceeding slash
$permalink_app_url = "/". $app_url;
//remove /appname
$external_post_permalink = str_replace_once($permalink_app_url,"",$permalink_pieces[path]);
//re-write the post url using the site url
$external_site_url_pieces = parse_url(get_bloginfo('wpurl'));
//break apart the external site address and get just the "site.com" part
$external_site_url = $external_site_url_pieces[host];
$external_post_url = get_bloginfo('siteurl'). $external_post_permalink;
if(!empty($permalink_pieces[query])) {
$external_post_url = $external_post_url .'?'. $permalink_pieces[query];
}
//return "app url is " . $app_url;
return $external_post_url;
}
// check to see if external post link contains the app name, and if it does,
// only replace the first instance
function str_replace_once($needle, $replace, $haystack) {
// Looks for the first occurence of $needle in $haystack
// and replaces it with $replace.
$pos = strpos($haystack, $needle);
if ($pos === false) {
// Nothing found
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
// attribution line
function wpbook_attribution_line($attribution_line,$author){
if($author == '') {
$author = get_the_author();
}
$attribution_line = str_replace('%author%',$author,$attribution_line);
$attribution_line = str_replace('%blogname%',html_entity_decode(get_bloginfo('name'),ENT_QUOTES),$attribution_line);
return $attribution_line;
}
/*
* Use postmeta to enable users to turn off streaming on case-by-case basis
* Based on how Alex King's Twitter Tools handles the same case for pushing
* posts to twitter
*/
function wpbook_meta_box() {
global $post;
$wpbook_publish = get_post_meta($post->ID, 'wpbook_fb_publish', true);
if ($wpbook_publish == '') {
$wpbook_publish = 'yes';
}
echo '
'.__('Publish this post to Facebook Wall?', 'wpbook').' ';
echo ' ';
echo ' ';
echo '
';
do_action('wpbook_post_options');
}
function wpbook_add_meta_box() {
global $wp_version;
if (version_compare($wp_version, '2.7', '>=')) {
add_meta_box('wpbook_post_form','WPBook', 'wpbook_meta_box', 'post', 'side');
} else {
add_meta_box('wpbook_post_form','WPBook', 'wpbook_meta_box', 'post', 'normal');
}
}
function wpbook_store_post_options($post_id, $post = false) {
if (!$post || $post->post_type == 'revision') { // store the metadata with the post, not the revision
return;
}
$wpbookAdminOptions = wpbook_getAdminOptions();
$post = get_post($post_id);
$stored_meta = get_post_meta($post_id, 'wpbook_fb_publish', true);
$posted_meta = $_POST['wpbook_fb_publish'];
$save = false;
/* if there is $posted_meta, that takes priority over stored */
if (!empty($posted_meta)) {
$posted_meta == 'yes' ? $meta = 'yes' : $meta = 'no';
$save = true;
}
/* if no posted meta, check stored meta */
else if (empty($stored_meta)) {
/* if no stored meta, but streaming publishing is on, default to yes */
if (($wpbookAdminOptions['stream_publish']) || ($wpbookAdminOptions['stream_publish_pages'])) {
$meta = 'yes';
} else {
$meta = 'no';
}
$save = true;
/* if there is stored meta, and user didn't touch it, don't save */
} else {
$save = false;
}
if ($save) {
if (!update_post_meta($post_id, 'wpbook_fb_publish', $meta)) {
add_post_meta($post_id, 'wpbook_fb_publish', $meta);
}
}
}
add_action('draft_post', 'wpbook_store_post_options', 1, 2);
add_action('publish_post', 'wpbook_store_post_options', 1, 2);
add_action('save_post', 'wpbook_store_post_options', 1, 2);
// based on sample code here:
// http://willnorris.com/2009/06/wordpress-plugin-pet-peeve-2-direct-calls-to-plugin-files
// thanks will
function wpbook_parse_request($wp) {
if (array_key_exists('wpbook', $wp->query_vars)){
if($wp->query_vars['wpbook'] == 'comment-handler') { // first process requests with "wpbook=comment-handler"
// process the request - in our case this is a comment being posted
nocache_headers();
$comment_post_ID = (int) $_POST['comment_post_ID'];
global $wpdb;
$status = $wpdb->get_row("SELECT post_status, comment_status FROM "
. "$wpdb->posts WHERE ID = '$comment_post_ID'");
if ( empty($status->comment_status) ) {
do_action('comment_id_not_found', $comment_post_ID);
exit;
} elseif ( !comments_open($comment_post_ID) ) {
do_action('comment_closed', $comment_post_ID);
wp_die( __('Sorry, comments are closed for this item.') );
} elseif ( in_array($status->post_status, array('draft', 'pending') ) ) {
do_action('comment_on_draft', $comment_post_ID);
exit;
}
$wpbookOptions = get_option('wpbookAdminOptions');
if (!empty($wpbookOptions)) {
foreach ($wpbookOptions as $key => $option)
$wpbookAdminOptions[$key] = $option;
}
$comment_author = trim(strip_tags($_POST['author']));
$comment_author_email = trim($_POST['email']);
$comment_author_url = trim($_POST['url']);
$comment_content = trim($_POST['comment']);
$comment_type = '';
$wpbook_require_email = $wpbookOptions['require_email'];
// need to account here for wpadminOptions version of email required
if(($wpbook_require_email == "true") && ('' == $comment_author_email)){
echo '
Sorry: comments require an email address
';
wp_die( __('Error: please enter an e-mail.'));
}
if($comment_author_email != ''){
if(!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|'
. 'info|name|aero|biz|info|jobs|museum|name|edu)$/i',
$comment_author_email)) {
wp_die( __('Error: please enter a valid e-mail.'));
}
}
if ( '' == $comment_content )
wp_die( __('Error: please type a comment.') );
$commentdata = compact('comment_post_ID', 'comment_author',
'comment_author_email', 'comment_author_url',
'comment_content', 'comment_type', 'user_ID');
$comment_id = wp_new_comment( $commentdata );
$comment = get_comment($comment_id);
if ( !$user->ID ) {
setcookie('comment_author_' . COOKIEHASH,
$comment->comment_author, time() + 30000000,
COOKIEPATH, COOKIE_DOMAIN);
setcookie('comment_author_email_' . COOKIEHASH,
$comment->comment_author_email, time() + 30000000,
COOKIEPATH, COOKIE_DOMAIN);
setcookie('comment_author_url_' . COOKIEHASH,
clean_url($comment->comment_author_url),
time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
}
// all done parsing, redirect to post, on comment anchor
$redirect_url = get_permalink($comment_post_ID);
$redirect_url .= '#comment-' . $comment_id;
// switched to raw php header redirect as $facebook->redirect was
// problematic and no fb session needed in this page
header( 'Location: ' . $redirect_url );
}
}
}
function wpbook_query_vars($vars) {
$vars[] = 'wpbook';
return $vars;
}
//show facebook avatar as gravatar
function wpbook_get_facebook_avatar($avatar, $comment, $size="50") {
$wpbookOptions = get_option('wpbookAdminOptions');
if (!empty($wpbookOptions)) {
foreach ($wpbookOptions as $key => $option)
$wpbookAdminOptions[$key] = $option;
}
if($wpbookAdminOptions['use_gravatar'] =="true"){
$author_url = get_comment_author_url();
$email = get_comment_author_email();
$default = $wpbookAdminOptions['gravatar_default'];
$rating=$wpbookAdminOptions['gravatar_rating'];
$size="50";
if(preg_match("@^(?:http://)?(?:www\.)?facebook@i",trim($author_url))){
$parse_author_url = (parse_url($author_url));
$parse_author_url_q = $parse_author_url['query'];
if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){
$fb_id = "/".$match[1];
} else {
/* have to account here for multiple options
* http://facebook.com/profile.php?id=12234
* http://www.facebook.com/pages/PageName/1234
* Application pages, groups?
*/
if(strrpos($parse_author_url['path'],'pages')) {
$fb_id_array = explode('/',$parse_author_url['path']);
$size = count($fb_id_array);
$fb_id = "/" . $fb_id_array[$size-1];
} else {
$fb_id = $parse_author_url['path'];
}
}
$grav_url= "http://graph.facebook.com".$fb_id."/picture?type=square";
}
else{
$grav_url = "http://www.gravatar.com/avatar/" .
md5(strtolower($email))."?d=" . $default."&s=".$size."&r=".$rating;
}
$grav_img = "";
return $grav_img; }
return $avatar;
}
// show facebook avatar as gravatar even outside facebook
function wpbook_get_global_facebook_avatar($avatar, $comment, $size="50") {
$wpbookOptions = get_option('wpbookAdminOptions');
if (!empty($wpbookOptions)) {
foreach ($wpbookOptions as $key => $option)
$wpbookAdminOptions[$key] = $option;
}
if(($wpbookAdminOptions['use_gravatar'] =="true") && ($wpbookAdminOptions['wpbook_use_global_gravatar'] =="true")){
$author_url = get_comment_author_url();
$email = get_comment_author_email();
$default = $wpbookAdminOptions['gravatar_default'];
$rating=$wpbookAdminOptions['gravatar_rating'];
$size="50";
$parse_author_url = (parse_url($author_url));
if(preg_match("@^(?:http://)?(?:www\.)?facebook@i",trim($author_url))){
$parse_author_url_q = $parse_author_url['query'];
if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){
$fb_id = "/".$match[1];
}
else {
/* have to account here for multiple options
* http://facebook.com/profile.php?id=12234
* http://www.facebook.com/pages/PageName/1234
* Application pages, groups?
*/
if(strrpos($parse_author_url['path'],'pages')) {
$fb_id_array = explode('/',$parse_author_url['path']);
$size = count($fb_id_array) -1;
$fb_id = "/" . $fb_id_array[$size];
} else {
$fb_id = $parse_author_url['path'];
}
}
$grav_url= "http://graph.facebook.com".$fb_id."/picture?type=square";
}
else {
$grav_url = "http://www.gravatar.com/avatar/" . md5(strtolower($email)).
"?d=" . $default."&s=".$size."&r=".$rating;
}
$grav_img = "";
return $grav_img;
}
return $avatar;
}
/**
* Thanks Otto - http://lists.automattic.com/pipermail/wp-hackers/2009-July/026759.html
*/
function wpbook_activation_check(){
global $wp_version;
if (version_compare(PHP_VERSION, '5.0.0', '<')) {
deactivate_plugins(basename(__FILE__)); // Deactivate ourself
wp_die("Sorry, but you can't run this plugin, it requires PHP 5 or higher.");
}
if (version_compare($wp_version, '2.9', '<')) {
wp_die("This plugin requires WordPress 2.6 or greater.");
}
}
add_filter('query_vars', 'wpbook_query_vars');
add_filter('post_link','fb_filter_postlink',1,1);
add_filter('page_link','fb_filter_postlink',1,1);
add_filter('get_pagenum_link','fb_filter_postlink_no_qs',1,1);
//add gravatar/facebook avatar support outside facebook
//filter for inside facebook is in the theme/config_wp_settings.php
add_filter('get_avatar','wpbook_get_global_facebook_avatar', 1, 3 );
/* you can't actually filter the tag_link and category_links this way
* because if you do, wordpress redirects to /app_url/tag/yourtag whenever
* you try to access /tag/yourtag inside FB
* add_filter('tag_link','fb_filter_postlink',1,1);
* add_filter('category_link','fb_filter_postlink',1,1);
*/
add_action('admin_menu', 'wpbook_options_page');
add_action('wp', 'wpbook_parse_request');
add_action('admin_menu', 'wpbook_add_meta_box');
// these capture new posts, not edits of previous posts
add_action('future_to_publish','wpbook_publish_to_facebook');
add_action('new_to_publish','wpbook_publish_to_facebook');
add_action('draft_to_publish','wpbook_publish_to_facebook');
add_action('pending_to_publish','wpbook_publish_to_facebook');
// cron job task
add_action('wpbook_cron_job', 'wpbook_import_comments');
register_activation_hook(__FILE__, 'wpbook_activate');
register_deactivation_hook(__FILE__, 'wpbook_deactivate');
?>