/
popular_posts.plugin.php
109 lines (95 loc) · 2.75 KB
/
popular_posts.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
<?php
class PopularPosts extends Plugin
{
/**
* Add the necessary template
*
**/
public function action_init()
{
$this->add_template( 'popular_posts', dirname(__FILE__) . '/popular_posts.php' );
$this->add_template( 'block.popular_posts', dirname(__FILE__) . '/block.popular_posts.php' );
}
/**
* Add to the list of possible block types.
*
**/
public function filter_block_list( $block_list )
{
$block_list[ 'popular_posts' ] = _t( 'Popular Posts', 'popular_posts' );
return $block_list;
}
/**
* Create a configuration form for this plugin
*
**/
public function configure()
{
$form = new FormUI( 'popular_posts' );
$form->append( 'checkbox', 'loggedintoo', 'popular_posts__loggedintoo', _t( 'Track views of logged-in users too', 'popular_posts' ) );
$form->append( 'submit', 'save', 'Save' );
$form->out();
}
/**
* Create a configuration form for the block
**/
public function action_block_form_popular_posts( $form, $block )
{
$content = $form->append( 'text', 'quantity', $block, _t( 'Posts to show:', 'popular_posts' ) );
$form->append( 'submit', 'save', _t( 'Save', 'popular_posts' ) );
}
/**
* Log the entry page view, when appropriate.
*
*/
public function action_add_template_vars( $theme, $handler_vars )
{
// If there is only one post
if ( $theme->post instanceof Post && count( $theme->posts ) == 1 ) {
// Only track users that aren't logged in, unless specifically overridden
if ( !User::identify()->loggedin || Options::get( 'popular_posts__loggedintoo' ) ) {
$set = Session::get_set( 'popular_posts', false );
$post = $theme->post;
if ( !in_array( $post->id, $set ) ){
$views = $post->info->views;
if ( $views == null ) {
$views = 0;
}
$views += 1;
$post->info->views = $views;
$post->info->commit();
Session::add_to_set( 'popular_posts', $post->id );
}
}
}
}
/**
* Display a template with the popular entries
*/
public function theme_popular_posts($theme, $limit = 5)
{
$theme->popular_posts = Posts::get( array(
'content_type' => 'entry',
'has:info' => 'views',
'orderby' => 'CAST(hipi1.value AS UNSIGNED) DESC', // As the postinfo value column is TEXT, ABS() forces the sorting to be numeric
'limit' => $limit
) );
return $theme->display( 'popular_posts' );
}
/**
* Populate a block with the popular entries
**/
public function action_block_content_popular_posts( $block, $theme )
{
if ( ! $limit = $block->quantity ) {
$limit = 5;
};
$block->popular_posts = Posts::get( array(
'content_type' => 'entry',
'has:info' => 'views',
'orderby' => 'CAST(hipi1.value as UNSIGNED) DESC', // As the postinfo value column is TEXT, ABS() forces the sorting to be numeric
'limit' => $limit
) );
}
}
?>