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(); ?> $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 '
' . esc_html__('No FAQs found.', 'jvb') . '
'; echo '