forked from kongondo/Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkupBlog.module
More file actions
1517 lines (1090 loc) · 56.1 KB
/
MarkupBlog.module
File metadata and controls
1517 lines (1090 loc) · 56.1 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Markup Blog Module for ProcessWire.
*
* This module gives you Methods to easily output your Blog contents.
* The markup is almost entirely left up to you, the user.
*
* @author Francis Otieno (Kongondo) <kongondo@gmail.com>
* @author Ryan Cramer
*
* Almost 100% original code from Ryan Cramer's Blog Profile.
* Compiled by Kongondo from the functions in Ryan Cramer's Blog Profile to Methods in this Class and where necessary adjusted/added some code.
*
* https://github.com/kongondo/Blog
* Created April 2014
*
* ProcessWire 2.x
* Copyright (C) 2011 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
*
*/
class MarkupBlog extends WireData implements Module {
/**
* Return information about this module (required).
*
* @access public
* @return array module info
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Markup Blog',
'summary' => 'Markup module to output a Blog based on the Blog Profile by Ryan Cramer',
'author' => 'Francis Otieno (Kongondo)',
'version' => 239,
'href' => 'https://processwire.com/talk/topic/7403-module-blog/',
'singular' => true,
'autoload' => false,
'requires' => 'ProcessBlog'
);
}
/**
* Initialise the module.
*
* This is an optional initialisation method called before any execute methods. It will be automatically loaded by ProcessWire.
*
* @access public
*
*/
public function init() {
$blogConfigs = $this->wire('modules')->getModuleConfigData('ProcessBlog');
// intialise some properties we'll use throught the class. These contain objects made up of the main blog pages and the main children
$this->blog = $this->wire('pages')->get($blogConfigs['blog']);
$this->posts = $this->wire('pages')->get($blogConfigs['blog-posts']);
$this->comments = $this->wire('pages')->get($blogConfigs['blog-comments']);
$this->authors = $this->wire('pages')->get($blogConfigs['blog-authors']);
$this->archives = $this->wire('pages')->get($blogConfigs['blog-archives']);
$this->settings = $this->wire('pages')->get($blogConfigs['blog-settings']);
$this->dnc = $this->wire('pages')->get($blogConfigs['blog-dnc']);// disable-new-comments
$this->dc = $this->wire('pages')->get($blogConfigs['blog-dc']);// disable-comments
$this->pauthor = $this->wire('pages')->get($blogConfigs['blog-pauthor']);
$this->blogStyle = $blogConfigs['blogStyle'];
$this->commentsUse = $blogConfigs['commentsUse'];
}
/**
* Render a list of tags.
*
* Each of the tags has a numPosts property containing the number of posts used by the tag.
* Also renders an alphabetical jumplist of tags.
* As seen on the frontend page: /blog/tags/
*
* @access public
* @param PageArray $tags
* @param array $options Options to configure rendered tags output
* @return string $jump . $out
*
*/
public function renderTags(PageArray $tags, $options = null) {
// default options for tags
$defaultOptions = array(
'tags_posts_text' =>$this->_('post,posts'),// come in the format 'singular,plural' for e.g. 1 'post'
);
// merge user options with default tags options
if($options != null && is_array($options)) $options = array_merge($defaultOptions, $options);
else $options = $defaultOptions;
list($singular, $plural) = explode(',', $options['tags_posts_text']);// come in the format 'singular,plural'
$post = '%d ' . $singular;
$posts = '%d ' . $plural;
// count the number of posts using each tag
foreach($tags as $tag) {
$tag->numPosts = $this->wire('pages')->count("template=blog-post, blog_tags=$tag");
}
$lastLetter = '';
$out = '';
$letters = array();
foreach($tags as $tag) {
$letter = strtoupper(substr($tag->title, 0, 1));// Grab the first letter of the tag title, make it uppercase
if($letter != $lastLetter) {// if this letter is not equal to the last letter
if($lastLetter) $out .= "</ul>";
$out .= "<h3 id='letter_$letter'>$letter</h3>";
$out .= "<ul class='tags posts-group'>";
$letters[] = $letter;// add to the letters array
}
$lastLetter = $letter;
$numPosts = sprintf(_n($post, $posts, $tag->numPosts), $tag->numPosts);
$out .= "<li><a href='{$tag->url}'>{$tag->title}</a> <span class='num-posts'>$numPosts</span></li>";
}
$out .= "</ul>";
// $jump is for the alphabetical jumplist that will be output at the top of the tags page
$jump = '';
$jump .= "<p class='jumplinks'>";
foreach($letters as $letter) {
$jump .= "<a href='./#letter_$letter'>$letter</a> ";
}
$jump .= "</p>";
// return the jumplist and the markup of tags
return $jump . $out;
}
/**
* Render previous and next posts links.
*
* As seen on the frontend page: /blog/post/ = where 'post' = name of the post
*
* @access public
* @param Page $page
* @param array $options Options to configure rendered next/prev posts's e.g. << for previous and >> for next
* @return string $out
*
*/
public function renderNextPrevPosts($page, Array $options = null) {
$date = $page->getUnformatted('blog_date');
// first sibling of this post whose 'date' is older than this one.
$prevPost = $page->parent->child("template=blog-post, blog_date<$date, sort=-blog_date");
// first sibling of this post whose 'date' is newer than this one.
$nextPost = $page->parent->child("template=blog-post, blog_date>$date, sort=blog_date");
// default options for next/prev posts
$defaultOptions = array(
'prev_post' => '<',// tag/text for previous post, e.g. «, «, etc, i.e. << >>, etc
'next_post' => '>',// tag/text for next post, e.g. ›, etc
'prev_post_text' => 'title',// if title or not empty: will show title of the prev post. Otherwise show specificed text, e.g. 'Older entries'
'next_post_text' => 'title',// if title or not empty: will show title of the next post. Otherwise show specificed text, e.g. 'Newer entries'
);
// merge user options with default next/prev options
if($options != null && is_array($options)) $options = array_merge($defaultOptions, $options);
else $options = $defaultOptions;
// only show next/prev string if not empty
$prev = $options['prev_post'] !='' ? "<span>" . $options['prev_post'] . "</span> " : '';
$next = $options['next_post'] !='' ? " <span>" . $options['next_post'] . "</span>" : '';
$prevPostText = $options['prev_post_text'];// previous post's link text
$nextPostText = $options['next_post_text'];// next post's link text
// determine if to show next/prev page link title or custom text e.g.
if ($prevPostText == 'title' || $prevPostText =='' || $nextPostText == 'title' || $nextPostText =='' ) {
$prevLinkText = $prevPost->title;
$nextLinkText = $nextPost->title;
}
else {
$prevLinkText = $prevPostText;
$nextLinkText = $nextPostText;
}
$out = "<div class='next-prev-posts'>";
// show next/prev post links if available
if($prevPost->id > 0) $out .= "<p class='prev-post'>" . $prev . "<a href='" . $prevPost->url . "'>" . $prevLinkText . "</a></p>";
if($nextPost->id > 0) $out .= "<p class='next-post'><a href='" . $nextPost->url . "'>" . $nextLinkText . "</a>" . $next . "</p>";
$out .= "</div>";
return $out;
}
/**
* Render a limited number of comments RSS.
*
* As seen on the frontend page: /blog/comments/rss/
*
* @access public
* @param int $limit
*
*/
public function renderCommentsRSS($limit) {
// selector to locate the comments we want
$start = 0;
$selector = "limit=$limit, start=$start, sort=-created, status>=" . Comment::statusApproved;
// find the comments we want to output
$comments = $this->findComments($selector);
$commentPages = new PageArray();
foreach($comments as $comment) {
$p = $this->wire('pages')->get($comment->pages_id);
if(!$p->id) continue;
$p = clone $p;
$p->comment_title = htmlentities($comment->cite, ENT_QUOTES, "UTF-8") . " reply to: " . $p->title;
$p->comment_body = htmlentities($comment->text, ENT_QUOTES, "UTF-8");
$p->comment_date = $comment->created;
$commentPages->add($p);
}
$rss = $this->wire('modules')->get('MarkupRSS');
$rss->title = $this->wire('pages')->get('/')->headline . ' - ' . $this->wire('page')->get('headline|title');
$rss->itemTitleField = 'comment_title';
$rss->itemDescriptionField = 'comment_body';
$rss->itemDescriptionLength = 0;
$rss->itemDateField = 'comment_date';
$rss->render($commentPages);
}
/**
* Render a list of categories, optionally showing a few posts from each.
*
* @access public
* @param PageArray $categories
* @param int $showNumPosts Number of posts to show from each category (default=0)
* @param array $options Options to configure rendered categories output
* @return $out string
*
*/
public function renderCategories(PageArray $categories, $showNumPosts = 0, Array $options = null) {
// default options for categories
$defaultOptions = array(
'categories_posts_text' =>$this->_('post,posts'),// come in the format 'singular,plural' for e.g. October 5 'posts'
'categories_not_found' => $this->_('No categories to display.'),// message when no posts found
'categories_more_text' => $this->_('View More'),// link text to view all posts in that category
);
// merge user options with default categories options
if($options != null && is_array($options)) $options = array_merge($defaultOptions, $options);
else $options = $defaultOptions;
foreach($categories as $category) {
$category->posts = $this->wire('pages')->find("template=blog-post, blog_categories=$category, limit=$showNumPosts, sort=-blog_date");
}
$out = '';
if(count($categories)){
list($singular, $plural) = explode(',', $options['categories_posts_text']);// come in the format 'singular,plural'
$cpost = '%d ' . $singular;
$cposts = '%d ' . $plural;
foreach($categories as $category) {
$out .= "<div class='category'>
<h3><a href='$category->url'>$category->title</a></h3>";
$n = $category->posts->getTotal();
$out .= "<span class='num-posts'>" . sprintf(_n($cpost, $cposts, $n), $n);
$out .= "<a class='rss' href='$category->url" . "rss'>" . $this->_('RSS') . "</a></span>";
if($n){
$out .= "<ul class='category-posts posts-group'>";
foreach($category->posts as $post){
$out .= "<li><a href='$post->url'>$post->title</a></li>";
}
// if more posts available than the limit we have set, show "view more" link
if($n > count($category->posts)){
$out .= "<li><a class='more' href='$category->url'>" . $options['categories_more_text'] . "</a></li>";
}
$out .= "</ul>";
}// end if($n);
$out .= "</div>";
}// end foreach $categories
}// end if count $categories
else {
$out .= "<p class='no-categories'>" . $options['categories_not_found'] . "</p>";
}
return $out;
}
/**
* Get an array of archives, optionally by year and optionally including a few posts.
*
* @access public
* @param int $year Retrieve archives for a specific year (default=retrieve all years)
* @param int $limit Max number of posts titles to show for each month, default=0 which means don't show any
* @return $years array() in this format:
*
* array(
* 2012 => array( // year 2012
* 1 => array( // month 1: January
* 'name' => 'January',
* 'url' => '/archives/2012/01/',
* 'posts' => PageArray, // containing first few posts
* 'total' => 5 // total # of posts in month
* ),
* 2 => array( // Month 2: February
* 'name' => 'February',
* ...and so on
* ),
* ...and so on
* ),
*
* 2011 => array( ... ), // year 2011
* ...and so on
* );
*
*/
public function getArchives($year = 0, $limit = 1) {
if($year) {
$firstYear = $year;
$lastYear = $year;
}
else {
$oldest = $this->wire('pages')->get("template=blog-post, blog_date>0, sort=blog_date");
$newest = $this->wire('pages')->get("template=blog-post, blog_date>0, sort=-blog_date");
if(!$newest->id) return '';
$firstYear = date('Y', $oldest->getUnformatted('blog_date'));
$lastYear = date('Y', $newest->getUnformatted('blog_date'));
}
$_limit = $limit > 1 ? (int) $limit : 2;
$years = array();
for($y = $lastYear; $y >= $firstYear; $y--) {
$months = array();
$numPostsYear = 0;
for($month = 1; $month <= 12; $month++) {
$firstDay = strtotime("$y-$month-01");
$lastDay = strtotime("+1 month", $firstDay)-1;
$posts = $this->wire('pages')->find("template=blog-post, blog_date>=$firstDay, blog_date<=$lastDay, limit=$_limit, sort=-blog_date");
$numPosts = $posts->getTotal();
if(!$numPosts) continue;
$numPostsYear += $numPosts;
$archivesPath = $this->archives->url;
$months[$month] = array(
'url' => $archivesPath . "$y/$month/",// note where our blog pages live vary depending on blogStyle
'name' => strftime('%B', $firstDay),
'posts' => $limit > 0 ? $posts : array(),
'total' => $numPosts
);
}
if(!$numPostsYear) continue;
$years[$y] = array(
'url' => $archivesPath . "$y/",// note where our blog pages live vary depending on blogStyle
'name' => $y,
'total' => $numPostsYear,
'months' => $months
);
}
return $years;
}
/**
* Render blog archives for a given year.
*
* Used by the /site/templates/blog-archives.php template.
* Render archives returned by the getArchives() method.
* Archives links include a year headline followed by a list of months in that year with posts,
* and the number of posts in each month.
*
* @access public
* @param array $years as returned by the getArchives() method
* @param array $options Options to configure rendered archives output
* @return $out string
*
*/
public function renderArchives(Array $years, Array $options = null) {
$out = '';
// default options for archives
$defaultOptions = array(
'archives_posts_text' =>$this->_('post,posts'),// come in the format 'singular,plural' for e.g. October 5 'posts'
'archives_month_view_all_text' => $this->_('View All'),// 'view all' that month's archives if limit set on amount to list
);
// merge user options with default archives options
if($options != null && is_array($options)) $options = array_merge($defaultOptions, $options);
else $options = $defaultOptions;
list($singular, $plural) = explode(',', $options['archives_posts_text']);// come in the format 'singular,plural'
$post = '%d ' . $singular;
$posts = '%d ' . $plural;
foreach($years as $year=>$y) {
$year = $y['name'];
$total = $y['total'];
$months = $y['months'];// this is an array
$url = $y['url'];
$out .= "<div class='archive'>
<h3><a href='$url'>$year</a></h3>
<span class='num-posts'>" . sprintf(_n($post, $posts, $total), $total) . "</span>";
$out .= "<ul class='posts-group'>";
foreach($months as $monthNum => $month){
$out .= "<li><a href='" . $month['url'] . "'>" . $month['name'] . "</a>";
$out .= "<span class='num-posts'>" . sprintf(_n($post, $posts, $month['total']), $month['total']) . "</span>";
if(count($month['posts'])) {// posts will be empty if $blog->archives() call specified 0 for limit;
$out .= "<ul>";
foreach($month['posts'] as $item){
$out .= "<li><a href='$item->url'>$item->title</a></li>";
}
if($month['total'] > count($month['posts'])){
$out .= "<li><a class='more' href='" . $month['url'] . "'>" . $options['archives_month_view_all_text'] . "</a></li>";
}
$out .= "</ul>";
}
$out .= "</li>";
}// end foreach $months as $monthNum
$out .= "</ul></div>";
}// end foreach $years as $year
return $out;
}
/**
* Renders a list of Blog authors.
*
* @access public
* @param PageArray $authors
* @return $out string
*
*/
public function renderAuthors(PageArray $authors) {
$out = "<ul class='authors posts-group'>";
foreach($authors as $author) {
$numPosts = $this->wire('pages')->count("template=blog-post, created_users_id=$author, limit=2");
// $numPostsStr = sprintf(_n('%d post', '%d posts', $author->numPosts), $author->numPosts);// not in use
// Note: $author->url2 is the blog-generated version, since $author->url is in the admin.
$authorName = $author->get('title') ? $author->get('title') : 'Author Name';// use generic 'Author Name' if author title not set
$out .= "<li><a href='{$author->url2}'>" . $authorName . "</a> <span class='num-posts'>$numPosts</span></li>";
}
$out .= "</ul>";
return $out;
}
/**
* Find comments from the given selector string
*
* Used internally in this class
*
* @access public
* @param string $selector
* @return $comments CommentArray
*
*/
public function findComments($selector) {
$comments = FieldtypeComments::findComments('blog_comments', $selector);// 1st argument is our comments field
$blogComments = $this->comments;
$gComments = $blogComments->blog_comments_view;// global comments/comments form visibility setting [only kicks in when selected AND post setting='']
$page = $this->wire('page');// Page being viewed (Page)
$admin = $page->editable();
foreach($comments as $c) {
// if this comment is not viewable for current user, remove it from the array
if(!$c->page->viewable()) $comments->remove($c);
// remove comments whose pages are set to 'disable-comments' if user is not admin
if($c->page->blog_comments_view && $c->page->blog_comments_view->id==$this->dc->id && !$admin) $comments->remove($c);
// visibility: empty/no selection in post comments && global disable comments set to dc but user not admin
if(!$c->page->blog_comments_view && $gComments && $gComments->id==$this->dc->id && !$admin) $comments->remove($c);
}
return $comments;
}
/**
* Find a limited number of recent comments.
*
* @access public
* @param int $limit Number of recent comments to find
* @param int $start Where to start, like 0 (default: null = automatic, based on page number)
* @param bool $admin Include non-approved and spam comments? (default: null = determine automatically)
* @return CommentArray
*
*/
public function findRecentComments($limit = 3, $start = null, $admin = null) {
$limit = (int) $limit;
$_limit = is_null($start) ? $limit : $limit+1;
$out = '';
$pageNum = $this->wire('input')->pageNum;
// auto-determine $start if not specified
if(is_null($start)) {
if($pageNum > 1) $start = $pageNum * $limit;
else $start = 0;
}
// we show pending and spam comments when page is editable
if(is_null($admin)) $admin = $this->wire('page')->editable();// we assign variable $admin the value $page->editable() method if argument $admin = null
// build selector to locate comments
$selector = "limit=$_limit, start=$start, sort=-created, ";
if($admin) $selector .= "status>=" . Comment::statusSpam . ", ";// if argument $admin = true OR if page is editable. [Note: statusSpam = -2, statusApproved = 1, statusPending = 0]
else $selector .= "status>=" . Comment::statusApproved . ", ";// if argument $admin = false
// find the comments we want to output
$comments = $this->findComments($selector);
return $comments;
}
/**
* Given a PageArray of blog entries generate and return the output.
*
* @access public
* @param PageArray|Page $posts The entries to generate output for
* @param bool $small Set to true if you want summarized versions of posts (default = false)
* @param array $options Options to configure posts' output
* @return $out string The generated output
*
*/
public function renderPosts($posts, $small = false, Array $options = null) {
if(!$posts instanceof PageArray) {
if($posts instanceof Page) {
// single page
$post = $posts;
$posts = new PageArray();
$posts->add($post);
}
elseif(is_string($posts)) {
// selector string
$selector = $posts;
$posts = $this->wire('pages')->find("template=blog-post, sort=-blog_date, $selector");
}
else {
throw new WireException('renderPosts requires a PageArray, Page or selector string');
}
}// end if(!$posts instanceof PageArray)
// excerpt/post truncated length
$limit = $this->settings->blog_small;
$summaryLimit = $limit ? $limit : 450;
// default options for various aspects of posts
$defaultOptions = array(
'post_count' => 1,// 0=off, 1=on - for the count in: Posts 1 to 5 of 15
'post_count_text' =>$this->_('Post'),// e.g. Posts 1 to 5 of 15
'post_not_found' => $this->_('No posts found.'),// message when no posts found
'post_author' => 1,// display name of post author: 0=off,1=top(below post-headline),2=bottom(in post-foot)
'post_author_text' => $this->_('Posted by'),//
'post_date' => 1,// display post's date: 0=off,1=top(below post-headline),2=bottom(in post-foot)
'post_date_text' => '',// e.g. Posted by author 'on' date
'post_edit' => 1,// display post's edit: 0=off,1=top(below post-headline),2=bottom(in post-foot)
'post_more_text' => $this->_('View More'),// for $small posts - link text to full post
'post_categories' => 2,// display post's categories: 0=off,1=top(below post-byline),2=bottom(in post-foot)
'post_categories_text' => $this->_('Categories:'),// e.g. 'Categories', 'In', 'Filed under', etc
'post_tags' => 2,// display post's tags: 0=off,1=top(below post-byline),2=bottom(in post-foot)
'post_tags_text' => $this->_('Tags:'),// e.g. 'Tagged', 'Related', etc
'post_small_allowable_tags' => '',// holds string of HTML tags for strip_tags $allowable_tags. Needs to be in format '<code><p><img>'
'post_small_headline_tag' => 'h4',
'post_large_headline_tag' => 'h2',
'post_small_tag' => 'p',
// post comments
'post_comments' => 1,// show comments info? 0=off,1=comments count top,2=comments count bottom,3=comments count top & bottom
'post_zero_comments_text' => $this->_('No comments yet'),
'post_comments_text' => $this->_('Comment,Comments'),// title in anchor comments icon + post-foot comments text, e.g. '2 Comments' or '1 Comment'. Must be in 'singular,plural' format
'post_comments_label' => $this->_('Comments:'),// this appears in post-foot, e.g. 'Comments': 2 Comments
// ## featured images ##
'post_image_alt' => 'description',// defaults to $image->description. Otherwise other stated field on the page, e.g. = 'title'
'post_image_tag' => 'featured',// string: image tag to look for in blog_images
// ** small/truncated post featured image **
'post_small_image' => 0,// display post's featured image: 0=off,1=top(above post-headline),2=bottom(first item in post-body)
'post_small_image_width' => '',// no width manipulation of featured image if this is blank or 0
'post_small_image_height' => '',// no height manipulation if this is blank or 0. Note, if size is true and both width and height > 0, use size() instead
/*size:
- image will be resized to exact dimensions -> $img = $image->size($x, $y)
- where $x = 'width' and $y = 'height'*/
'post_small_image_size' => false,// if 'size' = true AND both width and height > 0, this kicks in
// ** large/full post featured image **
'post_large_image' => 0,// display post's featured image: 0=off,1=top(above post-headline),2=bottom(first item in post-body)
'post_large_image_width' => '',// no width manipulation of featured image if this is blank or 0
'post_large_image_height' => '',// no height manipulation if this is blank or 0. Note, if size is true and both width and height > 0, use size() instead
'post_large_image_size' => false,// if 'size' = true AND both width and height > 0, this kicks in
// MarkupPagerNav options
'post_pagination' => array(),
);
// merge user options with default posts' options
if($options != null && is_array($options)) $options = array_merge($defaultOptions, $options);
else $options = $defaultOptions;
# ** prepare post-foot variables for later use. we need to do this earlier to determine how/if post-foot will be output ** #
// When $small = false, i.e. we want 'LARGE' POSTS [not-truncated]. NOTE: We don't show post categories and tags if $small = true
if(!$small) {
foreach($posts as $page) {
// if user wants to show categories in post
if($options['post_categories'] != 0) {
$page->categories = '';
if(count($page->blog_categories)) {
$page->categories = "<p class='categories'><span>" . $options['post_categories_text'] . " " . "</span>";
foreach($page->blog_categories as $category) $page->categories .= "<a href='{$category->url}'>{$category->title}</a>, ";
$page->categories = rtrim($page->categories, ", ") . "</p>";
}
}
// if user wants to show tags in post
if($options['post_tags'] != 0) {
$page->tag = '';
if(count($page->blog_tags)) {
$page->tags = "<p class='tags'><span>" . $options['post_tags_text'] . " " . "</span>";
foreach($page->blog_tags as $tag) $page->tags .= "<a href='{$tag->url}'>{$tag->title}</a>, ";
$page->tags = rtrim($page->tags, ", ") . "</p>";
}
}
}// end foreach $posts as $page
}// END if(!$small) for post-foot
#----------------------------------------- END determine if/how post-foot will be output -----------------------------------------#
foreach($posts as $page) {
// Note:we don't have a summary field for 'post' pages but left code here just in case we need it in future
// only run this if post small == true
if(empty($page->summary) && $small) {
// summary is blank so we auto-generate a summary from the body [note: blog body field is 'blog_body']
// note: second paramenter of strip_tags $allowable_tags is configurable!
$summary = strip_tags(substr($page->blog_body, 0, $summaryLimit), $options['post_small_allowable_tags']);
$page->summary = substr($summary, 0, strrpos($summary, ' '));
}// end if(empty($page->summary))
// set a couple new fields that our output will use
if($page->createdUser->get('title')) {
$authorTitle = $page->createdUser->get('title');
$page->set('authorName', $authorTitle);
$page->set('authorURL', $this->authors->url . $this->sanitizer->pageName($authorTitle) . '/');// use pageName sanitized author title in author page URL
}
else {
$page->set('authorName', 'Author Name');// use generic 'Author Name' if author title not set
$page->set('authorURL', '');// no link to author page until user sets author title
}
}// end foreach($posts as $page)
// start output
$out = '';
// Small means show truncated body text. 'Large' just means show the whole/full body text
if($small && $options['post_count'] !=0) {
// display a headline indicating quantities
$start = $posts->getStart()+1;
$end = $start + count($posts)-1;
$total = $posts->getTotal();
if($total) $out .= "<h3 class='posts-count'>" . $options['post_count_text'] . " " . sprintf(__('%1$d to %2$d of %3$d'), $start, $end, $total) . "</h3>";
}// end if($small)
#------------------------------------------- POSTS WRAPPER ------------------------------------------- #
$out .= $small ? "<div class='posts posts-small'>" : "<div class='posts'>";
// COMMENTS COUNT: for post comments count
$commentsCountTop = false;
$commentsCountBottom = false;
// post comments - only if commentsUse == 1
if($this->commentsUse == 1 && $options['post_comments'] !=0) {
// if we found comments
list($singular, $plural) = explode(',', $options['post_comments_text']);// come in the format 'singular,plural'
$comment = '%d ' . $singular;
$comments = '%d ' . $plural;
if($options['post_comments'] == 1 || $options['post_comments'] == 3) $commentsCountTop = true;
if($options['post_comments'] == 2 || $options['post_comments'] == 3) $commentsCountBottom = true;
}
// FEATURED IMAGE: determine if to output featured image in posts
$showFeaturedImage = false;
$featuredImageTop = false;// for positioning featured image (top/bottom)
// if featured image is to be output
if (($small && $options['post_small_image'] !=0) || (!$small && $options['post_large_image'] !=0)) $showFeaturedImage = true;// we will be outputting a featured image
// loop through and output posts
foreach($posts as $page) {
// post comments - only if commentsUse == 1
if($commentsCountTop == true || $commentsCountBottom == true) {
$numComments = $page->blog_comments->count();
if($numComments > 0) $numCommentsStr = sprintf(_n($comment, $comments, $numComments), $numComments);
else $numCommentsStr = $options['post_zero_comments_text'];// used as both title of icon anchor tag when no comments + in post-foot when no comments yet
}
#------------------------------------------- SINGLE POST START + POST HEAD ------------------------------------------- #
$out .= "<div class='post' id='{$page->id}'>
<div class='post-head'>";
// if($commentsCountTop == true) $out .= "<a class='num-comments-icon' href='{$page->url}#comments' title='$numCommentsStr'>$numComments</a>";// moved below $featuredImageTop
// headline size
$h = $small ? $options['post_small_headline_tag'] : $options['post_large_headline_tag'];
############################
$featuredImage = '';// will contain featured image string
// if featured image is to be output
if ($showFeaturedImage == true) {
########### if user specified they want to show a featured image we first attempt to find it in the page's blog_images. ###########
########### if that fails, we check for it in the post's first embedded image in blog_body ###########
// ** featured image dimensions **
$size = $small ? $options['post_small_image_size'] : $options['post_large_image_size'];
$width = $small ? (int) $options['post_small_image_width'] : (int) $options['post_large_image_width'];
$height = $small ? (int) $options['post_small_image_height'] : (int) $options['post_large_image_height'];
// retrieve first image with the tag 'featured' or user-specified 'tag'
$image = $page->blog_images->getTag($options['post_image_tag']);
// if we found the image
if($image) {
$alt = $options['post_image_alt'] == 'description' ? $image->description : $page->{$options['post_image_alt']};
if ($height > 0 && $width > 0 && $size == true) $thumb = $image->size($width, $height);
elseif ($width > 0) $thumb = $image->width($width);
elseif ($height > 0) $thumb = $image->height($height);
else $thumb = $image;
$featuredImage .= "<img class='post-featured-image' src='{$thumb->url}' alt='{$alt}' width='{$thumb->width}' height='{$thumb->height}' />";
}// end if $image found
// if we didn't find an image in $page->blog_images, we check for the first embedded image in blog_body if blog_body is not blank!
// only for small posts! no need to get embedded image if large/full post! otherwise it will be displayed twice!
elseif($small && $page->blog_body !='') {
/*for each summarised post, we use PHP's DOMDocument class to find the first image and use that as the featured image*/
// parse the html string into a DOMDocument
$dom = new DOMDocument();
$dom->loadHTML($page->blog_body);
// this grabs just the nth img found. we want the first one so item(0)
// we grab the first img found in blog_body
$image = $dom->getElementsByTagName('img')->item(0);// returns an object
// if an image found, we grab its 'src' attribute
if ($image !=null) {
$imageURL = $image->getAttribute('src');// string;
$alt = $page->{$options['post_image_alt']};
$width = $width > 0 ? "width={$width}" : '';
$height = $height > 0 ? "height={$height}" : '';
$featuredImage .= "<img class='post-featured-image' src='{$imageURL}' alt='{$alt}' {$width} {$height} />";
}
}// end else grab image from first image embedded in post
// determine featured image position. in here we already know there is an image; we just don't know if user wants it at the top or bottom
if(($featuredImage !='' && $options['post_small_image'] == 1) || ($featuredImage !='' && $options['post_large_image'] == 1)) $featuredImageTop = true;
}// end if featured image specified
#------------------------------------------- POST HEADLINE ------------------------------------------- #
// if featured image to be output above post-headline
if($featuredImageTop == true) $out .= $featuredImage;
if($commentsCountTop == true) $out .= "<a class='num-comments-icon' href='{$page->url}#comments' title='$numCommentsStr'>$numComments</a>";
$out .= "<$h class='post-headline'><a href='{$page->url}'>{$page->title}</a></$h>";
// meta/byline post_author_text
$author = $options['post_author'] == 1 ? "<span class='author'>" . $options['post_author_text'] . " " . "<a href='{$page->authorURL}'>{$page->authorName}</a></span> " : '';
$date = $options['post_date'] == 1 ? "<span class='date'>" . $options['post_date_text'] . " " . $page->blog_date . "</span>" : '';
// if page is editable (i.e., appropriate person logged in), let's show an edit link to the post
$edit = $options['post_edit'] == 1 && $page->editable() ? "<span class='edit'>(<a href='" . $this->wire('config')->urls->admin . "page/edit/?id={$page->id}'>" . $this->_('edit') . "</a>)</span>" : '';
if(!empty($author) || !empty($date) || !empty($edit)) $out .= "<p class='post-byline'>" . $author . $date . $edit . "</p>";// end p.post-byline
// show post categories and tags if user wants them at the 'top'
if($options['post_categories'] == 1) $out .= $page->categories;
if($options['post_tags'] == 1) $out .= $page->tags;
$out .= "</div>";/*end div.post-head*/
#------------------------------------------- POST BODY ------------------------------------------- #
$out .= "<div class='post-body'>";
// if featured image to be output below post-body
if($featuredImageTop == false) $out .= $featuredImage;
// if to output 'View More' link (if post $small)
if($small) {
$tag = $options['post_small_tag'];
$out .= "<$tag class='post-small'>" . $page->summary . "… <a class='more' href='{$page->url}'>" . $options['post_more_text'] . "</a></$tag>";
}
else {
$out .= $page->blog_body;
}
// Note: Left here for posterity. We are not including a gallery in this version
// if the post has images and no <img> tags in the body, then make it a gallery
// if(count($page->images) && strpos($page->body, '<img ') === false) include("./gallery.php");
$out .= "</div>";/*end div.post-body*/
#------------------------------------------- POST FOOT ------------------------------------------- #
if(!$small) {
// if post author OR post date are to be displayed at the 'bottom' == 2
$author = $options['post_author'] == 2 ? $author = "<span class='author'>" . $options['post_author_text'] . " " . "<a href='{$page->authorURL}'>{$page->authorName}</a></span> " : '';
$date = $options['post_date'] == 2 ? $date = "<span class='date'>{$page->blog_date}</span>" : '';
$edit = $options['post_edit'] == 2 && $page->editable() ? "<span class='edit'>(<a href='" . $this->wire('config')->urls->admin . "page/edit/?id={$page->id}'>" . $this->_('edit') . "</a>)</span>" : '';
// if post_categories OR post_tags are to be displayed at the 'bottom' == 2 + number of comments in 'large' posts
$categories = $options['post_categories'] == 2 ? $page->categories : '';
$tags = $options['post_tags'] == 2 ? $page->tags : '';
$comments = $commentsCountBottom == true ? "<p class='num-comments'><span>" . $options['post_comments_label'] . "</span> <a href='{$page->url}#comments'>$numCommentsStr</a></p>" : '';
if(!empty($author) || !empty($date) || !empty($categories) || !empty($tags) || !empty($comments) || !empty($edit)) {
$out .= "<div class='post-foot'>" . $author . $date . $edit . $categories . $tags . $comments . "</div>";// end div.post-foot
}
}// END if(!$small)
$out .= "</div>";/*end div.post => single post*/
}// end foreach($posts as $page)
if(!count($posts)) $out .= '<h4 id="no-posts">' . $options['post_not_found'] . '</h4>'; // <!--/.posts-->
$out .= "</div>";/*end div.posts => posts wrapper*/
#------------------------------------------- POST PAGINATION ------------------------------------------- #
// if there are more posts than the specified limit, then output pagination
if($posts->getLimit() < $posts->getTotal()) {
$markupPagerNavOptions = is_array($options['post_pagination']) && count($options['post_pagination']) ? $options['post_pagination'] : '';
$out .= $posts->renderPager($markupPagerNavOptions);// @todo - consider making renderPager configurable?
}
return $out;