| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class for retrieving configuration values from a file. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
declare( strict_types=1 ); |
| 11 |
|
| 12 |
namespace SolidWP\Performance\Config\Strategies; |
| 13 |
|
| 14 |
use Closure; |
| 15 |
use SolidWP\Performance\Config\Config; |
| 16 |
use SolidWP\Performance\Config\Contracts\Strategy; |
| 17 |
use SolidWP\Performance\Config\Provider; |
| 18 |
use SolidWP\Performance\Container; |
| 19 |
use SolidWP\Performance\StellarWP\Arrays\Arr; |
| 20 |
|
| 21 |
/** |
| 22 |
* Stores and retrieves configuration values from a file. |
| 23 |
* |
| 24 |
* @since 0.1.0 |
| 25 |
* |
| 26 |
* @package SolidWP\Performance |
| 27 |
*/ |
| 28 |
class Network implements Strategy { |
| 29 |
|
| 30 |
/** |
| 31 |
* The full decoded configuration. |
| 32 |
* |
| 33 |
* @since 0.1.0 |
| 34 |
* |
| 35 |
* @var null|array<string,mixed> |
| 36 |
*/ |
| 37 |
private ?array $config = null; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var Container |
| 41 |
*/ |
| 42 |
private Container $container; |
| 43 |
|
| 44 |
/** |
| 45 |
* @param Container $container The container. |
| 46 |
*/ |
| 47 |
public function __construct( Container $container ) { |
| 48 |
$this->container = $container; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritdoc} |
| 53 |
*/ |
| 54 |
public function get( string $key, ?Closure $next = null ) { |
| 55 |
$config = $this->fetch(); |
| 56 |
$value = Arr::get( $config, explode( '.', $key ) ); |
| 57 |
|
| 58 |
if ( isset( $value ) ) { |
| 59 |
return $value; |
| 60 |
} |
| 61 |
|
| 62 |
return $next === null ? null : $next( $key ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* {@inheritdoc} |
| 67 |
*/ |
| 68 |
public function save( array $changes, ?Closure $next = null ) { |
| 69 |
// Filter the current changes down to only the ones this strategy is responsible for. |
| 70 |
$responsible_changes = array_filter( |
| 71 |
$changes, |
| 72 |
function ( $key ) { |
| 73 |
return $this->is_responsible_for( $key ); |
| 74 |
}, |
| 75 |
ARRAY_FILTER_USE_KEY |
| 76 |
); |
| 77 |
|
| 78 |
$expanded_changes = []; |
| 79 |
|
| 80 |
// Expand array back into multidimensional array. |
| 81 |
foreach ( $responsible_changes as $key => $value ) { |
| 82 |
$expanded_changes = Arr::set( $expanded_changes, explode( '.', $key ), $value ); |
| 83 |
} |
| 84 |
|
| 85 |
// Save the changes in one transaction. |
| 86 |
if ( count( $expanded_changes ) > 0 ) { |
| 87 |
$config = $this->fetch(); |
| 88 |
$updated_changes = array_merge( $config, Arr::undot( $expanded_changes ) ); |
| 89 |
|
| 90 |
$option_key = Config::OPTION_KEY; |
| 91 |
|
| 92 |
remove_action( "update_site_option_{$option_key}", $this->container->callback( Provider::class, 'save_via_config' ) ); |
| 93 |
update_network_option( get_main_network_id(), Config::OPTION_KEY, $updated_changes ); |
| 94 |
add_action( "update_site_option_{$option_key}", $this->container->callback( Provider::class, 'save_via_config' ) ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( $next !== null ) { |
| 98 |
return $next( $changes ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* {@inheritdoc} |
| 104 |
*/ |
| 105 |
public function is_responsible_for( string $option_key ): bool { |
| 106 |
// All keys should be stored in the options table. |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Fetches & caches the config from the options table. |
| 112 |
* |
| 113 |
* @return array |
| 114 |
*/ |
| 115 |
private function fetch(): array { |
| 116 |
if ( ! function_exists( 'get_network_option' ) ) { |
| 117 |
return []; |
| 118 |
} |
| 119 |
|
| 120 |
if ( $this->config === null ) { |
| 121 |
$this->config = get_network_option( get_main_network_id(), Config::OPTION_KEY, [] ); |
| 122 |
} |
| 123 |
|
| 124 |
return $this->config; |
| 125 |
} |
| 126 |
} |
| 127 |
|