-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathAbstract_Feature.php
More file actions
290 lines (259 loc) · 6.88 KB
/
Abstract_Feature.php
File metadata and controls
290 lines (259 loc) · 6.88 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Abstract Feature base class.
*
* @package WordPress\AI\Abstracts
*/
declare( strict_types=1 );
namespace WordPress\AI\Abstracts;
use InvalidArgumentException;
use WordPress\AI\Contracts\Feature;
use WordPress\AI\Features\Feature_Category;
use WordPress\AI\Settings\Settings_Registration;
/**
* Base implementation for features.
*
* Provides common functionality for all features including enable/disable state.
*
* @since 0.6.0
*/
abstract class Abstract_Feature implements Feature {
/**
* Feature identifier.
*
* @since 0.6.0
* @var non-empty-string
*/
protected string $id;
/**
* Feature label.
*
* @since 0.6.0
* @var non-empty-string
*/
protected string $label;
/**
* Feature description.
*
* @since 0.6.0
* @var non-empty-string
*/
protected string $description;
/**
* Feature category.
*
* @since 0.6.0
* @var non-empty-string
*/
protected string $category;
/**
* Cache for this feature's enabled status.
*
* @since 0.6.0
* @var bool|null
*/
private ?bool $enabled_cache = null;
/**
* The feature stability level.
* @var 'deprecated'|'experimental'|'stable'
*/
private string $stability;
/**
* Constructor.
*
* Loads feature metadata and initializes properties.
*
* @since 0.6.0
*
* @throws \InvalidArgumentException If feature metadata is invalid.
*/
final public function __construct() {
$this->id = static::get_id();
if ( empty( $this->id ) ) {
throw new InvalidArgumentException(
esc_html__( 'Invalid Feature id returned by ::get_id().', 'ai' )
);
}
$metadata = $this->load_metadata();
if ( empty( $metadata['label'] ) ) {
throw new InvalidArgumentException(
esc_html__( 'Feature label is required in load_metadata().', 'ai' )
);
}
if ( empty( $metadata['description'] ) ) {
throw new InvalidArgumentException(
esc_html__( 'Feature description is required in load_metadata().', 'ai' )
);
}
if ( empty( $metadata['category'] ) ) {
$metadata['category'] = Feature_Category::OTHER;
}
$this->label = $metadata['label'];
$this->description = $metadata['description'];
$this->category = $metadata['category'];
$this->stability = $metadata['stability'] ?? 'experimental';
}
/**
* Loads feature metadata.
*
* Must return an array with keys: label, description.
* Optionally includes: category, stability.
*
* @since 0.6.0
*
* @return array{
* label: string,
* description: string,
* category?: string,
* stability?: 'deprecated'|'experimental'|'stable',
* } Feature metadata.
*/
abstract protected function load_metadata(): array;
/**
* {@inheritDoc}
*/
public function get_label(): string {
return $this->label;
}
/**
* {@inheritDoc}
*/
public function get_description(): string {
return $this->description;
}
/**
* {@inheritDoc}
*/
public function get_category(): string {
return $this->category;
}
/**
* {@inheritDoc}
*
* Features require both the global toggle and individual feature toggle to be enabled.
* Results are cached per instance to avoid redundant option lookups and filter calls.
*/
final public function is_enabled(): bool {
// Return cached result if available.
if ( null !== $this->enabled_cache ) {
return $this->enabled_cache;
}
// Check global features toggle first.
$global_enabled = (bool) get_option( Settings_Registration::GLOBAL_OPTION, false );
if ( ! $global_enabled ) {
$this->enabled_cache = false;
return false;
}
// Check feature-specific option.
$feature_enabled = (bool) get_option( "wpai_feature_{$this->id}_enabled", false );
// @todo remove in v1.0
$is_enabled = (bool) apply_filters_deprecated(
"ai_experiments_experiment_{$this->id}_enabled",
array( $feature_enabled ),
'0.6.0',
"wpai_feature_{$this->id}_enabled",
esc_html__( 'This will be removed in v1.0', 'ai' )
);
/**
* Filters the enabled status for a specific feature.
*
* The dynamic portion of the hook name, `$this->id`, refers to the feature ID.
*
* @since 0.6.0
*
* @param bool $feature_enabled Whether the feature is enabled.
*/
$is_enabled = (bool) apply_filters( "wpai_feature_{$this->id}_enabled", $is_enabled );
// Cache the result.
$this->enabled_cache = $is_enabled;
return $is_enabled;
}
/**
* {@inheritDoc}
*/
final public function get_stability(): string {
return $this->stability;
}
/**
* Registers feature-specific settings.
*
* Override this method in child classes to register custom settings options
* using WordPress Settings API (register_setting).
*
* @since 0.6.0
*
* @return void
*/
public function register_settings(): void {
// Default implementation does nothing.
// Child classes can override to register custom settings.
}
/**
* Gets the field definitions for feature-specific settings.
*
* Override this method in child classes to declare custom settings fields
* that will be rendered as a DataForm on the settings page. Each field
* should use the short option name (e.g. 'strategy'), not the full
* namespaced option name.
*
* @since x.x.x
*
* @return array<int, array{
* id: string,
* label: string,
* type: string,
* default?: mixed,
* elements?: list<array{value: string, label: string}>,
* isValid?: array{min?: int, max?: int},
* }> Array of field definitions matching the DataForm Field shape.
*/
public function get_settings_fields(): array {
return array();
}
/**
* Gets field definitions with fully resolved option names.
*
* Transforms the short field IDs from get_settings_fields() into
* full WordPress option names suitable for the REST API and frontend.
*
* @since x.x.x
*
* @return array<int, array{
* id: string,
* label: string,
* type: string,
* default?: mixed,
* elements?: list<array{value: string, label: string}>,
* isValid?: array{min?: int, max?: int},
* }> Array of field definitions with full option names.
*/
public function get_settings_fields_metadata(): array {
$fields = $this->get_settings_fields();
foreach ( $fields as &$field ) {
$field['id'] = $this->get_field_option_name( $field['id'] );
}
unset( $field );
return $fields;
}
/**
* Gets the option name for a custom feature setting field.
*
* Generates a properly namespaced option name for feature-specific settings.
* Use this when registering and storing custom settings fields to ensure
* consistent naming across the plugin.
*
* @since 0.6.0
*
* @param string $option_name The base option name (e.g., 'api_key', 'temperature').
* @return string The fully namespaced option name.
*/
final protected function get_field_option_name( string $option_name ): string {
return "wpai_feature_{$this->id}_field_{$option_name}";
}
/**
* {@inheritDoc}
*
* Must be implemented by child classes to set up hooks and functionality.
*/
abstract public function register(): void;
}