| 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_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
use Yoast\WP\SEO\Conditionals\Task_List_Enabled_Conditional; |
| 10 |
use Yoast\WP\SEO\Helpers\Capability_Helper; |
| 11 |
use Yoast\WP\SEO\Main; |
| 12 |
use Yoast\WP\SEO\Routes\Route_Interface; |
| 13 |
use Yoast\WP\SEO\Task_List\Domain\Exceptions\Task_Not_Found_Exception; |
| 14 |
use Yoast\WP\SEO\Task_List\Infrastructure\Tasks_Collectors\Cached_Tasks_Collector; |
| 15 |
use Yoast\WP\SEO\Task_List\Infrastructure\Tasks_Collectors\Tasks_Collector; |
| 16 |
use Yoast\WP\SEO\Tracking\Application\Action_Tracker; |
| 17 |
|
| 18 |
/** |
| 19 |
* Tasks route. |
| 20 |
*/ |
| 21 |
final class Complete_Task_Route implements Route_Interface { |
| 22 |
|
| 23 |
/** |
| 24 |
* The namespace of the route. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE; |
| 29 |
|
| 30 |
/** |
| 31 |
* The prefix of the route. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public const ROUTE_NAME = '/complete_task'; |
| 36 |
|
| 37 |
/** |
| 38 |
* The task collector. |
| 39 |
* |
| 40 |
* @var Tasks_Collector |
| 41 |
*/ |
| 42 |
private $tasks_collector; |
| 43 |
|
| 44 |
/** |
| 45 |
* Holds the capability helper instance. |
| 46 |
* |
| 47 |
* @var Capability_Helper |
| 48 |
*/ |
| 49 |
private $capability_helper; |
| 50 |
|
| 51 |
/** |
| 52 |
* Holds the action tracker instance. |
| 53 |
* |
| 54 |
* @var Action_Tracker |
| 55 |
*/ |
| 56 |
private $action_tracker; |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the needed conditionals. |
| 60 |
* |
| 61 |
* @return array<string> The conditionals that must be met to load this. |
| 62 |
*/ |
| 63 |
public static function get_conditionals(): array { |
| 64 |
return [ |
| 65 |
Task_List_Enabled_Conditional::class, |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* The constructor. |
| 71 |
* |
| 72 |
* @param Tasks_Collector $tasks_collector The collector for all tasks. |
| 73 |
* @param Capability_Helper $capability_helper The capability helper. |
| 74 |
* @param Action_Tracker $action_tracker The action tracker. |
| 75 |
*/ |
| 76 |
public function __construct( |
| 77 |
Tasks_Collector $tasks_collector, |
| 78 |
Capability_Helper $capability_helper, |
| 79 |
Action_Tracker $action_tracker |
| 80 |
) { |
| 81 |
$this->tasks_collector = $tasks_collector; |
| 82 |
$this->capability_helper = $capability_helper; |
| 83 |
$this->action_tracker = $action_tracker; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Registers routes for scores. |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function register_routes() { |
| 92 |
\register_rest_route( |
| 93 |
self::ROUTE_NAMESPACE, |
| 94 |
self::ROUTE_NAME, |
| 95 |
[ |
| 96 |
[ |
| 97 |
'methods' => 'POST', |
| 98 |
'callback' => [ $this, 'complete_task' ], |
| 99 |
'permission_callback' => [ $this, 'permission_manage_options' ], |
| 100 |
'args' => [ |
| 101 |
'options' => [ |
| 102 |
'type' => 'object', |
| 103 |
'required' => true, |
| 104 |
'properties' => [ |
| 105 |
'task' => [ |
| 106 |
'type' => 'string', |
| 107 |
'required' => true, |
| 108 |
'sanitize_callback' => 'sanitize_text_field', |
| 109 |
], |
| 110 |
], |
| 111 |
], |
| 112 |
], |
| 113 |
], |
| 114 |
], |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Completes a task. |
| 120 |
* |
| 121 |
* @param WP_REST_Request $request The request object. |
| 122 |
* |
| 123 |
* @return WP_REST_Response The success or failure response. |
| 124 |
* |
| 125 |
* @throws Task_Not_Found_Exception When the given task name is not implemented yet. |
| 126 |
*/ |
| 127 |
public function complete_task( WP_REST_Request $request ): WP_REST_Response { |
| 128 |
try { |
| 129 |
$this->action_tracker->track_version_for_performed_action( 'task_first_actioned_on' ); |
| 130 |
|
| 131 |
$task_name = $request->get_param( 'options' )['task']; |
| 132 |
$task = $this->tasks_collector->get_completeable_task( $task_name ); |
| 133 |
|
| 134 |
if ( ! $task ) { |
| 135 |
throw new Task_Not_Found_Exception(); |
| 136 |
} |
| 137 |
|
| 138 |
$task->complete_task(); |
| 139 |
} catch ( Exception $exception ) { |
| 140 |
return new WP_REST_Response( |
| 141 |
[ |
| 142 |
'success' => false, |
| 143 |
'error' => $exception->getMessage(), |
| 144 |
], |
| 145 |
$exception->getCode(), |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
\delete_transient( Cached_Tasks_Collector::TASKS_TRANSIENT ); |
| 150 |
|
| 151 |
return new WP_REST_Response( |
| 152 |
[ |
| 153 |
'success' => true, |
| 154 |
], |
| 155 |
200, |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Permission callback. |
| 161 |
* |
| 162 |
* @return bool True when user has the 'wpseo_manage_options' capability. |
| 163 |
*/ |
| 164 |
public function permission_manage_options() { |
| 165 |
return $this->capability_helper->current_user_can( 'wpseo_manage_options' ); |
| 166 |
} |
| 167 |
} |
| 168 |
|