示例#1
0
 /**
  * Check if the current user has associated his or her Facebook profile with his or her WordPress account
  * If the current user can edit posts and has not authorized Facebook then show a prompt encouraging action.
  *
  * @since 1.1
  */
 public static function connect_facebook_account($verify_permissions = null)
 {
     global $facebook;
     $profile_prompt = false;
     // check for permission to publish Open Graph action (publish article)
     // check for the superset permission: publish_stream
     if (!is_array($verify_permissions)) {
         $profile_prompt = true;
         $verify_permissions = array('publish_actions', 'publish_stream');
     }
     $current_user = wp_get_current_user();
     // no need to alert if he cannot create a post
     if (!user_can($current_user, 'edit_posts')) {
         return;
     }
     if (!class_exists('Facebook_User')) {
         require_once dirname(__FILE__) . '/facebook-user.php';
     }
     $facebook_user_data_exists = false;
     $facebook_user_data = Facebook_User::get_user_meta($current_user->ID, 'fb_data', true);
     if (is_array($facebook_user_data) && isset($facebook_user_data['fb_uid'])) {
         if (empty($verify_permissions)) {
             return;
         }
         $facebook_user_data_exists = true;
     }
     // Facebook information not found
     $facebook_user = Facebook_User::get_current_user(array('id', 'username'));
     if ($facebook_user) {
         $permissions = $facebook->get_current_user_permissions($facebook_user);
         $all_permissions_exist = true;
         foreach ($verify_permissions as $permission_to_verify) {
             if (!isset($permissions[$permission_to_verify])) {
                 $all_permissions_exist = false;
                 break;
             }
         }
         if ($all_permissions_exist) {
             if (!$facebook_user_data_exists || $facebook_user_data['fb_uid'] != $facebook_user['id']) {
                 $facebook_user_data = array('fb_uid' => $facebook_user['id'], 'activation_time' => time());
                 if (!empty($facebook_user['username'])) {
                     $facebook_user_data['username'] = $facebook_user['username'];
                 }
                 Facebook_User::update_user_meta($current_user->ID, 'fb_data', $facebook_user_data);
             }
             return;
         }
     }
     // 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 all others at P11 after scripts registered
     add_action('admin_enqueue_scripts', array('Facebook_Admin_Login', 'enqueue_scripts'), 11);
     if ($profile_prompt) {
         add_action('admin_notices', array('Facebook_Admin_Login', 'admin_notice'), 1, 0);
     }
     // up top
 }
 /**
  * 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');
     }
 }
 /**
  * Provides the implementations of the inherited abstract
  * methods.  The implementation uses user meta to maintain
  * a store for authorization codes, user ids, CSRF states, and
  * access tokens.
  */
 protected function setPersistentData($key, $value)
 {
     if (!in_array($key, self::$kSupportedKeys)) {
         self::errorLog('Unsupported key passed to setPersistentData.');
         return;
     }
     // load user functions
     if (!class_exists('Facebook_User')) {
         require_once dirname(dirname(dirname(__FILE__))) . '/facebook-user.php';
     }
     Facebook_User::update_user_meta(get_current_user_id(), $key, $value);
 }
示例#4
0
 /**
  * Save custom user information.
  *
  * @since 1.2
  *
  * @uses current_user_can() current user must be able to edit the passed WordPress user ID
  * @param int $wordpress_user_id WordPress user identifier
  * @return void
  */
 public static function save_data($wordpress_user_id)
 {
     if (!($wordpress_user_id && current_user_can('edit_user', $wordpress_user_id))) {
         return;
     }
     // allow decoupling of a WordPress account and a Facebook account
     if (isset($_POST['facebook_remove'])) {
         // WordPress Facebook User helper functions
         if (!class_exists('Facebook_User')) {
             require_once dirname(dirname(__FILE__)) . '/facebook-user.php';
         }
         $facebook_user_id = Facebook_User::get_facebook_profile_id($wordpress_user_id);
         if ($facebook_user_id) {
             // delete mapped FBID and other data
             Facebook_User::delete_user_meta($wordpress_user_id, 'fb_data');
             // delete post to Timeline opt-in if stored
             Facebook_User::delete_user_meta($wordpress_user_id, 'facebook_timeline_disabled');
             // Load WP HTTP helpers
             if (!class_exists('Facebook_WP_Extend')) {
                 require_once dirname(dirname(__FILE__)) . '/includes/facebook-php-sdk/class-facebook-wp.php';
             }
             // Revoke connection to app and all permissions
             Facebook_WP_Extend::graph_api_with_app_access_token($facebook_user_id . '/permissions', 'DELETE');
         }
         unset($facebook_user_id);
         // no need to store any other Facebook data
         return;
     }
     if (isset($_POST['facebook_fbid']) && ctype_digit($_POST['facebook_fbid'])) {
         // WordPress Facebook User helper functions
         if (!class_exists('Facebook_User')) {
             require_once dirname(dirname(__FILE__)) . '/facebook-user.php';
         }
         try {
             $facebook_user = Facebook_User::get_facebook_user($_POST['facebook_fbid'], array('fields' => array('id', 'username', 'link', 'third_party_id')));
             if (isset($facebook_user['id'])) {
                 $facebook_user_data = array('fb_uid' => $facebook_user['id'], 'activation_time' => time());
                 if (!empty($facebook_user['username'])) {
                     $facebook_user_data['username'] = $facebook_user['username'];
                 }
                 if (!empty($facebook_user['link'])) {
                     $facebook_user_data['link'] = $facebook_user['link'];
                 }
                 if (!empty($facebook_user['third_party_id'])) {
                     $facebook_user_data['third_party_id'] = $facebook_user['third_party_id'];
                 }
                 Facebook_User::update_user_meta($wordpress_user_id, 'fb_data', $facebook_user_data);
                 unset($facebook_user_data);
             }
             unset($facebook_user);
         } catch (Exception $e) {
         }
     }
     if (isset($_POST['facebook_timeline']) && $_POST['facebook_timeline'] == '1') {
         // WordPress Facebook User helper functions
         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 {
         // WordPress Facebook User helper functions
         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');
     }
 }
 /**
  * Set the appropriate settings for each form component
  *
  * @since 1.1
  * @param array $options social publisher options
  * @return array clean option sets.
  */
 public static function sanitize_publish_options($options)
 {
     global $facebook;
     if (!is_array($options) || empty($options)) {
         return array();
     }
     if (!class_exists('Facebook_User')) {
         require_once dirname(dirname(__FILE__)) . '/facebook-user.php';
     }
     // publish to timeline is really a per-user setting, not a per-site setting
     // handle the special user case
     $user_meta_key = 'facebook_timeline_disabled';
     $current_user = wp_get_current_user();
     if (isset($options['author_timeline']) && $options['author_timeline'] == 1) {
         Facebook_User::delete_user_meta($current_user->ID, $user_meta_key);
     } else {
         Facebook_User::update_user_meta($current_user->ID, $user_meta_key, '1');
     }
     // is a new page chosen?
     // if the same page selected on new_page_timeline as currently stored don't overwrite the access token
     // it is possible multiple users can create content from the page but should not overwrite each other when editing the page without changing the target page
     if (isset($options['new_page_timeline'])) {
         $page_id = trim($options['new_page_timeline']);
         if (!$page_id && isset($options['page_timeline']['id'])) {
             delete_option(self::OPTION_PUBLISH_TO_PAGE);
         } else {
             if ($page_id && !(isset($options['page_timeline']['id']) && $options['page_timeline']['id'] == $options['new_page_timeline'])) {
                 $pages_for_current_user = self::get_publishable_pages_for_current_user();
                 foreach ($pages_for_current_user as $page) {
                     if (isset($page['id']) && $page['id'] === $page_id) {
                         self::update_publish_to_page($page);
                         break;
                     }
                 }
             }
         }
     }
     return false;
 }