| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Task_List\Infrastructure\Tasks_Collectors; |
| 5 |
|
| 6 |
use WPSEO_Utils; |
| 7 |
|
| 8 |
/** |
| 9 |
* Manages the cached collection of tasks. |
| 10 |
*/ |
| 11 |
class Cached_Tasks_Collector implements Tasks_Collector_Interface { |
| 12 |
|
| 13 |
public const TASKS_TRANSIENT = 'wpseo_task_list_tasks'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Holds the tasks collector. |
| 17 |
* |
| 18 |
* @var Tasks_Collector |
| 19 |
*/ |
| 20 |
private $tasks_collector; |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructs the cached collector. |
| 24 |
* |
| 25 |
* @param Tasks_Collector $tasks_collector The tasks collector. |
| 26 |
*/ |
| 27 |
public function __construct( Tasks_Collector $tasks_collector ) { |
| 28 |
$this->tasks_collector = $tasks_collector; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Gets the tasks data. |
| 33 |
* |
| 34 |
* @TODO: Maybe this can be improved at some point by caching only the is_completed info instead of all the task data. |
| 35 |
* |
| 36 |
* @return array<string, array<string, string|bool>> The tasks data. |
| 37 |
*/ |
| 38 |
public function get_tasks_data(): array { |
| 39 |
$cached_tasks_data = \get_transient( self::TASKS_TRANSIENT ); |
| 40 |
|
| 41 |
if ( $cached_tasks_data !== false ) { |
| 42 |
return \json_decode( $cached_tasks_data, true ); |
| 43 |
} |
| 44 |
|
| 45 |
$tasks_data = $this->tasks_collector->get_tasks_data(); |
| 46 |
\set_transient( self::TASKS_TRANSIENT, WPSEO_Utils::format_json_encode( $tasks_data ), \MINUTE_IN_SECONDS ); |
| 47 |
|
| 48 |
return $tasks_data; |
| 49 |
} |
| 50 |
} |
| 51 |
|