* @since x.x.x */ namespace AdvancedAds\License; use AdvancedAds\Updater\Addon_Updater; use AdvancedAds\Utilities\Addons; use WP_Error; defined( 'ABSPATH' ) || exit; /** * Local license list, connect and disconnect. */ class License { /** * Local license store. * * @var Store */ private $store; /** * Get the singleton instance. * * @return self */ public static function get_instance(): self { static $instance; if ( null === $instance ) { $instance = new self(); } return $instance; } /** * Set up the local license store. */ private function __construct() { $this->store = new Store(); } /** * Get all licenses. * * @return array> */ public function get_licenses(): array { return $this->store->get_all(); } /** * Store license rows from the shop. * * @param array> $rows License rows from the shop. * @return void */ public function connect( array $rows ): void { $this->store->upsert_many( $rows ); $this->invalidate_update_caches(); } /** * Refresh the stored licenses from the shop, one exchange per shop account. * * Each stored key is traded for every license on its account, so new purchases show up * and accounts connected side by side are all kept. A key the shop rejects keeps its * stored row, so a revoked key or a wrong shop URL never deletes licenses. A network * failure stops the sync before anything is written. * * @return array>|WP_Error The synced list, or the error when no key synced. */ public function sync() { $site = $this->site_hostname(); $rows = []; $has_fresh = false; $rejection = null; foreach ( $this->store->get_all() as $row ) { $key = is_array( $row ) ? (string) ( $row['licenseKey'] ?? '' ) : ''; if ( '' === $key || isset( $rows[ $key ] ) ) { continue; } $account = Shop_Client::exchange( [ 'site' => $site, 'license' => $key, 'license_id' => (int) ( $row['licenseId'] ?? 0 ), ] ); if ( is_wp_error( $account ) ) { if ( 'network' === $account->get_error_code() ) { return $account; } $rejection = $account; $rows[ $key ] = $row; continue; } foreach ( $account as $fresh ) { $fresh_key = is_array( $fresh ) ? (string) ( $fresh['licenseKey'] ?? '' ) : ''; if ( '' !== $fresh_key ) { $rows[ $fresh_key ] = $fresh; $has_fresh = true; } } } if ( ! $has_fresh ) { return $rejection ?? $this->store->get_all(); } $this->store->replace_all( array_values( $rows ) ); $this->invalidate_update_caches(); return $this->store->get_all(); } /** * Activate a stored license for this site on the shop, then refresh the stored licenses. * * @param string $license_key License key. * @return true|WP_Error EDD error code (e.g. `no_activations_left`, `expired`) or `network`. */ public function activate( string $license_key ) { return $this->change_activation( 'activate_license', $license_key ); } /** * Free this site's slot on a stored license, then refresh the stored licenses. * * @param string $license_key License key. * @return true|WP_Error `failed` when the shop refuses (EDD gives no reason) or `network`. */ public function deactivate( string $license_key ) { return $this->change_activation( 'deactivate_license', $license_key ); } /** * Whether any stored license is activated on this site and not expired. * * @param bool $all_access Only count All Access licenses. * @return bool */ public function has_valid_license( bool $all_access = false ): bool { foreach ( $this->store->get_all() as $row ) { if ( is_array( $row ) && ( ! $all_access || $this->is_all_access( $row ) ) && 'expired' !== strtolower( (string) ( $row['status'] ?? '' ) ) && $this->is_license_active( $row ) ) { return true; } } return false; } /** * The license that covers an add-on on this site: activated here, and not expired when another one is. * * @param string $addon_id Short add-on id. * @return array|null Stored row, or null when no license activated here covers the add-on. */ public function license_for_addon( string $addon_id ): ?array { $expired = null; foreach ( $this->store->get_all() as $row ) { if ( ! is_array( $row ) || ! $this->covers( $row, $addon_id ) || ! $this->is_license_active( $row ) ) { continue; } if ( 'expired' !== strtolower( (string) ( $row['status'] ?? '' ) ) ) { return $row; } $expired = $expired ?? $row; } return $expired; } /** * Whether a license includes an add-on. * * @param array $row Stored license row. * @param string $addon_id Short add-on id. * @return bool */ private function covers( array $row, string $addon_id ): bool { if ( $this->is_all_access( $row ) ) { return Addons::is_all_access_addon( $addon_id ); } // Single-product licenses carry the shop product name: "Tracking", but "Advanced Ads Pro" for Pro. Bundles list their add-ons. $name = strtolower( trim( (string) ( $row['name'] ?? '' ) ) ); $product = strtolower( Addons::product_name( $addon_id ) ); $bundled = array_map( static fn( $addon ) => sanitize_key( (string) ( $addon['name'] ?? '' ) ), (array) ( $row['addons'] ?? [] ) ); return in_array( $name, [ $product, 'advanced ads ' . $product ], true ) || in_array( $addon_id, $bundled, true ); } /** * Whether the license is an All Access bundle, going by its shop name. * * @param array $row Stored license row. * @return bool */ public function is_all_access( array $row ): bool { return 0 === strpos( strtolower( trim( (string) ( $row['name'] ?? '' ) ) ), 'all access' ); } /** * Whether the license is activated on this site, i.e. this site is in its `sitesActivated`. * * @param array $row Stored license row. * @return bool */ public function is_license_active( array $row ): bool { $site = strtolower( $this->site_hostname() ); if ( '' === $site ) { return false; } $domains = array_map( static fn( $domain ) => strtolower( untrailingslashit( (string) $domain ) ), array_column( (array) ( $row['sitesActivated'] ?? [] ), 'domain' ) ); return in_array( $site, $domains, true ); } /** * Package link for an add-on, from a license that is active on this site. * * Uses EDD `get_version`, the same answer the add-on updater reads. * * @param string $license_key License key. * @param string $addon_id Short add-on id. * @return string|WP_Error `missing`, `license_inactive`, `no_package` or `network`. */ public function addon_package( string $license_key, string $addon_id ) { $row = null; foreach ( $this->store->get_all() as $stored ) { if ( is_array( $stored ) && (string) ( $stored['licenseKey'] ?? '' ) === $license_key ) { $row = $stored; break; } } if ( null === $row ) { return new WP_Error( 'missing' ); } if ( ! $this->is_license_active( $row ) ) { return new WP_Error( 'license_inactive' ); } $answer = Shop_Client::edd( 'get_version', [ 'license' => $license_key, 'item_id' => Addons::item_id( $addon_id ), 'slug' => dirname( (string) Addons::plugin_file( $addon_id ) ), 'url' => home_url(), ] ); if ( is_wp_error( $answer ) ) { return $answer; } // The shop still sends a link for a key it doesn't accept for this add-on, but marks it invalid and refuses the download. $package = (string) ( $answer['package'] ?? $answer['download_link'] ?? '' ); return ! Shop_Client::is_shop_url( $package ) || 'invalid' === ( $answer['valid_until'] ?? '' ) ? new WP_Error( 'no_package' ) : $package; } /** * Free this site's slot on every license active here, then forget every license on this site. * * The shop answer is ignored: a refusal or network error never blocks the local clear. * * @return void */ public function disconnect(): void { foreach ( $this->store->get_all() as $row ) { if ( is_array( $row ) && $this->is_license_active( $row ) ) { Shop_Client::edd( 'deactivate_license', [ 'license' => (string) ( $row['licenseKey'] ?? '' ), 'item_name' => (string) ( $row['name'] ?? '' ), 'url' => $this->site_hostname(), ] ); } } $this->store->clear(); $this->invalidate_update_caches(); } /** * Current site hostname for shop activate/deactivate/validate/exchange. * * Host plus path from `site_url()` (no scheme), matching 2.0.29 so * subdirectory installs and shop token exchange stay aligned. * * @return string */ public function site_hostname(): string { $parts = wp_parse_url( site_url() ); if ( ! is_array( $parts ) ) { return ''; } $host = (string) ( $parts['host'] ?? '' ); if ( '' === $host ) { return ''; } $path = (string) ( $parts['path'] ?? '' ); $path = '' !== $path ? untrailingslashit( $path ) : ''; return $host . $path; } /** * Send an EDD activate or deactivate for this site, then pull the fresh rows. * * Stored rows carry no EDD item id, so the product is matched by `item_name`, which * the shop fills with the download title and EDD checks against the license's download. * * @param string $action `activate_license` or `deactivate_license`. * @param string $license_key License key. * @return true|WP_Error */ private function change_activation( string $action, string $license_key ) { $row = null; foreach ( $this->store->get_all() as $stored ) { if ( is_array( $stored ) && (string) ( $stored['licenseKey'] ?? '' ) === $license_key ) { $row = $stored; break; } } if ( null === $row ) { return new WP_Error( 'missing' ); } $result = Shop_Client::edd( $action, [ 'license' => $license_key, 'item_name' => (string) ( $row['name'] ?? '' ), 'url' => $this->site_hostname(), ] ); if ( is_wp_error( $result ) ) { return $result; } if ( empty( $result['success'] ) ) { return new WP_Error( (string) ( $result['error'] ?? 'failed' ) ); } // The shop has already changed; a failed refresh only leaves the card stale until the next sync. $this->sync(); return true; } /** * Forget cached add-on package URLs after a successful license state change. * * @return void */ private function invalidate_update_caches(): void { ( new Addon_Updater() )->invalidate_cached_updates(); } }