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 / user-interface / tasks / get-tasks-route.php

get-tasks-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.5, at src/task-list/user-interface/tasks/get-tasks-route.php

158 lines 3.5 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\User_Interface\Tasks;
5
6 use Exception;
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\Application\Tasks_Repository;
13 use Yoast\WP\SEO\Tracking\Application\Action_Tracker;
14
15 /**
16 * Get tasks route.
17 */
18 final class Get_Tasks_Route implements Route_Interface {
19
20 /**
21 * The namespace of the route.
22 *
23 * @var string
24 */
25 public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
26
27 /**
28 * The prefix of the route.
29 *
30 * @var string
31 */
32 public const ROUTE_NAME = '/get_tasks';
33
34 /**
35 * The task repository.
36 *
37 * @var Tasks_Repository
38 */
39 private $tasks_repository;
40
41 /**
42 * Holds the capability helper instance.
43 *
44 * @var Capability_Helper
45 */
46 private $capability_helper;
47
48 /**
49 * Holds the action tracker instance.
50 *
51 * @var Action_Tracker
52 */
53 private $action_tracker;
54
55 /**
56 * Returns the needed conditionals.
57 *
58 * @return array<string> The conditionals that must be met to load this.
59 */
60 public static function get_conditionals(): array {
61 return [
62 Task_List_Enabled_Conditional::class,
63 ];
64 }
65
66 /**
67 * The constructor.
68 *
69 * @param Tasks_Repository $tasks_repository The repository for all tasks.
70 * @param Capability_Helper $capability_helper The capability helper.
71 * @param Action_Tracker $action_tracker The action tracker.
72 */
73 public function __construct(
74 Tasks_Repository $tasks_repository,
75 Capability_Helper $capability_helper,
76 Action_Tracker $action_tracker
77 ) {
78 $this->tasks_repository = $tasks_repository;
79 $this->capability_helper = $capability_helper;
80 $this->action_tracker = $action_tracker;
81 }
82
83 /**
84 * Registers routes for scores.
85 *
86 * @return void
87 */
88 public function register_routes() {
89 \register_rest_route(
90 self::ROUTE_NAMESPACE,
91 self::ROUTE_NAME,
92 [
93 [
94 'methods' => 'GET',
95 'callback' => [ $this, 'get_tasks' ],
96 'permission_callback' => [ $this, 'permission_manage_options' ],
97 'args' => [
98 'options' => [
99 'type' => 'object',
100 'required' => false,
101 'properties' => [
102 'filter' => [
103 'type' => 'string',
104 'required' => false,
105 'sanitize_callback' => 'sanitize_text_field',
106 ],
107 'limit' => [
108 'type' => 'int',
109 'required' => false,
110 'sanitize_callback' => 'absint',
111 ],
112 ],
113 ],
114 ],
115 ],
116 ],
117 );
118 }
119
120 /**
121 * Gets tasks with their information.
122 *
123 * @return WP_REST_Response The success or failure response.
124 */
125 public function get_tasks(): WP_REST_Response {
126 try {
127 $this->action_tracker->track_version_for_performed_action( 'task_list_first_opened_on' );
128
129 $tasks_data = $this->tasks_repository->get_tasks_data();
130 } catch ( Exception $exception ) {
131 return new WP_REST_Response(
132 [
133 'success' => false,
134 'error' => $exception->getMessage(),
135 ],
136 $exception->getCode(),
137 );
138 }
139
140 return new WP_REST_Response(
141 [
142 'success' => true,
143 'tasks' => $tasks_data,
144 ],
145 200,
146 );
147 }
148
149 /**
150 * Permission callback.
151 *
152 * @return bool True when user has the 'wpseo_manage_options' capability.
153 */
154 public function permission_manage_options() {
155 return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
156 }
157 }
158