10, 'redirection' => 3, ) ); if ( is_wp_error( $response ) ) { return 'unknown'; } $status = (int) wp_remote_retrieve_response_code( $response ); if ( $status >= 200 && $status < 400 ) { return 'present'; } if ( 404 === $status ) { return 'absent'; } return 'unknown'; } /** * Return the aggregated site context used by the assistant. * * @param WP_REST_Request $request Current REST request. */ public function get_context( WP_REST_Request $request ): WP_REST_Response { $request->get_route(); if ( ! $this->settings->get_enable_assistant() ) { return new WP_REST_Response( array( 'error' => 'assistant_disabled', 'message' => __( 'Enable ActionPanel AI in settings to load site context.', 'actionpanel-ai' ), 'settings' => array( 'assistantEnabled' => false, ), ), 403 ); } try { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } global $wpdb; $generated_at = time(); // -------- site -------- $site = array( 'wp' => get_bloginfo( 'version' ), 'php' => phpversion(), 'multisite' => is_multisite(), 'locale' => get_locale(), 'permalink' => (string) get_option( 'permalink_structure' ), 'blog_public' => (int) get_option( 'blog_public', 1 ), 'siteurl' => get_option( 'siteurl' ), 'home' => home_url(), 'site_name' => get_option( 'blogname' ), 'tagline' => get_option( 'blogdescription' ), ); // -------- homepage (static only) -------- $homepage = array( 'url' => home_url(), 'type' => get_option( 'show_on_front' ) === 'page' ? 'static' : 'posts', 'title' => null, 'last_modified' => null, ); if ( get_option( 'show_on_front' ) === 'page' ) { $front_id = (int) get_option( 'page_on_front' ); if ( $front_id ) { $post = get_post( $front_id ); if ( $post ) { $homepage['title'] = get_the_title( $post ); $homepage['last_modified'] = get_post_modified_time( 'c', true, $post ); $homepage['url'] = get_permalink( $post ); } } } // -------- env -------- $env = array( 'web_server' => isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( (string) $_SERVER['SERVER_SOFTWARE'] ) ) : '', 'db' => method_exists( $wpdb, 'db_version' ) ? trim( (string) $wpdb->db_version() ) : '', 'php_sapi' => PHP_SAPI, 'opcache_enabled' => function_exists( 'opcache_get_status' ) ? (bool) opcache_get_status( false ) : false, 'hosting' => $this->host_detector->detect(), 'ext_core' => array_values( array_filter( array( extension_loaded( 'curl' ) ? 'curl' : null, extension_loaded( 'gd' ) ? 'gd' : null, extension_loaded( 'intl' ) ? 'intl' : null, extension_loaded( 'mbstring' ) ? 'mbstring' : null, extension_loaded( 'mysqli' ) ? 'mysqli' : null, extension_loaded( 'zip' ) ? 'zip' : null, ) ) ), ); // -------- theme -------- $theme = wp_get_theme(); $parent = $theme->parent(); $supports = array( 'align_wide' => current_theme_supports( 'align-wide' ), 'editor_styles' => current_theme_supports( 'editor-styles' ), 'responsive_embeds' => current_theme_supports( 'responsive-embeds' ), 'custom_spacing' => current_theme_supports( 'custom-spacing' ), 'custom_line_height' => current_theme_supports( 'custom-line-height' ), 'link_color' => current_theme_supports( 'link-color' ), ); $palette_count = 0; $font_count = 0; if ( function_exists( 'wp_get_global_settings' ) ) { $settings = wp_get_global_settings(); if ( ! is_wp_error( $settings ) && is_array( $settings ) ) { if ( isset( $settings['color']['palette'] ) && is_array( $settings['color']['palette'] ) ) { $palette_count = count( $settings['color']['palette'] ); } if ( isset( $settings['typography']['fontFamilies'] ) && is_array( $settings['typography']['fontFamilies'] ) ) { $font_count = count( $settings['typography']['fontFamilies'] ); } } } $theme_ctx = array( 'active' => array( 'name' => $theme->get( 'Name' ), 'ver' => $theme->get( 'Version' ), 'is_child' => (bool) $parent, ), 'parent' => $parent ? array( 'name' => $parent->get( 'Name' ), 'ver' => $parent->get( 'Version' ), ) : null, 'supports' => $supports, 'theme_json' => array( 'color_palette_count' => $palette_count, 'font_families_count' => $font_count, ), ); // -------- plugins summary -------- $all_plugins = get_plugins(); $active_slugs = (array) get_option( 'active_plugins', array() ); $updates = get_site_transient( 'update_plugins' ); $updates_map = isset( $updates->response ) && is_array( $updates->response ) ? $updates->response : array(); $updates_list = array(); $updates_count = 0; foreach ( $updates_map as $plugin_file => $obj ) { ++$updates_count; $updates_list[] = array( 'slug' => $plugin_file, 'current' => $all_plugins[ $plugin_file ]['Version'] ?? null, 'latest' => $obj->new_version ?? null, 'age_days' => null, ); if ( count( $updates_list ) >= 5 ) { break; } } $top_outdated = array_slice( $updates_list, 0, 5 ); $plugins_ctx = array( 'counts' => array( 'installed' => count( $all_plugins ), 'active' => count( $active_slugs ), 'updates' => $updates_count, ), 'active_slugs' => array_values( array_map( 'strval', $active_slugs ) ), 'top_outdated' => $top_outdated, ); // -------- health (light) -------- $health = array( 'source' => 'lightweight_context_snapshot', 'summary_note' => 'Lightweight context snapshot only. Use read_site(resource="site_health") for the full WordPress Site Health report.', 'issues' => array(), ); if ( $updates_count > 0 ) { $health['issues'][] = array( 'status' => 'recommended', 'label' => 'Plugin updates available', 'id' => 'plugin_updates', ); } // -------- perf -------- $object_cache_enabled = function_exists( 'wp_using_ext_object_cache' ) ? (bool) wp_using_ext_object_cache() : false; $object_cache_backend = null; if ( $object_cache_enabled ) { global $wp_object_cache; if ( is_object( $wp_object_cache ) ) { $cls = strtolower( get_class( $wp_object_cache ) ); if ( strpos( $cls, 'redis' ) !== false ) { $object_cache_backend = 'redis'; } elseif ( strpos( $cls, 'memcached' ) !== false ) { $object_cache_backend = 'memcached'; } else { $object_cache_backend = 'external'; } } } $page_cache = Diagnostics::detect_page_cache( $active_slugs, $env['hosting'] ); $autoload_kb = null; $autoload_top = array(); $alloptions = wp_load_alloptions(); if ( is_array( $alloptions ) ) { $autoload_rows = array(); $autoload_sum = 0; foreach ( $alloptions as $option_name => $option_value ) { $bytes = strlen( maybe_serialize( $option_value ) ); $autoload_rows[] = array( 'option_name' => (string) $option_name, 'bytes' => $bytes, ); $autoload_sum += $bytes; } usort( $autoload_rows, static fn( array $a, array $b ): int => (int) $b['bytes'] <=> (int) $a['bytes'] ); $autoload_kb = (int) round( $autoload_sum / 1024 ); foreach ( array_slice( $autoload_rows, 0, 5 ) as $row ) { $row_kb = (int) round( (int) $row['bytes'] / 1024 ); if ( $row_kb <= 0 ) { continue; } $autoload_top[] = array( 'name' => $row['option_name'], 'kb' => $row_kb, ); } } $perf = array( 'memory_limit' => ini_get( 'memory_limit' ), 'max_exec' => (int) ini_get( 'max_execution_time' ), 'cache' => array( 'page' => (bool) $page_cache['enabled'], 'object' => $object_cache_enabled, ), 'object_cache' => array( 'enabled' => $object_cache_enabled, 'backend' => $object_cache_backend, ), 'page_cache' => $page_cache, 'autoload' => array( 'kb' => $autoload_kb, 'top' => $autoload_top, ), ); // -------- cpt/tax -------- $cpt_public = array(); foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $pt ) { $counts = wp_count_posts( $pt->name ); if ( is_wp_error( $counts ) ) { continue; } $published = 0; if ( is_object( $counts ) ) { $published = isset( $counts->publish ) ? (int) $counts->publish : array_sum( array_map( 'intval', (array) $counts ) ); } elseif ( is_numeric( $counts ) ) { $published = (int) $counts; } $cpt_public[] = array( 'type' => $pt->name, 'count' => $published, ); } usort( $cpt_public, static fn( array $a, array $b ): int => (int) $b['count'] <=> (int) $a['count'] ); $cpt_public_sampled = count( $cpt_public ) > 5; $cpt_public = array_slice( $cpt_public, 0, 5 ); $taxonomies = array(); foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $tx ) { $terms_count = wp_count_terms( $tx->name ); if ( is_wp_error( $terms_count ) ) { continue; } $taxonomies[] = array( 'taxonomy' => $tx->name, 'terms' => (int) $terms_count, ); } usort( $taxonomies, static fn( array $a, array $b ): int => (int) $b['terms'] <=> (int) $a['terms'] ); $taxonomies_sampled = count( $taxonomies ) > 5; $taxonomies = array_slice( $taxonomies, 0, 5 ); $cpt_tax = array( 'cpt_public' => $cpt_public, 'taxonomies' => $taxonomies, 'sampled' => array( 'cpt_public' => $cpt_public_sampled, 'taxonomies' => $taxonomies_sampled, ), ); // -------- menus (light) -------- $menus_ctx = array( 'locations' => array(), 'orphan_menus_count' => 0, ); if ( function_exists( 'get_nav_menu_locations' ) ) { $locs = get_nav_menu_locations(); if ( is_array( $locs ) ) { foreach ( $locs as $loc => $menu_id ) { if ( $menu_id ) { $items = function_exists( 'wp_get_nav_menu_items' ) ? wp_get_nav_menu_items( $menu_id ) : array(); if ( is_wp_error( $items ) ) { $items = array(); } $menus_ctx['locations'][] = array( 'location' => $loc, 'items' => is_array( $items ) ? count( $items ) : 0, ); if ( count( $menus_ctx['locations'] ) >= 3 ) { break; } } } $all_menus = function_exists( 'wp_get_nav_menus' ) ? wp_get_nav_menus() : array(); if ( is_wp_error( $all_menus ) ) { $all_menus = array(); } $used = array_values( array_filter( array_values( $locs ) ) ); $menus_ctx['orphan_menus_count'] = max( 0, ( is_array( $all_menus ) ? count( $all_menus ) : 0 ) - count( $used ) ); } } // -------- seo -------- $seo = array( 'robots_txt_file' => $this->get_robots_txt_status(), 'blog_public' => (int) get_option( 'blog_public', 1 ), ); // -------- cron -------- $cron = array( 'next_soon' => array(), 'overdue_count' => 0, ); $ca = _get_cron_array(); if ( is_array( $ca ) ) { $now = time(); $added = 0; foreach ( $ca as $ts => $hooks ) { foreach ( $hooks as $hook => $events ) { $due_in = (int) $ts - $now; if ( $due_in < 0 ) { ++$cron['overdue_count']; } if ( $added < 3 && $due_in >= 0 ) { $schedule = null; foreach ( $events as $sig => $evt ) { $schedule = $evt['schedule'] ?? null; break; } $cron['next_soon'][] = array( 'hook' => $hook, 'due_in_s' => $due_in, 'schedule' => $schedule, 'ts' => (int) $ts, 'ts_iso' => $this->timestamp_to_iso( (int) $ts ), ); ++$added; } } } } // -------- rest -------- $rest_ctx = array( 'routes_count' => 0, 'prefixes' => array(), 'has_wp_v2' => false, ); if ( function_exists( 'rest_get_server' ) ) { $routes = rest_get_server()->get_routes(); if ( is_array( $routes ) ) { $rest_ctx = Diagnostics::summarize_rest_routes( array_keys( $routes ), 10, 12, 'raw_registered_routes' ); $rest_ctx['summary_note'] = 'This summary is based on raw registered routes. read_site(resource="rest") uses flattened non-sensitive routes, so counts may differ.'; } } // -------- options (allowlist) -------- $options = array( 'timezone_string' => get_option( 'timezone_string' ), 'users_can_register' => (bool) get_option( 'users_can_register' ), 'default_comment_status' => get_option( 'default_comment_status' ), ); // -------- security flags -------- $security_flags = array( 'disallow_file_edit' => defined( 'DISALLOW_FILE_EDIT' ) ? (bool) DISALLOW_FILE_EDIT : false, 'auto_updates_core' => (bool) get_site_option( 'auto_update_core', true ), 'force_ssl_admin' => defined( 'FORCE_SSL_ADMIN' ) ? (bool) FORCE_SSL_ADMIN : false, ); // -------- woo (light) -------- $woo = null; if ( class_exists( 'WooCommerce' ) ) { $woo_pages = array( 'shop' => function_exists( 'wc_get_page_id' ) ? (int) wc_get_page_id( 'shop' ) : 0, 'cart' => function_exists( 'wc_get_page_id' ) ? (int) wc_get_page_id( 'cart' ) : 0, 'checkout' => function_exists( 'wc_get_page_id' ) ? (int) wc_get_page_id( 'checkout' ) : 0, 'myaccount' => function_exists( 'wc_get_page_id' ) ? (int) wc_get_page_id( 'myaccount' ) : 0, ); $woo = array( 'version' => defined( 'WC_VERSION' ) ? WC_VERSION : null, 'core_pages' => $woo_pages, 'core_pages_configured' => count( array_filter( $woo_pages, static fn( int $page_id ): bool => $page_id > 0 ) ) === count( $woo_pages ), ); } // -------- alerts & hints -------- $alerts = array(); // 1) Autoload bloat if ( is_int( $autoload_kb ) && $autoload_kb > 800 ) { $alerts[] = array( 'code' => 'AUTOLOAD_BLOAT', 'severity' => 'high', 'message' => sprintf( 'Autoloaded options total ~%sKB (target < 800KB).', (int) $autoload_kb ), 'evidence' => array( 'total_kb' => (int) $autoload_kb ), ); } // 2) Cron overdue if ( ! empty( $cron['overdue_count'] ) ) { $alerts[] = array( 'code' => 'CRON_OVERDUE', 'severity' => 'medium', 'message' => sprintf( 'WP-Cron shows %d overdue task(s).', (int) $cron['overdue_count'] ), 'evidence' => array( 'overdue_count' => (int) $cron['overdue_count'] ), ); } // 3) Mixed-content / URL scheme mismatch hint. $home_scheme = wp_parse_url( (string) $site['home'], PHP_URL_SCHEME ); $siteurl_scheme = wp_parse_url( (string) $site['siteurl'], PHP_URL_SCHEME ); if ( $home_scheme && $siteurl_scheme && ( $home_scheme !== $siteurl_scheme || 'https' !== $home_scheme || 'https' !== $siteurl_scheme ) ) { $alerts[] = array( 'code' => 'SITE_URL_SCHEME_ISSUE', 'severity' => 'low', 'message' => 'Site/home URLs are not both HTTPS or use mismatched schemes.', 'evidence' => array( 'home' => (string) $site['home'], 'siteurl' => (string) $site['siteurl'], ), ); } // 4) Page cache absent if ( empty( $page_cache['enabled'] ) ) { $alerts[] = array( 'code' => 'PAGE_CACHE_ABSENT', 'severity' => 'medium', 'message' => 'No page cache layer was detected from plugins or known managed-host signals.', 'evidence' => array( 'enabled' => (bool) $page_cache['enabled'], 'source' => $page_cache['source'], 'provider' => $page_cache['provider'], ), ); } // 5) Object cache disabled if ( empty( $object_cache_enabled ) ) { $alerts[] = array( 'code' => 'OBJECT_CACHE_DISABLED', 'severity' => 'low', 'message' => 'Object cache appears disabled.', 'evidence' => array( 'enabled' => (bool) $object_cache_enabled, 'backend' => $object_cache_backend, ), ); } // 6) REST core routes missing/suspicious if ( empty( $rest_ctx['routes_count'] ) || empty( $rest_ctx['has_wp_v2'] ) ) { $alerts[] = array( 'code' => 'REST_CORE_MISSING', 'severity' => 'high', 'message' => 'Core REST namespace not detected (wp/v2).', 'evidence' => array( 'routes_count' => (int) $rest_ctx['routes_count'], 'prefixes' => (array) $rest_ctx['prefixes'], 'has_wp_v2' => (bool) $rest_ctx['has_wp_v2'], ), ); } // 7) Permalinks plain if ( empty( $site['permalink'] ) ) { $alerts[] = array( 'code' => 'PERMALINK_PLAIN', 'severity' => 'medium', 'message' => 'Permalink structure is Plain. REST and SEO features may be limited.', 'evidence' => array( 'permalink' => '' ), ); } // 8) Plugin updates available if ( ! empty( $plugins_ctx['counts']['updates'] ) ) { $alerts[] = array( 'code' => 'PLUGIN_UPDATES', 'severity' => 'info', 'message' => sprintf( '%d plugin(s) have updates available.', (int) $plugins_ctx['counts']['updates'] ), 'evidence' => array( 'updates_count' => (int) $plugins_ctx['counts']['updates'] ), ); } // 9) Search engine visibility off. if ( isset( $site['blog_public'] ) && 0 === (int) $site['blog_public'] ) { $alerts[] = array( 'code' => 'SEARCH_VISIBILITY_OFF', 'severity' => 'low', 'message' => '“Discourage search engines” is enabled.', 'evidence' => array( 'blog_public' => (int) $site['blog_public'] ), ); } $context = array( 'site' => $site, 'homepage' => $homepage, 'env' => $env, 'theme' => $theme_ctx, 'plugins' => $plugins_ctx, 'health' => $health, 'perf' => $perf, 'cpt_tax' => $cpt_tax, 'menus' => $menus_ctx, 'seo' => $seo, 'cron' => $cron, 'rest' => $rest_ctx, 'options' => $options, 'security_flags' => $security_flags, 'woo' => $woo, 'alerts' => $alerts, ); $digest = 'sha256:' . hash( 'sha256', wp_json_encode( $context ) ); $payload = array( 'meta' => array( 'generated_at' => $generated_at, 'generated_at_iso' => $this->timestamp_to_iso( $generated_at ), 'digest' => $digest, ), ) + $context; return new WP_REST_Response( $payload ); } catch ( \Throwable $e ) { return new WP_REST_Response( array( 'error' => 'server_error', 'message' => 'Failed to generate diagnostics context.', ), 500 ); } } }