Skip to content

Commit

Permalink
merge methods & remove post_updated hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ilicfilip committed Dec 27, 2024
1 parent 1a7e4dd commit 266d185
Showing 1 changed file with 28 additions and 54 deletions.
82 changes: 28 additions & 54 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,60 @@ 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 );
// Set the type of activity.
$type = $update ? 'update' : 'publish';

if ( 'publish' !== $post->post_status ) {
return;
// Reset the words count if it's an update.
if ( 'update' === $type ) {
\progress_planner()->get_settings()->set( [ 'word_count', $post_id ], false );
}

// 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'
);

// If there is an update activity for this post, on this date, bail.
if ( ! empty( $existing ) ) {
// Bail if the post is not published.
if ( 'publish' !== $post->post_status ) {
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;
}
// Query arguments.
$query_args = [
'category' => 'content',
'type' => $type,
'data_id' => (string) $post_id,
];

if ( 'publish' !== $post->post_status ) {
return;
// 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 a publish activity for this post.
// Check if there is an activity for this post.
$existing = \progress_planner()->get_query()->query_activities(
[
'category' => 'content',
'type' => 'publish',
'data_id' => (string) $post_id,
],
$query_args,
'RAW'
);

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

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

/**
Expand Down

0 comments on commit 266d185

Please sign in to comment.