Jake Vanderwerf
2025-10-20 e729f920139f0c65902be2d6b2c32466b08375e8
inc/managers/DashboardManager.php
@@ -3,6 +3,7 @@
use JVBase\managers\CRUD;
use JVBase\meta\MetaManager;
use JVBase\utility\Features;
use WP_User;
if (!defined('ABSPATH')) {
@@ -126,6 +127,120 @@
        remove_action('init', [$this, 'buildDashboard']);
    }
   protected function getAllDashboardPages():array
   {
      $manageableContent = get_option(BASE.'all_dashboard_pages');
      if (JVB_TESTING) {
         $manageableContent = false;
      }
      if ($manageableContent === false) {
         $manageableContent = [];
         $bios = [];
         foreach (JVB_USER as $role => $config) {
            $manageableContent = array_merge($manageableContent, jvbRolePages($role));
         }
         if (Features::forSite()->has('referrals')) {
            $manageableContent[] = 'referrals';
         }
         foreach (JVB_TAXONOMY as $tax => $config) {
            if (Features::forTaxonomy($tax)->has('is_content')) {
               $manageableContent[] = strtolower($config['plural']);
            }
         }
         if (Features::forMembership()->has('can_invite')) {
            $manageableContent[] = 'invites';
         }
         if (Features::forMembership()->has('term_approval')) {
            $manageableContent[] = 'approvals';
         }
         if (Features::forMembership()->has('forum')) {
            $manageableContent[] = 'news';
         }
         if (Features::forMembership()->has('member_content')) {
            $manageableContent[] = 'metrics';
         }
         if (!empty($bios)) {
            $manageableContent[] = 'bio';
         }
         if (Features::forSite()->has('favourites')) {
            $manageableContent[] = 'favourites';
         }
         if (Features::anyContentHas('karma') || Features::anyTaxonomyHas('karma') || Features::anyUserHas('karma')){
            $manageableContent[] = 'karmic-score';
         }
         if (Features::forSite()->has('notifications')) {
            $manageableContent[] = 'notifications';
         }
         if (Features::forSite()->has('support')){
            $manageableContent[] = 'support';
         }
         if (Features::hasAnyIntegration()) {
            $manageableContent[] = 'integrations';
         }
         $manageableContent[] = 'admin';
         $manageableContent = apply_filters('jvbDashboardPages', $manageableContent);
         $manageableContent = array_unique($manageableContent);
         sort($manageableContent);
         $manageableContent = array_map(function ($content) {
            return str_replace('_', '-', $content);
         }, $manageableContent);
         update_option(BASE.'all_dashboard_pages', $manageableContent);
      }
      return $manageableContent;
   }
   protected function getRolePages(string $role):array
   {
      if (!array_key_exists(jvbNoBase($role), JVB_USER)) {
         return [];
      }
      $manageableContent = get_option(BASE.$role.'_pages');
      if (JVB_TESTING) {
         $manageableContent = false;
      }
      if ($manageableContent === false) {
         $manageableContent = [];
         $config = JVB_USER[$role];
         $content = $config['can_create'];
         $settings = $bio = false;
         if (array_key_exists('profile', $config)) {
            $manageableContent[] = $config['profile'];
         }
         foreach ($content as $c) {
            if (is_array($c)) {
               foreach ($c as $type => $contents) {
                  $manageableContent = array_merge($manageableContent, $contents);
               }
            } else {
               $manageableContent = array_merge($manageableContent, [$c]);
            }
         }
         if (array_key_exists('has_dashboard', $config)) {
            $manageableContent[] = 'settings';
         }
         update_option(BASE.$role.'_pages', $manageableContent);
      }
      return $manageableContent;
   }
    protected function getTitle(string $page):string
    {
        $content = JVB_CONTENT;
@@ -206,7 +321,14 @@
            return $template;
        }
        if (!isOurPeople() && !current_user_can('manage_options')) {
            wp_redirect(wp_login_url(get_home_url(2, '/dash')));
         error_log('Redirecting because:');
         if (!isOurPeople()) {
            error_log('Not our people');
         }
         if (!current_user_can('manage_options')) {
            error_log('Cannot manage options');
         }
            wp_redirect(wp_login_url(get_home_url(null, '/dash')));
            exit;
        }
@@ -214,6 +336,65 @@
        $page = $this->getCurrentPage();
      switch ($page) {
         case 'integrations':
            if (!Features::hasAnyIntegration('user', $this->role)) {
               wp_redirect(get_home_url(null, '/dash'));
               exit;
            }
            break;
         case 'bluesky':
         case 'cloudflare':
         case 'facebook':
         case 'google-maps':
         case 'google-my-business':
         case 'helcim':
         case 'instagram':
         case 'square':
         case 'umami':
            if (!Features::hasIntegration($page,'user', $this->role)) {
               wp_redirect(get_home_url(null, '/dash'));
               exit;
            }
            break;
         case 'bio':
            $permission = JVB_USER[$this->role]['profile']??false;
            if (!$permission || (!current_user_can('manage_'.$permission) && !current_user_can('manage_options'))) {
               wp_redirect(get_home_url(null, '/dash'));
               exit;
            }
            break;
         case 'settings':
            if (!current_user_can('manage_settings') && !current_user_can('manage_options')) {
               wp_redirect(get_home_url(null, '/dash'));
                  exit;
            }
            break;
         case 'approval':
            if (!current_user_can('skip_moderation')) {
               wp_redirect(get_home_url(null, '/dash'));
               exit;
            }
            break;
            case 'dash':
               break;
         default:
            $type = match($page) {
               'menu-item' => 'menu_item',
               'events' => 'event',
               default => $page
            };
            $permission = strtolower(str_replace(' ', '_',JVB_CONTENT[$type]['plural']??$type.'s'));
            if (!current_user_can('edit_'.$permission)) {
               error_log('User cannot edit: '.$permission);
               wp_redirect(get_home_url(null, '/dash'));
               exit;
            }
            break;
      }
      // Enqueue needed styles/scripts
      jvbInlineStyles('nav');
      jvbInlineStyles('dash');
@@ -415,7 +596,7 @@
            $page = str_replace('_', '-', $page);
            $link = ($page === 'dash') ? '/'.$page : "/dash/$page";
            ?>
            <link rel="preconnect" href="<?= get_home_url(2, $link)?>"/>
            <link rel="preconnect" href="<?= get_home_url(null, $link)?>"/>
            <?php
         }
          ?>
@@ -477,7 +658,7 @@
                    printf(
                        '<li%s><a href="%s"%s data-page="%s" data-dash title="%s">%s<span>%s</span></a></li>',
                        $active,
                        get_home_url(2, $link),
                        get_home_url(null, $link),
                        $current,
                        $page,
                        $title,
@@ -525,7 +706,7 @@
            $description = $this->getDescription($page);
            if ($title !== '') {
                echo '<li><p><a href="'.get_home_url(2, '/dash/'.$url.'/').'"
                echo '<li><p><a href="'.get_home_url(null, '/dash/'.$url.'/').'"
                    data-page="'.$url.'" data-dash>'.jvbIcon($page).ucfirst($title).'</a></p>'.$description.'</li>';
            }
@@ -543,10 +724,7 @@
     */
    protected function renderForm(string $type):void
    {
        if (!current_user_can('manage_'.$type)) {
            wp_redirect(get_home_url(2, '/dash'));
            exit;
        }
        wp_enqueue_script(
            'jvb-bio-manager',
            JVB_URL.'assets/js/min/bioManager.min.js',
@@ -562,10 +740,6 @@
    protected function renderSettings():void
    {
        if (!current_user_can('manage_options') && !current_user_can('manage_settings')) {
            wp_redirect(get_home_url(2, '/dash'));
            exit;
        }
      wp_enqueue_script('jvb-form');
        wp_enqueue_script(
            'jvb-bio-manager',
@@ -593,7 +767,7 @@
      if (!empty($integrations)) {
         $out = '<nav class="integrations"><ul>';
         $url = get_home_url(2, '/dash/integrations/');
         $url = get_home_url(null, '/dash/integrations/');
         $out .= '<li><a href="'.$url.'">'.jvbIcon('plugs-connected').'Integrations</a></li>';
         foreach ($integrations as $name=> $integration) {
            if (!JVB()->userCanConnect($name, $this->user->ID) || !$integration->hasDefaults()) {
@@ -609,12 +783,6 @@
   protected function renderIntegrations(string $page):void
   {
      //TODO: Make manage_integrations permission
//    if (!current_user_can('manage_integrations')) {
//       wp_redirect(get_home_url(2, '/dash'));
//       exit;
//    }
      echo $this->getIntegrationsMenu();
      $map = [
         'google-my-business' => 'gmb',
@@ -653,10 +821,6 @@
    protected function renderApprovals():void
    {
        if (!current_user_can('skip_moderation')) {
            wp_redirect(get_home_url(2, '/dash'));
            exit;
        }
        ?>
        <div class="approvals container">
            <nav class="tabs row start" role="tablist">
@@ -730,12 +894,6 @@
         'events' => 'event',
         default => $type
      };
      $permission = JVB_CONTENT[$type]['plural']??$type.'s';
        if (!current_user_can('edit_'.$permission)) {
            wp_redirect(get_home_url(2, '/dash'));
            exit;
        }
        $crud = new CRUD($type);
        $crud->render();
@@ -743,12 +901,6 @@
    protected function renderAdmin():void
    {
        //TODO: This has to be built from the settings from setup.php
        if (!current_user_can('manage_options')) {
            wp_redirect(get_home_url(2, '/dash'));
            exit;
        }
        ?>
        <nav class="tabs row start" role="tablist">
        <?php