self::check_server_environment(), 'compression' => self::check_compression(), 'opcache' => self::check_opcache(), 'object_cache' => self::check_object_cache(), 'cache_dir' => self::check_cache_directory(), 'homepage' => self::check_homepage_performance(), ); return $results; } /** * Server environment inspection. * * @return array */ public static function check_server_environment() { $server_software = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : 'Unknown'; $server_type = 'Unknown'; if ( stripos( $server_software, 'litespeed' ) !== false ) { $server_type = 'LiteSpeed'; } elseif ( stripos( $server_software, 'openlitespeed' ) !== false ) { $server_type = 'OpenLiteSpeed'; } elseif ( stripos( $server_software, 'nginx' ) !== false ) { $server_type = 'NGINX'; } elseif ( stripos( $server_software, 'apache' ) !== false ) { $server_type = 'Apache'; } return array( 'server_software' => $server_software, 'server_type' => $server_type, 'php_version' => PHP_VERSION, 'php_sapi' => PHP_SAPI, 'memory_limit' => ini_get( 'memory_limit' ), 'max_execution' => ini_get( 'max_execution_time' ) . 's', 'upload_max' => ini_get( 'upload_max_filesize' ), 'post_max' => ini_get( 'post_max_size' ), 'curl_version' => function_exists( 'curl_version' ) ? curl_version()['version'] : 'N/A', 'openssl' => defined( 'OPENSSL_VERSION_TEXT' ) ? OPENSSL_VERSION_TEXT : 'N/A', ); } /** * Check compression support (GZIP / Brotli). * * @return array */ public static function check_compression() { $gzip_supported = extension_loaded( 'zlib' ) && function_exists( 'gzencode' ); $brotli_supported = extension_loaded( 'brotli' ) || function_exists( 'brotli_compress' ); // Test actual HTTP response compression for home URL. $home_url = home_url( '/' ); $response = wp_remote_get( $home_url, array( 'headers' => array( 'Accept-Encoding' => 'gzip, br' ), 'sslverify' => false, 'timeout' => 8, ) ); $active_compression = 'none'; if ( ! is_wp_error( $response ) ) { $encoding = wp_remote_retrieve_header( $response, 'content-encoding' ); if ( stripos( $encoding, 'br' ) !== false ) { $active_compression = 'brotli'; } elseif ( stripos( $encoding, 'gzip' ) !== false ) { $active_compression = 'gzip'; } } return array( 'gzip_extension' => $gzip_supported, 'brotli_extension' => $brotli_supported, 'active_compression' => $active_compression, 'status' => ( 'none' !== $active_compression ), ); } /** * Check PHP OPcache status. * * @return array */ public static function check_opcache() { $enabled = function_exists( 'opcache_get_status' ) && ini_get( 'opcache.enable' ); $status = array( 'enabled' => (bool) $enabled, 'memory_used' => 'N/A', 'memory_free' => 'N/A', 'hit_rate' => 'N/A', 'can_reset' => function_exists( 'opcache_reset' ) && ! in_array( 'opcache_reset', explode( ',', ini_get( 'disable_functions' ) ), true ), ); if ( $enabled ) { $data = @opcache_get_status( false ); if ( is_array( $data ) && isset( $data['memory_usage'] ) ) { $status['memory_used'] = wpsco_format_size( $data['memory_usage']['used_memory'] ?? 0 ); $status['memory_free'] = wpsco_format_size( $data['memory_usage']['free_memory'] ?? 0 ); if ( isset( $data['opcache_statistics']['opcache_hit_rate'] ) ) { $status['hit_rate'] = round( $data['opcache_statistics']['opcache_hit_rate'], 1 ) . '%'; } } } return $status; } /** * Check Object Cache (Redis / Memcached). * * @return array */ public static function check_object_cache() { global $wp_object_cache; $has_redis = class_exists( 'Redis' ); $has_memcached = class_exists( 'Memcached' ) || class_exists( 'Memcache' ); $is_external = wp_using_ext_object_cache(); $engine = 'WordPress Internal (Non-persistent)'; if ( $is_external ) { if ( $has_redis && ( isset( $wp_object_cache->redis ) || class_exists( 'WP_Object_Cache' ) ) ) { $engine = 'Redis (Persistent)'; } elseif ( $has_memcached ) { $engine = 'Memcached (Persistent)'; } else { $engine = 'External Object Cache'; } } return array( 'is_persistent' => $is_external, 'engine' => $engine, 'redis_ext' => $has_redis, 'memcached_ext' => $has_memcached, ); } /** * Check cache directory health and permissions. * * @return array */ public static function check_cache_directory() { $dir = function_exists( 'wpsco_get_cache_dir' ) ? wpsco_get_cache_dir() : ( defined( 'WPSCO_CACHE_DIR' ) ? WPSCO_CACHE_DIR : '' ); $exists = is_dir( $dir ); $writable = $exists && wp_is_writable( $dir ); $size = $exists ? wpsco_get_dir_size( $dir ) : 0; return array( 'path' => $dir, 'exists' => $exists, 'writable' => $writable, 'size' => $size, 'formatted' => wpsco_format_size( $size ), ); } /** * Inspect homepage assets and delivery. * * @return array */ public static function check_homepage_performance() { $home_url = home_url( '/' ); $start_time = microtime( true ); $response = wp_remote_get( $home_url, array( 'sslverify' => false, 'timeout' => 10, ) ); $ttfb = round( ( microtime( true ) - $start_time ) * 1000 ); // ms if ( is_wp_error( $response ) ) { return array( 'status' => 'error', 'message' => $response->get_error_message(), 'ttfb' => $ttfb, ); } $code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $headers = wp_remote_retrieve_headers( $response ); // Analyze HTML content. // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -- Analyzing remote HTML markup, not enqueuing scripts. $css_links_match = preg_match_all( '/<' . 'link\b[^>]*\brel=[\'"]?stylesheet[\'"]?/i', $body ); $css_links = false !== $css_links_match ? (int) $css_links_match : 0; $js_scripts = substr_count( $body, ']*\b(width|height)=)[^>]*>/i', $body ); $cache_hit = ( isset( $headers['x-wp-speed-cache'] ) && 'HIT' === strtoupper( $headers['x-wp-speed-cache'] ) ) || ( strpos( $body, '