Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.49% covered (warning)
79.49%
31 / 39
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ObjectCachePreflight
79.49% covered (warning)
79.49%
31 / 39
0.00% covered (danger)
0.00%
0 / 1
10.86
0.00% covered (danger)
0.00%
0 / 1
 snapshot
79.49% covered (warning)
79.49%
31 / 39
0.00% covered (danger)
0.00%
0 / 1
10.86
1<?php
2
3namespace NewfoldLabs\WP\Module\Performance\Cache\Types;
4
5use NewfoldLabs\WP\Module\Data\HiiveConnection;
6
7/**
8 * Read-only diagnostics for Redis object cache readiness.
9 *
10 * Must not perform remote Hiive/HUAPI HTTP calls; local Hiive connection state is allowed.
11 */
12final class ObjectCachePreflight {
13
14    /**
15     * Build a structured preflight snapshot for UI/REST.
16     *
17     * @param bool $include_live_ping When false, avoids connecting to Redis (settings endpoint safe).
18     * @return array<string, mixed>
19     */
20    public static function snapshot( $include_live_ping = false ) {
21        $extension_loaded = extension_loaded( 'redis' );
22        $configured       = ObjectCache::is_configured_in_wp_config();
23        $constants_now    = ObjectCache::constants_visible_this_request();
24
25        // Only resolve when provisioning could run (same path as enable() before HTTP); avoids loading Hiive when phpredis is missing.
26        $hiive_connected = false;
27        if ( $extension_loaded && ! $configured ) {
28            $hiive_connected = HiiveConnection::is_connected();
29        } elseif ( $extension_loaded && $configured ) {
30            $hiive_connected = true;
31        }
32
33        $ping_ok = null;
34        $code    = null;
35        $message = null;
36
37        if ( ! $extension_loaded ) {
38            $ping_ok = false;
39            $code    = ObjectCacheErrorCodes::PHPREDIS_MISSING;
40            $message = __( 'Object caching is not supported on this server.', 'wp-module-performance' );
41        } elseif ( ! $configured ) {
42            $ping_ok = false;
43            // Same order as RedisCredentialsProvisioner::provision_enable_redis_via_hosting_api() before HTTP.
44            if ( ! $hiive_connected ) {
45                $code    = ObjectCacheErrorCodes::HIIVE_NOT_CONNECTED;
46                $message = __(
47                    'Object cache cannot be enabled automatically right now.',
48                    'wp-module-performance'
49                );
50            }
51            // When wp-config lacks creds but Hiive is connected, enabling will provision creds via the
52            // hosting API — that's a normal "ready to enable" state, not an error worth surfacing in the UI.
53        } elseif ( $include_live_ping ) {
54            // Important: bootstrap WP_REDIS_* from wp-config when constants exist in file but aren't defined() yet.
55            ObjectCache::bootstrap_redis_connection_constants_for_preflight();
56
57            $ping    = PhpRedisPinger::ping();
58            $ping_ok = (bool) ( $ping['ok'] ?? false );
59            if ( ! $ping_ok ) {
60                $code    = ObjectCacheErrorCodes::REDIS_UNREACHABLE;
61                $message = __( 'Could not connect to the object cache.', 'wp-module-performance' );
62            }
63        }
64
65        return array(
66            'extensionLoaded'             => $extension_loaded,
67            'configuredInWpConfig'        => $configured,
68            'constantsVisibleThisRequest' => $constants_now,
69            'hiiveConnected'              => $hiive_connected,
70            'redisPingOk'                 => $ping_ok,
71            'preflightCode'               => $code,
72            'preflightMessage'            => $message,
73        );
74    }
75}