-
Notifications
You must be signed in to change notification settings - Fork 0
/
deliciousfeed.plugin.php
194 lines (173 loc) · 4.97 KB
/
deliciousfeed.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* DeliciousFeed Plugin: Show the posts from Delicious feed
*/
class DeliciousFeed extends Plugin
{
private static function default_options( )
{
return array(
'user_id' => '',
'bookmark_tags' => '',
'bookmark_count' => '15',
'cache_expiry' => 1800
);
}
public function action_block_form_deliciousfeed( $form, $block )
{
// Load defaults
foreach ( self::default_options( ) as $k => $v ) {
if ( !isset( $block->$k ) )
$block->$k = $v;
}
$form->append( 'text', 'user_id', $block, _t( 'Delicious Username', 'deliciousfeed' ) );
$form->user_id->add_validator( 'validate_username' );
$form->user_id->add_validator( 'validate_required' );
$form->append( 'text', 'bookmark_tags', $block, _t( 'Tags (seperate by space)', 'deliciousfeed' ) );
$form->append( 'text', 'bookmark_count', $block, _t( '№ of Posts', 'deliciousfeed' ) );
$form->bookmark_count->add_validator( 'validate_uint' );
$form->bookmark_count->add_validator( 'validate_required' );
$form->append( 'text', 'cache_expiry', $block, _t( 'Cache Expiry (in seconds)', 'deliciousfeed' ) );
$form->cache_expiry->add_validator( 'validate_uint' );
$form->cache_expiry->add_validator( 'validate_required' );
}
public function validate_username( $username )
{
if ( preg_match( '/[A-Za-z0-9._]+/', $username ) === false ) {
return array( _t( 'Your Delicious username is not valid.', 'deliciousfeed' ) );
}
return array( );
}
public function validate_uint( $value )
{
if ( !ctype_digit( $value ) || strstr( $value, '.' ) || $value < 0 ) {
return array( _t( 'This field must be positive integer.', 'deliciousfeed' ) );
}
return array( );
}
private static function build_api_url( $block )
{
$url = 'http://feeds.delicious.com/v2/json/' . $block->user_id;
if ( $block->bookmark_tags ) {
$url .= '/' . urlencode( $block->bookmark_tags );
}
$url .= '?count=' . $block->bookmark_count;
return $url;
}
private static function get_external_content( $url )
{
// Get JSON content via Delicious API
$call = new RemoteRequest( $url );
$call->set_timeout( 5 );
$result = $call->execute( );
if ( Error::is_error( $result ) ) {
throw new Exception( _t( 'Unable to contact Delicious.', 'deliciousfeed' ) );
}
return $call->get_response_body( );
}
private static function parse_data( $block, $data )
{
// Decode JSON
$feed = json_decode( $data );
if ( !is_array( $feed ) ) {
// Response is not JSON
throw new Exception( _t( 'Response is not correct, maybe Delicious server is down or API is changed.', 'deliciousfeed' ) );
} else {
$deliciousfeed = array( );
foreach( $feed as $i => $link ) {
$delicious_post = new DeliciousPost( $link );
$deliciousfeed[ ] = $delicious_post;
}
}
return $deliciousfeed;
}
public function action_block_content_deliciousfeed( $block, $theme )
{
// Load defaults
foreach ( self::default_options( ) as $k => $v ) {
if ( !isset( $block->$k ) )
$block->$k = $v;
}
$cache_name = 'deliciousfeed_' . md5( serialize( array(
$block->user_id,
$block->bookmark_tags,
$block->bookmark_count,
) ) );
if ( $block->user_id != '' ) {
if ( Cache::has( $cache_name ) ) {
$block->bookmarks = Cache::get( $cache_name );
}
else {
try {
$url = self::build_api_url( $block );
$data = self::get_external_content( $url );
$bookmarks = self::parse_data( $block, $data );
$block->bookmarks = $bookmarks;
// Do cache
Cache::set( $cache_name, $block->bookmarks, $block->cache_expiry );
}
catch ( Exception $e ) {
$block->error = $e->getMessage( );
}
}
}
else {
$block->error = _t( 'DeliciousFeed Plugin is not configured properly.', 'deliciousfeed' );
}
}
/**
* On plugin init, add the template included with this plugin to the available templates in the theme
*/
public function action_init( )
{
$this->load_text_domain( 'deliciousfeed' );
$this->add_template( 'block.deliciousfeed', dirname( __FILE__ ) . '/block.deliciousfeed.php' );
}
public function filter_block_list( $block_list )
{
$block_list[ 'deliciousfeed' ] = _t( 'DeliciousFeed', 'deliciousfeed' );
return $block_list;
}
}
class DeliciousPost
{
public $data = array( );
public function __construct( stdClass $data )
{
foreach( $data as $name => $value ) {
$this->data[ $name ] = $value;
}
}
public function __get( $name )
{
switch ( $name ) {
case 'url':
return $this->data[ 'u' ];
break;
case 'title':
return htmlspecialchars( $this->data[ 'd' ] );
break;
case 'desc':
return htmlspecialchars( $this->data[ 'n' ] );
break;
case 'tags':
return htmlspecialchars( $this->data[ 't' ] );
break;
case 'tags_text':
return htmlspecialchars( implode( ' ', $this->data[ 't' ] ) );
break;
case 'timestamp':
return $this->data[ 'dt' ];
break;
default:
return FALSE;
break;
}
}
public function __set( $name, $value )
{
$this->data[ $name ] = $value;
return $this->data[ $name ];
}
}
?>