You've already forked wc-licensed-product
413 lines
15 KiB
PHP
413 lines
15 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* Admin Controller
|
||
|
|
*
|
||
|
|
* @package Jeremias\WcLicensedProduct\Admin
|
||
|
|
*/
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Jeremias\WcLicensedProduct\Admin;
|
||
|
|
|
||
|
|
use Jeremias\WcLicensedProduct\License\License;
|
||
|
|
use Jeremias\WcLicensedProduct\License\LicenseManager;
|
||
|
|
use Twig\Environment;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handles admin pages for license management
|
||
|
|
*/
|
||
|
|
final class AdminController
|
||
|
|
{
|
||
|
|
private Environment $twig;
|
||
|
|
private LicenseManager $licenseManager;
|
||
|
|
|
||
|
|
public function __construct(Environment $twig, LicenseManager $licenseManager)
|
||
|
|
{
|
||
|
|
$this->twig = $twig;
|
||
|
|
$this->licenseManager = $licenseManager;
|
||
|
|
$this->registerHooks();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Register WordPress hooks
|
||
|
|
*/
|
||
|
|
private function registerHooks(): void
|
||
|
|
{
|
||
|
|
// Add admin menu
|
||
|
|
add_action('admin_menu', [$this, 'addAdminMenu']);
|
||
|
|
|
||
|
|
// Enqueue admin styles
|
||
|
|
add_action('admin_enqueue_scripts', [$this, 'enqueueStyles']);
|
||
|
|
|
||
|
|
// Handle admin actions
|
||
|
|
add_action('admin_init', [$this, 'handleAdminActions']);
|
||
|
|
|
||
|
|
// Add licenses column to orders list
|
||
|
|
add_filter('manage_edit-shop_order_columns', [$this, 'addOrdersLicenseColumn']);
|
||
|
|
add_action('manage_shop_order_posts_custom_column', [$this, 'displayOrdersLicenseColumn'], 10, 2);
|
||
|
|
|
||
|
|
// HPOS compatibility
|
||
|
|
add_filter('woocommerce_shop_order_list_table_columns', [$this, 'addOrdersLicenseColumn']);
|
||
|
|
add_action('woocommerce_shop_order_list_table_custom_column', [$this, 'displayOrdersLicenseColumnHpos'], 10, 2);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add admin menu pages
|
||
|
|
*/
|
||
|
|
public function addAdminMenu(): void
|
||
|
|
{
|
||
|
|
add_submenu_page(
|
||
|
|
'woocommerce',
|
||
|
|
__('Licenses', 'wc-licensed-product'),
|
||
|
|
__('Licenses', 'wc-licensed-product'),
|
||
|
|
'manage_woocommerce',
|
||
|
|
'wc-licenses',
|
||
|
|
[$this, 'renderLicensesPage']
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enqueue admin styles and scripts
|
||
|
|
*/
|
||
|
|
public function enqueueStyles(string $hook): void
|
||
|
|
{
|
||
|
|
if ($hook !== 'woocommerce_page_wc-licenses') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
wp_enqueue_style(
|
||
|
|
'wc-licensed-product-admin',
|
||
|
|
WC_LICENSED_PRODUCT_PLUGIN_URL . 'assets/css/admin.css',
|
||
|
|
[],
|
||
|
|
WC_LICENSED_PRODUCT_VERSION
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle admin actions (update, delete licenses)
|
||
|
|
*/
|
||
|
|
public function handleAdminActions(): void
|
||
|
|
{
|
||
|
|
if (!isset($_GET['page']) || $_GET['page'] !== 'wc-licenses') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!current_user_can('manage_woocommerce')) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Handle status update
|
||
|
|
if (isset($_POST['action']) && $_POST['action'] === 'update_license_status') {
|
||
|
|
$this->handleStatusUpdate();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Handle delete
|
||
|
|
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['license_id'])) {
|
||
|
|
$this->handleDelete();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Handle revoke
|
||
|
|
if (isset($_GET['action']) && $_GET['action'] === 'revoke' && isset($_GET['license_id'])) {
|
||
|
|
$this->handleRevoke();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle license status update
|
||
|
|
*/
|
||
|
|
private function handleStatusUpdate(): void
|
||
|
|
{
|
||
|
|
if (!wp_verify_nonce($_POST['_wpnonce'] ?? '', 'update_license_status')) {
|
||
|
|
wp_die(__('Security check failed.', 'wc-licensed-product'));
|
||
|
|
}
|
||
|
|
|
||
|
|
$licenseId = absint($_POST['license_id'] ?? 0);
|
||
|
|
$status = sanitize_text_field($_POST['status'] ?? '');
|
||
|
|
|
||
|
|
if ($licenseId && in_array($status, [License::STATUS_ACTIVE, License::STATUS_INACTIVE, License::STATUS_REVOKED], true)) {
|
||
|
|
$this->licenseManager->updateLicenseStatus($licenseId, $status);
|
||
|
|
|
||
|
|
wp_redirect(admin_url('admin.php?page=wc-licenses&updated=1'));
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle license deletion
|
||
|
|
*/
|
||
|
|
private function handleDelete(): void
|
||
|
|
{
|
||
|
|
if (!wp_verify_nonce($_GET['_wpnonce'] ?? '', 'delete_license')) {
|
||
|
|
wp_die(__('Security check failed.', 'wc-licensed-product'));
|
||
|
|
}
|
||
|
|
|
||
|
|
$licenseId = absint($_GET['license_id'] ?? 0);
|
||
|
|
if ($licenseId) {
|
||
|
|
$this->licenseManager->deleteLicense($licenseId);
|
||
|
|
|
||
|
|
wp_redirect(admin_url('admin.php?page=wc-licenses&deleted=1'));
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle license revocation
|
||
|
|
*/
|
||
|
|
private function handleRevoke(): void
|
||
|
|
{
|
||
|
|
if (!wp_verify_nonce($_GET['_wpnonce'] ?? '', 'revoke_license')) {
|
||
|
|
wp_die(__('Security check failed.', 'wc-licensed-product'));
|
||
|
|
}
|
||
|
|
|
||
|
|
$licenseId = absint($_GET['license_id'] ?? 0);
|
||
|
|
if ($licenseId) {
|
||
|
|
$this->licenseManager->updateLicenseStatus($licenseId, License::STATUS_REVOKED);
|
||
|
|
|
||
|
|
wp_redirect(admin_url('admin.php?page=wc-licenses&revoked=1'));
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Render licenses admin page
|
||
|
|
*/
|
||
|
|
public function renderLicensesPage(): void
|
||
|
|
{
|
||
|
|
$page = isset($_GET['paged']) ? absint($_GET['paged']) : 1;
|
||
|
|
$perPage = 20;
|
||
|
|
|
||
|
|
$licenses = $this->licenseManager->getAllLicenses($page, $perPage);
|
||
|
|
$totalLicenses = $this->licenseManager->getLicenseCount();
|
||
|
|
$totalPages = ceil($totalLicenses / $perPage);
|
||
|
|
|
||
|
|
// Enrich licenses with related data
|
||
|
|
$enrichedLicenses = [];
|
||
|
|
foreach ($licenses as $license) {
|
||
|
|
$product = wc_get_product($license->getProductId());
|
||
|
|
$order = wc_get_order($license->getOrderId());
|
||
|
|
$customer = get_userdata($license->getCustomerId());
|
||
|
|
|
||
|
|
$enrichedLicenses[] = [
|
||
|
|
'license' => $license,
|
||
|
|
'product_name' => $product ? $product->get_name() : __('Unknown', 'wc-licensed-product'),
|
||
|
|
'product_edit_url' => $product ? get_edit_post_link($product->get_id()) : '',
|
||
|
|
'order_number' => $order ? $order->get_order_number() : '',
|
||
|
|
'order_edit_url' => $order ? $order->get_edit_order_url() : '',
|
||
|
|
'customer_name' => $customer ? $customer->display_name : __('Guest', 'wc-licensed-product'),
|
||
|
|
'customer_email' => $customer ? $customer->user_email : '',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
echo $this->twig->render('admin/licenses.html.twig', [
|
||
|
|
'licenses' => $enrichedLicenses,
|
||
|
|
'current_page' => $page,
|
||
|
|
'total_pages' => $totalPages,
|
||
|
|
'total_licenses' => $totalLicenses,
|
||
|
|
'admin_url' => admin_url('admin.php?page=wc-licenses'),
|
||
|
|
'notices' => $this->getNotices(),
|
||
|
|
]);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
// Fallback to PHP template
|
||
|
|
$this->renderLicensesPageFallback($enrichedLicenses, $page, $totalPages, $totalLicenses);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get admin notices
|
||
|
|
*/
|
||
|
|
private function getNotices(): array
|
||
|
|
{
|
||
|
|
$notices = [];
|
||
|
|
|
||
|
|
if (isset($_GET['updated'])) {
|
||
|
|
$notices[] = ['type' => 'success', 'message' => __('License updated successfully.', 'wc-licensed-product')];
|
||
|
|
}
|
||
|
|
if (isset($_GET['deleted'])) {
|
||
|
|
$notices[] = ['type' => 'success', 'message' => __('License deleted successfully.', 'wc-licensed-product')];
|
||
|
|
}
|
||
|
|
if (isset($_GET['revoked'])) {
|
||
|
|
$notices[] = ['type' => 'success', 'message' => __('License revoked successfully.', 'wc-licensed-product')];
|
||
|
|
}
|
||
|
|
|
||
|
|
return $notices;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Fallback render for licenses page
|
||
|
|
*/
|
||
|
|
private function renderLicensesPageFallback(array $enrichedLicenses, int $page, int $totalPages, int $totalLicenses): void
|
||
|
|
{
|
||
|
|
?>
|
||
|
|
<div class="wrap">
|
||
|
|
<h1><?php esc_html_e('Licenses', 'wc-licensed-product'); ?></h1>
|
||
|
|
|
||
|
|
<?php foreach ($this->getNotices() as $notice): ?>
|
||
|
|
<div class="notice notice-<?php echo esc_attr($notice['type']); ?> is-dismissible">
|
||
|
|
<p><?php echo esc_html($notice['message']); ?></p>
|
||
|
|
</div>
|
||
|
|
<?php endforeach; ?>
|
||
|
|
|
||
|
|
<table class="wp-list-table widefat fixed striped">
|
||
|
|
<thead>
|
||
|
|
<tr>
|
||
|
|
<th><?php esc_html_e('License Key', 'wc-licensed-product'); ?></th>
|
||
|
|
<th><?php esc_html_e('Product', 'wc-licensed-product'); ?></th>
|
||
|
|
<th><?php esc_html_e('Customer', 'wc-licensed-product'); ?></th>
|
||
|
|
<th><?php esc_html_e('Domain', 'wc-licensed-product'); ?></th>
|
||
|
|
<th><?php esc_html_e('Status', 'wc-licensed-product'); ?></th>
|
||
|
|
<th><?php esc_html_e('Expires', 'wc-licensed-product'); ?></th>
|
||
|
|
<th><?php esc_html_e('Actions', 'wc-licensed-product'); ?></th>
|
||
|
|
</tr>
|
||
|
|
</thead>
|
||
|
|
<tbody>
|
||
|
|
<?php if (empty($enrichedLicenses)): ?>
|
||
|
|
<tr>
|
||
|
|
<td colspan="7"><?php esc_html_e('No licenses found.', 'wc-licensed-product'); ?></td>
|
||
|
|
</tr>
|
||
|
|
<?php else: ?>
|
||
|
|
<?php foreach ($enrichedLicenses as $item): ?>
|
||
|
|
<tr>
|
||
|
|
<td><code><?php echo esc_html($item['license']->getLicenseKey()); ?></code></td>
|
||
|
|
<td>
|
||
|
|
<?php if ($item['product_edit_url']): ?>
|
||
|
|
<a href="<?php echo esc_url($item['product_edit_url']); ?>">
|
||
|
|
<?php echo esc_html($item['product_name']); ?>
|
||
|
|
</a>
|
||
|
|
<?php else: ?>
|
||
|
|
<?php echo esc_html($item['product_name']); ?>
|
||
|
|
<?php endif; ?>
|
||
|
|
</td>
|
||
|
|
<td>
|
||
|
|
<?php echo esc_html($item['customer_name']); ?>
|
||
|
|
<?php if ($item['customer_email']): ?>
|
||
|
|
<br><small><?php echo esc_html($item['customer_email']); ?></small>
|
||
|
|
<?php endif; ?>
|
||
|
|
</td>
|
||
|
|
<td><?php echo esc_html($item['license']->getDomain()); ?></td>
|
||
|
|
<td>
|
||
|
|
<span class="license-status license-status-<?php echo esc_attr($item['license']->getStatus()); ?>">
|
||
|
|
<?php echo esc_html(ucfirst($item['license']->getStatus())); ?>
|
||
|
|
</span>
|
||
|
|
</td>
|
||
|
|
<td>
|
||
|
|
<?php
|
||
|
|
$expiresAt = $item['license']->getExpiresAt();
|
||
|
|
echo $expiresAt
|
||
|
|
? esc_html($expiresAt->format(get_option('date_format')))
|
||
|
|
: esc_html__('Never', 'wc-licensed-product');
|
||
|
|
?>
|
||
|
|
</td>
|
||
|
|
<td>
|
||
|
|
<?php if ($item['license']->getStatus() !== License::STATUS_REVOKED): ?>
|
||
|
|
<a href="<?php echo esc_url(wp_nonce_url(
|
||
|
|
admin_url('admin.php?page=wc-licenses&action=revoke&license_id=' . $item['license']->getId()),
|
||
|
|
'revoke_license'
|
||
|
|
)); ?>" class="button button-small" onclick="return confirm('<?php esc_attr_e('Are you sure?', 'wc-licensed-product'); ?>')">
|
||
|
|
<?php esc_html_e('Revoke', 'wc-licensed-product'); ?>
|
||
|
|
</a>
|
||
|
|
<?php endif; ?>
|
||
|
|
<a href="<?php echo esc_url(wp_nonce_url(
|
||
|
|
admin_url('admin.php?page=wc-licenses&action=delete&license_id=' . $item['license']->getId()),
|
||
|
|
'delete_license'
|
||
|
|
)); ?>" class="button button-small button-link-delete" onclick="return confirm('<?php esc_attr_e('Are you sure you want to delete this license?', 'wc-licensed-product'); ?>')">
|
||
|
|
<?php esc_html_e('Delete', 'wc-licensed-product'); ?>
|
||
|
|
</a>
|
||
|
|
</td>
|
||
|
|
</tr>
|
||
|
|
<?php endforeach; ?>
|
||
|
|
<?php endif; ?>
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
|
||
|
|
<?php if ($totalPages > 1): ?>
|
||
|
|
<div class="tablenav bottom">
|
||
|
|
<div class="tablenav-pages">
|
||
|
|
<?php
|
||
|
|
echo paginate_links([
|
||
|
|
'base' => admin_url('admin.php?page=wc-licenses&paged=%#%'),
|
||
|
|
'format' => '',
|
||
|
|
'current' => $page,
|
||
|
|
'total' => $totalPages,
|
||
|
|
]);
|
||
|
|
?>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
<?php endif; ?>
|
||
|
|
</div>
|
||
|
|
<?php
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add license column to orders list
|
||
|
|
*/
|
||
|
|
public function addOrdersLicenseColumn(array $columns): array
|
||
|
|
{
|
||
|
|
$newColumns = [];
|
||
|
|
foreach ($columns as $key => $value) {
|
||
|
|
$newColumns[$key] = $value;
|
||
|
|
if ($key === 'order_status') {
|
||
|
|
$newColumns['license'] = __('License', 'wc-licensed-product');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $newColumns;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display license column content
|
||
|
|
*/
|
||
|
|
public function displayOrdersLicenseColumn(string $column, int $postId): void
|
||
|
|
{
|
||
|
|
if ($column !== 'license') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$order = wc_get_order($postId);
|
||
|
|
$this->outputLicenseColumnContent($order);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display license column content (HPOS)
|
||
|
|
*/
|
||
|
|
public function displayOrdersLicenseColumnHpos(string $column, \WC_Order $order): void
|
||
|
|
{
|
||
|
|
if ($column !== 'license') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->outputLicenseColumnContent($order);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Output license column content
|
||
|
|
*/
|
||
|
|
private function outputLicenseColumnContent(?\WC_Order $order): void
|
||
|
|
{
|
||
|
|
if (!$order) {
|
||
|
|
echo '—';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$hasLicensedProduct = false;
|
||
|
|
foreach ($order->get_items() as $item) {
|
||
|
|
$product = $item->get_product();
|
||
|
|
if ($product && $product->is_type('licensed')) {
|
||
|
|
$hasLicensedProduct = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!$hasLicensedProduct) {
|
||
|
|
echo '—';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$domain = $order->get_meta('_licensed_product_domain');
|
||
|
|
if ($domain) {
|
||
|
|
echo '<span class="dashicons dashicons-admin-network"></span> ' . esc_html($domain);
|
||
|
|
} else {
|
||
|
|
echo '<span class="dashicons dashicons-warning" title="' . esc_attr__('No domain specified', 'wc-licensed-product') . '"></span>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|