コード例 #1
0
ファイル: sfc-publish.php プロジェクト: TheReaCompany/pooplog
function sfc_publish_automatic($id, $post)
{
    // check to make sure post is published
    if ($post->post_status !== 'publish') {
        return;
    }
    // check options to see if we need to send to FB at all
    $options = get_option('sfc_options');
    if (!$options['autopublish_app'] && !$options['autopublish_profile']) {
        return;
    }
    // load facebook platform
    include_once 'facebook-platform/facebook.php';
    $fb = new Facebook($options['api_key'], $options['app_secret']);
    // to do this autopublish, we might need to switch users
    if ($options['user'] && $options['session_key']) {
        $tempuser = $fb->user;
        $tempkey = $fb->api_client->session_key = $session_key;
        $fb->set_user($options['user'], $options['session_key']);
    } else {
        return;
        // safety net: if we don't have a user and session key, we can't publish properly.
    }
    // build the post to send to FB
    // apply the content filters, in case some plugin is doing weird image stuff
    $content = apply_filters('the_content', $post->post_content);
    // look for the images to add with image_src
    $images = array();
    // get the post thumbnail, put it first in the image list
    if (current_theme_supports('post-thumbnails')) {
        if (has_post_thumbnail($post->ID)) {
            $thumbid = get_post_thumbnail_id($post->ID);
            $att = wp_get_attachment_image_src($thumbid, 'full');
            $images[] = $att[0];
        }
    }
    // look for any images in the content
    if (preg_match_all('/<img (.+?)>/', $content, $matches)) {
        foreach ($matches[1] as $match) {
            foreach (wp_kses_hair($match, array('http')) as $attr) {
                $img[$attr['name']] = $attr['value'];
            }
            if (isset($img['src'])) {
                if (isset($img['class']) && false === strpos($img['class'], 'wp-smiley')) {
                    // ignore smilies
                    $images[] = $img['src'];
                }
            }
        }
    }
    // build the attachment
    $permalink = get_permalink($post->ID);
    $attachment['name'] = $post->post_title;
    $attachment['href'] = $permalink;
    $attachment['description'] = sfc_publish_make_excerpt($post->post_content);
    $attachment['comments_xid'] = urlencode($permalink);
    // image attachments (up to 5, as that's all FB allows)
    $count = 0;
    foreach ($images as $image) {
        $attachment['media'][$count]['type'] = 'image';
        $attachment['media'][$count]['src'] = $image;
        $attachment['media'][$count]['href'] = $permalink;
        $count++;
        if ($count == 5) {
            break;
        }
    }
    // Read Post link
    $action_links[0]['text'] = 'Read Post';
    $action_links[0]['href'] = $permalink;
    // Link to comments
    $action_links[1]['text'] = 'See Comments';
    $action_links[1]['href'] = get_comments_link($post->ID);
    // publish to page
    if ($options['autopublish_app'] && !get_post_meta($id, '_fb_post_id_app', true) && $options['fanpage']) {
        if ($options['fanpage']) {
            $who = $options['fanpage'];
        } else {
            $who = $options['appid'];
        }
        // check to see if we can send to FB at all
        $result = $fb->api_client->users_hasAppPermission('publish_stream', $who);
        if (!$result) {
            break;
        }
        $fb_post_id = $fb->api_client->stream_publish(null, json_encode($attachment), json_encode($action_links), null, $who);
        if ($fb_post_id) {
            // update the post id so as to prevent automatically posting it twice
            update_post_meta($id, '_fb_post_id_app', $fb_post_id);
        }
    }
    // publish to profile
    if ($options['autopublish_profile'] && !get_post_meta($id, '_fb_post_id_profile', true)) {
        // check to see if we can send to FB at all
        $result = $fb->api_client->users_hasAppPermission('publish_stream');
        if (!$result) {
            break;
        }
        $fb_post_prof_id = $fb->api_client->stream_publish(null, json_encode($attachment), json_encode($action_links));
        if ($fb_post_prof_id) {
            // update the post id so as to prevent automatically posting it twice
            update_post_meta($id, '_fb_post_id_profile', $fb_post_prof_id);
        }
    }
    // switch users back, just in case
    if ($tempuser) {
        $fb->set_user($tempuser, $tempkey);
    }
}
コード例 #2
0
function sfc_publish_automatic($id, $post)
{
    // check to make sure post is published
    if ($post->post_status !== 'publish') {
        return;
    }
    // check options to see if we need to send to FB at all
    $options = get_option('sfc_options');
    if (!$options['autopublish_app'] && !$options['autopublish_profile']) {
        return;
    }
    // build the post to send to FB
    // apply the content filters, in case some plugin is doing weird image stuff
    $content = apply_filters('the_content', $post->post_content);
    // look for the images to add with image_src
    $image = null;
    // get the post thumbnail, put it first in the image list
    if (current_theme_supports('post-thumbnails')) {
        if (has_post_thumbnail($post->ID)) {
            $thumbid = get_post_thumbnail_id($post->ID);
            $att = wp_get_attachment_image_src($thumbid, 'full');
            if (!empty($att[0])) {
                $image = $att[0];
            }
        }
    }
    // look for any images in the content
    if (!$image && preg_match_all('/<img (.+?)>/i', $content, $matches)) {
        foreach ($matches[1] as $match) {
            foreach (wp_kses_hair($match, array('http')) as $attr) {
                $img[strtolower($attr['name'])] = $attr['value'];
            }
            if (isset($img['src'])) {
                if (isset($img['class']) && false === straipos($img['class'], apply_filters('sfc_img_exclude', array('wp-smiley')))) {
                    // ignore smilies
                    $image = $img['src'];
                    break;
                }
            }
        }
    }
    // build the attachment
    $permalink = apply_filters('sfc_publish_permalink', wp_get_shortlink($post->ID), $post->ID);
    $attachment['name'] = $post->post_title;
    $attachment['link'] = $permalink;
    if (!empty($post->post_excerpt)) {
        $attachment['description'] = sfc_publish_make_excerpt($post->post_excerpt);
    } else {
        $attachment['description'] = sfc_publish_make_excerpt($post->post_content);
    }
    if (!empty($image)) {
        $attachment['picture'] = $image;
    }
    // Actions
    $actions[0]['name'] = 'Share';
    $actions[0]['link'] = 'http://www.facebook.com/share.php?u=' . urlencode($permalink);
    $attachment['actions'] = $actions;
    // publish to app or page
    if ($options['autopublish_app'] && !get_post_meta($id, '_fb_post_id_app', true)) {
        if ($options['fanpage']) {
            $url = "https://graph.facebook.com/{$options['fanpage']}/feed";
            $attachment['access_token'] = $options['page_access_token'];
        } else {
            $url = "https://graph.facebook.com/{$options['appid']}/feed";
            $attachment['access_token'] = $options['app_access_token'];
        }
        $data = wp_remote_post($url, array('body' => http_build_query($attachment)));
        if (!is_wp_error($data)) {
            $resp = json_decode($data['body'], true);
            if ($resp['id']) {
                update_post_meta($id, '_fb_post_id_app', $resp['id']);
            }
        }
    }
    // publish to profile
    if ($options['autopublish_profile'] && !get_post_meta($id, '_fb_post_id_profile', true)) {
        $url = "https://graph.facebook.com/{$options['user']}/feed";
        // check the cookie for an access token. If not found, try to use the stored one.
        $cookie = sfc_cookie_parse();
        if ($cookie['access_token']) {
            $attachment['access_token'] = $cookie['access_token'];
        } else {
            $attachment['access_token'] = $options['access_token'];
        }
        $data = wp_remote_post($url, array('body' => http_build_query($attachment)));
        if (!is_wp_error($data)) {
            $resp = json_decode($data['body'], true);
            if ($resp['id']) {
                update_post_meta($id, '_fb_post_id_profile', $resp['id']);
            }
        }
    }
}