Jake Vanderwerf
7 days ago 46d681c6b825d21b3f698d793c4e630c687d90ad
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,26 @@
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 = new CacheManager('summary', WEEK_IN_SECONDS);
        $this->cache = Cache::for('summary_block', WEEK_IN_SECONDS);
        add_action('init', [ $this, 'registerBlock' ]);
      add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']);
      if (JVB_TESTING) {
         $this->cache->flush();
      }
    }
    public function registerBlock()
@@ -34,6 +40,11 @@
        ]);
    }
   public function enqueueScripts():void
   {
      wp_enqueue_script('jvb-page-nav');
   }
    protected function getConfig():string
    {
        return (is_tax()) ? 'tax' : 'content';
@@ -55,7 +66,7 @@
        $this->config = $this->getConfig();
        $key = $this->generateKey();
        $cache = $this->cache->get($key);
        $cache = false;
        if ($cache) {
            return $cache;
        }
@@ -66,7 +77,7 @@
        );
        /**
         * The h1 title element, excluding the h1 tag.
         * Can include additonal html (like <small> or <b>)
         * Can include additional html (like <small> or <b>)
         */
        $this->header = apply_filters(
            'jvbSummaryHeader',
@@ -83,6 +94,11 @@
        );
        $this->headerExtra = ($headerExtra === '') ? '' : '<div>'.$headerExtra.'</div>';
      $this->beforeSummary = apply_filters(
         'jvbBeforeSummary',
         '',
         $this->getType()
      );
        /**
         * The HTML string that appears within the <summary> block
         */
@@ -92,6 +108,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)
@@ -105,8 +127,6 @@
            $this->getType()
        );
        $this->icons = new JVBICons();
        ob_start();
        $this->renderBlock();
        $content = ob_get_clean();
@@ -138,6 +158,9 @@
            <?php endif; ?>
        </header>
        <?php
      if (!empty($this->beforeSummary)) {
         echo $this->beforeSummary;
      }
    }
    protected function renderDetails():void
@@ -148,9 +171,10 @@
        if (empty($details)) {
            return;
        }
      $open = $this->isOpen ? ' open' : '';
        ?>
        <details class="info">
            <summary class="row btw"><?= $this->detailsTitle ?></summary>
        <details class="info"<?=$open?>>
            <summary class="row x-btw"><?= $this->detailsTitle ?></summary>
            <?php
            foreach ($this->details as $key => $details) {
                if ($details === '') {
@@ -172,7 +196,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
@@ -180,7 +205,7 @@
        if (empty($this->type)) {
            $this->type = match (true) {
                is_tax()                => jvbNoBase(get_queried_object()->taxonomy),
                is_post_type_archive()  => jvbNoBase(get_post_type()),
                is_post_type_archive()  => jvbNoBase(get_queried_object()->name),
                default                 => jvbNoBase(get_queried_object()->post_type),
            };
        }