From aed6eca871ad2df76d695bdeaed71589477b45ce Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Fri, 1 Mar 2024 11:54:33 -0500 Subject: [PATCH] Adding a CLI command --- CHANGELOG.md | 4 ++ inc/class-cli.php | 128 +++++++++++++++++++++++++++++++++++ wp-experimental-features.php | 4 ++ 3 files changed, 136 insertions(+) create mode 100644 inc/class-cli.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b6e2676..4b7fc88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 1.3.1 - 2024-03-01 + +- Added a CLI command to control feature flags. + ## 1.3.0 - 2024-02-16 - Added REST API endpoint for features. diff --git a/inc/class-cli.php b/inc/class-cli.php new file mode 100644 index 0000000..8c2cf1a --- /dev/null +++ b/inc/class-cli.php @@ -0,0 +1,128 @@ + $label ) { + $flag = WP_CLI::colorize( '%B' . $flag . '%n' ); + + /** + * Filters the status of an experimental feature. + */ + $status = (bool) apply_filters( 'experimental_features_flag', false, $flag ); + + $status = $status + ? WP_CLI::colorize( '%Genabled%n' ) + : WP_CLI::colorize( '%Rdisabled%n' ); + + WP_CLI::line( "{$label} ({$flag}): {$status}" ); + } + + return 0; + } + + /** + * Enable an experimental feature by slug. + * + * @synopsis + * + * @param array $args The feature slug. + */ + public function enable( array $args ): int { + [ $feature ] = $args; + + $flags = Filter::flags(); + + if ( ! isset( $flags[ $feature ] ) ) { + WP_CLI::error( "Unknown feature: {$feature}. Use `wp experimental-features list` to see available features. Ensure you are passing the feature slug." ); + } + + if ( Filter::enable_flag( $feature ) ) { + WP_CLI::success( "Enabled feature: {$feature}" ); + } else { + WP_CLI::error( "Failed to enable feature: {$feature}" ); + } + + return 0; + } + + /** + * Disable an experimental feature by slug. + * + * @synopsis + * + * @param array $args The feature to disable. + */ + public function disable( array $args ): int { + [ $feature ] = $args; + + $flags = Filter::flags(); + + if ( ! isset( $flags[ $feature ] ) ) { + WP_CLI::error( "Unknown feature: {$feature}. Use `wp experimental-features list` to see available features. Ensure you are passing the feature slug." ); + } + + if ( Filter::disable_flag( $feature ) ) { + WP_CLI::success( "Disabled feature: {$feature}" ); + } else { + WP_CLI::error( "Failed to disable feature: {$feature}" ); + } + + return 0; + } + + /** + * Toggle an experimental feature by slug. + * + * @synopsis + * + * @param array $args The feature to toggle. + */ + public function toggle( array $args ): int { + [ $feature ] = $args; + + $flags = Filter::flags(); + + if ( ! isset( $flags[ $feature ] ) ) { + WP_CLI::error( "Unknown feature: {$feature}. Use `wp experimental-features list` to see available features. Ensure you are passing the feature slug." ); + } + + if ( ! Filter::toggle_flag( $feature ) ) { + WP_CLI::error( "Failed to toggle feature: {$feature}" ); + } + + $status = (bool) apply_filters( 'experimental_features_flag', false, $feature ); + + $status = $status + ? WP_CLI::colorize( '%Genabled%n' ) + : WP_CLI::colorize( '%Rdisabled%n' ); + + WP_CLI::success( "Toggled feature: {$feature} ({$status})" ); + + return 0; + } +} + +WP_CLI::add_command( 'experimental-features', CLI::class ); diff --git a/wp-experimental-features.php b/wp-experimental-features.php index 894ce21..f20d99d 100644 --- a/wp-experimental-features.php +++ b/wp-experimental-features.php @@ -21,3 +21,7 @@ Filter::init(); Options::init(); REST_API::init(); + +if ( defined( 'WP_CLI' ) && WP_CLI ) { + require_once __DIR__ . '/inc/class-cli.php'; +}