licenseManager = $licenseManager; $this->registerHooks(); } /** * Register WordPress hooks */ private function registerHooks(): void { // 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']); } /** * 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 */ public function sendExpirationWarnings(): void { // Check for licenses expiring in 7 days $this->processExpirationWarnings(7, 'expiring_7_days'); // Check for licenses expiring in 1 day $this->processExpirationWarnings(1, 'expiring_1_day'); } /** * Process and send expiration warnings for a specific time frame * * @param int $days Days until expiration * @param string $notificationType Notification type identifier */ private function processExpirationWarnings(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 if ($this->sendExpirationWarningEmail($license, $days)) { // Mark as notified $this->licenseManager->markExpirationNotified($license->getId(), $notificationType); } } } /** * Send expiration warning email to customer * * @param \Jeremias\WcLicensedProduct\License\License $license License object * @param int $daysRemaining Days until expiration * @return bool Whether email was sent successfully */ private function sendExpirationWarningEmail($license, int $daysRemaining): bool { $customer = get_userdata($license->getCustomerId()); if (!$customer || !$customer->user_email) { return false; } $product = wc_get_product($license->getProductId()); $productName = $product ? $product->get_name() : __('Unknown Product', 'wc-licensed-product'); $siteName = get_bloginfo('name'); $expiresAt = $license->getExpiresAt(); $expirationDate = $expiresAt ? $expiresAt->format(get_option('date_format')) : ''; // Email subject if ($daysRemaining === 1) { $subject = sprintf( /* translators: 1: Product name, 2: Site name */ __('[%2$s] Your license for %1$s expires tomorrow', 'wc-licensed-product'), $productName, $siteName ); } else { $subject = sprintf( /* translators: 1: Product name, 2: Number of days, 3: Site name */ __('[%3$s] Your license for %1$s expires in %2$d days', 'wc-licensed-product'), $productName, $daysRemaining, $siteName ); } // Email content $message = $this->buildExpirationWarningHtml($license, $customer, $productName, $daysRemaining, $expirationDate); // Send email $headers = [ 'Content-Type: text/html; charset=UTF-8', 'From: ' . $siteName . ' <' . get_option('admin_email') . '>', ]; return wp_mail($customer->user_email, $subject, $message, $headers); } /** * Build HTML content for expiration warning email * * @param \Jeremias\WcLicensedProduct\License\License $license License object * @param \WP_User $customer Customer user object * @param string $productName Product name * @param int $daysRemaining Days until expiration * @param string $expirationDate Formatted expiration date * @return string HTML email content */ private function buildExpirationWarningHtml($license, $customer, string $productName, int $daysRemaining, string $expirationDate): string { $siteName = get_bloginfo('name'); $siteUrl = home_url(); $accountUrl = wc_get_account_endpoint_url('licenses'); ob_start(); ?>
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 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; } $license = $this->licenseManager->getLicenseByOrderAndProduct($order->get_id(), $product->get_id()); if (!$license) { return; } if ($plainText) { echo "\n" . esc_html__('License Key:', 'wc-licensed-product') . ' ' . esc_html($license->getLicenseKey()) . "\n"; } else { ?>
getLicenseKey()); ?>
getLicenseKey()); ?>
|
getExpiresAt(); echo $expiresAt ? esc_html($expiresAt->format(get_option('date_format'))) : esc_html__('Never', 'wc-licensed-product'); ?> |