twig = $twig; $this->licenseManager = $licenseManager; $this->registerHooks(); } /** * Register WordPress hooks */ private function registerHooks(): void { // Add licenses endpoint add_action('init', [$this, 'addLicensesEndpoint']); // Add licenses menu item add_filter('woocommerce_account_menu_items', [$this, 'addLicensesMenuItem']); // Add licenses endpoint content add_action('woocommerce_account_licenses_endpoint', [$this, 'displayLicensesContent']); // Enqueue frontend styles add_action('wp_enqueue_scripts', [$this, 'enqueueStyles']); } /** * Add licenses endpoint for My Account */ public function addLicensesEndpoint(): void { add_rewrite_endpoint('licenses', EP_ROOT | EP_PAGES); } /** * Add licenses menu item to My Account navigation */ public function addLicensesMenuItem(array $items): array { // Insert licenses after orders $newItems = []; foreach ($items as $key => $value) { $newItems[$key] = $value; if ($key === 'orders') { $newItems['licenses'] = __('Licenses', 'wc-licensed-product'); } } return $newItems; } /** * Display licenses content in My Account */ public function displayLicensesContent(): void { $customerId = get_current_user_id(); if (!$customerId) { echo '

' . esc_html__('Please log in to view your licenses.', 'wc-licensed-product') . '

'; return; } $licenses = $this->licenseManager->getLicensesByCustomer($customerId); // Enrich licenses with product data $enrichedLicenses = []; foreach ($licenses as $license) { $product = wc_get_product($license->getProductId()); $order = wc_get_order($license->getOrderId()); $enrichedLicenses[] = [ 'license' => $license, 'product_name' => $product ? $product->get_name() : __('Unknown Product', 'wc-licensed-product'), 'product_url' => $product ? $product->get_permalink() : '', 'order_number' => $order ? $order->get_order_number() : '', 'order_url' => $order ? $order->get_view_order_url() : '', ]; } try { echo $this->twig->render('frontend/licenses.html.twig', [ 'licenses' => $enrichedLicenses, 'has_licenses' => !empty($enrichedLicenses), ]); } catch (\Exception $e) { // Fallback to PHP template if Twig fails $this->displayLicensesFallback($enrichedLicenses); } } /** * Fallback display method if Twig is unavailable */ private function displayLicensesFallback(array $enrichedLicenses): void { if (empty($enrichedLicenses)) { echo '

' . esc_html__('You have no licenses yet.', 'wc-licensed-product') . '

'; return; } ?>
getLicenseKey()); ?> getDomain()); ?> getStatus())); ?> getExpiresAt(); echo $expiresAt ? esc_html($expiresAt->format(get_option('date_format'))) : esc_html__('Never', 'wc-licensed-product'); ?>