File: /wordpress/plugins/wp-cloud-client/1.1.1/src/SiteOptions/OptionRegistry.php
<?php
declare(strict_types=1);
namespace VPlugins\WPCloudClient\SiteOptions;
use VPlugins\WPCloudClient\Integration\DiviHelper;
use VPlugins\WPCloudClient\Integration\WooCommerceHelper;
final class OptionRegistry {
/**
* Registry of available site options.
*
* @var array<string, array{reader: callable, writer: callable|null}>
*/
private array $registry;
/**
* Initialize the option registry with default options.
*/
public function __construct() {
$this->registry = [
'edit_url' => [
'reader' => static function (): string {
if ( DiviHelper::isActive() ) {
return site_url( '/' ) . '?et_fb=1&PageSpeed=off';
}
return site_url( '/wp-admin/edit.php' ) . '?post_type=page';
},
'writer' => null,
],
'business_profile' => [
'reader' => static fn (): mixed => get_option( 'bpr_business_profile', null ),
'writer' => static function ( mixed $value ): void {
update_option( 'bpr_business_profile', $value );
},
],
'home_url' => [
'reader' => static fn (): mixed => get_option( 'home', '' ),
'writer' => static function ( mixed $value ): void {
update_option( 'home', $value );
},
],
'site_url' => [
'reader' => static fn (): mixed => get_option( 'siteurl', '' ),
'writer' => static function ( mixed $value ): void {
update_option( 'siteurl', $value );
},
],
'site_title' => [
'reader' => static fn (): mixed => get_option( 'blogname', '' ),
'writer' => static function ( mixed $value ): void {
update_option( 'blogname', $value );
},
],
'site_tagline' => [
'reader' => static fn (): mixed => get_option( 'blogdescription', '' ),
'writer' => static function ( mixed $value ): void {
update_option( 'blogdescription', $value );
},
],
'admin_email' => [
'reader' => static fn (): mixed => get_option( 'admin_email', '' ),
'writer' => static function ( mixed $value ): void {
update_option( 'admin_email', $value );
},
],
/*
* timezone: expects an IANA timezone identifier (e.g. "America/New_York").
* Reader returns '' for sites configured with a manual UTC offset — WordPress
* stores those in gmt_offset and leaves timezone_string empty.
*/
'timezone' => [
'reader' => static fn (): mixed => get_option( 'timezone_string', '' ),
'writer' => static function ( mixed $value ): void {
update_option( 'timezone_string', $value );
},
],
'is_ecommerce' => [
'reader' => static fn (): bool => WooCommerceHelper::isActive(),
'writer' => null,
],
'updates' => [
'reader' => static function (): array {
if ( ! function_exists( 'get_core_updates' ) ) {
require_once ABSPATH . 'wp-admin/includes/update.php';
}
wp_update_plugins();
wp_update_themes();
wp_version_check();
$numPluginUpdates = 0;
$currentPlugins = get_site_transient( 'update_plugins' );
if ( isset( $currentPlugins->response ) ) {
$numPluginUpdates = count( $currentPlugins->response );
}
$numThemeUpdates = 0;
$currentThemes = get_site_transient( 'update_themes' );
if ( isset( $currentThemes->response ) ) {
$numThemeUpdates = count( $currentThemes->response );
}
$coreCurrentVersion = get_bloginfo( 'version' );
$coreLatestVersion = $coreCurrentVersion;
$coreUpdates = get_core_updates();
if ( ! empty( $coreUpdates ) && isset( $coreUpdates[0]->response ) && 'upgrade' === $coreUpdates[0]->response ) {
$coreLatestVersion = $coreUpdates[0]->current;
}
return [
'php_version' => phpversion(),
'core_version' => $coreCurrentVersion,
'latest_core_version' => $coreLatestVersion,
'num_of_plugin_updates' => $numPluginUpdates,
'num_of_theme_updates' => $numThemeUpdates,
];
},
'writer' => null,
],
];
}
/**
* Read the specified option keys.
*
* @param string[] $keys The option keys to read.
* @return array<string, mixed>
*/
public function read( array $keys ): array {
$values = [];
foreach ( $keys as $key ) {
if ( ! isset( $this->registry[ $key ] ) ) {
continue;
}
$values[ $key ] = ( $this->registry[ $key ]['reader'] )();
}
return $values;
}
/**
* Write the specified option key-value pairs.
*
* @param array<string, mixed> $options The option key-value pairs to write.
* @return array<string, mixed>
* @throws \InvalidArgumentException When attempting to write a read-only option.
*/
public function write( array $options ): array {
$written = [];
foreach ( $options as $key => $value ) {
if ( ! isset( $this->registry[ $key ] ) ) {
continue;
}
if ( null === $this->registry[ $key ]['writer'] ) {
throw new \InvalidArgumentException(
sprintf( 'Option "%s" is read-only', $key ),
);
}
( $this->registry[ $key ]['writer'] )( $value );
$written[ $key ] = $value;
}
return $written;
}
}