store = $store ?? new ABDS_SEO_License_Store(); $this->transport = $transport ?? 'wp_remote_post'; } /** * The license server base URL. * * @return string */ public function server_url(): string { $url = defined( 'ABDS_SEO_LICENSE_SERVER' ) ? (string) ABDS_SEO_LICENSE_SERVER : self::DEFAULT_SERVER; /** * Filters the license server base URL. * * @since 0.6.0 * * @param string $url Base URL, no trailing slash. */ return untrailingslashit( (string) apply_filters( 'abds_seo_license_server_url', $url ) ); } /** * Activates a license for this site. * * @param string $license_key Raw key as entered. * @return array Normalized verdict (see ABDS_SEO_License_Response::parse). */ public function activate( string $license_key ): array { return $this->post( 'activate', array( 'license_key' => $license_key, 'product_sku' => self::PRODUCT_SKU, 'site_url' => home_url(), 'site_instance_hash' => $this->store->instance_hash(), 'plugin_version' => defined( 'ABDS_SEO_VERSION' ) ? ABDS_SEO_VERSION : '', 'wp_version' => get_bloginfo( 'version' ), 'php_version' => PHP_VERSION, ) ); } /** * Validates the stored license. Returns a client-only "none" verdict when * no key is stored (nothing to check — the site is simply unlicensed). * * @return array */ public function validate(): array { $key = $this->store->get_key(); if ( '' === $key ) { return $this->none(); } return $this->post( 'validate', array( 'license_key' => $key, 'product_sku' => self::PRODUCT_SKU, 'site_instance_hash' => $this->store->instance_hash(), ) ); } /** * Deactivates this site's slot on the server. * * @return array */ public function deactivate(): array { $key = $this->store->get_key(); if ( '' === $key ) { return $this->none(); } return $this->deactivate_hash( $key, $this->store->instance_hash() ); } /** * Deactivates a specific, explicitly given instance hash — unlike * deactivate(), which always releases the CURRENT hash * ($this->store->instance_hash()). ABDS_SEO_License_Store's own * instance-hash migration needs this: it releases a site's now-orphaned * OLD hash while adopting a different, already-shared one as current, * so it cannot use instance_hash() to name what to release (that would * name the hash it's adopting, not the one it's abandoning). * * @param string $license_key License key. * @param string $hash The exact hash to release. * @return array */ public function deactivate_hash( string $license_key, string $hash ): array { return $this->post( 'deactivate', array( 'license_key' => $license_key, 'site_instance_hash' => $hash, ) ); } /** * POSTs to a license endpoint and normalizes the result. * * @param string $endpoint Endpoint name. * @param array $body Minimized request body. * @return array */ protected function post( string $endpoint, array $body ): array { $url = $this->server_url() . '/?rest_route=' . rawurlencode( '/abds-ls/v1/' . $endpoint ); $response = call_user_func( $this->transport, $url, array( 'method' => 'POST', 'timeout' => 15, 'headers' => array( 'Content-Type' => 'application/json' ), 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $response ) ) { return ABDS_SEO_License_Response::parse( true, 0, '' ); } return ABDS_SEO_License_Response::parse( false, (int) wp_remote_retrieve_response_code( $response ), (string) wp_remote_retrieve_body( $response ) ); } /** * Fetches the latest version metadata for this product. Deliberately sends * NO license key — update-info is public (version only); the key is only * ever spent minting a download token. Empty array on any failure. * * @return array */ public function fetch_update_info(): array { $url = add_query_arg( array( 'rest_route' => '/abds-ls/v1/update-info', 'product_sku' => self::PRODUCT_SKU, ), $this->server_url() . '/' ); $response = wp_remote_get( $url, array( 'timeout' => 15 ) ); if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { return array(); } $data = json_decode( (string) wp_remote_retrieve_body( $response ), true ); return is_array( $data ) ? $data : array(); } /** * Mints a one-shot download URL (token embedded) for a version. The license * key goes in the POST body, never a URL. Empty string when the server * won't authorize (no active license, package missing, unreachable). * * @param string $version Version to download. * @return string Tokenized download URL, or ''. */ public function request_download_url( string $version ): string { $key = $this->store->get_key(); if ( '' === $key ) { return ''; } $url = $this->server_url() . '/?rest_route=' . rawurlencode( '/abds-ls/v1/download-token' ); $response = call_user_func( $this->transport, $url, array( 'method' => 'POST', 'timeout' => 15, 'headers' => array( 'Content-Type' => 'application/json' ), 'body' => wp_json_encode( array( 'license_key' => $key, 'product_sku' => self::PRODUCT_SKU, 'version' => $version, ) ), ) ); if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { return ''; } $data = json_decode( (string) wp_remote_retrieve_body( $response ), true ); $token = is_array( $data ) ? (string) ( $data['token'] ?? '' ) : ''; if ( '' === $token ) { return ''; } // Build the download URL from the server URL the CLIENT knows (not the // server's own home_url, which may be an internal address the client // can't reach). Only the short-lived token rides in the query string — // never the license key. return $this->server_url() . '/?rest_route=' . rawurlencode( '/abds-ls/v1/download' ) . '&sku=' . rawurlencode( self::PRODUCT_SKU ) . '&version=' . rawurlencode( $version ) . '&token=' . rawurlencode( $token ); } /** * The "no key" verdict — a definitive, non-error unlicensed state. * * @return array */ protected function none(): array { return array( 'transport_error' => false, 'status' => self::STATUS_NONE, 'tier' => null, 'max_sites' => null, 'active_sites' => null, 'expires_at' => null, 'licensed' => false, ); } }