| 1 |
<?php |
| 2 |
/** |
| 3 |
* The provider responsible for registering Config classes with the container & WordPress. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Config; |
| 11 |
|
| 12 |
use SolidWP\Performance\Contracts\Service_Provider; |
| 13 |
use SolidWP\Performance\StellarWP\Arrays\Arr; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* The class for registering Config classes with the container & WordPress. |
| 21 |
* |
| 22 |
* @since 0.1.0 |
| 23 |
* |
| 24 |
* @package SolidWP\Performance |
| 25 |
*/ |
| 26 |
class Provider extends Service_Provider { |
| 27 |
|
| 28 |
/** |
| 29 |
* {@inheritdoc} |
| 30 |
*/ |
| 31 |
public function register(): void { |
| 32 |
$this->container->when( Advanced_Cache::class ) |
| 33 |
->needs( '$template_file' ) |
| 34 |
->give( '/src/views/cache/advanced-cache.php' ); |
| 35 |
|
| 36 |
swpsp_register_config(); |
| 37 |
|
| 38 |
$this->register_actions(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* {@inheritdoc} |
| 43 |
*/ |
| 44 |
public function register_actions(): void { |
| 45 |
$option_key = Config::OPTION_KEY; |
| 46 |
|
| 47 |
add_action( "update_option_{$option_key}", [ $this, 'save_via_config' ], 10, 2 ); |
| 48 |
add_action( "update_site_option_{$option_key}", [ $this, 'save_via_config' ], 10, 2 ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Saves the option updates using the Config architecture. |
| 53 |
* |
| 54 |
* @since 0.1.0 |
| 55 |
* |
| 56 |
* @param mixed $old_value The value being updated. |
| 57 |
* @param mixed $new_value The new values to update. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function save_via_config( $old_value, $new_value ) { |
| 62 |
$changes = Arr::dot( $new_value ); |
| 63 |
|
| 64 |
foreach ( $changes as $key => $value ) { |
| 65 |
$this->container->get( Config::class )->set( $key, $value ); |
| 66 |
} |
| 67 |
|
| 68 |
$this->container->get( Config::class )->save(); |
| 69 |
} |
| 70 |
} |
| 71 |
|