function sfc_getcomm_check($comments, $id)
{
    $fbpagepost = get_post_meta($id, '_fb_post_id_app', true);
    $fbprofpost = get_post_meta($id, '_fb_post_id_profile', true);
    if (!$fbpagepost && !$fbprofpost) {
        return $comments;
    }
    $options = get_option('sfc_options');
    if ($fbpagepost || $fbprofpost) {
        if (false === ($newcomms = get_transient('sfcgetcomm-' . $id))) {
            $newcomms = array();
            // get comments from the app or page post
            if ($fbpagepost) {
                if ($options['fanpage']) {
                    $token = $options['page_access_token'];
                } else {
                    $token = $options['app_access_token'];
                }
                $fbresp = sfc_remote($fbpagepost, 'comments', array('access_token' => $token));
                if (!empty($fbresp['data'])) {
                    foreach ($fbresp['data'] as $fbcomm) {
                        $nc = null;
                        $nc->comment_ID = $fbcomm['id'];
                        $nc->fbid = $fbcomm['from']['id'];
                        $nc->comment_post_ID = $id;
                        $nc->comment_author = $fbcomm['from']['name'];
                        $nc->comment_author_email = '';
                        $nc->comment_author_url = 'http://www.facebook.com/profile.php?id=' . $fbcomm['from']['id'];
                        $nc->comment_author_IP = '';
                        $time = strtotime($fbcomm['created_time']);
                        $nc->comment_date = date('Y-m-d H:i:s', $time);
                        $nc->comment_date_gmt = gmdate('Y-m-d H:i:s', $time);
                        $nc->comment_content = $fbcomm['message'];
                        $nc->comment_karma = 0;
                        $nc->comment_approved = 1;
                        $nc->comment_agent = 'SFC/1.0 (WordPress; en-US) SFC-GetComments/1.0';
                        $nc->comment_type = '';
                        $nc->comment_parent = 0;
                        $nc->user_id = 0;
                        wp_cache_add($nc->comment_ID, $nc, 'comment');
                        $newcomms[] = $nc;
                    }
                }
            }
            // get comments from the profile post
            if ($fbprofpost) {
                $token = $options['access_token'];
                $fbresp = sfc_remote($fbprofpost, 'comments', array('access_token' => $token));
                if (!empty($fbresp['data'])) {
                    foreach ($fbresp['data'] as $fbcomm) {
                        $nc = null;
                        $nc->comment_ID = $fbcomm['id'];
                        $nc->fbid = $fbcomm['from']['id'];
                        $nc->comment_post_ID = $id;
                        $nc->comment_author = $fbcomm['from']['name'];
                        $nc->comment_author_email = '';
                        $nc->comment_author_url = 'http://www.facebook.com/profile.php?id=' . $fbcomm['from']['id'];
                        $nc->comment_author_IP = '';
                        $time = strtotime($fbcomm['created_time']);
                        $nc->comment_date = date('Y-m-d H:i:s', $time);
                        $nc->comment_date_gmt = gmdate('Y-m-d H:i:s', $time);
                        $nc->comment_content = $fbcomm['message'];
                        $nc->comment_karma = 0;
                        $nc->comment_approved = 1;
                        $nc->comment_agent = 'SFC/1.0 (WordPress; en-US) SFC-GetComments/1.0';
                        $nc->comment_type = '';
                        $nc->comment_parent = 0;
                        $nc->user_id = 0;
                        wp_cache_add($nc->comment_ID, $nc, 'comment');
                        $newcomms[] = $nc;
                    }
                }
            }
            set_transient('sfcgetcomm-' . $id, $newcomms, 6 * 60 * 60);
            // 6 hours seems reasonable
        }
        global $sfc_getcomm_counts;
        $sfc_getcomm_counts[$id] = count($newcomms);
        // build a new array based on the two existing arrays
        if (!empty($newcomms)) {
            $finalcomm = array();
            while (!empty($comments) || !empty($newcomms)) {
                if (empty($comments)) {
                    $finalcomm = array_merge($finalcomm, $newcomms);
                    $newcomms = array();
                }
                if (empty($newcomms)) {
                    $finalcomm = array_merge($finalcomm, $comments);
                    $comments = array();
                }
                if (strtotime($comments[0]->comment_date) < strtotime($newcomms[0]->comment_date)) {
                    $finalcomm[] = array_shift($comments);
                } else {
                    $finalcomm[] = array_shift($newcomms);
                }
            }
            $comments = $finalcomm;
        }
    }
    return $comments;
}
 function widget($args, $instance)
 {
     extract($args);
     $title = apply_filters('widget_title', $instance['title']);
     $statuses = get_transient($this->get_field_id('statuses'));
     if ($statuses === false || !empty($statuses['error'])) {
         $statuses = sfc_remote($instance['profileid'], 'statuses', array('access_token' => $instance['access_token']));
         set_transient($this->get_field_id('statuses'), $statuses, 60 * 60);
         // 1 hour cache
     }
     if (!empty($statuses) && !empty($statuses['data'][0]['message'])) {
         $status = "<a href='http://www.facebook.com/{$statuses['data'][0]['from']['id']}/posts/{$statuses['data'][0]['id']}'>{$statuses['data'][0]['message']}</a>";
     }
     echo $before_widget;
     if ($title) {
         echo $before_title . $title . $after_title;
     }
     echo $status;
     echo $after_widget;
 }
Exemplo n.º 3
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;
    }
}
Exemplo n.º 4
0
function sfc_photo_get_photo($fbid, $code)
{
    if (false === ($photo = get_transient('sfcphoto-' . $fbid))) {
        $photo = sfc_remote($fbid, '', array('code' => $code));
        if (!empty($photo['images'])) {
            set_transient('sfcphoto-' . $fbid, $photo, 60 * 60);
        } else {
            $photo = false;
        }
    }
    return $photo;
}
function sfc_login_check($user)
{
    if (is_a($user, 'WP_User')) {
        return $user;
    }
    // check if user is already logged in, skip FB stuff
    // check for the valid cookie
    $cookie = sfc_cookie_parse();
    if (empty($cookie)) {
        return $user;
    }
    // the cookie is signed using our secret, so if we get it back from sfc_cookie_parse, then it's authenticated. So just log the user in.
    $fbuid = $cookie['user_id'];
    if ($fbuid) {
        global $wpdb;
        $user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'fbuid' AND meta_value = %s", $fbuid));
        if ($user_id) {
            $user = new WP_User($user_id);
        } else {
            $data = sfc_remote($fbuid, '', array('fields' => 'email', 'code' => $cookie['code']));
            if (!empty($data['email'])) {
                $user_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->users} WHERE user_email = %s", $data['email']));
            }
            if ($user_id) {
                $user = new WP_User($user_id);
                update_usermeta($user->ID, 'fbuid', $fbuid);
                // connect the account so we don't have to query this again
            }
            if (!$user_id) {
                do_action('sfc_login_new_fb_user');
                // TODO hook for creating new users if desired
                global $error;
                $error = '<strong>' . __('ERROR', 'sfc') . '</strong>: ' . __('Cannot log you in. There is no account on this site connected to that Facebook user identity.', 'sfc');
            }
        }
    }
    return $user;
}