Jake Vanderwerf
2026-02-17 a24a06002081ad71a78ffeff9072725ba39cf121
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
namespace JVBase\blocks;
 
use JVBase\managers\Cache;
use WP_Block;
use WP_Query;
 
class FAQBlock {
    protected Cache $cache;
    public function __construct()
    {
        $this->cache = Cache::for('faq_block', WEEK_IN_SECONDS)->connect('post', true)->connect('taxonomy', true);
        add_action('init', [ $this, 'registerBlock' ]);
        add_action('enqueue_block_editor_assets', [$this, 'localizeData']);
    }
 
    /**
     * Register the FAQ block
     */
    public function registerBlock() {
        // Register the block
        register_block_type(
            JVB_DIR . '/build/faq',
            [
                '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']);
    }
 
    /**
     * Enqueue editor assets
     */
    public static function enqueue_editor_assets() {
        // Pass section taxonomy name to JavaScript
        wp_localize_script(
            'jvb-faq-editor-script',
            'jvbFaq',
            [
                'sectionTaxonomy' => BASE . 'section',
                'faqPostType' => BASE . 'faq',
            ]
        );
    }
 
    /**
     * Enqueue frontend assets
     */
    public static function enqueue_frontend_assets() {
        // Check if block is being used on this page
        if (has_block('jvb/faq')) {
            wp_enqueue_script(
                'jvb-faq-view',
                JVB_URL . '/build/faq/view.js',
                [],
                filemtime(JVB_DIR . '/build/faq/view.js'),
                true
            );
        }
    }
 
    /**
     * Localize forms data for block editor
     */
    public function localizeData(): void
    {
        // Get all sections
        $section_taxonomy = BASE . 'section';
        $sections_data = $this->cache->remember(
            'sections',
            function() {
                $sections = get_terms([
                    'taxonomy' => BASE.'section',
                    'hide_empty' => false,
                    'orderby' => 'name',
                    'order' => 'ASC',
                ]);
 
                // Format sections for JavaScript
                $sections_data = [];
                if (!is_wp_error($sections) && !empty($sections)) {
                    foreach ($sections as $term) {
                        $sections_data[] = [
                            'id' => $term->term_id,
                            'name' => html_entity_decode($term->name),
                            'slug' => $term->slug,
                        ];
                    }
                }
                return $sections_data;
            }
        );
 
 
        // Pass section taxonomy name and sections to JavaScript
        wp_localize_script(
            'jvb-faq-editor-script',
            'jvbFaq',
            [
                'sectionTaxonomy' => $section_taxonomy,
                'faqPostType' => BASE . 'faq',
                'sections' => $sections_data,
            ]
        );
    }
 
    /**
     * Render callback
     *
     * @param array $attributes Block attributes
     * @param string $content Block content
     * @param WP_Block $block Block instance
     * @return string Rendered block HTML
     */
    public function render($attributes, $content, $block)
    {
        ob_start();
        ?>
        <?php
        /**
         * FAQ Block Template
         *
         * @param array    $attributes Block attributes
         * @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'] ?? [];
        $show_section_titles = $attributes['showSectionTitles'] ?? true;
        $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);
        $current_term = null;
 
        if ($is_tax_archive) {
            $current_term = get_queried_object();
            global $wp_query;
            $faq_query = $wp_query;
        } else {
            // Build query args based on context
            $query_args = [
                'post_type' => $faq_post_type,
                'posts_per_page' => -1,
                'post_status' => 'publish',
                'orderby' => 'menu_order title',
                'order' => 'ASC',
            ];
            // Get FAQs
            $faq_query = new WP_Query($query_args);
        }
 
 
        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;
        }
 
        // Organize FAQs by section
        $faqs_by_section = [];
 
        while ($faq_query->have_posts()) {
            $faq_query->the_post();
            $post_id = get_the_ID();
 
            $terms = get_the_terms($post_id, $section_taxonomy);
 
            if ($terms && !is_wp_error($terms)) {
                foreach ($terms as $term) {
                    if (!isset($faqs_by_section[$term->term_id])) {
                        $faqs_by_section[$term->term_id] = [
                            'term' => $term,
                            'faqs' => [],
                        ];
                    }
 
                    $faqs_by_section[$term->term_id]['faqs'][] = [
                        'id'        => $post_id,
                        'title'     => get_the_title(),
                        'content'   => get_the_excerpt(),
                        'url'       => get_the_permalink(),
                    ];
                }
            } else {
                // FAQ without section - add to "uncategorized"
                if (!isset($faqs_by_section[0])) {
                    $faqs_by_section[0] = [
                        'term' => (object) [
                            'term_id' => 0,
                            'name' => __('General', 'jvb'),
                            'slug' => 'general',
                        ],
                        'faqs' => [],
                    ];
                }
 
                $faqs_by_section[0]['faqs'][] = [
                    'id'        => $post_id,
                    'title'     => get_the_title(),
                    'content'   => get_the_excerpt(),
                    'url'       => get_the_permalink(),
                ];
            }
        }
 
        wp_reset_postdata();
 
        // If on main FAQ archive and we have a custom order, apply it
        if (!$is_tax_archive && !empty($section_order)) {
            $ordered_sections = [];
 
            // Add sections in custom order
            foreach ($section_order as $term_id) {
                if (isset($faqs_by_section[$term_id])) {
                    $ordered_sections[$term_id] = $faqs_by_section[$term_id];
                }
            }
 
            // Add any sections not in the custom order at the end
            foreach ($faqs_by_section as $term_id => $section_data) {
                if (!isset($ordered_sections[$term_id])) {
                    $ordered_sections[$term_id] = $section_data;
                }
            }
 
            $faqs_by_section = $ordered_sections;
        }
 
// Generate unique IDs for accordion functionality
        $block_id = 'faq-block-' . wp_unique_id();
 
// Render the block
        $wrapper_attributes = get_block_wrapper_attributes([
            'class' => 'faq-block',
            'data-block-id' => $block_id,
        ]);
 
        $nav = '';
        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);
                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>';
        }
        ?>
 
        <section <?php echo $wrapper_attributes; ?>>
            <?= $nav ?>
            <?php foreach ($faqs_by_section as $term_id => $section_data): ?>
                <div id="<?= $section_data['term']->slug?>" class="faq-section" data-section-id="<?php echo esc_attr($term_id); ?>">
                    <?php if ($show_section_titles): ?>
                        <h2>
                            <?php echo esc_html($section_data['term']->name); ?>
                        </h2>
                    <?php endif; ?>
 
                    <div class="faq-list">
                        <?php foreach ($section_data['faqs'] as $index => $faq): ?>
                            <?php
                            $faq_id = $block_id . '-faq-' . $faq['id'];
                            $is_expanded = !$collapse_by_default;
                            ?>
                        <details class="faq"<?= !$collapse_by_default ? ' open' : '' ?>>
                            <summary><h3><b>Q</b> <?= esc_html($faq['title']) ?></h3></summary>
                            <?= jvb_filter_content( $faq['content']) ?>
                            <a class="button" href="<?= $faq['url'] ?>" title="Learn More about <?=$faq['title']?>">Learn More</a>
                        </details>
                        <?php endforeach; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </section>
 
        <?php
        return ob_get_clean();
    }
}