-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFeature.php
More file actions
89 lines (79 loc) · 2.27 KB
/
Copy pathAbstractFeature.php
File metadata and controls
89 lines (79 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Abstract Feature.
*
* @package rtCamp\WPFramework\Contracts\Abstracts
* @since 1.0.0
*/
declare( strict_types = 1 );
namespace rtCamp\WPFramework\Contracts\Abstracts;
use rtCamp\WPFramework\Contracts\Interfaces\ConditionallyRegistrable;
use rtCamp\WPFramework\Utils\FeatureSelector;
/**
* Class AbstractFeature
*
* Base class for feature-flag-gated services. Each concrete feature extends a
* consumer-defined intermediate that supplies the shared FeatureSelector
* registry via get_feature_registry().
*
* On construction the feature self-registers its slug, display name, and
* description into the registry so the admin settings page discovers it
* automatically. can_register() reads the same registry to decide whether
* register_hooks() should run.
*
* @since 1.0.0
*/
abstract class AbstractFeature implements ConditionallyRegistrable {
/**
* Return the slug that identifies this feature flag.
*
* @return string Feature flag slug.
*/
abstract protected function get_slug(): string;
/**
* Return the shared FeatureSelector registry for this consumer.
*
* @return FeatureSelector Shared registry instance.
*/
abstract protected function get_feature_registry(): FeatureSelector;
/**
* Return the human-readable label shown on the settings page.
*
* Defaults to a title-cased version of the slug ("author-bio" → "Author Bio").
* Override to provide a more descriptive name.
*
* @return string Display name.
*/
protected function get_name(): string {
return ucwords( str_replace( [ '-', '_' ], ' ', $this->get_slug() ) );
}
/**
* Return the description shown beneath the label on the settings page.
*
* Defaults to empty. Override to describe what enabling this feature does.
*
* @return string Description.
*/
protected function get_description(): string {
return '';
}
/**
* Constructor. Self-registers the flag into the shared registry with metadata.
*/
public function __construct() {
$this->get_feature_registry()->register(
[
$this->get_slug() => [
'name' => $this->get_name(),
'description' => $this->get_description(),
],
]
);
}
/**
* {@inheritDoc}
*/
public function can_register(): bool {
return $this->get_feature_registry()->is_enabled( $this->get_slug() );
}
}