action_tracker = $action_tracker; $this->capability_helper = $capability_helper; $this->options_helper = $options_helper; } /** * Registers routes with WordPress. * * @return void */ public function register_routes() { \register_rest_route( self::ROUTE_NAMESPACE, self::ROUTE_PREFIX, [ [ 'methods' => 'POST', 'callback' => [ $this, 'track_action' ], 'permission_callback' => [ $this, 'check_capabilities' ], 'args' => [ 'action' => [ 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ], ], ], ], ); } /** * Tracks an action. * * @param WP_REST_Request $request The request object. * * @return WP_REST_Response|WP_Error The success or failure response. * * @throws Invalid_Tracked_Action_Exception When the given action is invalid. */ public function track_action( WP_REST_Request $request ): WP_REST_Response { $action_to_track = $request->get_param( 'action' ); try { if ( ! \in_array( $action_to_track, $this->options_helper->get_tracking_only_options(), true ) ) { throw new Invalid_Tracked_Action_Exception(); } $this->action_tracker->track_version_for_performed_action( $action_to_track ); } catch ( Exception $exception ) { return new WP_REST_Response( [ 'success' => false, 'error' => $exception->getMessage(), ], $exception->getCode(), ); } return new WP_REST_Response( [ 'success' => true, 'action' => $action_to_track, ], 200, ); } /** * Checks if the current user has the required capabilities. * * @return bool */ public function check_capabilities() { return $this->capability_helper->current_user_can( 'wpseo_manage_options' ); } }