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'], ] ); $faq = array_values(Registrar::getFeatured('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'); } /** * 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 $sections_data = $this->cache->remember( 'sections', function() { $sections = get_terms([ 'taxonomy' => $this->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' => $this->section, 'faqPostType' => $this->postType, '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(); ?> section); $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' => $this->postType, '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()) { return sprintf( '
%s