| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMailScoped\YayCommerce\AdminShell\Menu; |
| 4 |
|
| 5 |
use YayMailScoped\YayCommerce\AdminShell\Contracts\PluginMenuAdapter; |
| 6 |
use YayMailScoped\YayCommerce\AdminShell\License\Contracts\LicenseConfigAdapter; |
| 7 |
use YayMailScoped\YayCommerce\AdminShell\License\License; |
| 8 |
/** |
| 9 |
* Registers a plugin-named submenu under YayCommerce. |
| 10 |
* Works with any PluginMenuAdapter (lite or pro). |
| 11 |
* |
| 12 |
* For pro plugins: automatically redirects to Licenses page when license |
| 13 |
* is not active — no developer action needed. |
| 14 |
*/ |
| 15 |
class PluginSubmenu |
| 16 |
{ |
| 17 |
private PluginMenuAdapter $adapter; |
| 18 |
public function __construct(PluginMenuAdapter $adapter) |
| 19 |
{ |
| 20 |
$this->adapter = $adapter; |
| 21 |
} |
| 22 |
public function init() : void |
| 23 |
{ |
| 24 |
\add_action('admin_menu', [$this, 'register'], 10); |
| 25 |
} |
| 26 |
public function register() : void |
| 27 |
{ |
| 28 |
$menu_slug = $this->adapter->get_menu_slug(); |
| 29 |
if (empty($menu_slug)) { |
| 30 |
return; |
| 31 |
} |
| 32 |
global $submenu; |
| 33 |
$has_menu = \false; |
| 34 |
$is_override = \false; |
| 35 |
if (isset($submenu['yaycommerce'])) { |
| 36 |
$yaycommerce_menu = $submenu['yaycommerce']; |
| 37 |
foreach ($yaycommerce_menu as $key => $value) { |
| 38 |
if ($value[2] === $menu_slug) { |
| 39 |
if (\method_exists($this->adapter, 'is_licensed') && $this->adapter->is_licensed() || !\method_exists($this->adapter, 'is_licensed')) { |
| 40 |
\remove_submenu_page('yaycommerce', $menu_slug); |
| 41 |
$is_override = \true; |
| 42 |
} else { |
| 43 |
$has_menu = \true; |
| 44 |
} |
| 45 |
break; |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
if ($has_menu) { |
| 50 |
return; |
| 51 |
} |
| 52 |
$callback = $this->adapter->get_settings_page_callback(); |
| 53 |
// Guard: ensure callback is actually callable |
| 54 |
if (null !== $callback && !\is_callable($callback)) { |
| 55 |
$callback = null; |
| 56 |
} |
| 57 |
// Pro plugins without active license → override callback to redirect |
| 58 |
$needs_redirect = \false; |
| 59 |
if ($this->adapter instanceof LicenseConfigAdapter) { |
| 60 |
$license = new License($this->adapter); |
| 61 |
if (!$license->is_active() || $license->is_expired()) { |
| 62 |
$needs_redirect = \true; |
| 63 |
$callback = null; |
| 64 |
} |
| 65 |
} |
| 66 |
$page_id = \add_submenu_page('yaycommerce', $this->adapter->get_page_title(), $this->adapter->get_menu_title(), $this->adapter->get_capability(), $menu_slug, $callback ?? '__return_false', $this->adapter->get_settings_page_position()); |
| 67 |
if ($is_override) { |
| 68 |
\remove_all_actions('load-' . $page_id); |
| 69 |
} |
| 70 |
if ($needs_redirect) { |
| 71 |
\add_action('load-' . $page_id, [__CLASS__, 'redirect_to_licenses']); |
| 72 |
} |
| 73 |
} |
| 74 |
public static function redirect_to_licenses() : void |
| 75 |
{ |
| 76 |
\wp_safe_redirect(\admin_url('admin.php?page=yaycommerce-licenses')); |
| 77 |
exit; |
| 78 |
} |
| 79 |
} |
| 80 |
|