-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-last-published-post.php
More file actions
99 lines (86 loc) · 2.22 KB
/
class-last-published-post.php
File metadata and controls
99 lines (86 loc) · 2.22 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
<?php
/**
* Hello World data collector.
*
* @package Progress_Planner
*/
namespace Progress_Planner\Suggested_Tasks\Data_Collector;
use Progress_Planner\Suggested_Tasks\Data_Collector\Base_Data_Collector;
/**
* Post author data collector class.
*/
class Last_Published_Post extends Base_Data_Collector {
/**
* The data key.
*
* @var string
*/
protected const DATA_KEY = 'last_published_post_id';
/**
* The include post types.
*
* @var string[]
*/
protected $include_post_types = [];
/**
* Initialize the data collector.
*
* @return void
*/
public function init() {
\add_action( 'init', [ $this, 'set_include_post_types' ], 100 ); // Wait for all CPTs to be registered and collector manager to trigger it's init method (which is done on priority 99).
\add_action( 'transition_post_status', [ $this, 'update_last_published_post_cache' ], 10, 3 );
}
/**
* Set the include post types.
*
* @return void
*/
public function set_include_post_types() {
$this->include_post_types = \progress_planner()->get_activities__content_helpers()->get_post_types_names();
}
/**
* Update the cache when post status changes.
*
* @param string $new_status The new status.
* @param string $old_status The old status.
* @param \WP_Post $post The post.
*
* @return void
*/
public function update_last_published_post_cache( $new_status, $old_status, $post ) {
if ( true === \in_array( get_post_type( $post ), $this->include_post_types, true ) && ( $new_status === 'publish' || $old_status === 'publish' ) ) {
$this->update_cache();
}
}
/**
* Query the hello world post.
*
* @return array
*/
protected function calculate_data() {
// Default data.
$data = [
'post_id' => 0,
'long' => false,
'post_date' => '',
];
// Get the post that was created last.
$last_created_posts = \get_posts(
[
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => $this->include_post_types,
]
);
if ( ! empty( $last_created_posts ) ) {
$data = [
'post_id' => $last_created_posts[0]->ID,
'post_date' => $last_created_posts[0]->post_date,
];
}
return $data;
}
}