Skip to content

Commit

Permalink
Merge pull request #204 from Emilia-Capital/filip/fix-122
Browse files Browse the repository at this point in the history
Refactor post_updated and wp_insert_post callbacks
  • Loading branch information
jdevalk authored Jan 6, 2025
2 parents c126f9d + 78f0fc8 commit 1ce5170
Showing 1 changed file with 53 additions and 61 deletions.
114 changes: 53 additions & 61 deletions classes/actions/class-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ public function __construct() {
* @return void
*/
public function register_hooks() {
// Add activity when a post is updated.
\add_action( 'post_updated', [ $this, 'post_updated' ], 10, 2 );

// Add activity when a post is added.
\add_action( 'wp_insert_post', [ $this, 'insert_post' ], 10, 2 );
// Add activity when a post is added or updated.
\add_action( 'wp_insert_post', [ $this, 'insert_post' ], 10, 3 );
\add_action( 'transition_post_status', [ $this, 'transition_post_status' ], 10, 3 );

// Add activity when a post is trashed or deleted.
Expand All @@ -40,84 +38,41 @@ public function register_hooks() {
}

/**
* Post updated.
* Insert a post.
*
* Runs on post_updated hook.
* Runs on wp_insert_post hook.
*
* @param int $post_id The post ID.
* @param \WP_Post $post The post object.
*
* @param bool $update Whether this is an update.
* @return void
*/
public function post_updated( $post_id, $post ) {
public function insert_post( $post_id, $post, $update ) {
// Bail if we should skip saving.
if ( $this->should_skip_saving( $post ) ) {
return;
}

// Reset the words count.
\progress_planner()->get_settings()->set( [ 'word_count', $post_id ], false );

if ( 'publish' !== $post->post_status ) {
return;
}

// Check if there is an update activity for this post, on this date.
$existing = \progress_planner()->get_query()->query_activities(
[
'category' => 'content',
'type' => 'update',
'data_id' => (string) $post_id,
'start_date' => \progress_planner()->get_date()->get_datetime_from_mysql_date( $post->post_modified )->modify( '-12 hours' ),
'end_date' => \progress_planner()->get_date()->get_datetime_from_mysql_date( $post->post_modified )->modify( '+12 hours' ),
],
'RAW'
);
// Set the type of activity.
$type = $update ? 'update' : 'publish';

// If there is an update activity for this post, on this date, bail.
if ( ! empty( $existing ) ) {
return;
}

$this->add_post_activity( $post, 'update' );
}

/**
* Insert a post.
*
* Runs on wp_insert_post hook.
*
* @param int $post_id The post ID.
* @param \WP_Post $post The post object.
* @return void
*/
public function insert_post( $post_id, $post ) {
// Bail if we should skip saving.
if ( $this->should_skip_saving( $post ) ) {
return;
// Reset the words count if it's an update.
if ( 'update' === $type ) {
\progress_planner()->get_settings()->set( [ 'word_count', $post_id ], false );
}

// Bail if the post is not published.
if ( 'publish' !== $post->post_status ) {
return;
}

// Check if there is a publish activity for this post.
$existing = \progress_planner()->get_query()->query_activities(
[
'category' => 'content',
'type' => 'publish',
'data_id' => (string) $post_id,
],
'RAW'
);

// If there is a publish 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;
}

// Add a publish activity.
$this->add_post_activity( $post, 'publish' );
// Finally add an activity.
$this->add_post_activity( $post, $type );
}

/**
Expand All @@ -138,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 @@ -236,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 1ce5170

Please sign in to comment.