From 266d185a7e296e0d351b3326a37032f75fed34c6 Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Fri, 27 Dec 2024 08:36:50 +0100 Subject: [PATCH 1/2] merge methods & remove post_updated hook --- classes/actions/class-content.php | 82 +++++++++++-------------------- 1 file changed, 28 insertions(+), 54 deletions(-) diff --git a/classes/actions/class-content.php b/classes/actions/class-content.php index 4291c1e8c..a1f8c120e 100644 --- a/classes/actions/class-content.php +++ b/classes/actions/class-content.php @@ -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. @@ -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 ); } /** From 78f0fc8789634070c5bc92e15fcca1b013c62e66 Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Fri, 27 Dec 2024 12:36:51 +0100 Subject: [PATCH 2/2] 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. *