Jake Vanderwerf
2026-08-26 ccf5e61a075c35d24befc175994b5cb8957ca322
inc/blocks/SummaryBlock.php
@@ -1,7 +1,7 @@
<?php
namespace JVBase\blocks;
use JVBase\managers\CacheManager;
use JVBase\managers\Cache;
use JVBase\forms\TaxonomySelector;
use WP_Block;
@@ -11,20 +11,24 @@
class SummaryBlock
{
    protected CacheManager $cache;
    protected Cache $cache;
    protected string $config;
    protected string $type;
    protected string $path = JVB_DIR . '/build/summary';
    protected string $image;
    protected string $header;
    protected string $headerExtra;
   protected bool $isOpen = false;
    protected string $beforeSummary;
    protected string $detailsTitle;
    protected array $details;
    public function __construct()
    {
        $this->cache = CacheManager::for('summary_block', WEEK_IN_SECONDS);
        $this->cache = Cache::for('summary_block', 'post', WEEK_IN_SECONDS);
        add_action('init', [ $this, 'registerBlock' ]);
      add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']);
    }
    public function registerBlock()
@@ -34,28 +38,29 @@
        ]);
    }
   public function enqueueScripts():void
   {
      wp_enqueue_script('jvb-page-nav');
   }
    protected function getConfig():string
    {
        return (is_tax()) ? 'tax' : 'content';
    }
    protected function generateKey():string
    {
        $obj = get_queried_object();
        return match (true) {
            is_tax() => jvbNoBase($obj->taxonomy) . $obj->term_id,
            is_a($obj, 'WP_Post_type') => jvbNoBase($obj->name),
            is_a($obj, 'WP_User') => jvbUserRole($obj->ID),
            default => jvbNoBase($obj->post_type) . $obj->ID,
        };
    }
    public function render(array $attributes, string $content, WP_Block $block)
    {
        $this->config = $this->getConfig();
        $key = $this->generateKey();
      $this->cache->clear();
        $cache = $this->cache->get($key);
      if (is_tax()) {
         $this->cache = Cache::for('summary_tax', 'term', WEEK_IN_SECONDS);
      } elseif (is_author()) {
         $this->cache = Cache::for('summary_auth', 'user', WEEK_IN_SECONDS);
      } else {
         $this->cache = Cache::for('summary_post', 'post', WEEK_IN_SECONDS);
      }
        $cache = $this->cache->get(get_queried_object_id());
        if ($cache) {
            return $cache;
@@ -84,6 +89,11 @@
        );
        $this->headerExtra = ($headerExtra === '') ? '' : '<div>'.$headerExtra.'</div>';
      $this->beforeSummary = apply_filters(
         'jvbBeforeSummary',
         '',
         $this->getType()
      );
        /**
         * The HTML string that appears within the <summary> block
         */
@@ -93,6 +103,12 @@
            $this->getType()
        );
      $this->isOpen = apply_filters(
         'jvbSummaryIsOpen',
         false,
         $this->getType()
      );
        /**
         * The content of the <details> block.
         * This should return either an empty array for no information (so no details block)
@@ -109,7 +125,7 @@
        ob_start();
        $this->renderBlock();
        $content = ob_get_clean();
        $this->cache->set($key, $content);
        $this->cache->set(get_queried_object_id(), $content);
        return $content;
    }
@@ -137,6 +153,9 @@
            <?php endif; ?>
        </header>
        <?php
      if (!empty($this->beforeSummary)) {
         echo $this->beforeSummary;
      }
    }
    protected function renderDetails():void
@@ -147,9 +166,10 @@
        if (empty($details)) {
            return;
        }
      $open = $this->isOpen ? ' open' : '';
        ?>
        <details class="info">
            <summary class="row btw"><?= $this->detailsTitle ?></summary>
        <section><details class="info"<?=$open?>>
            <summary class="row x-btw"><?= $this->detailsTitle ?></summary>
            <?php
            foreach ($this->details as $key => $details) {
                if ($details === '') {
@@ -162,7 +182,7 @@
            <?php
            }
            ?>
        </details>
        </details></section>
        <?php
    }
@@ -171,7 +191,8 @@
        if (empty($this->details)) {
            return;
        }
        echo jvbOnThisPage(array_keys($this->details));
      $IDs = apply_filters('jvbSummaryOnThisPage', array_keys($this->details), $this->getType());
        echo jvbOnThisPage($IDs);
    }
    protected function getType():string