/**
 * 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');
}
Exemple #2
0
/**
 * Add Open Graph protocol markup to <head>
 *
 * @since 1.0
 */
function fb_add_og_protocol()
{
    global $post;
    $meta_tags = array('http://ogp.me/ns#locale' => fb_get_locale(), 'http://ogp.me/ns#site_name' => get_bloginfo('name'), 'http://ogp.me/ns#type' => 'website');
    if (is_home() || is_front_page()) {
        $meta_tags['http://ogp.me/ns#title'] = get_bloginfo('name');
        $meta_tags['http://ogp.me/ns#description'] = get_bloginfo('description');
    } else {
        if (is_single()) {
            $post_type = get_post_type();
            $meta_tags['http://ogp.me/ns#type'] = 'article';
            $meta_tags['http://ogp.me/ns#url'] = apply_filters('rel_canonical', get_permalink());
            if (post_type_supports($post_type, 'title')) {
                $meta_tags['http://ogp.me/ns#title'] = get_the_title();
            }
            if (post_type_supports($post_type, 'excerpt')) {
                // thanks to Angelo Mandato (http://wordpress.org/support/topic/plugin-facebook-plugin-conflicts-with-powerpress?replies=16)
                // Strip and format the wordpress way, but don't apply any other filters which adds junk that ends up getitng stripped back out
                if (!post_password_required($post)) {
                    // First lets get the post excerpt (shouldn't have any html, but anyone can enter anything...)
                    $meta_tags['http://ogp.me/ns#description'] = fb_strip_and_format_desc($post);
                }
            }
            $meta_tags['http://ogp.me/ns/article#published_time'] = get_the_date('c');
            $meta_tags['http://ogp.me/ns/article#modified_time'] = get_the_modified_date('c');
            if (post_type_supports($post_type, 'author') && isset($post->post_author)) {
                $meta_tags['http://ogp.me/ns/article#author'] = get_author_posts_url($post->post_author);
            }
            // add the first category as a section. all other categories as tags
            $cat_ids = get_the_category();
            if (!empty($cat_ids)) {
                $cat = get_category($cat_ids[0]);
                if (!empty($cat)) {
                    $meta_tags['http://ogp.me/ns/article#section'] = $cat->name;
                }
                //output the rest of the categories as tags
                unset($cat_ids[0]);
                if (!empty($cat_ids)) {
                    $meta_tags['http://ogp.me/ns/article#tag'] = array();
                    foreach ($cat_ids as $cat_id) {
                        $cat = get_category($cat_id);
                        $meta_tags['http://ogp.me/ns/article#tag'][] = $cat->name;
                        unset($cat);
                    }
                }
            }
            // add tags. treat tags as lower priority than multiple categories
            $tags = get_the_tags();
            if ($tags) {
                if (!array_key_exists('http://ogp.me/ns/article#tag', $meta_tags)) {
                    $meta_tags['http://ogp.me/ns/article#tag'] = array();
                }
                foreach ($tags as $tag) {
                    $meta_tags['http://ogp.me/ns/article#tag'][] = $tag->name;
                }
            }
            // does current post type and the current theme support post thumbnails?
            if (post_type_supports($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');
                if (!empty($post_thumbnail_url)) {
                    $image = array('url' => $post_thumbnail_url);
                    if (!empty($post_thumbnail_width)) {
                        $image['width'] = absint($post_thumbnail_width);
                    }
                    if (!empty($post_thumbnail_height)) {
                        $image['height'] = absint($post_thumbnail_height);
                    }
                    $meta_tags['http://ogp.me/ns#image'] = array($image);
                }
            }
        } else {
            if (is_author() && isset($post->post_author)) {
                $meta_tags['http://ogp.me/ns#type'] = 'profile';
                $meta_tags['http://ogp.me/ns/profile#first_name'] = get_the_author_meta('first_name', $post->post_author);
                $meta_tags['http://ogp.me/ns/profile#last_name'] = get_the_author_meta('last_name', $post->post_author);
                if (is_multi_author()) {
                    $meta_tags['http://ogp.me/ns/profile#username'] = get_the_author_meta('login', $post->post_author);
                }
            } else {
                if (is_page()) {
                    $meta_tags['http://ogp.me/ns#type'] = 'article';
                    $meta_tags['http://ogp.me/ns#title'] = get_the_title();
                    $meta_tags['http://ogp.me/ns#url'] = apply_filters('rel_canonical', get_permalink());
                }
            }
        }
    }
    $options = get_option('fb_options');
    if (!empty($options['app_id'])) {
        $meta_tags['http://ogp.me/ns/fb#app_id'] = $options['app_id'];
    }
    $meta_tags = apply_filters('fb_meta_tags', $meta_tags, $post);
    foreach ($meta_tags as $property => $content) {
        fb_output_og_protocol($property, $content);
    }
}