Skip to content

Commit

Permalink
Adding a CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Mar 1, 2024
1 parent fc897dc commit aed6eca
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
128 changes: 128 additions & 0 deletions inc/class-cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* CLI class file
*
* @package Experimental_Features
*/

namespace Experimental_Features;

use WP_CLI;

/**
* CLI class
*/
class CLI {
/**
* List all experimental features
*/
public function list(): int {
$flags = Filter::flags();

if ( empty( $flags ) ) {
WP_CLI::line( 'No experimental features found.' );

return 0;
}

foreach ( $flags as $flag => $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 <feature>
*
* @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 <feature>
*
* @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 <feature>
*
* @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 );
4 changes: 4 additions & 0 deletions wp-experimental-features.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@
Filter::init();
Options::init();
REST_API::init();

if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once __DIR__ . '/inc/class-cli.php';
}

0 comments on commit aed6eca

Please sign in to comment.