store = $store ?? new ABDS_SEO_License_Store(); $this->client = $client ?? new ABDS_SEO_License_Client( $this->store ); } /** * Registers the routes. * * @param ABDS_SEO_Loader $loader Hook loader. */ public function register_hooks( ABDS_SEO_Loader $loader ): void { $loader->add_action( 'rest_api_init', $this, 'register_routes' ); } /** * Registers GET/POST/DELETE /license. */ public function register_routes(): void { register_rest_route( 'abds-seo/v1', '/license', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_license' ), 'permission_callback' => array( $this, 'can_manage' ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'deactivate' ), 'permission_callback' => array( $this, 'can_manage' ), ), ) ); register_rest_route( 'abds-seo/v1', '/license/activate', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'activate' ), 'permission_callback' => array( $this, 'can_manage' ), 'args' => array( 'license_key' => array( 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), ), ) ); } /** * Capability check. * * @return bool */ public function can_manage(): bool { return current_user_can( ABDS_SEO_Activator::CAPABILITY ); } /** * GET /license — current status for the settings panel. * * @return WP_REST_Response */ public function get_license(): WP_REST_Response { return new WP_REST_Response( $this->snapshot(), 200 ); } /** * POST /license/activate — activates and, on success, stores the key. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function activate( WP_REST_Request $request ) { $key = trim( (string) $request->get_param( 'license_key' ) ); $verdict = $this->client->activate( $key ); if ( ! empty( $verdict['transport_error'] ) ) { return new WP_Error( 'license_unreachable', __( 'Could not contact the license server. Please try again later.', 'abds-seo' ), array( 'status' => 503 ) ); } if ( ABDS_SEO_License_Status_Names::ACTIVE !== $verdict['status'] ) { return new WP_Error( 'license_not_active', $this->message_for( (string) $verdict['status'] ), array( 'status' => 400, 'license_status' => (string) $verdict['status'], ) ); } $this->store->store_key( $key ); $this->store->cache_verdict( $verdict ); return new WP_REST_Response( $this->snapshot(), 200 ); } /** * DELETE /license — releases the slot on the server and forgets the key. * * @return WP_REST_Response */ public function deactivate(): WP_REST_Response { // Best-effort server release; local removal happens regardless so the // user is never stuck with a key they can't clear. $this->client->deactivate(); $this->store->delete_key(); return new WP_REST_Response( $this->snapshot(), 200 ); } /** * The state the settings panel renders from. * * @return array */ protected function snapshot(): array { return array( 'has_key' => $this->store->has_key(), 'masked' => $this->store->get_masked(), 'status' => $this->store->get_status(), 'server' => $this->client->server_url(), ); } /** * A human message for a non-active definitive status. * * @param string $status Server status. * @return string */ protected function message_for( string $status ): string { switch ( $status ) { case ABDS_SEO_License_Status_Names::NOT_FOUND: return __( 'No such key exists. Check that it is correct.', 'abds-seo' ); case ABDS_SEO_License_Status_Names::EXPIRED: return __( 'The license has expired. Renew it to activate.', 'abds-seo' ); case ABDS_SEO_License_Status_Names::SITE_LIMIT: return __( 'The site limit for this key has been reached. Free a slot on another site.', 'abds-seo' ); case ABDS_SEO_License_Status_Names::REVOKED: return __( 'The license has been revoked.', 'abds-seo' ); default: return __( 'Could not activate the license.', 'abds-seo' ); } } }