Skip to content

Commit

Permalink
fix repeated draft activities
Browse files Browse the repository at this point in the history
  • Loading branch information
ilicfilip committed Dec 27, 2024
1 parent 266d185 commit 78f0fc8
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions classes/actions/class-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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' );
}
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 78f0fc8

Please sign in to comment.