PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.6
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 / domain / tasks / parent-task-trait.php

parent-task-trait.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.6, at src/task-list/domain/tasks/parent-task-trait.php

109 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3 namespace Yoast\WP\SEO\Task_List\Domain\Tasks;
4
5 /**
6 * Trait for parent task functionality.
7 * Provides the implementation for Parent_Task_Interface methods.
8 */
9 trait Parent_Task_Trait {
10
11 /**
12 * The child tasks associated with the task.
13 *
14 * @var Child_Task_Interface[]
15 */
16 protected $child_tasks = [];
17
18 /**
19 * Returns the child tasks associated with the task.
20 *
21 * @return Child_Task_Interface[]
22 */
23 public function get_child_tasks(): array {
24 return $this->child_tasks;
25 }
26
27 /**
28 * Sets the child tasks associated with the task.
29 *
30 * @param Child_Task_Interface[] $child_tasks The child tasks to set.
31 *
32 * @return void
33 */
34 public function set_child_tasks( array $child_tasks ): void {
35 $this->child_tasks = $child_tasks;
36 }
37
38 /**
39 * Returns whether this task is completed.
40 * The parent task is completed when all child tasks are completed.
41 *
42 * @return bool Whether this task is completed.
43 */
44 public function get_is_completed(): bool {
45 $child_tasks = $this->child_tasks;
46
47 foreach ( $child_tasks as $task ) {
48 if ( ! $task->get_is_completed() ) {
49 return false;
50 }
51 }
52
53 return true;
54 }
55
56 /**
57 * Generates and sets the child tasks.
58 *
59 * @return Child_Task_Interface[] The generated child tasks.
60 */
61 public function generate_child_tasks(): array {
62 $this->child_tasks = $this->populate_child_tasks();
63
64 return $this->child_tasks;
65 }
66
67 /**
68 * Returns the task's duration, calculated by summing durations of incomplete child tasks.
69 *
70 * @return int The total duration in minutes.
71 */
72 public function get_duration(): int {
73 $child_tasks = $this->get_child_tasks();
74
75 if ( empty( $child_tasks ) ) {
76 return 0;
77 }
78
79 $total_duration = 0;
80 foreach ( $child_tasks as $child_task ) {
81 if ( ! $child_task->get_is_completed() ) {
82 $total_duration += $child_task->get_duration();
83 }
84 }
85
86 return $total_duration;
87 }
88
89 /**
90 * Returns an array representation of the task data.
91 * When used in a class extending Abstract_Task, this will call the parent's to_array()
92 * and add the parent task flag.
93 *
94 * @return array<string, string|bool> Returns in an array format.
95 */
96 public function to_array(): array {
97 $data = [];
98
99 $parent_class = \get_parent_class( $this );
100 if ( $parent_class !== false && \method_exists( $parent_class, 'to_array' ) ) {
101 $data = parent::to_array();
102 }
103
104 $data['isParentTask'] = true;
105
106 return $data;
107 }
108 }
109