/
linkblog.plugin.php
366 lines (304 loc) · 9.04 KB
/
linkblog.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
require( 'linkhandler.php' );
require( 'linkdatabase.php' );
class LinkBlog extends Plugin
{
/**
* Show options on plugins admin page
*/
public function configure()
{
$ui = new FormUI( strtolower( get_class( $this ) ) );
$ui->append( 'textarea', 'original_text', 'linkblog__original', _t( 'Text to use for describing original in feeds:' ) );
$ui->original_text->rows = 2;
$ui->append( 'checkbox', 'atom_permalink', 'linkblog__atom_permalink', _t( 'Override atom permalink with link URL' ) );
$ui->append( 'submit', 'save', _t( 'Save' ) );
return $ui;
}
/**
* Register content type
*/
public function action_plugin_activation( $plugin_file )
{
self::install();
}
public function action_plugin_deactivation( $plugin_file )
{
Post::deactivate_post_type( 'link' );
}
/**
* install various stuff we need
*/
static public function install()
{
Post::add_new_type( 'link' );
// Give anonymous users access
$group = UserGroup::get_by_name( 'anonymous' );
$group->grant( 'post_link', 'read' );
// Set default settings
Options::set( 'linkblog__original', '<p><a href="{permalink}">Permalink</a></p>' );
Options::set( 'linkblog__atom_permalink', false );
self::database();
}
/**
* install database
*/
static public function database()
{
// $schema = Config::get('db_connection');
list( $schema, $remainder ) = explode( ':', Config::get( 'db_connection' )->connection_string );
switch( $schema )
{
case 'sqlite':
$q = 'CREATE TABLE IF NOT EXISTS ' . DB::table( 'link_traffic' ) . '(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
post_id INTEGER NOT NULL,
date INTEGER NOT NULL,
type SMALLINTEGER NOT NULL,
ip INTEGER default NULL,
referrer VARCHAR(255) default NULL
);';
break;
case 'mysql':
default:
$q = 'CREATE TABLE IF NOT EXISTS ' . DB::table( 'link_traffic' ) . '(
`id` int(10) unsigned NOT NULL auto_increment,
`post_id` int(10) unsigned NOT NULL,
`date` int(10) unsigned NOT NULL,
`type` int(5) unsigned NOT NULL,
`ip` int(10) unsigned default NULL,
`referrer` varchar(255) default NULL,
PRIMARY KEY (`id`)
);';
}
// Utils::debug( $schema, $q );
// CREATE TABLE {$prefix}posts (
// id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
// slug VARCHAR(255) NOT NULL,
// content_type SMALLINTEGER NOT NULL,
// title VARCHAR(255) NOT NULL,
// guid VARCHAR(255) NOT NULL,
// content TEXT NOT NULL,
// cached_content LONGTEXT NOT NULL,
// user_id SMALLINTEGER NOT NULL,
// status SMALLINTEGER NOT NULL,
// pubdate INTEGER NOT NULL,
// updated INTEGER NOT NULL,
// modified INTEGER NOT NULL
// );
return DB::dbdelta( $q );
}
/**
* Register templates
*/
public function action_init()
{
// Add rewrite rules
// $this->add_rule('"link"/"redirect"/slug', 'link_redirect');
// Create templates
$this->add_template( 'link.single', dirname( __FILE__ ) . '/link.single.php' );
// register tables
DB::register_table( 'link_traffic' );
// Utils::debug( RewriteRules::by_name('link_redirect'), URL::get('link_redirect', array('slug' => 'bob')) );
self::database();
}
/**
* Redirect a link to its original destination
*/
public function action_plugin_act_link_redirect( $handler )
{
$slug= $handler->handler_vars['slug'];
$post= Post::get( array( 'slug' => $slug ) );
if ( $post == false ) {
$handler->theme->display( '404' );
exit;
}
$type= Traffum::TYPE_SEND_NORMAL;
if ( isset( $handler->handler_vars['refer'] ) && $handler->handler_vars['refer'] == 'atom' ) {
$type = Traffum::TYPE_SEND_ATOM;
}
Traffum::create( array( 'post_id' => $post->id, 'type' => $type ) );
Utils::redirect( $post->info->url );
exit;
}
/**
* Create name string
*/
public function filter_post_type_display( $type, $foruse )
{
$names = array(
'link' => array(
'singular' => _t( 'Link' ),
'plural' => _t( 'Links' ),
)
);
return isset( $names[$type][$foruse] ) ? $names[$type][$foruse] : $type;
}
/**
* Modify publish form
*/
public function action_form_publish( $form, $post )
{
if ( $post->content_type == Post::type( 'link' ) ) {
$url = $form->append( 'text', 'url', 'null:null', _t( 'URL' ), 'admincontrol_text' );
// CNS: If we're using this in conjunction with my modified version of the "Publish Quote" plugin, we might already have the URL in the handler_vars.
$vars = Controller::get_handler_vars();
$url->value = ( isset( $vars['url'] ) ) ? $vars['url'] : $post->info->url;
$form->move_after( $url, $form->title );
}
}
/**
* Initiate tracking on display
*/
function action_add_template_vars( $theme )
{
static $set = false;
if ( $set == true || !is_object( $theme->matched_rule ) || $theme->matched_rule->action != 'display_post' || $theme->post->content_type != Post::type( 'link' ) ) {
return;
}
$post= $theme->post;
$type= Traffum::TYPE_VIEW_NORMAL;
if ( Controller::get_var( 'refer' ) != null && Controller::get_var( 'refer' ) == 'atom' ) {
$type= Traffum::TYPE_VIEW_ATOM;
}
Traffum::create( array( 'post_id' => $post->id, 'type' => $type ) );
$set = true;
}
/**
* Save our data to the database
*/
public function action_publish_post( $post, $form )
{
if ( $post->content_type == Post::type( 'link' ) ) {
$this->action_form_publish( $form, $post );
$post->info->url = $form->url->value;
}
}
public function filter_post_link( $permalink, $post )
{
if ( $post->content_type == Post::type( 'link' ) ) {
return self::get_redirect_url( $post );
}
else {
return $permalink;
}
}
public function filter_post_permalink_atom( $permalink, $post )
{
if ( $post->content_type == Post::type( 'link' ) ) {
if ( Options::get( 'linkblog__atom_permalink' ) == true ) {
return self::get_redirect_url( $post, 'atom' );
}
}
return $permalink;
}
public static function get_redirect_url( $post, $context = null )
{
$params = array( 'slug' => $post->slug );
if ( isset( $context ) && $context == 'atom' ) {
$params['refer'] = 'atom';
}
$url = URL::get( 'link_redirect', $params );
return $url;
}
public static function get_permalink_url( $post, $context = null )
{
$url = $post->permalink;
if ( isset( $context ) && $context == 'atom' ) {
$url .= '?refer=atom';
}
return $url;
}
/**
* Modify the atom content to include the permalink and change the URL, if
* configured to.
*/
public function filter_post_content_atom( $content, $post )
{
if ( $post->content_type == Post::type( 'link' ) ) {
$text = Options::get( 'linkblog__original' );
$text = str_replace( '{original}', self::get_redirect_url( $post, 'atom' ), $text );
$text = str_replace( '{permalink}', self::get_permalink_url( $post, 'atom' ), $text );
return $content . $text;
}
else {
return $content;
}
}
/**
* Add links to places where entries are displayed in the conventional way (not a preset)
*/
public function filter_template_user_filters( $filters )
{
// We handle presets seperately; this is just for old-style searches
if ( !isset( $filters['preset'] ) )
{
// Cater for other pages like /page/1 which don't use presets yet
if ( isset( $filters['content_type'] ) ) {
$filters['content_type'] = Utils::single_array( $filters['content_type'] );
$filters['content_type'][] = Post::type( 'link' );
}
}
return $filters;
}
public function filter_posts_get_update_preset( $filters, $presetname, $paramarray )
{
switch( $presetname )
{
case 'home':
$filters['content_type']= Utils::single_array( $filters['content_type'] );
$filters['content_type'][]= Post::type('link');
break;
default:
break;
}
return $filters;
}
/**
* Provide the alternate representation of the new feeds
*/
public function filter_atom_get_collection_alternate_rules( $rules )
{
$rules['link_feed'] = 'display_home';
return $rules;
}
/**
* Add links to the main atom feed
*/
public function filter_atom_get_collection_content_type( $content_type )
{
$content_type = Utils::single_array( $content_type );
$content_type[] = Post::type( 'link' );
return $content_type;
}
/**
* Add needed rewrite rules
*/
public function filter_rewrite_rules( $rules )
{
$feed_regex= $feed_regex = implode( '|', LinkHandler::$feeds );
$rules[] = new RewriteRule( array(
'name' => 'link_feed',
'parse_regex' => '%feed/(?P<name>' . $feed_regex . ')/?$%i',
'build_str' => 'feed/{$name}',
'handler' => 'LinkHandler',
'action' => 'feed',
'priority' => 7,
'is_active' => 1,
'description' => 'Displays the link feeds',
) );
// '"link"/"redirect"/slug', 'link_redirect');
$rules[] = new RewriteRule( array(
'name' => 'link_redirect',
'parse_regex' => '%link/redirect/(?P<slug>[^/]+)/?$%i',
'build_str' => 'link/redirect/{$slug}',
'handler' => 'PluginHandler',
'action' => 'link_redirect',
'priority' => 7,
'is_active' => 1,
'description' => 'Redirects to the linked item',
) );
return $rules;
}
}
?>