|
| 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 | +} |
0 commit comments