Skip to content
Draft
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
70 changes: 70 additions & 0 deletions inc/options/class-wpseo-option-task-list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Internals\Options
*/

/**
* Option: wpseo_task_list.
*/
class WPSEO_Option_Task_List extends WPSEO_Option {

/**
* Option name.
*
* @var string
*/
public $option_name = 'wpseo_task_list';

/**
* Array of defaults for the option.
*
* Shouldn't be requested directly, use $this->get_defaults();
*
* @var array<string, array<string, string>>
*/
protected $defaults = [
'manually_completed_tasks' => [],
];

/**
* Get the singleton instance of this class.
*
* @return object
*/
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* All concrete classes must contain a validate_option() method which validates all
* values within the option.
*
* @param array<string, array<string, string>> $dirty New value for the option.
* @param array<string, array<string, string>> $clean Clean value for the option, normally the defaults.
* @param array<string, array<string, string>> $old Old value of the option.
*
* @return array<string, array<string, string>> The clean option with the saved value.
*/
protected function validate_option( $dirty, $clean, $old ) {

foreach ( $clean as $key => $value ) {
switch ( $key ) {
case 'manually_completed_tasks':
$clean[ $key ] = $old[ $key ];

if ( isset( $dirty[ $key ] ) && is_array( $dirty[ $key ] ) ) {
$clean[ $key ] = $dirty[ $key ];
}
break;
}
}

return $clean;
}
}
1 change: 1 addition & 0 deletions inc/options/class-wpseo-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class WPSEO_Options {
'wpseo_ms' => 'WPSEO_Option_MS',
'wpseo_taxonomy_meta' => 'WPSEO_Taxonomy_Meta',
'wpseo_llmstxt' => 'WPSEO_Option_Llmstxt',
'wpseo_task_list' => 'WPSEO_Option_Task_List',
'wpseo_tracking_only' => 'WPSEO_Option_Tracking_Only',
];

Expand Down
37 changes: 35 additions & 2 deletions src/task-list/application/tasks-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Yoast\WP\SEO\Task_List\Application;

use Yoast\WP\SEO\Helpers\Options_Helper;
use Yoast\WP\SEO\Task_List\Infrastructure\Tasks_Collectors\Cached_Tasks_Collector;

/**
Expand All @@ -16,13 +17,25 @@ class Tasks_Repository {
*/
private $tasks_collector;

/**
* The options helper.
*
* @var Options_Helper
*/
private $options_helper;

/**
* The constructor.
*
* @param Cached_Tasks_Collector $tasks_collector The tasks collector.
* @param Options_Helper $options_helper The options helper.
*/
public function __construct( Cached_Tasks_Collector $tasks_collector ) {
public function __construct(
Cached_Tasks_Collector $tasks_collector,
Options_Helper $options_helper
) {
$this->tasks_collector = $tasks_collector;
$this->options_helper = $options_helper;
}

/**
Expand All @@ -31,6 +44,26 @@ public function __construct( Cached_Tasks_Collector $tasks_collector ) {
* @return array<string, array<string, string|bool>> The tasks list.
*/
public function get_tasks_data(): array {
return $this->tasks_collector->get_tasks_data();
$tasks_data = $this->tasks_collector->get_tasks_data();
$tasks_data = $this->apply_manual_completion_overrides( $tasks_data );

return $tasks_data;
}

/**
* Applies manual completion overrides to the collected task data.
*
* @param array<string, array<string, string|bool>> $tasks_data The collected tasks data.
*
* @return array<string, array<string, string|bool>> The tasks data with manual completion overrides applied.
*/
private function apply_manual_completion_overrides( array $tasks_data ): array {
$manually_completed_tasks_ids = $this->options_helper->get( 'manually_completed_tasks', [] );

foreach ( $manually_completed_tasks_ids as $manually_completed_task_id ) {
$tasks_data[ $manually_completed_task_id ]['isCompleted'] = true;
}

return $tasks_data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Task_List\Infrastructure\Endpoints;

use Yoast\WP\SEO\Task_List\Domain\Endpoint\Endpoint_Interface;
use Yoast\WP\SEO\Task_List\User_Interface\Tasks\Manually_Complete_Task_Route;

/**
* Represents the manually complete task endpoint.
*/
class Manually_Complete_Task_Endpoint implements Endpoint_Interface {

/**
* Gets the name.
*
* @return string
*/
public function get_name(): string {
return 'setTaskCompletion';
}

/**
* Gets the namespace.
*
* @return string
*/
public function get_namespace(): string {
return Manually_Complete_Task_Route::ROUTE_NAMESPACE;
}

/**
* Gets the route.
*
* @return string
*/
public function get_route(): string {
return Manually_Complete_Task_Route::ROUTE_NAME;
}

/**
* Gets the URL.
*
* @return string
*/
public function get_url(): string {
return \rest_url( $this->get_namespace() . $this->get_route() );
}
}
80 changes: 80 additions & 0 deletions src/task-list/infrastructure/manual-task-completion-repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Task_List\Infrastructure;

use Yoast\WP\SEO\Helpers\Options_Helper;

/**
* Stores manual task completion overrides.
*/
class Manual_Task_Completion_Repository {

/**
* Holds the options helper.
*
* @var Options_Helper
*/
private $options_helper;

/**
* Constructs the repository.
*
* @param Options_Helper $options_helper The options helper.
*/
public function __construct( Options_Helper $options_helper ) {
$this->options_helper = $options_helper;
}

/**
* Checks whether a task is manually marked as completed.
*
* @param string $task_id The task ID.
*
* @return bool Whether the task is manually completed.
*/
public function is_task_manually_completed( string $task_id ): bool {
$completed_task_ids = $this->options_helper->get( 'manually_completed_tasks', [] );

return \in_array( $task_id, $completed_task_ids, true );
}

/**
* Marks a task as manually completed.
*
* @param string $task_id The task ID.
*
* @return void
*/
public function set_task_manually_completed( string $task_id ): void {
$completed_task_ids = $this->options_helper->get( 'manually_completed_tasks', [] );

// Avoid duplicates.
if ( \in_array( $task_id, $completed_task_ids, true ) ) {
return;
}

$completed_task_ids[] = $task_id;
$this->options_helper->set( 'manually_completed_tasks', $completed_task_ids );
}

/**
* Clears the manual completion state for a task.
*
* @param string $task_id The task ID.
*
* @return void
*/
public function clear_task_manual_completion( string $task_id ): void {
$completed_task_ids = $this->options_helper->get( 'manually_completed_tasks', [] );
$completed_task_ids = \array_values(
\array_filter(
$completed_task_ids,
static function ( $id ) use ( $task_id ) {
return $id !== $task_id;
}
)
);

$this->options_helper->set( 'manually_completed_tasks', $completed_task_ids );
}
}
Loading