-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitly.plugin.php
107 lines (91 loc) · 2.76 KB
/
bitly.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* Bit.ly Plugin
*
* Generates a bit.ly short URL for each post, and
* makes it available in the post's info. A default template is supplied
* which can be displayed in the header of the theme.
*
**/
class Bitly extends Plugin {
public function action_init() {
$this->add_template('bitly_short_url', dirname(__FILE__) . '/bitly_short_url.php');
}
public function action_plugin_ui($plugin_id, $action) {
if ($plugin_id == $this->plugin_id()) {
switch ($action) {
case _t('Configure'):
$form = new FormUI(__CLASS__);
$login = $form->append('text', 'login', 'bitly__login', _t('Login'));
$login->add_validator('validate_required');
$apiKey = $form->append('text', 'api_key', 'bitly__api_key', _t('API Key'));
$apiKey->add_validator('validate_required');
$form->append('submit', 'save', 'Save');
$form->on_success( array( $this, 'updated_config' ) );
$form->out();
break;
}
}
}
public function updated_config( FormUI $ui )
{
Session::notice( _t( 'Bit.ly options saved.', 'bitly' ) );
$ui->save();
}
public function filter_plugin_config( $actions, $plugin_id )
{
if ( $plugin_id == $this->plugin_id() ) {
$actions[]= _t( 'Configure' );
}
return $actions;
}
public function theme_show_bitly_shorturl($theme, $post) {
$theme->post = $post;
return $theme->fetch('bitly_short_url');
}
public function action_post_insert_after($post) {
if (Post::status('published') != $post->status) {
return;
}
try {
$bitly = new BitlyAPI(Options::get('bitly__login'), Options::get('bitly__api_key'));
$result = $bitly->shorten($post->permalink);
$post->info->short_url = $result->data->url;
}
catch (Exception $e) {
Session::error('Could not communicate with bit.ly API.', 'Bit.ly API');
}
}
}
class BitlyAPI {
public $endpoint = 'http://api.bit.ly/v3';
public $format = 'json';
protected $username = null;
protected $apiKey = null;
public function __construct($username, $apiKey) {
$this->username = $username;
$this->apiKey = $apiKey;
}
public function shorten($url) {
$params = array(
'login' => $this->username,
'apiKey' => $this->apiKey,
'format' => $this->format,
'longUrl' => $url,
);
$reqUrl = $this->endpoint . '/shorten?' . http_build_query($params);
$call = new RemoteRequest($reqUrl);
$call->set_timeout(5);
$result = $call->execute();
if (Error::is_error($result)) {
throw $result;
}
$response = $call->get_response_body();
$data = json_decode($response);
if ($data === null) {
throw new Exception("Could not communicate with bit.ly API");
}
return $data;
}
}
?>