get_post_data( $post_id ); if ( empty( $post_data ) ) { return array( '@type' => $this->type ); } $schema = array( '@type' => $this->type, '@id' => Schema_AI_ID_Builder::faq_page( $post_id ), 'name' => $post_data['title'], ); // Build mainEntity from AI fields or content extraction. $questions = $this->extract_questions( $post_data['content'], $fields ); if ( ! empty( $questions ) ) { $schema['mainEntity'] = $questions; } return $schema; } /** * Extract Question/Answer pairs. * * Priority: AI-detected fields > FAQ blocks > heading/paragraph pairs. * * @param string $content Post content HTML. * @param array $fields AI-detected fields. * @return array Array of Question schema objects. */ private function extract_questions( $content, $fields ) { // 1. AI-detected FAQ data. if ( ! empty( $fields['mainEntity'] ) && is_array( $fields['mainEntity'] ) ) { return $this->format_qa_pairs( $fields['mainEntity'] ); } // 2. Look for FAQ-style Q&A in fields. if ( ! empty( $fields['questions'] ) && is_array( $fields['questions'] ) ) { return $this->format_qa_pairs( $fields['questions'] ); } // 3. Extract from content headings and following paragraphs. return $this->extract_from_content( $content ); } /** * Format raw Q&A data into schema.org Question objects. * * @param array $pairs Array of question/answer pairs. * @return array Array of Question schema objects. */ private function format_qa_pairs( $pairs ) { $questions = array(); foreach ( $pairs as $pair ) { if ( is_array( $pair ) ) { // Already formatted as schema. if ( isset( $pair['@type'] ) && 'Question' === $pair['@type'] ) { $questions[] = $pair; continue; } // Raw pair with question/answer keys. $question_text = isset( $pair['question'] ) ? $pair['question'] : ( isset( $pair['name'] ) ? $pair['name'] : '' ); $answer_text = isset( $pair['answer'] ) ? $pair['answer'] : ( isset( $pair['acceptedAnswer'] ) ? $pair['acceptedAnswer'] : '' ); if ( is_array( $answer_text ) && isset( $answer_text['text'] ) ) { $answer_text = $answer_text['text']; } if ( ! empty( $question_text ) && ! empty( $answer_text ) ) { $questions[] = array( '@type' => 'Question', 'name' => $question_text, 'acceptedAnswer' => array( '@type' => 'Answer', 'text' => $answer_text, ), ); } } } return $questions; } /** * Extract FAQ pairs from content by matching headings with following paragraphs, * plus accordion/collapsible patterns (Gutenberg details, Elementor, etc.). * * @param string $content Post content HTML. * @return array Array of Question schema objects. */ private function extract_from_content( $content ) { $questions = array(); // ── 1. Heading-based extraction ── $pattern = '/]*>(.*?)<\/h\1>/is'; if ( preg_match_all( $pattern, $content, $heading_matches, PREG_OFFSET_CAPTURE ) ) { $total_headings = count( $heading_matches[0] ); for ( $i = 0; $i < $total_headings; $i++ ) { $heading_text = wp_strip_all_tags( $heading_matches[2][ $i ][0] ); $heading_text = trim( $heading_text ); if ( ! $this->is_question( $heading_text ) ) { continue; } $start = $heading_matches[0][ $i ][1] + strlen( $heading_matches[0][ $i ][0] ); $end = ( $i + 1 < $total_headings ) ? $heading_matches[0][ $i + 1 ][1] : strlen( $content ); $between = substr( $content, $start, $end - $start ); $answer = ''; if ( preg_match_all( '/]*>(.*?)<\/p>/is', $between, $p_matches ) ) { $paragraphs = array(); foreach ( $p_matches[1] as $p ) { $clean = wp_strip_all_tags( $p ); $clean = trim( $clean ); if ( ! empty( $clean ) ) { $paragraphs[] = $clean; } } $answer = implode( ' ', $paragraphs ); } if ( ! empty( $answer ) ) { $questions[] = array( '@type' => 'Question', 'name' => $heading_text, 'acceptedAnswer' => array( '@type' => 'Answer', 'text' => $answer, ), ); } } } // ── 2. Accordion / collapsible detection ── if ( class_exists( 'Schema_AI_Accordion_Detector' ) ) { // We need the post ID to detect Elementor data, but extract_from_content // only receives HTML. Use the global post as fallback. $post_id = 0; $post = get_post(); if ( $post ) { $post_id = $post->ID; } if ( $post_id > 0 ) { $detector = new Schema_AI_Accordion_Detector(); $accordion_pairs = $detector->detect( $post_id ); foreach ( $accordion_pairs as $pair ) { $questions[] = array( '@type' => 'Question', 'name' => $pair['question'], 'acceptedAnswer' => array( '@type' => 'Answer', 'text' => $pair['answer'], ), ); } } } // ── 3. Deduplicate by question text ── $seen = array(); $unique = array(); foreach ( $questions as $q ) { $key = strtolower( trim( $q['name'] ) ); if ( ! isset( $seen[ $key ] ) ) { $seen[ $key ] = true; $unique[] = $q; } } return $unique; } /** * Determine if a heading text looks like a question. * * @param string $text The heading text. * @return bool True if the text appears to be a question. */ private function is_question( $text ) { // Contains a question mark. if ( false !== strpos( $text, '?' ) ) { return true; } // Starts with common question words. $question_words = array( 'what', 'how', 'why', 'when', 'where', 'who', 'which', 'can', 'do', 'does', 'is', 'are', 'will', 'should' ); $lower = strtolower( $text ); foreach ( $question_words as $word ) { if ( 0 === strpos( $lower, $word . ' ' ) ) { return true; } } return false; } }