-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBlogPublishDate.module
More file actions
117 lines (98 loc) · 3.05 KB
/
BlogPublishDate.module
File metadata and controls
117 lines (98 loc) · 3.05 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
<?php
namespace ProcessWire;
/**
* Blog Post Publish Date Module for ProcessWire.
*
* This small autoload module sets a Blog Post's published date (field = 'blog_date')
* Based on the code here: https://processwire.com/talk/topic/2448-show-date-published-on-a-page/ by SiNNuT and Soma
*
* @author Francis Otieno (Kongondo) <kongondo@gmail.com>
* @author Some code from SiNNuT, Soma
*
* https://github.com/kongondo/Blog
* Created August 2014
*
* ProcessWire 3.x
* Copyright (C) 2016 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
*
*/
class BlogPublishDate extends WireData implements Module {
/**
* Return information about this module (required).
*
* @access public
* @return array module info
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Blog Post Publish Date',
'summary' => 'Automatically set a Blog Post publish date on publish',
'author' => 'Francis Otieno (Kongondo)',
'version' => '2.4.6',
'href' => 'https://processwire.com/talk/topic/17105-module-blog/',
'singular' => true,
'autoload' => true,
'requires' => 'ProcessBlog'
);
}
/**
* Initialise the module.
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called.
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
* @access public
*
*/
public function init() {
// add a hook after $page->save (only on Blog Posts pages)
$this->wire('pages')->addHookAfter('save', $this, 'afterSave');
}
/**
* Hook to set a Blog Post's 'published date' (if not set already) when a Blog Post is published.
*
* This function will not empty any values extant in 'blog_date'.
* This is to preserve the original Blog Post publish date.
*
* @access public
*
*/
public function afterSave($event) {
// get the page object saved
$page = $event->argumentsByName('page');
// only on pages that use the template 'blog-post
if ($page->template != 'blog-post') return;
/* - we don't want to touch unpublished pages ['pending' and 'expired']
- neither published pages with blog_date already filled in
- nor if for some reason field 'blog_date' not found in the 'blog-post' template and page is published
*/
if ($page->is(Page::statusUnpublished) || $page->blog_date || !$page->template->hasField('blog_date')) return;
// if page is published and blog_field not populated, add current timestamp
$page->blog_date = time();
$page->save('blog_date');
}
/**
* Called only when the module is installed.
*
* @access public
*
*/
public function ___install() {
// Don't need to add anything here...
}
/**
* Called only when the module is uninstalled.
*
* This should return the site to the same state it was in before the module was installed.
*
* @access public
*
*/
public function ___uninstall() {
// Don't need to add anything here...
}
}