Yoast\WP\SEO\Task_List\User_Interface\Tasks
Complete_Task_Route{} │ final │ Yoast 1.0└─ Route_Interface
Tasks route.
Хуков нет.
Использование
$Complete_Task_Route = new Complete_Task_Route(); // use class methods
Методы
- public __construct(
- public complete_task( WP_REST_Request $request )
- public static get_conditionals()
- public permission_manage_options()
- public register_routes()
Код Complete_Task_Route{} Complete Task Route{} Yoast 27.8
final class Complete_Task_Route implements Route_Interface {
/**
* The namespace of the route.
*
* @var string
*/
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
/**
* The prefix of the route.
*
* @var string
*/
public const ROUTE_NAME = '/complete_task';
/**
* The task collector.
*
* @var Tasks_Collector
*/
private $tasks_collector;
/**
* Holds the capability helper instance.
*
* @var Capability_Helper
*/
private $capability_helper;
/**
* Holds the action tracker instance.
*
* @var Action_Tracker
*/
private $action_tracker;
/**
* Returns the needed conditionals.
*
* @return array<string> The conditionals that must be met to load this.
*/
public static function get_conditionals(): array {
return [
Task_List_Enabled_Conditional::class,
];
}
/**
* The constructor.
*
* @param Tasks_Collector $tasks_collector The collector for all tasks.
* @param Capability_Helper $capability_helper The capability helper.
* @param Action_Tracker $action_tracker The action tracker.
*/
public function __construct(
Tasks_Collector $tasks_collector,
Capability_Helper $capability_helper,
Action_Tracker $action_tracker
) {
$this->tasks_collector = $tasks_collector;
$this->capability_helper = $capability_helper;
$this->action_tracker = $action_tracker;
}
/**
* Registers routes for scores.
*
* @return void
*/
public function register_routes() {
\register_rest_route(
self::ROUTE_NAMESPACE,
self::ROUTE_NAME,
[
[
'methods' => 'POST',
'callback' => [ $this, 'complete_task' ],
'permission_callback' => [ $this, 'permission_manage_options' ],
'args' => [
'options' => [
'type' => 'object',
'required' => true,
'properties' => [
'task' => [
'type' => 'string',
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
],
],
],
],
],
],
);
}
/**
* Completes a task.
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response The success or failure response.
*
* @throws Task_Not_Found_Exception When the given task name is not implemented yet.
*/
public function complete_task( WP_REST_Request $request ): WP_REST_Response {
try {
$this->action_tracker->track_version_for_performed_action( 'task_first_actioned_on' );
$task_name = $request->get_param( 'options' )['task'];
$task = $this->tasks_collector->get_completeable_task( $task_name );
if ( ! $task ) {
throw new Task_Not_Found_Exception();
}
$task->complete_task();
} catch ( Exception $exception ) {
return new WP_REST_Response(
[
'success' => false,
'error' => $exception->getMessage(),
],
$exception->getCode(),
);
}
\delete_transient( Cached_Tasks_Collector::TASKS_TRANSIENT );
return new WP_REST_Response(
[
'success' => true,
],
200,
);
}
/**
* Permission callback.
*
* @return bool True when user has the 'wpseo_manage_options' capability.
*/
public function permission_manage_options() {
return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
}
}