| | |
| | | namespace JVBase\blocks; |
| | | |
| | | use JVBase\managers\Cache; |
| | | use JVBase\forms\TaxonomySelector; |
| | | use JVBase\meta\MetaManager; |
| | | 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); |
| | |
| | | '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'); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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', |
| | |
| | | 'jvb-faq-editor-script', |
| | | 'jvbFaq', |
| | | [ |
| | | 'sectionTaxonomy' => $section_taxonomy, |
| | | 'faqPostType' => BASE . 'faq', |
| | | 'sectionTaxonomy' => $this->section, |
| | | 'faqPostType' => $this->postType, |
| | | 'sections' => $sections_data, |
| | | ] |
| | | ); |
| | |
| | | * @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'] ?? []; |
| | |
| | | $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) { |
| | |
| | | } 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', |
| | |
| | | |
| | | |
| | | 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 |
| | |
| | | $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) { |
| | |
| | | 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>'; |
| | | } |
| | | ?> |
| | | |
| | |
| | | ?> |
| | | <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; ?> |