licenseManager = $licenseManager; $this->registerHooks(); } /** * Register WordPress hooks */ private function registerHooks(): void { // Register custom WooCommerce email classes add_filter('woocommerce_email_classes', [$this, 'registerEmailClasses']); // Add license info to order completed email add_action('woocommerce_email_after_order_table', [$this, 'addLicenseInfoToEmail'], 20, 4); // Add license info to order details in emails add_action('woocommerce_order_item_meta_end', [$this, 'addLicenseToOrderItem'], 10, 4); // Schedule cron job for expiration warnings add_action('init', [$this, 'scheduleExpirationCheck']); // Cron action for checking expiring licenses add_action('wclp_check_expiring_licenses', [$this, 'sendExpirationWarnings']); } /** * Register custom email classes with WooCommerce * * @param array $email_classes Existing email classes * @return array Modified email classes */ public function registerEmailClasses(array $email_classes): array { $email_classes['WCLP_License_Expiration'] = new LicenseExpirationEmail(); $email_classes['WCLP_License_Expired'] = new LicenseExpiredEmail(); return $email_classes; } /** * Schedule the expiration check cron job */ public function scheduleExpirationCheck(): void { if (!wp_next_scheduled('wclp_check_expiring_licenses')) { wp_schedule_event(time(), 'daily', 'wclp_check_expiring_licenses'); } } /** * Send expiration warning emails and auto-expire licenses */ public function sendExpirationWarnings(): void { // First, auto-expire licenses that have passed their expiration date $this->autoExpireAndNotify(); // Check if expiration emails are enabled in settings if (!SettingsController::isExpirationEmailsEnabled()) { return; } // Get the WooCommerce email instance $mailer = WC()->mailer(); $emails = $mailer->get_emails(); if (!isset($emails['WCLP_License_Expiration'])) { return; } /** @var LicenseExpirationEmail $expirationEmail */ $expirationEmail = $emails['WCLP_License_Expiration']; // Check if the email is enabled if (!$expirationEmail->is_enabled()) { return; } // Get configurable warning days $firstWarningDays = SettingsController::getFirstWarningDays(); $secondWarningDays = SettingsController::getSecondWarningDays(); // Check for licenses expiring at first warning threshold $this->processExpirationWarnings($expirationEmail, $firstWarningDays, 'expiring_first_warning'); // Check for licenses expiring at second warning threshold (if enabled) if ($secondWarningDays > 0 && $secondWarningDays < $firstWarningDays) { $this->processExpirationWarnings($expirationEmail, $secondWarningDays, 'expiring_second_warning'); } } /** * Auto-expire licenses and send expired notifications */ private function autoExpireAndNotify(): void { // Get licenses that should be auto-expired $expiredActiveLicenses = $this->licenseManager->getExpiredActiveLicenses(); if (empty($expiredActiveLicenses)) { return; } // Get the WooCommerce email instance for expired notifications $mailer = WC()->mailer(); $emails = $mailer->get_emails(); /** @var LicenseExpiredEmail|null $expiredEmail */ $expiredEmail = $emails['WCLP_License_Expired'] ?? null; foreach ($expiredActiveLicenses as $license) { // Auto-expire the license $wasExpired = $this->licenseManager->autoExpireLicense($license->getId()); if ($wasExpired && $expiredEmail && $expiredEmail->is_enabled()) { // Check if we haven't already sent an expired notification if (!$this->licenseManager->wasExpirationNotified($license->getId(), 'license_expired')) { // Send expired notification email if ($expiredEmail->trigger($license)) { $this->licenseManager->markExpirationNotified($license->getId(), 'license_expired'); } } } } } /** * Process and send expiration warnings for a specific time frame * * @param LicenseExpirationEmail $email Email instance * @param int $days Days until expiration * @param string $notificationType Notification type identifier */ private function processExpirationWarnings(LicenseExpirationEmail $email, int $days, string $notificationType): void { $licenses = $this->licenseManager->getLicensesExpiringSoon($days); foreach ($licenses as $license) { // Skip if already notified if ($this->licenseManager->wasExpirationNotified($license->getId(), $notificationType)) { continue; } // Send the warning email using WooCommerce email system if ($email->trigger($license, $days)) { // Mark as notified $this->licenseManager->markExpirationNotified($license->getId(), $notificationType); } } } /** * Add license information to order completed email */ public function addLicenseInfoToEmail(\WC_Order $order, bool $sentToAdmin, bool $plainText, $email): void { // Only add to completed order email sent to customer if ($sentToAdmin || !$email || $email->id !== 'customer_completed_order') { return; } $licenses = $this->getLicensesForOrder($order); if (empty($licenses)) { return; } if ($plainText) { $this->renderPlainTextLicenseInfo($licenses, $order); } else { $this->renderHtmlLicenseInfo($licenses, $order); } } /** * Add license key(s) to order item in email */ public function addLicenseToOrderItem(int $itemId, \WC_Order_Item $item, \WC_Order $order, bool $plainText): void { $product = $item->get_product(); if (!$product || !$product->is_type('licensed')) { return; } $licenses = $this->licenseManager->getLicensesByOrderAndProduct($order->get_id(), $product->get_id()); if (empty($licenses)) { return; } if ($plainText) { echo "\n" . esc_html__('License Keys:', 'wc-licensed-product') . "\n"; foreach ($licenses as $license) { echo ' - ' . esc_html($license->getLicenseKey()); echo ' (' . esc_html($license->getDomain()) . ')' . "\n"; } } else { ?>
getLicenseKey()); ?>
getDomain()); ?>
getLicenseKey()); ?>
|
getDomain()); ?> | getExpiresAt(); echo $expiresAt ? esc_html($expiresAt->format(get_option('date_format'))) : esc_html__('Never', 'wc-licensed-product'); ?> |