$license) { if (empty($license['products'])) continue; if (in_array($slug, $license['products'])) { return $key; } } return null; } /** * Retrieve the status of a given plugin slug. * * @param string $slug The slug of the plugin. * * @return string|null The status of the plugin, e.g. 'valid', 'expired', or null if no matching license was found. */ public static function getPluginLicenseStatus($slug) { $licenseKey = self::getPluginLicense($slug); $status = self::getLicenseInfo($licenseKey); return $status['status'] ?? null; } /** * Checks if the given plugin slug has a valid license. * * @param string $slug The slug of the plugin. * * @return bool True if the plugin has a valid license, false otherwise. */ public static function isPluginLicenseValid($slug) { $status = self::getPluginLicenseStatus($slug); return $status === 'valid'; } /** * Checks if the given plugin is expired. * * @param string $slug The slug of the plugin. */ public static function isPluginLicenseExpired($slug) { $status = self::getPluginLicenseStatus($slug); return $status === 'license_expired'; } /** * Store license details in the WordPress database. * * @param string $licenseKey * @param object $license */ public static function storeLicense($licenseKey, $licenseData) { $data = [ 'status' => $licenseData->status, 'type' => $licenseData->license_details->type ?? null, 'sku' => $licenseData->license_details->sku ?? null, 'max_domains' => $licenseData->license_details->max_domains ?? null, 'user' => $licenseData->license_details->user ?? null, 'products' => isset($licenseData->products) ? wp_list_pluck($licenseData->products, 'slug') : null, ]; Option::saveOptionGroup($licenseKey, $data, self::LICENSE_OPTION_KEY); } /** * Update license in the database * * @param string $licenseKey * @param object $license */ public static function updateLicense($licenseKey, $licenseData) { Option::saveOptionGroup($licenseKey, $licenseData, self::LICENSE_OPTION_KEY); } /** * Removes a license from WordPress database. * * @param string $licenseKey * * @return void */ public static function removeLicense($licenseKey) { Option::deleteOptionGroup($licenseKey, self::LICENSE_OPTION_KEY); } /** * Checks if user has any valid license or not. * * @return bool */ public static function isValidLicenseAvailable() { return !empty(self::getLicenses()); } /** * Checks all stored licenses to see if any of them is premium. * * @return string|null License key. `null` if no premium licenses were found. */ public static function isPremiumLicenseAvailable() { foreach (self::getLicenses() as $key => $data) { if (!empty($data['sku']) && $data['sku'] === 'premium') { return $key; } } return null; } /** * Checks all stored licenses to see if they are valid or not. * * @return void */ public static function checkLicensesStatus() { $apiCommunicator = new ApiCommunicator(); $licenses = LicenseHelper::getLicenses('all'); foreach ($licenses as $key => $data) { try { $licenseData = $apiCommunicator->validateLicense($key); LicenseHelper::storeLicense($key, $licenseData); } catch (LicenseException $e) { // If status is empty, do nothing (probably server error, or connection issue) if (!$e->getStatus()) return; // If license is expired, update the status if ($e->getStatus() === 'license_expired') { $data['status'] = $e->getStatus(); LicenseHelper::updateLicense($key, $data); return; } // If license is invalid, remove the license LicenseHelper::removeLicense($key); } } } }