Jake Vanderwerf
3 days ago ba1e1ccf869b818f7a7a897264dfea05563a7796
inc/registrar/Registrar.php
@@ -32,6 +32,7 @@
   protected string $type;
   protected string $singular;
   protected string $plural;
   protected string $profile;
   protected string $description ='';
   protected Fields $fields;
   protected array $sections = [];
@@ -47,15 +48,23 @@
   public ?string $rewrite_taxonomy = null;
   public bool $add_image_column = false;
   public bool $prefix_post_type = false;
   public string $prefix_with = 'by';
   public bool $system = false;
   protected static array $allFlags = [
      //Shared Flags
      'favouritable', 'karma', 'show_feed', 'show_directory', 'approve_new', 'has_responses', 'invitable',
      //Post Flags
      'hide_single', 'redirect_to_author', 'is_calendar', 'single_image', 'is_timeline', 'is_gallery', 'is_faq', 'is_glossary', 'rewrite_taxonomy',
      'hide_single', 'redirect_to_author', 'is_calendar', 'single_image', 'is_timeline', 'is_gallery', 'is_faq', 'is_glossary', 'rewrite_taxonomy', 'add_image_column',
      //Taxonomy Flags
      'is_content', 'is_ownable', 'verify_entry', 'track_changes', 'associate_user_content',
      'is_content', 'is_ownable', 'verify_entry', 'track_changes', 'associate_user_content', 'prefix_post_type',
      //User Flags
      'has_dashboard', 'can_register', 'can_create', 'keep_stats', 'can_favourite', 'member_verified', 'profile_link', 'manage_others'
      'has_dashboard', 'can_register', 'can_create', 'keep_stats', 'can_favourite', 'member_verified', 'profile_link', 'manage_others',
      //System
      'system'
   ];
   /**********************************************************************************************
   SHARED FLAGS
@@ -139,12 +148,12 @@
   /**
    * @var bool Whether users/content need to request the owner for admission
    */
   protected bool $verify_entry;
   protected bool $verify_entry = false;
   protected ?MakeVerification $verifyEntryHandler = null;
   /**
    * @var bool Whether we should track post movements from term to term (ie. artists in tattoo shops)
    */
   protected bool $track_changes;
   protected bool $track_changes = false;
   protected ?MakeTrackChanges $trackChangesHandler = null;
   /**
@@ -182,11 +191,21 @@
   /**
    * @var array|string
    */
   protected array|string $can_create = [];
   protected array $can_create = [];
   /**
    * @var array slugs of other user roles this role can manage
    */
   protected array $manage_others = [];
   /**
    * @var string The slug of the taxonomy that defines different access points
    * Example, for edmonton.ink, we have artist_type, and $this->>can_create would be:
    *       [
    *          'piercer'   => ['piercings', 'artwork', 'events'],
    *          'tattoo_artist'=> ['tattoos', 'artwork', 'events'],
    *          'artist' => ['artwork']
    *       ]
    */
   protected string $user_subtype;
   /** Configs **/
   protected Breadcrumbs $breadcrumbs;
@@ -219,10 +238,63 @@
//    $this->initClasses();
      $this->setFields();
      add_action('init', [$this, 'register'], 0);
      add_action('init', [$this, 'register'], 2);
      add_filter('jvbDashboardPage', [$this, 'renderDashPage'], 10, 3);
   }
   public static function maybeExcludeSingles(array $IDs):array
   {
      self::ensureInstanced();
      $features = ['hide_single', 'is_timeline'];
      foreach ($features as $feature) {
         foreach (self::withFeature($feature) as $instance) {
            $instance = self::getInstance($instance);
            $cache = Cache::for('tsf')->connect($instance->getType());
            $cache->flush();
            $exclude = $cache->remember(
               $feature,
               function () use ($instance, $feature) {
                  switch ($feature) {
                     case 'hide_single':
                        return $instance->excludeSingle();
                     case 'is_timeline':
                        return $instance->excludeTimeline();
                     default:
                        return [];
                  }
               }
            );
            if (!empty($exclude)) {
               $IDs = array_merge($IDs, $exclude);
            }
         }
      }
      return $IDs;
   }
   protected function excludeSingle():array
   {
      return get_posts([
         'post_type'    => $this->based,
         'posts_per_page'=> -1,
         'fields'    => 'ids',
         'post_status'  => 'publish',
      ]);
   }
   protected function excludeTimeline():array
   {
      return get_posts([
         'post_type'    => $this->based,
         'posts_per_page'=> -1,
         'fields'    => 'ids',
         'post_status'  => 'publish',
         'post_parent__not_in'   => [0], // Only get posts with a parent
      ]);
   }
   protected function initRegistrar():void {
      $this->registrar = match ($this->type) {
         'post' => new Posts($this->slug, $this->singular, $this->plural),
@@ -396,8 +468,9 @@
   {
      return $this->integrationConfigs;
   }
   public function hasIntegration(string $integration) {
      return in_array($integration, $this->integrationConfigs);
   public function hasIntegration(string $integration):bool
   {
      return array_key_exists($integration, $this->integrationConfigs);
   }
    public function hasAnyIntegrations(array $integrations = []):bool
    {
@@ -490,6 +563,29 @@
      }
      return $this;
   }
   public function unsetAll(array $flags):self
   {
      $flags = array_filter($flags, function($flag) {
         return in_array($flag, static::$allFlags);
      });
      foreach ($flags as $flag) {
         $this->$flag = false;
         switch ($flag) {
            case 'is_content':
               remove_action('init', [$this, 'setupContent'], 20);
               break;
            case 'is_glossary':
               $this->hide_single = false;
               break;
         }
      }
      return $this;
   }
   public function prefixWith(string $prefix):self
   {
      $this->prefix_with = sanitize_title($prefix);
      return $this;
   }
   public function removeAll(array $flags):self
   {
      $flags = array_filter($flags, function($flag) {
@@ -509,8 +605,20 @@
      }
      return isset($this->$feature) && $this->$feature === true;
   }
   /**
    * @deprecated use withFeature
    * @param string $feature
    * @param string|null $type
    * @return array
    */
   public static function getFeatured(string $feature, ?string $type = null):array
   {
      return self::withFeature($feature, $type);
   }
   public static function withFeature(string $feature, ?string $type = null):array
   {
      self::ensureInstanced();
      if (!in_array($feature, static::$allFlags)) {
@@ -519,13 +627,30 @@
      }
      return array_map(function($inst) { return $inst->slug; },array_filter(self::$instances, function ($inst) use ($feature, $type){
         if (!is_null($type) && $inst->type !== $type) {
         if ((!is_null($type) && $inst->type !== $type) || $inst->system) {
            return false;
         }
         return property_exists($inst, $feature) && isset($inst->$feature) && $inst->$feature === true;
      }));
   }
   public static function withIntegration(string $integration, ?string $type = null):array
   {
      self::ensureInstanced();
      if (!Site::has($integration)) {
         error_log('[Registrar]::withIntegration Integration not available to fetch: '.$integration);
         return [];
      }
      return array_map(function($inst) { return $inst->slug; },array_filter(self::$instances, function ($inst) use ($integration, $type){
         if (!is_null($type) && $inst->type !== $type) {
            return false;
         }
         return array_key_exists($integration, $this->integrationConfigs);
      }));
   }
   public function config(string $config):mixed
   {
      $allowed = ['breadcrumbs','calendar','dashboard','directory','feed','management','has_responses','seo','trackchanges','verification'];
@@ -548,7 +673,7 @@
      protected function getBreadcrumbs():Breadcrumbs
      {
         if (!isset($this->breadcrumbs)) {
            $this->breadcrumbs = new Breadcrumbs($this->slug, $this);
            $this->breadcrumbs = new Breadcrumbs($this->slug);
         }
         return $this->breadcrumbs;
@@ -566,7 +691,7 @@
      protected function getDashboard():Dashboard
      {
         if (!isset($this->dashboard)) {
            $this->dashboard = new Dashboard($this->plural, $this);
            $this->dashboard = new Dashboard($this->plural);
         }
         return $this->dashboard;
@@ -609,11 +734,42 @@
   }
   public function addSection(string $title):Section
   {
      $section = new Section($title, $this);
      $this->sections[] = $section;
      return $section;
      $slug = sanitize_title($title);
      if (!array_key_exists($slug, $this->sections)) {
         $section = new Section($title, $this);
         $this->sections[$slug] = $section;
      }
      return $this->sections[$slug];
   }
   public static function maybeBuildSections():void
   {
      foreach (self::$instances as $inst) {
         $inst->buildSections();
      }
   }
      protected function buildSections():void
      {
         $fields = $this->getFields();
         $sections = array_unique(array_values(array_map(function ($f) {
            return array_key_exists('section', $f) && !is_null($f['section']) ? $f['section'] : 'main';
         }, $fields)));
         foreach ($sections as $s) {
            $section = new Section($s, $this);
            $section->setTitle(ucwords(implode(' ', explode('-', $s))));
            $sectionFields = array_map(function ($f) {
               return $f['name'];
            }, array_filter($fields, function ($f) use ($s) {
               $tmp = array_key_exists('section', $f) && !is_null($f['section']) ? $f['section'] : 'main';
               return $s === $tmp;
            }));
            $section->setFields($sectionFields);
            $this->sections[$s] = $section;
         }
      }
   public function setSectionOrder(array $sections):self
   {
      $allSections = array_map(function($section) {
@@ -661,7 +817,7 @@
            $this->hideSingleHandler = new HideSingle($this->slug, $this);
         }
         if ($this->is_timeline) {
            $this->isTimelineHandler = new MakeTimelineType($this->slug, $this);
            $this->isTimelineHandler = new MakeTimelineType($this->slug);
            $this->registrar->hierarchical = true;
         }
         if ($this->is_calendar) {
@@ -675,6 +831,10 @@
         if ($this->registrar) {
            $this->registrar->register();
         }
         if ($this->add_image_column) {
            add_filter("manage_{$this->based}_posts_columns", [$this, 'addImageColumn']);
            add_action("manage_{$this->based}_posts_custom_column", [$this, 'showImageColumn'], 10, 2);
         }
      } elseif ($this->type === 'term') {
         if ($this->is_content) {
            if ($this->verify_entry) {
@@ -685,13 +845,17 @@
            }
         }
         if ($this->prefix_post_type) {
            $this->addPostTypeRewrites();
         }
         if ($this->registrar) {
            $this->registrar->register();
         }
      }
      if ($this->karma) {
         $this->karmaManager = KarmaManager::for($this->slug);
         $this->karmaManager = KarmaManager::for($this->slug, $this->type);
      }
   }
   public static function getInstance(string $slug):Registrar|false
@@ -719,7 +883,7 @@
   {
      self::ensureInstanced();
      $instances = ($type) ? array_filter(static::$instances, function($instance) use ($type) {
         return $instance->type === $type;
         return $instance->type === $type && !$instance->system;
      }) : static::$instances;
      return array_keys($instances);
   }
@@ -733,19 +897,22 @@
      }, static::$instances);
   }
   public function getCreatable():array
   public function getCreatable(bool $based = false):array
   {
      if ($this->type !== 'user') {
         return [];
      }
      return $this->can_create;
      return $based ? array_map(function ($item) { return jvbCheckBase($item); },$this->can_create) : $this->can_create;
   }
   public function setCreatable(string|array $creatable):self
   {
      $this->can_create = $creatable;
      $this->can_create = is_string($creatable) ? [jvbNoBase($creatable)] : array_map(function ($type) {
            return jvbNoBase($type);
        }, $creatable);
      return $this;
   }
   public function getManageOthers():array
   {
      if ($this->type !== 'user'){
@@ -821,8 +988,8 @@
   }
      public function addTermCreatedMeta(int $termId):void
      {
         $meta = Meta::forTerm($termId);
         $meta->set('date_published', date('Y-m-d H:i:s'));
            update_term_meta($termId, BASE . 'date_published', date('Y-m-d H:i:s'));
            update_term_meta($termId, BASE . 'date_modified', date('Y-m-d H:i:s'));
      }
      public function handleContentTermMetaChange(int $meta_id, int $term_id, string $meta_key, $meta_value):void
      {
@@ -836,8 +1003,14 @@
      }
      public function addTermUpdatedMeta(int $termId):void
      {
         $meta = Meta::forTerm($termId);
         $meta->set('date_modified', date('Y-m-d H:i:s'));
            static $processing = [];
            if (isset($processing[$termId])) return;
            $processing[$termId] = true;
            update_term_meta($termId, BASE . 'date_modified', date('Y-m-d H:i:s'));
            unset($processing[$termId]);
      }
   public function renderContent(string $content, array $block):string
   {
@@ -851,24 +1024,85 @@
         Cache::for($this->slug)->flush();
      }
      $out = Cache::for($this->slug)->remember(
         get_the_ID(),
         function() {
        $per_page = 10;
        $page = $_GET['tp']??1;
            $items = get_terms([
               'taxonomy'  => jvbCheckBase($this->slug),
        $args = apply_filters('jvb_content_tax_args_'.$this->slug, [
            'taxonomy'  => $this->based,
//                'hide_empty' => true,
               'fields' => 'ids',
            'fields' => 'ids',
            'number'     => $per_page,
            'offset'    => ($page - 1) * $per_page,
            'meta_key'  => BASE.'date_modified',
            'meta_type' => 'DATETIME',
            'orderby'   => 'meta_value',
         'order'     => 'desc',
        ]);
        $cache = Cache::for($this->slug)->connect('taxonomy');
      $max = $cache->remember(
         'max',
         function () {
            $max = get_terms([
               'taxonomy'  => $this->based,
               'fields'    => 'ids',
               'number'     => 0,
               'hide_empty'    => true
            ]);
            return  count($max??[]);
         }
      );
        $totalPages = floor($max/$per_page);
        global $wp;
        $current = get_home_url(null, '/'.$wp->request.'/');
        $pages = '';
        for ($i = 1; $i<=$totalPages; $i++) {
            $pages .= (int)$page === $i ?
                sprintf(
                    '<li class="current">%s</li>',
                    $i
                ): sprintf(
                '<li><a href="%s">%s</a></li>',
                add_query_arg('tp', $i, $current),
                $i
            );
        }
        $nav = sprintf(
            '<nav class="pagination">%s<ul>%s</ul>%s</nav>',
            $page > 1 ? '<a href="'.add_query_arg('tp', $page-1, $current).'" title="Next Page" class="btn">'.jvbIcon('arrow-circle-left').'<span class="screen-reader-text">Previous Page</span></a>' : '',
            $pages,
            $page < $totalPages ? '<a href="'.add_query_arg('tp', $page+1, $current).'" title="Next Page" class="btn">'.jvbIcon('arrow-circle-right').'<span class="screen-reader-text">Next Page</span></a>' : '',
        );
      $out = $nav. Cache::for($this->slug)->remember(
         $cache->generateKey(['type' =>'contentArchive', ... $args]),
         function() use ($args) {
            $items = get_terms($args);
            $out = [];
                $method = BASE.'render_'.$this->slug.'_content';
            if ($items && !is_wp_error($items)) {
               foreach ($items as $item) {
                  $meta = Meta::forTerm($item);
               foreach ($items as $termID) {
                  $meta = Meta::forTerm($termID);
                        if (function_exists($method)) {
                            $out[] = $method($termID);
                            continue;
                        }
                  $meta = Meta::forTerm($termID);
                  $slug = sanitize_title($meta->get('name'));
                  $item = sprintf(
                     '<li id="%s"><h2><a href="%s">%s</a></h2><p>%s</p><ul class="item-grid">',
                     '<li id="%s"><h2><a href="%s">%s</a></h2><p>%s</p><ul class="loop scroll">',
                     $slug,
                     get_term_link($item, jvbCheckBase($this->slug))??'',
                     get_term_link($termID, $this->based)??'',
                     $meta->get('name'),
                     $meta->get('description')
                  );
@@ -878,6 +1112,12 @@
                        'post_status'  => 'publish',
                        'posts_per_page'  => 3,
                        'fields' => 'ids',
                                'tax_query' => [
                                    [
                                        'taxonomy'  => $this->based,
                                        'terms'     => $termID
                                    ]
                                ]
                     ]);
                     if ($posts->have_posts()) {
                        while($posts->have_posts()) {
@@ -900,9 +1140,13 @@
                  $out[] = $item;
               }
            }
            return empty($out) ? '' : '<ul class="content-term-list">'.implode('',$out).'</ul>';
                $before = apply_filters(BASE.'before_'.$this->slug.'_content','');
                $out = empty($out) ? '' : '<ul class="content-term-list">'.implode('',$out).'</ul>';
                $after = apply_filters(BASE.'after_'.$this->slug.'_content', '');
                return $before.$out.$after;
         }
      );
      ).$nav;
      error_log('Built the '.$this->slug.' page content.');
      return $content . $out;
   }
@@ -910,8 +1154,8 @@
   public static function ensureInstanced():void
   {
      if (empty(self::$instances)) {
         do_action('jvbDefineRegistrar');
         do_action('jvbDefineRegistrarFields');
         do_action('jvb_define_registrar');
         do_action('jvb_define_fields');
      }
   }
@@ -919,7 +1163,116 @@
    * FLAGGED FEATURES
   *****************************************************************/
   public function profile(?string $slug = null, ?string $singular = null, ?string $plural = null):self
   {
      if (!$slug) {
         $slug = $this->slug.'_profile';
      }
      if (!$singular) {
         $singular = $this->singular;
      }
      if (!$plural) {
         $plural = $this->plural;
      }
      $this->profile_link = true;
      $this->profile = $slug;
      return Registrar::forPost($slug, $singular, $plural);
   }
   public function getProfile():self|false
   {
      if (!$this->profile_link) {
         return false;
      }
      return self::getInstance($this->profile);
   }
    public static function getProfileTypes():array
    {
        $hasProfiles = self::withFeature('profile_link');
        if (empty($hasProfiles)) {
            return [];
        }
        return array_filter(array_map(function($profile) {
            $instance = self::getInstance($profile);
            return $instance->getProfile()->based??false;
        }, $hasProfiles));
    }
   public function setUserSubtype(string $type):self
   {
      $this->user_subtype = sanitize_text_field($type);
      return $this;
   }
   public function getUserSubtype():string|false
   {
      return $this->user_subtype??false;
   }
   public function addImageColumn(array $columns):array
   {
      $keys = array_keys($columns);
      $index = array_search('cb', $keys);
      if ($index !== false) {
         $pos = $index+1;
         $columns = array_slice($columns, 0, $pos, true) + ['jvb_featured_image' => 'Image'] + array_slice($columns, $pos, null, true);
      }
      return $columns;
   }
   public function showImageColumn(string $column, int $postID):void
   {
      if ($column === 'jvb_featured_image') {
         echo get_the_post_thumbnail($postID, 'tiny');
      }
   }
   protected function addPostTypeRewrites():void
   {
      $for = $this->registrar->for;
      foreach ($for as $type) {
         $registrar = Registrar::getInstance($type);
         if ($registrar) {
            $base = $registrar->registrar->rewrite['slug']??$registrar->slug;
            $prefix = empty($this->prefix_with) ? '' : '/'.$this->prefix_with;
            $prefix = str_replace('//', '/', $prefix);
            $slug = str_contains($this->slug, '_') ? str_replace('_','-', $this->slug) : $this->slug;
            add_rewrite_rule(
               $base.$prefix.'/'.$slug.'/([a-z0-9-]+)/?$',
               'index.php?post_type='.$registrar->getBased().'&'.$this->based.'=$matches[1]',
               'top'
            );
            add_rewrite_rule(
               $base.$prefix.'/'.$slug.'/([a-z0-9-]+)/page/([0-9-]+)/?$',
               'index.php?post_type='.$registrar->getBased().'&'.$this->based.'=$matches[1]&paged=$matches[2]',
               'top'
            );
         }
      }
   }
   public function getFeedFields():array
   {
      $config = $this->getConfig('feed');
      $all = $this->getFields();
      $img = $config['images']??['post_thumbnail'];
      $f = $config['fields']??['post_title', 'post_date', 'post_excerpt'];
      $f = array_filter($f, function($field) use ($img) {
         return !in_array($field, $img);
      });
      $images = [];
      $fields = [];
      foreach($img as $i) {
         $images[] = $all[$i];
      }
      foreach ($f as $x) {
         $fields[] = $all[$x];
      }
      return [$images,$fields];
   }
}