'. $js_code .'
';
}
/*
* Helper function for shortening a string
* to the desired length
*
* @param $string
* The string that will be shortened
*
* @param $length
* An integer to set the length of the resulting string
*/
function diggthis_cut($string, $length) {
if (strlen($string) > $length) {
$string = substr($string, 0, $length);
$string = preg_replace("/\s+\S+$/", "...", $string);
}
return $string;
}
/**
* Do the Digg API request and retrieve the XML feed
* with the top Digg stories for a given domain.
*
* @return string $xml
*/
function diggthis_request($options) {
$api_url = 'http://services.digg.com/stories';
$request_url = url($api_url, $options);
$xml = '';
$cache = cache_get($request_url, 'cache');
if(is_object($cache) && $cache->expire > time()) {
$xml = $cache->data;
} else {
cache_clear_all($request_url, 'cache');
$response = drupal_http_request($request_url);
// cache the XML string since unserializing simplexml objects does not work
$xml = $response->data;
cache_set($request_url, $xml, 'cache', time() + variable_get('diggthis_cache_lifetime', 43200));
}
return $xml;
}
/**
* Do the Digg API request and retrieve the XML feed
* with the top Digg stories for a given domain.
*
* @return String themed HTML of top Digg stories
*
* @todo replace hard coded urls
*/
function diggthis_top_stories() {
$options = array(
'query' => array('count' => 10,
'appkey' => 'http://www.seo-expert-blog.com',
'domain' => 'www.seo-expert-blog.com',
'sort' => 'digg_count-desc'),
'absolute' => TRUE
);
$xml = diggthis_request($options);
return theme('diggthis_top_stories', simplexml_load_string($xml));
}
/**
* Theme function for the digg this story page
*
* @param Object $data
* @return String $html
*/
function theme_diggthis_top_stories($data) {
$html = '';
foreach ($data->story as $story) {
$digg_link = l(t('Diggs received: !diggs', array('!diggs' => $story['diggs'])),
$story['href'], array('absolute' => TRUE));
$story_link = l($story->title, $story['link'], array('absolute' => TRUE));
$html .= '';
$html .= '
' . $story_link . '
';
$html .= '
' . $story->description . '
';
$html .= '
' . $digg_link . '
';
$html .= '
';
}
return $html;
}