twig = $twig; $this->licenseManager = $licenseManager; $this->versionManager = $versionManager; $this->downloadController = $downloadController; $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 and scripts add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']); } /** * 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 and downloads $enrichedLicenses = []; foreach ($licenses as $license) { $product = wc_get_product($license->getProductId()); $order = wc_get_order($license->getOrderId()); // Get available downloads for this license $downloads = []; if ($license->getStatus() === 'active') { $versions = $this->versionManager->getVersionsByProduct($license->getProductId()); foreach ($versions as $version) { if ($version->isActive() && ($version->getAttachmentId() || $version->getDownloadUrl())) { $downloads[] = [ 'version' => $version->getVersion(), 'version_id' => $version->getId(), 'filename' => $version->getDownloadFilename(), 'download_url' => $this->downloadController->generateDownloadUrl( $license->getId(), $version->getId() ), 'release_notes' => $version->getReleaseNotes(), 'released_at' => $version->getReleasedAt()->format(get_option('date_format')), ]; } } } $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() : '', 'downloads' => $downloads, ]; } 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; } ?> [ 'copied' => __('Copied!', 'wc-licensed-product'), 'copyFailed' => __('Copy failed', 'wc-licensed-product'), ], ]); } }