| | |
| | | <?php |
| | | namespace JVBase\managers; |
| | | |
| | | use JVBase\forms\TaxonomySelector;use JVBase\managers\CRUD; |
| | | use JVBase\meta\MetaManager; |
| | | use JVBase\forms\TaxonomySelector; |
| | | use JVBase\managers\CRUD; |
| | | use JVBase\meta\Form; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\utility\Features; |
| | | use JVBase\ui\Navigation; |
| | | use WP_User; |
| | |
| | | class DashboardManager |
| | | { |
| | | protected WP_User $user; |
| | | protected CacheManager $cache; |
| | | protected Cache $cache; |
| | | protected string $role; |
| | | protected string $baseURL; |
| | | protected int $userLink; |
| | | |
| | | public function __construct() |
| | | { |
| | | $this->cache = CacheManager::for('dashboard', WEEK_IN_SECONDS); |
| | | $this->cache = Cache::for('dashboard', WEEK_IN_SECONDS)->connect('user'); |
| | | add_action('init', [$this, 'registerDashboard']); |
| | | if (!$this->isRegistered()) { |
| | | add_action('init', [$this, 'buildDashboard']); |
| | | } |
| | | $this->cache->flush(); |
| | | $this->user = wp_get_current_user(); |
| | | $this->role = jvbUserRole($this->user->ID); |
| | | $this->userLink = (int)get_user_meta($this->user->ID, BASE.'link', true); |
| | |
| | | $singular = 'Dashboard'; |
| | | register_post_type(BASE.'dash', array( |
| | | 'labels' => [ |
| | | 'name' => $plural, |
| | | 'singular_name' => $singular, |
| | | 'menu_name' => $plural, |
| | | 'add_new' => "Add New {$singular}", |
| | | 'add_new_item' => "Add New {$singular}", |
| | | 'edit_item' => "Edit {$singular}", |
| | | 'new_item' => "New {$singular}", |
| | | 'view_item' => "View {$singular}", |
| | | 'search_items' => "Search {$plural}", |
| | | 'not_found' => "No {$plural} found", |
| | | 'not_found_in_trash' => "No {$plural} found in Trash" |
| | | ], |
| | | 'name' => $plural, |
| | | 'singular_name' => $singular, |
| | | 'menu_name' => $plural, |
| | | 'name_admin_bar' => $singular, |
| | | 'add_new' => "Add New", |
| | | 'add_new_item' => "Add New {$singular}", |
| | | 'new_item' => "New {$singular}", |
| | | 'edit_item' => "Edit {$singular}", |
| | | 'view_item' => "View {$singular}", |
| | | 'all_items' => "All {$plural}", |
| | | 'search_items' => "Search {$plural}", |
| | | 'parent_item_colon' => "Parent {$plural}:", |
| | | 'not_found' => "No {$plural} found.", |
| | | 'not_found_in_trash' => "No {$plural} found in Trash.", |
| | | ], |
| | | 'menu_icon' => jvbCSSIcon('gauge'), |
| | | 'public' => true, |
| | | 'publicly_queryable' => true, |
| | |
| | | */ |
| | | public function redirectFromAdmin() |
| | | { |
| | | // Skip if already processing a redirect |
| | | if (defined('DOING_AJAX') && DOING_AJAX) { |
| | | return; |
| | | } |
| | | |
| | | // Ensure user is fully loaded |
| | | if (!did_action('wp_loaded')) { |
| | | return; |
| | | } |
| | | |
| | | // Allow admins to access wp-admin if needed |
| | | if (current_user_can('manage_options')) { |
| | | return; |
| | | } |
| | | // Redirect to custom dashboard |
| | | $this->redirectToDashboard(); |
| | | if (is_user_logged_in() && isOurPeople()) { |
| | | $this->redirectToDashboard(); |
| | | } |
| | | } |
| | | |
| | | protected function redirectToLogin():void |
| | |
| | | $page = $this->getCurrentPageTitle(); |
| | | // Check if page exists in allowed pages |
| | | $allowedPages = $this->getUserAllowedPages(); |
| | | |
| | | if (!in_array($page, $allowedPages)) { |
| | | error_log("User not allowed to access page: {$page}"); |
| | | $this->redirectToDashboard(); |
| | | return; |
| | | } |
| | | } |
| | | } |
| | |
| | | // Pass along to the Integrations template handler which knows to check for subpages |
| | | $page = 'integrations'; |
| | | } |
| | | echo $this->renderDashboard($page); |
| | | //TODO: Reenable |
| | | // echo $this->cache->remember( |
| | | // $page, |
| | | // function() use ($page) { |
| | | // return $this->renderDashboard($page); |
| | | // } |
| | | // ); |
| | | // echo $this->renderDashboard($page); |
| | | |
| | | echo $this->cache->remember( |
| | | $page, |
| | | function() use ($page) { |
| | | return $this->renderDashboard($page); |
| | | } |
| | | ); |
| | | |
| | | return ''; |
| | | } |
| | |
| | | if (!is_singular(BASE.'dash') && !is_post_type_archive(BASE.'dash')) { |
| | | return; |
| | | } |
| | | wp_enqueue_style('jvb-icons-dash'); |
| | | wp_enqueue_style('jvb-icons-forms'); |
| | | IconsManager::for('forms')->enqueueIconStyles(); |
| | | IconsManager::for('dash')->enqueueIconStyles(); |
| | | |
| | | wp_enqueue_script('jvb-form'); |
| | | wp_enqueue_script('jvb-selector'); |
| | |
| | | break; |
| | | case 'admin': |
| | | case 'dash': |
| | | if (current_user_can('manage_options') && apply_filters('jvbAdminDashboard', '') === '') { |
| | | wp_enqueue_script( |
| | | 'jvb-admin', |
| | | JVB_URL . 'assets/js/min/admin.min.js', |
| | | [ |
| | | 'jvb-queue', |
| | | 'jvb-loading' |
| | | ], |
| | | [ |
| | | 'strategy' => 'defer', |
| | | 'in_footer' => true |
| | | ] |
| | | ); |
| | | // if (current_user_can('manage_options') && apply_filters('jvbAdminDashboard', '') === '') { |
| | | // wp_enqueue_script( |
| | | // 'jvb-admin', |
| | | // JVB_URL . 'assets/js/min/admin.min.js', |
| | | // [ |
| | | // 'jvb-queue', |
| | | //// 'jvb-loading' |
| | | // ], |
| | | // [ |
| | | // 'strategy' => 'defer', |
| | | // 'in_footer' => true |
| | | // ] |
| | | // ); |
| | | |
| | | wp_localize_script( |
| | | 'jvb-admin', |
| | | 'jvbAdmin', |
| | | [ |
| | | 'nonce' => wp_create_nonce('itsme') |
| | | ] |
| | | ); |
| | | } |
| | | // wp_localize_script( |
| | | // 'jvb-admin', |
| | | // 'jvbAdmin', |
| | | // [ |
| | | // 'nonce' => wp_create_nonce('itsme') |
| | | // ] |
| | | // ); |
| | | // } |
| | | break; |
| | | case 'seo': |
| | | wp_enqueue_script('jvb-schema'); |
| | |
| | | if (!$post) { |
| | | return ''; |
| | | } |
| | | |
| | | return $post->post_title; |
| | | return html_entity_decode($post->post_title); |
| | | } |
| | | protected function getCurrentPageSlug():string |
| | | { |
| | |
| | | <ul> |
| | | <?= jvbNotificationMenu() ?> |
| | | <?= jvbHelpMenu() ?> |
| | | <li><a href="<?=wp_logout_url(get_home_url())?>" title="Logout"><?=jvbIcon('sign-out')?></a></li> |
| | | </ul> |
| | | </nav> |
| | | </header> |
| | |
| | | |
| | | echo '<h2>What would you like to do today?</h2>'; |
| | | |
| | | echo '<ul>'; |
| | | echo '<ul class="dashboard">'; |
| | | foreach ($pages as $slug => $page) { |
| | | if ($page === 'dash') { |
| | | continue; |
| | |
| | | $jvb_everything = array_merge(JVB_CONTENT, JVB_TAXONOMY); |
| | | |
| | | foreach ($jvb_everything as $type => $settings) { |
| | | $meta = new MetaManager(null, 'form'); |
| | | $fields = jvbGetFields($type); |
| | | ?> |
| | | <template class="<?= $type ?>Table"> |
| | |
| | | <?php |
| | | $config['type'] = 'text'; |
| | | $config['description'] = ''; |
| | | $meta->render('form', $n, $config); |
| | | Form::render($n, null, $config); |
| | | ?> |
| | | </td> |
| | | <?php |
| | |
| | | echo jvbNewModal( |
| | | 'edit-modal '.$type, |
| | | 'Edit '.ucfirst($type), |
| | | $meta->renderForm('admin', [], $fields) |
| | | jvbRenderForm('admin', $fields) |
| | | ); |
| | | } |
| | | |
| | | return ob_get_clean(); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | $cacheKey = "user_pages_{$userID}"; |
| | | $pages = $this->cache->get($cacheKey); |
| | | $pages = false; |
| | | $pages = $this->cache->get($userID); |
| | | |
| | | if ($pages === false || JVB_TESTING) { |
| | | if (user_can($userID, 'manage_options')) { |
| | | // Admin gets all pages as flat array |
| | | $pages = $this->getAllDashboardPages(); |
| | | // Extract just the values (slugs) |
| | | $this->cache->set($cacheKey, $pages, WEEK_IN_SECONDS); |
| | | $this->cache->set($userID, $pages, WEEK_IN_SECONDS); |
| | | return $pages; |
| | | } |
| | | $roles = array_map('jvbNoBase', $user->roles); |
| | |
| | | $pages = apply_filters('jvbUserDashboardPages', $pages, $user->roles, $userID); |
| | | $pages = array_unique($pages); |
| | | |
| | | $this->cache->set($cacheKey, $pages, WEEK_IN_SECONDS); |
| | | $this->cache->set($userID, $pages, WEEK_IN_SECONDS); |
| | | } |
| | | |
| | | return $pages; |
| | |
| | | // Default to edit_{type}s |
| | | return 'edit_'.$type.'s'; |
| | | } |
| | | |
| | | /** |
| | | * Invalidate dashboard page cache for a user or all users |
| | | * Call this when user roles or permissions change |
| | | * @param int|null $userID Specific user to invalidate, null for all |
| | | * @return void |
| | | */ |
| | | public function invalidatePagesCache(?int $userID = null):void |
| | | { |
| | | if ($userID !== null) { |
| | | $this->cache->delete("user_pages_{$userID}"); |
| | | } else { |
| | | // Invalidate all user caches by invalidating the group |
| | | $this->cache->invalidate(); |
| | | } |
| | | } |
| | | } |