Facebook.php 5.89 KB
<?php
/**
 * http://wiki.gotenzing.com/groups/tenzing/wiki/aa35a/FB_Connect_in_WP.html
 *
 * This needs to go in the <html tag
 * xmlns:fb="http://www.facebook.com/2008/fbml"
 * If you want to use FBML AND pass W3C validation
 *
 * Graph API Reference:
 * http://developers.facebook.com/docs/reference/api/user
 */

namespace Tz\WordPress\Tools\Auth\Facebook;

use Tz\WordPress\Tools;
use Tz\WordPress\Tools\Auth;

use FB;

use Exception, InvalidArgumentException;

const VERSION = 0.2;

const OPTION_NAME = 'tz_auth_fb';

    call_user_func(function() {
        Vars::$options = new Tools\WP_Option(OPTION_NAME, Array('button_title' => 'Login', 'ext_perms' => Array('email' => 1)));

        Tools\add_actions(__NAMESPACE__ . '\Actions');
        Tools\add_shortcodes(__NAMESPACE__ . '\ShortCodes');

        if (is_admin()) {
            require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
        }
    });

/**
 * Generates markup for a Facebook Login button
 * @param {Boolean} $echo TRUE to echo the button, false to return a string
 * @returns NULL|String
 */
function drawLoginButton($echo = true) {
    $title = Vars::$options['button_title'] ?: 'Login';
    $btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . $title . '</span></a>';
    // $btn = '<fb:login-button></fb:login-button>';

    if (!$echo) {
        return $btn;
    }

    echo $btn;
}

// This might need some work...what happens if the object fails?
function getSDK() {
    static $instance = false;
    if (false === $instance) {
        require_once(__DIR__ . DIRECTORY_SEPARATOR . 'facebook-sdk.php');
        $instance = new FB\Facebook(Array(
            'appId'  => Vars::$options['application_id']
          , 'secret' => Vars::$options['application_secret']
          , 'cookie' => true
          , 'domain' => Vars::$options['domain_name']
        ));
    }

    return $instance;
}

function load() {
?>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({appId: '<?php echo Vars::$options['application_id']; ?>', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('auth.login', function(response) { window.location.reload(); });
  };

  (function() {
    var e   = document.createElement('script');
    e.async = true;
    e.src   = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script>
<?php
}

class Actions {
    public static function set_current_user() {
        $sdk = getSDK();

        // User is not logged in to Facebook
        if (null === ($sess = $sdk->getSession())) {
            return;
        }

        // User logged out of WordPress, log them out of Facebook
        if (isset($_COOKIE['wpfb_logout'])) {
            setcookie('wpfb_logout', '', time() - 3600, '/', Vars::$options['domain_name']);
            $url = $sdk->getLogoutUrl();
            $sdk->setSession();

            header('Location: ' . $url);
        }

        // if user is not logged in do the following
        // if user is logged in merge account? do checks?

        try {
            $info = $sdk->api('/me');
            $username = 'fbc' . $sess['uid'];
        } catch (FB\FacebookApiException $e) {
            // Load up an error thingie
            return;
        }

        if (is_user_logged_in()) {
            // was user already logged in from Facebook/other or were they logged in and then linked with facebook
            // merge account
            // return
        }

        require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
        if (username_exists($username)) {
            $user = Auth\signin($username);
        } else {
        // User logged in via Facebook for the first time, register/activate a linked WordPress account

            // Email address is already registered...
            if (false !== get_user_by('email', $info['email'])) {
                // Not sure if I can throw exception, this is outside the theme stuff...
                throw new Exception('email conflict');
            }

            try {
                $key  = Auth\register($username, $info['email'], _generate_password());
                $id   = Auth\activate($key);
                $user = Auth\signin($username);

                _update_user(Array(
                    'ID'            => $user->ID
                  , 'user_nicename' => $info['name']
                  , 'first_name'    => $info['first_name']
                  , 'last_name'     => $info['last_name']
                  , 'nickname'      => $info['name']
                  , 'display_name'  => $info['name']
                  , 'user_url'      => ($info['user_website'] ?: '')
                ));

                update_user_meta($user->ID, 'fbuid', $info['id']);
            } catch (Exception $e) {
                // many types of exceptions
            }
        }
    }

    public static function wp_enqueue_scripts() {
        _enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent'));

        _localize_script('tz-facebook', 'TzFBData', Array('ext_perms' => implode(',', array_keys(Vars::$options['ext_perms']))));
    }

    /**
     * Set a cookie to tell this to logout of Facebook on next pass
     */
    public static function wp_logout() {
        setcookie('wpfb_logout', 1, 0, '/', Vars::$options['domain_name']);
    }
}

class ShortCodes {
    public static function fb_login_button() {
        $sdk = getSDK();
        if ($sdk->getSession()) {
            ob_start();
            print_r($sdk->getSession());
            print_r($_COOKIE);
            print_r($sdk->api('/me'));
            $data = '<pre>' . ob_get_contents() . '</pre>';
            ob_end_clean();

            return $data;
        } else {
            return drawLoginButton(false);
        }
    }
}

class Vars {
    /**
     * WordPress option for this module
     * @type WP_Option
     */
    public static $options;
}
?>