* @since 2.0.19 */ namespace AdvancedAds\License; defined( 'ABSPATH' ) || exit; /** * Reads and writes the local license list (WP option). * * Use upsert / upsert_many so the same licenseKey is updated in place, not appended twice. */ class Store { /** * Option that holds the license row list (same key as 2.0.29 rich licenses). * * @var string */ public const OPTION_NAME = 'advanced-ads-app-licenses'; /** * Option: add-on id => license key that owns this site activation. * * @var string */ private const OPTION_NAME_ADDONS = 'advanced-ads-addon-licenses'; /** * Return every stored license row. * * @return array> */ public function get_all(): array { $stored = get_option( self::OPTION_NAME, [] ); return is_array( $stored ) ? array_values( $stored ) : []; } /** * Add license rows, replacing any stored row with the same licenseKey. * * @param array> $rows License rows from the shop. * @return void */ public function upsert_many( array $rows ): void { $by_key = []; foreach ( array_merge( $this->get_all(), $rows ) as $row ) { $key = is_array( $row ) ? (string) ( $row['licenseKey'] ?? '' ) : ''; if ( '' !== $key ) { $by_key[ $key ] = $row; } } $this->replace_all( array_values( $by_key ) ); } /** * Overwrite the whole license list. * * @param array> $rows License rows. * @return void */ public function replace_all( array $rows ): void { update_option( self::OPTION_NAME, $rows, false ); } /** * Delete the local license list and add-on map (disconnect / reset). * * @return void */ public function clear(): void { delete_option( self::OPTION_NAME ); delete_option( self::OPTION_NAME_ADDONS ); } }