%s', do_shortcode( $content ) ); } /** * Render a single Q&A pair as `
/` with schema.org * microdata. Matches the markup emitted by the Gutenberg faq-item block. */ public function render_qa( $atts, $content = null ): string { $atts = shortcode_atts( array( 'question' => '', ), $atts, 'aeo_qa' ); $q = trim( (string) $atts['question'] ); $a = trim( (string) $content ); if ( '' === $q || '' === wp_strip_all_tags( $a ) ) { return ''; } return sprintf( '
' . '%s' . '
' . '
%s
' . '
' . '
', esc_html( $q ), wp_kses_post( do_shortcode( $a ) ) ); } /** * Extract Q&A pairs from raw post content by parsing the shortcode source * (NOT the rendered HTML — this runs during wp_head before the_content). * * @return array */ public static function extract_pairs( string $content ): array { if ( '' === $content || false === strpos( $content, '[aeo_qa' ) ) { return array(); } $pairs = array(); if ( preg_match_all( '/\[aeo_qa\b([^\]]*)\](.*?)\[\/aeo_qa\]/is', $content, $matches, PREG_SET_ORDER ) ) { foreach ( $matches as $m ) { $attrs = shortcode_parse_atts( $m[1] ) ?: array(); $question = isset( $attrs['question'] ) ? trim( wp_strip_all_tags( $attrs['question'] ) ) : ''; $answer = trim( wp_strip_all_tags( $m[2] ?? '' ) ); if ( '' !== $question && '' !== $answer ) { $pairs[] = array( 'q' => $question, 'a' => $answer ); } } } return $pairs; } }