initial
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Feature_Flags;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Gravity_SMTP;
|
||||
|
||||
class Feature_Flag_Manager {
|
||||
|
||||
protected $repo;
|
||||
|
||||
public function __construct( Feature_Flag_Repository $repo ) {
|
||||
$this->repo = $repo;
|
||||
}
|
||||
|
||||
public function __call( $name, $arguments ) {
|
||||
if ( ! method_exists( $this->repo, $name ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return call_user_func_array( array( $this->repo, $name ), $arguments );
|
||||
}
|
||||
|
||||
public static function __callStatic( $name, $arguments ) {
|
||||
$self = Gravity_SMTP::$container->get( Feature_Flags_Service_Provider::FEATURE_FLAG_MANAGER );
|
||||
if ( ! method_exists( $self->repo, $name ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return call_user_func_array( array( $self->repo, $name ), $arguments );
|
||||
}
|
||||
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Feature_Flags;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Gravity_SMTP;
|
||||
|
||||
class Feature_Flag_Repository {
|
||||
|
||||
const FILTER_ALL_FEATURE_FLAGS = 'gravitysmtp_feature_flags';
|
||||
const FILTER_SINGLE_FEATURE_FLAG = 'gravitysmtp_feature_flag';
|
||||
|
||||
public function flags() {
|
||||
return apply_filters( self::FILTER_ALL_FEATURE_FLAGS, array() );
|
||||
}
|
||||
|
||||
public function add( $flag_slug, $flag_label ) {
|
||||
add_filter( self::FILTER_ALL_FEATURE_FLAGS, function( $flags ) use ( $flag_slug, $flag_label ) {
|
||||
$flags[ $flag_slug ] = $flag_label;
|
||||
|
||||
return $flags;
|
||||
} );
|
||||
}
|
||||
|
||||
public function is_enabled( $flag_slug ) {
|
||||
return apply_filters( self::FILTER_SINGLE_FEATURE_FLAG, false, $flag_slug );
|
||||
}
|
||||
|
||||
public function enable_flag( $flag_slug, $priority = PHP_INT_MAX ) {
|
||||
add_filter( self::FILTER_SINGLE_FEATURE_FLAG, function ( $is_enabled, $checked_flag ) use ( $flag_slug ) {
|
||||
if ( $checked_flag === $flag_slug ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $is_enabled;
|
||||
}, $priority, 2 );
|
||||
}
|
||||
|
||||
public function disable_flag( $flag_slug, $priority = PHP_INT_MAX ) {
|
||||
add_filter( self::FILTER_SINGLE_FEATURE_FLAG, function ( $is_enabled, $checked_flag ) use ( $flag_slug ) {
|
||||
if ( $checked_flag === $flag_slug ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $is_enabled;
|
||||
}, $priority, 2 );
|
||||
}
|
||||
|
||||
public function enabled_flags() {
|
||||
$all_flags = $this->flags();
|
||||
$self = $this;
|
||||
|
||||
return array_filter( $all_flags, function ( $flag_slug ) use ( $self ) {
|
||||
return $this->is_enabled( $flag_slug );
|
||||
}, ARRAY_FILTER_USE_KEY );
|
||||
}
|
||||
|
||||
public function flags_by_status() {
|
||||
$all_flags = $this->flags();
|
||||
$response = array();
|
||||
|
||||
foreach ( $all_flags as $flag_slug => $label ) {
|
||||
$response[ $flag_slug ] = $this->is_enabled( $flag_slug );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Feature_Flags;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Feature_Flags\Config\Feature_Flags_Config;
|
||||
use Gravity_Forms\Gravity_Tools\Providers\Config_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Tools\Service_Container;
|
||||
|
||||
class Feature_Flags_Service_Provider extends Config_Service_Provider {
|
||||
|
||||
const FEATURE_FLAG_REPOSITORY = 'feature_flag_repository';
|
||||
const FEATURE_FLAG_MANAGER = 'feature_flag_manager';
|
||||
|
||||
const FEATURE_FLAGS_CONFIG = 'feature_flags_config';
|
||||
|
||||
protected $configs = array(
|
||||
self::FEATURE_FLAGS_CONFIG => Feature_Flags_Config::class,
|
||||
);
|
||||
|
||||
public function register( Service_Container $container ) {
|
||||
parent::register( $container );
|
||||
|
||||
$container->add( self::FEATURE_FLAG_REPOSITORY, function () {
|
||||
return new Feature_Flag_Repository();
|
||||
} );
|
||||
|
||||
$container->add( self::FEATURE_FLAG_MANAGER, function () use ( $container ) {
|
||||
return new Feature_Flag_Manager( $container->get( self::FEATURE_FLAG_REPOSITORY ) );
|
||||
} );
|
||||
}
|
||||
|
||||
public function init( \Gravity_Forms\Gravity_Tools\Service_Container $container ) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Feature_Flags\Config;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Feature_Flags\Feature_Flag_Manager;
|
||||
use Gravity_Forms\Gravity_Tools\Config;
|
||||
|
||||
class Feature_Flags_Config extends Config {
|
||||
|
||||
protected $script_to_localize = 'gravitysmtp_scripts_admin';
|
||||
protected $name = 'gravitysmtp_admin_config';
|
||||
|
||||
public function should_enqueue() {
|
||||
return is_admin();
|
||||
}
|
||||
|
||||
public function data() {
|
||||
return array(
|
||||
'common' => array(
|
||||
'feature_flags' => array(
|
||||
'data' => array(
|
||||
'all' => Feature_Flag_Manager::flags(),
|
||||
'enabled' => Feature_Flag_Manager::enabled_flags(),
|
||||
'statuses' => Feature_Flag_Manager::flags_by_status(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user