Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up local tasks #200

Merged
merged 6 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Fixed:

* Duplicate weekly suggested tasks.
* Fixed the REST API endpoint for getting stats.

= 1.0.1 =
Expand Down
46 changes: 46 additions & 0 deletions classes/suggested-tasks/class-local-tasks-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function __construct() {

\add_filter( 'progress_planner_suggested_tasks_items', [ $this, 'inject_tasks' ] );
\add_action( 'plugins_loaded', [ $this, 'add_plugin_integration' ] );

// Add the cleanup action.
\add_action( 'admin_init', [ $this, 'cleanup_pending_tasks' ] );
}

/**
Expand Down Expand Up @@ -229,4 +232,47 @@ public function remove_pending_task( $task ) {
$tasks = \array_diff( $tasks, [ $task ] );
return \update_option( self::OPTION_NAME, $tasks );
}

/**
* Remove all tasks which have date set to the previous week.
* Tasks for the current week will be added automatically.
*
* @return void
*/
public function cleanup_pending_tasks() {

$cleanup_recently_performed = \progress_planner()->get_cache()->get( 'cleanup_pending_tasks' );

if ( $cleanup_recently_performed ) {
return;
}

$tasks = (array) $this->get_pending_tasks();

if ( empty( $tasks ) ) {
return;
}

$task_count = count( $tasks );

$tasks = \array_filter(
$tasks,
function ( $task ) {
$task_object = ( new Local_Task_Factory( $task ) )->get_task();
$task_data = $task_object->get_data();

if ( isset( $task_data['year_week'] ) ) {
return \gmdate( 'YW' ) === $task_data['year_week'];
}

return true;
}
);

if ( count( $tasks ) !== $task_count ) {
\update_option( self::OPTION_NAME, $tasks );
}

\progress_planner()->get_cache()->set( 'cleanup_pending_tasks', true, DAY_IN_SECONDS );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ public function get_task(): Task_Local {
return new Task_Local( [ 'task_id' => $this->task_id ] );
}

$type = substr( $this->task_id, 0, $last_pos );
$task_suffix = substr( $this->task_id, $last_pos + 1 );

$task_suffix_key = 'remote-task' === $type ? 'remote_task_id' : 'year_week';

return new Task_Local(
[
'type' => substr( $this->task_id, 0, $last_pos ),
'year_week' => substr( $this->task_id, $last_pos + 1 ),
'type' => $type,
$task_suffix_key => $task_suffix,
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Progress_Planner\Suggested_Tasks\Local_Tasks\Providers;

use Progress_Planner\Suggested_Tasks\Local_Tasks\Local_Task_Factory;
/**
* Add tasks for Core updates.
*/
Expand Down Expand Up @@ -54,7 +55,10 @@ public function evaluate_task( $task_id ) {
require_once ABSPATH . 'wp-admin/includes/update.php'; // @phpstan-ignore requireOnce.fileNotFound
}

if ( 0 === strpos( $task_id, self::TYPE ) && 0 === \wp_get_update_data()['counts']['total'] ) {
$task_object = ( new Local_Task_Factory( $task_id ) )->get_task();
$task_data = $task_object->get_data();

if ( $task_data['type'] === self::TYPE && \gmdate( 'YW' ) === $task_data['year_week'] && 0 === \wp_get_update_data()['counts']['total'] ) {
return $task_id;
}
return false;
Expand Down Expand Up @@ -136,7 +140,9 @@ public function is_task_type_snoozed() {
}

foreach ( $snoozed as $task ) {
if ( self::TYPE === $task['id'] ) {
$task_object = ( new Local_Task_Factory( $task['id'] ) )->get_task();
$task_data = $task_object->get_data();
if ( $task_data['type'] === self::TYPE ) {
return true;
}
}
Expand Down
36 changes: 35 additions & 1 deletion tests/phpunit/test-class-suggested-tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Suggested_Tasks_Test extends \WP_UnitTestCase {
* @return void
*/
public function set_up() {
$this->suggested_tasks = new Suggested_Tasks();
$this->suggested_tasks = \progress_planner()->get_suggested_tasks();
}

/**
Expand Down Expand Up @@ -96,4 +96,38 @@ public function test_mark_task_as_snoozed() {
*/
public function test_maybe_unsnooze_tasks() {
}

/**
* Test the task_cleanup method.
*
* @return void
*/
public function test_task_cleanup() {
// Tasks that should not be removed.
$tasks_to_keep = [
'remote-task-1234',
'post_id/14|type/update-post',
'date/202452|long/0|type/create-post',
'update-core-' . \gmdate( 'YW' ),
'settings-saved-' . \gmdate( 'YW' ),
];

foreach ( $tasks_to_keep as $task_id ) {
$this->suggested_tasks->get_local()->add_pending_task( $task_id );
}

// Tasks that should be removed.
$tasks_to_remove = [
'update-core-202451',
'settings-saved-202451',
];

foreach ( $tasks_to_remove as $task_id ) {
$this->suggested_tasks->get_local()->add_pending_task( $task_id );
}

$this->suggested_tasks->get_local()->cleanup_pending_tasks();

$this->assertEquals( count( $tasks_to_keep ), \count( $this->suggested_tasks->get_local()->get_pending_tasks() ) );
}
}
Loading