-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoreblocks.plugin.php
427 lines (385 loc) · 11.7 KB
/
coreblocks.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
namespace Habari;
if ( !defined( 'HABARI_PATH' ) ) { die( 'No direct access' ); }
/**
* Create blocks commonly used by site owners.
*
*/
class CoreBlocks extends Plugin
{
private $allblocks = array();
/**
* When the plugin is initialized, register the block templates and set up supporting data.
*/
function action_init()
{
$this->allblocks = array(
'recent_comments' => _t( 'Recent Comments' ),
'recent_posts' => _t( 'Recent Posts' ),
'monthly_archives' => _t( 'Monthly Archives' ),
'tag_archives' => _t( 'Tag Archives' ),
'meta_links' => _t( 'Meta Links' ),
'search_form' => _t( 'Search Form' ),
'text' => _t( 'Text' ),
);
foreach ( array_keys( $this->allblocks ) as $blockname ) {
$this->add_template( "block.$blockname", dirname( __FILE__ ) . "/block.$blockname.php" );
}
$this->add_template( "block.dropdown.tag_archives", dirname( __FILE__ ) . "/block.dropdown.tag_archives.php" );
$this->add_template( "block.dropdown.monthly_archives", dirname( __FILE__ ) . "/block.dropdown.monthly_archives.php" );
}
/**
* Encapsulate the list of URLs the block can display
* @return array A list of URLs that can be displayed
*/
function meta_urls()
{
return array(
_t( 'Site Feed' ) => URL::get( 'atom_feed', array( 'index' => '1' ) ),
_t( 'Comments Feed' ) => URL::get( 'atom_feed_comments' ),
'Habari' => 'http://habariproject.org/',
);
}
/**
* Add available blocks to the list of possible block types.
*
* @param array $block_list an Associative array of the internal names and display names of blocks
*
* @return array The modified $block_list array
*/
public function filter_block_list( $block_list )
{
$allblocks = $this->allblocks;
foreach ( $allblocks as $blockname => $nicename ) {
$block_list[ $blockname ] = _t( $nicename );
}
return $block_list;
}
/**
* Recent Comments
*
* Allow configuration of the number of recent comments to show
*
* @param FormUI $form The configuration form for this block
* @param Block $block The block instance to be configured
*/
public function action_block_form_recent_comments( $form, $block )
{
$form->append( FormControlLabel::wrap( _t( 'Comments to show:' ),
FormControlText::create( 'quantity', $block )
));
}
/**
* Recent Comments
*
* Handle recent comment block output
*
* @param Block $block The block instance to be configured
* @param Theme $theme The active theme
*/
public function action_block_content_recent_comments( $block, $theme )
{
if ( ! $limit = $block->quantity ) {
$limit = 5;
};
$offset = 0;
$published_posts = 0;
$valid_comments = array();
// prevent endless looping if there are fewer comments than $limit
$comments_remain = true;
while ( $published_posts < $limit && $comments_remain ) {
$comments = Comments::get( array(
'limit' => $limit - $published_posts,
'status' => 'approved',
'type' => 'comment',
'offset' => $offset,
'orderby' => 'date DESC',
) );
// check the posts
foreach ( $comments as $key => $comment ) {
if ( ( $comment->post->status ) == Post::status( 'published' ) ) {
$valid_comments[] = $comments[ $key ];
++$published_posts;
}
++$offset;
}
// stop looping if out of comments
if ( count( $comments ) === 0 ) {
$comments_remain = false;
}
}
$block->recent_comments = $valid_comments;
}
/**
* Recent Posts
*
* Allow configuration of the number of recent posts to show
*
* @param FormUI $form The configuration form for this block
* @param Block $block The block instance to be configured
*/
public function action_block_form_recent_posts( $form, $block )
{
$form->append( FormControlLabel::wrap( _t( 'Posts to show:' ),
FormControlText::create( 'quantity', $block )
));
$content_types = Post::list_active_post_types();
unset($content_types['any']);
foreach ( $content_types as $k => $v ) {
$content_types[ $k ] = Plugins::filter( 'post_type_display', $k, 'plural' );
}
$form->append( FormControlLabel::wrap( _t( 'Content Types to Include:' ),
FormControlCheckboxes::create( 'content_type', $block )->set_options( $content_types )
));
}
/**
* Recent Posts
*
* Handle recent post block output
*
* @param Block $block The block instance to be configured
* @param Theme $theme The active theme
*/
public function action_block_content_recent_posts( $block, $theme )
{
if ( ! $limit = $block->quantity ) {
$limit = 5;
};
if ( empty( $block->content_types ) ) {
$block->content_types = array( 'entry' );
}
$block->recent_posts = Posts::get( array(
'limit' => $limit,
'status' => 'published',
'content_type' => $block->content_types,
'orderby' => 'pubdate DESC',
) );
}
/**
* Monthly Archives
*
* Allow configuration of the monthly archive options
*
* @param FormUI $form The configuration form for this block
* @param Block $block The block instance to be configured
*/
public function action_block_form_monthly_archives( $form, $block )
{
$form->append( FormControlLabel::wrap( _t( 'Display full month names:' ),
FormControlCheckbox::create( 'full_names', $block )
));
$form->append( FormControlLabel::wrap( _t( 'Append post count:' ),
FormControlCheckbox::create( 'show_counts', $block )
));
$form->append( FormControlLabel::wrap( _t( 'Preferred Output Style:' ),
FormControlSelect::create( 'style', $block )->set_options( array('dropdown' => _t( 'Dropdown' ), 'list' => _t( 'List' ) ) )
));
}
/**
* Monthly Archives
*
* Handle monthly archive block output
*
* @param Block $block The block instance to be configured
* @param Theme $theme The active theme
*/
public function action_block_content_monthly_archives( $block, $theme )
{
$months = array();
$results = Posts::get( array(
'content_type' => 'entry',
'status' => 'published',
'month_cts' => 1 )
);
foreach ( $results as $result ) {
if ( $block->full_names ) {
$display_month = DateTime::create()->set_date( $result->year, $result->month, 1 )->get( 'F' );
}
else {
$display_month = DateTime::create()->set_date( $result->year, $result->month, 1 )->get( 'M' );
}
$count = '';
if ( $block->show_counts ) {
$count = " (" . $result->ct . ")";
}
$result->month = str_pad( $result->month, 2, 0, STR_PAD_LEFT );
$url = URL::get( 'display_entries_by_date', array( 'year' => $result->year, 'month' => $result->month ) );
$months[] = array(
'display_month' => $display_month,
'count' => $count,
'year' => $result->year,
'url' => $url,
);
}
$block->months = $months;
}
/**
* Monthly Archives
*
* @param Type $type The block template to be used
* @param Block $block The block instance to be configured
*/
function filter_block_content_type_monthly_archives( $types, $block )
{
array_unshift( $types, $newtype = "block.{$block->style}.{$block->type}" );
if ( isset( $block->title ) ) {
array_unshift( $types, "block.{$block->style}.{$block->type}." . Utils::slugify( $block->title ) );
}
return $types;
}
/**
* Tag Archives
*
* Allow configuration of the tag archive options
*
* @param FormUI $form The configuration form for this block
* @param Block $block The block instance to be configured
*/
public function action_block_form_tag_archives( $form, $block )
{
$form->append( FormControlLabel::wrap( _t( 'Append post count:' ),
FormControlCheckbox::create( 'show_counts', $block )
));
$form->append( FormControlLabel::wrap( _t( 'Preferred Output Style:' ),
FormControlSelect::create( 'style', $block )->set_options( array('dropdown' => _t( 'Dropdown' ), 'list' => _t( 'List' ) ) )
));
}
/**
* Tag Archives
*
* Handle tag archive block output
*
* @param Block $block The block instance to be configured
* @param Theme $theme The active theme
*/
public function action_block_content_tag_archives( $block, $theme )
{
$tags = array();
$results = Tags::vocabulary()->get_tree();
foreach( $results as $result ) {
$count = '';
if ( $block->show_counts ) {
$count = " (" . Posts::count_by_tag( $result->term_display, "published") . ")";
}
$url = URL::get( 'display_entries_by_tag', array( 'tag' => $result->term ) );
$tags[] = array(
'tag' => $result->term_display,
'count' => $count,
'url' => $url,
);
}
$block->tags = $tags;
}
/**
* Tag Archives
*
* @param Type $type The block template to be used
* @param Block $block The block instance to be configured
*/
function filter_block_content_type_tag_archives( $types, $block )
{
array_unshift( $types, $newtype = "block.{$block->style}.{$block->type}" );
if ( isset( $block->title ) ) {
array_unshift( $types, "block.{$block->style}.{$block->type}." . Utils::slugify( $block->title ) );
}
return $types;
}
/**
* Meta Links
*
* Allow configuration of the meta links options
*
* @param FormUI $form The configuration form for this block
* @param Block $block The block instance to be configured
*/
public function action_block_form_meta_links( $form, $block )
{
$form->append( FormControlLabel::wrap( _t( "Links to show:" ),
FormControlCheckboxes::create( 'links', $block )->set_options( array_flip( $this->meta_urls() ) )
));
}
/**
* Meta Links
*
* Handle meta links block output
*
* @param Block $block The block instance to be configured
* @param Theme $theme The active theme
*/
public function action_block_content_meta_links( $block, $theme )
{
$list = array();
if ( User::identify()->loggedin ) {
$list[ Site::get_url( 'logout' ) ] = _t( 'Logout' );
}
else {
$list[ Site::get_url( 'login' ) ] = _t( 'Login' );
}
$meta_urls = array_flip( $this->meta_urls() );
$links = $block->links;
if ( count( $links ) > 0 ) {
foreach ( $links as $link ) {
$list[ $link ] = $meta_urls[ $link ];
}
}
$block->list = $list;
}
/**
* Search Form
*
* Allow configuration of the search form options
*
* @param FormUI $form The configuration form for this block
* @param Block $block The block instance to be configured
*/
public function action_block_form_search_form( $form, $block )
{
$form->append( FormControlLabel::wrap( _t( 'Button:' ),
FormControlText::create( 'button', $block )
));
}
/**
* Search Form
*
* Handle search form block output
*
* @param Block $block The block instance to be configured
* @param Theme $theme The active theme
*/
public function action_block_content_search_form( $block, $theme )
{
$block->form = '<form method="get" id="searchform" action="' . URL::get( 'display_search' ) .
'"><p><input type="text" id="s" name="criteria" value="' . ( isset( $theme->criteria ) ? htmlentities( $theme->criteria, ENT_COMPAT, 'UTF-8' ) : '' ) .
'"><input type="submit" id="searchsubmit" value="' . ( isset( $block->button ) ? $block->button : _t( 'Search' ) ) . '"></p></form>';
}
/**
* Text
*
* Display unformatted text without any markup.
* If you require HTML output, try the Simple Block plugin instead.
*
* @param FormUI $form The configuration form for this block
* @param Block $block The block instance to be configured
* Configuration form with one big textarea. Raw to allow JS/HTML/etc. Insert them at your own peril.
*/
public function action_block_form_text( $form, $block )
{
$form->append( FormControlLabel::wrap( _t( 'Content:',
FormControlTextArea::create( 'content', $block, array( 'rows' => 5 ) ) )
));
}
/**
* Text
*
* Display unformatted text without any markup.
* If you require HTML output, try the Simple Block plugin instead.
*
* @param Block $block The block instance to be configured
* @param Theme $theme The active theme
*/
public function action_block_content_text( $block, $theme )
{
return $block;
}
}
?>