/** * Check to see if the active admin has authenticated with Facebook * If not, display a warning message * * @since 1.0 */ function fb_check_connected_accounts() { $current_user = wp_get_current_user(); global $facebook; if (!isset($facebook)) { return; } $options = get_option('fb_options'); // check if we have enough info to handle the authFacebook function if (!$options || empty($options['app_id']) || empty($options['app_secret'])) { return; } //see if they have connected their account to facebook $fb_data = fb_get_user_meta($current_user->ID, 'fb_data', true); //if no, show message prompting to connect if (empty($fb_data['fb_uid']) && isset($options['social_publisher']) && isset($options['social_publisher']['enabled'])) { $fb_user = fb_get_current_user(); if ($fb_user) { $perms = $facebook->api('/me/permissions', 'GET', array('ref' => 'fbwpp')); } if ($fb_user && isset($perms['data'][0]['manage_pages']) && isset($perms['data'][0]['publish_actions']) && isset($perms['data'][0]['publish_stream'])) { $fb_user_data = array('fb_uid' => $fb_user['id'], 'username' => $fb_user['username'], 'activation_time' => time()); fb_update_user_meta($current_user->ID, 'fb_data', $fb_user_data); } else { fb_admin_dialog(sprintf(__('Facebook social publishing is enabled. %sLink your Facebook account to your WordPress account</a> to get full functionality, including adding new Posts to your Timeline.', 'facebook'), '<a href="#" onclick="authFacebook(); return false;">'), true); } } else { } }
/** * Posts an Open Graph action to an author's Facebook Timeline * * @since 1.0 * @param int $post_id The post ID that will be posted */ function fb_post_to_author_fb_timeline($post_id) { global $post; global $facebook; $status_messages = array(); if (!isset($facebook)) { return; } $options = get_option('fb_options'); $fb_mentioned_friends = get_post_meta($post_id, 'fb_mentioned_friends', true); if (!empty($fb_mentioned_friends)) { // does current post type and the current theme support post thumbnails? if (post_type_supports($post->post_type, 'thumbnail') && function_exists('has_post_thumbnail') && has_post_thumbnail()) { list($post_thumbnail_url, $post_thumbnail_width, $post_thumbnail_height) = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); } $mentioned_friends_message = get_post_meta($post_id, 'fb_mentioned_friends_message', true); $publish_ids_friends = array(); $friends_posts = ''; foreach ($fb_mentioned_friends as $friend) { try { if (!isset($post_thumbnail_url)) { $args = array('link' => apply_filters('rel_canonical', get_permalink()), 'name' => get_the_title(), 'caption' => apply_filters('the_excerpt', get_the_excerpt()), 'description' => fb_strip_and_format_desc($post), 'message' => $mentioned_friends_message); } else { $args = array('link' => apply_filters('rel_canonical', get_permalink()), 'picture' => $post_thumbnail_url, 'name' => get_the_title(), 'caption' => apply_filters('the_excerpt', get_the_excerpt()), 'description' => fb_strip_and_format_desc($post), 'message' => $mentioned_friends_message); } $args['ref'] = 'fbwpp'; $publish_result = $facebook->api('/' . $friend['id'] . '/feed', 'POST', $args); $publish_ids_friends[] = sanitize_text_field($publish_result['id']); $friends_posts .= '<a href="' . esc_url(fb_get_permalink_from_feed_publish_id($publish_result['id'])) . '" target="_blank"><img src="' . esc_url('http://graph.facebook.com/' . $friend['id'] . '/picture') . '" width="15"></a> '; } catch (WP_FacebookApiException $e) { $error_result = $e->getResult(); if ($e->getCode() == 210) { $status_messages[] = array('message' => sprintf(__('Failed posting to mentioned friend\'s Facebook Timeline. <img src="' . esc_url('http://graph.facebook.com/' . $friend['id'] . '/picture') . '" width="15"> Error: Page doesn\'t allow posts from other Facebook users. Full error: ' . json_encode($error_result['error']), true)), 'error' => true); } else { $status_messages[] = array('message' => sprintf(__('Failed posting to mentioned friend\'s Facebook Timeline. <img src="' . esc_url('http://graph.facebook.com/' . $friend['id'] . '/picture') . '" width="15"> Error: ' . json_encode($error_result['error']), true)), 'error' => true); } } } update_post_meta($post_id, 'fb_mentioned_friends_post_ids', $publish_ids_friends); if (!empty($publish_ids_friends)) { $status_messages[] = array('message' => sprintf(__('Posted to mentioned friends\' Facebook Timelines. ' . $friends_posts)), 'error' => false); } } $fb_mentioned_pages = get_post_meta($post_id, 'fb_mentioned_pages', true); $pages_posts = ''; if (!empty($fb_mentioned_pages)) { $mentioned_pages_message = get_post_meta($post_id, 'fb_mentioned_pages_message', true); $publish_ids_pages = array(); foreach ($fb_mentioned_pages as $page) { try { if (!isset($post_thumbnail_url)) { $args = array('link' => apply_filters('rel_canonical', get_permalink()), 'name' => get_the_title(), 'caption' => apply_filters('the_excerpt', get_the_excerpt()), 'description' => fb_strip_and_format_desc($post), 'message' => $mentioned_pages_message); } else { $args = array('link' => apply_filters('rel_canonical', get_permalink()), 'picture' => $post_thumbnail_url, 'name' => get_the_title(), 'caption' => apply_filters('the_excerpt', get_the_excerpt()), 'description' => fb_strip_and_format_desc($post), 'message' => $mentioned_pages_message); } $args['ref'] = 'fbwpp'; $publish_result = $facebook->api('/' . $page['id'] . '/feed', 'POST', $args); $publish_ids_pages[] = sanitize_text_field($publish_result['id']); $pages_posts .= '<a href="' . sanitize_text_field(fb_get_permalink_from_feed_publish_id($publish_result['id'])) . '" target="_blank"><img src="http://graph.facebook.com/' . $page['id'] . '/picture" width="15" target="_blank"></a> '; } catch (WP_FacebookApiException $e) { $error_result = $e->getResult(); if ($e->getCode() == 210) { $status_messages[] = array('message' => sprintf(__('Failed posting to mentioned page\'s Facebook Timeline. <img src="http://graph.facebook.com/' . $page['id'] . '/picture" width="15"> Error: Page doesn\'t allow posts from other Facebook users. Full error: ' . json_encode($error_result['error']), true)), 'error' => true); } else { $status_messages[] = array('message' => sprintf(__('Failed posting to mentioned page\'s Facebook Timeline. <img src="http://graph.facebook.com/' . $page['id'] . '/picture" width="15"> Error: ' . json_encode($error_result['error']), true)), 'error' => true); } } } update_post_meta($post_id, 'fb_mentioned_pages_post_ids', $publish_ids_pages); if (!empty($publish_ids_pages)) { $status_messages[] = array('message' => sprintf(__('Posted to mentioned pages\' Facebook Timelines. ' . $pages_posts)), 'error' => false); } } $fb_user = fb_get_current_user(); if (isset($fb_user)) { $perms = $facebook->api('/me/permissions', 'GET', array('ref' => 'fbwpp')); } if (isset($fb_user) && isset($perms['data'][0]['manage_pages']) && isset($perms['data'][0]['publish_actions']) && isset($perms['data'][0]['publish_stream'])) { $author_message = get_post_meta($post_id, 'fb_author_message', true); try { //POST https://graph.facebook.com/me/news.reads?article=[article object URL] $publish_result = $facebook->api('/me/news.publishes', 'POST', array('message' => $author_message, 'article' => get_permalink($post_id))); update_post_meta($post_id, 'fb_author_post_id', sanitize_text_field($publish_result['id'])); } catch (WP_FacebookApiException $e) { $error_result = $e->getResult(); //Unset the option to publish to an author's Timeline, since the likely failure is because the admin didn't set up the proper OG action and object in their App Settings //if it's a token issue, it's because the Author hasn't auth'd the WP site yet, so don't unset the option (since that will turn it off for all authors) /*if ($e->getType() != 'OAuthException') { $options['social_publisher']['publish_to_authors_facebook_timeline'] = false; update_option( 'fb_options', $options ); }*/ $status_messages[] = array('message' => sprintf(__('Failed posting to your Facebook Timeline. Error: ' . json_encode($error_result['error']), true)), 'error' => true); } if (isset($publish_result) && isset($publish_result['id'])) { $status_messages[] = array('message' => sprintf(__('Posted to <a href="http://www.facebook.com/' . sanitize_text_field($publish_result['id']) . '" target="_blank">your Facebook Timeline</a>', false)), 'error' => false); } } $existing_status_messages = get_post_meta($post_id, 'fb_status_messages', true); if (!empty($existing_status_messages)) { $status_messages = array_merge($existing_status_messages, $status_messages); } update_post_meta($post->ID, 'fb_status_messages', $status_messages); add_filter('redirect_post_location', 'fb_add_new_post_location'); }
function fb_add_friend_mention_box_save($post_id) { // verify if this is an auto save routine. // If it is our form has not been submitted, so we dont want to do anything if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } $fb_user = fb_get_current_user(); if (!$fb_user) { return; } // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if (empty($_POST['fb_friend_mention_box_noncename']) || !wp_verify_nonce($_POST['fb_friend_mention_box_noncename'], plugin_basename(__FILE__))) { return; } // Check permissions if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) { return; } } else { if (!current_user_can('edit_post', $post_id)) { return; } } // OK, we're authenticated: we need to find and save the data if (isset($_POST) && isset($_POST['fb_page_mention_box_autocomplete'])) { $autocomplete_data = $_POST['fb_friend_mention_box_autocomplete']; preg_match_all('/\\[(.*?)\\|(.*?)\\]/su', $autocomplete_data, $friend_details, PREG_SET_ORDER); // probably using add_post_meta(), update_post_meta(), or // a custom table (see Further Reading section below) $friends_details_meta = array(); foreach ($friend_details as $friend_detail) { $friends_details_meta[] = array('id' => sanitize_text_field($friend_detail[1]), 'name' => sanitize_text_field($friend_detail[2])); } update_post_meta($post_id, 'fb_mentioned_friends', $friends_details_meta); update_post_meta($post_id, 'fb_mentioned_friends_message', sanitize_text_field($_POST['fb_friend_mention_box_message'])); } }