-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add Beta Feature Flags (#7242)
- Loading branch information
Showing
8 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Give\BetaFeatures\Actions; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class RegisterSettingSection | ||
{ | ||
public function __invoke($sections) | ||
{ | ||
$sections['beta'] = __('Beta Features', 'give'); | ||
return $sections; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Give\BetaFeatures\Actions; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class RegisterSettings | ||
{ | ||
public function __invoke($settings) | ||
{ | ||
if('beta' !== give_get_current_setting_section()) { | ||
return $settings; | ||
} | ||
|
||
return $this->getSettings(); | ||
} | ||
|
||
protected function getSettings() | ||
{ | ||
return [ | ||
['id' => 'give_title_beta_features_1', 'type' => 'title'], | ||
[ | ||
'name' => __('Event Tickets', 'give'), | ||
'desc' => __( | ||
'If enabled, you’ll be get access to the event tickets feature where you can create and manage events, and link them to your donation form. Since this is in a beta, your feedback is crucial to help us improve and make the experience better before making it public.', | ||
'give' | ||
), | ||
'id' => 'enable_event_tickets', | ||
'type' => 'radio_inline', | ||
'default' => 'enabled', | ||
'options' => [ | ||
'enabled' => __('Enabled', 'give'), | ||
'disabled' => __('Disabled', 'give'), | ||
], | ||
], | ||
['id' => 'give_title_beta_features_2', 'type' => 'sectionend'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Give\BetaFeatures\Facades; | ||
|
||
use Give\Framework\Support\Facades\Facade; | ||
|
||
/** | ||
* @method static bool eventTickets() | ||
* @method static bool enabled(string $feature) | ||
*/ | ||
class FeatureFlag extends Facade | ||
{ | ||
protected function getFacadeAccessor() | ||
{ | ||
return \Give\BetaFeatures\Repositories\FeatureFlagRepository::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Give\BetaFeatures\Repositories; | ||
|
||
class FeatureFlagRepository | ||
{ | ||
public function eventTickets(): bool | ||
{ | ||
return $this->enabled('event_tickets', true); | ||
} | ||
|
||
public function enabled($feature, $default = false): bool | ||
{ | ||
// Workaround so that the updated option is available at the start of the request. | ||
$option = isset($_POST["enable_$feature"]) | ||
? give_clean($_POST["enable_$feature"]) | ||
: give_get_option("enable_$feature", $default); | ||
|
||
return give_is_setting_enabled($option); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Give\BetaFeatures; | ||
|
||
use Give\Helpers\Hooks; | ||
use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class ServiceProvider implements ServiceProviderInterface | ||
{ | ||
/** | ||
* @unreleased | ||
* @inheritDoc | ||
*/ | ||
public function register(): void | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* @inheritDoc | ||
*/ | ||
public function boot(): void | ||
{ | ||
Hooks::addFilter('give_get_settings_general', Actions\RegisterSettings::class); | ||
Hooks::addFilter('give_get_sections_general', Actions\RegisterSettingSection::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
tests/Unit/BetaFeatures/Repositories/FeatureFlagRepositoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Give\Tests\Unit\BetaFeatures\Repositories; | ||
|
||
use Give\BetaFeatures\Facades\FeatureFlag; | ||
use Give\Tests\TestCase; | ||
use Give\Tests\TestTraits\RefreshDatabase; | ||
|
||
class FeatureFlagRepositoryTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function testEventTicketsShouldBeEnabledByDefault() | ||
{ | ||
$this->assertTrue(FeatureFlag::eventTickets()); | ||
} | ||
|
||
public function testShouldReturnDisabledWhenNotSet() | ||
{ | ||
$this->assertFalse(FeatureFlag::enabled('my_feature')); | ||
} | ||
|
||
public function testShouldReturnDisabledWhenNotEnabled() | ||
{ | ||
give_update_option('enable_my_feature', false); | ||
|
||
$this->assertFalse(FeatureFlag::enabled('my_feature')); | ||
} | ||
|
||
public function testShouldReturnEnabledWhenEnabled() | ||
{ | ||
give_update_option('enable_my_feature', true); | ||
|
||
$this->assertTrue(FeatureFlag::enabled('my_feature')); | ||
} | ||
|
||
public function testShouldReturnDisabledWhenSetInGlobal() | ||
{ | ||
$_POST['enable_my_feature'] = 'disabled'; | ||
|
||
$this->assertFalse(FeatureFlag::enabled('my_feature')); | ||
} | ||
|
||
public function testShouldReturnEnabledWhenSetInGlobal() | ||
{ | ||
$_POST['enable_my_feature'] = 'enabled'; | ||
|
||
$this->assertTrue(FeatureFlag::enabled('my_feature')); | ||
} | ||
} |