From f73e99bfe9204a8e3c8eb099be888b56a7ab6faf Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Thu, 5 Dec 2024 14:55:11 -0500 Subject: [PATCH 1/6] Include a conditional feature without having to instantiate Effect --- .gitignore | 1 + src/features/class-effect.php | 10 ++++++---- src/features/class-group.php | 11 +++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) 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/src/features/class-effect.php b/src/features/class-effect.php index ff86366..18b8823 100644 --- a/src/features/class-effect.php +++ b/src/features/class-effect.php @@ -23,11 +23,11 @@ final class Effect implements Feature { /** * 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_bool( $this->when ) && $this->when === true ) { + $this->then->boot(); + } elseif ( ( $this->when )() === true ) { $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 ) ); + } } From 6ee62905eac1ad89bd3f03d5a46843a8fede7f4c Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Thu, 5 Dec 2024 14:59:22 -0500 Subject: [PATCH 2/6] Linting fixes --- src/features/class-effect.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/features/class-effect.php b/src/features/class-effect.php index 18b8823..930269c 100644 --- a/src/features/class-effect.php +++ b/src/features/class-effect.php @@ -16,7 +16,7 @@ final class Effect implements Feature { /** * The condition to check. * - * @var callable + * @var bool|callable */ private $when; @@ -37,9 +37,9 @@ public function __construct( * Boot the feature. */ public function boot(): void { - if ( is_bool( $this->when ) && $this->when === true ) { + if ( is_callable( $this->when ) && ( $this->when )() === true ) { $this->then->boot(); - } elseif ( ( $this->when )() === true ) { + } elseif ( is_bool( $this->when ) && true === $this->when ) { $this->then->boot(); } } From da11bc275d3f0c8de4e35f965ca5d0acb2eefd39 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Thu, 5 Dec 2024 15:03:30 -0500 Subject: [PATCH 3/6] Documentation --- docs/features.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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( From c1112a41a0258932061d27eb81dd1e4c486fbbf9 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Thu, 5 Dec 2024 15:04:15 -0500 Subject: [PATCH 4/6] CHANGELOG --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 From c8348d0a3ca7f1e6f73afc01e603e2da33ad3a78 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Fri, 6 Dec 2024 13:26:52 -0500 Subject: [PATCH 5/6] Adding tests --- tests/Feature/.gitkeep | 0 tests/Feature/Features/GroupTest.php | 48 ++++++++++++++++++++++++++++ tests/Unit/.gitkeep | 0 3 files changed, 48 insertions(+) delete mode 100644 tests/Feature/.gitkeep create mode 100644 tests/Feature/Features/GroupTest.php delete mode 100644 tests/Unit/.gitkeep 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..050af6c --- /dev/null +++ b/tests/Feature/Features/GroupTest.php @@ -0,0 +1,48 @@ + $_SERVER['booted_quick_feature'] = true ), + new class implements 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 From 2d3e8d67a68766153040a28ddd3c5a55cd436b45 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Fri, 6 Dec 2024 13:28:25 -0500 Subject: [PATCH 6/6] phpcs fixes --- tests/Feature/Features/GroupTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Features/GroupTest.php b/tests/Feature/Features/GroupTest.php index 050af6c..e6056cb 100644 --- a/tests/Feature/Features/GroupTest.php +++ b/tests/Feature/Features/GroupTest.php @@ -3,6 +3,8 @@ * GroupTest class file. * * @package wp-type-extensions + * + * phpcs:disable Squiz.PHP.DisallowMultipleAssignments.Found, WordPress.Security.ValidatedSanitizedInput */ namespace Alley\WP\Tests\Feature\Features; @@ -16,6 +18,9 @@ * Tests for Group class. */ class GroupTest extends Test_Case { + /** + * Test that group can boot a set of features. + */ public function test_it_can_boot_a_group_of_features(): void { $_SERVER['booted_quick_feature'] = false; $_SERVER['booted_feature'] = false; @@ -24,7 +29,10 @@ public function test_it_can_boot_a_group_of_features(): void { $group = new Group( new Quick_Feature( fn () => $_SERVER['booted_quick_feature'] = true ), - new class implements Feature { + new class() implements Feature { + /** + * Boot the feature. + */ public function boot(): void { $_SERVER['booted_feature'] = true; }