3. Just below the last input (not the comment text area, just the name/email/url inputs, add this:
That will add the necessary pieces to allow the script to work.
Hopefully, a future version of WordPress will make this simpler.
*/
// if you don't want the plugin to ask for email permission, ever, then define this to true in your wp-config
if ( !defined('SFC_DISABLE_EMAIL_PERMISSION') )
define( 'SFC_DISABLE_EMAIL_PERMISSION', false );
// checks for sfc on activation
function sfc_comm_activation_check(){
if (function_exists('sfc_version')) {
if (version_compare(sfc_version(), '0.1', '>=')) {
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_comm_activation_check');
// force load jQuery (we need it later anyway)
add_action('wp_enqueue_scripts','sfc_comm_jquery');
function sfc_comm_jquery() {
wp_enqueue_script('jquery');
}
// set a variable to know when we are showing comments (no point in adding js to other pages)
add_action('comment_form','sfc_comm_comments_enable');
function sfc_comm_comments_enable() {
global $sfc_comm_comments_form;
$sfc_comm_comments_form = true;
}
// add placeholder for sending comment to Facebook checkbox
add_action('comment_form','sfc_comm_send_place');
function sfc_comm_send_place() {
?>
', ']]>', $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);
}
return $text;
}
// this bit is to allow the user to add the relevant comments login button to the comments form easily
// user need only stick a do_action('alt_comment_login'); wherever he wants the button to display
add_action('alt_comment_login','sfc_comm_login_button');
add_action('comment_form_before_fields', 'sfc_comm_login_button',10,0); // WP 3.0 support
function sfc_comm_login_button() {
echo 'Connect with Facebook
';
}
// this exists so that other plugins can hook into the same place to add their login buttons
if (!function_exists('alt_login_method_div')) {
add_action('alt_comment_login','alt_login_method_div',5,0);
add_action('comment_form_before_fields', 'alt_login_method_div',5,0); // WP 3.0 support
function alt_login_method_div() { echo ''; }
add_action('alt_comment_login','alt_login_method_div_close',20,0);
add_action('comment_form_before_fields', 'alt_login_method_div_close',20,0); // WP 3.0 support
function alt_login_method_div_close() { echo '
'; }
}
// WP 3.0 support
if (!function_exists('comment_user_details_begin')) {
add_action('comment_form_before_fields', 'comment_user_details_begin',1,0);
function comment_user_details_begin() { echo ''; }
}
// generate facebook avatar code for FB user comments
add_filter('get_avatar','sfc_comm_avatar', 10, 5);
function sfc_comm_avatar($avatar, $id_or_email, $size = '96', $default = '', $alt = false) {
// check to be sure this is for a comment
if ( !is_object($id_or_email) || !isset($id_or_email->comment_ID) || $id_or_email->user_id)
return $avatar;
// check for fbuid comment meta
$fbuid = get_comment_meta($id_or_email->comment_ID, 'fbuid', true);
if ($fbuid) {
// return the avatar code
return "";
}
// check for number@facebook.com email address (deprecated, auto-converts to new meta data)
if (preg_match('|(\d+)\@facebook\.com|', $id_or_email->comment_author_email, $m)) {
// save the fbuid as meta data
update_comment_meta($id_or_email->comment_ID, 'fbuid', $m[1]);
// return the avatar code
return "";
}
return $avatar;
}
// store the FB user ID as comment meta data ('fbuid')
add_action('comment_post','sfc_comm_add_meta', 10, 1);
function sfc_comm_add_meta($comment_id) {
$options = get_option('sfc_options');
include_once 'facebook-platform/facebook.php';
$fb=new Facebook($options['api_key'], $options['app_secret']);
$fbuid=$fb->get_loggedin_user();
if ($fbuid) {
update_comment_meta($comment_id, 'fbuid', $fbuid);
}
}
// Add user fields for FB commenters
add_filter('pre_comment_on_post','sfc_comm_fill_in_fields');
function sfc_comm_fill_in_fields($comment_post_ID) {
if (is_user_logged_in()) return; // do nothing to WP users
$options = get_option('sfc_options');
include_once 'facebook-platform/facebook.php';
$fb=new Facebook($options['api_key'], $options['app_secret']);
$fbuid=$fb->get_loggedin_user();
// this is a facebook user, override the sent values with FB info
if ($fbuid) {
$user_details = $fb->api_client->users_getInfo($fbuid, 'name, profile_url');
if (is_array($user_details)) {
$_POST['author'] = $user_details[0]['name'];
$_POST['url'] = $user_details[0]['profile_url'];
}
$query = "SELECT email FROM user WHERE uid=\"{$fbuid}\"";
$email = $fb->api_client->fql_query($query);
if (is_array($email)) {
$email = $email[0]['email'];
$_POST['email'] = $email;
}
}
}