=== modified file 'sites/all/modules/shorten/shorten.module'
--- sites/all/modules/shorten/shorten.module	2009-11-09 22:02:38 +0000
+++ sites/all/modules/shorten/shorten.module	2009-11-10 00:06:42 +0000
@@ -130,10 +130,16 @@
   if ($cached->data) {
     return $cached->data;
   }
-  $url = _shorten_get_url($original, $service);
+  $services = module_invoke_all('shorten_service', $original);
+  if (isset($services[$service])) {
+    $url = _shorten_get_url($original, $services[$service]);
+  }
   //If the primary service fails, try the secondary service.
   if (!$url) {
-    $url = _shorten_get_url($original, variable_get('shorten_service_backup', 'TinyURL'));
+    $service = variable_get('shorten_service_backup', 'TinyURL');
+    if (isset($services[$service])) {
+      $url = _shorten_get_url($original, $services[$service]);
+    }
     //If the secondary service fails, use the original URL.
     if (!$url) {
       $url = $original;
@@ -161,27 +167,31 @@
  * @return
  *   An abbreviated URL.
  */
-function _shorten_get_url($original, $service) {
+function _shorten_get_url($original, $api) {
   $method = drupal_strtoupper(variable_get('shorten_method', _shorten_method_default()));
-  $services = module_invoke_all('shorten_service', $original);
-  foreach ($services as $name => $api) {
-    if ($service == $name) {
-      if (is_string($api)) {
-        $url = shorten_fetch($api . $original);
-        break;
-      }
-      elseif (is_array($api) && $api['custom'] === 'xml') {
-        $url = shorten_fetch($api['url'] . $original, $api['tag']);
-        break;
-      }
-      elseif (is_array($api) && $api['custom'] === FALSE) {
-        $url = shorten_fetch($api['url'] . $original);
-        break;
-      }
-      elseif (is_array($api) && $api['custom'] === TRUE) {
-        $url = $api['url'];
-        $method = t('A custom method');
-        break;
+  if (is_string($api)) {
+    $url = shorten_fetch($api . $original);
+  }
+  elseif (is_array($api) && isset($api['custom'])) {
+    if ($api['custom'] === 'xml') {
+      $url = shorten_fetch($api['url'] . $original, $api['tag']);
+    }
+    elseif ($api['custom'] === FALSE) {
+      $url = shorten_fetch($api['url'] . $original);
+    }
+    elseif ($api['custom'] === TRUE) {
+      $url = $api['url'];
+      $method = t('A custom method');
+    }
+    elseif (is_string($api['custom']) && function_exists($api['custom'])) {
+      $method =  t('A custom method') . ': ' . $api['custom'] . '()';
+      if (is_array($api['args'])) {
+        $args = $api['args'];
+        array_unshift($args, $original);
+        $url = call_user_func_array($api['custom'], $args);
+      }
+      else {
+        $url = call_user_func($api['custom'], $original);
       }
     }
   }
@@ -238,11 +248,7 @@
     'urlShort' => 'http://u.mavrev.com/api.php?url=',
   );
   if (module_exists('shorturl')) {
-    $url = check_url('http://' . variable_get('shorturl_domain', $_SERVER['HTTP_HOST']) . '/' . shorturl_shorten($original));
-    $services['This site'] = array(
-      'custom' => TRUE,
-      'url' => $url,
-    );
+    $services['This site'] = array('custom' => '_shorten_fetch_shorturl');
   }
   if (variable_get('shorten_cligs', '')) {
     $services['cli.gs'] = 'http://cli.gs/api/v1/cligs/create?appid=drupal&key='. variable_get('shorten_cligs', '') .'&url=';
@@ -307,6 +313,13 @@
 }
 
 /**
+ * Fetches shortened URL from shorturl module. Function defined in shorten_shorten_service()
+ */
+function _shorten_fetch_shorturl($original) {
+  return check_url('http://' . variable_get('shorturl_domain', $_SERVER['HTTP_HOST']) . '/' . shorturl_shorten($original));
+}
+
+/**
  * Displays the Shorten form.
  */
 function shorten_form_display() {

