function rotatingtweets_get_tweets($tw_screen_name, $tw_include_rts, $tw_exclude_replies, $tw_get_favorites = FALSE, $tw_search = FALSE, $tw_list = FALSE) { # Set timer $rt_starttime = microtime(true); # Check cache rotatingtweets_shrink_cache(); # Clear up variables $tw_screen_name = trim(remove_accents(str_replace('@', '', $tw_screen_name))); if ($tw_list) { $tw_list = strtolower(sanitize_file_name($tw_list)); } if (empty($tw_search)) { $possibledividers = array(' ', ';', ','); $rt_namesarray = false; foreach ($possibledividers as $possibledivider) { if (strpos($tw_screen_name, $possibledivider) !== false) { $rt_namesarray = explode(' ', $tw_screen_name); $tw_search = 'from:' . implode(' OR from:', $rt_namesarray); } } } else { $tw_search = trim($tw_search); } $cacheoption = get_option('rotatingtweets-api-settings'); if (!isset($cacheoption['cache_delay'])) { $cache_delay = 120; } else { $cache_delay = max(60, intval($cacheoption['cache_delay'])); } if ($tw_include_rts != 1) { $tw_include_rts = 0; } if ($tw_exclude_replies != 1) { $tw_exclude_replies = 0; } # Get the option strong if ($tw_search) { $stringname = 'search-' . $tw_include_rts . $tw_exclude_replies . '-' . sanitize_file_name($tw_search); } elseif ($tw_get_favorites) { $stringname = $tw_screen_name . $tw_include_rts . $tw_exclude_replies . 'favorites'; } elseif ($tw_list) { $stringname = $tw_screen_name . $tw_include_rts . $tw_exclude_replies . 'list-' . $tw_list; } else { $stringname = $tw_screen_name . $tw_include_rts . $tw_exclude_replies; } $optionname = "rotatingtweets-cache"; $option = get_option($optionname); # Attempt to deal with 'Cannot use string offset as an array' error $timegap = $cache_delay + 1; if (is_array($option)) { if (WP_DEBUG) { echo "\n<!-- var option is an array -->"; } if (isset($option[$stringname]['json'][0])) { if (WP_DEBUG) { echo "<!-- option[{$stringname}] exists -->"; } if (is_array($option[$stringname]['json'][0])) { $latest_json = $option[$stringname]['json']; $latest_json_date = $option[$stringname]['datetime']; $timegap = time() - $latest_json_date; if (WP_DEBUG) { echo "<!-- option[{$stringname}]['json'][0] is an array - {$timegap} seconds since last load -->"; } } elseif (is_object($option[$stringname]['json'][0])) { if (WP_DEBUG) { echo "<!-- option[{$stringname}]['json'][0] is an object -->"; } unset($option[$stringname]); } else { if (WP_DEBUG) { echo "<!-- option[{$stringname}]['json'][0] is neither an object nor an array! -->"; } unset($option[$stringname]); } } elseif (WP_DEBUG) { echo "<!-- option[{$stringname}] does not exist -->"; } } else { if (WP_DEBUG) { echo "\n<!-- var option is NOT an array -->"; } unset($option); } # Checks if it is time to call Twitter directly yet or if it should use the cache if ($timegap > $cache_delay) { $apioptions = array('screen_name' => $tw_screen_name, 'include_entities' => 1, 'count' => 40, 'include_rts' => $tw_include_rts, 'exclude_replies' => $tw_exclude_replies); if ($tw_search) { $apioptions['q'] = $tw_search; // $apioptions['result_type']='recent'; $twitterdata = rotatingtweets_call_twitter_API('search/tweets', $apioptions); } elseif ($tw_get_favorites) { $twitterdata = rotatingtweets_call_twitter_API('favorites/list', $apioptions); } elseif ($tw_list) { unset($apioptions['screen_name']); $apioptions['slug'] = $tw_list; $apioptions['owner_screen_name'] = $tw_screen_name; $twitterdata = rotatingtweets_call_twitter_API('lists/statuses', $apioptions); } else { $twitterdata = rotatingtweets_call_twitter_API('statuses/user_timeline', $apioptions); } if (!is_wp_error($twitterdata)) { $twitterjson = json_decode($twitterdata['body'], TRUE); if (WP_DEBUG) { $rt_time_taken = number_format(microtime(true) - $rt_starttime, 4); echo "<!-- Rotating Tweets - got new data - time taken: {$rt_time_taken} seconds -->"; } } else { set_transient('rotatingtweets_wp_error', $twitterdata->get_error_messages(), 120); } } elseif (WP_DEBUG) { $rt_time_taken = number_format(microtime(true) - $rt_starttime, 4); echo "<!-- Rotating Tweets - used cache - " . ($cache_delay - $timegap) . " seconds remaining - time taken: {$rt_time_taken} seconds -->"; } # Checks for errors in the reply if (!empty($twitterjson['errors'])) { # If there's an error, reset the cache timer to make sure we don't hit Twitter too hard and get rate limited. // print_r($twitterjson); if ($twitterjson['errors'][0]['code'] == 88) { $rate = rotatingtweets_get_rate_data(); if ($rate && $rate['remaining_hits'] == 0) { $option[$stringname]['datetime'] = $rate['reset_time_in_seconds'] - $cache_delay + 1; update_option($optionname, $option); } else { $option[$stringname]['datetime'] = time(); update_option($optionname, $option); } } else { $option[$stringname]['datetime'] = time(); update_option($optionname, $option); } } elseif (!empty($twitterjson['error'])) { # If Twitter is being rate limited, delays the next load until the reset time # For some reason the rate limiting error has a different error variable! $rate = rotatingtweets_get_rate_data(); if ($rate && $rate['remaining_hits'] == 0) { $option[$stringname]['datetime'] = $rate['reset_time_in_seconds'] - $cache_delay + 1; update_option($optionname, $option); } } elseif (!empty($twitterjson)) { unset($firstentry); if (isset($twitterjson['statuses'])) { if (WP_DEBUG) { echo "<!-- using [statuses] -->"; } $twitterjson = $twitterjson['statuses']; } elseif (isset($twitterjson['results'])) { if (WP_DEBUG) { echo "<!-- using [results] -->"; } $twitterjson = $twitterjson['results']; } if (is_array($twitterjson) && isset($twitterjson[0])) { $firstentry = $twitterjson[0]; } if (!empty($firstentry['text'])) { $latest_json = rotatingtweets_shrink_json($twitterjson); $option[$stringname]['json'] = $latest_json; $option[$stringname]['datetime'] = time(); if (WP_DEBUG) { echo "<!-- Storing cache entry for {$stringname} in {$optionname} -->"; } update_option($optionname, $option); } } if (isset($latest_json)) { return $latest_json; } else { return; } }
function rotatingtweets_get_tweets_sf($tw_screen_name, $tw_include_rts, $tw_exclude_replies, $tw_get_favorites = FALSE, $tw_search = FALSE, $tw_list = FALSE, $tw_merge = TRUE, $tw_collection = FALSE) { # Set timer $rt_starttime = microtime(true); # Clear up variables $tw_screen_name = trim(remove_accents(str_replace('@', '', $tw_screen_name))); if ($tw_list) { $tw_list = strtolower(sanitize_file_name($tw_list)); } if (!empty($tw_search)) { $tw_search = trim($tw_search); } if ($tw_collection) { $tw_collection = trim($tw_collection); } $cache_delay = rotatingtweets_get_cache_delay(); if ($tw_include_rts != 1) { $tw_include_rts = 0; } if ($tw_exclude_replies != 1) { $tw_exclude_replies = 0; } # Get the option strong if ($tw_search) { $stringname = 'search-' . $tw_include_rts . $tw_exclude_replies . '-' . $tw_search; } elseif ($tw_collection) { $stringname = 'collection-' . $tw_collection; } elseif ($tw_get_favorites) { $stringname = $tw_screen_name . $tw_include_rts . $tw_exclude_replies . 'favorites'; } elseif ($tw_list) { $stringname = $tw_screen_name . $tw_include_rts . $tw_exclude_replies . 'list-' . $tw_list; } else { $stringname = $tw_screen_name . $tw_include_rts . $tw_exclude_replies; } $transientname = substr('rtc-' . sanitize_file_name($stringname), 0, 45); $option = rotatingtweets_get_transient($transientname); if (WP_DEBUG && !$option) { echo "<!-- Option failed to load -->"; echo "<!-- Option \n"; print_r($option); echo " -->"; } # Attempt to deal with 'Cannot use string offset as an array' error $timegap = $cache_delay + 1; if (is_array($option)) { if (isset($option[$stringname]['json'][0])) { if (WP_DEBUG) { echo "<!-- option[{$stringname}] exists -->"; } if (is_array($option[$stringname]['json'][0])) { $latest_json = $option[$stringname]['json']; $latest_json_date = $option[$stringname]['datetime']; $timegap = time() - $latest_json_date; if (WP_DEBUG) { echo "<!-- option[{$stringname}]['json'][0] is an array - {$timegap} seconds since last load -->"; } } elseif (is_object($option[$stringname]['json'][0])) { if (WP_DEBUG) { echo "<!-- option[{$stringname}]['json'][0] is an object -->"; } unset($option[$stringname]); } else { if (WP_DEBUG) { echo "<!-- option[{$stringname}]['json'][0] is neither an object nor an array! -->"; } unset($option[$stringname]); } } elseif (WP_DEBUG) { echo "<!-- option[{$stringname}] does not exist -->"; } } else { if (WP_DEBUG) { echo "\n<!-- var option is NOT an array\n"; print_r($option); echo "\n-->"; } unset($option); } # Checks if it is time to call Twitter directly yet or if it should use the cache if ($timegap > $cache_delay) { $apioptions = array('screen_name' => $tw_screen_name, 'include_entities' => 1, 'count' => 40, 'include_rts' => $tw_include_rts, 'exclude_replies' => $tw_exclude_replies); $twitterusers = FALSE; if ($tw_search) { $apioptions['q'] = $tw_search; // $apioptions['result_type']='recent'; $twitterdata = rotatingtweets_call_twitter_API('search/tweets', $apioptions); } elseif ($tw_collection) { $twitterdata = rotatingtweets_call_twitter_API('collections/entries', $apioptions); $twitterusers = rotatingtweets_call_twitter_API('collections/show', $apioptions); } elseif ($tw_get_favorites) { $twitterdata = rotatingtweets_call_twitter_API('favorites/list', $apioptions); } elseif ($tw_list) { unset($apioptions['screen_name']); $apioptions['slug'] = $tw_list; $apioptions['owner_screen_name'] = $tw_screen_name; $twitterdata = rotatingtweets_call_twitter_API('lists/statuses', $apioptions); } else { $twitterdata = rotatingtweets_call_twitter_API('statuses/user_timeline', $apioptions); } if (!is_wp_error($twitterdata)) { if ($twitterusers) { $twitterjson = rotatingtweets_transform_collection_data($twitterdata, $twitterusers); } else { $twitterjson = json_decode($twitterdata['body'], TRUE); } if (WP_DEBUG) { $rt_time_taken = number_format(microtime(true) - $rt_starttime, 4); echo "<!-- Rotating Tweets - got new data - time taken: {$rt_time_taken} seconds -->"; } } else { rotatingtweets_set_transient('rotatingtweets_wp_error', $twitterdata->get_error_messages(), 120); } } elseif (WP_DEBUG) { $rt_time_taken = number_format(microtime(true) - $rt_starttime, 4); echo "<!-- Rotating Tweets - used cache - " . ($cache_delay - $timegap) . " seconds remaining - time taken: {$rt_time_taken} seconds -->"; } # Checks for errors in the reply if (!empty($twitterjson['errors'])) { # If there's an error, reset the cache timer to make sure we don't hit Twitter too hard and get rate limited. // print_r($twitterjson); if ($twitterjson['errors'][0]['code'] == 88) { $rate = rotatingtweets_get_rate_data(); if ($rate && $rate['remaining_hits'] == 0) { $option[$stringname]['datetime'] = $rate['reset_time_in_seconds'] - $cache_delay + 1; rotatingtweets_set_transient($transientname, $option, 60 * 60 * 24 * 7); } else { $option[$stringname]['datetime'] = time(); rotatingtweets_set_transient($transientname, $option, 60 * 60 * 24 * 7); } } else { $option[$stringname]['datetime'] = time(); rotatingtweets_set_transient($transientname, $option, 60 * 60 * 24 * 7); } } elseif (!empty($twitterjson['error'])) { # If Twitter is being rate limited, delays the next load until the reset time # For some reason the rate limiting error has a different error variable! $rate = rotatingtweets_get_rate_data(); if ($rate && $rate['remaining_hits'] == 0) { $option[$stringname]['datetime'] = $rate['reset_time_in_seconds'] - $cache_delay + 1; rotatingtweets_set_transient($transientname, $option, 60 * 60 * 24 * 7); } } elseif (!empty($twitterjson)) { unset($firstentry); if (isset($twitterjson['statuses'])) { if (WP_DEBUG) { echo "<!-- using [statuses] -->"; } $twitterjson = $twitterjson['statuses']; } elseif (isset($twitterjson['results'])) { if (WP_DEBUG) { echo "<!-- using [results] -->"; } $twitterjson = $twitterjson['results']; } if (isset($twitterjson) && is_array($twitterjson) && isset($twitterjson[0])) { $firstentry = $twitterjson[0]; } if (!empty($firstentry['text'])) { $number_returned_tweets = count($twitterjson); if (WP_DEBUG) { echo "<!-- " . $number_returned_tweets . " tweets returned -->"; } if ($tw_search && $tw_merge && $number_returned_tweets < 40 && isset($latest_json) && is_array($latest_json) && count($latest_json) > 0) { if (WP_DEBUG) { echo "<!-- " . count($latest_json) . " tweets in cache -->"; } $twitterjson = rotatingtweet_combine_jsons($twitterjson, $latest_json); if (WP_DEBUG) { echo "<!-- " . count($twitterjson) . " tweets in merged json -->"; } } $latest_json = rotatingtweets_shrink_json($twitterjson); $option[$stringname]['json'] = $latest_json; $option[$stringname]['datetime'] = time(); $rtcacheresult = rotatingtweets_set_transient($transientname, $option, 60 * 60 * 24 * 7); if ($rtcacheresult && WP_DEBUG) { echo "<!-- Successfully stored cache entry for {$stringname} in {$transientname} -->"; } elseif (WP_DEBUG) { echo "<!-- Failed to store cache entry for {$stringname} in {$transientname} -->"; } } } if (isset($latest_json)) { return $latest_json; } else { return; } }