/** * Search Facebook friends with names matching a given string up to a maximum number of results * * @since 1.2 * * @param string $search_term search string * @param int $limit maximum number of results * @return array { * friend results * * @type string 'object_type' user. Differentiate between User and Page results combined in one search. * @type string 'id' Facebook User identifier. * @type string 'name' Facebook User name. * @type string 'picture' Facebook User picture URL. * } */ public static function search_friends($search_term, $limit = 4) { if (!class_exists('Facebook_User')) { require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/facebook-user.php'; } $facebook_user_id = Facebook_User::get_facebook_profile_id(get_current_user_id()); if (!$facebook_user_id) { return array(); } // cached list of all friends $cache_key = 'facebook_13_friends_' . $facebook_user_id; $friends = get_transient($cache_key); if ($friends === false) { if (!class_exists('Facebook_WP_Extend')) { require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/includes/facebook-php-sdk/class-facebook-wp.php'; } try { $friends = Facebook_WP_Extend::graph_api_with_app_access_token($facebook_user_id . '/friends', 'GET', array('fields' => 'id,name,picture', 'ref' => 'fbwpp')); } catch (WP_FacebookApiException $e) { return array(); } if (isset($friends['data']) && is_array($friends['data'])) { $friends = $friends['data']; $clean_friends = array(); foreach ($friends as $friend) { // FBID and name required if (!(isset($friend['name']) && $friend['name'] && isset($friend['id']) && $friend['id'])) { continue; } $clean_friend = array('id' => $friend['id'], 'name' => $friend['name'], 'name_lower' => strtolower($friend['name'])); if (isset($friend['picture']['data']['url'])) { $clean_friend['picture'] = $friend['picture']['data']['url']; } $clean_friends[] = $clean_friend; unset($clean_friend); } $friends = $clean_friends; unset($clean_friends); } else { $friends = array(); } set_transient($cache_key, $friends, 60 * 15); // cache friends list for 15 minutes } // no friends to match against if (empty($friends)) { return array(); } $search_term = strtolower($search_term); // nothing to search against if (!$search_term) { return array(); } $matched_friends = array(); $matched_count = 0; foreach ($friends as $friend) { if ($matched_count === $limit) { break; } // does the search term appear in the name? if (strpos($friend['name_lower'], $search_term) !== false) { $friend['object_type'] = 'user'; unset($friend['name_lower']); $matched_friends[] = $friend; $matched_count++; } } return $matched_friends; }
/** * Mention WordPress users with manage_options capability who can also edit the Facebook app * * @since 1.5.3 * * @param string $app_id Facebook application identifier * @return void */ public static function app_editors($app_id) { // HTTP interface to Facebook if (!class_exists('Facebook_WP_Extend')) { require_once dirname(dirname(__FILE__)) . '/includes/facebook-php-sdk/class-facebook-wp.php'; } $app_roles = Facebook_WP_Extend::graph_api_with_app_access_token($app_id . '/roles', 'GET', array('fields' => 'user,role')); if (empty($app_roles) || !isset($app_roles['data'])) { return; } $app_roles = $app_roles['data']; // Facebook to WordPress user helper class if (!class_exists('Facebook_User')) { require_once dirname(dirname(__FILE__)) . '/facebook-user.php'; } $current_user_facebook_id = Facebook_User::get_facebook_profile_id(get_current_user_id()); $facebook_users_can_edit = array(); foreach ($app_roles as $facebook_user) { if (!(isset($facebook_user['user']) && $facebook_user['user'] && isset($facebook_user['role']) && in_array($facebook_user['role'], array('administrators', 'developers'), true))) { continue; } // confirm the current WordPress user's ability to edit Facebook app values if ($current_user_facebook_id && $facebook_user['user'] == $current_user_facebook_id) { echo '<p>' . __('You have the ability to change these application settings on Facebook.', 'facebook') . '</p>'; return; } $facebook_users_can_edit[$facebook_user['user']] = true; } unset($current_user_facebook_id); unset($app_roles); if (empty($facebook_users_can_edit)) { return; } // fb => [], wp => [] $facebook_users = Facebook_User::get_wordpress_users_associated_with_facebook_accounts('manage_options'); if (empty($facebook_users) || !isset($facebook_users['fb']) || empty($facebook_users['fb'])) { return; } $facebook_users = $facebook_users['fb']; // WordPress accounts capable of managing WordPress site options who have associated a Facebook account capable of editing the current WordPress site's Facebook app $wordpress_users_can_edit = array(); foreach ($facebook_users as $facebook_user) { if (isset($facebook_user->fb_data) && isset($facebook_user->fb_data['fb_uid']) && isset($facebook_users_can_edit[$facebook_user->fb_data['fb_uid']])) { $wordpress_users_can_edit[] = $facebook_user; } } unset($facebook_users); if (empty($wordpress_users_can_edit)) { return; } // display a list of people who could help edit Facebook app values // link to Facebook account page instead of email due to the more public nature of a Facebook account $wordpress_users_display = array(); foreach ($wordpress_users_can_edit as $wordpress_user) { if (!isset($wordpress_user->display_name)) { continue; } $facebook_profile_link = Facebook_User::facebook_profile_link($wordpress_user->fb_data); if ($facebook_profile_link) { $wordpress_users_display[] = '<a href="' . esc_url($facebook_profile_link, array('http', 'https')) . '" target="_blank">' . esc_html($wordpress_user->display_name) . '</a>'; } else { $wordpress_users_display[] = esc_html($wordpress_user->display_name); } unset($facebook_profile_link); } if (empty($wordpress_users_display)) { return; } // format the display of the list of people $wordpress_users_display_count = count($wordpress_users_display); $ask_string = ''; if ($wordpress_users_display_count === 1) { $ask_string = $wordpress_users_display[0]; } else { if ($wordpress_users_display_count === 2) { $ask_string = $wordpress_users_display[0] . ' ' . _x('or', 'bridge between two options: this or that or these', 'facebook') . ' ' . $wordpress_users_display[1]; } else { $ask_string = ', ' . _x('or', 'bridge between two options: this or that or these', 'facebook') . ' ' . array_pop($wordpress_users_display); $ask_string = implode(', ', $wordpress_users_display) . $ask_string; } } echo '<p>' . sprintf(__('%s can change these application settings on Facebook.', 'facebook'), $ask_string) . '</p>'; }
/** * Publish a post to a Facebook User Timeline. * * @since 1.0 * * @global \Facebook_Loader $facebook_loader Access Facebook application credentials * @param int $post_id WordPress post identifier * @param stdClass|WP_Post $post WordPress post object * @return void */ public static function publish_to_facebook_profile($post_id, $post) { global $facebook_loader; $post_id = absint($post_id); if (!(isset($facebook_loader) && $facebook_loader->app_access_token_exists() && $post && $post_id)) { return; } // does the current post have an existing Facebook post id stored? no need to publish again if (get_post_meta($post_id, 'fb_author_post_id', true)) { return; } $meta_box_present = true; if (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) { $meta_box_present = false; } if (!class_exists('Facebook_Social_Publisher_Meta_Box_Profile')) { require_once dirname(__FILE__) . '/publish-box-profile.php'; } if ($meta_box_present && get_post_meta($post_id, Facebook_Social_Publisher_Meta_Box_Profile::POST_META_KEY_FEATURE_ENABLED, true) === '0') { return; } setup_postdata($post); $post_type = get_post_type($post); if (!(self::post_type_is_public($post_type) && post_type_supports($post_type, 'author') && isset($post->post_author))) { return; } $post_author = (int) $post->post_author; if (!$post_author) { return; } // test the author, not the current actor if (!self::user_can_publish_to_facebook($post_author)) { return; } if (!class_exists('Facebook_User')) { require_once $facebook_loader->plugin_directory . 'facebook-user.php'; } $author_facebook_id = Facebook_User::get_facebook_profile_id($post_author); if (!$author_facebook_id) { return; } // check our assumptions about a valid link in place // fail if a piece of the filter process killed our response $link = apply_filters('facebook_rel_canonical', get_permalink($post_id)); if (!$link) { return; } $og_action = false; if (!class_exists('Facebook_Social_Publisher_Settings')) { require_once $facebook_loader->plugin_directory . 'admin/settings-social-publisher.php'; } if (get_option(Facebook_Social_Publisher_Settings::OPTION_OG_ACTION)) { $og_action = true; } if (!class_exists('Facebook_Open_Graph_Protocol')) { require_once $facebook_loader->plugin_directory . 'open-graph-protocol.php'; } $path = $author_facebook_id . '/'; if ($og_action && Facebook_Open_Graph_Protocol::get_post_og_type($post) === 'article') { $story = array('article' => $link); $path .= 'news.publishes'; if ($meta_box_present) { $story['fb:explicitly_shared'] = 'true'; } } else { $story = array('link' => $link); $path .= 'feed'; } $message = get_post_meta($post_id, Facebook_Social_Publisher_Meta_Box_Profile::POST_META_KEY_MESSAGE, true); if (is_string($message) && $message) { $story['message'] = trim($message); } if (!class_exists('Facebook_WP_Extend')) { require_once $facebook_loader->plugin_directory . 'includes/facebook-php-sdk/class-facebook-wp.php'; } $status_messages = array(); try { $publish_result = Facebook_WP_Extend::graph_api_with_app_access_token($path, 'POST', $story); if (isset($publish_result['id'])) { update_post_meta($post_id, 'fb_author_post_id', sanitize_text_field($publish_result['id'])); delete_post_meta($post_id, Facebook_Social_Publisher_Meta_Box_Profile::POST_META_KEY_MESSAGE); delete_post_meta($post_id, Facebook_Social_Publisher_Meta_Box_Profile::POST_META_KEY_FEATURE_ENABLED); } } catch (WP_FacebookApiException $e) { $error_result = $e->getResult(); $status_messages[] = array('message' => esc_html(__('Failed posting to your Facebook Timeline.', 'facebook')) . ' ' . esc_html(__('Error', 'facebook')) . ': ' . esc_html(json_encode($error_result['error'])), 'error' => true); } if (isset($publish_result) && isset($publish_result['id'])) { $link = '<a href="' . esc_url('https://www.facebook.com/' . $publish_result['id'], array('http', 'https')) . '" target="_blank">' . esc_html(__('Facebook Timeline', 'facebook')) . '</a>'; if (empty($message)) { $message = sprintf(esc_html(__('Posted to %s', 'facebook')), $link); } else { $message = sprintf(esc_html(__('Posted to %1$s with message "%2$s"', 'facebook')), $link, esc_html($message)); } $status_messages[] = array('message' => $message, 'error' => false); } // add new status messages if (!empty($status_messages)) { $existing_status_messages = get_post_meta($post_id, 'fb_status_messages', true); if (is_array($existing_status_messages) && !empty($existing_status_messages)) { $status_messages = array_merge($existing_status_messages, $status_messages); } update_post_meta($post_id, 'facebook_status_messages', $status_messages); add_filter('redirect_post_location', array('Facebook_Social_Publisher', 'add_new_post_location')); } }
/** * 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'); } }