-
Notifications
You must be signed in to change notification settings - Fork 0
/
coredashblocks.plugin.php
289 lines (256 loc) · 11 KB
/
coredashblocks.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
<?php namespace Habari; ?>
<?php if ( !defined( 'HABARI_PATH' ) ) { die( 'No direct access' ); }
/**
* CoreDashBlocks - Provides a core set of blocks for the dashboard.
*/
class CoreDashBlocks extends Plugin
{
private $theme;
/**
* Add some templates to the theme for the blocks
*/
public function action_init()
{
$this->add_template( 'dashboard.block.latest_entries', __DIR__ . '/dashboard.block.latest_entries.php' );
$this->add_template( 'dashboard.block.latest_log_activity', __DIR__ . '/dashboard.block.latest_log_activity.php' );
$this->add_template( 'dashboard.block.latest_comments', __DIR__ . '/dashboard.block.latest_comments.php' );
$this->add_template( 'dashboard.block.post_types_statuses', __DIR__ . '/dashboard.block.post_types_statuses.php' );
}
/**
* Add the blocks this plugin provides to the list of available blocks
* @param array $block_list An array of block names, indexed by unique string identifiers
* @return array The altered array
*/
public function filter_block_list($block_list)
{
$block_list['latest_entries'] = _t( 'Latest Entries');
if (User::identify()->can('manage_all_comments')) {
$block_list['latest_comments'] = _t( 'Latest Comments');
}
if (User::identify()->can('manage_logs')) {
$block_list['latest_log_activity'] = _t( 'Latest Log Activity');
}
$block_list['post_types_statuses'] = _t( 'Post Types and Statuses');
return $block_list;
}
/**
* Return a list of blocks that can be used for the dashboard
* @param array $block_list An array of block names, indexed by unique string identifiers
* @return array The altered array
*/
public function filter_dashboard_block_list($block_list)
{
$block_list['latest_entries'] = _t( 'Latest Entries');
$block_list['latest_comments'] = _t( 'Latest Comments');
$block_list['latest_log_activity'] = _t( 'Latest Log Activity');
$block_list['post_types_statuses'] = _t( 'Post Types and Statuses');
return $block_list;
}
/**
* Produce the content for the latest entries block
* @param Block $block The block object
* @param Theme $theme The theme that the block will be output with
*/
public function action_block_content_latest_entries($block, $theme)
{
$block->recent_posts = Posts::get( array( 'status' => 'published', 'limit' => 8, 'type' => Post::type('entry'), 'orderby' => 'pubdate DESC' ) );
$block->link = URL::get('admin', array('page'=>'posts', 'type'=>Post::type('entry')));
}
/**
* Produce the content for the latest log activity block
* @param Block $block The block object
* @param Theme $theme The theme that the block will be output with
*/
public function action_block_content_latest_log_activity($block, $theme)
{
$params = array(
'where' => array(
'user_id' => User::identify()->id
),
'orderby' => 'id DESC', /* Otherwise, exactly same timestamp values muck it up... Plus, this is more efficient to sort on the primary key... */
'limit' => isset($block->logs_number_display) ? $block->logs_number_display : 8,
);
$block->logs = EventLog::get( $params );
$block->link = URL::get('admin', array('page' => 'logs'));
$block->has_options = true;
}
/**
* Produce a form for the editing of the log activity block
* @param FormUI $form The form to allow editing of this block
* @param Block $block The block object to edit
*/
public function action_block_form_latest_log_activity($form, $block)
{
$form->append( FormControlLabel::wrap( _t('Number of items'),
FormControlText::create( 'logs_number_display', $block )
));
$form->append( FormControlSubmit::create( 'submit' )->set_caption( _t( 'Submit' ) ) );
}
/**
* Produce the content for the Latest Comments block
* @param Block $block The block object
* @param Theme $theme The theme that the block will be output with
*/
public function action_block_content_latest_comments($block, $theme)
{
$comment_types = array( Comment::type( 'comment' ) );
$query = '
select
{posts}.*,
max( {comments}.date) as newest_comment_date
from
{posts},
{comments}
where
{posts}.id = {comments}.post_id
AND {posts}.status = ?
AND {comments}.status = ?
AND {comments}.type IN ( ' . implode( ', ', array_fill( 0, count( $comment_types ), '?' ) ) . ' )
group by
{posts}.id
order by
newest_comment_date desc
';
$query_args = array_merge( array( Post::status( 'published' ), Comment::status( 'approved' ) ), $comment_types );
$posts = DB::get_results( $query, $query_args, 'Post' );
$latestcomments = array();
foreach( $posts as $post ) {
$comments = DB::get_results( 'SELECT * FROM {comments} WHERE post_id = ? AND status = ? AND type = ? ORDER BY date DESC LIMIT 5;', array( $post->id, Comment::status('approved'), Comment::type('comment')), 'Comment' );
$latestcomments[$post->id] = $comments;
}
$block->latestcomments_posts = $posts;
$block->latestcomments = $latestcomments;
$block->link = URL::get('admin', array('page' => 'comments'));
}
public function action_block_content_post_types_statuses($block, $theme)
{
$messages = array();
$user = User::identify();
$post_types = Post::list_active_post_types();
array_shift( $post_types );
$post_statuses = array_values( Post::list_post_statuses() );
array_shift( $post_statuses );
foreach( $post_types as $type => $type_id ) {
$plural = Plugins::filter( 'post_type_display', $type, 'plural' );
foreach( $post_statuses as $status => $status_id ) {
$status_display = MultiByte::ucfirst( Plugins::filter( 'post_status_display', Post::status_name( $status_id ) ) );
$site_count = Posts::get( array( 'content_type' => $type_id, 'count' => true, 'status' => $status_id ) );
$user_count = Posts::get( array( 'content_type' => $type_id, 'count' => true, 'status' => $status_id, 'user_id' => $user->id ) );
$message = array();
// @locale First variable is the post status, second is the post type
$message['label'] = _t( '%1$s %2$s', array( $status_display, $plural ) );
if( ! $site_count ) {
$message['site_count'] = '';
}
else if( $user->cannot( 'post_unpublished' ) && Post::status_name( $status_id ) != 'published' ) {
$message['site_count'] = '';
}
else {
$message['site_count'] = $site_count;
}
$perms = array(
'post_any' => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
'own_posts' => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
'post_' . $type => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
);
if ( $user->can_any( $perms ) && $message['site_count'] ) {
$message['site_count'] = '<a href="' . Utils::htmlspecialchars( URL::get( 'admin', array( 'page' => 'posts', 'type' => Post::type( $type ), 'status' => $status_id ) ) ) . '">' . Utils::htmlspecialchars( $message['site_count'] ) . '</a>';
}
if( ! $user_count ) {
$message['user_count'] = '';
}
else {
$message['user_count'] = $user_count;
}
// @locale First variable is the post status, second is the post type
$perms = array(
'own_posts' => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
'post_' . $type => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
);
if ( $user->can_any( $perms ) && $message['user_count'] ) {
$message['user_count'] = '<a href="' . Utils::htmlspecialchars( URL::get( 'admin', array( 'page' => 'posts', 'type' => Post::type( $type ), 'status' => $status_id, 'user_id' => $user->id ) ) ) . '">' . Utils::htmlspecialchars( $message['user_count'] ) . '</a>';
}
if( $message['site_count'] || $message['user_count'] ) {
$messages[] = $message;
}
}
}
$block->messages = $messages;
}
/**
* filter_dash_block_post_types
* Function used to set theme variables to the post types dashboard widget
* @param string $block_id
* @return string The contents of the block
*/
public function filter_dash_block_post_types_and_statuses( $block, $block_id, $theme )
{
$messages = array();
$user = User::identify();
$post_types = Post::list_active_post_types();
array_shift( $post_types );
$post_statuses = array_values( Post::list_post_statuses() );
array_shift( $post_statuses );
foreach( $post_types as $type => $type_id ) {
$plural = Plugins::filter( 'post_type_display', $type, 'plural' );
foreach( $post_statuses as $status => $status_id ) {
$status_display = MultiByte::ucfirst( Plugins::filter( 'post_status_display', Post::status_name( $status_id ) ) );
$site_count = Posts::get( array( 'content_type' => $type_id, 'count' => true, 'status' => $status_id ) );
$user_count = Posts::get( array( 'content_type' => $type_id, 'count' => true, 'status' => $status_id, 'user_id' => $user->id ) );
// @locale First variable is the post status, second is the post type
$message['label'] = _t( '%1$s %2$s', array( $status_display, $plural ) );
if( ! $site_count ) {
$message['site_count'] = '';
}
else if( $user->cannot( 'post_unpublished' ) && Post::status_name( $status_id ) != 'published' ) {
$message['site_count'] = '';
}
else {
$message['site_count'] = $site_count;
}
$perms = array(
'post_any' => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
'own_posts' => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
'post_' . $type => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
);
if ( $user->can_any( $perms ) && $message['site_count'] ) {
$message['site_count'] = '<a href="' . Utils::htmlspecialchars( URL::get( 'admin', array( 'page' => 'posts', 'type' => Post::type( $type ), 'status' => $status_id ) ) ) . '">' . Utils::htmlspecialchars( $message['site_count'] ) . '</a>';
}
if( ! $user_count ) {
$message['user_count'] = '';
}
else {
$message['user_count'] = $user_count;
}
// @locale First variable is the post status, second is the post type
$perms = array(
'own_posts' => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
'post_' . $type => array( ACL::get_bitmask( 'delete' ), ACL::get_bitmask( 'edit' ) ),
);
if ( $user->can_any( $perms ) && $message['user_count'] ) {
$message['user_count'] = '<a href="' . Utils::htmlspecialchars( URL::get( 'admin', array( 'page' => 'posts', 'type' => Post::type( $type ), 'status' => $status_id, 'user_id' => $user->id ) ) ) . '">' . Utils::htmlspecialchars( $message['user_count'] ) . '</a>';
}
if( $message['site_count'] || $message['user_count'] ) {
$messages[] = $message;
}
}
}
$theme->type_messages = $messages;
$block['title'] = _t( 'Post Types and Statuses' );
$block['content'] = $theme->fetch( 'dash_posttypes' );
return $block;
}
/**
* Adds the stylesheet to the admin header only on the dashboard.
*
* @param Theme $theme The current theme being used.
*/
public function action_admin_header( $theme )
{
$vars = Controller::get_handler_vars();
if( 'dashboard' == $theme->page ) {
Stack::add( 'admin_stylesheet', array( $this->get_url() . '/coredashblocks.css', 'screen' ), 'coredashblocks', array( 'admin-css' ) );
}
}
}
?>