Skip to content

Commit adb5756

Browse files
committed
dev: add support for experiment deprecation
1 parent e1d2ff0 commit adb5756

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

src/Experimental/Experiment/AbstractExperiment.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ abstract class AbstractExperiment {
2222
* @var ?string
2323
*/
2424
protected static $slug;
25+
2526
/**
2627
* The experiment's configuration.
2728
*
28-
* @var ?array{title:string,description:string}
29+
* @var ?array{title:string,description:string,deprecationMessage?:?string}
2930
*/
3031
protected $config;
3132

@@ -44,7 +45,7 @@ abstract protected static function slug(): string;
4445
/**
4546
* Defines the experiment configuration.
4647
*
47-
* @return array{title:string,description:string}
48+
* @return array{title:string,description:string,deprecationMessage?:?string}
4849
*/
4950
abstract protected function config(): array;
5051

@@ -67,6 +68,19 @@ public function load(): void {
6768

6869
$this->init();
6970

71+
// Log a warning if the experiment is deprecated.
72+
$deprecated_message = $this->get_deprecation_message();
73+
if ( ! empty( $deprecated_message ) ) {
74+
graphql_debug(
75+
sprintf(
76+
// translators: %1$s: The experiment's slug, %2$s: The deprecation message.
77+
__( 'The experiment %1$s is deprecated: %2$s', 'wp-graphql' ),
78+
esc_html( static::get_slug() ),
79+
esc_html( $deprecated_message )
80+
)
81+
);
82+
}
83+
7084
/**
7185
* Fires after the experiment is loaded.
7286
*
@@ -78,7 +92,7 @@ public function load(): void {
7892
/**
7993
* Gets the experiment's configuration array.
8094
*
81-
* @return array{title:string,description:string}
95+
* @return array{title:string,description:string,deprecationMessage?:?string}
8296
*/
8397
public function get_config(): array {
8498
if ( ! isset( $this->config ) ) {
@@ -146,10 +160,26 @@ public function is_active(): bool {
146160
return $this->is_active;
147161
}
148162

163+
/**
164+
* Gets the deprecation message, if it exists.
165+
*/
166+
public function get_deprecation_message(): ?string {
167+
$config = $this->get_config();
168+
169+
return isset( $config['deprecationMessage'] ) ? $config['deprecationMessage'] : null;
170+
}
171+
172+
/**
173+
* Checks whether the experiment has been deprecated.
174+
*/
175+
public function is_deprecated(): bool {
176+
return ! empty( $this->get_deprecation_message() );
177+
}
178+
149179
/**
150180
* Prepares the configuration.
151181
*
152-
* @return array{title:string,description:string}
182+
* @return array{title:string,description:string,deprecationMessage?:?string}
153183
*
154184
* @throws \Exception If the experiment is missing a slug.
155185
*/
@@ -209,5 +239,15 @@ protected function validate_config( array $config ): void {
209239
)
210240
);
211241
}
242+
243+
if ( isset( $config['deprecationMessage'] ) && ( ! is_string( $config['deprecationMessage'] ) || empty( $config['deprecationMessage'] ) ) ) {
244+
throw new \Exception(
245+
sprintf(
246+
/* translators: %s: The experiment's class name. */
247+
esc_html__( 'The experiment %s has an invalid deprecation message in the configuration. If you are trying to deprecate the Experiment, ensure a valid `deprecationMessage` is defined in the ::config() method. Otherwise remove the `deprecationMessage` from the array', 'wp-graphql' ),
248+
static::class
249+
)
250+
);
251+
}
212252
}
213253
}

0 commit comments

Comments
 (0)