Permalink
Browse files

authorize Facebook application from user profile page

  • Loading branch information...
1 parent 39d78e4 commit 09398debfb6234ecb4f1e4800f2f1499a3675d6e @niallkennedy niallkennedy committed Feb 5, 2013
Showing with 205 additions and 14 deletions.
  1. +7 −2 admin/login.php
  2. +147 −0 admin/profile.php
  3. +22 −8 admin/settings.php
  4. +24 −2 static/js/admin/login.js
  5. +1 −1 static/js/admin/login.min.js
  6. +4 −1 uninstall.php
View
@@ -75,7 +75,7 @@ public static function connect_facebook_account( $verify_permissions = null ) {
}
// priority before js sdk registration needed to add JS inside FbAsyncInit
- add_action( 'admin_enqueue_scripts', array( 'Facebook_Admin_Login', 'add_async_load_javascript_filter' ), -1, 0 );
+ add_action( 'admin_enqueue_scripts', array( 'Facebook_Admin_Login', 'add_async_load_javascript_filter' ), -1, 2 );
// add all others at P11 after scripts registered
add_action( 'admin_enqueue_scripts', array( 'Facebook_Admin_Login', 'enqueue_scripts' ), 11 );
@@ -95,6 +95,11 @@ public static function admin_notice() {
echo '</p></div>';
}
+ /**
+ * Add output to the JavaScript SDK async loader success function filter
+ *
+ * @since 1.1
+ */
public static function add_async_load_javascript_filter() {
// async load our script after we async load Facebook JavaScript SDK
add_filter( 'facebook_jssdk_init_extras', array( 'Facebook_Admin_Login', 'async_load_javascript' ), 10, 2 );
@@ -117,7 +122,7 @@ public static function enqueue_scripts() {
* @return string JavaScript code to be appended to the fbAsyncInit function
*/
public static function async_load_javascript( $js_block = '', $app_id = '' ) {
- return $js_block . 'jQuery.ajax({url:' . json_encode( plugins_url( 'static/js/admin/login' . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ) . '.js', dirname(__FILE__) ) ) . ',cache:true,dataType:"script"}).success(function(){FB_WP.admin.login.attach_events();});';
+ return $js_block . 'jQuery.ajax({url:' . json_encode( plugins_url( 'static/js/admin/login' . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ) . '.js', dirname(__FILE__) ) ) . ',cache:true,dataType:"script"}).success(function(){FB_WP.admin.login.messages.author_permissions_text=' . json_encode( __( 'Allow new posts to your Facebook Timeline', 'facebook' ) ) . ';FB_WP.admin.login.attach_events()});';
}
}
?>
View
@@ -0,0 +1,147 @@
+<?php
+/**
+ * Add content to a WordPress user profile
+ *
+ * @since 1.2
+ */
+class Facebook_User_Profile {
+
+ /**
+ * Conditionally load features on the edit profile page
+ *
+ * @since 1.2
+ */
+ public static function init() {
+ $current_user = wp_get_current_user();
+
+ if ( ! ( $current_user && user_can( $current_user, 'edit_posts' ) ) )
+ return;
+
+ // prompt to log in or update account info
+ if ( ! class_exists( 'Facebook_Admin_Login' ) )
+ require_once( dirname(__FILE__) . '/login.php' );
+ Facebook_Admin_Login::connect_facebook_account( array( 'publish_actions', 'publish_stream' ) );
+
+ // use the Admin Login async JS an an indicator of further login action needed (not connected or connected without all required Timeline permissions)
+ if ( remove_action( 'admin_enqueue_scripts', array( 'Facebook_Admin_Login', 'add_async_load_javascript_filter' ), -1, 2 ) ) {
+ add_action( 'admin_enqueue_scripts', array( 'Facebook_User_Profile', 'add_async_load_javascript_filter' ), -1, 2 );
+ } else {
+ // connected. permissions exist
+ add_action( 'personal_options', array( 'Facebook_User_Profile', 'personal_options' ) );
+ add_action( 'personal_options_update', array( 'Facebook_User_Profile', 'save_data' ) );
+ add_action( 'show_user_profile', array( 'Facebook_User_Profile', 'enhance_input_field' ) );
+ }
+
+ add_filter( 'user_contactmethods', array( 'Facebook_User_Profile', 'user_contactmethods' ), 1, 2 );
+ }
+
+ /**
+ * Allow an author to disable posting to Timeline
+ *
+ * @since 1.2
+ * @param $wordpress_user WP_User object for the current profile
+ */
+ public static function personal_options( $wordpress_user ) {
+ echo '<tr class="facebook-post-to-timeline"><th scope="row">Facebook</th><td><input class="checkbox" type="checkbox" name="facebook_timeline" id="facebook-timeline" value="1"';
+
+ if ( $wordpress_user ) {
+ if ( ! class_exists( 'Facebook_User' ) )
+ require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
+ checked( ! Facebook_User::get_user_meta( $wordpress_user->ID, 'facebook_timeline_disabled', true ) );
+ }
+
+ echo ' /> <label for="facebook-timeline">' . esc_html( __( 'Post an article to my Facebook Timeline after it is public.', 'facebook' ) ) . '</label><br /></td></tr>';
+ }
+
+ /**
+ * Add a Facebook form field to the contact info section
+ *
+ * @since 1.2
+ * @param array $user_contactmethods associative array of id label pairs.
+ * @param WP_User $user WordPress user
+ */
+ public static function user_contactmethods( $user_contactmethods, $user ) {
+ if ( is_array( $user_contactmethods ) && $user && method_exists( $user, 'exists' ) && user_can( $user, 'edit_posts' ) ) {
+ $user_contactmethods['facebook'] = 'Facebook';
+
+ if ( ! class_exists( 'Facebook_User' ) )
+ require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
+ $facebook_user_data = Facebook_User::get_user_meta( get_current_user_id(), 'fb_data', true );
+
+ if ( isset( $facebook_user_data['username'] ) )
+ $user->facebook = esc_url_raw( 'https://www.facebook.com/' . $facebook_user_data['username'], array( 'http', 'https' ) );
+ else if ( isset( $facebook_user_data['fb_uid'] ) )
+ $user->facebook = esc_url_raw( 'https://www.facebook.com/profile.php?' . http_build_query( array( 'id' => $facebook_user_data['fb_uid'] ), '', '&' ), array( 'http', 'https' ) );
+ unset( $facebook_user_data );
+ }
+
+ return $user_contactmethods;
+ }
+
+ /**
+ * Add output to the JavaScript SDK async loader success function filter
+ *
+ * @since 1.2
+ */
+ public static function add_async_load_javascript_filter() {
+ // async load our script after we async load Facebook JavaScript SDK
+ add_filter( 'facebook_jssdk_init_extras', array( 'Facebook_User_Profile', 'async_load_javascript' ), 10, 2 );
+ }
+
+ /**
+ * add JavaScript code to the fbAsyncInit function run after Facebook JavaScript SDK has loaded.
+ *
+ * @since 1.2
+ * @param string $js_block existing JavaScript in filter
+ * @param string Facebook application id
+ * @return string JavaScript code to be appended to the fbAsyncInit function
+ */
+ public static function async_load_javascript( $js_block = '', $app_id = '' ) {
+ return $js_block . 'jQuery.ajax({url:' . json_encode( plugins_url( 'static/js/admin/login' . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ) . '.js', dirname(__FILE__) ) ) . ',cache:true,dataType:"script"}).done(function(){FB_WP.admin.login.messages.author_permissions_text=' . json_encode( __( 'Allow new posts to your Facebook Timeline', 'facebook' ) ) . ';FB_WP.admin.login.edit_profile()});';
+ }
+
+ /**
+ * Add information to contact info field after output
+ *
+ * @since 1.2
+ * @param WP_User WordPress user
+ */
+ public static function enhance_input_field( $wordpress_user ) {
+ global $facebook_loader;
+
+ echo '<script type="text/javascript">jQuery(function(){';
+ echo 'var facebook_input_el=jQuery("#facebook");if(facebook_input_el.length===0){return}';
+
+ // disable the input. encourage permissions management on Facebook.com
+ echo 'facebook_input_el.prop("disabled",true );';
+ if ( isset( $facebook_loader ) && isset( $facebook_loader->credentials['app_id'] ) ) {
+ echo 'facebook_input_el.after( jQuery("<div />").append( jQuery("<a />").attr({"href":' . json_encode( esc_url_raw( 'https://www.facebook.com/settings?tab=applications#application-li-' . $facebook_loader->credentials['app_id'], array( 'http', 'https' ) ) ) . ',"target":"_blank"}).text(' . json_encode( _x( 'Edit Permissions', 'edit permissions granted by a Facebook account to a Facebook application', 'facebook' ) ) . ' ) ) );';
+ }
+
+ echo '});</script>';
+ }
+
+ /**
+ * Save custom user information
+ *
+ * @since 1.2
+ * @param int $wordpress_user_id WordPress user identifier
+ */
+ public static function save_data( $wordpress_user_id ) {
+ remove_filter( 'user_contactmethods', array( 'Facebook_User_Profile', 'user_contactmethods' ), 1, 2 );
+
+ if ( ! ( $wordpress_user_id && current_user_can( 'edit_user', $wordpress_user_id ) ) )
+ return;
+
+ if ( isset( $_POST[ 'facebook_timeline' ] ) && $_POST[ 'facebook_timeline' ] == '1' ) {
+ if ( ! class_exists( 'Facebook_User' ) )
+ require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
+ Facebook_User::delete_user_meta( $wordpress_user_id, 'facebook_timeline_disabled' ); // delete if stored
+ } else {
+ if ( ! class_exists( 'Facebook_User' ) )
+ require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
+ Facebook_User::update_user_meta( $wordpress_user_id, 'facebook_timeline_disabled', '1' );
+ }
+ }
+}
+?>
View
@@ -23,16 +23,30 @@ public static function init() {
self::migrate_options();
add_action( 'admin_menu', array( 'Facebook_Settings', 'settings_menu_items' ) );
add_filter( 'plugin_action_links', array( 'Facebook_Settings', 'plugin_action_links' ), 10, 2 );
+ add_action( 'admin_init', array( 'Facebook_Settings', 'load_social_settings' ), 1 );
add_action( 'admin_enqueue_scripts', array( 'Facebook_Settings', 'enqueue_scripts' ) );
+ }
- if ( self::app_credentials_exist() ) {
- $available_features = apply_filters( 'facebook_features', self::$features );
- if ( is_array( $available_features ) && ! empty( $available_features ) ) {
- if ( isset( $available_features['social_publisher'] ) ) {
- // check user capability to publish to Facebook
- $current_user = wp_get_current_user();
- if ( user_can( $current_user, 'edit_posts' ) )
- add_action( 'admin_init', array( 'Facebook_Settings', 'prompt_user_login' ) );
+ /**
+ * Load extra settings only if Facebook application credentials exist
+ *
+ * @since 1.2
+ */
+ public static function load_social_settings() {
+ global $facebook_loader;
+
+ if ( ! ( isset( $facebook_loader ) && $facebook_loader->app_access_token_exists() ) )
+ return;
+
+ $available_features = apply_filters( 'facebook_features', self::$features );
+ if ( is_array( $available_features ) && ! empty( $available_features ) ) {
+ if ( isset( $available_features['social_publisher'] ) ) {
+ // check user capability to publish to Facebook
+ $current_user = wp_get_current_user();
+ if ( user_can( $current_user, 'edit_posts' ) ) {
+ if ( ! class_exists( 'Facebook_User_Profile' ) )
+ require_once( dirname(__FILE__) . '/profile.php' );
+ add_action( 'load-profile.php', array( 'Facebook_User_Profile', 'init' ) );
}
}
}
@@ -2,6 +2,9 @@
var FB_WP = FB_WP || {};
FB_WP.admin = FB_WP.admin || {};
FB_WP.admin.login = {
+ messages: {
+ author_permissions_text: "Allow new posts to your Facebook Timeline"
+ },
attach_events: function() {
// Facebook JavaScript SDK not present
if ( typeof FB === "undefined" ) {
@@ -10,6 +13,9 @@ FB_WP.admin.login = {
jQuery(".facebook-login").each(function(index) {
var login_el = jQuery(this);
+ if ( login_el.data( "parsed" ) === true ) {
+ return;
+ }
var scope = login_el.data("scope");
if ( scope === "person" ) {
login_el.click( FB_WP.admin.login.post_to_timeline );
@@ -18,27 +24,40 @@ FB_WP.admin.login = {
login_el.css( "cursor", "pointer" );
login_el.css( "color", "#21759B" );
login_el.css( "text-decoration", "underline" );
+
+ // only process once
+ login_el.data( "parsed", true );
} else if ( scope === "page" ) {
login_el.click( FB_WP.admin.login.post_to_page );
// match link style
login_el.css( "cursor", "pointer" );
login_el.css( "color", "#21759B" );
login_el.css( "text-decoration", "underline" );
+
+ // only process once
+ login_el.data( "parsed", true );
}
});
},
+ edit_profile: function() {
+ var facebook_input_el = jQuery( "#facebook" );
+ if ( facebook_input_el.length !== 0 && jQuery.trim( facebook_input_el.val() ) === "" ) {
+ facebook_input_el.after( jQuery( "<div />" ).addClass( "facebook-login" ).attr( "data-scope", "person" ).css( "font-weight", "bold" ).text( FB_WP.admin.login.messages.author_permissions_text ) );
+ FB_WP.admin.login.attach_events();
+ }
+ },
post_to_timeline: function(){
FB.login( function(response){
if ( response.authResponse ) {
- FB_WP.admin.login.redirect_with_parameter("fb_extended_token", -1);
+ FB_WP.admin.login.refresh();
}
}, {scope:"publish_stream,publish_actions"} );
},
post_to_page: function(){
FB.login( function(response){
if ( response.authResponse ) {
- FB_WP.admin.login.redirect_with_parameter("fb_extended_token", -1);
+ FB_WP.admin.login.refresh();
}
}, {scope:"manage_pages,publish_stream,publish_actions"} );
},
@@ -70,5 +89,8 @@ FB_WP.admin.login = {
document.location.search = kvp.join("&");
}
+ },
+ refresh: function() {
+ FB_WP.admin.login.redirect_with_parameter("fb_extended_token", -1);
}
}
@@ -1 +1 @@
-var FB_WP=FB_WP||{};FB_WP.admin=FB_WP.admin||{};FB_WP.admin.login={attach_events:function(){if(typeof FB==="undefined"){return}jQuery(".facebook-login").each(function(index){var login_el=jQuery(this);var scope=login_el.data("scope");if(scope==="person"){login_el.click(FB_WP.admin.login.post_to_timeline);login_el.css("cursor","pointer");login_el.css("color","#21759B");login_el.css("text-decoration","underline")}else{if(scope==="page"){login_el.click(FB_WP.admin.login.post_to_page);login_el.css("cursor","pointer");login_el.css("color","#21759B");login_el.css("text-decoration","underline")}}})},post_to_timeline:function(){FB.login(function(response){if(response.authResponse){FB_WP.admin.login.redirect_with_parameter("fb_extended_token",-1)}},{scope:"publish_stream,publish_actions"})},post_to_page:function(){FB.login(function(response){if(response.authResponse){FB_WP.admin.login.redirect_with_parameter("fb_extended_token",-1)}},{scope:"manage_pages,publish_stream,publish_actions"})},redirect_with_parameter:function(key,value){var param=new Object();param[key]=value;key=escape(key);value=escape(value);var kvp=document.location.search.substr(1).split("&");if(kvp==""){document.location.search="?"+jQuery.param(param)}else{var i=kvp.length;var x;while(i--){x=kvp[i].split("=");if(x[0]==key){x[1]=value;kvp[i]=jQuery.param(param);break}}if(i<0){kvp[kvp.length]=jQuery.param(param)}document.location.search=kvp.join("&")}}};
+var FB_WP=FB_WP||{};FB_WP.admin=FB_WP.admin||{};FB_WP.admin.login={messages:{author_permissions_text:"Allow new posts to your Facebook Timeline"},attach_events:function(){if(typeof FB==="undefined"){return}jQuery(".facebook-login").each(function(index){var login_el=jQuery(this);if(login_el.data("parsed")===true){return}var scope=login_el.data("scope");if(scope==="person"){login_el.click(FB_WP.admin.login.post_to_timeline);login_el.css("cursor","pointer");login_el.css("color","#21759B");login_el.css("text-decoration","underline");login_el.data("parsed",true)}else{if(scope==="page"){login_el.click(FB_WP.admin.login.post_to_page);login_el.css("cursor","pointer");login_el.css("color","#21759B");login_el.css("text-decoration","underline");login_el.data("parsed",true)}}})},edit_profile:function(){var facebook_input_el=jQuery("#facebook");if(facebook_input_el.length!==0&&jQuery.trim(facebook_input_el.val())===""){facebook_input_el.after(jQuery("<div />").addClass("facebook-login").attr("data-scope","person").css("font-weight","bold").text(FB_WP.admin.login.messages.author_permissions_text));FB_WP.admin.login.attach_events()}},post_to_timeline:function(){FB.login(function(response){if(response.authResponse){FB_WP.admin.login.refresh()}},{scope:"publish_stream,publish_actions"})},post_to_page:function(){FB.login(function(response){if(response.authResponse){FB_WP.admin.login.refresh()}},{scope:"manage_pages,publish_stream,publish_actions"})},redirect_with_parameter:function(key,value){var param=new Object();param[key]=value;key=escape(key);value=escape(value);var kvp=document.location.search.substr(1).split("&");if(kvp==""){document.location.search="?"+jQuery.param(param)}else{var i=kvp.length;var x;while(i--){x=kvp[i].split("=");if(x[0]==key){x[1]=value;kvp[i]=jQuery.param(param);break}}if(i<0){kvp[kvp.length]=jQuery.param(param)}document.location.search=kvp.join("&")}},refresh:function(){FB_WP.admin.login.redirect_with_parameter("fb_extended_token",-1)}};
View
@@ -9,10 +9,13 @@
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) )
exit();
+if ( ! class_exists( 'Facebook_User' ) )
+ require_once( dirname(__FILE__) . '/facebook-user.php' );
+
// user data
$__user_id = get_current_user_id();
foreach ( array('state', 'code', 'access_token', 'user_id', 'fb_data', 'facebook_timeline_disabled') as $meta_key ) {
- delete_user_meta( $__user_id, $meta_key );
+ Facebook_User::delete_user_meta( $__user_id, $meta_key );
}
unset( $__user_id );

0 comments on commit 09398de

Please sign in to comment.