Jake Vanderwerf
3 days ago ba1e1ccf869b818f7a7a897264dfea05563a7796
inc/blocks/FAQBlock.php
@@ -2,11 +2,16 @@
namespace JVBase\blocks;
use JVBase\managers\Cache;
use JVBase\registrar\Registrar;
use WP_Block;
use WP_Query;
class FAQBlock {
   protected Cache $cache;
   protected string $postType;
   protected string $section;
   protected string $slug;
   protected bool $isDirectory;
   public function __construct()
   {
      $this->cache = Cache::for('faq_block', WEEK_IN_SECONDS)->connect('post', true)->connect('taxonomy', true);
@@ -25,12 +30,14 @@
            'render_callback' => [$this, 'render'],
         ]
      );
//
//    // Localize script data for the editor
//    add_action('enqueue_block_editor_assets', [$this, 'enqueue_editor_assets']);
//
//    // Enqueue frontend scripts
//    add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_assets']);
      $faq = array_values(Registrar::withFeature('is_faq','post'));
      $registrar = Registrar::getInstance($faq[0]);
      $this->section = array_map('jvbCheckBase', $registrar->registrar->taxonomies)[0];
      $this->postType = $registrar->getBased();
      $this->slug = $registrar->getSlug();
      $this->isDirectory = $registrar->hasFeature('show_directory');
   }
   /**
@@ -70,12 +77,11 @@
   public function localizeData(): void
   {
      // Get all sections
      $section_taxonomy = BASE . 'section';
      $sections_data = $this->cache->remember(
         'sections',
         function() {
            $sections = get_terms([
               'taxonomy' => BASE.'section',
               'taxonomy' => $this->section,
               'hide_empty' => false,
               'orderby' => 'name',
               'order' => 'ASC',
@@ -102,8 +108,8 @@
         'jvb-faq-editor-script',
         'jvbFaq',
         [
            'sectionTaxonomy' => $section_taxonomy,
            'faqPostType' => BASE . 'faq',
            'sectionTaxonomy' => $this->section,
            'faqPostType' => $this->postType,
            'sections' => $sections_data,
         ]
      );
@@ -129,10 +135,6 @@
       * @param string   $content    Block content
       * @param WP_Block $block      Block instance
       */
// Get BASE constant
      $base = defined('BASE') ? BASE : '';
      $faq_post_type = $base . 'faq';
      $section_taxonomy = $base . 'section';
// Get block attributes
      $section_order = $attributes['sectionOrder'] ?? [];
@@ -140,7 +142,7 @@
      $collapse_by_default = $attributes['collapseByDefault'] ?? false;
// Determine if we're on a taxonomy archive or main FAQ archive
      $is_tax_archive = is_tax($section_taxonomy);
      $is_tax_archive = is_tax($this->section);
      $current_term = null;
      if ($is_tax_archive) {
@@ -150,7 +152,7 @@
      } else {
         // Build query args based on context
         $query_args = [
            'post_type' => $faq_post_type,
            'post_type' => $this->postType,
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'orderby' => 'menu_order title',
@@ -162,10 +164,10 @@
      if (!$faq_query->have_posts()) {
         echo '<div class="faq-block faq-block--empty">';
         echo '<p>' . esc_html__('No FAQs found.', 'jvb') . '</p>';
         echo '</div>';
         return;
         return sprintf(
            '<div class="faq-block empty"><p>%s</p></div>',
            esc_html__('No FAQs found.', 'jvb')
         );
      }
      // Organize FAQs by section
@@ -175,7 +177,7 @@
         $faq_query->the_post();
         $post_id = get_the_ID();
         $terms = get_the_terms($post_id, $section_taxonomy);
         $terms = get_the_terms($post_id, $this->section);
         if ($terms && !is_wp_error($terms)) {
            foreach ($terms as $term) {
@@ -251,14 +253,25 @@
      if (!empty($section_order)) {
         $nav = '<nav id="faq"><h2>Sections:</h2><ol>';
         foreach ($section_order as $term_id) {
            $term = get_term($term_id, $section_taxonomy);
            $term = get_term($term_id, $this->section);
            if ($term && !is_wp_error($term)) {
               $url = (!$is_tax_archive) ? "#{$term->slug}" : get_term_link($term);
               $nav .= '<li><a href="'.$url.'">'.html_entity_decode($term->name).'</a></li>';
            }
         }
         $seeAll = ($is_tax_archive) ? '<p><a href="'.get_post_type_archive_link(BASE.'faq').'">'.__('See All FAQs', 'jvb').'</a></p>' : '';
         $nav .= '</ol>'.$seeAll.'</nav>';
         $buttons = '';
         if ($this->isDirectory) {
            $directory = JVB()->directories()->directories($this->slug);
            $url = $directory['url']??'';
            if (!empty($url)) {
               $buttons .= '<li><a href="'.$url.'">'.__('See Alphabetical List', 'jvb').'</a></li>';
            }
         }
         if (is_tax()) {
            $buttons .= '<li><a href="'.get_home_url(null, '/faq/').'">'.__('See All FAQs', 'jvb').'</a></li>';
         }
         $buttons = empty($buttons) ? '' : '<ul class="buttons">'.$buttons.'</ul>';
         $nav .= '</ol>'.$buttons.'</nav>';
      }
      ?>
@@ -280,7 +293,7 @@
                     ?>
                  <details class="faq"<?= !$collapse_by_default ? ' open' : '' ?>>
                     <summary><h3><b>Q</b> <?= esc_html($faq['title']) ?></h3></summary>
                     <?= apply_filters('the_content', $faq['content']) ?>
                     <?= jvb_filter_content( $faq['content']) ?>
                     <a class="button" href="<?= $faq['url'] ?>" title="Learn More about <?=$faq['title']?>">Learn More</a>
                  </details>
                  <?php endforeach; ?>