• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 8218b111533b92ec47f94130a94bcd0102263a45

01 Dec 2025 09:48AM UTC coverage: 53.092%. First build
8218b111533b92ec47f94130a94bcd0102263a45

push

github

web-flow
Merge pull request #22759 from Yoast/feature/task-list

Feature/task list

8697 of 16050 branches covered (54.19%)

Branch coverage included in aggregate %.

98 of 605 new or added lines in 51 files covered. (16.2%)

32413 of 61381 relevant lines covered (52.81%)

46976.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/task-list/user-interface/tasks/complete-task-route.php
1
<?php
2
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3
namespace Yoast\WP\SEO\Task_List\User_Interface\Tasks;
4

5
use Exception;
6
use WP_REST_Request;
7
use WP_REST_Response;
8
use Yoast\WP\SEO\Conditionals\Task_List_Enabled_Conditional;
9
use Yoast\WP\SEO\Helpers\Capability_Helper;
10
use Yoast\WP\SEO\Main;
11
use Yoast\WP\SEO\Routes\Route_Interface;
12
use Yoast\WP\SEO\Task_List\Domain\Exceptions\Task_Not_Found_Exception;
13
use Yoast\WP\SEO\Task_List\Infrastructure\Tasks_Collectors\Cached_Tasks_Collector;
14
use Yoast\WP\SEO\Task_List\Infrastructure\Tasks_Collectors\Tasks_Collector;
15

16
/**
17
 * Tasks route.
18
 */
19
final class Complete_Task_Route implements Route_Interface {
20

21
        /**
22
         * The namespace of the route.
23
         *
24
         * @var string
25
         */
26
        public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
27

28
        /**
29
         * The prefix of the route.
30
         *
31
         * @var string
32
         */
33
        public const ROUTE_NAME = '/complete_task';
34

35
        /**
36
         * The task collector.
37
         *
38
         * @var Tasks_Collector
39
         */
40
        private $tasks_collector;
41

42
        /**
43
         * Holds the capability helper instance.
44
         *
45
         * @var Capability_Helper
46
         */
47
        private $capability_helper;
48

49
        /**
50
         * Returns the needed conditionals.
51
         *
52
         * @return array<string> The conditionals that must be met to load this.
53
         */
NEW
54
        public static function get_conditionals(): array {
×
NEW
55
                return [
×
NEW
56
                        Task_List_Enabled_Conditional::class,
×
NEW
57
                ];
×
58
        }
59

60
        /**
61
         * The constructor.
62
         *
63
         * @param Tasks_Collector   $tasks_collector   The collector for all tasks.
64
         * @param Capability_Helper $capability_helper The capability helper.
65
         */
NEW
66
        public function __construct(
×
67
                Tasks_Collector $tasks_collector,
68
                Capability_Helper $capability_helper
69
        ) {
NEW
70
                $this->tasks_collector   = $tasks_collector;
×
NEW
71
                $this->capability_helper = $capability_helper;
×
72
        }
73

74
        /**
75
         * Registers routes for scores.
76
         *
77
         * @return void
78
         */
NEW
79
        public function register_routes() {
×
NEW
80
                \register_rest_route(
×
NEW
81
                        self::ROUTE_NAMESPACE,
×
NEW
82
                        self::ROUTE_NAME,
×
NEW
83
                        [
×
NEW
84
                                [
×
NEW
85
                                        'methods'             => 'POST',
×
NEW
86
                                        'callback'            => [ $this, 'complete_task' ],
×
NEW
87
                                        'permission_callback' => [ $this, 'permission_manage_options' ],
×
NEW
88
                                        'args'                => [
×
NEW
89
                                                'options' => [
×
NEW
90
                                                        'type'       => 'object',
×
NEW
91
                                                        'required'   => true,
×
NEW
92
                                                        'properties' => [
×
NEW
93
                                                                'task' => [
×
NEW
94
                                                                        'type'              => 'string',
×
NEW
95
                                                                        'required'          => true,
×
NEW
96
                                                                        'sanitize_callback' => 'sanitize_text_field',
×
NEW
97
                                                                ],
×
NEW
98
                                                        ],
×
NEW
99
                                                ],
×
NEW
100
                                        ],
×
NEW
101
                                ],
×
NEW
102
                        ]
×
NEW
103
                );
×
104
        }
105

106
        /**
107
         * Completes a task.
108
         *
109
         * @param WP_REST_Request $request The request object.
110
         *
111
         * @return WP_REST_Response The success or failure response.
112
         *
113
         * @throws Task_Not_Found_Exception When the given task name is not implemented yet.
114
         */
NEW
115
        public function complete_task( WP_REST_Request $request ): WP_REST_Response {
×
116
                try {
NEW
117
                        $task_name = $request->get_param( 'options' )['task'];
×
NEW
118
                        $task      = $this->tasks_collector->get_completeable_task( $task_name );
×
119

NEW
120
                        if ( ! $task ) {
×
NEW
121
                                throw new Task_Not_Found_Exception();
×
122
                        }
123

NEW
124
                        $task->complete_task();
×
NEW
125
                } catch ( Exception $exception ) {
×
NEW
126
                        return new WP_REST_Response(
×
NEW
127
                                [
×
NEW
128
                                        'success' => false,
×
NEW
129
                                        'error'   => $exception->getMessage(),
×
NEW
130
                                ],
×
NEW
131
                                $exception->getCode()
×
NEW
132
                        );
×
133
                }
134

NEW
135
                \delete_transient( Cached_Tasks_Collector::TASKS_TRANSIENT );
×
136

NEW
137
                return new WP_REST_Response(
×
NEW
138
                        [
×
NEW
139
                                'success' => true,
×
NEW
140
                        ],
×
NEW
141
                        200
×
NEW
142
                );
×
143
        }
144

145
        /**
146
         * Permission callback.
147
         *
148
         * @return bool True when user has the 'wpseo_manage_options' capability.
149
         */
NEW
150
        public function permission_manage_options() {
×
NEW
151
                return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
×
152
        }
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc