/** * Get posts * * @return array */ public function get_posts() { // try to get posts from cache $posts = json_decode(get_transient('rfbp_posts'), true); if (is_array($posts)) { return $posts; } // fetch posts from FB API $api = rfbp_get_api(); $posts = $api->get_posts(); if (is_array($posts)) { // API call succeeded, store posts in cache // JSON encode posts before storing $encoded_posts = json_encode($posts); set_transient('rfbp_posts', $encoded_posts, apply_filters('rfbp_cache_time', 3600)); // user set cache time set_transient('rfbp_posts_fallback', $encoded_posts, 2629744); // 1 month return $posts; } // API call failed, get posts from fallback transient $posts = json_decode(get_transient('rfbp_posts_fallback'), true); // If fallback also failed, return empty array if (is_array($posts)) { return $posts; } return array(); }
public function test_facebook_api() { $api = rfbp_get_api(); $ping = $api->ping(); if ($ping) { add_settings_error('rfbp', 'rfbp-api-success', __('Your configuration seems to be okay and working! Nice work.', 'recent-facebook-posts'), "updated"); } else { add_settings_error('rfbp', 'rfbp-api-error', __('Facebook returned the following error when testing your configuration.', 'recent-facebook-posts') . '<pre>' . $api->get_error_message() . '</pre>'); } }
public function get_posts() { // try to get posts from cache if ($posts = get_transient('rfbp_posts')) { return $posts; } $opts = rfbp_get_settings(); $api = rfbp_get_api(); $data = $api->get_posts(); // did api call succeed? if (!$data) { return $this->get_fallback_posts(); } $posts = array(); foreach ($data as $p) { // skip this "post" if it is not of one of the following types if (!in_array($p->type, array('status', 'photo', 'video', 'link'))) { continue; } // skip empty status updates if ($p->type == 'status' && (!isset($p->message) || empty($p->message))) { continue; } // skip empty links. if ($p->type == 'link' && !isset($p->name) && (!isset($p->message) || empty($p->message))) { continue; } // skip friend approvals if ($p->type == 'status' && $p->status_type == 'approved_friend') { continue; } //split user and post ID (userID_postID) $idArray = explode("_", $p->id); $post = array(); $post['type'] = $p->type; $post['content'] = isset($p->message) ? utf8_encode($p->message) : ''; $post['image'] = null; // set post content and image if ($p->type == 'photo') { $image = "//graph.facebook.com/" . $p->object_id . '/picture'; $post['image'] = $image; } elseif ($p->type == 'video') { $image = $p->picture; // hacky if ($opts['img_size'] == 'normal') { $image = str_replace(array("_s.jpg", "_s.png"), array("_n.jpg", "_n.png"), $image); } $post['image'] = $image; } elseif ($p->type == 'link') { $post['link_image'] = isset($p->picture) ? $p->picture : ''; $post['link_name'] = isset($p->name) ? $p->name : ''; $post['link_caption'] = isset($p->caption) ? $p->caption : ''; $post['link_description'] = isset($p->description) ? $p->description : ''; $post['link_url'] = $p->link; } // calculate post like and comment counts if (isset($p->likes->summary->total_count)) { $like_count = $p->likes->summary->total_count; } else { $like_count = 0; } if (isset($p->comments->summary->total_count)) { $comment_count = $p->comments->summary->total_count; } else { $comment_count = 0; } $post['timestamp'] = strtotime($p->created_time); $post['like_count'] = $like_count; $post['comment_count'] = $comment_count; $post['url'] = "http://www.facebook.com/" . $opts['fb_id'] . "/posts/" . $idArray[1]; $posts[] = $post; } // store results in cache for later use if ($posts) { set_transient('rfbp_posts', $posts, apply_filters('rfbp_cache_time', 3600)); // user set cache time set_transient('rfbp_posts_fallback', $posts, 2629744); // 1 month } return $posts; }