esc_html__( 'Question', 'ai-optimization-for-google-position-zero' ),
'answer_placeholder' => esc_html__( 'Answer', 'ai-optimization-for-google-position-zero' ),
)
);
wp_enqueue_script( 'aiopt-faq-admin' );
}
/**
* Add Meta Box
*/
add_action( 'add_meta_boxes', 'aiopt_add_faq_meta_box' );
function aiopt_add_faq_meta_box() {
add_meta_box(
'aiopt_faq_meta_box',
__( 'FAQs for AI Optimization', 'ai-optimization-for-google-position-zero' ),
'aiopt_faq_meta_box_callback',
array( 'post', 'page' ),
'normal',
'default'
);
}
/**
* Meta Box Callback
*/
function aiopt_faq_meta_box_callback( $post ) {
wp_nonce_field( 'aiopt_save_faq_nonce_action', 'aiopt_faq_nonce_field' );
$content = apply_filters( 'the_content', $post->post_content );
$report = aiopt_analyze_content( $content );
echo '
' . esc_html__( 'Current Status (Summary)', 'ai-optimization-for-google-position-zero' ) . '
';
echo '';
foreach ( $report as $label => $value ) {
echo '- ' . esc_html( ucfirst( str_replace( '_', ' ', $label ) ) ) . ': ' . esc_html( $value ) . '
';
}
echo '
';
$questions = get_post_meta( $post->ID, '_aiopt_faq_questions', true );
$answers = get_post_meta( $post->ID, '_aiopt_faq_answers', true );
$show_faqs = get_post_meta( $post->ID, '_aiopt_faq_show', true );
if ( ! is_array( $questions ) || empty( $questions ) ) {
$questions = array( '' );
}
if ( ! is_array( $answers ) || empty( $answers ) ) {
$answers = array( '' );
}
?>
loadHTML( '' . $html );
libxml_clear_errors();
$report = array();
$report['title'] = get_the_title() ?: 'Missing';
$h1 = $dom->getElementsByTagName( 'h1' );
$report['h1'] = ( get_the_title() > 0 ) ? 'Present' : 'Missing';
$p = $dom->getElementsByTagName( 'p' );
$report['paragraphs'] = $p->length;
return $report;
}
/**
* Append FAQs to content with JSON-LD
*/
add_filter( 'the_content', 'aiopt_append_faqs_to_content' );
function aiopt_append_faqs_to_content( $content ) {
if ( ! is_singular( array( 'post', 'page' ) ) ) {
return $content;
}
global $post;
$questions = get_post_meta( $post->ID, '_aiopt_faq_questions', true );
$answers = get_post_meta( $post->ID, '_aiopt_faq_answers', true );
$show_faqs = get_post_meta( $post->ID, '_aiopt_faq_show', true );
if ( ! is_array( $questions ) || empty( $questions ) ) {
return $content;
}
$faqHtml = '';
$faqSchema = array();
if ( '1' === $show_faqs ) {
$faqHtml .= '' . esc_html__( 'Frequently Asked Questions', 'ai-optimization-for-google-position-zero' ) . '
';
}
foreach ( $questions as $i => $q ) {
$a = isset( $answers[ $i ] ) ? $answers[ $i ] : '';
if ( trim( $q ) && trim( $a ) ) {
if ( '1' === $show_faqs ) {
$faqHtml .= '- ' . esc_html( $q ) . '
' . wp_kses_post( $a ) . ' ';
}
$faqSchema[] = array(
'@type' => 'Question',
'name' => wp_strip_all_tags( $q ),
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => wp_strip_all_tags( $a ),
),
);
}
}
if ( '1' === $show_faqs ) {
$faqHtml .= '
';
}
if ( ! empty( $faqSchema ) ) {
$faqHtml .= '';
}
return $content . $faqHtml;
}