diff --git a/.gitignore b/.gitignore index f733455..af29aa3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock .php_cs.cache .phpunit.result.cache .php-cs-fixer.cache +.vscode diff --git a/CHANGELOG.md b/CHANGELOG.md index a9f2b60..4ef46ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C ## Unreleased -Nothing yet. +### Added + +- Added `when()` method to `Group` class to conditionally include features without using `Effect`. ## 3.0.0 diff --git a/docs/features.md b/docs/features.md index c1ea36d..c175ea6 100644 --- a/docs/features.md +++ b/docs/features.md @@ -92,6 +92,23 @@ $plugin->include( ), ); +// This can also be written using the when() helper. +$plugin->when( + when: fn () => get_current_blog_id() !== 1, + then: fn () => new Ordered( + first: new Library\Plugin_Loader( + plugins: [ + 'block-visibility/block-visibility.php', + ], + ), + then: new Group( + new Features\Block_Visibility_Settings(), + new Features\Block_Visibility_Custom_Conditions(), + ), + ), + ), +); + // Load the Google Tag Manager script on templates. $plugin->include( new Template_Feature( diff --git a/src/features/class-effect.php b/src/features/class-effect.php index ff86366..930269c 100644 --- a/src/features/class-effect.php +++ b/src/features/class-effect.php @@ -16,18 +16,18 @@ final class Effect implements Feature { /** * The condition to check. * - * @var callable + * @var bool|callable */ private $when; /** * Constructor. * - * @param callable $when The condition to check. - * @param Feature $then The feature to boot if the condition is met. + * @param bool|callable $when The condition to check. + * @param Feature $then The feature to boot if the condition is met. */ public function __construct( - callable $when, + bool|callable $when, private readonly Feature $then, ) { $this->when = $when; @@ -37,7 +37,9 @@ public function __construct( * Boot the feature. */ public function boot(): void { - if ( ( $this->when )() === true ) { + if ( is_callable( $this->when ) && ( $this->when )() === true ) { + $this->then->boot(); + } elseif ( is_bool( $this->when ) && true === $this->when ) { $this->then->boot(); } } diff --git a/src/features/class-group.php b/src/features/class-group.php index 7f747d2..9a43a4c 100644 --- a/src/features/class-group.php +++ b/src/features/class-group.php @@ -47,4 +47,15 @@ public function boot(): void { public function include( Feature ...$features ): void { array_push( $this->features, ...$features ); } + + /** + * Include a conditional feature. + * + * @param bool|callable $when The condition to check. + * @param Feature $then The feature to boot if the condition is met. + * @return void + */ + public function when( bool|callable $when, Feature $then ): void { + $this->include( new Effect( $when, $then ) ); + } } diff --git a/tests/Feature/.gitkeep b/tests/Feature/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/Feature/Features/GroupTest.php b/tests/Feature/Features/GroupTest.php new file mode 100644 index 0000000..e6056cb --- /dev/null +++ b/tests/Feature/Features/GroupTest.php @@ -0,0 +1,56 @@ + $_SERVER['booted_quick_feature'] = true ), + new class() implements Feature { + /** + * Boot the feature. + */ + public function boot(): void { + $_SERVER['booted_feature'] = true; + } + }, + new Group( + new Quick_Feature( fn () => $_SERVER['booted_group_of_groups'] = true ), + ), + ); + + $group->include( + new Quick_Feature( fn () => $_SERVER['included_feature'] = true ), + ); + + $group->boot(); + + $this->assertTrue( $_SERVER['booted_feature'] ); + $this->assertTrue( $_SERVER['booted_quick_feature'] ); + $this->assertTrue( $_SERVER['booted_group_of_groups'] ); + $this->assertTrue( $_SERVER['included_feature'] ); + } +} diff --git a/tests/Unit/.gitkeep b/tests/Unit/.gitkeep deleted file mode 100644 index e69de29..0000000