From 78f0fc8789634070c5bc92e15fcca1b013c62e66 Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Fri, 27 Dec 2024 12:36:51 +0100 Subject: [PATCH] fix repeated draft activities --- classes/actions/class-content.php | 60 ++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/classes/actions/class-content.php b/classes/actions/class-content.php index a1f8c120e..3f299e351 100644 --- a/classes/actions/class-content.php +++ b/classes/actions/class-content.php @@ -66,27 +66,8 @@ public function insert_post( $post_id, $post, $update ) { return; } - // Query arguments. - $query_args = [ - 'category' => 'content', - 'type' => $type, - 'data_id' => (string) $post_id, - ]; - - // If it's an update add the start and end date. We don't want to add multiple update activities for the same post on the same day. - if ( 'update' === $type ) { - $query_args['start_date'] = \progress_planner()->get_date()->get_datetime_from_mysql_date( $post->post_modified )->modify( '-12 hours' ); - $query_args['end_date'] = \progress_planner()->get_date()->get_datetime_from_mysql_date( $post->post_modified )->modify( '+12 hours' ); - } - - // Check if there is an activity for this post. - $existing = \progress_planner()->get_query()->query_activities( - $query_args, - 'RAW' - ); - - // If there is an activity for this post, bail. - if ( ! empty( $existing ) ) { + // Bail if there is a recent activity for this post. + if ( $this->is_there_recent_activity( $post, $type ) ) { return; } @@ -112,6 +93,11 @@ public function transition_post_status( $new_status, $old_status, $post ) { return; } + // Bail if there is a recent activity for this post. + if ( $this->is_there_recent_activity( $post, $new_status ) ) { + return; + } + // Add activity. $this->add_post_activity( $post, $new_status === 'publish' ? 'publish' : 'update' ); } @@ -210,6 +196,38 @@ private function should_skip_saving( $post ) { return false; } + /** + * Check if there is a recent activity for this post. + * + * @param \WP_Post $post The post object. + * @param string $type The type of activity (ie publish, update, trash, delete etc). + * + * @return bool + */ + private function is_there_recent_activity( $post, $type ) { + // Query arguments. + $query_args = [ + 'category' => 'content', + 'type' => $type, + 'data_id' => (string) $post->ID, + ]; + + // If it's an update add the start and end date. We don't want to add multiple update activities for the same post on the same day. + if ( 'update' === $type ) { + $query_args['start_date'] = \progress_planner()->get_date()->get_datetime_from_mysql_date( $post->post_modified )->modify( '-12 hours' ); + $query_args['end_date'] = \progress_planner()->get_date()->get_datetime_from_mysql_date( $post->post_modified )->modify( '+12 hours' ); + } + + // Check if there is an activity for this post. + $existing = \progress_planner()->get_query()->query_activities( + $query_args, + 'RAW' + ); + + // If there is an activity for this post, bail. + return ! empty( $existing ) ? true : false; + } + /** * Add an update activity. *