post_status ) { return false; } if ( ! empty( $post->post_password ) || post_password_required( $post ) ) { return false; } if ( in_array( $post->post_type, array( 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'oembed_cache', 'wp_navigation' ), true ) ) { return false; } /** * Filter whether a post should be excluded from AEOLynx public outputs. * * @param bool $excluded True to exclude the post. * @param WP_Post $post Candidate post object. */ if ( (bool) apply_filters( 'aeolynx_exclude_post', false, $post ) ) { return false; } return true; } /** * Header lines applied to the plugin's own non-HTML responses. * * @return array */ public static function hardening_header_lines() { return array( 'X-Content-Type-Options: nosniff', "Content-Security-Policy: default-src 'none'; sandbox", 'Referrer-Policy: no-referrer', ); } /** * Defense-in-depth headers for the plugin's own non-HTML responses * (markdown streams, llms.txt, discovery documents, robots policy). * * Scoped strictly to plugin-emitted responses — never sent site-wide, * where a plugin-emitted CSP could break themes or other plugins. * * @return void */ public static function send_hardening_headers() { if ( headers_sent() ) { return; } foreach ( self::hardening_header_lines() as $header_line ) { header( $header_line ); } } /** * Enqueue the lightweight client navigator/document registration bridge * and the AI Summary / citation bar stylesheet. * * @return void */ public static function enqueue_scripts() { if ( ! self::is_enabled() ) { return; } wp_register_script( 'aeolynx-register', AEOLYNX_CORE_URL . 'public/js/aeolynx-register.js', array(), AEOLYNX_CORE_VERSION, true ); if ( function_exists( 'wp_script_add_data' ) ) { wp_script_add_data( 'aeolynx-register', 'strategy', 'defer' ); } wp_localize_script( 'aeolynx-register', 'AEOLynxConfig', array( 'restUrl' => esc_url_raw( rest_url( 'aeolynx/v1/call/' ) ), 'manifestUrl' => esc_url_raw( rest_url( 'aeolynx/v1/manifest' ) ), 'toolsUrl' => esc_url_raw( rest_url( 'aeolynx/v1/tools' ) ), 'wellKnown' => esc_url_raw( home_url( '/.well-known/mcp.json' ) ), 'llmsTxtUrl' => esc_url_raw( home_url( '/llms.txt' ) ), 'postId' => (int) get_queried_object_id(), 'version' => AEOLYNX_CORE_VERSION, ) ); wp_enqueue_script( 'aeolynx-register' ); // Register AI box assets lazily — enqueued only when the widget or shortcode renders (BUG-004 / BUG-018). wp_register_style( 'aeolynx-ai-box', AEOLYNX_CORE_URL . 'public/css/webmcp-ai-box.css', array(), AEOLYNX_CORE_VERSION ); wp_register_script( 'aeolynx-ai-box-theme', AEOLYNX_CORE_URL . 'public/js/aeolynx-ai-box-theme.js', array(), AEOLYNX_CORE_VERSION, true ); if ( function_exists( 'wp_script_add_data' ) ) { wp_script_add_data( 'aeolynx-ai-box-theme', 'strategy', 'defer' ); } } /** * Filter HTTP response headers via WordPress wp_headers filter. * * @param mixed $headers Existing HTTP headers. * @return array */ public static function filter_wp_headers( $headers = array() ) { if ( ! is_array( $headers ) ) { $headers = array(); } if ( is_admin() || ! self::is_enabled() ) { return $headers; } $links = array( '<' . esc_url_raw( rest_url( 'aeolynx/v1/manifest' ) ) . '>; rel="model-context-protocol"', '<' . esc_url_raw( home_url( '/.well-known/mcp.json' ) ) . '>; rel="mcp-server"', '<' . esc_url_raw( home_url( '/llms.txt' ) ) . '>; rel="llms-txt"', '<' . esc_url_raw( home_url( '/.well-known/api-catalog' ) ) . '>; rel="api-catalog"', '<' . esc_url_raw( rest_url( 'aeolynx/v1/manifest' ) ) . '>; rel="service-desc"', ); $headers['X-WebMCP'] = AEOLYNX_CORE_VERSION; $headers['X-ModelContextProtocol'] = '1.0'; if ( ! empty( $headers['Link'] ) ) { $headers['Link'] .= ', ' . implode( ', ', $links ); } else { $headers['Link'] = implode( ', ', $links ); } return $headers; } /** * Render discovery , , manifest JSON, and Schema.org @graph in . * * @return void */ public static function render_head() { if ( ! self::is_enabled() ) { return; } $context = self::resolve_context(); $post_id = 0; if ( 'single' === $context ) { $post_id = (int) get_queried_object_id(); } elseif ( 'homepage' === $context && is_front_page() && ! is_home() ) { $post_id = (int) get_option( 'page_on_front', 0 ); } $manifest = AEOLynx_Schema_Builder::build( $context, $post_id ); $manifest_url = rest_url( 'aeolynx/v1/manifest' ); $well_known = home_url( '/.well-known/mcp.json' ); $llms_url = home_url( '/llms.txt' ); $canonical = AEOLynx_Schema_Graph::get_canonical_url( $post_id, $context ); $md_url = add_query_arg( 'format', 'markdown', $canonical ); $rate_limit = max( 10, absint( get_option( 'aeolynx_rate_limit', 60 ) ) ); printf( '' . "\n", esc_attr( sprintf( 'allow=read-tools; max-calls-per-minute=%d; crawl-delay=1', $rate_limit ) ) ); printf( '' . "\n", esc_url( $manifest_url ) ); printf( '' . "\n" ); printf( '' . "\n", esc_url( $manifest_url ) ); printf( '' . "\n", esc_url( $well_known ) ); printf( '' . "\n", esc_url( $llms_url ) ); printf( '' . "\n", esc_url( home_url( '/.well-known/ai-catalog.json' ) ) ); if ( (bool) get_option( 'aeolynx_markdown_enabled', 1 ) ) { printf( '' . "\n", esc_url( $md_url ), esc_attr__( 'Clean Semantic Markdown for AI Agents', 'aeolynx-for-webmcp' ) ); } $manifest_json = wp_json_encode( $manifest, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); if ( is_string( $manifest_json ) && '' !== $manifest_json ) { if ( function_exists( 'wp_print_inline_script_tag' ) ) { wp_print_inline_script_tag( $manifest_json, array( 'type' => 'application/json', 'id' => 'webmcp-manifest', ) ); } } $schema_ld = AEOLynx_Schema_Graph::generate( $post_id, $context ); if ( ! empty( $schema_ld ) ) { $schema_json = wp_json_encode( $schema_ld, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( is_string( $schema_json ) && '' !== $schema_json ) { if ( function_exists( 'wp_print_inline_script_tag' ) ) { wp_print_inline_script_tag( $schema_json, array( 'type' => 'application/ld+json', 'id' => 'webmcp-jsonld', ) ); } } } } /** * Resolve the configured AI box color theme (light, dark, or auto). * * @return string */ public static function get_ai_box_theme() { $theme = (string) get_option( 'aeolynx_ai_box_theme', 'auto' ); return in_array( $theme, array( 'light', 'dark', 'auto' ), true ) ? $theme : 'auto'; } /** * Render the AI Summary + "Ask AI / Cite This Page" widget HTML for a post. * * @param WP_Post $post Target post. * @param bool $include_summary Whether to display the TL;DR summary box. * @param bool $include_citebar Whether to display the "Ask AI / View Markdown" bar. * @return string */ public static function build_frontend_ai_box_html( $post, $include_summary = true, $include_citebar = true ) { if ( ! self::is_public_post( $post ) ) { return ''; } // Enqueue AI box stylesheet & auto-theme luminance script only when rendered (BUG-004 / BUG-018). wp_enqueue_style( 'aeolynx-ai-box' ); if ( 'auto' === self::get_ai_box_theme() ) { wp_enqueue_script( 'aeolynx-ai-box-theme' ); } $summary = AEOLynx_Activity_Log::get_effective_ai_summary( $post ); $permalink = (string) get_permalink( $post ); $md_url = add_query_arg( 'format', 'markdown', $permalink ); $prompt = sprintf( /* translators: 1: Post title, 2: Permalink URL */ __( 'Summarize and cite the key insights from "%1$s" (%2$s)', 'aeolynx-for-webmcp' ), get_the_title( $post ), $permalink ); $chatgpt_url = 'https://chatgpt.com/?q=' . rawurlencode( $prompt ); $perplexity_url = 'https://www.perplexity.ai/search?q=' . rawurlencode( $prompt ); $claude_url = 'https://claude.ai/new?q=' . rawurlencode( $prompt ); $html = ''; // Smart auto theme is applied by public/js/aeolynx-ai-box-theme.js (enqueued). return (string) apply_filters( 'aeolynx_ai_box_html', $html, $post, $include_summary, $include_citebar ); } /** * Shortcode callback for `[aeolynx_ai_summary]`. * * @return string */ public static function render_ai_summary_shortcode() { $post = get_post(); if ( ! ( $post instanceof WP_Post ) ) { return ''; } return self::build_frontend_ai_box_html( $post, true, true ); } /** * Automatically prepend the AI Summary and/or "Ask AI" bar when opted in via settings. * * @param mixed $content Post HTML content. * @return string */ public static function maybe_prepend_ai_widget( $content = '' ) { $content = (string) $content; if ( is_admin() || ! is_singular() || ! in_the_loop() || ! is_main_query() ) { return $content; } $show_summary = (bool) get_option( 'aeolynx_ai_summary_render', 0 ); $show_citebar = (bool) get_option( 'aeolynx_cite_bar_render', 0 ); if ( ! $show_summary && ! $show_citebar ) { return $content; } $post = get_post(); if ( ! ( $post instanceof WP_Post ) ) { return $content; } return self::build_frontend_ai_box_html( $post, $show_summary, $show_citebar ) . $content; } /** * Resolve current WordPress request context. * * @return string */ public static function resolve_context() { if ( is_404() ) { return '404'; } if ( is_search() ) { return 'search'; } if ( is_singular() ) { return 'single'; } if ( is_home() || is_front_page() ) { return 'homepage'; } if ( is_archive() ) { return 'archive'; } return 'homepage'; } } }