Skip to content

Commit bbc0015

Browse files
authored
Merge pull request #531 from ProgressPlanner/ari/new-task-publish-draft-content
New task provider: Check unpublished (draft/pending) content
2 parents da458cc + 3ef40ca commit bbc0015

7 files changed

Lines changed: 480 additions & 24 deletions

File tree

classes/suggested-tasks/class-tasks-manager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Progress_Planner\Suggested_Tasks\Providers\Fewer_Tags;
3232
use Progress_Planner\Suggested_Tasks\Providers\Remove_Terms_Without_Posts;
3333
use Progress_Planner\Suggested_Tasks\Providers\Update_Term_Description;
34+
use Progress_Planner\Suggested_Tasks\Providers\Unpublished_Content;
3435
use Progress_Planner\Suggested_Tasks\Providers\Collaborator;
3536

3637
/**
@@ -73,6 +74,7 @@ public function __construct() {
7374
new Remove_Terms_Without_Posts(),
7475
new Fewer_Tags(),
7576
new Update_Term_Description(),
77+
new Unpublished_Content(),
7678
new Collaborator(),
7779
];
7880

classes/suggested-tasks/data-collector/class-data-collector-manager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Progress_Planner\Suggested_Tasks\Data_Collector\Post_Tag_Count;
2020
use Progress_Planner\Suggested_Tasks\Data_Collector\Published_Post_Count;
2121
use Progress_Planner\Suggested_Tasks\Data_Collector\Yoast_Orphaned_Content;
22+
use Progress_Planner\Suggested_Tasks\Data_Collector\Unpublished_Content;
2223

2324
/**
2425
* Base data collector.
@@ -50,6 +51,7 @@ public function __construct() {
5051
new Terms_Without_Description(),
5152
new Post_Tag_Count(),
5253
new Published_Post_Count(),
54+
new Unpublished_Content(),
5355
];
5456

5557
// Add the plugin integration.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Terms without description data collector.
4+
*
5+
* @package Progress_Planner
6+
*/
7+
8+
namespace Progress_Planner\Suggested_Tasks\Data_Collector;
9+
10+
use Progress_Planner\Suggested_Tasks\Data_Collector\Base_Data_Collector;
11+
12+
/**
13+
* Terms without posts data collector class.
14+
*/
15+
class Unpublished_Content extends Base_Data_Collector {
16+
17+
/**
18+
* The data key.
19+
*
20+
* @var string
21+
*/
22+
protected const DATA_KEY = 'unpublished_content';
23+
24+
/**
25+
* The minimum number of posts.
26+
*
27+
* @var int
28+
*/
29+
protected const POSTS_TO_COLLECT = 1;
30+
31+
/**
32+
* Initialize the data collector.
33+
*
34+
* @return void
35+
*/
36+
public function init() {
37+
\add_action( 'delete_post', [ $this, 'update_unpublished_content_cache' ], 10 );
38+
39+
// We need to update the cache when a post status changes.
40+
\add_action( 'transition_post_status', [ $this, 'on_post_status_changed' ], 10, 3 );
41+
}
42+
43+
/**
44+
* Update the cache when post status changes.
45+
*
46+
* @param string $new_status The new status.
47+
* @param string $old_status The old status.
48+
* @param \WP_Post $post The post.
49+
*
50+
* @return void
51+
*/
52+
public function on_post_status_changed( $new_status, $old_status, $post ) {
53+
// If the status is the same or we don't include the post type, do nothing.
54+
if ( $old_status === $new_status || ! \in_array( \get_post_type( $post ), \progress_planner()->get_settings()->get_post_types_names(), true ) ) {
55+
return;
56+
}
57+
58+
if ( $new_status === 'publish' ) {
59+
$this->update_cache();
60+
}
61+
}
62+
63+
/**
64+
* Update the cache when term is edited or deleted.
65+
*
66+
* @return void
67+
*/
68+
public function update_unpublished_content_cache() {
69+
$this->update_cache();
70+
}
71+
72+
/**
73+
* Query the terms without posts.
74+
*
75+
* @return array|null
76+
*/
77+
protected function calculate_data() {
78+
$args = [
79+
'post_type' => \progress_planner()->get_settings()->get_post_types_names(),
80+
'posts_per_page' => static::POSTS_TO_COLLECT,
81+
'post_status' => [ 'draft', 'pending' ],
82+
'orderby' => 'modified',
83+
'order' => 'ASC',
84+
'fields' => 'ids',
85+
'date_query' => [
86+
[
87+
'column' => 'post_modified',
88+
'before' => '-1 week',
89+
],
90+
],
91+
];
92+
93+
/**
94+
* Filter the post IDs to exclude from the query.
95+
*
96+
* @param array $post__not_in The post IDs to exclude.
97+
*
98+
* @return array
99+
*/
100+
$post__not_in = \apply_filters( 'progress_planner_unpublished_content_exclude_post_ids', [] );
101+
102+
if ( ! empty( $post__not_in ) ) {
103+
$args['post__not_in'] = $post__not_in;
104+
}
105+
106+
$posts = \get_posts( $args );
107+
108+
return $posts ? [ 'post_id' => $posts[0] ] : null;
109+
}
110+
}

classes/suggested-tasks/providers/class-content-review.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -344,25 +344,6 @@ public function get_tasks_to_inject() {
344344
return $added_tasks;
345345
}
346346

347-
/**
348-
* Get the post ID from the task ID.
349-
*
350-
* @param string $task_id The task ID.
351-
*
352-
* @return \WP_Post|null
353-
*/
354-
public function get_post_from_task_id( $task_id ) {
355-
$tasks = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'task_id' => $task_id ] );
356-
357-
if ( empty( $tasks ) ) {
358-
return null;
359-
}
360-
361-
return isset( $tasks[0]->target_post_id ) && $tasks[0]->target_post_id
362-
? \get_post( $tasks[0]->target_post_id )
363-
: null;
364-
}
365-
366347
/**
367348
* This method is added just to override the parent method.
368349
* For this task provider we can't check if it is snoozed like for other as we snooze the task for specific post.

classes/suggested-tasks/providers/class-tasks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public function is_onboarding_task() {
341341
}
342342

343343
/**
344-
* Check if a task category is snoozed.
344+
* Check if task provider is snoozed.
345345
*
346346
* @return bool
347347
*/

0 commit comments

Comments
 (0)