| | |
| | | use JVBase\forms\TaxonomySelector; |
| | | use JVBase\base\Site; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\managers\IconsManager;use JVBase\managers\RoleManager;use JVBase\meta\Form;use JVBase\meta\Meta;use JVBase\registrar\Fields;use JVBase\registrar\Registrar; |
| | | use JVBase\managers\IconsManager; |
| | | use JVBase\managers\LoginManager; |
| | | use JVBase\managers\RoleManager; |
| | | use JVBase\meta\Form; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\registrar\Fields; |
| | | use JVBase\registrar\Registrar; |
| | | use JVBase\ui\Navigation; |
| | | use JVBase\ui\Tabs; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | |
| | | add_action(BASE.'dashboard_page_dash', [$this, 'renderMain'], 10); |
| | | } |
| | | add_action(BASE.'dashboard_page_account', [$this, 'renderAccount'], 10); |
| | | add_action(BASE.'dashboard_page_reset_password', [$this, 'resetPasswordPage'], 10); |
| | | } |
| | | |
| | | protected function registerDashboard():void |
| | |
| | | return; |
| | | } |
| | | $page = $this->getCurrentPage(); |
| | | if (!$page) { |
| | | $this->redirectToDashboard(); |
| | | } |
| | | if (!in_array($page, $this->pages)) { |
| | | error_log('Looking for page: '.$page. ' in: '.print_r($this->pages, true)); |
| | | error_log('[DashboardManager]::handleRedirect could not find page for '.$page); |
| | | error_log('Looking for page: '.$page->getSlug(). ' in: '.print_r($this->pages, true)); |
| | | error_log('[DashboardManager]::handleRedirect could not find page for '.$page->getTitle()); |
| | | return; |
| | | } |
| | | $permission = $page->getPermission(); |
| | | if (!empty($permission) && !current_user_can($permission)) { |
| | | error_log('[DashboardManager]::handleRedirect User cannot manage '.$page); |
| | | if (!empty($permission) && !$this->handlePermission($page)) { |
| | | error_log('[DashboardManager]::handleRedirect User cannot manage '.$page->getTitle()); |
| | | $this->redirectToDashboard(); |
| | | } |
| | | } |
| | |
| | | // uasort($pages, fn($a, $b) => $a->getOrder() <=> $b->getOrder()); |
| | | uasort($all, fn($a, $b) => $a->getOrder() <=> $b->getOrder()); |
| | | |
| | | |
| | | foreach ($all as $slug => $item) { |
| | | if (is_a($item, Section::class)) { |
| | | $this->buildMenuSection($item, $menu); |
| | | } else { |
| | | $menuItem = $menu->addItem($item->getTitle(), jvbDashIcon($item->getIcon())); |
| | | $menuItem->url($item->getURL()); |
| | | if ($this->getCurrentPage() === $item) { |
| | | $menuItem->current(); |
| | | } |
| | | } |
| | | } |
| | | return $menu->render(); |
| | |
| | | $main = $this->getMainPage($section->getSlug()); |
| | | if ($main && $section->getIsLink()) { |
| | | $page->url($main->getURL()); |
| | | |
| | | if ($this->getCurrentPage() === $page) { |
| | | $page->current(); |
| | | } |
| | | } |
| | | $submenu = $page->submenu(); |
| | | |
| | |
| | | } elseif ($slug !== $section->getSlug()) { |
| | | $menuItem = $submenu->addItem($item->getTitle(), jvbDashIcon($item->getIcon())); |
| | | $menuItem->url($item->getURL()); |
| | | |
| | | if ($this->getCurrentPage() === $item) { |
| | | $menuItem->current(); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | $account = $this->addPage('Account', 'account', 'user-circle'); |
| | | $account->setScripts(['jvb-form','jvb-tabs']); |
| | | $account->setSection('account'); |
| | | |
| | | $this->sections['account'] = new Section('Account', 'account', 'user-circle'); |
| | |
| | | |
| | | $password = $this->addPage('Reset Password', 'reset-password', 'password', $account->getID()); |
| | | $password->setOrder(2); |
| | | $password->setScripts(['jvb-form', 'jvb-tabs']); |
| | | $password->setSection('account'); |
| | | |
| | | } |
| | |
| | | $page = $this->addPage('Integrations', 'integrations', 'plugs-connected'); |
| | | $page->setSection('settings'); |
| | | $page->setOrder($order); |
| | | $page->setPermission('user_has_integrations'); |
| | | $page->setScripts(['jvb-integrations']); |
| | | $parent = $page->getID(); |
| | | |
| | |
| | | protected function setCurrentUserPages():void |
| | | { |
| | | $this->currentUserPages = array_filter($this->pages, function($page) { |
| | | return current_user_can('manage_options') || empty($page->getPermission()) || current_user_can($page->getPermission()); |
| | | return current_user_can('manage_options') || empty($page->getPermission()) || $this->handlePermission($page); |
| | | }); |
| | | } |
| | | protected function handlePermission($page):bool |
| | | { |
| | | return match($page->getPermission()) { |
| | | 'user_has_integrations' => $this->handleIntegrationPermission(), |
| | | default => current_user_can($page->getPermission()) |
| | | }; |
| | | } |
| | | protected function handleIntegrationPermission():bool |
| | | { |
| | | $user = wp_get_current_user(); |
| | | $role = jvbUserRole($user->ID); |
| | | if (current_user_can('manage_options')) { |
| | | return true; |
| | | } |
| | | $registrar = Registrar::getInstance($role); |
| | | if (!$registrar) { |
| | | return false; |
| | | } |
| | | return $registrar->hasAnyIntegrations(); |
| | | } |
| | | |
| | | #[NoReturn]protected function redirectToLogin():void |
| | | { |
| | |
| | | |
| | | public function renderAccount():void |
| | | { |
| | | |
| | | $user = get_userdata(get_current_user_id()); |
| | | $role = jvbUserRole($user->ID); |
| | | $registrar = Registrar::getInstance($role); |
| | | |
| | | |
| | | if ($registrar) { |
| | | $meta = Meta::forUser($user->ID); |
| | | $fields = $registrar->getFields(); |
| | | $sections = $registrar->getSections(); |
| | | $tabs = new Tabs(); |
| | | foreach ($sections as $slug => $config) { |
| | | $tab = $tabs->addTab($slug); |
| | | $tab->title($config['title']); |
| | | if (!empty($config['description'])) { |
| | | $tab->description($config['description']); |
| | | } |
| | | if (!empty($config['icon'])) { |
| | | $tab->icon($config['icon']); |
| | | } |
| | | $content = implode('', array_map(function ($f) use ($meta) { |
| | | return Form::renderFrom($meta,$f); |
| | | },$config['fields'])); |
| | | $tab->content($content); |
| | | } |
| | | echo '<form data-save="user" data-auto data-action="dash">'.jvbFormStatus().$tabs->render().'<button type="submit">'.jvbDashIcon('floppy-disk').'Save</button></form>'; |
| | | } else { |
| | | $fields = Fields::getUserFields(); |
| | | $meta = new Meta($user->ID, 'user', $fields); |
| | | } |
| | | |
| | | echo Form::renderFormFrom($meta, 'user', [ |
| | | 'heading' => 'Your Account', |
| | | 'description' => [ |
| | | 'You can set your information here.', |
| | | 'To reset your password, check the side menu under "Account"' |
| | | ] |
| | | ], |
| | | 'submit' => true |
| | | ]); |
| | | } |
| | | |
| | | $script = 'document.addEventListener(\'DOMContentLoaded\', async function () { |
| | | |
| | | window.auth.subscribe(event => { |
| | | if (event === \'auth-loaded\') { |
| | | const form = document.querySelector(\'form[data-save="user"]\'); |
| | | if (!form || !window.jvbForm) return; |
| | | |
| | | window.jvbForm.registerForm(form); |
| | | } |
| | | }); |
| | | });'; |
| | | // wp_add_inline_script('jvb-form', $script); |
| | | } |
| | | |
| | | public function resetPasswordPage():void |
| | | { |
| | | ?> |
| | | <h1>Reset Your Password</h1> |
| | | <p>If you'd like to reset your password, you can do so here.</p> |
| | | <?php |
| | | if (Site::has('magic_link')) { |
| | | echo '<p>Alternatively, you can always login using the magic link, which sets a temporary password for 15 minutes.</p>'; |
| | | } |
| | | ?> |
| | | <?= LoginManager::getInstance()->renderLoginForm('resetpass'); ?> |
| | | |
| | | <?php |
| | | } |
| | | } |