Friend Connect Integration. WordPress Open source blog engine PHP / MySQL based Completely...
-
Upload
robert-potter -
Category
Documents
-
view
226 -
download
3
Transcript of Friend Connect Integration. WordPress Open source blog engine PHP / MySQL based Completely...
WordPress Plugins
• Plugins are tools to extend the functionality of WordPress
• Allow easy modification, customization, and enhancement to a WordPress blog with no need to change the core.
• The plugins API provides a set of integration points (hooks) to set your plugin in motion.
WordPress Hooks
• Actions:o Are the hooks that the WordPress core launches at
specific points durin execution.o Plugins can specify one or more functions to be
executed at these points.• Filters:
o are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen.
WordPress Login integrationUsing OpenSocial PHP Client libraries// include OpenSocial Client Librariesinclude_once (realpath(dirname(__FILE__)) . '/osapi/osapi.php');function getViewer() { $viewer = null; $fcauth_token = $_COOKIE["fcauth" . $site_id]; $provider = new osapiFriendConnectProvider(); $auth = new osapiFCAuth($fcauth_token); $osapi = new osapi($provider,$auth); $batch = $osapi->newBatch(); $profile_fields = array('id' , 'name', 'thumbnailUrl', 'profileUrl'); $self_request_params = array( 'userId' => '@viewer', // Person we are fetching. 'groupId' => '@self', // @self for one person. 'fields' => $profile_fields // Which profile fields to request. ); $batch->add($osapi->people->get($self_request_params), 'viewer'); try { $a = $batch->execute(); $viewer = $a['viewer']; } catch (Exception $e) { // log error } return $viewer;}
http://code.google.com/p/opensocial-php-client
WordPress Login integration
include_once(ABSPATH . 'wp-includes/registration.php');include_once(ABSPATH . 'wp-includes/comment.php');include_once(ABSPATH . 'wp-includes/pluggable.php');include_once(ABSPATH . 'wp-includes/user.php');
// include OpenSocial Client Librariesinclude_once (realpath(dirname(__FILE__)) . '/osapi/osapi.php');
$viewer = getViewer();$local_user = wp_get_current_user();
Include required files
WordPress Login integration
add_action( 'init', 'gfcInit');
function gfcInit() { global $viewer; global $local_user; $logged_in = false; if($viewer != null && $local_user->id == 0) { $logged_in = gfcLogin($viewer); }}
Initialize
WordPress Login integration
function gfcLogin($viewer) { global $wpdb; $uname = $viewer->displayName; $profileurl = $viewer->profileUrl; $image_url = $viewer->thumbnailUrl; $gfcid = $viewer->id; $pass = “a secret string”;
$meta_key = $gfcid."_fc_meta_key"; // Check if a user with this profileId and meta_key exists $metas = $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$meta_key' LIMIT 1") );
// ... tbc...
Login and/or create user
WordPress Login integration
//... if (count($metas) > 0) { // We found me $userid = $metas[0]; } else { $userid = wp_create_user($gfcid, $pass, $gfcid."@friendconnect.google.com"); update_usermeta($userid, $meta_key, $pass); } // Log him in update_usermeta($userid, "user_url", $profileurl); update_usermeta($userid, "image_url", $image_url); update_usermeta($userid, "display_name", $uname); $cred['user_login'] = $gfcid; $cred['user_password'] = get_usermeta($userid, $meta_key); wp_signon($cred);} // end gfcLogin
Login and/or create user
WordPress Login integration
add_action( 'wp_head', 'gfcHead');function gfcHead() { $options = get_option("FriendConnect"); global $local_user; $wg_data = get_option("FriendConnect Sign in"); ?> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load('friendconnect', '0.8'); </script> <script type="text/javascript" src="<?php echo GFC_PLUGIN_URL ?>gfc_integration.js"></script> <script type="text/javascript"> var GFC_SITE_ID = "<?php echo $options['gfcid']; ?>"; var GFC_PLUGIN_URL = "<?php echo GFC_PLUGIN_URL; ?>"; var GFC_LOGOUT_URL = "<?php echo wp_logout_url(get_permalink()); ?>"; var GFC_LOGGED_IN = <?php echo $local_user->id > 0 ? 'true':'false'?>; google.friendconnect.container.setParentUrl("<?php echo $options['gfcurl']; ?>"); </script> <?php}
Include JS Libraries
WordPress Login integration
/** * this filter gets the comments template and adds the FC required code to it */add_filter('comments_template', 'gfcCommentsTemplate');
function gfcCommentsTemplate($post_id) { return realpath(dirname(__FILE__)).'/comments_wrapper.php';}
Add login button
WordPress Login integration
require( TEMPLATEPATH . $file );?><div id="friend_connect_login" style="display:block;"></div><script type="text/javascript"> google.friendconnect.container.initOpenSocialApi( {site: GFC_SITE_ID, onload: function(securityToken) {
if (!GFC_LOGGED_IN && !window.timesloaded) {window.timesloaded = 1;google.friendconnect.renderSignInButton({ 'id':
'friend_connect_login', 'text' : 'Click here to join ', 'style': 'standard' });
} else {window.timesloaded ++;
}if (window.timesloaded > 1 && !GFC_LOGGED_IN) {
window.top.location.reload();}if (GFC_LOGGED_IN) {
gfcRenderViewer('friend_connect_login');}
}})</script>
Add login button
WordPress Login integration
function gfcRenderViewer(el_id) {var req = opensocial.newDataRequest();var opt_params = {};opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS]
= [opensocial.Person.Field.ID,opensocial.Person.Field.THUMBNAIL_URL,opensocial.Person.Field.PROFILE_URL,opensocial.Person.Field.URLS, opensocial.Person.Field.NAME ];
req.add(req.newFetchPersonRequest('VIEWER', opt_params), 'viewer');req.send( function(data) { gfcRenderViewerSuccess(data, el_id);});
}
Render viewer info
WordPress Login integration
function gfcRenderViewerSuccess(data, el_id) {var viewer = data.get('viewer').getData();document.getElementById(el_id).innerHTML = [ '<img style="width: 32px; height: 32px;" ', 'class="avatar avatar-32 photo" align="left" src="', viewer.getField("thumbnailUrl") , '" alt="avatar"/>',
'<strong>Hello ', viewer.getField("displayName"),'!</strong><br/>', '<a href="#"
onclick="google.friendconnect.requestSettings()">Settings</a> | ', '<a href="#"
onclick="google.friendconnect.requestInvite()">Invite</a> | ', '<a href="', GFC_LOGOUT_URL, '" ', 'onclick="google.friendconnect.requestSignOut()">Sign
out</div>'].join('');}
Render viewer info
Gadgets integration
/** * init the fc widgets */add_action("plugins_loaded", "init_members_widget");
function init_members_widget() { register_sidebar_widget("GFC Members", "fc_members_widget"); register_widget_control("GFC Members", "fc_members_widget_control");}
Register your widget
Gadgets integration
function fc_members_widget_control() { $data = get_option("GFC Members"); ?> <p><label for="FriendConnectMembers_title">Title: <input type="text" name="FriendConnectMembers_title" id="FriendConnectMembers_title" value="<?php $data['FriendConnectMembers_title'])?>" /> </label></p> ... <?php if(isset($_POST['FriendConnectMembers_title'])) { $data['FriendConnectMembers_title'] = attribute_escape($_POST['FriendConnectMembers_title']); update_option('GFC Members', $data); } ....}
Widget Control
Gadgets integration
function fc_members_widget() { extract($args); $options = get_option("FriendConnect"); $wg_options = get_option("GFC Members"); echo $before_widget; echo $before_title . $wg_options['FriendConnectMembers_title']. $after_title; ?> <div id="div-members-widget" style="width: 100%”></div> <script type="text/javascript"> var members_skin = <?php echo render_skin();?>; members_skin['NUMBER_ROWS'] = '<?php echo $wg_options['FriendConnectMembers_rows'];?>'; google.friendconnect.container.renderMembersGadget( { id: 'div-members-widget', site: '<?php echo $options['gfcid']?>' }, members_skin ); </script> <?php echo $after_widget;}
Widget Content
Drupal
• Open source content management platform.• PHP based• Fully customizable via theme and module
system
Drupal Modules
• A Drupal module is simply a collection of files containing a set of routines written in PHP
• This approach allows module code to be run at specific places in the engine.
• This code can then do whatever is needed to enhance the existing functionality.
• The places where code can be executed are called "hooks" and are defined by a fixed interface.
Drupal Integration
/** * Implementation of hook_schema(). */function friendconnect_schema() { $schema['friendconnect'] = array( 'description' => t('Google Friend Connect profile to Local User mapping.'), 'fields' => array( 'fcid' => array( 'type' => 'varchar', 'length' => 100, 'not null' => TRUE, 'description' => t('The Google Friend Connect profile id.'), ), 'uid' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => t('The local Drupal instance user uid.'), ),....
friendconnect.install
Drupal Integration
/** * Implementation of hook_install(). */function friendconnect_install() { drupal_install_schema('friendconnect');};
/** * Implementation of hook_uninstall(). * * MYSQL command: * DROP TABLE `friendconnect`; */function friendconnect_uninstall() { drupal_uninstall_schema('friendconnect');};
friendconnect.install
Drupal Integration
function friendconnect_menu() { $items = array(); $items['admin/settings/friendconnect'] = array( 'title' => t('Google Friend Connect'), 'type' => MENU_NORMAL_ITEM, 'page callback' => 'drupal_get_form', 'page arguments' => array('friendconnect_admin'), 'access arguments' => array('administer site configuration'), 'description' => t('Configure how your site talks to Google Friend' . ' Connect.'), ); // This page is rendered to the user only when an error occurs. $items['friendconnect/join'] = array( 'title' => t('Auto-Login with Google Friend Connect'), 'type' => MENU_SUGGESTED_ITEM, 'page callback' => 'friendconnect_join', 'access arguments' => array('access content'), 'description' => t('Login to local Drupal account ' . '(create one if needed).'), ); return $items;};
frendconnect.moduleImplementation of hook_menu();
Drupal Integration
function friendconnect_admin() { // site id $form['friendconnect_site_config'] = array( '#type' => 'fieldset', '#id' => 'friendconnect_site_config', '#title' => t('Friend Connect Configuration') , '#collapsible' => true, '#collapsed' => false, '#weight' => -2, ); $form['friendconnect_site_config']['friendconnect_siteid'] = array( '#type' => 'textfield', '#title' => t('Site ID'), '#required' => true, '#size' => 25, '#default_value' => variable_get('friendconnect_siteid', ''), '#description' => t('Unique site identifier provided by Google' . ' Friend Connect during initial registration.'), '#weight' => 0, ); .....}
Implementation of hook_admin();
Drupal Integration
function friendconnect_init() { .... // load remote js file drupal_set_html_head('<script type="text/javascript" ' . 'src="http://www.google.com/jsapi"></script>'); drupal_set_html_head('<script type="text/javascript" ' . 'src="'.$parent_url.'/jscolor/jscolor.js"></script>'); drupal_add_js('google.load("friendconnect", "0.8");', 'inline', 'header');
// load local js file drupal_add_js($module_path . 'friendconnect.js', 'module', 'footer'); ...};
Implementation of hook_init();
Drupal Integration
function friendconnect_join() { .... $site_id = variable_get('friendconnect_siteid', ''); $fcauth_token = $_COOKIE["fcauth" . $site_id]; //... get the viewer .... // http://www.google.com/friendconnect/api/people/@viewer/@self? // fcauth=<cookie value>
// login the old/new visitor into the local site $uname = local_user($viewer->id, $viewer->displayName); $status = register_user($viewer->id, $uname); if ($status != 0) return $status;
// update visitor profile image friendconnect_local_image($user->uid, $viewer->thumbnailUrl);
// take the user back to the original page drupal_goto($_GET['fcto']);
};
Implementation of friendconnect_join();
Drupal Integration
function register_user($fcid, $uname) { // let us try logging in the user now .. if (user_external_login_register($uname, 'friendconnect') != NULL) { $error = '<p>' . 'Auto-login as user \'' . $uname . '\' failed! Please try again later..' . '</p>'; $_GET['error'] = $error; return drupal_get_form('friendconnect_custom_error_page'); }
// register new username with friend connect id global $user; $query = 'SELECT uid FROM {friendconnect} WHERE fcid="%s" LIMIT 1'; $result = db_query($query, mysql_escape_string($fcid)); $exists = 0; while ($val = db_fetch_object($result)) $exists = 1; if ($exists == 0) { $query = 'INSERT INTO {friendconnect} (fcid, uid, creation_time)' . ' VALUES("%s", %d, NOW())'; $result = db_query($query, mysql_escape_string($fcid), $user->uid); } return 0; // all okay indicator};
Implementation of register_user();
Drupal Integration
function friendconnect_form_alter(&$form, $form_state, $form_id) { if ($form_id == 'user_login_block' || $form_id == 'user_login') { $form['friendconnect_displayset']['friendconnect_display'] = array( '#type' => 'markup', '#prefix' => <div id="friendconnect_login_btn">' '#value' => t('loading...'), '#suffix' => '</div>', ); }}
Implementation of hook_form_alter();
Drupal Integration
function friendconnect_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': // members $blocks[0] = array( 'info' => t('Friend Connect Members'), ); return $blocks; case 'configure': switch ($delta) { Case 0: // friendconnect members $form['friendconnect_members_rows'] = array( '#type' => 'textfield', '#title' => t('Rows per page'), '#size' => 2, '#description' => t('Amount of rows...'), '#default_value' => variable_get( 'friendconnect_members_rows', 4), ); break; } return $form; ...
Implementation of hook_block();
Drupal Integration
case 'save': switch ($delta) { case 0: variable_set('friendconnect_members_rows', $edit['friendconnect_members_rows']); Break; }case 'view': switch ($delta) { case 0: $block['content'] = members_content(); break; ...
Implementation of hook_block();
Drupal Integration
function friendconnect_members_content() { return '<div id="friendconnect_members"' . ' style="width: 100%;">Loading..</div>' . '<script type="text/javascript"> var GFC_members_skin = '.friendconnect_build_skin().'; GFC_members_skin[\'NUMBER_ROWS\'] = ' . variable_get('friendconnect_members_rows', 4).'; google.friendconnect.container.renderMembersGadget( { id: \'friendconnect_members\', site: \''.variable_get('friendconnect_siteid', '').'\' }, GFC_members_skin); </script>';};
Implementation of members_content();