]*>([^<]*\?)\s*<\/h[2-4]>\s*(]*>.*?<\/p>(?:\s*
]*>.*?<\/p>)*)/si',
$content, $matches, PREG_SET_ORDER
) ) {
foreach ( $matches as $m ) {
$pairs[] = $this->build_faq_pair( $m[1], $m[2] );
}
if ( ! empty( $pairs ) ) {
return $pairs;
}
}
// ---------------------------------------------------------------
// 8. Text-based Q:/A: or Question:/Answer: patterns (fallback)
// ---------------------------------------------------------------
$stripped = wp_strip_all_tags( $content );
if ( preg_match_all(
'/(?:Q:|Question:)\s*(.+?)\n+(?:A:|Answer:)\s*(.+?)(?=\n\s*(?:Q:|Question:)|\n\n\n|$)/si',
$stripped, $matches, PREG_SET_ORDER
) ) {
foreach ( $matches as $m ) {
$pairs[] = $this->build_faq_pair( $m[1], $m[2] );
}
}
return $pairs;
}
/**
* Build a single FAQ schema pair (Question + AcceptedAnswer).
*
* @param string $question Raw question text (may contain HTML).
* @param string $answer Raw answer text (may contain HTML).
* @return array Schema.org Question entity.
*/
private function build_faq_pair( $question, $answer ) {
return array(
'@type' => 'Question',
'name' => wp_strip_all_tags( trim( $question ) ),
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => wp_strip_all_tags( trim( $answer ) ),
),
);
}
// -----------------------------------------------------------------------
// Detection Helpers
// -----------------------------------------------------------------------
/**
* Check if auto-detected schema would fire for this post.
*
* @param \WP_Post $post Post.
* @return bool
*/
private function has_auto_schema( $post, $schemas = null ) {
if ( null === $schemas ) {
$schema_engine = new Schema();
$result = $schema_engine->get_for_post( $post->ID );
$schemas = (array) ( $result['schemas'] ?? array() );
}
if ( empty( $schemas ) ) {
return false;
}
// Breadcrumb alone doesn't count as meaningful schema coverage.
foreach ( $schemas as $schema ) {
$type = isset( $schema['@type'] ) ? $schema['@type'] : '';
if ( $type && 'BreadcrumbList' !== $type ) {
return true;
}
}
return false;
}
/**
* Detect FAQ-style content across all common WordPress patterns.
*
* Checks for: Yoast/Rank Math FAQ blocks, shortcodes, HTML5 details,
* definition lists, accordion markup, question headings, and text Q/A.
*
* @param string $content Post content (raw post_content).
* @return bool
*/
private function has_faq_content( $content ) {
if ( class_exists( __NAMESPACE__ . '\\FaqParser' ) ) {
$own_faq = FaqParser::parse_aeogm( $content );
if ( ! empty( $own_faq['pairs'] ) ) {
return true;
}
}
// Gutenberg FAQ blocks (Yoast, Rank Math).
if ( false !== strpos( $content, 'wp:yoast/faq-block' ) ) {
return true;
}
if ( false !== strpos( $content, 'wp:rank-math/faq-block' ) ) {
return true;
}
// FAQ shortcodes, including AEO God Mode's own accordion contract.
if ( preg_match( '/\[(?:aeogm_faqs?|faq|accordion|toggle|question|answer)\b/i', $content ) ) {
return true;
}
// Schema FAQ classes from Yoast / Rank Math.
if ( false !== strpos( $content, 'schema-faq-question' ) ) {
return true;
}
if ( false !== strpos( $content, 'rank-math-question' ) ) {
return true;
}
// HTML5 details/summary (native accordion).
if ( false !== strpos( $content, ']*>[^<]*\?<\/dt>/i', $content ) ) {
return true;
}
}
// Heading tags ending in ? (strong signal of FAQ content).
if ( preg_match( '/]*>[^<]*\?\s*<\/h[2-4]>/i', $content ) ) {
// Count how many question headings. Single one could be anything,
// but 2+ strongly suggests an FAQ section.
preg_match_all( '/]*>[^<]*\?\s*<\/h[2-4]>/i', $content, $qh );
if ( count( $qh[0] ) >= 2 ) {
return true;
}
}
// Text-based patterns: Q:, Question:, FAQ heading, "Frequently Asked".
if ( preg_match( '/(?:^|\n)\s*(?:Q\d*[\.:]\s|Question\s*\d*[\.:]\s)/mi', wp_strip_all_tags( $content ) ) ) {
return true;
}
// Section heading containing "FAQ" or "Frequently Asked".
if ( preg_match( '/]*>[^<]*(?:FAQ|Frequently\s+Asked)[^<]*<\/h[2-4]>/i', $content ) ) {
return true;
}
return false;
}
/**
* Check for existing FAQ schema from any source.
*
* Covers: our schema override, Yoast FAQ blocks (auto-generate schema),
* Rank Math FAQ blocks (auto-generate schema), Yoast/Rank Math meta,
* [faq] shortcodes that produce schema via theme, and third-party
* FAQ plugins' shortcodes that auto-generate schema.
*
* @param \WP_Post $post Post.
* @return bool
*/
private function has_faq_schema( $post, $schemas = null ) {
$content = $post->post_content;
// AEO God Mode's schema engine is the source of truth for its own
// automatic markup. If it emits FAQPage, the editor checker must not
// call the same content an unhandled FAQ opportunity.
if ( $this->has_generated_schema_type( $post, 'FAQPage', $schemas ) ) {
return true;
}
// Yoast FAQ block auto-generates FAQPage schema on the front end.
if ( false !== strpos( $content, 'wp:yoast/faq-block' ) ) {
return true;
}
// Rank Math FAQ block auto-generates FAQPage schema on the front end.
// Check both block comment format and rendered CSS class format.
if ( false !== strpos( $content, 'wp:rank-math/faq-block' ) ) {
return true;
}
if ( false !== strpos( $content, 'wp-block-rank-math-faq-block' ) ) {
return true;
}
if ( false !== strpos( $content, 'rank-math-question' ) ) {
return true;
}
// Generic [faq] shortcodes may be handled by their owning third-party
// plugin. Our own [aeogm_faq] is intentionally not counted here: its
// truth comes from has_generated_schema_type(), which honours module
// and per-post disable switches.
if ( preg_match_all( '/\[faq\b[^\]]*\]/i', $content, $shortcode_matches ) ) {
if ( count( $shortcode_matches[0] ) >= 2 ) {
return true;
}
}
// [faq] / [faq title="..."] shortcodes. Many themes/plugins generate
// FAQPage schema from these, and AEO God Mode's own Schema_Generator
// produces FAQPage schema for [faq title="..."]...[/faq] pairs.
// Require 2+ pairs to mirror what detect_faq_schema() actually emits.
if ( preg_match_all( '/\[faq\b[^\]]*\]/i', $content, $shortcode_matches ) ) {
if ( count( $shortcode_matches[0] ) >= 2 ) {
return true;
}
}
// Yoast schema page type meta.
$yoast_schema = get_post_meta( $post->ID, '_yoast_wpseo_schema_page_type', true );
if ( $yoast_schema && false !== strpos( $yoast_schema, 'FAQ' ) ) {
return true;
}
// Rank Math rich snippet type.
$rm_snippet = get_post_meta( $post->ID, 'rank_math_rich_snippet', true );
if ( 'faq' === $rm_snippet ) {
return true;
}
// Rank Math schema FAQ type meta.
$rm_schema = get_post_meta( $post->ID, 'rank_math_schema_FAQPage', true );
if ( ! empty( $rm_schema ) ) {
return true;
}
return false;
}
/**
* Detect HowTo-style content.
*
* @param \WP_Post $post Post.
* @return bool
*/
private function has_howto_content( $post, $schemas = null ) {
if ( get_post_meta( $post->ID, '_asgm_disable_schema', true ) || get_post_meta( $post->ID, '_asgm_disable_howto', true ) ) {
return false;
}
// Rank Math / Yoast HowTo blocks.
if ( false !== strpos( $post->post_content, 'wp:yoast/how-to-block' ) ) {
return true;
}
if ( false !== strpos( $post->post_content, 'wp:rank-math/howto-block' ) ) {
return true;
}
// Eligibility is distinct from effective output. Asking the merged
// graph here made the old opportunity condition logically impossible
// because has_howto_schema() queried that same graph again.
return class_exists( __NAMESPACE__ . '\\Schema' )
&& null !== ( new Schema() )->detect_howto_for_post( $post->ID );
}
/**
* Check for existing HowTo schema.
*
* @param \WP_Post $post Post.
* @return bool
*/
private function has_howto_schema( $post, $schemas = null ) {
if ( $this->has_generated_schema_type( $post, 'HowTo', $schemas ) ) {
return true;
}
// Yoast HowTo block auto-generates schema.
if ( false !== strpos( $post->post_content, 'wp:yoast/how-to-block' ) ) {
return true;
}
// Rank Math HowTo block auto-generates schema.
if ( false !== strpos( $post->post_content, 'wp:rank-math/howto-block' ) ) {
return true;
}
// Rank Math rich snippet type.
$rm_snippet = get_post_meta( $post->ID, 'rank_math_rich_snippet', true );
if ( 'howto' === $rm_snippet ) {
return true;
}
return false;
}
/**
* Whether AEO God Mode's automatic schema graph contains a type.
*
* This deliberately asks the schema engine instead of duplicating its
* content detectors. It is the shared contract used by the generator,
* front-end output and editor checker.
*
* @param \WP_Post $post Post object.
* @param string $type Schema.org type.
* @return bool
*/
private function has_generated_schema_type( $post, $type, $schemas = null ) {
if ( null === $schemas ) {
$previous_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
$GLOBALS['post'] = $post;
$result = self::schema_post_snapshot( $post );
$schemas = (array) ( $result['schemas'] ?? array() );
if ( null !== $previous_post ) {
$GLOBALS['post'] = $previous_post;
} else {
unset( $GLOBALS['post'] );
}
}
foreach ( (array) $schemas as $schema ) {
if ( $type === ( $schema['@type'] ?? '' ) ) {
return true;
}
}
return false;
}
/**
* Check for definitive answer structure (H2 followed by a direct paragraph).
*
* @param string $content Post content.
* @return bool
*/
private function has_definitive_answer_structure( $content ) {
// Use the same detector that powers Answer Density when a question-shaped
// heading exists. This prevents one panel from saying "direct" while the
// other says "no answer structure" for the exact same opener.
if ( class_exists( '\\AISEOGodMode\\Answer_Density' ) ) {
$headings = Answer_Density::find_question_headings( $content );
foreach ( $headings as $heading ) {
$body = Answer_Density::extract_after_heading(
$content,
$heading['end'],
$heading['level']
);
$classification = Answer_Density::classify_answer( $body );
if ( in_array( $classification['classification'], array( 'direct', 'buried' ), true ) ) {
return true;
}
}
}
// Gutenberg stores block delimiters between the closing heading and the
// opening paragraph. The former `\s*` check treated those comments as
// content and falsely failed valid answer-first posts after a block edit.
$without_block_comments = preg_replace( '//', '', (string) $content );
return (bool) preg_match(
'/]*>.*?<\/h[23]>\s*]*>/si',
$without_block_comments
);
}
}