* elements, and definition lists. * * @package Schema_AI * @subpackage Schema_AI/includes/extraction * @since 1.1.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; } class Schema_AI_Accordion_Detector { /** * Detect FAQ pairs from accordion/collapsible patterns in post content. * * Runs all sub-detectors and returns merged, deduplicated results. * * @param int $post_id WordPress post ID. * @return array Array of [ 'question' => string, 'answer' => string ] pairs. */ public function detect( $post_id ) { $post = get_post( $post_id ); if ( ! $post ) { return array(); } $raw_content = $post->post_content; $pairs = array(); // 1. Gutenberg details blocks. $pairs = array_merge( $pairs, $this->detect_gutenberg_details( $raw_content ) ); // 2. Generic HTML
tags (rendered content). $rendered = do_shortcode( do_blocks( $raw_content ) ); $pairs = array_merge( $pairs, $this->detect_html_details( $rendered ) ); // 3. Elementor accordion / toggle widgets. $pairs = array_merge( $pairs, $this->detect_elementor_accordion( $post_id ) ); // 4. Definition lists used as FAQ. $pairs = array_merge( $pairs, $this->detect_definition_lists( $rendered ) ); return $this->deduplicate( $pairs ); } /** * Detect Gutenberg `` blocks. * * Each block has a (question) and remaining content (answer). * * @param string $raw_content Raw post_content with block comments. * @return array FAQ pairs. */ private function detect_gutenberg_details( $raw_content ) { $pairs = array(); if ( false === strpos( $raw_content, 'wp:details' ) ) { return $pairs; } // Match wp:details blocks — they render to
……
. $rendered = do_blocks( $raw_content ); // The rendered output contains
tags which we parse in detect_html_details. // This method is intentionally a no-op to avoid double-counting — the rendered //
tags are caught by detect_html_details(). return $pairs; } /** * Detect generic HTML
elements. * * Works with any source that renders to standard HTML details/summary. * * @param string $html Rendered HTML content. * @return array FAQ pairs. */ private function detect_html_details( $html ) { $pairs = array(); if ( false === stripos( $html, '...QA...
. $pattern = '/]*>\s*]*>(.*?)<\/summary>(.*?)<\/details>/is'; if ( preg_match_all( $pattern, $html, $matches ) ) { for ( $i = 0; $i < count( $matches[0] ); $i++ ) { $question = trim( wp_strip_all_tags( $matches[1][ $i ] ) ); $answer = trim( wp_strip_all_tags( $matches[2][ $i ] ) ); if ( ! empty( $question ) && ! empty( $answer ) ) { $pairs[] = array( 'question' => $question, 'answer' => $answer, ); } } } return $pairs; } /** * Detect Elementor accordion and toggle widgets. * * Parses the `_elementor_data` post meta for accordion/toggle widget data. * * @param int $post_id WordPress post ID. * @return array FAQ pairs. */ private function detect_elementor_accordion( $post_id ) { $pairs = array(); $elementor_data = get_post_meta( $post_id, '_elementor_data', true ); if ( empty( $elementor_data ) ) { return $pairs; } $data = json_decode( $elementor_data, true ); if ( ! is_array( $data ) ) { return $pairs; } $this->walk_elementor_elements( $data, $pairs ); return $pairs; } /** * Recursively walk Elementor element tree to find accordion/toggle widgets. * * @param array $elements Elementor elements array. * @param array $pairs Reference to FAQ pairs array. */ private function walk_elementor_elements( $elements, &$pairs ) { foreach ( $elements as $element ) { if ( ! is_array( $element ) ) { continue; } $widget_type = isset( $element['widgetType'] ) ? $element['widgetType'] : ''; if ( in_array( $widget_type, array( 'accordion', 'toggle' ), true ) ) { $settings = isset( $element['settings'] ) ? $element['settings'] : array(); $tabs = isset( $settings['tabs'] ) ? $settings['tabs'] : array(); foreach ( $tabs as $tab ) { $title = isset( $tab['tab_title'] ) ? trim( wp_strip_all_tags( $tab['tab_title'] ) ) : ''; $content = isset( $tab['tab_content'] ) ? trim( wp_strip_all_tags( $tab['tab_content'] ) ) : ''; if ( ! empty( $title ) && ! empty( $content ) ) { $pairs[] = array( 'question' => $title, 'answer' => $content, ); } } } // Recurse into child elements. if ( ! empty( $element['elements'] ) && is_array( $element['elements'] ) ) { $this->walk_elementor_elements( $element['elements'], $pairs ); } } } /** * Detect definition lists (
...
...) used as FAQ. * * @param string $html Rendered HTML content. * @return array FAQ pairs. */ private function detect_definition_lists( $html ) { $pairs = array(); if ( false === stripos( $html, ' blocks and extract dt/dd pairs. $dl_pattern = '/]*>(.*?)<\/dl>/is'; if ( ! preg_match_all( $dl_pattern, $html, $dl_matches ) ) { return $pairs; } foreach ( $dl_matches[1] as $dl_content ) { // Match consecutive dt/dd pairs. $dt_dd_pattern = '/]*>(.*?)<\/dt>\s*]*>(.*?)<\/dd>/is'; if ( preg_match_all( $dt_dd_pattern, $dl_content, $dd_matches ) ) { for ( $i = 0; $i < count( $dd_matches[0] ); $i++ ) { $question = trim( wp_strip_all_tags( $dd_matches[1][ $i ] ) ); $answer = trim( wp_strip_all_tags( $dd_matches[2][ $i ] ) ); if ( ! empty( $question ) && ! empty( $answer ) ) { $pairs[] = array( 'question' => $question, 'answer' => $answer, ); } } } } return $pairs; } /** * Deduplicate FAQ pairs by question text (case-insensitive). * * @param array $pairs Array of FAQ pairs. * @return array Deduplicated pairs. */ private function deduplicate( $pairs ) { $seen = array(); $unique = array(); foreach ( $pairs as $pair ) { $key = strtolower( trim( $pair['question'] ) ); if ( ! isset( $seen[ $key ] ) ) { $seen[ $key ] = true; $unique[] = $pair; } } return $unique; } }