Skip to content

Commit

Permalink
Merge branch 'develop' into filip/onboard-on-page
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath authored Dec 18, 2024
2 parents ffad361 + 8dcf210 commit 56d5d44
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
8 changes: 5 additions & 3 deletions classes/suggested-tasks/class-local-tasks-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Progress_Planner\Suggested_Tasks;

use Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory;

/**
* Local_Tasks_Manager class.
*/
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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();
}
Expand Down
73 changes: 45 additions & 28 deletions classes/suggested-tasks/local-tasks/class-local-task-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 56d5d44

Please sign in to comment.