Clean up bit.ly admin panel behavior, and hide it from the shorteners list if it's not fully configured.

This commit is contained in:
Brion Vibber 2010-10-07 13:05:51 -07:00
parent 80f0b9421f
commit cad4805516
2 changed files with 55 additions and 7 deletions

View File

@ -39,6 +39,8 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
{ {
public $shortenerName = 'bit.ly'; public $shortenerName = 'bit.ly';
public $serviceUrl = 'http://bit.ly/api?method=shorten&version=2.0.1&longUrl=%s'; public $serviceUrl = 'http://bit.ly/api?method=shorten&version=2.0.1&longUrl=%s';
public $login; // To set a site-default when admins or users don't override it.
public $apiKey;
function onInitializePlugin(){ function onInitializePlugin(){
parent::onInitializePlugin(); parent::onInitializePlugin();
@ -47,6 +49,21 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
} }
} }
/**
* Add bit.ly to the list of available URL shorteners if it's configured,
* otherwise leave it out.
*
* @param array $shorteners
* @return boolean hook return value
*/
function onGetUrlShorteners(&$shorteners)
{
if ($this->getLogin() && $this->getApiKey()) {
return parent::onGetUrlShorteners($shorteners);
}
return true;
}
/** /**
* Short a URL * Short a URL
* @param url * @param url
@ -68,7 +85,11 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
*/ */
protected function getLogin() protected function getLogin()
{ {
return common_config('bitly', 'default_login'); $login = common_config('bitly', 'default_login');
if (!$login) {
$login = $this->login;
}
return $login;
} }
/** /**
@ -78,7 +99,11 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
*/ */
protected function getApiKey() protected function getApiKey()
{ {
return common_config('bitly', 'default_apikey'); $key = common_config('bitly', 'default_apikey');
if (!$key) {
$key = $this->apiKey;
}
return $key;
} }
/** /**
@ -213,4 +238,20 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
return true; return true;
} }
} }
/**
* Internal hook point to check the default global credentials so
* the admin form knows if we have a fallback or not.
*
* @param string $login
* @param string $apiKey
* @return boolean hook return value
*/
function onBitlyDefaultCredentials(&$login, &$apiKey)
{
$login = $this->login;
$apiKey = $this->apiKey;
return false;
}
} }

View File

@ -206,11 +206,18 @@ class BitlyAdminPanelForm extends AdminForm
array('id' => 'settings_bitly') array('id' => 'settings_bitly')
); );
$this->out->element('legend', null, _m('Default credentials')); $this->out->element('legend', null, _m('Default credentials'));
// Do we have global defaults to fall back on?
$login = $apiKey = false;
Event::handle('BitlyDefaultCredentials', array(&$login, &$apiKey));
$haveGlobalDefaults = ($login && $apiKey);
if ($login && $apiKey) {
$this->out->element('p', 'form_guide', $this->out->element('p', 'form_guide',
_m('When users select the bit.ly shortening service, by default ' . _m('Leave these empty to use the default credentials.'));
'these credentials will be used. If you leave these empty, ' . } else {
'users will need to set up their own credentials in order to ' . $this->out->element('p', 'form_guide',
'use bit.ly.')); _m('If you leave these empty, bit.ly will be unavailable to users.'));
}
$this->out->elementStart('ul', 'form_data'); $this->out->elementStart('ul', 'form_data');
$this->li(); $this->li();