%s', esc_url( admin_url( 'admin.php?page=aeolynx-for-webmcp&tab=xray' ) ), esc_html__( 'Page AEO X-Ray', 'aeolynx-for-webmcp' ) ); $sandbox_link = sprintf( '%s', esc_url( admin_url( 'admin.php?page=aeolynx-for-webmcp&tab=sandbox' ) ), esc_html__( 'Agent Sandbox', 'aeolynx-for-webmcp' ) ); $docs_link = sprintf( '%s', esc_url( 'https://aeolynx.wikiofautomation.com/docs/' ), esc_html__( 'Docs', 'aeolynx-for-webmcp' ) ); $settings_link = sprintf( '%s', esc_url( admin_url( 'admin.php?page=aeolynx-for-webmcp&tab=schema' ) ), esc_html__( 'Settings', 'aeolynx-for-webmcp' ) ); array_unshift( $links, $xray_link, $sandbox_link, $settings_link, $docs_link ); return $links; } /** * Register all plugin settings with explicit sanitization callbacks. * * @return void */ public static function register_settings() { $bool_settings = array( 'aeolynx_enabled', 'aeolynx_schema_enabled', 'aeolynx_speakable_enabled', 'aeolynx_auto_faq_enabled', 'aeolynx_woo_schema_on', 'aeolynx_markdown_enabled', 'aeolynx_llmstxt_enabled', 'aeolynx_llmsfull_enabled', 'aeolynx_robots_enabled', 'aeolynx_activity_log_on', ); foreach ( $bool_settings as $opt ) { register_setting( 'aeolynx_settings', $opt, array( 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 1, ) ); } // Frontend widgets default to OFF until the site owner opts in. foreach ( array( 'aeolynx_ai_summary_render', 'aeolynx_cite_bar_render' ) as $opt ) { register_setting( 'aeolynx_settings', $opt, array( 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 0, ) ); } $bot_keys = array( 'aeolynx_bot_oai_search', 'aeolynx_bot_chatgpt_user', 'aeolynx_bot_perplexity', 'aeolynx_bot_claude', 'aeolynx_bot_google_ext', 'aeolynx_bot_gptbot', 'aeolynx_bot_ccbot', ); foreach ( $bot_keys as $bot_key ) { register_setting( 'aeolynx_settings', $bot_key, array( 'type' => 'string', 'sanitize_callback' => array( __CLASS__, 'sanitize_bot_rule' ), 'default' => 'allow', ) ); } register_setting( 'aeolynx_settings', 'aeolynx_rate_limit', array( 'type' => 'integer', 'sanitize_callback' => array( __CLASS__, 'sanitize_rate_limit' ), 'default' => 60, ) ); register_setting( 'aeolynx_settings', 'aeolynx_entity_type', array( 'type' => 'string', 'sanitize_callback' => array( __CLASS__, 'sanitize_entity_type' ), 'default' => 'Organization', ) ); register_setting( 'aeolynx_settings', 'aeolynx_entity_name', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '', ) ); register_setting( 'aeolynx_settings', 'aeolynx_logo_url', array( 'type' => 'string', 'sanitize_callback' => array( __CLASS__, 'sanitize_http_url' ), 'default' => '', ) ); register_setting( 'aeolynx_settings', 'aeolynx_same_as', array( 'type' => 'string', 'sanitize_callback' => array( __CLASS__, 'sanitize_url_list' ), 'default' => '', ) ); register_setting( 'aeolynx_settings', 'aeolynx_speakable_css', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => 'h1.entry-title, .entry-title, .entry-content > p:first-of-type, .wp-block-post-content > p:first-of-type, .wm-ai-box__summary', ) ); register_setting( 'aeolynx_settings', 'aeolynx_default_schema', array( 'type' => 'string', 'sanitize_callback' => array( __CLASS__, 'sanitize_default_schema' ), 'default' => 'BlogPosting', ) ); register_setting( 'aeolynx_settings', 'aeolynx_ai_box_theme', array( 'type' => 'string', 'sanitize_callback' => array( __CLASS__, 'sanitize_ai_box_theme' ), 'default' => 'auto', ) ); register_setting( 'aeolynx_settings', 'aeolynx_content_signal', array( 'type' => 'string', 'sanitize_callback' => array( __CLASS__, 'sanitize_content_signal' ), 'default' => 'allow_all', ) ); register_setting( 'aeolynx_audit_settings', 'aeolynx_cloudflare_scan', array( 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 0, ) ); // MCP REST API access control (Issue 1): optional API-key requirement, default OFF. register_setting( 'aeolynx_settings', 'aeolynx_require_api_key', array( 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 0, ) ); } /** * Sanitize the Content Signals policy choice. * * @param mixed $val Raw input. * @return string */ public static function sanitize_content_signal( $val ) { $clean = sanitize_key( (string) $val ); $allowed = array( 'off', 'allow_all', 'no_training', 'search_only', 'block_all' ); return in_array( $clean, $allowed, true ) ? $clean : 'allow_all'; } /** * Sanitize a single URL, allowing only http(s) schemes. * * Blocks file://, phar://, javascript:, and other non-HTTP schemes at * save time; anything disallowed becomes an empty string. * * @param mixed $val Raw input. * @return string */ public static function sanitize_http_url( $val ) { $url = esc_url_raw( trim( (string) $val ) ); if ( '' === $url ) { return ''; } $scheme = wp_parse_url( $url, PHP_URL_SCHEME ); if ( ! in_array( strtolower( (string) $scheme ), array( 'http', 'https' ), true ) ) { return ''; } return $url; } /** * Sanitize a newline/comma-separated URL list, keeping only http(s) URLs. * * @param mixed $val Raw input. * @return string */ public static function sanitize_url_list( $val ) { $parts = preg_split( '/[\r\n,]+/', (string) $val ); $keep = array(); if ( is_array( $parts ) ) { foreach ( $parts as $part ) { $url = self::sanitize_http_url( $part ); if ( '' !== $url ) { $keep[] = $url; } } } return implode( "\n", $keep ); } /** * Resolve a stable asset version (file mtime, falling back to the plugin version). * * @param string $relative_path Path relative to the plugin folder. * @return string */ private static function asset_version( $relative_path ) { $mtime = filemtime( AEOLYNX_CORE_PATH . $relative_path ); return $mtime ? (string) $mtime : AEOLYNX_CORE_VERSION; } /** * Sanitize per-bot rule (allow or disallow). * * @param mixed $val Raw input. * @return string */ public static function sanitize_bot_rule( $val ) { $clean = sanitize_key( (string) $val ); return 'disallow' === $clean ? 'disallow' : 'allow'; } /** * Sanitize rate limit (10 to 600 requests/min). * * @param mixed $val Raw input. * @return int */ public static function sanitize_rate_limit( $val ) { return min( 600, max( 10, absint( $val ) ) ); } /** * Sanitize Publisher Entity Type. * * @param mixed $val Raw input. * @return string */ public static function sanitize_entity_type( $val ) { $clean = sanitize_text_field( (string) $val ); return in_array( $clean, array( 'Organization', 'Person' ), true ) ? $clean : 'Organization'; } /** * Sanitize Default Article Schema Type. * * @param mixed $val Raw input. * @return string */ public static function sanitize_default_schema( $val ) { $clean = sanitize_text_field( (string) $val ); $allowed = array( 'BlogPosting', 'Article', 'NewsArticle', 'TechArticle' ); return in_array( $clean, $allowed, true ) ? $clean : 'BlogPosting'; } /** * Sanitize the frontend AI box theme choice. * * @param mixed $val Raw input. * @return string */ public static function sanitize_ai_box_theme( $val ) { $clean = sanitize_key( (string) $val ); return in_array( $clean, array( 'light', 'dark', 'auto' ), true ) ? $clean : 'auto'; } /** * Enqueue admin CSS and JS strictly on AEOLynx for WebMCP plugin and post-edit screens. * * @param mixed $hook Current admin screen hook suffix. * @return void */ public static function enqueue_assets( $hook = '' ) { $hook = (string) $hook; $is_app = ( 'toplevel_page_aeolynx-for-webmcp' === $hook ); $is_editor = ( 'post.php' === $hook || 'post-new.php' === $hook ); if ( ! $is_app && ! $is_editor ) { return; } wp_enqueue_style( 'aeolynx-admin', AEOLYNX_CORE_URL . 'admin/css/aeolynx-admin.css', array( 'dashicons' ), self::asset_version( 'admin/css/aeolynx-admin.css' ) ); if ( $is_editor ) { wp_enqueue_style( 'aeolynx-metabox', AEOLYNX_CORE_URL . 'admin/css/aeolynx-metabox.css', array( 'aeolynx-admin' ), self::asset_version( 'admin/css/aeolynx-metabox.css' ) ); wp_enqueue_script( 'aeolynx-metabox', AEOLYNX_CORE_URL . 'admin/js/aeolynx-metabox.js', array(), self::asset_version( 'admin/js/aeolynx-metabox.js' ), true ); return; } wp_enqueue_script( 'aeolynx-admin', AEOLYNX_CORE_URL . 'admin/js/aeolynx-admin.js', array( 'jquery' ), self::asset_version( 'admin/js/aeolynx-admin.js' ), true ); wp_localize_script( 'aeolynx-admin', 'AEOLynxAdmin', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aeolynx_sandbox_nonce' ), 'version' => AEOLYNX_CORE_VERSION, 'i18n' => array( 'no_activity' => __( 'No AI activity yet', 'aeolynx-for-webmcp' ), 'no_activity_hint' => __( 'AI crawler and agent traffic appears here as it happens - every entry is a real request your server received.', 'aeolynx-for-webmcp' ), // translators: %d: Total recorded event count. 'show_all_events' => __( 'Show all %d events', 'aeolynx-for-webmcp' ), 'revoke_confirm' => __( 'Revoke this API key? Clients using it will immediately lose access.', 'aeolynx-for-webmcp' ), 'key_label_needed' => __( 'Enter a label for the new key first.', 'aeolynx-for-webmcp' ), 'key_generated' => __( 'API key generated — copied to clipboard. It will not be shown again; list refreshes in 8s.', 'aeolynx-for-webmcp' ), 'key_revoked' => __( 'API key revoked.', 'aeolynx-for-webmcp' ), 'request_failed' => __( 'Request failed.', 'aeolynx-for-webmcp' ), // translators: %s: Readiness level name from Cloudflare. 'cf_verdict' => __( 'Cloudflare verdict: %s', 'aeolynx-for-webmcp' ), 'just_now' => __( 'just now', 'aeolynx-for-webmcp' ), // translators: %d: Minutes since the event. 'mins_ago' => __( '%dm ago', 'aeolynx-for-webmcp' ), // translators: %d: Hours since the event. 'hours_ago' => __( '%dh ago', 'aeolynx-for-webmcp' ), // translators: %d: Days since the event. 'days_ago' => __( '%dd ago', 'aeolynx-for-webmcp' ), ), ) ); } /** * Render the AEOLynx for WebMCP app shell (topbar + rail nav + active screen). * * @return void */ public static function render_admin_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $active_tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'dashboard'; if ( ! in_array( $active_tab, array( 'dashboard', 'xray', 'tracking', 'sandbox', 'audit', 'schema' ), true ) ) { $active_tab = 'dashboard'; } $screens = array( 'dashboard' => array( 'label' => __( 'Overview', 'aeolynx-for-webmcp' ), 'subtitle' => __( 'Live proof that your site is AI-ready: health score, agent activity, and endpoint previews.', 'aeolynx-for-webmcp' ), ), 'audit' => array( 'label' => __( 'Agent Audit', 'aeolynx-for-webmcp' ), 'subtitle' => __( 'Industry-standard agent-readiness audit — the same checks Cloudflare\'s isitagentready.com runs, scanned from your own server.', 'aeolynx-for-webmcp' ), ), 'xray' => array( 'label' => __( 'X-Ray Scanner', 'aeolynx-for-webmcp' ), 'subtitle' => __( 'See exactly what AI answer engines will quote from any page — and fix the gaps in one click.', 'aeolynx-for-webmcp' ), ), 'tracking' => array( 'label' => __( 'AI Search Tracking', 'aeolynx-for-webmcp' ), 'subtitle' => __( 'Mine buyer-intent AI search prompts locally from your own site, test them across engines, or automate weekly multi-engine tracking with AEOLynx Pro.', 'aeolynx-for-webmcp' ), ), 'sandbox' => array( 'label' => __( 'Agent Sandbox', 'aeolynx-for-webmcp' ), 'subtitle' => __( 'Run any of the 6 MCP tools against your live site and inspect the exact JSON an agent receives.', 'aeolynx-for-webmcp' ), ), 'schema' => array( 'label' => __( 'Settings', 'aeolynx-for-webmcp' ), 'subtitle' => __( 'Control the schema engine, per-bot crawler policy, frontend widgets, and rate limits.', 'aeolynx-for-webmcp' ), ), ); $is_active = (bool) get_option( 'aeolynx_enabled', 1 ); ?>

'dashicons-chart-line', 'audit' => 'dashicons-shield', 'xray' => 'dashicons-search', 'tracking' => 'dashicons-chart-area', 'sandbox' => 'dashicons-editor-code', 'schema' => 'dashicons-admin-settings', ); return isset( $icons[ $slug ] ) ? $icons[ $slug ] : 'dashicons-marker'; } /** * AJAX handler: Run any of the 6 MCP tools in the Admin Agent Sandbox. * * @return void */ /** * Per-user fixed-window throttle for expensive AJAX handlers. * * Counters live in the options table using the same atomic window-naming * scheme as the REST rate limiter: one INSERT ... ON DUPLICATE KEY UPDATE * per call, shared across load-balanced web nodes and untouched by * cache-plugin purges. Call AFTER nonce + capability checks. * * @param string $bucket Handler group identifier. * @param int $limit Maximum calls per window per user. * @param int $window Window length in seconds. * @return true|WP_Error */ public static function throttle_ajax( $bucket, $limit = 30, $window = MINUTE_IN_SECONDS ) { $user_id = get_current_user_id(); if ( $user_id <= 0 ) { return true; // Anonymous callers are already rejected by the capability check. } $now = time(); $window_start = $now - ( $now % $window ); $option_name = 'aeolynx_throttle_' . sanitize_key( $bucket ) . '_' . $user_id . '_' . $window_start; global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Single-statement atomic upsert; caching would defeat the counter. $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->options} ( option_name, option_value, autoload ) VALUES ( %s, 1, 'no' ) " . 'ON DUPLICATE KEY UPDATE option_value = option_value + 1', $option_name ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Read-back of the atomic counter written above. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $option_name ) ); $count = ( $row && is_numeric( $row->option_value ) ) ? (int) $row->option_value : 1; // Occasionally sweep counters from expired windows (about 2% of calls). if ( 0 === wp_rand( 0, 49 ) ) { self::sql_sweep_throttle_counters( $now ); } if ( $count > $limit ) { return new WP_Error( 'aeolynx_throttled', __( 'Too many requests. Please wait a moment and try again.', 'aeolynx-for-webmcp' ), array( 'status' => 429 ) ); } return true; } /** * Delete expired AJAX throttle rows using portable SQL (MySQL, MariaDB & SQLite safe). * * @param int $now Current timestamp. * @return void */ public static function sql_sweep_throttle_counters( $now ) { global $wpdb; $cutoff = (int) $now - 5 * MINUTE_IN_SECONDS; $like = $wpdb->esc_like( 'aeolynx_throttle_' ) . '%'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Portable prefix scan avoiding MySQL-only SUBSTRING_INDEX. $names = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s LIMIT 250", $like ) ); if ( ! is_array( $names ) || empty( $names ) ) { return; } $expired = array(); foreach ( $names as $name ) { $pos = strrpos( (string) $name, '_' ); if ( false === $pos ) { continue; } $ts = substr( (string) $name, $pos + 1 ); if ( ctype_digit( $ts ) && (int) $ts < $cutoff ) { $expired[] = (string) $name; } } if ( empty( $expired ) ) { return; } $placeholders = implode( ', ', array_fill( 0, count( $expired ), '%s' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Dynamic placeholder list matches $expired exactly. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name IN ( {$placeholders} )", $expired ) ); } /** * Throttle guard for AJAX handlers: sends a JSON 429 and stops when the * per-user budget for the bucket is exhausted. * * @param string $bucket Handler group identifier. * @param int $limit Maximum calls per minute per user. * @return void */ private static function guard_ajax( $bucket, $limit ) { $ok = self::throttle_ajax( $bucket, $limit ); if ( is_wp_error( $ok ) ) { wp_send_json_error( array( 'error' => $ok->get_error_message() ), 429 ); } } /** * AJAX: issue a short-lived (60s) one-time execution token for the sandbox. * * The standard 24h nonce stays as the first factor; this token narrows the * window for replaying a leaked sandbox request to one minute and one use. * * @return void */ public static function ajax_get_exec_token() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } $token = wp_generate_password( 32, false, false ); set_transient( 'aeolynx_exec_' . md5( $token ), get_current_user_id(), 60 ); wp_send_json_success( array( 'token' => $token ) ); } /** * Verify and burn a one-time sandbox execution token. * * @return void Sends JSON 403 and exits when the token is missing, stale, * already used, or issued to a different user. */ private static function verify_exec_token() { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check_ajax_referer runs in ajax_run_sandbox_tool before this guard is invoked. $provided = isset( $_POST['exec_token'] ) ? sanitize_text_field( wp_unslash( $_POST['exec_token'] ) ) : ''; $key = 'aeolynx_exec_' . md5( $provided ); if ( '' === $provided ) { wp_send_json_error( array( 'error' => __( 'Missing execution token. Reload the Sandbox and try again.', 'aeolynx-for-webmcp' ) ), 403 ); } $owner = get_transient( $key ); if ( false === $owner || (int) $owner !== get_current_user_id() ) { wp_send_json_error( array( 'error' => __( 'Execution token expired or invalid. Reload the Sandbox and try again.', 'aeolynx-for-webmcp' ) ), 403 ); } // One-time burn: a replayed request fails even inside the 60s window. delete_transient( $key ); } public static function ajax_run_sandbox_tool() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } self::guard_ajax( 'sandbox', 30 ); self::verify_exec_token(); $start = microtime( true ); $tool = isset( $_POST['tool'] ) ? sanitize_text_field( wp_unslash( $_POST['tool'] ) ) : ''; $param = isset( $_POST['param'] ) ? sanitize_text_field( wp_unslash( $_POST['param'] ) ) : ''; $params = array(); if ( 'searchSite' === $tool ) { $params['query'] = '' !== $param ? $param : 'WordPress'; $params['limit'] = 5; } elseif ( 'getPostContent' === $tool ) { $post_id = absint( $param ); if ( $post_id <= 0 ) { $recent_post = get_posts( array( 'post_type' => array( 'post', 'page' ), 'post_status' => 'publish', 'posts_per_page' => 1, ) ); $post_id = ! empty( $recent_post[0] ) ? (int) $recent_post[0]->ID : 1; } $params['post_id'] = $post_id; } elseif ( 'getFeaturedContent' === $tool || 'getSiteTaxonomies' === $tool ) { $params['limit'] = '' !== $param ? min( 20, max( 1, absint( $param ) ) ) : 5; } elseif ( 'getCatalogItems' === $tool ) { $params['query'] = $param; $params['limit'] = 5; } $req = new WP_REST_Request( 'POST', '/aeolynx/v1/call/' . $tool ); $req->set_param( 'tool', $tool ); $req->set_body( (string) wp_json_encode( $params ) ); $req->set_header( 'content-type', 'application/json' ); $res = AEOLynx_REST::call_tool( $req ); $elapsed_ms = round( ( microtime( true ) - $start ) * 1000, 2 ); if ( is_wp_error( $res ) ) { wp_send_json_error( array( 'error' => $res->get_error_message(), 'elapsed_ms' => $elapsed_ms, ) ); } wp_send_json_success( array( 'tool' => $tool, 'elapsed_ms' => $elapsed_ms, 'result' => $res->get_data(), ) ); } /** * AJAX handler: Preview the clean Markdown content negotiation stream for a post. * * @return void */ public static function ajax_preview_markdown() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } self::guard_ajax( 'preview', 30 ); $post_id = isset( $_POST['post_id'] ) ? absint( wp_unslash( $_POST['post_id'] ) ) : 0; if ( $post_id <= 0 ) { $recent = get_posts( array( 'post_type' => array( 'post', 'page' ), 'post_status' => 'publish', 'posts_per_page' => 1, ) ); if ( ! empty( $recent[0] ) ) { $post_id = (int) $recent[0]->ID; } } $post = $post_id > 0 ? get_post( $post_id ) : null; if ( ! ( $post instanceof WP_Post ) ) { wp_send_json_error( array( 'error' => __( 'No published posts or pages found to preview.', 'aeolynx-for-webmcp' ) ) ); } AEOLynx_Activity_Log::record_event( 'markdown', 'Admin Markdown Inspector', get_the_title( $post ), __( 'Generated clean AI Markdown preview with YAML frontmatter', 'aeolynx-for-webmcp' ) ); $markdown = AEOLynx_Markdown::build_post_markdown_document( $post ); wp_send_json_success( array( 'post_id' => $post->ID, 'title' => get_the_title( $post ), 'markdown' => $markdown, ) ); } /** * AJAX handler: Run the Page AEO & Agent X-Ray Audit on a selected post/page. * * @return void */ public static function ajax_run_xray_audit() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } self::guard_ajax( 'xray', 20 ); $post_id = isset( $_POST['post_id'] ) ? absint( wp_unslash( $_POST['post_id'] ) ) : 0; if ( $post_id <= 0 ) { $recent = get_posts( array( 'post_type' => array( 'post', 'page' ), 'post_status' => 'publish', 'posts_per_page' => 1, ) ); if ( ! empty( $recent[0] ) ) { $post_id = (int) $recent[0]->ID; } } $post = $post_id > 0 ? get_post( $post_id ) : null; if ( ! ( $post instanceof WP_Post ) ) { wp_send_json_error( array( 'error' => __( 'No published post or page found to scan.', 'aeolynx-for-webmcp' ) ) ); } $report = AEOLynx_Activity_Log::run_post_xray_audit( $post ); wp_send_json_success( $report ); } /** * AJAX handler: Inject selected Answer-Gap questions (with user-edited answers) * into a post's FAQPage schema metadata, deduped by question text, then * re-run the X-Ray audit so the score updates immediately. * * @return void */ public static function ajax_inject_answer_gaps() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } self::guard_ajax( 'inject', 10 ); $post_id = isset( $_POST['post_id'] ) ? absint( wp_unslash( $_POST['post_id'] ) ) : 0; $post = $post_id > 0 ? get_post( $post_id ) : null; if ( ! ( $post instanceof WP_Post ) ) { wp_send_json_error( array( 'error' => __( 'Invalid post selected.', 'aeolynx-for-webmcp' ) ) ); } // Client sends the user's checked & edited gaps as JSON; fall back to auto-generated ones. $incoming = array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- JSON decoded & sanitized item-by-item below. $raw_gaps = isset( $_POST['gaps'] ) ? wp_unslash( $_POST['gaps'] ) : ''; if ( is_string( $raw_gaps ) && '' !== $raw_gaps ) { $decoded = json_decode( $raw_gaps, true ); if ( is_array( $decoded ) ) { foreach ( $decoded as $item ) { if ( ! is_array( $item ) ) { continue; } $q = isset( $item['q'] ) ? sanitize_text_field( (string) $item['q'] ) : ''; $a = isset( $item['a'] ) ? sanitize_textarea_field( (string) $item['a'] ) : ''; if ( '' !== $q && '' !== $a ) { $incoming[] = array( 'q' => $q, 'a' => $a, ); } } } } if ( empty( $incoming ) ) { $generated = AEOLynx_Activity_Log::generate_answer_gap_questions( $post ); foreach ( $generated as $g ) { $incoming[] = array( 'q' => $g['q'], 'a' => $g['a_hint'], ); } } $existing = get_post_meta( $post_id, '_aeolynx_faq_items', true ); $existing = is_array( $existing ) ? $existing : array(); $seen = array(); foreach ( $existing as $item ) { $seen[ md5( mb_strtolower( trim( (string) ( isset( $item['q'] ) ? $item['q'] : '' ) ) ) ) ] = true; } $injected = 0; $skipped = 0; foreach ( $incoming as $item ) { $key = md5( mb_strtolower( trim( $item['q'] ) ) ); if ( isset( $seen[ $key ] ) ) { continue; } // Never store instructional Answer-Gap placeholders in FAQPage meta. if ( ! AEOLynx_Schema_Graph::is_publishable_faq_answer( $item['a'] ) ) { ++$skipped; continue; } $seen[ $key ] = true; $existing[] = $item; ++$injected; } if ( $injected > 0 ) { update_post_meta( $post_id, '_aeolynx_faq_items', array_values( $existing ) ); } AEOLynx_Activity_Log::record_event( 'xray', 'Answer-Gap Auto-Injector', get_the_title( $post ), sprintf( /* translators: 1: Number of FAQs injected, 2: Post ID */ __( 'Injected %1$d new Answer-Gap FAQPage nodes into Post #%2$d (duplicates skipped)', 'aeolynx-for-webmcp' ), $injected, $post_id ) ); $report = AEOLynx_Activity_Log::run_post_xray_audit( $post ); $report['injected'] = $injected; $report['skipped'] = $skipped; if ( 0 === $injected && $skipped > 0 ) { $report['inject_notice'] = __( 'Placeholder Answer-Gap hints were not injected. Edit each answer into a real factual reply (40+ characters), then inject again.', 'aeolynx-for-webmcp' ); } wp_send_json_success( $report ); } /** * AJAX handler: Inspect the live output of /llms.txt, /llms-full.txt, or /.well-known/mcp.json. * * @return void */ public static function ajax_inspect_endpoint() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } self::guard_ajax( 'inspect', 30 ); $endpoint = isset( $_POST['endpoint'] ) ? sanitize_key( wp_unslash( $_POST['endpoint'] ) ) : 'llmstxt'; if ( 'llmsfull' === $endpoint ) { $output = AEOLynx_LlmsTxt::generate_full_content(); $label = home_url( '/llms-full.txt' ); } elseif ( 'wellknown' === $endpoint ) { $manifest = AEOLynx_Well_Known::build_mcp_payload(); $output = (string) wp_json_encode( $manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); $label = home_url( '/.well-known/mcp.json' ); } else { $output = AEOLynx_LlmsTxt::generate_summary_content(); $label = home_url( '/llms.txt' ); } wp_send_json_success( array( 'label' => $label, 'output' => $output, ) ); } /** * AJAX handler: Clear the local AI Activity Log. * * @return void */ public static function ajax_clear_activity_log() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } delete_option( 'aeolynx_activity_events' ); delete_option( 'aeolynx_activity_counters' ); wp_send_json_success( array( 'cleared' => true ) ); } /** * AJAX: generate a new MCP API key (raw key is returned exactly once). * * @return void */ /** * AJAX: run the local Schema.org validation on the homepage graph and the * latest published post's graph; returns per-context pass/error lists. * * @return void */ public static function ajax_validate_schema() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } self::guard_ajax( 'validate', 20 ); $latest = get_posts( array( 'post_type' => array( 'post', 'page' ), 'post_status' => 'publish', 'posts_per_page' => 1, 'no_found_rows' => true, ) ); $latest_id = ! empty( $latest[0] ) ? (int) $latest[0]->ID : 0; $contexts = array( 'homepage' => AEOLynx_Schema_Graph::generate( 0, 'homepage' ), ); if ( $latest_id > 0 ) { $contexts[ 'post_' . $latest_id ] = AEOLynx_Schema_Graph::generate( $latest_id, 'single' ); } $results = array(); foreach ( $contexts as $label => $graph ) { $results[ $label ] = AEOLynx_Schema_Graph::validate_graph( $graph ); } wp_send_json_success( array( 'results' => $results ) ); } public static function ajax_generate_api_key() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } self::guard_ajax( 'keys', 20 ); $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : ''; $raw = AEOLynx_REST::generate_api_key( $label ); if ( is_wp_error( $raw ) ) { wp_send_json_error( array( 'error' => $raw->get_error_message() ), 400 ); } wp_send_json_success( array( 'key' => $raw, 'label' => $label, ) ); } /** * AJAX: revoke a stored MCP API key by hash. * * @return void */ public static function ajax_revoke_api_key() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } self::guard_ajax( 'keys', 20 ); $hash = isset( $_POST['hash'] ) ? sanitize_text_field( wp_unslash( $_POST['hash'] ) ) : ''; if ( '' === $hash || ! preg_match( '/^[a-f0-9]{64}$/i', $hash ) ) { wp_send_json_error( array( 'error' => __( 'Invalid key identifier.', 'aeolynx-for-webmcp' ) ), 400 ); } $revoked = AEOLynx_REST::revoke_api_key( $hash ); if ( ! $revoked ) { wp_send_json_error( array( 'error' => __( 'Key not found (it may already be revoked).', 'aeolynx-for-webmcp' ) ), 404 ); } wp_send_json_success( array( 'revoked' => true ) ); } /** * AJAX handler: Return current activity events + counters for the live feed. * * @return void */ public static function ajax_get_activity() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } wp_send_json_success( array( 'events' => AEOLynx_Activity_Log::get_events(), 'counters' => AEOLynx_Activity_Log::get_counters(), ) ); } /** * AJAX handler: Search published content for the post picker combobox. * * @return void */ public static function ajax_search_posts() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } $search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : ''; $post_types = array( 'post', 'page' ); if ( post_type_exists( 'product' ) ) { $post_types[] = 'product'; } $query_args = array( 'post_type' => $post_types, 'post_status' => 'publish', 'posts_per_page' => 20, 'orderby' => 'modified', 'order' => 'DESC', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ); if ( '' !== $search ) { $query_args['s'] = $search; } $posts = get_posts( $query_args ); $results = array(); foreach ( $posts as $p ) { $results[] = array( 'id' => (int) $p->ID, 'title' => html_entity_decode( get_the_title( $p ), ENT_QUOTES, 'UTF-8' ), 'type' => (string) $p->post_type, ); } wp_send_json_success( $results ); } /** * AJAX handler: Re-mine buyer-intent AI search prompts locally from public site content (v1.5.1). * * @return void */ public static function ajax_mine_local_prompts() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } self::guard_ajax( 'mine', 15 ); $mined = AEOLynx_Prompt_Miner::mine( 16 ); wp_send_json_success( $mined ); } /** * AJAX handler: Permanently dismiss the in-dashboard release announcement for the current user. * * @return void */ public static function ajax_dismiss_release() { check_ajax_referer( 'aeolynx_sandbox_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'error' => __( 'Unauthorized access.', 'aeolynx-for-webmcp' ) ), 403 ); } $release = isset( $_POST['release'] ) ? sanitize_text_field( wp_unslash( $_POST['release'] ) ) : ''; if ( '' === $release ) { $release = defined( 'AEOLYNX_CORE_VERSION' ) ? (string) AEOLYNX_CORE_VERSION : '1.5.2'; } // Keep the stored tag short and strictly version-like. $release = substr( preg_replace( '/[^0-9A-Za-z._-]/', '', $release ), 0, 24 ); update_user_meta( get_current_user_id(), 'aeolynx_dismissed_release', $release ); wp_send_json_success( array( 'dismissed' => $release ) ); } } }