PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / task-list / infrastructure / tasks-collectors / tasks-collector.php

tasks-collector.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.5, at src/task-list/infrastructure/tasks-collectors/tasks-collector.php

168 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Yoast\WP\SEO\Task_List\Domain\Components\Call_To_Action_Entry;
7 use Yoast\WP\SEO\Task_List\Domain\Exceptions\Incorrect_Child_Task_Usage_Exception;
8 use Yoast\WP\SEO\Task_List\Domain\Exceptions\Invalid_Tasks_Exception;
9 use Yoast\WP\SEO\Task_List\Domain\Tasks\Child_Task_Interface;
10 use Yoast\WP\SEO\Task_List\Domain\Tasks\Completeable_Task_Interface;
11 use Yoast\WP\SEO\Task_List\Domain\Tasks\Parent_Task_Interface;
12 use Yoast\WP\SEO\Task_List\Domain\Tasks\Post_Type_Task_Interface;
13 use Yoast\WP\SEO\Task_List\Domain\Tasks\Task_Interface;
14 use Yoast\WP\SEO\Tracking\Infrastructure\Tracking_Link_Adapter;
15
16 /**
17 * Manages the collection of tasks.
18 */
19 class Tasks_Collector implements Tasks_Collector_Interface {
20
21 /**
22 * Holds all the tasks.
23 *
24 * @var Task_Interface[]
25 */
26 private $tasks;
27
28 /**
29 * Holds the tracking link adapter.
30 *
31 * @var Tracking_Link_Adapter
32 */
33 private $tracking_link_adapter;
34
35 /**
36 * Constructs the collector.
37 *
38 * @param Task_Interface ...$tasks All the tasks.
39 *
40 * @throws Incorrect_Child_Task_Usage_Exception If a child task is added.
41 */
42 public function __construct( Task_Interface ...$tasks ) {
43 $tasks_with_id = [];
44 foreach ( $tasks as $task ) {
45 if ( $task instanceof Child_Task_Interface ) {
46 // Implementations of the Child_Task_Interface are excluded at the DI level, but let's make sure to also throw an exception.
47 throw new Incorrect_Child_Task_Usage_Exception( \esc_html( $task->get_id() ) );
48 }
49
50 if ( $task instanceof Post_Type_Task_Interface ) {
51 continue;
52 }
53 $tasks_with_id[ $task->get_id() ] = $task;
54 }
55
56 $this->tasks = $tasks_with_id;
57 }
58
59 /**
60 * Sets the tracking link adapter.
61 *
62 * @required
63 *
64 * @codeCoverageIgnore - Is handled by DI-container.
65 *
66 * @param Tracking_Link_Adapter $tracking_link_adapter The tracking link adapter.
67 *
68 * @return void
69 */
70 public function set_tracking_link_adapter( Tracking_Link_Adapter $tracking_link_adapter ) {
71 $this->tracking_link_adapter = $tracking_link_adapter;
72 }
73
74 /**
75 * Gets a task.
76 *
77 * @param string $task_id The task ID.
78 *
79 * @return Task_Interface The given task.
80 */
81 public function get_task( string $task_id ): ?Task_Interface {
82 $all_tasks = $this->get_tasks();
83 return ( $all_tasks[ $task_id ] ?? null );
84 }
85
86 /**
87 * Gets a completeable task.
88 *
89 * @param string $task_id The task ID.
90 *
91 * @return Task_Interface The given task.
92 */
93 public function get_completeable_task( string $task_id ): ?Completeable_Task_Interface {
94 $all_tasks = $this->get_tasks();
95 $task = ( $all_tasks[ $task_id ] ?? null );
96
97 if ( ! $task instanceof Completeable_Task_Interface ) {
98 return null;
99 }
100
101 return $task;
102 }
103
104 /**
105 * Gets the tasks.
106 *
107 * @return array<string, array<string, Task_Interface>> The tasks.
108 *
109 * @throws Invalid_Tasks_Exception If an invalid task is added.
110 */
111 public function get_tasks(): array {
112 /**
113 * Filter: 'wpseo_task_list_tasks' - Allows adding more tasks to the task list.
114 *
115 * @param array<string, array<string, Task_Interface>> $tasks The tasks for the task list.
116 */
117 $tasks = \apply_filters( 'wpseo_task_list_tasks', $this->tasks );
118
119 $extra_tasks = [];
120 foreach ( $tasks as $task_id => $task ) {
121 if ( ! $task instanceof Task_Interface ) {
122 throw new Invalid_Tasks_Exception();
123 }
124
125 if ( $task->is_valid() === false ) {
126 unset( $tasks[ $task_id ] );
127 continue;
128 }
129
130 // Generate child tasks for parent tasks.
131 if ( $task instanceof Parent_Task_Interface ) {
132 \array_push( $extra_tasks, ...$task->generate_child_tasks() );
133 }
134 }
135
136 return \array_merge( $tasks, $extra_tasks );
137 }
138
139 /**
140 * Gets the tasks data.
141 *
142 * @return array<string, array<string, string|bool>> The tasks data.
143 */
144 public function get_tasks_data(): array {
145 $tasks = $this->get_tasks();
146 $tasks_data = [];
147
148 foreach ( $tasks as $task ) {
149 // We need to enhance the links of the CTA first, if that exists.
150 if ( $task->get_call_to_action() !== null ) {
151 $task->set_enhanced_call_to_action(
152 new Call_To_Action_Entry(
153 $task->get_call_to_action()->get_label(),
154 $task->get_call_to_action()->get_type(),
155 $this->tracking_link_adapter->create_tracking_link_for_tasks(
156 $task->get_call_to_action()->get_href(),
157 ),
158 ),
159 );
160 }
161
162 $tasks_data[ $task->get_id() ] = $task->to_array();
163 }
164
165 return $tasks_data;
166 }
167 }
168