*/ private readonly \Closure $redis_config; /** * @param DropinInstaller $dropin object-cache.php. * @param RedisProbe $probe Connection check. * @param string $dropin_source Drop-in source. * @param callable(Settings): array $redis_config Compiles the runtime "redis" section. */ public function __construct( private readonly DropinInstaller $dropin, private readonly RedisProbe $probe, private readonly string $dropin_source, callable $redis_config, ) { $this->redis_config = \Closure::fromCallable( $redis_config ); } /** * {@inheritDoc} * * @param Settings $settings Settings. * @param bool $replace_foreign Replace a foreign drop-in. */ public function apply( Settings $settings, bool $replace_foreign = false ): array { if ( ! $settings->get( 'redis_enabled' ) ) { return $this->remove(); } $config = ( $this->redis_config )( $settings ); $result = $this->probe->check( $config ); if ( ! $result->ok ) { $this->remove(); return [ new Problem( 'redis_unavailable', 'Object cache is off because Redis is unreachable: ' . $result->message ) ]; } $was_ours = DropinStatus::Ours === $this->dropin->status(); try { $this->dropin->install( $this->dropin_source, $replace_foreign ); } catch ( ForeignDropinException ) { return [ new Problem( 'object_cache_foreign', 'Another plugin owns wp-content/object-cache.php. Replace it to use the AKantio object cache.' ) ]; } catch ( \Throwable ) { return [ new Problem( 'object_cache_unwritable', 'wp-content/object-cache.php could not be written. Check file permissions.' ) ]; } if ( ! $was_ours ) { try { $this->probe->flush( $config ); } catch ( \Throwable ) { return [ new Problem( 'redis_unavailable', 'The object cache was enabled but stale data could not be flushed. Flush it from the Redis screen.' ) ]; } } return []; } /** * {@inheritDoc} */ public function remove(): array { try { $this->dropin->uninstall(); } catch ( \Throwable ) { return [ new Problem( 'object_cache_unwritable', 'wp-content/object-cache.php could not be removed.' ) ]; } return []; } }