/
MarkupBlog.module
1622 lines (1296 loc) · 63.8 KB
/
MarkupBlog.module
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
namespace ProcessWire;
/**
* 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.
* Since version 1, the class has been developed further with lots of new methods not in the original code-base.
*
* https://github.com/kongondo/Blog
* Created April 2014
*
* ProcessWire 3.x
* Copyright (C) 2016 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' => '2.4.6',
'href' => 'https://processwire.com/talk/topic/17105-module-blog/',
'singular' => true,
'autoload' => false,
'requires' => 'ProcessBlog'
);
}
/* ######################### - PROPERTIES - ######################### */
private $small;
private $options;
private $commentsCountTop;
private $commentsCountBottom;
private $showFeaturedImage;
private $featuredImageTop;
private $numComments;
private $numCommentsStr;
private $summaryLimit;
private $featuredImage;
/* ######################### - INIT - ######################### */
/**
* 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() {
$modules = $this->wire('modules');
$pages = $this->wire('pages');
$blogConfigs = $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 = $pages->get($blogConfigs['blog']);
$this->posts = $pages->get($blogConfigs['blog-posts']);
$this->comments = $pages->get($blogConfigs['blog-comments']);
$this->authors = $pages->get($blogConfigs['blog-authors']);
$this->archives = $pages->get($blogConfigs['blog-archives']);
$this->settings = $pages->get($blogConfigs['blog-settings']);
$this->dnc = $pages->get($blogConfigs['blog-dnc']); // disable-new-comments
$this->dc = $pages->get($blogConfigs['blog-dc']); // disable-comments
$this->pauthor = $pages->get($blogConfigs['blog-pauthor']);
$this->blogStyle = $blogConfigs['blogStyle'];
$this->commentsUse = $blogConfigs['commentsUse'];
// @todo/@note: getting intermittent errors in ProcessWire 3.x, hence this. However, unsure if this is the best way to do it?
$modules->get('FieldtypeComments'); // Load FieldtypeComments in MarkupBlog to ensure that Comment:: constants are available.
}
/* ######################### - RENDER MARKUP - ######################### */
/**
* 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 Blog tags to render.
* @param array $options Options to configure rendered tags output.
* @return string $jump . $out Generated Blog tags markup.
*
*/
public function renderTags(PageArray $tags, $options = null) {
// default options for tags
// come in the format 'singular,plural' for e.g. 1 'post'
$defaultOptions = array('tags_posts_text' => $this->_('post,posts'));
// 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;
$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;
// count the number of of posts referencing this $tag
$numPosts = $tag->references("template=blog-post")->count;
$numPosts = sprintf(_n($post, $posts, $numPosts), $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' is name of the post.
*
* @access public
* @param Page $page The current Blog post page.
* @param array $options Options to configure rendered next/prev posts's e.g. << for previous and >> for next
* @return string $out Generated navigation markup.
*
*/
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 integer $limit Number of comments to render.
*
*/
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 Category pages to return.
* @param integer $showNumPosts Number of posts to show from each category (default=0).
* @param array $options Options to configure rendered categories output.
* @return string $out Generated markup of Blog categories.
*
*/
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;
$out = '';
if (!empty($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>";
// find a limited number of posts that reference this category
$category->posts = $category->references("template=blog-post,limit={$showNumPosts},, sort=-blog_date");
$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;
}
/**
* 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 getArchives() method.
* @param array $options Options to configure rendered archives output.
* @return string $out Generated Blog archives markup.
*
*/
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 (!empty($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 Author pages.
* @return string $out Generated markup of Blog Authors.
*
*/
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");
// $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;
}
/**
* Return PageArray of posts related to the current post.
*
* The returned PageArray can then be used with renderNav() to list the related posts.
*
* @access public
* @param Page $post limit of maximum related posts to find.
* @param integer $relatedBy criteria for finding related posts.
* @param integer $limit Find this limited number of related posts.
* @param string $sort prioritise related posts to find, especially when limit set.
* @return PageArray|null $related Related posts or null if none found.
*
*/
public function renderRelatedPosts(Page $post = null, $relatedBy = null, $limit = null, $sort = null) {
if (is_null($post)) $page = $this->wire('page');
else $page = $post;
if (!$page instanceof Page) throw new WireException('renderRelatedPosts requires a Page');
$pages = $this->wire('pages');
$related = null;
$selector = '';
// set defaults
$relatedBy = $relatedBy === null ? $relatedBy = 1 : $relatedBy;
$limit = $limit === null ? $limit = 3 : $limit;
$sort = $sort === null ? $sort = '-blog_date' : $sort;
// sanitize submitted sort selector value. Default is sort by latest post
$sort = $this->wire('sanitizer')->selectorValue($sort);
// find related posts by given criteria. Default is to relate by both tags AND categories
if ($relatedBy == 1 && !empty($page->blog_tags) && !empty($page->blog_categories)) $selector = "blog_tags=$page->blog_tags, blog_categories=$page->blog_categories";
elseif ($relatedBy == 2 && !empty($page->blog_tags)) $selector = "blog_tags=$page->blog_tags"; // relate by tags only
elseif ($relatedBy == 3 && !empty($page->blog_categories)) $selector = "blog_categories=$page->blog_categories"; // relate by categories only
// find related posts
if ($selector) $related = $pages->find("template=blog-post, id!=$page->id, limit=$limit, sort=$sort, $selector");
return $related;
}
/**
* Render a list of comments.
*
* If page is editable, then non-approved comments will be included (and identified) in the list.
*
* @access public
* @param CommentArray $comments.
* @param integer $limit Optional limit of maximum comments to show.
* @param array $options Optional text describing a post's commenting status (e.g. 'comments not allowed').
* @return string $out Comments markup.
*
*/
public function renderComments(CommentArray $comments, $limit = 0, array $options = null) {
$out = '';
$page = $this->wire('page'); // Page being viewed (Page)
$admin = $page->editable();
$cnt = 0;
$pageNum = $this->wire('input')->pageNum;
$commentArray = new CommentArray();
$prevURL = ''; // URL for previous pagination, if applicable (string)
$nextURL = ''; // URL for next pagination, if applicable (string)
// global AND post's comments/comments form visibility state. Note: post's comments/comments form SELECTED visibility status overrides global setting
$noCommentsForm = false;
$noNewComments = '';
$blogComments = $this->comments;
$gComments = $blogComments->blog_comments_view; // global comments/comments form visibility setting [only kicks in when selected AND post setting='']
$gCmntsCnt = $blogComments->blog_comments_max; // global: maximum comments allowed per post [form will be hidden if this limit exceeded]. Note: this overrides post's 'always show comments' form
$pComments = $page->blog_comments_view; // post comments/comments form visibility setting [only kicks in when SELECTED]
$pCmntsCnt = count($page->blog_comments);
$defaultOptions = array(
'comments_closed' => $this->_('Comments closed for this post.'), // string: message when comments closed
'comments_disallowed' => $this->_('Comments not allowed for this post.'), // string: message when comments not allowed for post
'comments_empty' => $this->_('Be the first to comment'), // string: message when there are no comments for post
'comments_login' => $this->_('You need to log in to comment.'), // string: message when comment form hidden until user logs in
'comments_login_required' => false, // bool: if user needs to login before commenting [default is false - they don't need to log in first]
'comments_success' => $this->_('Thank you, your submission has been saved.'),
'comments_error' => $this->_('Your submission was not saved due to one or more errors. Try again.'),
'comments_text' => $this->_('Comment,Comments'), // comments text, e.g. 2 'Comments' or 1 'Comment' next to comments count (div#comments). Must be in 'singular,plural' format
'comments_list_empty' => $this->_('No comments found.'),
'comments_list_reply_text' => $this->_('replied to'),
'comments_by_text' => $this->_('Comment by'),
'comments_post_text' => $this->_('Post a comment'),
'comments_prev_text' => $this->_('Back'),
'comments_next_text' => $this->_('Next'),
);
// merge user options with default comments options
if ($options != null && is_array($options)) {
$options = array_merge($defaultOptions, $options);
} else {
$options = $defaultOptions;
}
if (($pComments && $pComments->id == $this->dnc->id) || ($gComments && $gComments->id == $this->dnc->id && !$pComments) || ($gCmntsCnt && $pCmntsCnt > $gCmntsCnt)) {
$noCommentsForm = true; // will use later to disable showing comments form
// only need to show this on 'blog-post' pages not in 'blog-comments'!
if ($page->template == 'blog-post') $noNewComments = '<h4 id="comments-closed">' . $options['comments_closed'] . '</h4>'; // save to a variable; will use later below
} elseif (($pComments && $pComments->id == $this->dc->id) || ($gComments && $gComments->id == $this->dc->id && !$pComments)) {
if ($page->template == 'blog-post') return '<div id="comments"><h4 id="comments-disallowed">' . $options['comments_disallowed'] . '</h4></div><!--/#comments-->';
}
// check if we should introduce pagination
if ($limit && (count($comments) > $limit || $pageNum > 1)) {
if ($pageNum > 2) $prevURL = $page->url . 'page' . ($pageNum - 1);
elseif ($pageNum > 1) $prevURL = $page->url;
else $prevURL = '';
if (count($comments) > $limit) $nextURL = $page->url . 'page' . ($pageNum + 1);
else $nextURL = '';
}
// setup comments for output, copying all comments we will display into $commentArray
foreach ($comments as $c) {
if (!$admin && $c->status != Comment::statusApproved) continue;
$c->date = $this->formatDate($c->created, 2);
$commentArray->add($c);
$cnt++;
if ($limit && $cnt >= $limit) break;
}
$out .= "<div id='comments'>";
if ($page->template == 'blog-post') {
// if we are viewing a post page, then start with a headline
$n = count($commentArray);
if ($n) {
$out .= "<span class='num-comments-icon'>$n</span>";
list($comment, $comments) = explode(',', $options['comments_text']); // come in the format 'singular,plural'
$out .= "<h4>" . sprintf(_n($comment, $comments, $n), $n) . "</h4>";
}
}
if (!empty($commentArray)) {
// output the individual comments if comments found
$out .= "<ul class='comments CommentList'>";
foreach ($commentArray as $c) {
$out .= "<li class='comment CommentListItem' id='comment" . $c->id . "'>
<p class='comment-head CommentHeader'>";
$cite = htmlentities($c->cite, ENT_QUOTES, "UTF-8");
$website = htmlentities($c->website, ENT_QUOTES, "UTF-8");
if ($website) $cite = "<a href='$website' rel='nofollow' target='_blank'>$cite</a>";
if ($page->template == 'blog-post') $out .= sprintf(__('%1$s %2$s on %3$s'), $options['comments_by_text'], $cite, $c->date);
else {
$out .= "<a href='{$c->page->url}#comment{$c->id}'>";
$out .= sprintf(__('%1$s %2$s %3$s on %4$s'), $cite, $options['comments_list_reply_text'], $c->page->title, $c->date);
$out .= "</a>";
}
if ($c->status == Comment::statusPending) $out .= " <span class='comment-status-pending'>" . $this->_('PENDING') . "</span>";
elseif ($c->status == Comment::statusSpam) $out .= " <span class='comment-status-spam'>" . $this->_('SPAM') . "</span>";
elseif ($c->page->blog_comments_view && $c->page->blog_comments_view->id == $this->dc->id || !$c->page->blog_comments_view && $gComments && $gComments->id == $this->dc->id)
$out .= " <span class='comment-status-hidden'>" . $this->_('HIDDEN') . "</span>";
$out .= "</p>
<div class='comment-body CommentText'>";
$text = htmlentities($c->text, ENT_QUOTES, "UTF-8"); // grab the comment text
$text = str_replace("\n\n", "</p><p>", $text);
$text = str_replace("\n", "<br />", $text);
$out .= "<p>$text</p>
</div></li>";
} // end foreach commentArray
$out .= "</ul>";
} // end if(count($commentArray))
// if comments count == 0
elseif (empty($commentArray)) {
// prevents output of 'be the first to comment' when comments closed but there are no comments yet
if (($page->template == 'blog-post' && $pComments && $pComments->id != $this->dnc->id) || ($page->template == 'blog-post' && !$pComments && !$gComments)) {
$out .= '<h4 id="comments-empty">' . $options['comments_empty'] . '</h4>';
} elseif ($page->template == 'blog-comments') $out .= '<h4 id="comments-empty">' . $options['comments_list_empty'] . '</h4>';
}
if ($page->template == 'blog-post' && $noCommentsForm == false) {
if (($options['comments_login_required'] && $this->wire('user')->isLoggedin()) || (!$options['comments_login_required'])) {
$commentsFormOptions = $this->getCommentsFormOptions($options);
// if we are viewing a post page, then output a comments form
$out .= $page->blog_comments->renderForm($commentsFormOptions);
$out .= "</div><!--/#comments-->";
} else $out .= '<h4 id="comments-login">' . $options['comments_login'] . '</h4></div><!--/#comments-->';
} // end if $page->template == "blog-post" && $noCommentsForm == false
else $out .= $noNewComments . '</div><!--/#comments-->';
// output pagination if necessary
if ($prevURL || $nextURL) {
$out .= "<p class='comments-pagination'>";
if ($prevURL) $out .= "<a class='button button-prev' href='$prevURL'>" . $options['comments_prev_text'] . "</a> ";
if ($nextURL) $out .= "<a class='button button-next' href='$nextURL'>" . $options['comments_next_text'] . "</a> ";
$out .= "</p>";
}
return $out;
}
/**
* Render a Blog post's author widget.
*
* @access public
* @param array $options Options to configure post author widget.
* @return string $out Markup of widget.
*
*/
public function postAuthor(array $options = null) {
$postAuthor = $this->pauthor;
// default options for author widget
$defaultOptions = array(
'author_text' => $this->_('Author'), // text next to author title|name
'author_thumb_width' => 100, // no width manipulation of author thumb if this is blank or 0
'author_thumb_height' => '', // no height manipulation of author thumb if this is blank or 0
'author_thumb_alt' => 'title', // defaults to $author->title. Otherwise other stated field on the page, e.g. = 'title' or if 'description' then $image->description
);
// merge user options with default author widget options
if ($options != null && is_array($options)) $options = array_merge($defaultOptions, $options);
else $options = $defaultOptions;
$out = '';
// if widget is available [i.e. published]...
if (!$postAuthor->is(Page::statusUnpublished)) {
$author = $this->wire('page')->createdUser;
$authorName = $author->get('title') ? $author->get('title') : 'Author Name'; // if no author title not set use generic 'Author Name'
$image = $author->blog_images->first();
$authorText = $options['author_text'] != '' ? "<small> " . $options['author_text'] . "</small>" : '';
if ($image) {
$alt = $options['author_thumb_alt'] == 'description' ? $image->description : $author->{$options['author_thumb_alt']};
$width = (int) $options['author_thumb_width'];
$height = (int) $options['author_thumb_height'];
if ($width > 0) $thumb = $image->width($width);
elseif ($height > 0) $thumb = $image->height($height);
else $thumb = $image;
$photo = "<a title='$authorName'>
<img class='author-photo' src='{$thumb->url}' alt='{$alt}' width='{$thumb->width}' height='{$thumb->height}' >
</a>";
} else $photo = '';
$out .= "<div id='post-author' class='clearfix'>" . $photo . "<h4 class='post-author-name'>" . $authorName . $authorText . "</h4>" . $author->blog_body . "</div>";
}
return $out;
}
/**
* Render an RSS feed of the given pages.
*
* Note that you should not output anything further after calling this, as it outputs the RSS directly.
*
* @access public
* @param PageArray $items Pages to include in the feed.
* @param string $title Title of the feed. If not provided, pulled from current page.
* @param string $description Description of the feed. If not provided, pulled from current page.
*
*/
public function renderRSS(PageArray $items, $title = '', $description = '') {
$page = $this->wire('page');
if (empty($title)) $title = $page->get('headline|title') . ' - ' . $this->wire('pages')->get('/')->headline;
if (empty($description)) $description = $page->get('summary|meta_description'); // @todo - no page summary so change to get from blog_body instead?
$rss = $this->wire('modules')->get('MarkupRSS');
$rss->title = $title;
$rss->description = $description;
$rss->itemDescriptionField = 'body';
$rss->itemDescriptionLength = 0; // no maxlength
$rss->itemDateField = 'date';
$rss->render($items);
}
/**
* Render a secondary navigation.
*
* When the $mobile option is set, make the ul.nav disappear when at mobile width.
* and instead show only the form <select> navigation instead.
* This happens because the css media query recognizes the 'no-mobile' class.
* and hides any thing carrying that class. Likewise, anything with the 'mobile'.
* class is only shown when at mobile width.
*
* @access public
* @param string $headline Headline to display above nav
* @param array|PageArray $items May be an array of Page objects or array of ($url => $label)
* @param Page|string $currentURL Current item that should be highlighted, may be Page or $url
* @param bool $mobile Replace the nav with a <select> when at mobile width?
* @return string $out Generated navigation markup.
*
*/
public function renderNav($headline, $items, $currentURL = '', $mobile = true) {
if (empty($items)) return '';
if ($currentURL instanceof Page) {
$currentPage = $currentURL;
$currentURL = $currentPage->url;
} else $currentPage = null;
$nav = array();
if ($this->blogStyle == 1 || $this->blogStyle == 2) $blogParentId = $this->blog->id;
elseif ($this->blogStyle == 3 || $this->blogStyle == 4) $blogParentId = 1;
foreach ($items as $url => $title) {
if ($title instanceof Page) {
$page = $title;
$title = $page->title;
$url = $page->url;
// if nav item is root-level and $currentPage shares this root parent, then
// make it the currentURL so that it is still highlighted in the nav
/* ORIGINAL CODE in Blog Profile. My Page Tree is different, hence amended below */
// if($page->parent_id == 1 && $currentPage && $currentPage->rootParent === $page) $currentURL = $currentPage->rootParent->url;
/* @kongondo above amendment */
if ($page->parent_id == $blogParentId && $currentPage && $currentPage->parent === $page) $currentURL = $currentPage->parent->url;
}
$nav[$url] = $title;
}
$out = '';
if ($headline) $out .= "<h4 class='nav-headline'>$headline</h4>";
$class = 'nav links';
if ($mobile) $class .= ' no-mobile'; // hides the ul.nav when mobile width
$out .= "<ul class='$class'>";
// $nav is the array we populated earlier with the $url and $title of each $item
foreach ($nav as $url => $title) {
if ($url == $currentURL) $out .= "<li class='on'><a class='on' href='$url'>$title</a></li>";
else $out .= "<li><a href='$url'>$title</a></li>";
}
$out .= "</ul>";
/*
* here's where we display the mobile select navigation. The form we put here
* has the class 'mobile' so it is only visible when at mobile width.
*
*/
if ($mobile) {
$out .= "<form class='mobile'>
<select class='nav'><option value='./'></option>";
// $nav is the array we populated earlier with the $url and $title of each $item
foreach ($nav as $url => $title) {
$selected = $url == $currentURL ? " selected='selected'" : '';
$out .= "<option$selected value='$url'>$title</option>";
}
$out .= "</select>
</form>";
}
return $out;
}
/**
* Given a items of blog entries generate and return the output.
*
* @access public
* @param PageArray|Page|String $posts The entries to generate output for.
* @param bool $small Set to true if summarized versions of posts are required. Default is false.
* @param array $options Options to configure posts' output.
* @return string $out The generated post markup.
*
*/
public function renderPosts($posts, $small = false, array $options = null) {
$posts = $this->processPosts($posts);
$this->options = $this->processPostsOptions($options);
$options = $this->options;
$this->small = $small;
// excerpt/post truncated length
$limit = $this->settings->blog_small;
$this->summaryLimit = $limit ? $limit : 450;
// start output
$out = '';
// Small means show truncated body text. 'Large' just means show the whole/full body text
if ($this->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 .= $this->small ? "<div class='posts posts-small'>" : "<div class='posts'>";
// COMMENTS COUNT: for post comments count
$this->commentsCountTop = false;
$this->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) $this->commentsCountTop = true;
if ($options['post_comments'] == 2 || $options['post_comments'] == 3) $this->commentsCountBottom = true;
}
// FEATURED IMAGE: determine if to output featured image in posts
$this->showFeaturedImage = false;
$this->featuredImageTop = false; // for positioning featured image (top/bottom)
// if featured image is to be output
if (($this->small && $options['post_small_image'] != 0) || (!$this->small && $options['post_large_image'] != 0)) $this->showFeaturedImage = true; // we will be outputting a featured image
// loop through and output posts
foreach ($posts as $page) {
// set each post page's summary, author-name and author-url
$this->setExraProperties($page);
// if showing full posts, set the post's categories and tags as properties for each post
if (!$this->small) {
$this->setCategories($page);
$this->setTags($page);
}
// post comments - only if commentsUse == 1
if ($this->commentsCountTop == true || $this->commentsCountBottom == true) {
$this->numComments = $page->blog_comments->count();
if ($this->numComments > 0) $this->numCommentsStr = sprintf(_n($comment, $comments, $this->numComments), $this->numComments);
else $this->numCommentsStr = $options['post_zero_comments_text']; // used as both title of icon anchor tag when no comments + in post-foot when no comments yet
}
#------------------------------------------- FEATURED IMAGE ------------------------------------------- #
$this->featuredImage = ''; // will contain featured image string
// if featured image is to be output
if ($this->showFeaturedImage == true) {
$this->featuredImage = $this->getFeaturedImage($page);
// 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 (($this->featuredImage != '' && $options['post_small_image'] == 1) || ($this->featuredImage != '' && $options['post_large_image'] == 1)) $this->featuredImageTop = true;
} // end if featured image specified
# ******************************** SINGLE POST START + POST HEAD ******************************** #
#------------------------------------------- POST HEADLINE ------------------------------------------- #
$out .= $this->renderPostHead($page);
#------------------------------------------- POST BODY ------------------------------------------- #
$out .= "<div class='post' id='{$page->id}'>";
$out .= "<div class='post-body'>";
// if featured image to be output below post-body
if ($this->featuredImageTop == false) $out .= $this->featuredImage;
// if to output 'View More' link (if post $small)
if ($this->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 ------------------------------------------- #
// output footer markup if showing FULL post
if (!$this->small) $out .= $this->renderFooter($page);
$out .= "</div>"; // end div.post => single post
} // end foreach($posts as $page)
if (empty($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
$out .= $this->renderPagination($posts);
return $out;
}
/**
* Renders markup of a Blog post's headline.
*
* Used by renderPosts().
*
* @access private
* @param Page $page The post to generate a headline for.
* @return string $out The generated markup.
*
*/
private function renderPostHead($page) {
$options = $this->options;
$numCommentsStr = $this->numCommentsStr;
$numComments = $this->numComments;
$out = '';
$out .= "<div class='post-head'>";
// @note: moved below $featuredImageTop
// if($commentsCountTop == true) $out .= "<a class='num-comments-icon' href='{$page->url}#comments' title='$numCommentsStr'>$numComments</a>";
// headline size
$h = $this->small ? $options['post_small_headline_tag'] : $options['post_large_headline_tag'];
#------------------------------------------- POST HEADLINE ------------------------------------------- #
// if featured image to be output above post-headline
if ($this->featuredImageTop == true) $out .= $this->featuredImage;
if ($this->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
$editURL = $this->wire('config')->urls->admin . "page/edit/?id={$page->id}'>";
$edit = $page->editable() && $options['post_edit'] == 1 ? "<span class='edit'>(<a href='" . $editURL . $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