function sfc_comm_fill_in_fields($comment_post_ID)
{
    if (is_user_logged_in()) {
        return;
    }
    // do nothing to WP users
    $cookie = sfc_cookie_parse();
    if (empty($cookie)) {
        return;
    }
    $uid = $cookie['uid'];
    $token = $cookie['access_token'];
    if (empty($uid) || empty($token)) {
        return;
    }
    // need both of these to get the data from FB
    $url = "https://graph.facebook.com/{$uid}/?fields=name,email,website&access_token={$token}";
    $data = wp_remote_get($url);
    if (!is_wp_error($data)) {
        $json = json_decode($data['body'], true);
        if ($json) {
            $_POST['author'] = $json['name'];
            $_POST['url'] = $json['website'];
            $_POST['email'] = $json['email'];
        }
    }
}
function sfc_remote($obj, $connection = '', $args = array(), $type = 'GET')
{
    $type = strtoupper($type);
    if (empty($obj)) {
        return null;
    }
    if (empty($args['access_token'])) {
        $cookie = sfc_cookie_parse();
        if (!empty($cookie['access_token'])) {
            $args['access_token'] = $cookie['access_token'];
        }
    }
    $url = 'https://graph.facebook.com/' . $obj;
    if (!empty($connection)) {
        $url .= '/' . $connection;
    }
    if ($type == 'GET') {
        $url .= '?access_token=' . $args['access_token'];
    }
    if ($type == 'POST') {
        $data = wp_remote_post($url, $args);
    } else {
        if ($type == 'GET') {
            $data = wp_remote_get($url, $args);
        }
    }
    if ($data && !is_wp_error($data)) {
        $resp = json_decode($data['body'], true);
        return $resp;
    }
    return false;
}
Example #3
0
function media_sfc_photos_form($errors)
{
    global $redir_tab, $type;
    $redir_tab = 'sfcphotos';
    media_upload_header();
    $post_id = intval($_REQUEST['post_id']);
    $user = sfc_cookie_parse();
    if (!isset($user['user_id'])) {
        ?>
<p><?php 
        _e("You don't appear to be logged into Facebook. Click the button below to login and grant photo access.", 'sfc');
        ?>
</p>
		<fb:login-button v="2" scope="offline_access,user_photos" onlogin="******"><?php 
        _e('Connect with Facebook', 'sfc');
        ?>
</fb:login-button><?php 
    }
    if (isset($_GET['send']) && !preg_match('/^[0-9]+$/i', $_GET['send'])) {
        // photo ids are bigints
        unset($_GET['send']);
    }
    if (isset($_GET['send'])) {
        $send_id = $_GET['send'];
        $photo = sfc_photo_get_photo($send_id, $user['code']);
        $photo = apply_filters('sfc_photo_insert', $photo);
        list($width, $height) = image_constrain_size_for_editor($photo['width'], $photo['height'], 'large');
        $alt = '';
        if (!empty($photo['name'])) {
            $alt = esc_attr($photo['name']);
        }
        $html = "<a href='{$photo['link']}'><img src='{$photo['source']}' alt='{$alt}' width='{$width}' height='{$height}' class='size-full fb-image-{$photo['id']}'/></a>";
        if (!empty($photo['name'])) {
            $html = "[caption id='fb_attachment_{$photo['id']}' width='{$width}' caption='{$alt}']" . $html . '[/caption]';
        }
        return media_send_to_editor($html);
    }
    if (!empty($_GET['album']) && !preg_match('/^[0-9]+$/i', $_GET['album'])) {
        // album ids are bigints
        unset($_GET['album']);
    }
    if (!empty($_GET['album'])) {
        // show an album
        $album = $_GET['album'];
        if (false === ($photos = get_transient('sfcphotos-' . $album))) {
            $photos = sfc_remote($album, 'photos', array('code' => $user['code'], 'timeout' => 60, 'limit' => 0));
            if ($photos === false) {
                ?>
<p><?php 
                _e('Facebook is being really, really slow and not responding to requests in a reasonable period of time. Try again later.', 'sfc');
                ?>
</p><?php 
                return;
            }
            // cache the data because Facebook's Graph API is slow as hell
            if (!empty($photos)) {
                set_transient('sfcphotos-' . $album, $photos, 6 * 60 * 60);
            }
            // 6 hours
        }
        if (empty($photos['data'])) {
            ?>
<p><?php 
            _e('This album appears to be empty', 'sfc');
            ?>
</p><?php 
            $link = admin_url("media-upload.php?post_id={$post_id}&type={$type}&tab={$redir_tab}");
            echo "<p><a href='{$link}'>" . __('Go Back', 'sfc') . "</a></p>";
            return;
        }
        $photos = $photos['data'];
        $link = admin_url("media-upload.php?post_id={$post_id}&type={$type}&tab={$redir_tab}");
        echo "<p><a href='{$link}'>" . __('Go Back', 'sfc') . "</a></p>";
        echo '<table><tr>';
        $i = 1;
        foreach ($photos as $photo) {
            echo '<td>';
            $link = admin_url("media-upload.php?post_id={$post_id}&type={$type}&tab={$redir_tab}&album={$album['id']}&send={$photo['id']}");
            echo "<p><a href='{$link}'><img src='{$photo['picture']}' /></a></p>";
            echo '</td>';
            if ($i % 3 == 0) {
                echo '</tr><tr>';
            }
            $i++;
        }
        echo '</tr></table>';
    } else {
        if (false === ($albums = get_transient('sfcphotos-' . $user['user_id']))) {
            $albums = sfc_remote($user['user_id'], 'albums', array('code' => $user['code'], 'timeout' => 60, 'limit' => 0));
            if ($albums === false) {
                ?>
<p><?php 
                _e('Facebook is being really, really slow and not responding to requests in a reasonable period of time. Try again later.', 'sfc');
                ?>
</p><?php 
                return;
            }
            // cache the data because Facebook's Graph API is slow as hell
            if (!empty($albums['data'])) {
                set_transient('sfcphotos-' . $user['user_id'], $albums, 6 * 60 * 60);
            }
            // 6 hours
        }
        if (empty($albums['data'])) {
            ?>
<p><?php 
            _e('Either you have no photo albums on Facebook, or you have not granted the site permission to access them. Either way, click the button below to login and grant access.', 'sfc');
            ?>
</p>
			<fb:login-button v="2" scope="offline_access,user_photos" onlogin="******"><?php 
            _e('Connect with Facebook', 'sfc');
            ?>
</fb:login-button><?php 
        } else {
            $albums = $albums['data'];
            echo '<table><tr>';
            $i = 1;
            foreach ($albums as $album) {
                echo '<td>';
                $link = admin_url("media-upload.php?post_id={$post_id}&type={$type}&tab={$redir_tab}&album={$album['id']}");
                // retrieve the cover image for the album
                if (false !== ($photo = sfc_photo_get_photo($album['cover_photo'], $user['code']))) {
                    echo "<p><a href='{$link}'><img src='{$photo['picture']}' /></a></p>";
                } else {
                    // TODO cover not available
                }
                echo "<p><a href='{$link}'>{$album['name']}</a></p>";
                echo '</td>';
                if ($i % 3 == 0) {
                    echo '</tr><tr>';
                }
                $i++;
            }
            echo '</tr></table>';
        }
    }
}
Example #4
0
function sfc_publish_validate_options($input)
{
    $options = get_option('sfc_options');
    if (isset($input['autopublish_app']) && $input['autopublish_app'] != 1) {
        $input['autopublish_app'] = 0;
    }
    if (isset($input['autopublish_profile']) && $input['autopublish_profile'] != 1) {
        $input['autopublish_profile'] = 0;
    }
    unset($input['user']);
    unset($input['page_access_token']);
    unset($input['app_access_token']);
    // find the access token and save it if it's there
    $cookie = sfc_cookie_parse();
    if ($input['access_token']) {
        $input['user'] = $cookie['user_id'];
        // for fan pages, we need to go get their access token
        if ($input['fanpage']) {
            // connect to FB, find a list of the available Pages
            $data = wp_remote_get("https://graph.facebook.com/{$input['user']}/accounts?access_token={$input['access_token']}", array('sslverify' => 0));
            if (!is_wp_error($data)) {
                $pages = json_decode($data['body'], true);
                if (is_array($pages) && isset($pages['data'])) {
                    foreach ($pages['data'] as $page) {
                        if ($page['id'] == $input['fanpage']) {
                            $input['page_access_token'] = $page['access_token'];
                            break;
                        }
                    }
                }
            }
        }
        // get application access token
        $data = wp_remote_get("https://graph.facebook.com/oauth/access_token?client_id={$input['appid']}&client_secret={$input['app_secret']}&type=client_cred", array('sslverify' => 0));
        if (!is_wp_error($data)) {
            $token = $data['body'];
            if (strpos($token, 'access_token=') !== false) {
                $input['app_access_token'] = str_replace('access_token=', '', $token);
            }
        }
    }
    return $input;
}
Example #5
0
function sfc_is_fan($pageid = '0')
{
    $user = sfc_cookie_parse();
    if (!isset($user['user_id'])) {
        return false;
        // user isn't "connected", so we don't know who they are, so we can't check to see if they're a fan
    }
    $options = get_option('sfc_options');
    if ($pageid == '0') {
        if (!empty($options['fanpage'])) {
            $pageid = $options['fanpage'];
        } else {
            $pageid = $options['appid'];
        }
    }
    if (!empty($options['fanpage'])) {
        $token = $options['page_access_token'];
    } else {
        $token = $options['app_access_token'];
    }
    $fbresp = sfc_remote($user['user_id'], "likes/{$pageid}", array('access_token' => $token));
    if (isset($fbresp['data'][0]['name'])) {
        return true;
    } else {
        return false;
    }
}
function sfc_login_logout()
{
    // check for FB cookies, if not found, do nothing
    $cookie = sfc_cookie_parse();
    if (empty($cookie)) {
        return;
    }
    // we have an FB login, log them out with a redirect
    add_action('sfc_async_init', 'sfc_login_logout_js');
    ?>
	<html><head></head><body>
	<?php 
    sfc_add_base_js();
    ?>
	</body></html>
<?php 
    exit;
}
Example #7
0
function sfc_login_logout()
{
    $options = get_option('sfc_options');
    // check for FB cookies, if not found, do nothing
    $cookie = sfc_cookie_parse();
    if (empty($cookie)) {
        return;
    }
    // force remove the cookie, since FB can't be relied on to do it properly
    $domain = '.' . parse_url(home_url('/'), PHP_URL_HOST);
    setcookie('fbsr_' . $options['appid'], ' ', time() - 31536000, "/", $domain);
    // we have an FB login, log them out with a redirect
    add_action('sfc_async_init', 'sfc_login_logout_js');
    ?>
	<html><head></head><body>
	<?php 
    sfc_add_base_js(array('cookie' => false));
    ?>
	</body></html>
<?php 
    exit;
}