From a31a3657849dd6b4bd3c23d2b71e106fc9d3c1d1 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 13 Dec 2024 12:51:16 +0200 Subject: [PATCH 1/2] Refactor the Local_Task_Factory class a bit --- .../class-local-tasks-manager.php | 8 +- .../local-tasks/class-local-task-factory.php | 73 ++++++++++++------- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/classes/suggested-tasks/class-local-tasks-manager.php b/classes/suggested-tasks/class-local-tasks-manager.php index 569a50477..9d515524e 100644 --- a/classes/suggested-tasks/class-local-tasks-manager.php +++ b/classes/suggested-tasks/class-local-tasks-manager.php @@ -7,6 +7,8 @@ namespace Progress_Planner\Suggested_Tasks; +use Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory; + /** * Local_Tasks_Manager class. */ @@ -144,7 +146,7 @@ public function evaluate_tasks() { * @return bool|string */ public function evaluate_task( $task_id ) { - $task_object = \Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory::create( $task_id ); + $task_object = ( new Local_Task_Factory( $task_id ) )->get_task(); $task_provider = $this->get_task_provider( $task_object->get_provider_type() ); if ( ! $task_provider ) { @@ -162,7 +164,7 @@ public function evaluate_task( $task_id ) { * @return array|false */ public function get_task_details( $task_id ) { - $task_object = \Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory::create( $task_id ); + $task_object = ( new Local_Task_Factory( $task_id ) )->get_task(); $task_provider = $this->get_task_provider( $task_object->get_provider_type() ); if ( ! $task_provider ) { @@ -180,7 +182,7 @@ public function get_task_details( $task_id ) { * @return array */ public function get_data_from_task_id( $task_id ) { - $task_object = \Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory::create( $task_id ); + $task_object = ( new Local_Task_Factory( $task_id ) )->get_task(); return $task_object->get_data(); } diff --git a/classes/suggested-tasks/local-tasks/class-local-task-factory.php b/classes/suggested-tasks/local-tasks/class-local-task-factory.php index 04761c785..d1a56d889 100644 --- a/classes/suggested-tasks/local-tasks/class-local-task-factory.php +++ b/classes/suggested-tasks/local-tasks/class-local-task-factory.php @@ -11,46 +11,63 @@ * Local task factory. */ class Local_Task_Factory { + /** - * Create a task. + * The task ID. + * + * @var string + */ + private $task_id; + + /** + * Constructor. * * @param string $task_id The task ID. + */ + public function __construct( string $task_id ) { + $this->task_id = $task_id; + } + + /** + * Get the task. * * @return \Progress_Planner\Suggested_Tasks\Local_Tasks\Task_Local */ - public static function create( string $task_id ): Task_Local { - if ( str_contains( $task_id, '|' ) ) { - // Parse detailed format. - $parts = \explode( '|', $task_id ); - $data = []; - foreach ( $parts as $part ) { - $part = \explode( '/', $part ); - if ( 2 !== \count( $part ) ) { - continue; - } - $data[ $part[0] ] = ( \is_numeric( $part[1] ) ) - ? (int) $part[1] - : $part[1]; - } - \ksort( $data ); + public function get_task(): Task_Local { - // Convert (int) 1 and (int) 0 to (bool) true and (bool) false. - if ( isset( $data['long'] ) ) { - $data['long'] = (bool) $data['long']; + // Parse simple format, e.g. 'update-core-202449'. + if ( ! str_contains( $this->task_id, '|' ) ) { + $last_pos = strrpos( $this->task_id, '-' ); + if ( false === $last_pos ) { + return new Task_Local( [ 'task_id' => $this->task_id ] ); } - $data['task_id'] = $task_id; - } else { - $data = []; + return new Task_Local( + [ + 'type' => substr( $this->task_id, 0, $last_pos ), + 'year_week' => substr( $this->task_id, $last_pos + 1 ), + ] + ); + } + + $data = [ 'task_id' => $this->task_id ]; - // Parse simple format, e.g. 'update-core-202449'. - $last_pos = strrpos( $task_id, '-' ); - if ( false !== $last_pos ) { - $data['type'] = substr( $task_id, 0, $last_pos ); - $data['year_week'] = substr( $task_id, $last_pos + 1 ); + // Parse detailed format. + $parts = \explode( '|', $this->task_id ); + foreach ( $parts as $part ) { + $part = \explode( '/', $part ); + if ( 2 !== \count( $part ) ) { + continue; } + $data[ $part[0] ] = ( \is_numeric( $part[1] ) ) + ? (int) $part[1] + : $part[1]; + } + \ksort( $data ); - $data['task_id'] = $task_id; + // Convert (int) 1 and (int) 0 to (bool) true and (bool) false. + if ( isset( $data['long'] ) ) { + $data['long'] = (bool) $data['long']; } return new Task_Local( $data ); From 2fa297341f331461703fd089cbcc3763fe24683b Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Wed, 18 Dec 2024 08:56:17 +0100 Subject: [PATCH 2/2] update local tasks factory call --- .../local-tasks/providers/class-content-update.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/classes/suggested-tasks/local-tasks/providers/class-content-update.php b/classes/suggested-tasks/local-tasks/providers/class-content-update.php index 757dbd366..6df978a63 100644 --- a/classes/suggested-tasks/local-tasks/providers/class-content-update.php +++ b/classes/suggested-tasks/local-tasks/providers/class-content-update.php @@ -7,6 +7,8 @@ namespace Progress_Planner\Suggested_Tasks\Local_Tasks\Providers; +use Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory; + /** * Add tasks for content updates. */ @@ -179,7 +181,7 @@ public function transition_post_status( $new_status, $old_status, $post ) { } foreach ( \progress_planner()->get_suggested_tasks()->get_local()->get_pending_tasks() as $task_id ) { - $task_object = \Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory::create( $task_id ); + $task_object = ( new Local_Task_Factory( $task_id ) )->get_task(); $task_data = $task_object->get_data(); if ( self::TYPE === $task_data['type'] && ( isset( $task_data['post_id'] ) && (int) $task_data['post_id'] === (int) $post->ID ) ) { // Remove the task from the pending local tasks list.